• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
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/ssl_platform_key_nss.h"
6 
7 #include <cert.h>
8 #include <keyhi.h>
9 #include <pk11pub.h>
10 #include <prerror.h>
11 #include <secmodt.h>
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "base/logging.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/threading/scoped_blocking_call.h"
19 #include "crypto/nss_crypto_module_delegate.h"
20 #include "crypto/scoped_nss_types.h"
21 #include "net/cert/x509_certificate.h"
22 #include "net/ssl/ssl_platform_key_util.h"
23 #include "net/ssl/ssl_private_key.h"
24 #include "net/ssl/threaded_ssl_private_key.h"
25 #include "third_party/boringssl/src/include/openssl/bn.h"
26 #include "third_party/boringssl/src/include/openssl/bytestring.h"
27 #include "third_party/boringssl/src/include/openssl/ec.h"
28 #include "third_party/boringssl/src/include/openssl/ec_key.h"
29 #include "third_party/boringssl/src/include/openssl/ecdsa.h"
30 #include "third_party/boringssl/src/include/openssl/evp.h"
31 #include "third_party/boringssl/src/include/openssl/mem.h"
32 #include "third_party/boringssl/src/include/openssl/nid.h"
33 #include "third_party/boringssl/src/include/openssl/rsa.h"
34 #include "third_party/boringssl/src/include/openssl/ssl.h"
35 
36 namespace net {
37 
38 namespace {
39 
LogPRError(const char * message)40 void LogPRError(const char* message) {
41   PRErrorCode err = PR_GetError();
42   const char* err_name = PR_ErrorToName(err);
43   if (err_name == nullptr)
44     err_name = "";
45   LOG(ERROR) << message << ": " << err << " (" << err_name << ")";
46 }
47 
48 class SSLPlatformKeyNSS : public ThreadedSSLPrivateKey::Delegate {
49  public:
SSLPlatformKeyNSS(int type,scoped_refptr<crypto::CryptoModuleBlockingPasswordDelegate> password_delegate,crypto::ScopedSECKEYPrivateKey key)50   SSLPlatformKeyNSS(int type,
51                     scoped_refptr<crypto::CryptoModuleBlockingPasswordDelegate>
52                         password_delegate,
53                     crypto::ScopedSECKEYPrivateKey key)
54       : type_(type),
55         password_delegate_(std::move(password_delegate)),
56         key_(std::move(key)),
57         supports_pss_(PK11_DoesMechanism(key_->pkcs11Slot, CKM_RSA_PKCS_PSS)) {}
58 
59   SSLPlatformKeyNSS(const SSLPlatformKeyNSS&) = delete;
60   SSLPlatformKeyNSS& operator=(const SSLPlatformKeyNSS&) = delete;
61 
62   ~SSLPlatformKeyNSS() override = default;
63 
GetProviderName()64   std::string GetProviderName() override {
65     // This logic accesses fields directly on the struct, so it may run on any
66     // thread without caching.
67     return base::StringPrintf("%s, %s",
68                               PK11_GetModule(key_->pkcs11Slot)->commonName,
69                               PK11_GetSlotName(key_->pkcs11Slot));
70   }
71 
GetAlgorithmPreferences()72   std::vector<uint16_t> GetAlgorithmPreferences() override {
73     return SSLPrivateKey::DefaultAlgorithmPreferences(type_, supports_pss_);
74   }
75 
Sign(uint16_t algorithm,base::span<const uint8_t> input,std::vector<uint8_t> * signature)76   Error Sign(uint16_t algorithm,
77              base::span<const uint8_t> input,
78              std::vector<uint8_t>* signature) override {
79     const EVP_MD* md = SSL_get_signature_algorithm_digest(algorithm);
80     uint8_t digest[EVP_MAX_MD_SIZE];
81     unsigned digest_len;
82     if (!md || !EVP_Digest(input.data(), input.size(), digest, &digest_len, md,
83                            nullptr)) {
84       return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
85     }
86     SECItem digest_item;
87     digest_item.data = digest;
88     digest_item.len = digest_len;
89 
90     CK_MECHANISM_TYPE mechanism = PK11_MapSignKeyType(key_->keyType);
91     SECItem param = {siBuffer, nullptr, 0};
92     CK_RSA_PKCS_PSS_PARAMS pss_params;
93     bssl::UniquePtr<uint8_t> free_digest_info;
94     if (SSL_is_signature_algorithm_rsa_pss(algorithm)) {
95       switch (EVP_MD_type(md)) {
96         case NID_sha256:
97           pss_params.hashAlg = CKM_SHA256;
98           pss_params.mgf = CKG_MGF1_SHA256;
99           break;
100         case NID_sha384:
101           pss_params.hashAlg = CKM_SHA384;
102           pss_params.mgf = CKG_MGF1_SHA384;
103           break;
104         case NID_sha512:
105           pss_params.hashAlg = CKM_SHA512;
106           pss_params.mgf = CKG_MGF1_SHA512;
107           break;
108         default:
109           LOG(ERROR) << "Unexpected hash algorithm";
110           return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
111       }
112       // Use the hash length for the salt length.
113       pss_params.sLen = EVP_MD_size(md);
114       mechanism = CKM_RSA_PKCS_PSS;
115       param.data = reinterpret_cast<unsigned char*>(&pss_params);
116       param.len = sizeof(pss_params);
117     } else if (SSL_get_signature_algorithm_key_type(algorithm) ==
118                EVP_PKEY_RSA) {
119       // PK11_SignWithMechanism expects the caller to prepend the DigestInfo for
120       // PKCS #1.
121       int hash_nid = EVP_MD_type(SSL_get_signature_algorithm_digest(algorithm));
122       int is_alloced;
123       size_t prefix_len;
124       if (!RSA_add_pkcs1_prefix(&digest_item.data, &prefix_len, &is_alloced,
125                                 hash_nid, digest_item.data, digest_item.len)) {
126         return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
127       }
128       digest_item.len = prefix_len;
129       if (is_alloced)
130         free_digest_info.reset(digest_item.data);
131     }
132 
133     {
134       const int len = PK11_SignatureLen(key_.get());
135       if (len <= 0) {
136         LogPRError("PK11_SignatureLen failed");
137         return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
138       }
139       signature->resize(len);
140       SECItem signature_item;
141       signature_item.data = signature->data();
142       signature_item.len = signature->size();
143 
144       SECStatus rv = PK11_SignWithMechanism(key_.get(), mechanism, &param,
145                                             &signature_item, &digest_item);
146       if (rv != SECSuccess) {
147         LogPRError("PK11_SignWithMechanism failed");
148         return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
149       }
150       signature->resize(signature_item.len);
151     }
152 
153     // NSS emits raw ECDSA signatures, but BoringSSL expects a DER-encoded
154     // ECDSA-Sig-Value.
155     if (SSL_get_signature_algorithm_key_type(algorithm) == EVP_PKEY_EC) {
156       if (signature->size() % 2 != 0) {
157         LOG(ERROR) << "Bad signature length";
158         return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
159       }
160       size_t order_len = signature->size() / 2;
161 
162       // Convert the RAW ECDSA signature to a DER-encoded ECDSA-Sig-Value.
163       bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
164       if (!sig || !BN_bin2bn(signature->data(), order_len, sig->r) ||
165           !BN_bin2bn(signature->data() + order_len, order_len, sig->s)) {
166         return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
167       }
168 
169       {
170         const int len = i2d_ECDSA_SIG(sig.get(), nullptr);
171         if (len <= 0)
172           return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
173         signature->resize(len);
174       }
175 
176       {
177         uint8_t* ptr = signature->data();
178         const int len = i2d_ECDSA_SIG(sig.get(), &ptr);
179         if (len <= 0)
180           return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
181         signature->resize(len);
182       }
183     }
184 
185     return OK;
186   }
187 
188  private:
189   int type_;
190   // NSS retains a pointer to the password delegate, so retain a reference here
191   // to ensure the lifetimes are correct.
192   scoped_refptr<crypto::CryptoModuleBlockingPasswordDelegate>
193       password_delegate_;
194   crypto::ScopedSECKEYPrivateKey key_;
195   bool supports_pss_;
196 };
197 
198 }  // namespace
199 
FetchClientCertPrivateKey(const X509Certificate * certificate,CERTCertificate * cert_certificate,scoped_refptr<crypto::CryptoModuleBlockingPasswordDelegate> password_delegate)200 scoped_refptr<SSLPrivateKey> FetchClientCertPrivateKey(
201     const X509Certificate* certificate,
202     CERTCertificate* cert_certificate,
203     scoped_refptr<crypto::CryptoModuleBlockingPasswordDelegate>
204         password_delegate) {
205   // This function may acquire the NSS lock or reenter this code via extension
206   // hooks (such as smart card UI). To ensure threads are not starved or
207   // deadlocked, the base::ScopedBlockingCall below increments the thread pool
208   // capacity if this method takes too much time to run.
209   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
210                                                 base::BlockingType::MAY_BLOCK);
211 
212   void* wincx = password_delegate ? password_delegate->wincx() : nullptr;
213   crypto::ScopedSECKEYPrivateKey key(
214       PK11_FindKeyByAnyCert(cert_certificate, wincx));
215   if (!key)
216     return nullptr;
217 
218   int type;
219   size_t max_length;
220   if (!GetClientCertInfo(certificate, &type, &max_length))
221     return nullptr;
222 
223   // Note that key contains a reference to password_delegate->wincx() and may
224   // use it in PK11_Sign. Thus password_delegate must outlive key. We pass it
225   // into SSLPlatformKeyNSS to tie the lifetimes together. See
226   // https://crbug.com/779090.
227   return base::MakeRefCounted<ThreadedSSLPrivateKey>(
228       std::make_unique<SSLPlatformKeyNSS>(type, std::move(password_delegate),
229                                           std::move(key)),
230       GetSSLPlatformKeyTaskRunner());
231 }
232 
233 }  // namespace net
234