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> |
11 #include <openssl/rsa.h> | 11 #include <openssl/rsa.h> |
12 #include <openssl/x509.h> | 12 #include <openssl/x509.h> |
13 | 13 |
14 #include "base/android/build_info.h" | 14 #include "base/android/build_info.h" |
15 #include "base/android/jni_android.h" | 15 #include "base/android/jni_android.h" |
16 #include "base/android/jni_array.h" | 16 #include "base/android/jni_array.h" |
17 #include "base/android/scoped_java_ref.h" | 17 #include "base/android/scoped_java_ref.h" |
18 #include "base/basictypes.h" | |
19 #include "base/bind.h" | 18 #include "base/bind.h" |
20 #include "base/callback.h" | 19 #include "base/callback.h" |
21 #include "base/compiler_specific.h" | 20 #include "base/compiler_specific.h" |
22 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
23 #include "base/files/file_util.h" | 22 #include "base/files/file_util.h" |
24 #include "base/files/scoped_file.h" | 23 #include "base/files/scoped_file.h" |
25 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
26 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
27 #include "crypto/openssl_util.h" | 26 #include "crypto/openssl_util.h" |
28 #include "jni/AndroidKeyStoreTestUtil_jni.h" | 27 #include "jni/AndroidKeyStoreTestUtil_jni.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 169 } |
171 return pkey; | 170 return pkey; |
172 } | 171 } |
173 | 172 |
174 // Retrieve a JNI local ref from encoded PKCS#8 data. | 173 // Retrieve a JNI local ref from encoded PKCS#8 data. |
175 ScopedJava GetPKCS8PrivateKeyJava(PrivateKeyType key_type, | 174 ScopedJava GetPKCS8PrivateKeyJava(PrivateKeyType key_type, |
176 const std::string& pkcs8_key) { | 175 const std::string& pkcs8_key) { |
177 JNIEnv* env = InitEnv(); | 176 JNIEnv* env = InitEnv(); |
178 base::android::ScopedJavaLocalRef<jbyteArray> bytes( | 177 base::android::ScopedJavaLocalRef<jbyteArray> bytes( |
179 base::android::ToJavaByteArray( | 178 base::android::ToJavaByteArray( |
180 env, | 179 env, reinterpret_cast<const uint8_t*>(pkcs8_key.data()), |
181 reinterpret_cast<const uint8*>(pkcs8_key.data()), | |
182 pkcs8_key.size())); | 180 pkcs8_key.size())); |
183 | 181 |
184 ScopedJava key( | 182 ScopedJava key( |
185 Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8( | 183 Java_AndroidKeyStoreTestUtil_createPrivateKeyFromPKCS8( |
186 env, key_type, bytes.obj())); | 184 env, key_type, bytes.obj())); |
187 | 185 |
188 return key; | 186 return key; |
189 } | 187 } |
190 | 188 |
191 const char kTestRsaKeyFile[] = "android-test-key-rsa.pem"; | 189 const char kTestRsaKeyFile[] = "android-test-key-rsa.pem"; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 // |android_key| is a JNI reference to the platform PrivateKey object. | 366 // |android_key| is a JNI reference to the platform PrivateKey object. |
369 // |openssl_key| is a pointer to an OpenSSL key object for the exact | 367 // |openssl_key| is a pointer to an OpenSSL key object for the exact |
370 // same key content. | 368 // same key content. |
371 // |message| is a message. | 369 // |message| is a message. |
372 // |result| will receive the result. | 370 // |result| will receive the result. |
373 void DoKeySigning(jobject android_key, | 371 void DoKeySigning(jobject android_key, |
374 EVP_PKEY* openssl_key, | 372 EVP_PKEY* openssl_key, |
375 const base::StringPiece& message, | 373 const base::StringPiece& message, |
376 std::string* result) { | 374 std::string* result) { |
377 // First, get the platform signature. | 375 // First, get the platform signature. |
378 std::vector<uint8> android_signature; | 376 std::vector<uint8_t> android_signature; |
379 ASSERT_TRUE( | 377 ASSERT_TRUE( |
380 RawSignDigestWithPrivateKey(android_key, | 378 RawSignDigestWithPrivateKey(android_key, |
381 message, | 379 message, |
382 &android_signature)); | 380 &android_signature)); |
383 | 381 |
384 result->assign( | 382 result->assign( |
385 reinterpret_cast<const char*>(&android_signature[0]), | 383 reinterpret_cast<const char*>(&android_signature[0]), |
386 android_signature.size()); | 384 android_signature.size()); |
387 } | 385 } |
388 | 386 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 // Convert it to encoded PKCS#8 bytes. | 419 // Convert it to encoded PKCS#8 bytes. |
422 std::string pkcs8_data; | 420 std::string pkcs8_data; |
423 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data)); | 421 ASSERT_TRUE(GetPrivateKeyPkcs8Bytes(pkey, &pkcs8_data)); |
424 | 422 |
425 // Create platform PrivateKey object from it. | 423 // Create platform PrivateKey object from it. |
426 ScopedJava key_java = GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA, | 424 ScopedJava key_java = GetPKCS8PrivateKeyJava(PRIVATE_KEY_TYPE_RSA, |
427 pkcs8_data); | 425 pkcs8_data); |
428 ASSERT_FALSE(key_java.is_null()); | 426 ASSERT_FALSE(key_java.is_null()); |
429 | 427 |
430 // Retrieve the corresponding modulus through JNI | 428 // Retrieve the corresponding modulus through JNI |
431 std::vector<uint8> modulus_java; | 429 std::vector<uint8_t> modulus_java; |
432 ASSERT_TRUE(GetRSAKeyModulus(key_java.obj(), &modulus_java)); | 430 ASSERT_TRUE(GetRSAKeyModulus(key_java.obj(), &modulus_java)); |
433 | 431 |
434 // Create an OpenSSL BIGNUM from it. | 432 // Create an OpenSSL BIGNUM from it. |
435 crypto::ScopedBIGNUM bn( | 433 crypto::ScopedBIGNUM bn( |
436 BN_bin2bn(reinterpret_cast<const unsigned char*>(&modulus_java[0]), | 434 BN_bin2bn(reinterpret_cast<const unsigned char*>(&modulus_java[0]), |
437 static_cast<int>(modulus_java.size()), | 435 static_cast<int>(modulus_java.size()), |
438 NULL)); | 436 NULL)); |
439 ASSERT_TRUE(bn.get()); | 437 ASSERT_TRUE(bn.get()); |
440 | 438 |
441 // Compare it to the one in the RSA key, they must be identical. | 439 // Compare it to the one in the RSA key, they must be identical. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 std::string signature; | 548 std::string signature; |
551 DoKeySigningWithWrapper(wrapper_key.get(), | 549 DoKeySigningWithWrapper(wrapper_key.get(), |
552 openssl_key.get(), | 550 openssl_key.get(), |
553 message, | 551 message, |
554 &signature); | 552 &signature); |
555 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); | 553 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); |
556 } | 554 } |
557 | 555 |
558 } // namespace android | 556 } // namespace android |
559 } // namespace net | 557 } // namespace net |
OLD | NEW |