OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <openssl/bn.h> | 5 #include <openssl/bn.h> |
6 #include <openssl/dsa.h> | 6 #include <openssl/dsa.h> |
7 #include <openssl/ecdsa.h> | 7 #include <openssl/ecdsa.h> |
8 #include <openssl/err.h> | 8 #include <openssl/err.h> |
9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
10 #include <openssl/pem.h> | 10 #include <openssl/pem.h> |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // Finally, it also checks that using the EVP_PKEY generated with | 51 // Finally, it also checks that using the EVP_PKEY generated with |
52 // GetOpenSSLPrivateKeyWrapper() works correctly. | 52 // GetOpenSSLPrivateKeyWrapper() works correctly. |
53 | 53 |
54 namespace net { | 54 namespace net { |
55 namespace android { | 55 namespace android { |
56 | 56 |
57 namespace { | 57 namespace { |
58 | 58 |
59 typedef base::android::ScopedJavaLocalRef<jobject> ScopedJava; | 59 typedef base::android::ScopedJavaLocalRef<jobject> ScopedJava; |
60 | 60 |
61 JNIEnv* InitEnv() { | |
62 JNIEnv* env = base::android::AttachCurrentThread(); | |
63 static bool inited = false; | |
64 if (!inited) { | |
65 RegisterNativesImpl(env); | |
66 inited = true; | |
67 } | |
68 return env; | |
69 } | |
70 | |
71 // Returns true if running on an Android version older than 4.2 | 61 // Returns true if running on an Android version older than 4.2 |
72 bool IsOnAndroidOlderThan_4_2(void) { | 62 bool IsOnAndroidOlderThan_4_2(void) { |
73 const int kAndroid42ApiLevel = 17; | 63 const int kAndroid42ApiLevel = 17; |
74 int level = base::android::BuildInfo::GetInstance()->sdk_int(); | 64 int level = base::android::BuildInfo::GetInstance()->sdk_int(); |
75 return level < kAndroid42ApiLevel; | 65 return level < kAndroid42ApiLevel; |
76 } | 66 } |
77 | 67 |
78 // Implements the callback expected by ERR_print_errors_cb(). | 68 // Implements the callback expected by ERR_print_errors_cb(). |
79 // used by GetOpenSSLErrorString below. | 69 // used by GetOpenSSLErrorString below. |
80 int openssl_print_error_callback(const char* msg, size_t msglen, void* u) { | 70 int openssl_print_error_callback(const char* msg, size_t msglen, void* u) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 LOG(ERROR) << "Could not load public key file: " << filename | 154 LOG(ERROR) << "Could not load public key file: " << filename |
165 << ", " << GetOpenSSLErrorString(); | 155 << ", " << GetOpenSSLErrorString(); |
166 return NULL; | 156 return NULL; |
167 } | 157 } |
168 return pkey; | 158 return pkey; |
169 } | 159 } |
170 | 160 |
171 // Retrieve a JNI local ref from encoded PKCS#8 data. | 161 // Retrieve a JNI local ref from encoded PKCS#8 data. |
172 ScopedJava GetPKCS8PrivateKeyJava(PrivateKeyType key_type, | 162 ScopedJava GetPKCS8PrivateKeyJava(PrivateKeyType key_type, |
173 const std::string& pkcs8_key) { | 163 const std::string& pkcs8_key) { |
174 JNIEnv* env = InitEnv(); | 164 JNIEnv* env = base::android::AttachCurrentThread(); |
175 base::android::ScopedJavaLocalRef<jbyteArray> bytes( | 165 base::android::ScopedJavaLocalRef<jbyteArray> bytes( |
176 base::android::ToJavaByteArray( | 166 base::android::ToJavaByteArray( |
177 env, reinterpret_cast<const uint8_t*>(pkcs8_key.data()), | 167 env, reinterpret_cast<const uint8_t*>(pkcs8_key.data()), |
178 pkcs8_key.size())); | 168 pkcs8_key.size())); |
179 | 169 |
180 ScopedJava key( | 170 ScopedJava key( |
181 Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8( | 171 Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8( |
182 env, key_type, bytes.obj())); | 172 env, key_type, bytes.obj())); |
183 | 173 |
184 return key; | 174 return key; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 391 |
402 result->assign( | 392 result->assign( |
403 reinterpret_cast<const char*>(&wrapper_signature[0]), | 393 reinterpret_cast<const char*>(&wrapper_signature[0]), |
404 wrapper_signature.size()); | 394 wrapper_signature.size()); |
405 } | 395 } |
406 | 396 |
407 } // namespace | 397 } // namespace |
408 | 398 |
409 TEST(AndroidKeyStore, GetRSAKeyModulus) { | 399 TEST(AndroidKeyStore, GetRSAKeyModulus) { |
410 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | 400 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
411 InitEnv(); | |
412 | 401 |
413 // Load the test RSA key. | 402 // Load the test RSA key. |
414 crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestRsaKeyFile)); | 403 crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(kTestRsaKeyFile)); |
415 ASSERT_TRUE(pkey.get()); | 404 ASSERT_TRUE(pkey.get()); |
416 | 405 |
417 // Convert it to encoded PKCS#8 bytes. | 406 // Convert it to encoded PKCS#8 bytes. |
418 std::string pkcs8_data; | 407 std::string pkcs8_data; |
419 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data)); | 408 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data)); |
420 | 409 |
421 // Create platform PrivateKey object from it. | 410 // Create platform PrivateKey object from it. |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 std::string signature; | 535 std::string signature; |
547 DoKeySigningWithWrapper(wrapper_key.get(), | 536 DoKeySigningWithWrapper(wrapper_key.get(), |
548 openssl_key.get(), | 537 openssl_key.get(), |
549 message, | 538 message, |
550 &signature); | 539 &signature); |
551 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); | 540 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); |
552 } | 541 } |
553 | 542 |
554 } // namespace android | 543 } // namespace android |
555 } // namespace net | 544 } // namespace net |
OLD | NEW |