1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
// Copyright 2014 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/openssl_platform_key.h"
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <Security/cssm.h>
#include <Security/SecBase.h>
#include <Security/SecCertificate.h>
#include <Security/SecIdentity.h>
#include <Security/SecKey.h>
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/mac/mac_logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_policy.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "crypto/mac_security_services_lock.h"
#include "net/base/net_errors.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/openssl_ssl_util.h"
namespace net {
namespace {
class ScopedCSSM_CC_HANDLE {
public:
ScopedCSSM_CC_HANDLE() : handle_(0) {
}
~ScopedCSSM_CC_HANDLE() {
reset();
}
CSSM_CC_HANDLE get() const {
return handle_;
}
void reset() {
if (handle_)
CSSM_DeleteContext(handle_);
handle_ = 0;
}
CSSM_CC_HANDLE* InitializeInto() {
reset();
return &handle_;
}
private:
CSSM_CC_HANDLE handle_;
DISALLOW_COPY_AND_ASSIGN(ScopedCSSM_CC_HANDLE);
};
// Looks up the private key for |certificate| in KeyChain and returns
// a SecKeyRef or NULL on failure. The caller takes ownership of the
// result.
SecKeyRef FetchSecKeyRefForCertificate(const X509Certificate* certificate) {
OSStatus status;
base::ScopedCFTypeRef<SecIdentityRef> identity;
{
base::AutoLock lock(crypto::GetMacSecurityServicesLock());
status = SecIdentityCreateWithCertificate(
NULL, certificate->os_cert_handle(), identity.InitializeInto());
}
if (status != noErr) {
OSSTATUS_LOG(WARNING, status);
return NULL;
}
base::ScopedCFTypeRef<SecKeyRef> private_key;
status = SecIdentityCopyPrivateKey(identity, private_key.InitializeInto());
if (status != noErr) {
OSSTATUS_LOG(WARNING, status);
return NULL;
}
return private_key.release();
}
extern const RSA_METHOD mac_rsa_method;
extern const ECDSA_METHOD mac_ecdsa_method;
// KeyExData contains the data that is contained in the EX_DATA of the
// RSA and ECDSA objects that are created to wrap Mac system keys.
struct KeyExData {
KeyExData(SecKeyRef key, const CSSM_KEY* cssm_key)
: key(key, base::scoped_policy::RETAIN), cssm_key(cssm_key) {}
base::ScopedCFTypeRef<SecKeyRef> key;
const CSSM_KEY* cssm_key;
};
// ExDataDup is called when one of the RSA or EC_KEY objects is
// duplicated. This is not supported and should never happen.
int ExDataDup(CRYPTO_EX_DATA* to,
const CRYPTO_EX_DATA* from,
void** from_d,
int idx,
long argl,
void* argp) {
CHECK_EQ((void*)NULL, *from_d);
return 0;
}
// ExDataFree is called when one of the RSA or EC_KEY objects is freed.
void ExDataFree(void* parent,
void* ptr,
CRYPTO_EX_DATA* ex_data,
int idx,
long argl, void* argp) {
KeyExData* data = reinterpret_cast<KeyExData*>(ptr);
delete data;
}
// BoringSSLEngine is a BoringSSL ENGINE that implements RSA and ECDSA
// by forwarding the requested operations to Apple's CSSM
// implementation.
class BoringSSLEngine {
public:
BoringSSLEngine()
: rsa_index_(RSA_get_ex_new_index(0 /* argl */,
NULL /* argp */,
NULL /* new_func */,
ExDataDup,
ExDataFree)),
ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */,
NULL /* argp */,
NULL /* new_func */,
ExDataDup,
ExDataFree)),
engine_(ENGINE_new()) {
ENGINE_set_RSA_method(
engine_, &mac_rsa_method, sizeof(mac_rsa_method));
ENGINE_set_ECDSA_method(
engine_, &mac_ecdsa_method, sizeof(mac_ecdsa_method));
}
int rsa_ex_index() const { return rsa_index_; }
int ec_key_ex_index() const { return ec_key_index_; }
const ENGINE* engine() const { return engine_; }
private:
const int rsa_index_;
const int ec_key_index_;
ENGINE* const engine_;
};
base::LazyInstance<BoringSSLEngine>::Leaky global_boringssl_engine =
LAZY_INSTANCE_INITIALIZER;
// Helper function for making a signature.
// MakeCSSMSignature uses the key information in |ex_data| to sign the
// |in_len| bytes pointed by |in|. It writes up to |max_out| bytes
// into the buffer pointed to by |out|, setting |*out_len| to the
// number of bytes written. It returns 1 on success and 0 on failure.
int MakeCSSMSignature(const KeyExData* ex_data,
size_t* out_len,
uint8_t* out,
size_t max_out,
const uint8_t* in,
size_t in_len) {
CSSM_CSP_HANDLE csp_handle;
OSStatus status = SecKeyGetCSPHandle(ex_data->key.get(), &csp_handle);
if (status != noErr) {
OSSTATUS_LOG(WARNING, status);
OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
return 0;
}
const CSSM_ACCESS_CREDENTIALS* cssm_creds = NULL;
status = SecKeyGetCredentials(ex_data->key.get(), CSSM_ACL_AUTHORIZATION_SIGN,
kSecCredentialTypeDefault, &cssm_creds);
if (status != noErr) {
OSSTATUS_LOG(WARNING, status);
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
ScopedCSSM_CC_HANDLE cssm_signature;
if (CSSM_CSP_CreateSignatureContext(
csp_handle, ex_data->cssm_key->KeyHeader.AlgorithmId, cssm_creds,
ex_data->cssm_key, cssm_signature.InitializeInto()) != CSSM_OK) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
if (ex_data->cssm_key->KeyHeader.AlgorithmId == CSSM_ALGID_RSA) {
// Set RSA blinding.
CSSM_CONTEXT_ATTRIBUTE blinding_attr;
blinding_attr.AttributeType = CSSM_ATTRIBUTE_RSA_BLINDING;
blinding_attr.AttributeLength = sizeof(uint32);
blinding_attr.Attribute.Uint32 = 1;
if (CSSM_UpdateContextAttributes(
cssm_signature.get(), 1, &blinding_attr) != CSSM_OK) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
}
CSSM_DATA hash_data;
hash_data.Length = in_len;
hash_data.Data = const_cast<uint8*>(in);
CSSM_DATA signature_data;
signature_data.Length = max_out;
signature_data.Data = out;
if (CSSM_SignData(cssm_signature.get(), &hash_data, 1,
CSSM_ALGID_NONE, &signature_data) != CSSM_OK) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
*out_len = signature_data.Length;
return 1;
}
// Custom RSA_METHOD that uses the platform APIs for signing.
const KeyExData* RsaGetExData(const RSA* rsa) {
return reinterpret_cast<const KeyExData*>(
RSA_get_ex_data(rsa, global_boringssl_engine.Get().rsa_ex_index()));
}
size_t RsaMethodSize(const RSA *rsa) {
const KeyExData *ex_data = RsaGetExData(rsa);
return (ex_data->cssm_key->KeyHeader.LogicalKeySizeInBits + 7) / 8;
}
int RsaMethodEncrypt(RSA* rsa,
size_t* out_len,
uint8_t* out,
size_t max_out,
const uint8_t* in,
size_t in_len,
int padding) {
NOTIMPLEMENTED();
OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
int RsaMethodSignRaw(RSA* rsa,
size_t* out_len,
uint8_t* out,
size_t max_out,
const uint8_t* in,
size_t in_len,
int padding) {
// Only support PKCS#1 padding.
DCHECK_EQ(RSA_PKCS1_PADDING, padding);
if (padding != RSA_PKCS1_PADDING) {
OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE);
return 0;
}
const KeyExData *ex_data = RsaGetExData(rsa);
if (!ex_data) {
OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
return 0;
}
DCHECK_EQ(CSSM_ALGID_RSA, ex_data->cssm_key->KeyHeader.AlgorithmId);
return MakeCSSMSignature(ex_data, out_len, out, max_out, in, in_len);
}
int RsaMethodDecrypt(RSA* rsa,
size_t* out_len,
uint8_t* out,
size_t max_out,
const uint8_t* in,
size_t in_len,
int padding) {
NOTIMPLEMENTED();
OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
int RsaMethodVerifyRaw(RSA* rsa,
size_t* out_len,
uint8_t* out,
size_t max_out,
const uint8_t* in,
size_t in_len,
int padding) {
NOTIMPLEMENTED();
OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
const RSA_METHOD mac_rsa_method = {
{
0 /* references */,
1 /* is_static */
} /* common */,
NULL /* app_data */,
NULL /* init */,
NULL /* finish */,
RsaMethodSize,
NULL /* sign */,
NULL /* verify */,
RsaMethodEncrypt,
RsaMethodSignRaw,
RsaMethodDecrypt,
RsaMethodVerifyRaw,
NULL /* private_transform */,
NULL /* mod_exp */,
NULL /* bn_mod_exp */,
RSA_FLAG_OPAQUE,
NULL /* keygen */,
};
crypto::ScopedEVP_PKEY CreateRSAWrapper(SecKeyRef key,
const CSSM_KEY* cssm_key) {
crypto::ScopedRSA rsa(
RSA_new_method(global_boringssl_engine.Get().engine()));
if (!rsa)
return crypto::ScopedEVP_PKEY();
RSA_set_ex_data(
rsa.get(), global_boringssl_engine.Get().rsa_ex_index(),
new KeyExData(key, cssm_key));
crypto::ScopedEVP_PKEY pkey(EVP_PKEY_new());
if (!pkey)
return crypto::ScopedEVP_PKEY();
if (!EVP_PKEY_set1_RSA(pkey.get(), rsa.get()))
return crypto::ScopedEVP_PKEY();
return pkey.Pass();
}
// Custom ECDSA_METHOD that uses the platform APIs.
// Note that for now, only signing through ECDSA_sign() is really supported.
// all other method pointers are either stubs returning errors, or no-ops.
const KeyExData* EcKeyGetExData(const EC_KEY* ec_key) {
return reinterpret_cast<const KeyExData*>(EC_KEY_get_ex_data(
ec_key, global_boringssl_engine.Get().ec_key_ex_index()));
}
size_t EcdsaMethodGroupOrderSize(const EC_KEY* ec_key) {
const KeyExData* ex_data = EcKeyGetExData(ec_key);
// LogicalKeySizeInBits is the size of an EC public key. But an
// ECDSA signature length depends on the size of the base point's
// order. For P-256, P-384, and P-521, these two sizes are the same.
return (ex_data->cssm_key->KeyHeader.LogicalKeySizeInBits + 7) / 8;
}
int EcdsaMethodSign(const uint8_t* digest,
size_t digest_len,
uint8_t* sig,
unsigned int* sig_len,
EC_KEY* ec_key) {
const KeyExData *ex_data = EcKeyGetExData(ec_key);
if (!ex_data) {
OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
return 0;
}
DCHECK_EQ(CSSM_ALGID_ECDSA, ex_data->cssm_key->KeyHeader.AlgorithmId);
// TODO(davidben): Fix BoringSSL to make sig_len a size_t*.
size_t out_len;
int ret = MakeCSSMSignature(
ex_data, &out_len, sig, ECDSA_size(ec_key), digest, digest_len);
if (!ret)
return 0;
*sig_len = out_len;
return 1;
}
int EcdsaMethodVerify(const uint8_t* digest,
size_t digest_len,
const uint8_t* sig,
size_t sig_len,
EC_KEY* eckey) {
NOTIMPLEMENTED();
OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_NOT_IMPLEMENTED);
return 0;
}
const ECDSA_METHOD mac_ecdsa_method = {
{
0 /* references */,
1 /* is_static */
} /* common */,
NULL /* app_data */,
NULL /* init */,
NULL /* finish */,
EcdsaMethodGroupOrderSize,
EcdsaMethodSign,
EcdsaMethodVerify,
ECDSA_FLAG_OPAQUE,
};
crypto::ScopedEVP_PKEY CreateECDSAWrapper(SecKeyRef key,
const CSSM_KEY* cssm_key) {
crypto::ScopedEC_KEY ec_key(
EC_KEY_new_method(global_boringssl_engine.Get().engine()));
if (!ec_key)
return crypto::ScopedEVP_PKEY();
EC_KEY_set_ex_data(
ec_key.get(), global_boringssl_engine.Get().ec_key_ex_index(),
new KeyExData(key, cssm_key));
crypto::ScopedEVP_PKEY pkey(EVP_PKEY_new());
if (!pkey)
return crypto::ScopedEVP_PKEY();
if (!EVP_PKEY_set1_EC_KEY(pkey.get(), ec_key.get()))
return crypto::ScopedEVP_PKEY();
return pkey.Pass();
}
crypto::ScopedEVP_PKEY CreatePkeyWrapper(SecKeyRef key) {
const CSSM_KEY* cssm_key;
OSStatus status = SecKeyGetCSSMKey(key, &cssm_key);
if (status != noErr)
return crypto::ScopedEVP_PKEY();
switch (cssm_key->KeyHeader.AlgorithmId) {
case CSSM_ALGID_RSA:
return CreateRSAWrapper(key, cssm_key);
case CSSM_ALGID_ECDSA:
return CreateECDSAWrapper(key, cssm_key);
default:
// TODO(davidben): Filter out anything other than ECDSA and RSA
// elsewhere. We don't support other key types.
NOTREACHED();
LOG(ERROR) << "Unknown key type";
return crypto::ScopedEVP_PKEY();
}
}
} // namespace
crypto::ScopedEVP_PKEY FetchClientCertPrivateKey(
const X509Certificate* certificate) {
// Look up the private key.
base::ScopedCFTypeRef<SecKeyRef> private_key(
FetchSecKeyRefForCertificate(certificate));
if (!private_key)
return crypto::ScopedEVP_PKEY();
// Create an EVP_PKEY wrapper.
return CreatePkeyWrapper(private_key.get());
}
} // namespace net
|