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

Side by Side Diff: net/ssl/ssl_platform_key_nss.cc

Issue 2822283002: Remove SSLPrivateKey metadata hooks. (Closed)
Patch Set: emaxx comment Created 3 years, 8 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 unified diff | Download patch
« no previous file with comments | « net/ssl/ssl_platform_key_mac_unittest.cc ('k') | net/ssl/ssl_platform_key_nss_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <cert.h> 5 #include <cert.h>
6 #include <keyhi.h> 6 #include <keyhi.h>
7 #include <pk11pub.h> 7 #include <pk11pub.h>
8 #include <prerror.h> 8 #include <prerror.h>
9 9
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "crypto/scoped_nss_types.h" 15 #include "crypto/scoped_nss_types.h"
16 #include "net/cert/x509_certificate.h" 16 #include "net/cert/x509_certificate.h"
17 #include "net/ssl/client_key_store.h" 17 #include "net/ssl/client_key_store.h"
18 #include "net/ssl/ssl_platform_key.h" 18 #include "net/ssl/ssl_platform_key.h"
19 #include "net/ssl/ssl_platform_key_util.h" 19 #include "net/ssl/ssl_platform_key_util.h"
20 #include "net/ssl/ssl_private_key.h" 20 #include "net/ssl/ssl_private_key.h"
21 #include "net/ssl/threaded_ssl_private_key.h" 21 #include "net/ssl/threaded_ssl_private_key.h"
22 #include "third_party/boringssl/src/include/openssl/bn.h" 22 #include "third_party/boringssl/src/include/openssl/bn.h"
23 #include "third_party/boringssl/src/include/openssl/bytestring.h" 23 #include "third_party/boringssl/src/include/openssl/bytestring.h"
24 #include "third_party/boringssl/src/include/openssl/ec.h" 24 #include "third_party/boringssl/src/include/openssl/ec.h"
25 #include "third_party/boringssl/src/include/openssl/ec_key.h" 25 #include "third_party/boringssl/src/include/openssl/ec_key.h"
26 #include "third_party/boringssl/src/include/openssl/ecdsa.h" 26 #include "third_party/boringssl/src/include/openssl/ecdsa.h"
27 #include "third_party/boringssl/src/include/openssl/evp.h"
27 #include "third_party/boringssl/src/include/openssl/mem.h" 28 #include "third_party/boringssl/src/include/openssl/mem.h"
28 #include "third_party/boringssl/src/include/openssl/nid.h" 29 #include "third_party/boringssl/src/include/openssl/nid.h"
29 #include "third_party/boringssl/src/include/openssl/rsa.h" 30 #include "third_party/boringssl/src/include/openssl/rsa.h"
30 31
31 namespace net { 32 namespace net {
32 33
33 namespace { 34 namespace {
34 35
35 void LogPRError(const char* message) { 36 void LogPRError(const char* message) {
36 PRErrorCode err = PR_GetError(); 37 PRErrorCode err = PR_GetError();
37 const char* err_name = PR_ErrorToName(err); 38 const char* err_name = PR_ErrorToName(err);
38 if (err_name == nullptr) 39 if (err_name == nullptr)
39 err_name = ""; 40 err_name = "";
40 LOG(ERROR) << message << ": " << err << " (" << err_name << ")"; 41 LOG(ERROR) << message << ": " << err << " (" << err_name << ")";
41 } 42 }
42 43
43 class SSLPlatformKeyNSS : public ThreadedSSLPrivateKey::Delegate { 44 class SSLPlatformKeyNSS : public ThreadedSSLPrivateKey::Delegate {
44 public: 45 public:
45 SSLPlatformKeyNSS(SSLPrivateKey::Type type, 46 SSLPlatformKeyNSS(int type, crypto::ScopedSECKEYPrivateKey key)
46 size_t max_length, 47 : type_(type), key_(std::move(key)) {}
47 crypto::ScopedSECKEYPrivateKey key)
48 : type_(type), max_length_(max_length), key_(std::move(key)) {}
49 ~SSLPlatformKeyNSS() override {} 48 ~SSLPlatformKeyNSS() override {}
50 49
51 SSLPrivateKey::Type GetType() override { return type_; }
52
53 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { 50 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override {
54 static const SSLPrivateKey::Hash kHashes[] = { 51 static const SSLPrivateKey::Hash kHashes[] = {
55 SSLPrivateKey::Hash::SHA512, SSLPrivateKey::Hash::SHA384, 52 SSLPrivateKey::Hash::SHA512, SSLPrivateKey::Hash::SHA384,
56 SSLPrivateKey::Hash::SHA256, SSLPrivateKey::Hash::SHA1}; 53 SSLPrivateKey::Hash::SHA256, SSLPrivateKey::Hash::SHA1};
57 return std::vector<SSLPrivateKey::Hash>(kHashes, 54 return std::vector<SSLPrivateKey::Hash>(kHashes,
58 kHashes + arraysize(kHashes)); 55 kHashes + arraysize(kHashes));
59 } 56 }
60 57
61 size_t GetMaxSignatureLengthInBytes() override { return max_length_; }
62
63 Error SignDigest(SSLPrivateKey::Hash hash, 58 Error SignDigest(SSLPrivateKey::Hash hash,
64 const base::StringPiece& input, 59 const base::StringPiece& input,
65 std::vector<uint8_t>* signature) override { 60 std::vector<uint8_t>* signature) override {
66 SECItem digest_item; 61 SECItem digest_item;
67 digest_item.data = 62 digest_item.data =
68 const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(input.data())); 63 const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(input.data()));
69 digest_item.len = input.size(); 64 digest_item.len = input.size();
70 65
71 bssl::UniquePtr<uint8_t> free_digest_info; 66 bssl::UniquePtr<uint8_t> free_digest_info;
72 if (type_ == SSLPrivateKey::Type::RSA) { 67 if (type_ == EVP_PKEY_RSA) {
73 // PK11_Sign expects the caller to prepend the DigestInfo. 68 // PK11_Sign expects the caller to prepend the DigestInfo.
74 int hash_nid = NID_undef; 69 int hash_nid = NID_undef;
75 switch (hash) { 70 switch (hash) {
76 case SSLPrivateKey::Hash::MD5_SHA1: 71 case SSLPrivateKey::Hash::MD5_SHA1:
77 hash_nid = NID_md5_sha1; 72 hash_nid = NID_md5_sha1;
78 break; 73 break;
79 case SSLPrivateKey::Hash::SHA1: 74 case SSLPrivateKey::Hash::SHA1:
80 hash_nid = NID_sha1; 75 hash_nid = NID_sha1;
81 break; 76 break;
82 case SSLPrivateKey::Hash::SHA256: 77 case SSLPrivateKey::Hash::SHA256:
(...skipping 30 matching lines...) Expand all
113 108
114 SECStatus rv = PK11_Sign(key_.get(), &signature_item, &digest_item); 109 SECStatus rv = PK11_Sign(key_.get(), &signature_item, &digest_item);
115 if (rv != SECSuccess) { 110 if (rv != SECSuccess) {
116 LogPRError("PK11_Sign failed"); 111 LogPRError("PK11_Sign failed");
117 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; 112 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
118 } 113 }
119 signature->resize(signature_item.len); 114 signature->resize(signature_item.len);
120 115
121 // NSS emits raw ECDSA signatures, but BoringSSL expects a DER-encoded 116 // NSS emits raw ECDSA signatures, but BoringSSL expects a DER-encoded
122 // ECDSA-Sig-Value. 117 // ECDSA-Sig-Value.
123 if (SSLPrivateKey::IsECDSAType(type_)) { 118 if (type_ == EVP_PKEY_EC) {
124 if (signature->size() % 2 != 0) { 119 if (signature->size() % 2 != 0) {
125 LOG(ERROR) << "Bad signature length"; 120 LOG(ERROR) << "Bad signature length";
126 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; 121 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
127 } 122 }
128 size_t order_len = signature->size() / 2; 123 size_t order_len = signature->size() / 2;
129 124
130 // Convert the RAW ECDSA signature to a DER-encoded ECDSA-Sig-Value. 125 // Convert the RAW ECDSA signature to a DER-encoded ECDSA-Sig-Value.
131 bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new()); 126 bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
132 if (!sig || !BN_bin2bn(signature->data(), order_len, sig->r) || 127 if (!sig || !BN_bin2bn(signature->data(), order_len, sig->r) ||
133 !BN_bin2bn(signature->data() + order_len, order_len, sig->s)) { 128 !BN_bin2bn(signature->data() + order_len, order_len, sig->s)) {
134 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; 129 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
135 } 130 }
136 131
137 int len = i2d_ECDSA_SIG(sig.get(), nullptr); 132 int len = i2d_ECDSA_SIG(sig.get(), nullptr);
138 if (len <= 0) 133 if (len <= 0)
139 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; 134 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
140 signature->resize(len); 135 signature->resize(len);
141 uint8_t* ptr = signature->data(); 136 uint8_t* ptr = signature->data();
142 len = i2d_ECDSA_SIG(sig.get(), &ptr); 137 len = i2d_ECDSA_SIG(sig.get(), &ptr);
143 if (len <= 0) 138 if (len <= 0)
144 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED; 139 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
145 signature->resize(len); 140 signature->resize(len);
146 } 141 }
147 142
148 return OK; 143 return OK;
149 } 144 }
150 145
151 private: 146 private:
152 SSLPrivateKey::Type type_; 147 int type_;
153 size_t max_length_;
154 crypto::ScopedSECKEYPrivateKey key_; 148 crypto::ScopedSECKEYPrivateKey key_;
155 149
156 DISALLOW_COPY_AND_ASSIGN(SSLPlatformKeyNSS); 150 DISALLOW_COPY_AND_ASSIGN(SSLPlatformKeyNSS);
157 }; 151 };
158 152
159 } // namespace 153 } // namespace
160 154
161 scoped_refptr<SSLPrivateKey> FetchClientCertPrivateKey( 155 scoped_refptr<SSLPrivateKey> FetchClientCertPrivateKey(
162 const X509Certificate* certificate) { 156 const X509Certificate* certificate) {
163 crypto::ScopedSECKEYPrivateKey key( 157 crypto::ScopedSECKEYPrivateKey key(
164 PK11_FindKeyByAnyCert(certificate->os_cert_handle(), nullptr)); 158 PK11_FindKeyByAnyCert(certificate->os_cert_handle(), nullptr));
165 if (!key) { 159 if (!key) {
166 return ClientKeyStore::GetInstance()->FetchClientCertPrivateKey( 160 return ClientKeyStore::GetInstance()->FetchClientCertPrivateKey(
167 *certificate); 161 *certificate);
168 } 162 }
169 163
170 SSLPrivateKey::Type type; 164 int type;
171 size_t max_length; 165 size_t max_length;
172 if (!GetClientCertInfo(certificate, &type, &max_length)) 166 if (!GetClientCertInfo(certificate, &type, &max_length))
173 return nullptr; 167 return nullptr;
174 168
175 return make_scoped_refptr(new ThreadedSSLPrivateKey( 169 return make_scoped_refptr(new ThreadedSSLPrivateKey(
176 base::MakeUnique<SSLPlatformKeyNSS>(type, max_length, std::move(key)), 170 base::MakeUnique<SSLPlatformKeyNSS>(type, std::move(key)),
177 GetSSLPlatformKeyTaskRunner())); 171 GetSSLPlatformKeyTaskRunner()));
178 } 172 }
179 173
180 } // namespace net 174 } // namespace net
OLDNEW
« no previous file with comments | « net/ssl/ssl_platform_key_mac_unittest.cc ('k') | net/ssl/ssl_platform_key_nss_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698