OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <openssl/dsa.h> | |
6 #include <openssl/ecdsa.h> | |
7 #include <openssl/err.h> | |
8 #include <openssl/evp.h> | |
9 #include <openssl/rsa.h> | |
10 #include <openssl/x509.h> | |
11 | |
12 #include "base/android/jni_android.h" | |
13 #include "base/android/jni_array.h" | |
14 #include "base/android/scoped_java_ref.h" | |
15 #include "base/basictypes.h" | |
16 #include "base/bind.h" | |
17 #include "base/callback.h" | |
18 #include "base/compiler_specific.h" | |
19 #include "crypto/openssl_util.h" | |
20 #include "jni/AndroidKeyStoreTestUtil_jni.h" | |
21 #include "net/android/keystore.h" | |
22 #include "net/android/keystore_openssl.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 using base::android::AttachCurrentThread; | |
26 using base::android::ScopedJavaLocalRef; | |
27 using base::android::ToJavaByteArray; | |
28 | |
29 // Technical note: | |
30 // | |
31 // This source file not only checks that signing with SignWithPrivateKey() | |
32 // works correctly, it also verifies that the generated signature matches | |
33 // 100% of what OpenSSL generates when calling RSA_sign(NID_md5_sha1,...), | |
34 // DSA_sign(0, ...) or ECDSA_sign(0, ...). | |
35 // | |
36 // That's crucial to ensure that this function can later be used to | |
37 // implement client certificate support. More specifically, that it is | |
38 // possible to create a custom EVP_PKEY that uses SignWithPrivateKey() | |
39 // internally to perform RSA/DSA/ECDSA signing, as invoked by the | |
40 // OpenSSL code at ssl/s3_clnt.c:ssl3_send_client_verify(). | |
41 // | |
42 // Finally, it also checks that using the EVP_PKEY generated with | |
43 // GetOpenSSLPrivateKeyWrapper() works too as well. | |
44 // | |
45 | |
46 namespace net { | |
47 namespace android { | |
48 | |
49 namespace { | |
50 | |
51 JNIEnv* InitEnv() { | |
52 JNIEnv* env = AttachCurrentThread(); | |
53 static bool inited = false; | |
54 if (!inited) { | |
55 RegisterNativesImpl(env); | |
56 inited = true; | |
57 } | |
58 return env; | |
59 } | |
60 | |
61 // Returns true if running on an Android version older than 4.2 | |
62 bool IsOnAndroidOlderThan_4_2(void) { | |
63 JNIEnv* env = InitEnv(); | |
64 const int kAndroid42ApiLevel = 16; | |
65 int level = Java_AndroidKeyStoreTestUtil_GetSdkApiLevel(env); | |
66 return level < kAndroid42ApiLevel; | |
67 } | |
68 | |
69 // Implements the callback expected by ERR_print_errors_cb(). | |
70 // used by GetOpenSSLErrorString. | |
71 int openssl_print_error_callback(const char* msg, size_t msglen, void* u) { | |
72 std::string* result = reinterpret_cast<std::string*>(u); | |
73 result->append(msg, msglen); | |
74 return 1; | |
75 } | |
76 | |
77 // Retrieve OpenSSL error as a string | |
78 std::string GetOpenSSLErrorString(void) { | |
79 std::string result; | |
80 ERR_print_errors_cb(openssl_print_error_callback, &result); | |
81 return result; | |
82 } | |
83 | |
84 // Retrieve a JNI local ref from encoded PKCS#8 data. | |
85 ScopedJavaLocalRef<jobject> GetPKCS8PrivateKey( | |
86 SSLClientCertType key_type, | |
87 const unsigned char* pkcs8_bytes, | |
88 size_t pkcs8_size) { | |
89 JNIEnv* env = InitEnv(); | |
90 ScopedJavaLocalRef<jbyteArray> bytes( | |
91 ToJavaByteArray(env, | |
92 reinterpret_cast<const uint8*>(pkcs8_bytes), | |
93 pkcs8_size)); | |
94 | |
95 ScopedJavaLocalRef<jobject> key( | |
96 Java_AndroidKeyStoreTestUtil_CreatePrivateKeyFromPKCS8( | |
97 env, key_type, bytes.obj())); | |
98 | |
99 return key; | |
100 } | |
101 | |
102 // Create an OpenSSL EVP_PKEY object from a PKCS#8 in-memory content. | |
103 EVP_PKEY* GetOpenSSLPKCS8PrivateKey(int type, | |
104 const unsigned char* key_bytes, | |
105 size_t key_size) { | |
106 const unsigned char* p = key_bytes; | |
107 long p_length = static_cast<long>(key_size); | |
108 return d2i_PrivateKey(type, NULL, &p, p_length); | |
109 } | |
110 | |
111 // A simple test RSA 2048-bits key, in PKCS#8 format, the following was | |
112 // generated with: | |
113 // | |
114 // openssl genrsa -out key.pem 2048 | |
115 // openssl pkcs8 -topk8 -inform PEM -outform DER -in key.pem | |
116 // -out key.pkcs8 -nocrypt | |
117 // xxd -i key.pkcs8 | |
118 // | |
119 const unsigned char test_rsa_key_pkcs8[] = { | |
Ryan Sleevi
2013/01/26 01:51:57
Why can you not just keep these as separate files,
digit1
2013/01/28 10:16:30
Yes, I'll move the data to files.
| |
120 0x30, 0x82, 0x04, 0xbd, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, | |
121 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, | |
122 0x04, 0xa7, 0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, | |
123 0x01, 0x00, 0xc0, 0x41, 0x18, 0x86, 0x1c, 0xe3, 0x60, 0xd7, 0xec, 0x38, | |
124 0x70, 0xd0, 0x35, 0x64, 0x6e, 0x0e, 0x4f, 0xaf, 0x3f, 0x05, 0x57, 0x05, | |
125 0xc3, 0xe9, 0x8d, 0xc3, 0x92, 0x14, 0xa1, 0xea, 0xab, 0xe3, 0x5f, 0x61, | |
126 0x70, 0x9a, 0xd7, 0x43, 0x2f, 0xc2, 0x31, 0xd7, 0x8c, 0x65, 0x89, 0xea, | |
127 0x8b, 0xdb, 0xa7, 0x9f, 0x9e, 0xba, 0xea, 0x48, 0x7d, 0x71, 0x10, 0x11, | |
128 0x86, 0x9d, 0x55, 0x68, 0xd4, 0x31, 0xa0, 0x33, 0x06, 0x5f, 0x0f, 0x43, | |
129 0xe5, 0x52, 0x46, 0x8d, 0xc1, 0x26, 0xb2, 0x08, 0xd6, 0xa5, 0x9f, 0x85, | |
130 0x24, 0xb0, 0x4d, 0x7e, 0x2a, 0x7b, 0x4e, 0xcd, 0x54, 0xe5, 0x11, 0xf8, | |
131 0x39, 0xf5, 0x13, 0x5c, 0xb4, 0xff, 0x07, 0x82, 0xaf, 0xdc, 0x05, 0x27, | |
132 0x35, 0x5a, 0x48, 0xc9, 0xa0, 0x3b, 0xe5, 0x03, 0x33, 0x32, 0x9a, 0x60, | |
133 0xe7, 0x0d, 0xce, 0x23, 0xb7, 0xbf, 0x6e, 0x9e, 0x14, 0x6f, 0xe0, 0x5f, | |
134 0x36, 0x98, 0xdd, 0x1e, 0xde, 0x29, 0x51, 0x2c, 0x10, 0xc1, 0x4b, 0x07, | |
135 0x98, 0x60, 0x51, 0x88, 0x75, 0x76, 0x4b, 0xcb, 0x19, 0x22, 0x0c, 0x4e, | |
136 0xc5, 0x5f, 0xb0, 0x40, 0xb7, 0x6b, 0x5b, 0x45, 0xd7, 0x5d, 0xaa, 0x09, | |
137 0xe2, 0xdc, 0x8f, 0x6c, 0x07, 0xa7, 0x75, 0x4f, 0x2c, 0x5d, 0xd8, 0xa4, | |
138 0x2f, 0xdd, 0x8c, 0xc8, 0xa6, 0x3a, 0xaf, 0x5c, 0xf5, 0x2d, 0xbb, 0x4d, | |
139 0x8c, 0xf4, 0xc7, 0x01, 0x9b, 0x95, 0x1b, 0x51, 0x3d, 0x44, 0x87, 0xa0, | |
140 0x48, 0x79, 0xc0, 0x79, 0x23, 0x98, 0x86, 0xae, 0x20, 0xa1, 0x67, 0xf0, | |
141 0x20, 0x03, 0x46, 0x8c, 0xc2, 0x96, 0xdf, 0x22, 0x70, 0xa8, 0xed, 0xef, | |
142 0x8e, 0xa9, 0x09, 0x32, 0x9d, 0xe5, 0x95, 0x53, 0x36, 0x67, 0x69, 0xfe, | |
143 0x11, 0x38, 0x74, 0xda, 0x9f, 0x7f, 0xba, 0x17, 0xd3, 0x31, 0x94, 0xd3, | |
144 0xe3, 0x66, 0xb6, 0x84, 0x79, 0x01, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, | |
145 0x82, 0x01, 0x00, 0x55, 0xc5, 0x34, 0xe1, 0xb0, 0x45, 0xa8, 0xd0, 0xeb, | |
146 0xec, 0x0a, 0x38, 0x79, 0x79, 0x82, 0xb8, 0x13, 0xc8, 0xc5, 0x3e, 0xe6, | |
147 0xa2, 0x05, 0xd3, 0x4e, 0x91, 0xaf, 0xbc, 0x50, 0xb8, 0x57, 0x53, 0x2b, | |
148 0x1c, 0x57, 0x96, 0x5c, 0xee, 0xf6, 0x81, 0x96, 0xa6, 0xe9, 0x55, 0xeb, | |
149 0x7f, 0x9e, 0x41, 0xb2, 0xb8, 0xbd, 0xa1, 0xfa, 0x1f, 0xb9, 0x07, 0x15, | |
150 0xfa, 0x1b, 0xaa, 0x59, 0x8e, 0x59, 0x0c, 0x82, 0xc2, 0x00, 0xd7, 0xac, | |
151 0x01, 0xc8, 0x6f, 0x3a, 0x56, 0xc7, 0x93, 0x31, 0xde, 0x4b, 0x94, 0xbc, | |
152 0x64, 0x34, 0x08, 0x0b, 0xaa, 0x7b, 0xdd, 0x63, 0x3c, 0xab, 0xe1, 0x3e, | |
153 0x71, 0x15, 0xba, 0x46, 0x97, 0x17, 0x90, 0xa7, 0x93, 0x20, 0x4d, 0xf2, | |
154 0x66, 0x99, 0xa0, 0xdb, 0xd4, 0x48, 0x0e, 0x30, 0x8f, 0x8a, 0xe9, 0xca, | |
155 0x81, 0xec, 0xd2, 0xf5, 0xe2, 0x6d, 0x79, 0x94, 0x2a, 0x26, 0x47, 0x34, | |
156 0xa4, 0x2a, 0xf6, 0x31, 0x78, 0x4e, 0xcc, 0xea, 0x1c, 0xf1, 0xa5, 0x64, | |
157 0x29, 0x32, 0x0d, 0xab, 0xe0, 0x34, 0x5a, 0xec, 0xb7, 0x38, 0xca, 0x96, | |
158 0x3b, 0x04, 0x42, 0xfc, 0xbc, 0xd0, 0x7b, 0x44, 0xc7, 0x10, 0xa4, 0xac, | |
159 0x02, 0x12, 0x60, 0xbd, 0x2a, 0xf5, 0x1a, 0x1f, 0xa4, 0x21, 0xc5, 0x83, | |
160 0x42, 0x25, 0xcb, 0x1a, 0x51, 0xe7, 0x38, 0xe7, 0xa8, 0x21, 0xeb, 0x33, | |
161 0xf7, 0x0a, 0x78, 0x4d, 0xf9, 0x7c, 0x39, 0x79, 0xa3, 0x36, 0x45, 0x6b, | |
162 0x68, 0x73, 0x39, 0x15, 0x4b, 0x01, 0x39, 0x45, 0xae, 0xb4, 0xd5, 0x18, | |
163 0xce, 0xab, 0x7d, 0x0b, 0x94, 0xd2, 0x45, 0x0c, 0xec, 0x88, 0x43, 0x84, | |
164 0x36, 0x3d, 0x5f, 0x4f, 0x23, 0x90, 0x86, 0x40, 0x2c, 0xd5, 0xdd, 0x93, | |
165 0x3a, 0x08, 0x16, 0xb1, 0x92, 0x9d, 0x23, 0xe2, 0xc1, 0x56, 0xd8, 0x17, | |
166 0x2b, 0x72, 0xd1, 0xa0, 0xa8, 0xfc, 0x21, 0x02, 0x81, 0x81, 0x00, 0xf0, | |
167 0x7f, 0x17, 0x65, 0x8e, 0xd1, 0x5e, 0x30, 0x14, 0xa7, 0x93, 0x37, 0x11, | |
168 0xde, 0xbe, 0x25, 0xc5, 0x5b, 0x40, 0x60, 0x94, 0x28, 0x9b, 0x88, 0x74, | |
169 0x47, 0xe2, 0x04, 0x65, 0xc6, 0x30, 0xc3, 0x3c, 0xa1, 0xa6, 0x76, 0x3a, | |
170 0xb2, 0x8e, 0x5f, 0xec, 0xb6, 0xe4, 0x06, 0x2c, 0x19, 0x24, 0x09, 0x5b, | |
171 0xc5, 0xa7, 0xf4, 0x06, 0xdb, 0xa3, 0x2b, 0x35, 0xbc, 0x28, 0x91, 0xc4, | |
172 0xff, 0x61, 0x72, 0x3f, 0xb3, 0x7e, 0x22, 0x4c, 0x52, 0xa7, 0xe3, 0x12, | |
173 0x29, 0xc4, 0xdd, 0x7f, 0x8c, 0xa6, 0x76, 0x82, 0x82, 0x91, 0x6d, 0xd7, | |
174 0xec, 0x2d, 0x2f, 0xaa, 0x6a, 0x44, 0x11, 0xdc, 0x5b, 0xa5, 0xb6, 0xfe, | |
175 0x3d, 0x2c, 0x36, 0x06, 0x84, 0x8a, 0x82, 0xbd, 0x24, 0xe5, 0x0c, 0xd6, | |
176 0x1a, 0x5f, 0x10, 0x1c, 0x92, 0x9c, 0xdb, 0xce, 0x8f, 0x9e, 0x2f, 0x3d, | |
177 0x01, 0xd2, 0x75, 0x58, 0xdd, 0x0b, 0x4d, 0x02, 0x81, 0x81, 0x00, 0xcc, | |
178 0xa5, 0xdd, 0x5f, 0x26, 0x99, 0x91, 0x3a, 0xab, 0x30, 0xf2, 0x01, 0x75, | |
179 0xe2, 0x59, 0x75, 0x98, 0xc9, 0x32, 0x89, 0x29, 0x36, 0x77, 0x7d, 0x7a, | |
180 0xa0, 0xee, 0x9d, 0xce, 0xe6, 0xe9, 0x47, 0xd0, 0x2b, 0x6b, 0x69, 0x81, | |
181 0x1b, 0xd7, 0x19, 0x04, 0x51, 0xb0, 0x72, 0x9c, 0x87, 0x41, 0x7d, 0x94, | |
182 0x01, 0x3f, 0x36, 0x10, 0x92, 0x9f, 0x2f, 0x7e, 0xf1, 0x49, 0x1f, 0x1b, | |
183 0x40, 0x9c, 0xbe, 0xa4, 0x37, 0xe8, 0xa4, 0x9c, 0xc3, 0xfd, 0xb0, 0xe0, | |
184 0x48, 0xb2, 0xd7, 0xc5, 0x41, 0xd1, 0x4e, 0xda, 0xee, 0x4c, 0xbf, 0x45, | |
185 0xa9, 0xef, 0xe3, 0x1d, 0x9d, 0x99, 0xdf, 0xa3, 0xc3, 0x55, 0x25, 0x22, | |
186 0xba, 0x32, 0xc4, 0xfe, 0xa0, 0x8f, 0x50, 0xfa, 0x05, 0x64, 0xa5, 0xd9, | |
187 0x92, 0x2b, 0x27, 0xc9, 0x96, 0x23, 0xe3, 0xe3, 0xeb, 0xde, 0x47, 0xa7, | |
188 0x3d, 0x33, 0x8d, 0xb6, 0x73, 0x02, 0x85, 0x02, 0x81, 0x80, 0x58, 0xe8, | |
189 0x63, 0x19, 0xe4, 0x66, 0x7a, 0x4f, 0x84, 0x13, 0x3f, 0x55, 0x48, 0x81, | |
190 0xf4, 0x01, 0xba, 0xa8, 0x35, 0x70, 0x7e, 0xd5, 0x54, 0x4a, 0x69, 0xd2, | |
191 0x79, 0x37, 0xee, 0xf8, 0x09, 0xe6, 0xe3, 0x6f, 0x4f, 0x3e, 0xbe, 0x0c, | |
192 0x6c, 0x9e, 0x01, 0xc0, 0xcb, 0x23, 0x8d, 0x01, 0xee, 0x54, 0x97, 0x5c, | |
193 0xc6, 0xee, 0x6b, 0xea, 0x9e, 0xb3, 0xc6, 0xb5, 0xbc, 0xb9, 0xc6, 0xfe, | |
194 0x32, 0x64, 0x2e, 0x30, 0x89, 0x1c, 0xdc, 0xe2, 0x61, 0xb6, 0x8c, 0x6c, | |
195 0x6c, 0x9f, 0x06, 0x1c, 0x55, 0x1d, 0xd2, 0xb9, 0xba, 0x51, 0xc5, 0x55, | |
196 0x46, 0x8f, 0x2c, 0x8d, 0x04, 0x85, 0x25, 0xd5, 0xab, 0xb9, 0xae, 0xdb, | |
197 0xa6, 0x90, 0x82, 0x70, 0x55, 0x54, 0x67, 0xe0, 0x4f, 0xdd, 0x22, 0xf9, | |
198 0xb4, 0xd3, 0x1b, 0xfd, 0x07, 0x88, 0x2b, 0x20, 0xe4, 0xf5, 0xc9, 0xb3, | |
199 0xf6, 0xbd, 0xf3, 0x10, 0x24, 0xb1, 0x02, 0x81, 0x80, 0x7b, 0x67, 0x3d, | |
200 0x51, 0x26, 0x36, 0x8e, 0x23, 0xa1, 0x9d, 0x57, 0x21, 0x58, 0x53, 0x90, | |
201 0x7c, 0x60, 0x10, 0x5a, 0xff, 0xe8, 0xb1, 0x26, 0x66, 0xac, 0xee, 0xa4, | |
202 0x54, 0xd6, 0xb1, 0xd9, 0x53, 0xeb, 0x8c, 0x73, 0x2d, 0xe0, 0xa3, 0xc8, | |
203 0x16, 0x16, 0xcb, 0xa7, 0xa9, 0xc5, 0x07, 0xae, 0x8f, 0x2a, 0x13, 0x82, | |
204 0x69, 0x78, 0x9e, 0xe1, 0x8c, 0xc3, 0x70, 0x7e, 0x16, 0x5a, 0xd9, 0xa0, | |
205 0x6b, 0x39, 0x1d, 0x59, 0x95, 0x01, 0xcf, 0x11, 0x88, 0x7a, 0x06, 0x7c, | |
206 0x89, 0xae, 0x32, 0x1d, 0x23, 0xfe, 0xd2, 0x99, 0xc6, 0xf1, 0x1c, 0x23, | |
207 0x42, 0x81, 0xd6, 0x4a, 0x36, 0x58, 0x4a, 0xee, 0x6a, 0x01, 0x41, 0xe4, | |
208 0x61, 0x73, 0xe5, 0x9f, 0xe6, 0x45, 0x8d, 0xc0, 0xfe, 0x5d, 0x6f, 0x4d, | |
209 0xc4, 0xa5, 0x43, 0x7b, 0x0a, 0xed, 0xa2, 0x8a, 0x9c, 0x0c, 0x95, 0xd4, | |
210 0x23, 0x8d, 0x34, 0x56, 0xfd, 0x02, 0x81, 0x81, 0x00, 0xe2, 0x05, 0xb1, | |
211 0xe7, 0x6b, 0xf1, 0xa9, 0xa5, 0xe1, 0x48, 0xb5, 0xe0, 0x76, 0x95, 0xa0, | |
212 0x7e, 0xb0, 0xa6, 0x32, 0x2d, 0x16, 0x07, 0xe9, 0x30, 0x90, 0xf4, 0xfe, | |
213 0x54, 0x98, 0x7c, 0xdc, 0xf5, 0x67, 0x87, 0xa1, 0x1c, 0x18, 0x49, 0x06, | |
214 0x4a, 0xaf, 0xbb, 0xbb, 0xb1, 0x0b, 0xcc, 0x3d, 0x89, 0x2a, 0x70, 0x18, | |
215 0x39, 0x92, 0x6f, 0xc4, 0xcb, 0x76, 0xe6, 0x1e, 0xd5, 0x72, 0xc1, 0x98, | |
216 0x75, 0x5c, 0xdb, 0x15, 0x01, 0x1f, 0xb7, 0xcd, 0xc2, 0x88, 0xc5, 0x2c, | |
217 0x5d, 0x1d, 0x76, 0x1c, 0xd5, 0xcd, 0xb5, 0xa0, 0x18, 0xd7, 0x0d, 0x53, | |
218 0x43, 0xb7, 0x31, 0x51, 0x30, 0x27, 0xf5, 0x60, 0x8c, 0x8e, 0x17, 0x44, | |
219 0xac, 0xeb, 0xac, 0xde, 0x39, 0x28, 0x78, 0x02, 0x95, 0xeb, 0x2f, 0xaa, | |
220 0xd8, 0x26, 0x76, 0xbd, 0xeb, 0x0e, 0x3f, 0x99, 0xe6, 0xb6, 0xf5, 0xcf, | |
221 0xd3, 0xe2, 0xf4, 0xa2, 0xab | |
222 }; | |
223 | |
224 // Retrieve a JNI local ref for our test RSA key. | |
225 ScopedJavaLocalRef<jobject> GetRSATestKey() { | |
226 return GetPKCS8PrivateKey(CLIENT_CERT_RSA_SIGN, | |
227 test_rsa_key_pkcs8, | |
228 sizeof(test_rsa_key_pkcs8)); | |
229 } | |
230 | |
231 // Retrieve the OpenSSL object for our test RSA key. | |
232 EVP_PKEY* GetRSATestKeyOpenSSL() { | |
233 return GetOpenSSLPKCS8PrivateKey(EVP_PKEY_RSA, | |
234 test_rsa_key_pkcs8, | |
235 sizeof(test_rsa_key_pkcs8)); | |
236 } | |
237 | |
238 // A simple 2048-bits DSA test key. | |
239 // | |
240 // openssl dsaparam -out dsaparam.pem 2048 | |
241 // openssl gendsa -out key.pem dsaparam.pem | |
242 // openssl pkcs8 -topk8 -inform PEM -outform DER -in key.pem key.pkcs8 | |
243 // -out key.pkcs8 -nocrypt | |
244 // xxd -i key.pkcs8 | |
245 const unsigned char test_dsa_key_pkcs8[] = { | |
246 0x30, 0x82, 0x02, 0x64, 0x02, 0x01, 0x00, 0x30, 0x82, 0x02, 0x39, 0x06, | |
247 0x07, 0x2a, 0x86, 0x48, 0xce, 0x38, 0x04, 0x01, 0x30, 0x82, 0x02, 0x2c, | |
248 0x02, 0x82, 0x01, 0x01, 0x00, 0xef, 0xea, 0xa5, 0x9b, 0xd7, 0x39, 0xd4, | |
249 0x47, 0x6e, 0x69, 0xd5, 0xb2, 0x6c, 0xce, 0xa4, 0xdd, 0x87, 0xd9, 0xc5, | |
250 0x6a, 0x6f, 0x9c, 0x00, 0x68, 0x6d, 0xa3, 0x7a, 0x5c, 0x6b, 0x88, 0xcb, | |
251 0x88, 0xc1, 0x2b, 0x3c, 0x2d, 0x2d, 0x37, 0xaf, 0x10, 0x3f, 0xba, 0x62, | |
252 0xf2, 0x4f, 0x85, 0xc2, 0xb9, 0xe4, 0xaf, 0xbe, 0x3c, 0x7c, 0xcd, 0x44, | |
253 0xdf, 0x92, 0xa7, 0x73, 0xd7, 0x9d, 0xed, 0x06, 0x1c, 0x42, 0x98, 0xab, | |
254 0xc6, 0x0a, 0x4b, 0x05, 0x50, 0x44, 0xd1, 0xec, 0xbb, 0xd3, 0x0b, 0x26, | |
255 0x45, 0xce, 0xf0, 0x71, 0xda, 0xd6, 0xe6, 0x83, 0xa3, 0x7a, 0x8d, 0x76, | |
256 0xf5, 0x21, 0xe4, 0x5e, 0x0f, 0x32, 0x44, 0x31, 0xc5, 0x2c, 0x50, 0x88, | |
257 0xe5, 0x2c, 0xec, 0x94, 0x85, 0xa3, 0xdc, 0x69, 0x2a, 0xcd, 0xa1, 0x14, | |
258 0xea, 0x9e, 0xac, 0x11, 0xd5, 0xda, 0x2c, 0x47, 0x34, 0xdf, 0xd6, 0x0f, | |
259 0x30, 0xd5, 0xa6, 0x2f, 0xc0, 0x82, 0x31, 0x38, 0xa8, 0x16, 0x68, 0x3d, | |
260 0x22, 0x11, 0x11, 0x2c, 0x03, 0x3c, 0x4e, 0x01, 0x0a, 0x8a, 0x37, 0x1c, | |
261 0x91, 0x2f, 0x11, 0x2a, 0x1c, 0x9e, 0xec, 0x25, 0x4b, 0xb7, 0xa5, 0xb5, | |
262 0x14, 0x4e, 0xcb, 0xa6, 0xfc, 0x10, 0xa0, 0x6c, 0x00, 0x0e, 0x2d, 0x8e, | |
263 0xe6, 0x69, 0x8d, 0x4e, 0x8e, 0x04, 0x84, 0xbc, 0x83, 0x83, 0xd4, 0x46, | |
264 0x07, 0x6c, 0x04, 0x96, 0x04, 0xc0, 0x72, 0xfc, 0xee, 0xaf, 0xbe, 0x6c, | |
265 0x73, 0x34, 0xd4, 0x57, 0xcf, 0xc6, 0xed, 0x01, 0xd1, 0x9f, 0xe7, 0x69, | |
266 0x7f, 0x92, 0x80, 0xf3, 0xdf, 0x96, 0xda, 0x78, 0x2c, 0x16, 0xb9, 0x4c, | |
267 0x62, 0x79, 0x37, 0x1d, 0x97, 0xb4, 0xaa, 0x77, 0xa6, 0x56, 0x6f, 0xb1, | |
268 0x36, 0xab, 0xb1, 0xd4, 0x56, 0x16, 0xc6, 0x86, 0x51, 0xe0, 0xe4, 0x22, | |
269 0x9b, 0xae, 0x99, 0xbd, 0x60, 0x0d, 0xbd, 0x9e, 0x13, 0x02, 0x21, 0x00, | |
270 0x89, 0xe8, 0x1a, 0xac, 0xbf, 0x62, 0xc5, 0x57, 0x24, 0x90, 0x83, 0xfc, | |
271 0x2f, 0xf1, 0x57, 0x2e, 0x16, 0xa5, 0x99, 0xea, 0x13, 0xc7, 0xb4, 0xe5, | |
272 0xc0, 0xf2, 0x3a, 0x38, 0x07, 0x88, 0xcc, 0x8f, 0x02, 0x82, 0x01, 0x00, | |
273 0x3c, 0x97, 0xd5, 0x97, 0xd9, 0xa5, 0xf9, 0xac, 0xa6, 0x8d, 0xf0, 0xf4, | |
274 0xd3, 0x41, 0x3a, 0x37, 0x67, 0x9d, 0xf8, 0x97, 0xa6, 0x2f, 0xe8, 0x44, | |
275 0xa3, 0x9e, 0x9c, 0xca, 0xed, 0x56, 0x2c, 0x25, 0x6e, 0x7f, 0xbb, 0x7a, | |
276 0x30, 0x52, 0x5e, 0x8b, 0xb2, 0x10, 0x22, 0xd6, 0x29, 0x88, 0xc8, 0x3b, | |
277 0x27, 0xa6, 0xfb, 0x30, 0xdf, 0xa9, 0xd8, 0x02, 0x4e, 0x51, 0xcb, 0xfd, | |
278 0x38, 0x97, 0x6d, 0x1c, 0x96, 0x4e, 0x4e, 0xd2, 0x0b, 0xba, 0x6b, 0x86, | |
279 0xfb, 0x58, 0xb9, 0xd0, 0xdd, 0xb7, 0x44, 0xc1, 0x5a, 0xec, 0xd9, 0x8f, | |
280 0x77, 0x0b, 0xc9, 0xe4, 0xef, 0xed, 0xd6, 0x62, 0x75, 0xc8, 0x99, 0xfd, | |
281 0x09, 0x20, 0xac, 0x2c, 0xc1, 0xfc, 0xd3, 0x54, 0x13, 0x99, 0x27, 0x6e, | |
282 0x6c, 0x6d, 0x0c, 0x28, 0x25, 0xe2, 0xa1, 0x4e, 0x6c, 0x47, 0x19, 0x22, | |
283 0xcf, 0x2a, 0xf9, 0x6b, 0x99, 0x03, 0x88, 0xb5, 0x7f, 0x15, 0x6e, 0x0f, | |
284 0xe3, 0x86, 0x0a, 0x02, 0x85, 0x29, 0x09, 0xcc, 0xcb, 0xa3, 0xd7, 0x95, | |
285 0x9b, 0xeb, 0x80, 0x5d, 0x4c, 0x19, 0xa8, 0x3b, 0x2e, 0x3f, 0x0a, 0x04, | |
286 0x27, 0x5b, 0x04, 0x5c, 0xa1, 0x75, 0xcb, 0x54, 0xcf, 0x3c, 0x0d, 0x42, | |
287 0x19, 0x62, 0x33, 0x08, 0x70, 0x7b, 0x78, 0x11, 0xcf, 0x6d, 0xb5, 0x8d, | |
288 0x77, 0x2b, 0xac, 0xbe, 0xde, 0x9e, 0x4a, 0x0b, 0x36, 0xa0, 0x4a, 0x34, | |
289 0x2c, 0x95, 0x92, 0x49, 0x10, 0x82, 0x4b, 0x9f, 0xe0, 0xf8, 0xb5, 0xd9, | |
290 0x32, 0x61, 0x47, 0x0f, 0xc4, 0x87, 0xc3, 0x06, 0x33, 0x9f, 0x6b, 0x28, | |
291 0xee, 0xac, 0x24, 0xbc, 0xfe, 0xba, 0xa3, 0xeb, 0x20, 0x7f, 0x67, 0x42, | |
292 0x73, 0xb3, 0xe5, 0xf9, 0xc3, 0x2f, 0x6e, 0xef, 0xb3, 0xc5, 0x52, 0x1a, | |
293 0xfb, 0x96, 0x70, 0x96, 0x47, 0x54, 0x31, 0xd4, 0x25, 0xbd, 0x29, 0x7b, | |
294 0x19, 0xa5, 0xdc, 0xb9, 0x04, 0x22, 0x02, 0x20, 0x03, 0x82, 0xb9, 0xc8, | |
295 0x3e, 0xee, 0x73, 0x81, 0x4e, 0x7c, 0xfa, 0x3d, 0xad, 0x10, 0xc4, 0x3a, | |
296 0xfc, 0x76, 0x7a, 0x1f, 0x8c, 0xf3, 0x6d, 0x40, 0xda, 0x2d, 0x1b, 0x41, | |
297 0x95, 0xcb, 0x74, 0x1d | |
298 }; | |
299 | |
300 // Retrieve a JNI local ref for our test DSA key. | |
301 ScopedJavaLocalRef<jobject> GetDSATestKey() { | |
302 return GetPKCS8PrivateKey(CLIENT_CERT_DSS_SIGN, | |
303 test_dsa_key_pkcs8, | |
304 sizeof(test_dsa_key_pkcs8)); | |
305 } | |
306 | |
307 // Retrieve the OpenSSL object for our test DSA key. | |
308 EVP_PKEY* GetDSATestKeyOpenSSL() { | |
309 return GetOpenSSLPKCS8PrivateKey(EVP_PKEY_DSA, | |
310 test_dsa_key_pkcs8, | |
311 sizeof(test_dsa_key_pkcs8)); | |
312 } | |
313 | |
314 // The DSA public key generated from the previous DSA private one, with: | |
315 // openssl dsa -in key.pem -outform DER -out key_public.der | |
316 // xxd -i key_public.der | |
317 const unsigned char test_dsa_public_der[] = { | |
318 0x30, 0x82, 0x03, 0x46, 0x30, 0x82, 0x02, 0x39, 0x06, 0x07, 0x2a, 0x86, | |
319 0x48, 0xce, 0x38, 0x04, 0x01, 0x30, 0x82, 0x02, 0x2c, 0x02, 0x82, 0x01, | |
320 0x01, 0x00, 0xef, 0xea, 0xa5, 0x9b, 0xd7, 0x39, 0xd4, 0x47, 0x6e, 0x69, | |
321 0xd5, 0xb2, 0x6c, 0xce, 0xa4, 0xdd, 0x87, 0xd9, 0xc5, 0x6a, 0x6f, 0x9c, | |
322 0x00, 0x68, 0x6d, 0xa3, 0x7a, 0x5c, 0x6b, 0x88, 0xcb, 0x88, 0xc1, 0x2b, | |
323 0x3c, 0x2d, 0x2d, 0x37, 0xaf, 0x10, 0x3f, 0xba, 0x62, 0xf2, 0x4f, 0x85, | |
324 0xc2, 0xb9, 0xe4, 0xaf, 0xbe, 0x3c, 0x7c, 0xcd, 0x44, 0xdf, 0x92, 0xa7, | |
325 0x73, 0xd7, 0x9d, 0xed, 0x06, 0x1c, 0x42, 0x98, 0xab, 0xc6, 0x0a, 0x4b, | |
326 0x05, 0x50, 0x44, 0xd1, 0xec, 0xbb, 0xd3, 0x0b, 0x26, 0x45, 0xce, 0xf0, | |
327 0x71, 0xda, 0xd6, 0xe6, 0x83, 0xa3, 0x7a, 0x8d, 0x76, 0xf5, 0x21, 0xe4, | |
328 0x5e, 0x0f, 0x32, 0x44, 0x31, 0xc5, 0x2c, 0x50, 0x88, 0xe5, 0x2c, 0xec, | |
329 0x94, 0x85, 0xa3, 0xdc, 0x69, 0x2a, 0xcd, 0xa1, 0x14, 0xea, 0x9e, 0xac, | |
330 0x11, 0xd5, 0xda, 0x2c, 0x47, 0x34, 0xdf, 0xd6, 0x0f, 0x30, 0xd5, 0xa6, | |
331 0x2f, 0xc0, 0x82, 0x31, 0x38, 0xa8, 0x16, 0x68, 0x3d, 0x22, 0x11, 0x11, | |
332 0x2c, 0x03, 0x3c, 0x4e, 0x01, 0x0a, 0x8a, 0x37, 0x1c, 0x91, 0x2f, 0x11, | |
333 0x2a, 0x1c, 0x9e, 0xec, 0x25, 0x4b, 0xb7, 0xa5, 0xb5, 0x14, 0x4e, 0xcb, | |
334 0xa6, 0xfc, 0x10, 0xa0, 0x6c, 0x00, 0x0e, 0x2d, 0x8e, 0xe6, 0x69, 0x8d, | |
335 0x4e, 0x8e, 0x04, 0x84, 0xbc, 0x83, 0x83, 0xd4, 0x46, 0x07, 0x6c, 0x04, | |
336 0x96, 0x04, 0xc0, 0x72, 0xfc, 0xee, 0xaf, 0xbe, 0x6c, 0x73, 0x34, 0xd4, | |
337 0x57, 0xcf, 0xc6, 0xed, 0x01, 0xd1, 0x9f, 0xe7, 0x69, 0x7f, 0x92, 0x80, | |
338 0xf3, 0xdf, 0x96, 0xda, 0x78, 0x2c, 0x16, 0xb9, 0x4c, 0x62, 0x79, 0x37, | |
339 0x1d, 0x97, 0xb4, 0xaa, 0x77, 0xa6, 0x56, 0x6f, 0xb1, 0x36, 0xab, 0xb1, | |
340 0xd4, 0x56, 0x16, 0xc6, 0x86, 0x51, 0xe0, 0xe4, 0x22, 0x9b, 0xae, 0x99, | |
341 0xbd, 0x60, 0x0d, 0xbd, 0x9e, 0x13, 0x02, 0x21, 0x00, 0x89, 0xe8, 0x1a, | |
342 0xac, 0xbf, 0x62, 0xc5, 0x57, 0x24, 0x90, 0x83, 0xfc, 0x2f, 0xf1, 0x57, | |
343 0x2e, 0x16, 0xa5, 0x99, 0xea, 0x13, 0xc7, 0xb4, 0xe5, 0xc0, 0xf2, 0x3a, | |
344 0x38, 0x07, 0x88, 0xcc, 0x8f, 0x02, 0x82, 0x01, 0x00, 0x3c, 0x97, 0xd5, | |
345 0x97, 0xd9, 0xa5, 0xf9, 0xac, 0xa6, 0x8d, 0xf0, 0xf4, 0xd3, 0x41, 0x3a, | |
346 0x37, 0x67, 0x9d, 0xf8, 0x97, 0xa6, 0x2f, 0xe8, 0x44, 0xa3, 0x9e, 0x9c, | |
347 0xca, 0xed, 0x56, 0x2c, 0x25, 0x6e, 0x7f, 0xbb, 0x7a, 0x30, 0x52, 0x5e, | |
348 0x8b, 0xb2, 0x10, 0x22, 0xd6, 0x29, 0x88, 0xc8, 0x3b, 0x27, 0xa6, 0xfb, | |
349 0x30, 0xdf, 0xa9, 0xd8, 0x02, 0x4e, 0x51, 0xcb, 0xfd, 0x38, 0x97, 0x6d, | |
350 0x1c, 0x96, 0x4e, 0x4e, 0xd2, 0x0b, 0xba, 0x6b, 0x86, 0xfb, 0x58, 0xb9, | |
351 0xd0, 0xdd, 0xb7, 0x44, 0xc1, 0x5a, 0xec, 0xd9, 0x8f, 0x77, 0x0b, 0xc9, | |
352 0xe4, 0xef, 0xed, 0xd6, 0x62, 0x75, 0xc8, 0x99, 0xfd, 0x09, 0x20, 0xac, | |
353 0x2c, 0xc1, 0xfc, 0xd3, 0x54, 0x13, 0x99, 0x27, 0x6e, 0x6c, 0x6d, 0x0c, | |
354 0x28, 0x25, 0xe2, 0xa1, 0x4e, 0x6c, 0x47, 0x19, 0x22, 0xcf, 0x2a, 0xf9, | |
355 0x6b, 0x99, 0x03, 0x88, 0xb5, 0x7f, 0x15, 0x6e, 0x0f, 0xe3, 0x86, 0x0a, | |
356 0x02, 0x85, 0x29, 0x09, 0xcc, 0xcb, 0xa3, 0xd7, 0x95, 0x9b, 0xeb, 0x80, | |
357 0x5d, 0x4c, 0x19, 0xa8, 0x3b, 0x2e, 0x3f, 0x0a, 0x04, 0x27, 0x5b, 0x04, | |
358 0x5c, 0xa1, 0x75, 0xcb, 0x54, 0xcf, 0x3c, 0x0d, 0x42, 0x19, 0x62, 0x33, | |
359 0x08, 0x70, 0x7b, 0x78, 0x11, 0xcf, 0x6d, 0xb5, 0x8d, 0x77, 0x2b, 0xac, | |
360 0xbe, 0xde, 0x9e, 0x4a, 0x0b, 0x36, 0xa0, 0x4a, 0x34, 0x2c, 0x95, 0x92, | |
361 0x49, 0x10, 0x82, 0x4b, 0x9f, 0xe0, 0xf8, 0xb5, 0xd9, 0x32, 0x61, 0x47, | |
362 0x0f, 0xc4, 0x87, 0xc3, 0x06, 0x33, 0x9f, 0x6b, 0x28, 0xee, 0xac, 0x24, | |
363 0xbc, 0xfe, 0xba, 0xa3, 0xeb, 0x20, 0x7f, 0x67, 0x42, 0x73, 0xb3, 0xe5, | |
364 0xf9, 0xc3, 0x2f, 0x6e, 0xef, 0xb3, 0xc5, 0x52, 0x1a, 0xfb, 0x96, 0x70, | |
365 0x96, 0x47, 0x54, 0x31, 0xd4, 0x25, 0xbd, 0x29, 0x7b, 0x19, 0xa5, 0xdc, | |
366 0xb9, 0x03, 0x82, 0x01, 0x05, 0x00, 0x02, 0x82, 0x01, 0x00, 0x7e, 0x8c, | |
367 0x72, 0x95, 0xc6, 0x27, 0xef, 0x1a, 0xd1, 0x5c, 0xfd, 0xe1, 0xc7, 0xdf, | |
368 0x92, 0xdd, 0xaf, 0x59, 0x1b, 0x9a, 0xec, 0x9f, 0x87, 0x09, 0xe8, 0x9f, | |
369 0x4e, 0x89, 0x42, 0xcc, 0x40, 0x80, 0x1d, 0x73, 0x4b, 0x05, 0xfe, 0x68, | |
370 0x41, 0xdb, 0xda, 0x7a, 0x37, 0xd1, 0xdc, 0x99, 0xc1, 0xdb, 0x95, 0x63, | |
371 0x37, 0x3d, 0xef, 0x7a, 0x21, 0x20, 0x06, 0x69, 0xa2, 0xfa, 0x36, 0xf5, | |
372 0xbd, 0xee, 0x3f, 0x13, 0xc1, 0x49, 0xb8, 0xbb, 0x85, 0x3e, 0x80, 0x63, | |
373 0xe3, 0xf1, 0xba, 0x5f, 0xad, 0x9a, 0xf6, 0x22, 0xb0, 0xd2, 0x4c, 0x7e, | |
374 0xc0, 0x8a, 0x81, 0x8f, 0xa5, 0x71, 0x03, 0xde, 0xf4, 0x73, 0x7e, 0xf9, | |
375 0x05, 0x91, 0x36, 0x5f, 0x3c, 0x79, 0x7f, 0x3f, 0xeb, 0xb9, 0x36, 0xca, | |
376 0xb8, 0x40, 0x9b, 0x1d, 0xce, 0xce, 0x04, 0x89, 0xc8, 0xc4, 0xa0, 0xc9, | |
377 0x63, 0x42, 0x81, 0xdc, 0x80, 0xe1, 0xa5, 0x58, 0xd4, 0x10, 0xd4, 0x2c, | |
378 0x6c, 0xae, 0xdd, 0xb8, 0x17, 0xdc, 0xcb, 0x7f, 0x62, 0x4f, 0x51, 0xcf, | |
379 0x0a, 0x10, 0x44, 0x0b, 0xe9, 0xf4, 0x5d, 0x3d, 0x78, 0xd0, 0xe3, 0xf9, | |
380 0xc5, 0x0f, 0xde, 0x09, 0x72, 0xa1, 0xaf, 0xaf, 0xd7, 0x30, 0x57, 0xca, | |
381 0xb7, 0xb1, 0xa6, 0x8a, 0x04, 0x71, 0x63, 0x30, 0xf8, 0xa9, 0x44, 0x2f, | |
382 0x8f, 0xc4, 0x37, 0x0c, 0xc9, 0x9d, 0x72, 0x12, 0x7c, 0x7b, 0xd9, 0x2a, | |
383 0x52, 0x34, 0x47, 0xf1, 0x98, 0xc4, 0xbc, 0xa7, 0x74, 0x1c, 0x0a, 0xd3, | |
384 0x6d, 0x7f, 0x8c, 0xce, 0x28, 0x78, 0x59, 0xc9, 0x50, 0x38, 0xc6, 0xf7, | |
385 0x4c, 0x74, 0x83, 0x04, 0x60, 0x34, 0x64, 0x4f, 0xc1, 0x99, 0x8e, 0x6a, | |
386 0x7e, 0x30, 0x59, 0x3b, 0xd5, 0x65, 0xb3, 0x56, 0x68, 0x7d, 0xa4, 0xec, | |
387 0xf0, 0x3c, 0xb2, 0x5a, 0x09, 0xf3, 0xc7, 0xeb, 0x8d, 0x09, 0xab, 0x1d, | |
388 0x6f, 0x5c | |
389 }; | |
390 | |
391 // Call this function to verify that one message signed with our | |
392 // test DSA private key is correct. Since DSA signing introduces | |
393 // random elements in the signature, it is not possible to compare | |
394 // signature bits directly. However, one can use the public key | |
395 // to do the check. | |
396 void VerifyTestDSASignature(const base::StringPiece& message, | |
397 const base::StringPiece& signature) { | |
398 const unsigned char* p = test_dsa_public_der; | |
399 long length = static_cast<long>(sizeof(test_dsa_public_der)); | |
400 DSA* pub_key = d2i_DSA_PUBKEY(NULL, &p, length); | |
401 ASSERT_TRUE(pub_key != NULL); | |
402 | |
403 const unsigned char* digest = | |
404 reinterpret_cast<const unsigned char*>(message.data()); | |
405 int digest_len = static_cast<int>(message.size()); | |
406 const unsigned char* sigbuf = | |
407 reinterpret_cast<const unsigned char*>(signature.data()); | |
408 int siglen = static_cast<int>(signature.size()); | |
409 | |
410 ASSERT_EQ(1, DSA_verify(0, digest, digest_len, sigbuf, siglen, pub_key)); | |
411 DSA_free(pub_key); | |
412 } | |
413 | |
414 // A simple ECDSA test key. | |
415 // | |
416 // openssl ecparam -genkey -name prime256v1 -out key.pem | |
417 // openssl pkcs8 -topk8 -inform PEM -outform DER -in key.pem | |
418 // -out key.pkcs8 -nocrypt | |
419 // xxd -i key.pkcs8 | |
420 const unsigned char test_ecdsa_key_pkcs8[] = { | |
421 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, | |
422 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, | |
423 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, | |
424 0xd8, 0x57, 0x0f, 0xee, 0x4c, 0x93, 0xbe, 0x42, 0x2e, 0x2b, 0x2e, 0xf2, | |
425 0x7d, 0x56, 0xd9, 0x7b, 0x41, 0xc2, 0xc5, 0xd8, 0xbc, 0x77, 0x90, 0x0c, | |
426 0x38, 0xf4, 0xc2, 0x01, 0xc3, 0xd1, 0x6c, 0x24, 0xa1, 0x44, 0x03, 0x42, | |
427 0x00, 0x04, 0x97, 0x7a, 0x0e, 0x39, 0x93, 0xc7, 0x21, 0xc9, 0x3f, 0x68, | |
428 0xf5, 0x77, 0x56, 0xc5, 0xbb, 0xb2, 0x28, 0x64, 0x73, 0x90, 0x73, 0xbd, | |
429 0xaa, 0x4d, 0x98, 0x27, 0x47, 0xa1, 0x30, 0x55, 0x2e, 0x36, 0x91, 0x4e, | |
430 0x25, 0xea, 0x83, 0x13, 0x97, 0xc8, 0x0a, 0xc8, 0xcf, 0xaa, 0xb4, 0x55, | |
431 0x34, 0x6b, 0x11, 0x4b, 0xbd, 0x7e, 0x94, 0xe4, 0x77, 0x98, 0xa8, 0x10, | |
432 0x7b, 0x63, 0x21, 0x7f, 0xa9, 0xcf | |
433 }; | |
434 | |
435 // Retrieve a JNI local ref for our test ECDSA key. | |
436 ScopedJavaLocalRef<jobject> GetECDSATestKey() { | |
437 return GetPKCS8PrivateKey(CLIENT_CERT_ECDSA_SIGN, | |
438 test_ecdsa_key_pkcs8, | |
439 sizeof(test_ecdsa_key_pkcs8)); | |
440 } | |
441 | |
442 // Retrieve the OpenSSL object for our test ECDSA key. | |
443 EVP_PKEY* GetECDSATestKeyOpenSSL() { | |
444 return GetOpenSSLPKCS8PrivateKey(EVP_PKEY_EC, | |
445 test_ecdsa_key_pkcs8, | |
446 sizeof(test_ecdsa_key_pkcs8)); | |
447 } | |
448 | |
449 // The DSA public key generated from the previous ECDSA private one, with: | |
450 // openssl ec -in key.pem -outform DER -out key_public.der -pubout | |
451 // xxd -i key_public.der | |
452 const unsigned char test_ecdsa_public_der[] = { | |
453 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, | |
454 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, | |
455 0x42, 0x00, 0x04, 0x97, 0x7a, 0x0e, 0x39, 0x93, 0xc7, 0x21, 0xc9, 0x3f, | |
456 0x68, 0xf5, 0x77, 0x56, 0xc5, 0xbb, 0xb2, 0x28, 0x64, 0x73, 0x90, 0x73, | |
457 0xbd, 0xaa, 0x4d, 0x98, 0x27, 0x47, 0xa1, 0x30, 0x55, 0x2e, 0x36, 0x91, | |
458 0x4e, 0x25, 0xea, 0x83, 0x13, 0x97, 0xc8, 0x0a, 0xc8, 0xcf, 0xaa, 0xb4, | |
459 0x55, 0x34, 0x6b, 0x11, 0x4b, 0xbd, 0x7e, 0x94, 0xe4, 0x77, 0x98, 0xa8, | |
460 0x10, 0x7b, 0x63, 0x21, 0x7f, 0xa9, 0xcf | |
461 }; | |
462 | |
463 // Call this function to verify that one message signed with our | |
464 // test DSA private key is correct. Since DSA signing introduces | |
465 // random elements in the signature, it is not possible to compare | |
466 // signature bits directly. However, one can use the public key | |
467 // to do the check. | |
468 void VerifyTestECDSASignature(const base::StringPiece& message, | |
469 const base::StringPiece& signature) { | |
470 const unsigned char* p = test_ecdsa_public_der; | |
471 long length = static_cast<long>(sizeof(test_ecdsa_public_der)); | |
472 EC_KEY* pub_key = d2i_EC_PUBKEY(NULL, &p, length); | |
473 ASSERT_TRUE(pub_key != NULL); | |
474 | |
475 const unsigned char* digest = | |
476 reinterpret_cast<const unsigned char*>(message.data()); | |
477 int digest_len = static_cast<int>(message.size()); | |
478 const unsigned char* sigbuf = | |
479 reinterpret_cast<const unsigned char*>(signature.data()); | |
480 int siglen = static_cast<int>(signature.size()); | |
481 | |
482 ASSERT_EQ( | |
483 1, ECDSA_verify(0, digest, digest_len, sigbuf, siglen, pub_key)); | |
484 EC_KEY_free(pub_key); | |
485 } | |
486 | |
487 // Sign a message with OpenSSL, return the result as a string. | |
488 // |message| is the message to be signed. | |
489 // |openssl_key| is an OpenSSL EVP_PKEY to use. | |
490 // |result| receives the result. | |
491 void SignWithOpenSSL(const base::StringPiece& message, | |
492 EVP_PKEY* openssl_key, | |
493 std::string* result) { | |
494 const unsigned char* digest = | |
495 reinterpret_cast<const unsigned char*>(message.data()); | |
496 unsigned int digest_len = static_cast<unsigned int>(message.size()); | |
497 // Calling size functions like "RSA_size()" doesn't work at all | |
498 // with custom RSA_METHOD implementations. That's probably a bug | |
499 // in OpenSSL, so instead just use a very large buffer. | |
500 // Note that the code in ssl/s3_clnt.c does something similar. | |
501 unsigned char openssl_signature[8192]; | |
502 unsigned int openssl_signature_len = 0; | |
503 int key_type = EVP_PKEY_id(openssl_key); | |
504 switch (key_type) { | |
505 case EVP_PKEY_RSA: | |
506 { | |
507 RSA* rsa = EVP_PKEY_get1_RSA(openssl_key); | |
508 ASSERT_TRUE(rsa != NULL); | |
509 int ret = RSA_sign(NID_md5_sha1, | |
510 digest, | |
511 digest_len, | |
512 openssl_signature, | |
513 &openssl_signature_len, | |
514 rsa); | |
515 ASSERT_EQ(1, ret) << GetOpenSSLErrorString(); | |
516 RSA_free(rsa); | |
517 } | |
518 break; | |
519 case EVP_PKEY_DSA: | |
520 { | |
521 DSA* dsa = EVP_PKEY_get1_DSA(openssl_key); | |
522 ASSERT_TRUE(dsa != NULL); | |
523 int ret = DSA_sign(0, // ignored by the function | |
524 digest, | |
525 digest_len, | |
526 openssl_signature, | |
527 &openssl_signature_len, | |
528 dsa); | |
529 ASSERT_EQ(1, ret) << GetOpenSSLErrorString(); | |
530 DSA_free(dsa); | |
531 } | |
532 break; | |
533 case EVP_PKEY_EC: | |
534 { | |
535 EC_KEY* ecdsa = EVP_PKEY_get1_EC_KEY(openssl_key); | |
536 ASSERT_TRUE(ecdsa != NULL); | |
537 LOG(INFO) << "Calling ECDSA_sign"; | |
538 int ret = ECDSA_sign(0, // ignored by the function | |
539 digest, | |
540 digest_len, | |
541 openssl_signature, | |
542 &openssl_signature_len, | |
543 ecdsa); | |
544 ASSERT_EQ(1, ret) << GetOpenSSLErrorString(); | |
545 EC_KEY_free(ecdsa); | |
546 } | |
547 break; | |
548 default: | |
549 LOG(WARNING) << "Invalid OpenSSL key type: " + key_type; | |
550 return; | |
551 } | |
552 result->assign(reinterpret_cast<const char*>(openssl_signature), | |
553 static_cast<size_t>(openssl_signature_len)); | |
554 } | |
555 | |
556 // Check that a generated signature for a given message matches | |
557 // OpenSSL output byte-by-byte. | |
558 // |message| is the input message. | |
559 // |signature| is the generated signature for the message. | |
560 // |openssl_key| is a raw EVP_PKEY for the same private key than the | |
561 // one which was used to generate the signature. | |
562 void CompareSignatureWithOpenSSL(const base::StringPiece& message, | |
563 const base::StringPiece& signature, | |
564 EVP_PKEY* openssl_key) { | |
565 std::string openssl_signature; | |
566 SignWithOpenSSL(message, openssl_key, &openssl_signature); | |
567 | |
568 ASSERT_EQ(signature.size(), openssl_signature.size()); | |
569 for (size_t n = 0; n < signature.size(); ++n) | |
570 ASSERT_EQ(openssl_signature[n], signature[n]); | |
571 | |
572 // All good, just exit. | |
573 } | |
574 | |
575 // Sign a message with our platform API. | |
576 // | |
577 // |android_key| is a JNI reference to the platform PrivateKey object. | |
578 // |openssl_key| is a pointer to an OpenSSL key object for the exact | |
579 // same key content. | |
580 // |message| is a message. | |
581 // |result| will receive the result. | |
582 void DoKeySigning(jobject android_key, | |
583 EVP_PKEY* openssl_key, | |
584 const base::StringPiece& message, | |
585 std::string* result) { | |
586 // First, get the platform signature. | |
587 std::vector<uint8> android_signature; | |
588 ASSERT_TRUE( | |
589 SignWithPrivateKey(android_key, | |
590 message, | |
591 &android_signature)); | |
592 | |
593 result->assign( | |
594 reinterpret_cast<const char*>(&android_signature[0]), | |
595 android_signature.size()); | |
596 } | |
597 | |
598 // Sign a message with our OpenSSL EVP_PKEY wrapper around platform | |
599 // APIS. | |
600 // | |
601 // |android_key| is a JNI reference to the platform PrivateKey object. | |
602 // |openssl_key| is a pointer to an OpenSSL key object for the exact | |
603 // same key content. | |
604 // |message| is a message. | |
605 // |result| will receive the result. | |
606 void DoKeySigningWithWrapper(EVP_PKEY* wrapper_key, | |
607 EVP_PKEY* openssl_key, | |
608 const base::StringPiece& message, | |
609 std::string* result) { | |
610 // First, get the signature platform signature. | |
ppi
2013/01/29 17:57:10
nit: double "signature".
| |
611 std::string wrapper_signature; | |
612 SignWithOpenSSL(message, wrapper_key, &wrapper_signature); | |
613 ASSERT_NE(0U, wrapper_signature.size()); | |
614 | |
615 result->assign( | |
616 reinterpret_cast<const char*>(&wrapper_signature[0]), | |
617 wrapper_signature.size()); | |
618 } | |
619 | |
620 } // namespace | |
621 | |
622 TEST(AndroidKeyStore,GetPrivateKeySigningTypeRSA) { | |
623 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
624 | |
625 ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKey(); | |
626 ASSERT_FALSE(rsa_key.is_null()); | |
627 EXPECT_EQ(CLIENT_CERT_RSA_SIGN, | |
628 GetPrivateKeySigningType(rsa_key.obj())); | |
629 } | |
630 | |
631 TEST(AndroidKeyStore,SignWithPrivateKeyRSA) { | |
632 | |
633 ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKey(); | |
634 ASSERT_FALSE(rsa_key.is_null()); | |
635 | |
636 if (IsOnAndroidOlderThan_4_2()) { | |
637 LOG(INFO) << "This test can't run on Android < 4.2"; | |
ppi
2013/01/29 17:57:10
nit: should this be indented with two spaces?
digit1
2013/01/29 23:17:39
Done.
| |
638 return; | |
639 } | |
640 | |
641 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key( | |
642 GetRSATestKeyOpenSSL()); | |
643 ASSERT_TRUE(openssl_key.get() != NULL); | |
644 | |
645 // Message size must be 36 for RSA_sign(NID_md5_sha1,...) to return | |
646 // without an error. | |
647 // See third_party/openssl/openssl/crypto/rsa/rsa_sign.c | |
648 std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
649 ASSERT_EQ(36U, message.size()); | |
650 | |
651 std::string signature; | |
652 DoKeySigning(rsa_key.obj(), openssl_key.get(), message, &signature); | |
653 CompareSignatureWithOpenSSL(message, signature, openssl_key.get()); | |
654 // All good. | |
655 } | |
656 | |
657 TEST(AndroidKeyStore,SignWithWrapperKeyRSA) { | |
658 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
659 | |
660 ScopedJavaLocalRef<jobject> rsa_key = GetRSATestKey(); | |
661 ASSERT_FALSE(rsa_key.is_null()); | |
662 | |
663 if (IsOnAndroidOlderThan_4_2()) { | |
664 LOG(INFO) << "This test can't run on Android < 4.2"; | |
ppi
2013/01/29 17:57:11
nit: should this be indented with two spaces?
digit1
2013/01/29 23:17:39
Yes, I probably forgot to change my editor's setti
| |
665 return; | |
666 } | |
667 | |
668 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> wrapper_key( | |
669 GetOpenSSLPrivateKeyWrapper(rsa_key.obj())); | |
670 ASSERT_TRUE(wrapper_key.get() != NULL); | |
671 | |
672 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key( | |
673 GetRSATestKeyOpenSSL()); | |
674 ASSERT_TRUE(openssl_key.get() != NULL); | |
675 | |
676 // Message size must be 36 for RSA_sign(NID_md5_sha1,...) to return | |
677 // without an error. | |
678 std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
679 ASSERT_EQ(36U, message.size()); | |
680 | |
681 std::string signature; | |
682 DoKeySigningWithWrapper(wrapper_key.get(), | |
683 openssl_key.get(), | |
684 message, | |
685 &signature); | |
686 CompareSignatureWithOpenSSL(message, signature, openssl_key.get()); | |
687 // All good. | |
688 } | |
689 | |
690 TEST(AndroidKeyStore,GetPrivateKeySigningTypeDSA) { | |
691 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
692 | |
693 ScopedJavaLocalRef<jobject> dsa_key = GetDSATestKey(); | |
694 ASSERT_FALSE(dsa_key.is_null()); | |
695 EXPECT_EQ(CLIENT_CERT_DSS_SIGN, | |
696 GetPrivateKeySigningType(dsa_key.obj())); | |
697 } | |
698 | |
699 TEST(AndroidKeyStore,SignWithPrivateKeyDSA) { | |
700 ScopedJavaLocalRef<jobject> dsa_key = GetDSATestKey(); | |
701 ASSERT_FALSE(dsa_key.is_null()); | |
702 | |
703 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key( | |
704 GetDSATestKeyOpenSSL()); | |
705 ASSERT_TRUE(openssl_key.get() != NULL); | |
706 | |
707 // Message size must be 36 for DSA_sign(NID_md5_sha1,...) to return | |
708 // without an error. | |
709 // See third_party/openssl/openssl/crypto/dsa/dsa_sign.c | |
710 std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
711 ASSERT_EQ(36U, message.size()); | |
712 | |
713 std::string signature; | |
714 DoKeySigning(dsa_key.obj(), openssl_key.get(), message, &signature); | |
715 VerifyTestDSASignature(message, signature); | |
716 // All good. | |
717 } | |
718 | |
719 // TODO(digit): Enable by implementing DSA_METHOD wrapper. | |
720 TEST(AndroidKeyStore,DISABLED_SignWithWrapperKeyDSA) { | |
721 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
722 | |
723 ScopedJavaLocalRef<jobject> dsa_key = GetDSATestKey(); | |
724 ASSERT_FALSE(dsa_key.is_null()); | |
725 | |
726 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> wrapper_key( | |
727 GetOpenSSLPrivateKeyWrapper(dsa_key.obj())); | |
728 ASSERT_TRUE(wrapper_key.get() != NULL); | |
729 | |
730 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key( | |
731 GetDSATestKeyOpenSSL()); | |
732 ASSERT_TRUE(openssl_key.get() != NULL); | |
733 | |
734 // Message size must be 36 for DSA_sign(NID_md5_sha1,...) to return | |
735 // without an error. | |
736 std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
737 ASSERT_EQ(36U, message.size()); | |
738 | |
739 std::string signature; | |
740 DoKeySigningWithWrapper(wrapper_key.get(), | |
741 openssl_key.get(), | |
742 message, | |
743 &signature); | |
744 VerifyTestDSASignature(message, signature); | |
745 // All good. | |
746 } | |
747 | |
748 TEST(AndroidKeyStore,GetPrivateKeySigningTypeECDSA) { | |
749 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
750 | |
751 ScopedJavaLocalRef<jobject> ecdsa_key = GetECDSATestKey(); | |
752 ASSERT_FALSE(ecdsa_key.is_null()); | |
753 EXPECT_EQ(CLIENT_CERT_ECDSA_SIGN, | |
754 GetPrivateKeySigningType(ecdsa_key.obj())); | |
755 } | |
756 | |
757 TEST(AndroidKeyStore,SignWithPrivateKeyECDSA) { | |
758 ScopedJavaLocalRef<jobject> ecdsa_key = GetECDSATestKey(); | |
759 ASSERT_FALSE(ecdsa_key.is_null()); | |
760 | |
761 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key( | |
762 GetECDSATestKeyOpenSSL()); | |
763 ASSERT_TRUE(openssl_key.get() != NULL); | |
764 | |
765 // Message size must be 36 for ECDSA_sign(NID_md5_sha1,...) to return | |
766 // without an error. | |
767 // See third_party/openssl/openssl/crypto/dsa/ecdsa_sign.c | |
768 std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
769 ASSERT_EQ(36U, message.size()); | |
770 | |
771 std::string signature; | |
772 DoKeySigning(ecdsa_key.obj(), openssl_key.get(), message, &signature); | |
773 VerifyTestECDSASignature(message, signature); | |
774 // All good. | |
775 } | |
776 | |
777 // TODO(digit): Enable by implement ECDSA_METHOD wrapper. | |
778 TEST(AndroidKeyStore,DISABLED_SignWithWrapperKeyECDSA) { | |
779 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
780 | |
781 ScopedJavaLocalRef<jobject> ecdsa_key = GetECDSATestKey(); | |
782 ASSERT_FALSE(ecdsa_key.is_null()); | |
783 | |
784 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> wrapper_key( | |
785 GetOpenSSLPrivateKeyWrapper(ecdsa_key.obj())); | |
786 ASSERT_TRUE(wrapper_key.get() != NULL); | |
787 | |
788 crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> openssl_key( | |
789 GetECDSATestKeyOpenSSL()); | |
790 ASSERT_TRUE(openssl_key.get() != NULL); | |
791 | |
792 // Message size must be 36 for ECDSA_sign(NID_md5_sha1,...) to return | |
793 // without an error. | |
794 std::string message = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
795 ASSERT_EQ(36U, message.size()); | |
796 | |
797 std::string signature; | |
798 DoKeySigningWithWrapper(wrapper_key.get(), | |
799 openssl_key.get(), | |
800 message, | |
801 &signature); | |
802 VerifyTestECDSASignature(message, signature); | |
803 // All good. | |
804 } | |
805 | |
806 } // namespace android | |
807 } // namespace net | |
OLD | NEW |