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> | |
13 | 12 |
14 #include "base/android/build_info.h" | 13 #include "base/android/build_info.h" |
15 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.h" |
16 #include "base/android/jni_array.h" | 15 #include "base/android/jni_array.h" |
17 #include "base/android/scoped_java_ref.h" | 16 #include "base/android/scoped_java_ref.h" |
18 #include "base/bind.h" | 17 #include "base/bind.h" |
19 #include "base/callback.h" | 18 #include "base/callback.h" |
20 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
21 #include "base/files/file_path.h" | 20 #include "base/files/file_path.h" |
22 #include "base/files/file_util.h" | 21 #include "base/files/file_util.h" |
23 #include "base/files/scoped_file.h" | 22 #include "base/files/scoped_file.h" |
24 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
25 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "crypto/auto_cbb.h" |
26 #include "crypto/openssl_util.h" | 26 #include "crypto/openssl_util.h" |
27 #include "net/android/keystore.h" | 27 #include "net/android/keystore.h" |
28 #include "net/android/keystore_openssl.h" | 28 #include "net/android/keystore_openssl.h" |
29 #include "net/base/test_data_directory.h" | 29 #include "net/base/test_data_directory.h" |
30 #include "net/ssl/scoped_openssl_types.h" | 30 #include "net/ssl/scoped_openssl_types.h" |
31 #include "net/test/jni/AndroidKeyStoreTestUtil_jni.h" | 31 #include "net/test/jni/AndroidKeyStoreTestUtil_jni.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
33 | 33 |
34 // Technical note: | 34 // Technical note: |
35 // | 35 // |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 } | 121 } |
122 return pkey; | 122 return pkey; |
123 } | 123 } |
124 | 124 |
125 // Convert a private key into its PKCS#8 encoded representation. | 125 // Convert a private key into its PKCS#8 encoded representation. |
126 // |pkey| is the EVP_PKEY handle for the private key. | 126 // |pkey| is the EVP_PKEY handle for the private key. |
127 // |pkcs8| will receive the PKCS#8 bytes. | 127 // |pkcs8| will receive the PKCS#8 bytes. |
128 // Returns true on success, false otherwise. | 128 // Returns true on success, false otherwise. |
129 bool GetPrivateKeyPkcs8Bytes(const crypto::ScopedEVP_PKEY& pkey, | 129 bool GetPrivateKeyPkcs8Bytes(const crypto::ScopedEVP_PKEY& pkey, |
130 std::string* pkcs8) { | 130 std::string* pkcs8) { |
131 // Convert to PKCS#8 object. | 131 uint8_t* der; |
132 ScopedPKCS8_PRIV_KEY_INFO p8_info(EVP_PKEY2PKCS8(pkey.get())); | 132 size_t der_len; |
133 if (!p8_info.get()) { | 133 crypto::AutoCBB cbb; |
134 LOG(ERROR) << "Can't get PKCS#8 private key from EVP_PKEY: " | 134 if (!CBB_init(cbb.get(), 0) || |
135 << GetOpenSSLErrorString(); | 135 !EVP_marshal_private_key(cbb.get(), pkey.get()) || |
| 136 !CBB_finish(cbb.get(), &der, &der_len)) { |
136 return false; | 137 return false; |
137 } | 138 } |
138 | 139 pkcs8->assign(reinterpret_cast<const char*>(der), der_len); |
139 // Then convert it | 140 OPENSSL_free(der); |
140 int len = i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), NULL); | |
141 unsigned char* p = OpenSSLWriteInto(pkcs8, static_cast<size_t>(len)); | |
142 i2d_PKCS8_PRIV_KEY_INFO(p8_info.get(), &p); | |
143 return true; | 141 return true; |
144 } | 142 } |
145 | 143 |
146 bool ImportPrivateKeyFileAsPkcs8(const char* filename, | 144 bool ImportPrivateKeyFileAsPkcs8(const char* filename, |
147 std::string* pkcs8) { | 145 std::string* pkcs8) { |
148 crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(filename)); | 146 crypto::ScopedEVP_PKEY pkey(ImportPrivateKeyFile(filename)); |
149 if (!pkey.get()) | 147 if (!pkey.get()) |
150 return false; | 148 return false; |
151 return GetPrivateKeyPkcs8Bytes(pkey, pkcs8); | 149 return GetPrivateKeyPkcs8Bytes(pkey, pkcs8); |
152 } | 150 } |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 std::string signature; | 546 std::string signature; |
549 DoKeySigningWithWrapper(wrapper_key.get(), | 547 DoKeySigningWithWrapper(wrapper_key.get(), |
550 openssl_key.get(), | 548 openssl_key.get(), |
551 message, | 549 message, |
552 &signature); | 550 &signature); |
553 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); | 551 ASSERT_TRUE(VerifyTestECDSASignature(message, signature)); |
554 } | 552 } |
555 | 553 |
556 } // namespace android | 554 } // namespace android |
557 } // namespace net | 555 } // namespace net |
OLD | NEW |