| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/base/openssl_private_key_store.h" | 5 #include "net/base/openssl_private_key_store.h" |
| 6 | 6 |
| 7 #include <openssl/bytestring.h> | 7 #include <openssl/bytestring.h> |
| 8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
| 9 #include <openssl/mem.h> | 9 #include <openssl/mem.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 13 #include "crypto/auto_cbb.h" | |
| 14 #include "crypto/openssl_util.h" | 13 #include "crypto/openssl_util.h" |
| 15 #include "crypto/scoped_openssl_types.h" | |
| 16 #include "net/android/network_library.h" | 14 #include "net/android/network_library.h" |
| 17 | 15 |
| 18 namespace net { | 16 namespace net { |
| 19 | 17 |
| 20 bool OpenSSLPrivateKeyStore::StoreKeyPair(const GURL& url, EVP_PKEY* pkey) { | 18 bool OpenSSLPrivateKeyStore::StoreKeyPair(const GURL& url, EVP_PKEY* pkey) { |
| 21 // Always clear openssl errors on exit. | 19 // Always clear openssl errors on exit. |
| 22 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); | 20 crypto::OpenSSLErrStackTracer err_trace(FROM_HERE); |
| 23 | 21 |
| 24 uint8_t* public_key; | 22 uint8_t* public_key; |
| 25 size_t public_len; | 23 size_t public_len; |
| 26 crypto::AutoCBB cbb; | 24 bssl::ScopedCBB cbb; |
| 27 if (!CBB_init(cbb.get(), 0) || !EVP_marshal_public_key(cbb.get(), pkey) || | 25 if (!CBB_init(cbb.get(), 0) || !EVP_marshal_public_key(cbb.get(), pkey) || |
| 28 !CBB_finish(cbb.get(), &public_key, &public_len)) { | 26 !CBB_finish(cbb.get(), &public_key, &public_len)) { |
| 29 return false; | 27 return false; |
| 30 } | 28 } |
| 31 crypto::ScopedOpenSSLBytes free_public_key(public_key); | 29 bssl::UniquePtr<uint8_t> free_public_key(public_key); |
| 32 | 30 |
| 33 uint8_t* private_key; | 31 uint8_t* private_key; |
| 34 size_t private_len; | 32 size_t private_len; |
| 35 cbb.Reset(); | 33 cbb.Reset(); |
| 36 if (!CBB_init(cbb.get(), 0) || !EVP_marshal_private_key(cbb.get(), pkey) || | 34 if (!CBB_init(cbb.get(), 0) || !EVP_marshal_private_key(cbb.get(), pkey) || |
| 37 !CBB_finish(cbb.get(), &private_key, &private_len)) { | 35 !CBB_finish(cbb.get(), &private_key, &private_len)) { |
| 38 return false; | 36 return false; |
| 39 } | 37 } |
| 40 crypto::ScopedOpenSSLBytes free_private_key(private_key); | 38 bssl::UniquePtr<uint8_t> free_private_key(private_key); |
| 41 | 39 |
| 42 if (!android::StoreKeyPair(public_key, public_len, private_key, | 40 if (!android::StoreKeyPair(public_key, public_len, private_key, |
| 43 private_len)) { | 41 private_len)) { |
| 44 LOG(ERROR) << "StoreKeyPair failed. public_len = " << public_len | 42 LOG(ERROR) << "StoreKeyPair failed. public_len = " << public_len |
| 45 << " private_len = " << private_len; | 43 << " private_len = " << private_len; |
| 46 } | 44 } |
| 47 return true; | 45 return true; |
| 48 } | 46 } |
| 49 | 47 |
| 50 } // namespace net | 48 } // namespace net |
| OLD | NEW |