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

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

Issue 1178193002: Sign CertificateVerify messages on a background thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 5 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
OLDNEW
(Empty)
1 // Copyright 2014 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/ssl_platform_key.h"
6
7 #include <openssl/ecdsa.h>
8 #include <openssl/obj.h>
9 #include <openssl/rsa.h>
10
11 #include <Security/cssm.h>
12 #include <Security/SecBase.h>
13 #include <Security/SecCertificate.h>
14 #include <Security/SecIdentity.h>
15 #include <Security/SecKey.h>
16
17 #include "base/lazy_instance.h"
18 #include "base/location.h"
19 #include "base/logging.h"
20 #include "base/mac/mac_logging.h"
21 #include "base/mac/scoped_cftyperef.h"
22 #include "base/memory/scoped_policy.h"
23 #include "base/memory/scoped_ptr.h"
24 #include "base/sequenced_task_runner.h"
25 #include "base/stl_util.h"
26 #include "base/synchronization/lock.h"
27 #include "crypto/mac_security_services_lock.h"
28 #include "crypto/openssl_util.h"
29 #include "crypto/scoped_openssl_types.h"
30 #include "net/base/net_errors.h"
31 #include "net/cert/x509_certificate.h"
32 #include "net/ssl/ssl_private_key.h"
33 #include "net/ssl/threaded_ssl_private_key.h"
34
35 namespace net {
36
37 namespace {
38
39 class ScopedCSSM_CC_HANDLE {
40 public:
41 ScopedCSSM_CC_HANDLE() : handle_(0) {}
42
43 ~ScopedCSSM_CC_HANDLE() { reset(); }
44
45 CSSM_CC_HANDLE get() const { return handle_; }
46
47 void reset() {
48 if (handle_)
49 CSSM_DeleteContext(handle_);
50 handle_ = 0;
51 }
52
53 private:
54 CSSM_CC_HANDLE handle_;
55
56 DISALLOW_COPY_AND_ASSIGN(ScopedCSSM_CC_HANDLE);
57 };
58
59 // Looks up the private key for |certificate| in KeyChain and returns
60 // a SecKeyRef or nullptr on failure. The caller takes ownership of the
61 // result.
62 SecKeyRef FetchSecKeyRefForCertificate(const X509Certificate* certificate) {
63 OSStatus status;
64 base::ScopedCFTypeRef<SecIdentityRef> identity;
65 {
66 base::AutoLock lock(crypto::GetMacSecurityServicesLock());
67 status = SecIdentityCreateWithCertificate(
68 nullptr, certificate->os_cert_handle(), identity.InitializeInto());
69 }
70 if (status != noErr) {
71 OSSTATUS_LOG(WARNING, status);
72 return nullptr;
73 }
74
75 base::ScopedCFTypeRef<SecKeyRef> private_key;
76 status = SecIdentityCopyPrivateKey(identity, private_key.InitializeInto());
77 if (status != noErr) {
78 OSSTATUS_LOG(WARNING, status);
79 return nullptr;
80 }
81
82 return private_key.release();
83 }
84
85 class SSLPlatformKeyMac : public ThreadedSSLPrivateKey::Delegate {
86 public:
87 SSLPlatformKeyMac(SecKeyRef key, const CSSM_KEY* cssm_key)
88 : key_(key, base::scoped_policy::RETAIN), cssm_key_(cssm_key) {
89 DCHECK(cssm_key_->KeyHeader.AlgorithmId == CSSM_ALGID_RSA ||
90 cssm_key_->KeyHeader.AlgorithmId == CSSM_ALGID_ECDSA);
91 }
92
93 ~SSLPlatformKeyMac() override {}
94
95 SSLPrivateKey::Type GetType() override {
96 if (cssm_key_->KeyHeader.AlgorithmId == CSSM_ALGID_RSA) {
97 return SSLPrivateKey::Type::RSA;
98 } else {
99 DCHECK_EQ(CSSM_ALGID_ECDSA, cssm_key_->KeyHeader.AlgorithmId);
100 return SSLPrivateKey::Type::ECDSA;
101 }
102 }
103
104 bool SupportsHash(SSLPrivateKey::Hash hash) override { return true; }
105
106 size_t GetMaxSignatureLengthInBytes() override {
107 if (cssm_key_->KeyHeader.AlgorithmId == CSSM_ALGID_RSA) {
108 return (cssm_key_->KeyHeader.LogicalKeySizeInBits + 7) / 8;
109 } else {
110 // LogicalKeySizeInBits is the size of an EC public key. But an
111 // ECDSA signature length depends on the size of the base point's
112 // order. For P-256, P-384, and P-521, these two sizes are the same.
113 return ECDSA_SIG_max_len((cssm_key_->KeyHeader.LogicalKeySizeInBits + 7) /
114 8);
115 }
116 }
117
118 Error SignDigest(SSLPrivateKey::Hash hash,
119 const base::StringPiece& input,
120 std::vector<uint8_t>* signature) override {
121 crypto::OpenSSLErrStackTracer tracer(FROM_HERE);
122
123 CSSM_CSP_HANDLE csp_handle;
124 OSStatus status = SecKeyGetCSPHandle(key_.get(), &csp_handle);
125 if (status != noErr) {
126 OSSTATUS_LOG(WARNING, status);
127 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
128 }
129
130 const CSSM_ACCESS_CREDENTIALS* cssm_creds = nullptr;
131 status = SecKeyGetCredentials(key_.get(), CSSM_ACL_AUTHORIZATION_SIGN,
132 kSecCredentialTypeDefault, &cssm_creds);
133 if (status != noErr) {
134 OSSTATUS_LOG(WARNING, status);
135 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
136 }
137
138 CSSM_CC_HANDLE* cssm_signature_raw = nullptr;
139 if (CSSM_CSP_CreateSignatureContext(
140 csp_handle, cssm_key_->KeyHeader.AlgorithmId, cssm_creds, cssm_key_,
141 &cssm_signature_raw) != CSSM_OK) {
142 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
143 }
144 ScopedCSSM_CC_HANDLE cssm_signature(cssm_signature_raw);
145
146 CSSM_DATA hash_data;
147 hash_data.Length = input.size();
148 hash_data.Data =
149 const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(input.data()));
150
151 crypto::ScopedOpenSSLBytes free_digest_info;
152 if (cssm_key_->KeyHeader.AlgorithmId == CSSM_ALGID_RSA) {
153 // CSSM expects the caller to prepend the DigestInfo.
154 int hash_nid = NID_undef;
155 switch (hash) {
156 case SSLPrivateKey::Hash::MD5_SHA1:
157 hash_nid = NID_md5_sha1;
158 break;
159 case SSLPrivateKey::Hash::SHA1:
160 hash_nid = NID_sha1;
161 break;
162 case SSLPrivateKey::Hash::SHA256:
163 hash_nid = NID_sha256;
164 break;
165 case SSLPrivateKey::Hash::SHA384:
166 hash_nid = NID_sha384;
167 break;
168 case SSLPrivateKey::Hash::SHA512:
169 hash_nid = NID_sha512;
170 break;
171 }
172 DCHECK_NE(NID_undef, hash_nid);
173 int is_alloced;
174 if (!RSA_add_pkcs1_prefix(&hash_data.Data, &hash_data.Length, &is_alloced,
175 hash_nid, hash_data.Data, hash_data.Length)) {
176 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
177 }
178 if (is_alloced)
179 free_digest_info.reset(hash_data.Data);
180
181 // Set RSA blinding.
182 CSSM_CONTEXT_ATTRIBUTE blinding_attr;
183 blinding_attr.AttributeType = CSSM_ATTRIBUTE_RSA_BLINDING;
184 blinding_attr.AttributeLength = sizeof(uint32_t);
185 blinding_attr.Attribute.Uint32 = 1;
186 if (CSSM_UpdateContextAttributes(cssm_signature.get(), 1,
187 &blinding_attr) != CSSM_OK) {
188 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
189 }
190 }
191
192 signature->resize(GetMaxSignatureLengthInBytes());
193 CSSM_DATA signature_data;
194 signature_data.Length = signature->size();
195 signature_data.Data = vector_as_array(signature);
196
197 if (CSSM_SignData(cssm_signature.get(), &hash_data, 1, CSSM_ALGID_NONE,
198 &signature_data) != CSSM_OK) {
199 return ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED;
200 }
201 signature->resize(signature_data.Length);
202 return OK;
203 }
204
205 private:
206 base::ScopedCFTypeRef<SecKeyRef> key_;
207 const CSSM_KEY* cssm_key_;
208
209 DISALLOW_COPY_AND_ASSIGN(SSLPlatformKeyMac);
210 };
211
212 } // namespace
213
214 scoped_ptr<SSLPrivateKey> FetchClientCertPrivateKey(
215 X509Certificate* certificate,
216 scoped_refptr<base::SequencedTaskRunner> task_runner) {
217 // Look up the private key.
218 base::ScopedCFTypeRef<SecKeyRef> private_key(
219 FetchSecKeyRefForCertificate(certificate));
220 if (!private_key)
221 return nullptr;
222
223 const CSSM_KEY* cssm_key;
224 OSStatus status = SecKeyGetCSSMKey(private_key.get(), &cssm_key);
225 if (status != noErr)
226 return nullptr;
227
228 if (cssm_key->KeyHeader.AlgorithmId != CSSM_ALGID_RSA &&
229 cssm_key->KeyHeader.AlgorithmId != CSSM_ALGID_ECDSA) {
230 LOG(ERROR) << "Unknown key type: " << cssm_key->KeyHeader.AlgorithmId;
231 return nullptr;
232 }
233 return make_scoped_ptr(new ThreadedSSLPrivateKey(
234 make_scoped_ptr(new SSLPlatformKeyMac(private_key.get(), cssm_key)),
235 task_runner.Pass()));
236 }
237
238 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698