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/bn.h> | |
6 #include <openssl/dsa.h> | |
7 #include <openssl/ecdsa.h> | |
8 #include <openssl/err.h> | |
9 #include <openssl/evp.h> | |
10 #include <openssl/pem.h> | |
11 #include <openssl/rsa.h> | |
12 #include <openssl/x509.h> | |
13 | |
14 #include "base/android/build_info.h" | |
15 #include "base/android/jni_android.h" | |
16 #include "base/android/jni_array.h" | |
17 #include "base/android/scoped_java_ref.h" | |
18 #include "base/basictypes.h" | |
19 #include "base/bind.h" | |
20 #include "base/callback.h" | |
21 #include "base/compiler_specific.h" | |
22 #include "base/file_path.h" | |
23 #include "base/file_util.h" | |
24 #include "base/memory/scoped_handle.h" | |
25 #include "base/string_util.h" | |
26 #include "crypto/openssl_util.h" | |
27 #include "jni/AndroidKeyStoreTestUtil_jni.h" | |
28 #include "net/android/keystore.h" | |
29 #include "net/android/keystore_openssl.h" | |
30 #include "net/base/test_data_directory.h" | |
31 #include "testing/gtest/include/gtest/gtest.h" | |
32 | |
33 // Technical note: | |
34 // | |
35 // This source file not only checks that signing with | |
36 // RawSignDigestWithPrivateKey() works correctly, it also verifies that | |
37 // the generated signature matches 100% of what OpenSSL generates when | |
38 // calling RSA_sign(NID_md5_sha1,...), DSA_sign(0, ...) or | |
39 // ECDSA_sign(0, ...). | |
40 // | |
41 // That's crucial to ensure that this function can later be used to | |
42 // implement client certificate support. More specifically, that it is | |
43 // possible to create a custom EVP_PKEY that uses | |
44 // RawSignDigestWithPrivateKey() internally to perform RSA/DSA/ECDSA | |
45 // signing, as invoked by the OpenSSL code at | |
46 // openssl/ssl/s3_clnt.c:ssl3_send_client_verify(). | |
47 // | |
48 // For more details, read the comments in AndroidKeyStore.java. | |
49 // | |
50 // Finally, it also checks that using the EVP_PKEY generated with | |
51 // GetOpenSSLPrivateKeyWrapper() works correctly. | |
52 | |
53 namespace net { | |
54 namespace android { | |
55 | |
56 namespace { | |
57 | |
58 typedef crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> ScopedEVP_PKEY; | |
59 typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA; | |
60 typedef crypto::ScopedOpenSSL<DSA, DSA_free> ScopedDSA; | |
61 typedef crypto::ScopedOpenSSL<EC_KEY, EC_KEY_free> ScopedEC_KEY; | |
62 typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM; | |
63 | |
64 typedef crypto::ScopedOpenSSL< | |
65 PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free> | |
66 ScopedPKCS8_PRIV_KEY_INFO; | |
67 | |
68 typedef base::android::ScopedJavaLocalRef<jobject> ScopedJava; | |
69 | |
70 JNIEnv* InitEnv() { | |
71 JNIEnv* env = base::android::AttachCurrentThread(); | |
72 static bool inited = false; | |
73 if (!inited) { | |
74 RegisterNativesImpl(env); | |
75 inited = true; | |
76 } | |
77 return env; | |
78 } | |
79 | |
80 // Returns true if running on an Android version older than 4.2 | |
81 bool IsOnAndroidOlderThan_4_2(void) { | |
82 const int kAndroid42ApiLevel = 17; | |
83 int level = base::android::BuildInfo::GetInstance()->sdk_int(); | |
84 return level < kAndroid42ApiLevel; | |
85 } | |
86 | |
87 // Implements the callback expected by ERR_print_errors_cb(). | |
88 // used by GetOpenSSLErrorString below. | |
89 int openssl_print_error_callback(const char* msg, size_t msglen, void* u) { | |
90 std::string* result = reinterpret_cast<std::string*>(u); | |
91 result->append(msg, msglen); | |
92 return 1; | |
93 } | |
94 | |
95 // Retrieves the OpenSSL error as a string | |
96 std::string GetOpenSSLErrorString(void) { | |
97 std::string result; | |
98 ERR_print_errors_cb(openssl_print_error_callback, &result); | |
99 return result; | |
100 } | |
101 | |
102 // Resize a string to |size| bytes of data, then return its data buffer | |
103 // address cast as an 'unsigned char*', as expected by OpenSSL functions. | |
104 // |str| the target string. | |
105 // |size| the number of bytes to write into the string. | |
106 // Return the string's new buffer in memory, as an 'unsigned char*' | |
107 // pointer. | |
108 unsigned char* OpenSSLWriteInto(std::string* str, size_t size) { | |
109 return reinterpret_cast<unsigned char*>(WriteInto(str, size + 1)); | |
110 } | |
111 | |
112 // Load a given private key file into an EVP_PKEY. | |
113 // |filename| is the key file path. | |
114 // Returns a new EVP_PKEY on success, NULL on failure. | |
115 EVP_PKEY* ImportPrivateKeyFile(const char* filename) { | |
116 // Load file in memory. | |
117 FilePath certs_dir = GetTestCertsDirectory(); | |
118 FilePath file_path = certs_dir.AppendASCII(filename); | |
119 ScopedStdioHandle handle( | |
120 file_util::OpenFile(file_path, "rb")); | |
121 if (!handle.get()) { | |
122 LOG(ERROR) << "Could not open private key file: " << filename; | |
123 return NULL; | |
124 } | |
125 // Assume it is PEM_encoded. Load it as an EVP_PKEY. | |
126 EVP_PKEY* pkey = PEM_read_PrivateKey(handle.get(), NULL, NULL, NULL); | |
127 if (!pkey) { | |
128 LOG(ERROR) << "Could not load public key file: " << filename | |
129 << ", " << GetOpenSSLErrorString(); | |
130 return NULL; | |
131 } | |
132 return pkey; | |
133 } | |
134 | |
135 // Convert a private key into its PKCS#8 encoded representation. | |
136 // |pkey| is the EVP_PKEY handle for the private key. | |
137 // |pkcs8| will receive the PKCS#8 bytes. | |
138 // Returns true on success, false otherwise. | |
139 bool GetPrivateKeyPkcs8Bytes(const ScopedEVP_PKEY& pkey, | |
140 std::string* pkcs8) { | |
141 // Convert to PKCS#8 object. | |
bulach
2013/02/11 11:59:05
nit: unindent 141-153
digit1
2013/02/11 14:03:45
Done.
| |
142 ScopedPKCS8_PRIV_KEY_INFO p8_info(EVP_PKEY2PKCS8(pkey.get())); | |
143 if (!p8_info.get()) { | |
144 LOG(ERROR) << "Can't get PKCS#8 private key from EVP_PKEY: " | |
145 << GetOpenSSLErrorString(); | |
146 return false; | |
147 } | |
148 | |
149 // Then convert it | |
150 int len = i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), NULL); | |
151 unsigned char* p = OpenSSLWriteInto(pkcs8, static_cast<size_t>(len)); | |
152 i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), &p); | |
153 return true; | |
154 } | |
155 | |
156 bool ImportPrivateKeyFileAsPkcs8(const char* filename, | |
157 std::string* pkcs8) { | |
158 ScopedEVP_PKEY pkey(ImportPrivateKeyFile(filename)); | |
159 if (!pkey.get()) | |
160 return false; | |
161 return GetPrivateKeyPkcs8Bytes(pkey, pkcs8); | |
162 } | |
163 | |
164 // Same as ImportPrivateKey, but for public ones. | |
165 EVP_PKEY* ImportPublicKeyFile(const char* filename) { | |
166 // Load file as PEM data. | |
167 FilePath certs_dir = GetTestCertsDirectory(); | |
168 FilePath file_path = certs_dir.AppendASCII(filename); | |
169 ScopedStdioHandle handle(file_util::OpenFile(file_path, "rb")); | |
170 if (!handle.get()) { | |
171 LOG(ERROR) << "Could not open public key file: " << filename; | |
172 return NULL; | |
173 } | |
174 EVP_PKEY* pkey = PEM_read_PUBKEY(handle.get(), NULL, NULL, NULL); | |
175 if (!pkey) { | |
176 LOG(ERROR) << "Could not load public key file: " << filename | |
177 << ", " << GetOpenSSLErrorString(); | |
178 return NULL; | |
179 } | |
180 return pkey; | |
181 } | |
182 | |
183 // Retrieve a JNI local ref from encoded PKCS#8 data. | |
184 ScopedJava GetPKCS8PrivateKeyJava(PrivateKeyType key_type, | |
185 const std::string& pkcs8_key) { | |
186 JNIEnv* env = InitEnv(); | |
187 base::android::ScopedJavaLocalRef<jbyteArray> bytes( | |
188 base::android::ToJavaByteArray( | |
189 env, | |
190 reinterpret_cast<const uint8*>(pkcs8_key.data()), | |
191 pkcs8_key.size())); | |
192 | |
193 ScopedJava key( | |
194 Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8( | |
195 env, key_type, bytes.obj())); | |
196 | |
197 return key; | |
198 } | |
199 | |
200 const char kTestRsaKeyFile[] = "android-test-key-rsa.pem"; | |
201 | |
202 // The RSA test hash must be 36 bytes exactly. | |
203 const char kTestRsaHash[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
204 | |
205 // Retrieve a JNI local ref for our test RSA key. | |
206 ScopedJava GetRSATestKeyJava() { | |
207 std::string key; | |
208 if (!ImportPrivateKeyFileAsPkcs8(kTestRsaKeyFile, &key)) | |
209 return ScopedJava(); | |
210 return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA, key); | |
211 } | |
212 | |
213 const char kTestDsaKeyFile[] = "android-test-key-dsa.pem"; | |
214 const char kTestDsaPublicKeyFile[] = "android-test-key-dsa-public.pem"; | |
215 | |
216 // The DSA test hash must be 20 bytes exactly. | |
217 const char kTestDsaHash[] = "0123456789ABCDEFGHIJ"; | |
218 | |
219 // Retrieve a JNI local ref for our test DSA key. | |
220 ScopedJava GetDSATestKeyJava() { | |
221 std::string key; | |
222 if (!ImportPrivateKeyFileAsPkcs8(kTestDsaKeyFile, &key)) | |
223 return ScopedJava(); | |
224 return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_DSA, key); | |
225 } | |
226 | |
227 // Call this function to verify that one message signed with our | |
228 // test DSA private key is correct. Since DSA signing introduces | |
229 // random elements in the signature, it is not possible to compare | |
230 // signature bits directly. However, one can use the public key | |
231 // to do the check. | |
232 bool VerifyTestDSASignature(const base::StringPiece& message, | |
233 const base::StringPiece& signature) { | |
234 ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestDsaPublicKeyFile)); | |
235 if (!pkey.get()) | |
236 return false; | |
237 | |
238 ScopedDSA pub_key(EVP_PKEY_get1_DSA(pkey.get())); | |
239 if (!pub_key.get()) { | |
240 LOG(ERROR) << "Could not get DSA public key: " | |
241 << GetOpenSSLErrorString(); | |
242 return false; | |
243 } | |
244 | |
245 const unsigned char* digest = | |
246 reinterpret_cast<const unsigned char*>(message.data()); | |
247 int digest_len = static_cast<int>(message.size()); | |
248 const unsigned char* sigbuf = | |
249 reinterpret_cast<const unsigned char*>(signature.data()); | |
250 int siglen = static_cast<int>(signature.size()); | |
251 | |
252 int ret = DSA_verify( | |
253 0, digest, digest_len, sigbuf, siglen, pub_key.get()); | |
254 if (ret != 1) { | |
255 LOG(ERROR) << "DSA_verify() failed: " << GetOpenSSLErrorString(); | |
256 return false; | |
257 } | |
258 return true; | |
259 } | |
260 | |
261 const char kTestEcdsaKeyFile[] = "android-test-key-ecdsa.pem"; | |
262 const char kTestEcdsaPublicKeyFile[] = "android-test-key-ecdsa-public.pem"; | |
263 | |
264 // The test hash for ECDSA keys must be 20 bytes exactly. | |
265 const char kTestEcdsaHash[] = "0123456789ABCDEFGHIJ"; | |
266 | |
267 // Retrieve a JNI local ref for our test ECDSA key. | |
268 ScopedJava GetECDSATestKeyJava() { | |
269 std::string key; | |
270 if (!ImportPrivateKeyFileAsPkcs8(kTestEcdsaKeyFile, &key)) | |
271 return ScopedJava(); | |
272 return GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_ECDSA, key); | |
273 } | |
274 | |
275 // Call this function to verify that one message signed with our | |
276 // test DSA private key is correct. Since DSA signing introduces | |
277 // random elements in the signature, it is not possible to compare | |
278 // signature bits directly. However, one can use the public key | |
279 // to do the check. | |
280 bool VerifyTestECDSASignature(const base::StringPiece& message, | |
281 const base::StringPiece& signature) { | |
282 ScopedEVP_PKEY pkey(ImportPublicKeyFile(kTestEcdsaPublicKeyFile)); | |
283 if (!pkey.get()) | |
284 return false; | |
285 ScopedEC_KEY pub_key(EVP_PKEY_get1_EC_KEY(pkey.get())); | |
286 if (!pub_key.get()) { | |
287 LOG(ERROR) << "Could not get ECDSA public key: " | |
288 << GetOpenSSLErrorString(); | |
289 return false; | |
290 } | |
291 | |
292 const unsigned char* digest = | |
293 reinterpret_cast<const unsigned char*>(message.data()); | |
294 int digest_len = static_cast<int>(message.size()); | |
295 const unsigned char* sigbuf = | |
296 reinterpret_cast<const unsigned char*>(signature.data()); | |
297 int siglen = static_cast<int>(signature.size()); | |
298 | |
299 int ret = ECDSA_verify( | |
300 0, digest, digest_len, sigbuf, siglen, pub_key.get()); | |
301 if (ret != 1) { | |
302 LOG(ERROR) << "ECDSA_verify() failed: " << GetOpenSSLErrorString(); | |
303 return false; | |
304 } | |
305 return true; | |
306 } | |
307 | |
308 // Sign a message with OpenSSL, return the result as a string. | |
309 // |message| is the message to be signed. | |
310 // |openssl_key| is an OpenSSL EVP_PKEY to use. | |
311 // |result| receives the result. | |
312 // Returns true on success, false otherwise. | |
313 bool SignWithOpenSSL(const base::StringPiece& message, | |
314 EVP_PKEY* openssl_key, | |
315 std::string* result) { | |
316 const unsigned char* digest = | |
317 reinterpret_cast<const unsigned char*>(message.data()); | |
318 unsigned int digest_len = static_cast<unsigned int>(message.size()); | |
319 std::string signature; | |
320 size_t signature_size; | |
321 size_t max_signature_size; | |
322 int key_type = EVP_PKEY_id(openssl_key); | |
323 switch (key_type) { | |
324 case EVP_PKEY_RSA: | |
325 { | |
bulach
2013/02/11 11:59:05
nit: I think this goes on the case line (same belo
digit1
2013/02/11 14:03:45
Done.
| |
326 ScopedRSA rsa(EVP_PKEY_get1_RSA(openssl_key)); | |
327 if (!rsa.get()) { | |
328 LOG(ERROR) << "Could not get RSA from EVP_PKEY: " | |
329 << GetOpenSSLErrorString(); | |
330 return false; | |
331 } | |
332 // With RSA, the signature will always be RSA_size() bytes. | |
333 max_signature_size = static_cast<size_t>(RSA_size(rsa.get())); | |
334 unsigned char* p = OpenSSLWriteInto(&signature, | |
335 max_signature_size); | |
336 unsigned int p_len = 0; | |
337 int ret = RSA_sign( | |
338 NID_md5_sha1, digest, digest_len, p, &p_len, rsa.get()); | |
339 if (ret != 1) { | |
340 LOG(ERROR) << "RSA_sign() failed: " << GetOpenSSLErrorString(); | |
341 return false; | |
342 } | |
343 signature_size = static_cast<size_t>(p_len); | |
344 } | |
345 break; | |
346 case EVP_PKEY_DSA: | |
347 { | |
348 ScopedDSA dsa(EVP_PKEY_get1_DSA(openssl_key)); | |
349 if (!dsa.get()) { | |
350 LOG(ERROR) << "Could not get DSA from EVP_PKEY: " | |
351 << GetOpenSSLErrorString(); | |
352 return false; | |
353 } | |
354 // Note, the actual signature can be smaller than DSA_size() | |
355 max_signature_size = static_cast<size_t>(DSA_size(dsa.get())); | |
356 unsigned char* p = OpenSSLWriteInto(&signature, | |
357 max_signature_size); | |
358 unsigned int p_len = 0; | |
359 // Note: first parameter is ignored by function. | |
360 int ret = DSA_sign(0, digest, digest_len, p, &p_len, dsa.get()); | |
361 if (ret != 1) { | |
362 LOG(ERROR) << "DSA_sign() failed: " << GetOpenSSLErrorString(); | |
363 return false; | |
364 } | |
365 signature_size = static_cast<size_t>(p_len); | |
366 } | |
367 break; | |
368 case EVP_PKEY_EC: | |
369 { | |
370 ScopedEC_KEY ecdsa(EVP_PKEY_get1_EC_KEY(openssl_key)); | |
371 if (!ecdsa.get()) { | |
372 LOG(ERROR) << "Could not get EC_KEY from EVP_PKEY: " | |
373 << GetOpenSSLErrorString(); | |
374 return false; | |
375 } | |
376 // Note, the actual signature can be smaller than ECDSA_size() | |
377 max_signature_size = ECDSA_size(ecdsa.get()); | |
378 unsigned char* p = OpenSSLWriteInto(&signature, | |
379 max_signature_size); | |
380 unsigned int p_len = 0; | |
381 // Note: first parameter is ignored by function. | |
382 int ret = ECDSA_sign( | |
383 0, digest, digest_len, p, &p_len, ecdsa.get()); | |
384 if (ret != 1) { | |
385 LOG(ERROR) << "ECDSA_sign() fialed: " << GetOpenSSLErrorString(); | |
386 return false; | |
387 } | |
388 signature_size = static_cast<size_t>(p_len); | |
389 } | |
390 break; | |
391 default: | |
392 LOG(WARNING) << "Invalid OpenSSL key type: " << key_type; | |
393 return false; | |
394 } | |
395 | |
396 if (signature_size == 0) { | |
397 LOG(ERROR) << "Signature is empty!"; | |
398 return false; | |
399 } | |
400 if (signature_size > max_signature_size) { | |
401 LOG(ERROR) << "Signature size mismatch, actual " << signature_size | |
402 << ", expected <= " << max_signature_size; | |
403 return false; | |
404 } | |
405 signature.resize(signature_size); | |
406 result->swap(signature); | |
407 return true; | |
408 } | |
409 | |
410 // Check that a generated signature for a given message matches | |
411 // OpenSSL output byte-by-byte. | |
412 // |message| is the input message. | |
413 // |signature| is the generated signature for the message. | |
414 // |openssl_key| is a raw EVP_PKEY for the same private key than the | |
415 // one which was used to generate the signature. | |
416 // Returns true on success, false otherwise. | |
417 bool CompareSignatureWithOpenSSL(const base::StringPiece& message, | |
418 const base::StringPiece& signature, | |
419 EVP_PKEY* openssl_key) { | |
420 std::string openssl_signature; | |
421 SignWithOpenSSL(message, openssl_key, &openssl_signature); | |
422 | |
423 if (signature.size() != openssl_signature.size()) { | |
424 LOG(ERROR) << "Signature size mismatch, actual " | |
425 << signature.size() << ", expected " | |
426 << openssl_signature.size(); | |
427 return false; | |
428 } | |
429 for (size_t n = 0; n < signature.size(); ++n) { | |
430 if (openssl_signature[n] != signature[n]) { | |
431 LOG(ERROR) << "Signature byte mismatch at index " << n | |
432 << "actual " << signature[n] << ", expected " | |
bulach
2013/02/11 11:59:05
nit: maybe base::HexEncode for the byte values?
digit1
2013/02/11 14:03:45
Done.
| |
433 << openssl_signature[n]; | |
434 return false; | |
435 } | |
436 } | |
437 return true; | |
438 } | |
439 | |
440 // Sign a message with our platform API. | |
441 // | |
442 // |android_key| is a JNI reference to the platform PrivateKey object. | |
443 // |openssl_key| is a pointer to an OpenSSL key object for the exact | |
444 // same key content. | |
445 // |message| is a message. | |
446 // |result| will receive the result. | |
447 void DoKeySigning(jobject android_key, | |
448 EVP_PKEY* openssl_key, | |
449 const base::StringPiece& message, | |
450 std::string* result) { | |
451 // First, get the platform signature. | |
452 std::vector<uint8> android_signature; | |
453 ASSERT_TRUE( | |
454 RawSignDigestWithPrivateKey(android_key, | |
455 message, | |
456 &android_signature)); | |
457 | |
458 result->assign( | |
459 reinterpret_cast<const char*>(&android_signature[0]), | |
460 android_signature.size()); | |
461 } | |
462 | |
463 // Sign a message with our OpenSSL EVP_PKEY wrapper around platform | |
464 // APIS. | |
465 // | |
466 // |android_key| is a JNI reference to the platform PrivateKey object. | |
467 // |openssl_key| is a pointer to an OpenSSL key object for the exact | |
468 // same key content. | |
469 // |message| is a message. | |
470 // |result| will receive the result. | |
471 void DoKeySigningWithWrapper(EVP_PKEY* wrapper_key, | |
472 EVP_PKEY* openssl_key, | |
473 const base::StringPiece& message, | |
474 std::string* result) { | |
475 // First, get the platform signature. | |
476 std::string wrapper_signature; | |
477 SignWithOpenSSL(message, wrapper_key, &wrapper_signature); | |
478 ASSERT_NE(0U, wrapper_signature.size()); | |
479 | |
480 result->assign( | |
481 reinterpret_cast<const char*>(&wrapper_signature[0]), | |
482 wrapper_signature.size()); | |
483 } | |
484 | |
485 } // namespace | |
486 | |
487 TEST(AndroidKeyStore,GetRSAKeyModulus) { | |
488 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
489 InitEnv(); | |
bulach
2013/02/11 11:59:05
nit: these two lines seem to be repeated, consider
digit1
2013/02/11 14:03:45
InitEnv() doesn't repeat much. And it's not possib
| |
490 | |
491 // Load the test RSA key. | |
492 ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestRsaKeyFile)); | |
493 ASSERT_TRUE(pkey.get()); | |
494 | |
495 // Convert it to encoded PKCS#8 bytes. | |
496 std::string pkcs8_data; | |
497 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data)); | |
498 | |
499 // Create platform PrivateKey object from it. | |
500 ScopedJava key_java = GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA, | |
bulach
2013/02/11 11:59:05
nit: first param in the next line
digit1
2013/02/11 14:03:45
I believe this line follows the Google coding styl
| |
501 pkcs8_data); | |
502 ASSERT_FALSE(key_java.is_null()); | |
503 | |
504 // Retrieve the corresponding modulus through JNI | |
505 std::vector<uint8> modulus_java; | |
506 ASSERT_TRUE(GetRSAKeyModulus(key_java.obj(), &modulus_java)); | |
507 | |
508 // Create an OpenSSL BIGNUM from it. | |
509 ScopedBIGNUM bn( | |
510 BN_bin2bn( | |
511 reinterpret_cast<const unsigned char*>(&modulus_java[0]), | |
512 static_cast<int>(modulus_java.size()), | |
513 NULL)); | |
514 ASSERT_TRUE(bn.get()); | |
515 | |
516 // Compare it to the one in the RSA key, they must be identical. | |
517 ScopedRSA rsa(EVP_PKEY_get1_RSA(pkey.get())); | |
518 ASSERT_TRUE(rsa.get()) << GetOpenSSLErrorString(); | |
519 | |
520 ASSERT_EQ(0, BN_cmp(bn.get(), rsa.get()->n)); | |
521 } | |
522 | |
523 TEST(AndroidKeyStore,GetDSAKeyParamQ) { | |
524 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
525 InitEnv(); | |
526 | |
527 // Load the test DSA key. | |
528 ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestDsaKeyFile)); | |
529 ASSERT_TRUE(pkey.get()); | |
530 | |
531 // Convert it to encoded PKCS#8 bytes. | |
532 std::string pkcs8_data; | |
533 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data)); | |
534 | |
535 // Create platform PrivateKey object from it. | |
536 ScopedJava key_java = GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_DSA, | |
537 pkcs8_data); | |
538 ASSERT_FALSE(key_java.is_null()); | |
539 | |
540 // Retrieve the corresponding Q parameter through JNI | |
541 std::vector<uint8> q_java; | |
542 ASSERT_TRUE(GetDSAKeyParamQ(key_java.obj(), &q_java)); | |
543 | |
544 // Create an OpenSSL BIGNUM from it. | |
545 ScopedBIGNUM bn( | |
546 BN_bin2bn( | |
547 reinterpret_cast<const unsigned char*>(&q_java[0]), | |
548 static_cast<int>(q_java.size()), | |
549 NULL)); | |
550 ASSERT_TRUE(bn.get()); | |
551 | |
552 // Compare it to the one in the RSA key, they must be identical. | |
553 ScopedDSA dsa(EVP_PKEY_get1_DSA(pkey.get())); | |
554 ASSERT_TRUE(dsa.get()) << GetOpenSSLErrorString(); | |
555 | |
556 ASSERT_EQ(0, BN_cmp(bn.get(), dsa.get()->q)); | |
557 } | |
558 | |
559 TEST(AndroidKeyStore,GetPrivateKeyTypeRSA) { | |
560 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
561 | |
562 ScopedJava rsa_key = GetRSATestKeyJava(); | |
563 ASSERT_FALSE(rsa_key.is_null()); | |
564 EXPECT_EQ(PRIVATE_KEY_TYPE_RSA, | |
565 GetPrivateKeyType(rsa_key.obj())); | |
566 } | |
567 | |
568 TEST(AndroidKeyStore,SignWithPrivateKeyRSA) { | |
569 ScopedJava rsa_key = GetRSATestKeyJava(); | |
570 ASSERT_FALSE(rsa_key.is_null()); | |
571 | |
572 if (IsOnAndroidOlderThan_4_2()) { | |
573 LOG(INFO) << "This test can't run on Android < 4.2"; | |
574 return; | |
575 } | |
576 | |
577 ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile)); | |
578 ASSERT_TRUE(openssl_key.get()); | |
579 | |
580 std::string message = kTestRsaHash; | |
581 ASSERT_EQ(36U, message.size()); | |
582 | |
583 std::string signature; | |
584 DoKeySigning(rsa_key.obj(), openssl_key.get(), message, &signature); | |
585 ASSERT_TRUE( | |
586 CompareSignatureWithOpenSSL(message, signature, openssl_key.get())); | |
587 // All good. | |
588 } | |
589 | |
590 TEST(AndroidKeyStore,SignWithWrapperKeyRSA) { | |
591 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
592 | |
593 ScopedJava rsa_key = GetRSATestKeyJava(); | |
594 ASSERT_FALSE(rsa_key.is_null()); | |
595 | |
596 ScopedEVP_PKEY wrapper_key(GetOpenSSLPrivateKeyWrapper(rsa_key.obj())); | |
597 ASSERT_TRUE(wrapper_key.get() != NULL); | |
598 | |
599 ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestRsaKeyFile)); | |
600 ASSERT_TRUE(openssl_key.get()); | |
601 | |
602 // Check that RSA_size() works properly on the wrapper key. | |
603 EXPECT_EQ(EVP_PKEY_size(openssl_key.get()), | |
604 EVP_PKEY_size(wrapper_key.get())); | |
605 | |
606 // Message size must be 36 for RSA_sign(NID_md5_sha1,...) to return | |
607 // without an error. | |
608 std::string message = kTestRsaHash; | |
609 ASSERT_EQ(36U, message.size()); | |
610 | |
611 std::string signature; | |
612 DoKeySigningWithWrapper(wrapper_key.get(), | |
613 openssl_key.get(), | |
614 message, | |
615 &signature); | |
616 ASSERT_TRUE( | |
617 CompareSignatureWithOpenSSL(message, signature, openssl_key.get())); | |
618 } | |
619 | |
620 TEST(AndroidKeyStore,GetPrivateKeyTypeDSA) { | |
621 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
622 | |
623 ScopedJava dsa_key = GetDSATestKeyJava(); | |
624 ASSERT_FALSE(dsa_key.is_null()); | |
625 EXPECT_EQ(PRIVATE_KEY_TYPE_DSA, | |
626 GetPrivateKeyType(dsa_key.obj())); | |
627 } | |
628 | |
629 TEST(AndroidKeyStore,SignWithPrivateKeyDSA) { | |
630 ScopedJava dsa_key = GetDSATestKeyJava(); | |
631 ASSERT_FALSE(dsa_key.is_null()); | |
632 | |
633 ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile)); | |
634 ASSERT_TRUE(openssl_key.get()); | |
635 | |
636 std::string message = kTestDsaHash; | |
637 ASSERT_EQ(20U, message.size()); | |
638 | |
639 std::string signature; | |
640 DoKeySigning(dsa_key.obj(), openssl_key.get(), message, &signature); | |
641 ASSERT_TRUE(VerifyTestDSASignature(message, signature)); | |
642 } | |
643 | |
644 TEST(AndroidKeyStore,SignWithWrapperKeyDSA) { | |
645 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
646 | |
647 ScopedJava dsa_key = GetDSATestKeyJava(); | |
648 ASSERT_FALSE(dsa_key.is_null()); | |
649 | |
650 ScopedEVP_PKEY wrapper_key( | |
651 GetOpenSSLPrivateKeyWrapper(dsa_key.obj())); | |
652 ASSERT_TRUE(wrapper_key.get()); | |
653 | |
654 ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestDsaKeyFile)); | |
655 ASSERT_TRUE(openssl_key.get()); | |
656 | |
657 // Check that DSA_size() works correctly on the wrapper. | |
658 EXPECT_EQ(EVP_PKEY_size(openssl_key.get()), | |
659 EVP_PKEY_size(wrapper_key.get())); | |
660 | |
661 std::string message = kTestDsaHash; | |
662 std::string signature; | |
663 DoKeySigningWithWrapper(wrapper_key.get(), | |
664 openssl_key.get(), | |
665 message, | |
666 &signature); | |
667 ASSERT_TRUE(VerifyTestDSASignature(message, signature)); | |
668 } | |
669 | |
670 TEST(AndroidKeyStore,GetPrivateKeyTypeECDSA) { | |
671 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | |
672 | |
673 ScopedJava ecdsa_key = GetECDSATestKeyJava(); | |
674 ASSERT_FALSE(ecdsa_key.is_null()); | |
675 EXPECT_EQ(PRIVATE_KEY_TYPE_ECDSA, | |
676 GetPrivateKeyType(ecdsa_key.obj())); | |
677 } | |
678 | |
679 TEST(AndroidKeyStore,SignWithPrivateKeyECDSA) { | |
680 ScopedJava ecdsa_key = GetECDSATestKeyJava(); | |
681 ASSERT_FALSE(ecdsa_key.is_null()); | |
682 | |
683 ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile)); | |
684 ASSERT_TRUE(openssl_key.get()); | |
685 | |
686 std::string message = kTestEcdsaHash; | |
687 std::string signature; | |
688 DoKeySigning(ecdsa_key.obj(), openssl_key.get(), message, &signature); | |
689 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); | |
690 } | |
691 | |
692 TEST(AndroidKeyStore, SignWithWrapperKeyECDSA) { | |
693 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
694 | |
695 ScopedJava ecdsa_key = GetECDSATestKeyJava(); | |
696 ASSERT_FALSE(ecdsa_key.is_null()); | |
697 | |
698 ScopedEVP_PKEY wrapper_key( | |
699 GetOpenSSLPrivateKeyWrapper(ecdsa_key.obj())); | |
700 ASSERT_TRUE(wrapper_key.get()); | |
701 | |
702 ScopedEVP_PKEY openssl_key(ImportPrivateKeyFile(kTestEcdsaKeyFile)); | |
703 ASSERT_TRUE(openssl_key.get()); | |
704 | |
705 // Check that ECDSA size works correctly on the wrapper. | |
706 EXPECT_EQ(EVP_PKEY_size(openssl_key.get()), | |
707 EVP_PKEY_size(wrapper_key.get())); | |
708 | |
709 std::string message = kTestEcdsaHash; | |
710 std::string signature; | |
711 DoKeySigningWithWrapper(wrapper_key.get(), | |
712 openssl_key.get(), | |
713 message, | |
714 &signature); | |
715 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); | |
716 } | |
717 | |
718 } // namespace android | |
719 } // namespace net | |
OLD | NEW |