| 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 "net/ssl/openssl_client_key_store.h" | 5 #include "net/ssl/openssl_client_key_store.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "net/cert/asn1_util.h" | |
| 11 #include "net/cert/x509_certificate.h" | 10 #include "net/cert/x509_certificate.h" |
| 12 #include "net/ssl/ssl_private_key.h" | 11 #include "net/ssl/ssl_private_key.h" |
| 13 #include "third_party/boringssl/src/include/openssl/evp.h" | 12 #include "third_party/boringssl/src/include/openssl/evp.h" |
| 14 #include "third_party/boringssl/src/include/openssl/mem.h" | 13 #include "third_party/boringssl/src/include/openssl/mem.h" |
| 15 #include "third_party/boringssl/src/include/openssl/x509.h" | 14 #include "third_party/boringssl/src/include/openssl/x509.h" |
| 16 | 15 |
| 17 namespace net { | 16 namespace net { |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 // Serializes the SubjectPublicKeyInfo for |cert|. | 20 // Serializes the SubjectPublicKeyInfo for |cert|. |
| 22 bool GetCertificateSPKI(const X509Certificate* cert, std::string* spki) { | 21 bool GetCertificateSPKI(const X509Certificate* cert, std::string* spki) { |
| 23 #if BUILDFLAG(USE_BYTE_CERTS) | |
| 24 base::StringPiece cert_der( | |
| 25 reinterpret_cast<const char*>(CRYPTO_BUFFER_data(cert->os_cert_handle())), | |
| 26 CRYPTO_BUFFER_len(cert->os_cert_handle())); | |
| 27 base::StringPiece spki_tmp; | |
| 28 if (!asn1::ExtractSPKIFromDERCert(cert_der, &spki_tmp)) | |
| 29 return false; | |
| 30 spki_tmp.CopyToString(spki); | |
| 31 return true; | |
| 32 #else | |
| 33 bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(cert->os_cert_handle())); | 22 bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(cert->os_cert_handle())); |
| 34 if (!pkey) { | 23 if (!pkey) { |
| 35 LOG(ERROR) << "Can't extract private key from certificate!"; | 24 LOG(ERROR) << "Can't extract private key from certificate!"; |
| 36 return false; | 25 return false; |
| 37 } | 26 } |
| 38 | 27 |
| 39 bssl::ScopedCBB cbb; | 28 bssl::ScopedCBB cbb; |
| 40 uint8_t* der; | 29 uint8_t* der; |
| 41 size_t der_len; | 30 size_t der_len; |
| 42 if (!CBB_init(cbb.get(), 0) || | 31 if (!CBB_init(cbb.get(), 0) || |
| 43 !EVP_marshal_public_key(cbb.get(), pkey.get()) || | 32 !EVP_marshal_public_key(cbb.get(), pkey.get()) || |
| 44 !CBB_finish(cbb.get(), &der, &der_len)) { | 33 !CBB_finish(cbb.get(), &der, &der_len)) { |
| 45 return false; | 34 return false; |
| 46 } | 35 } |
| 47 | 36 |
| 48 spki->assign(reinterpret_cast<char*>(der), | 37 spki->assign(reinterpret_cast<char*>(der), |
| 49 reinterpret_cast<char*>(der) + der_len); | 38 reinterpret_cast<char*>(der) + der_len); |
| 50 OPENSSL_free(der); | 39 OPENSSL_free(der); |
| 51 return true; | 40 return true; |
| 52 #endif | |
| 53 } | 41 } |
| 54 | 42 |
| 55 } // namespace | 43 } // namespace |
| 56 | 44 |
| 57 OpenSSLClientKeyStore* OpenSSLClientKeyStore::GetInstance() { | 45 OpenSSLClientKeyStore* OpenSSLClientKeyStore::GetInstance() { |
| 58 return base::Singleton<OpenSSLClientKeyStore>::get(); | 46 return base::Singleton<OpenSSLClientKeyStore>::get(); |
| 59 } | 47 } |
| 60 | 48 |
| 61 bool OpenSSLClientKeyStore::RecordClientCertPrivateKey( | 49 bool OpenSSLClientKeyStore::RecordClientCertPrivateKey( |
| 62 const X509Certificate* client_cert, | 50 const X509Certificate* client_cert, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 89 | 77 |
| 90 void OpenSSLClientKeyStore::Flush() { | 78 void OpenSSLClientKeyStore::Flush() { |
| 91 key_map_.clear(); | 79 key_map_.clear(); |
| 92 } | 80 } |
| 93 | 81 |
| 94 OpenSSLClientKeyStore::OpenSSLClientKeyStore() {} | 82 OpenSSLClientKeyStore::OpenSSLClientKeyStore() {} |
| 95 | 83 |
| 96 OpenSSLClientKeyStore::~OpenSSLClientKeyStore() {} | 84 OpenSSLClientKeyStore::~OpenSSLClientKeyStore() {} |
| 97 | 85 |
| 98 } // namespace net | 86 } // namespace net |
| OLD | NEW |