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

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

Powered by Google App Engine
This is Rietveld 408576698