Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Unified Diff: net/ssl/client_cert_identity_test_util.cc

Issue 2898573002: Refactor client cert private key handling. (Closed)
Patch Set: removed no longer needed forward declaration Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/ssl/client_cert_identity_test_util.h ('k') | net/ssl/client_cert_identity_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/ssl/client_cert_identity_test_util.cc
diff --git a/net/ssl/client_cert_identity_test_util.cc b/net/ssl/client_cert_identity_test_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..2a3873f6539135f8b0c091bd7f593b48a0836856
--- /dev/null
+++ b/net/ssl/client_cert_identity_test_util.cc
@@ -0,0 +1,82 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/ssl/client_cert_identity_test_util.h"
+
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/memory/ptr_util.h"
+#include "net/ssl/ssl_private_key.h"
+#include "net/ssl/test_ssl_private_key.h"
+#include "net/test/cert_test_util.h"
+#include "third_party/boringssl/src/include/openssl/bytestring.h"
+#include "third_party/boringssl/src/include/openssl/evp.h"
+
+namespace net {
+
+FakeClientCertIdentity::FakeClientCertIdentity(
+ scoped_refptr<X509Certificate> cert,
+ scoped_refptr<SSLPrivateKey> key)
+ : ClientCertIdentity(std::move(cert)), key_(std::move(key)) {}
+
+FakeClientCertIdentity::~FakeClientCertIdentity() = default;
+
+// static
+std::unique_ptr<FakeClientCertIdentity>
+FakeClientCertIdentity::CreateFromCertAndKeyFiles(
+ const base::FilePath& dir,
+ const std::string& cert_filename,
+ const std::string& key_filename) {
+ scoped_refptr<X509Certificate> cert =
+ net::ImportCertFromFile(dir, cert_filename);
+ if (!cert)
+ return nullptr;
+
+ std::string pkcs8;
+ if (!base::ReadFileToString(dir.AppendASCII(key_filename), &pkcs8))
+ return nullptr;
+
+ CBS cbs;
+ CBS_init(&cbs, reinterpret_cast<const uint8_t*>(pkcs8.data()), pkcs8.size());
+ bssl::UniquePtr<EVP_PKEY> pkey(EVP_parse_private_key(&cbs));
+ if (!pkey || CBS_len(&cbs) != 0)
+ return nullptr;
+
+ scoped_refptr<SSLPrivateKey> ssl_private_key =
+ WrapOpenSSLPrivateKey(std::move(pkey));
+ if (!ssl_private_key)
+ return nullptr;
+
+ return base::MakeUnique<FakeClientCertIdentity>(cert, ssl_private_key);
+}
+
+std::unique_ptr<FakeClientCertIdentity> FakeClientCertIdentity::Copy() {
+ return base::MakeUnique<FakeClientCertIdentity>(certificate(), key_);
+}
+
+void FakeClientCertIdentity::AcquirePrivateKey(
+ const base::Callback<void(scoped_refptr<SSLPrivateKey>)>&
+ private_key_callback) {
+ private_key_callback.Run(key_);
+}
+
+#if defined(OS_MACOSX)
+SecIdentityRef FakeClientCertIdentity::sec_identity_ref() const {
+ // Any tests that depend on having a real SecIdentityRef should use a real
+ // ClientCertIdentityMac.
+ NOTREACHED();
+ return nullptr;
+}
+#endif
+
+ClientCertIdentityList FakeClientCertIdentityListFromCertificateList(
+ const CertificateList& certs) {
+ ClientCertIdentityList result;
+ for (const auto& cert : certs) {
+ result.push_back(base::MakeUnique<FakeClientCertIdentity>(cert, nullptr));
+ }
+ return result;
+}
+
+} // namespace net
« no previous file with comments | « net/ssl/client_cert_identity_test_util.h ('k') | net/ssl/client_cert_identity_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698