OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/ssl/client_cert_identity_test_util.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "net/ssl/ssl_private_key.h" |
| 11 #include "net/ssl/test_ssl_private_key.h" |
| 12 #include "net/test/cert_test_util.h" |
| 13 #include "third_party/boringssl/src/include/openssl/bytestring.h" |
| 14 #include "third_party/boringssl/src/include/openssl/evp.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 FakeClientCertIdentity::FakeClientCertIdentity( |
| 19 scoped_refptr<X509Certificate> cert, |
| 20 scoped_refptr<SSLPrivateKey> key) |
| 21 : ClientCertIdentity(std::move(cert)), key_(std::move(key)) {} |
| 22 |
| 23 FakeClientCertIdentity::~FakeClientCertIdentity() = default; |
| 24 |
| 25 // static |
| 26 std::unique_ptr<FakeClientCertIdentity> |
| 27 FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 28 const base::FilePath& dir, |
| 29 const std::string& cert_filename, |
| 30 const std::string& key_filename) { |
| 31 scoped_refptr<X509Certificate> cert = |
| 32 net::ImportCertFromFile(dir, cert_filename); |
| 33 if (!cert) |
| 34 return nullptr; |
| 35 |
| 36 std::string pkcs8; |
| 37 if (!base::ReadFileToString(dir.AppendASCII(key_filename), &pkcs8)) |
| 38 return nullptr; |
| 39 |
| 40 CBS cbs; |
| 41 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(pkcs8.data()), pkcs8.size()); |
| 42 bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs)); |
| 43 if (!pkey || CBS_len(&cbs) != 0) |
| 44 return nullptr; |
| 45 |
| 46 scoped_refptr<SSLPrivateKey> ssl_private_key = |
| 47 WrapOpenSSLPrivateKey(std::move(pkey)); |
| 48 if (!ssl_private_key) |
| 49 return nullptr; |
| 50 |
| 51 return base::MakeUnique<FakeClientCertIdentity>(cert, ssl_private_key); |
| 52 } |
| 53 |
| 54 std::unique_ptr<FakeClientCertIdentity> FakeClientCertIdentity::Copy() { |
| 55 return base::MakeUnique<FakeClientCertIdentity>(certificate(), key_); |
| 56 } |
| 57 |
| 58 void FakeClientCertIdentity::AcquirePrivateKey( |
| 59 const base::Callback<void(scoped_refptr<SSLPrivateKey>)>& |
| 60 private_key_callback) { |
| 61 private_key_callback.Run(key_); |
| 62 } |
| 63 |
| 64 #if defined(OS_MACOSX) |
| 65 SecIdentityRef FakeClientCertIdentity::sec_identity_ref() const { |
| 66 // Any tests that depend on having a real SecIdentityRef should use a real |
| 67 // ClientCertIdentityMac. |
| 68 NOTREACHED(); |
| 69 return nullptr; |
| 70 } |
| 71 #endif |
| 72 |
| 73 ClientCertIdentityList FakeClientCertIdentityListFromCertificateList( |
| 74 const CertificateList& certs) { |
| 75 ClientCertIdentityList result; |
| 76 for (const auto& cert : certs) { |
| 77 result.push_back(base::MakeUnique<FakeClientCertIdentity>(cert, nullptr)); |
| 78 } |
| 79 return result; |
| 80 } |
| 81 |
| 82 } // namespace net |
OLD | NEW |