forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenssl_platform_key_win.cc
675 lines (592 loc) · 21.1 KB
/
openssl_platform_key_win.cc
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// 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 <windows.h>
#include <NCrypt.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <openssl/bn.h>
#include <openssl/digest.h>
#include <openssl/ec_key.h>
#include <openssl/err.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <openssl/obj_mac.h>
#include <openssl/rsa.h>
#include <openssl/sha.h>
#include "base/debug/debugger.h"
#include "base/debug/stack_trace.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/win/windows_version.h"
#include "crypto/scoped_capi_types.h"
#include "crypto/wincrypt_shim.h"
#include "net/base/net_errors.h"
#include "net/cert/x509_certificate.h"
#include "net/ssl/openssl_ssl_util.h"
namespace net {
namespace {
using NCryptFreeObjectFunc = SECURITY_STATUS(WINAPI*)(NCRYPT_HANDLE);
using NCryptGetPropertyFunc =
SECURITY_STATUS(WINAPI*)(NCRYPT_HANDLE, // hObject
LPCWSTR, // pszProperty
PBYTE, // pbOutput
DWORD, // cbOutput
DWORD*, // pcbResult
DWORD); // dwFlags
using NCryptSignHashFunc =
SECURITY_STATUS(WINAPI*)(NCRYPT_KEY_HANDLE, // hKey
VOID*, // pPaddingInfo
PBYTE, // pbHashValue
DWORD, // cbHashValue
PBYTE, // pbSignature
DWORD, // cbSignature
DWORD*, // pcbResult
DWORD); // dwFlags
class CNGFunctions {
public:
CNGFunctions()
: ncrypt_free_object_(nullptr),
ncrypt_get_property_(nullptr),
ncrypt_sign_hash_(nullptr) {
HMODULE ncrypt = GetModuleHandle(L"ncrypt.dll");
if (ncrypt != nullptr) {
ncrypt_free_object_ = reinterpret_cast<NCryptFreeObjectFunc>(
GetProcAddress(ncrypt, "NCryptFreeObject"));
ncrypt_get_property_ = reinterpret_cast<NCryptGetPropertyFunc>(
GetProcAddress(ncrypt, "NCryptGetProperty"));
ncrypt_sign_hash_ = reinterpret_cast<NCryptSignHashFunc>(
GetProcAddress(ncrypt, "NCryptSignHash"));
}
}
NCryptFreeObjectFunc ncrypt_free_object() const {
return ncrypt_free_object_;
}
NCryptGetPropertyFunc ncrypt_get_property() const {
return ncrypt_get_property_;
}
NCryptSignHashFunc ncrypt_sign_hash() const { return ncrypt_sign_hash_; }
private:
NCryptFreeObjectFunc ncrypt_free_object_;
NCryptGetPropertyFunc ncrypt_get_property_;
NCryptSignHashFunc ncrypt_sign_hash_;
};
base::LazyInstance<CNGFunctions>::Leaky g_cng_functions =
LAZY_INSTANCE_INITIALIZER;
struct CERT_KEY_CONTEXTDeleter {
void operator()(PCERT_KEY_CONTEXT key) {
if (key->dwKeySpec == CERT_NCRYPT_KEY_SPEC) {
g_cng_functions.Get().ncrypt_free_object()(key->hNCryptKey);
} else {
CryptReleaseContext(key->hCryptProv, 0);
}
delete key;
}
};
using ScopedCERT_KEY_CONTEXT =
scoped_ptr<CERT_KEY_CONTEXT, CERT_KEY_CONTEXTDeleter>;
// KeyExData contains the data that is contained in the EX_DATA of the
// RSA and ECDSA objects that are created to wrap Windows system keys.
struct KeyExData {
KeyExData(ScopedCERT_KEY_CONTEXT key, DWORD key_length)
: key(key.Pass()), key_length(key_length) {}
ScopedCERT_KEY_CONTEXT key;
DWORD key_length;
};
// 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*)nullptr, *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;
}
extern const RSA_METHOD win_rsa_method;
extern const ECDSA_METHOD win_ecdsa_method;
// BoringSSLEngine is a BoringSSL ENGINE that implements RSA and ECDSA
// by forwarding the requested operations to CAPI or CNG.
class BoringSSLEngine {
public:
BoringSSLEngine()
: rsa_index_(RSA_get_ex_new_index(0 /* argl */,
nullptr /* argp */,
nullptr /* new_func */,
ExDataDup,
ExDataFree)),
ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */,
nullptr /* argp */,
nullptr /* new_func */,
ExDataDup,
ExDataFree)),
engine_(ENGINE_new()) {
ENGINE_set_RSA_method(engine_, &win_rsa_method, sizeof(win_rsa_method));
ENGINE_set_ECDSA_method(engine_, &win_ecdsa_method,
sizeof(win_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;
// 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->key_length + 7) / 8;
}
int RsaMethodSign(int hash_nid,
const uint8_t* in,
unsigned in_len,
uint8_t* out,
unsigned* out_len,
const RSA* rsa) {
// TODO(davidben): Switch BoringSSL's sign hook to using size_t rather than
// unsigned.
const KeyExData* ex_data = RsaGetExData(rsa);
if (!ex_data) {
NOTREACHED();
OPENSSL_PUT_ERROR(RSA, RSA_sign, ERR_R_INTERNAL_ERROR);
return 0;
}
if (ex_data->key->dwKeySpec == CERT_NCRYPT_KEY_SPEC) {
BCRYPT_PKCS1_PADDING_INFO rsa_padding_info;
switch (hash_nid) {
case NID_md5_sha1:
rsa_padding_info.pszAlgId = nullptr;
break;
case NID_sha1:
rsa_padding_info.pszAlgId = BCRYPT_SHA1_ALGORITHM;
break;
case NID_sha256:
rsa_padding_info.pszAlgId = BCRYPT_SHA256_ALGORITHM;
break;
case NID_sha384:
rsa_padding_info.pszAlgId = BCRYPT_SHA384_ALGORITHM;
break;
case NID_sha512:
rsa_padding_info.pszAlgId = BCRYPT_SHA512_ALGORITHM;
break;
default:
OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
DWORD signature_len;
SECURITY_STATUS ncrypt_status = g_cng_functions.Get().ncrypt_sign_hash()(
ex_data->key->hNCryptKey, &rsa_padding_info, const_cast<PBYTE>(in),
in_len, out, RSA_size(rsa), &signature_len, BCRYPT_PAD_PKCS1);
if (FAILED(ncrypt_status) || signature_len == 0) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
*out_len = signature_len;
return 1;
}
ALG_ID hash_alg;
switch (hash_nid) {
case NID_md5_sha1:
hash_alg = CALG_SSL3_SHAMD5;
break;
case NID_sha1:
hash_alg = CALG_SHA1;
break;
case NID_sha256:
hash_alg = CALG_SHA_256;
break;
case NID_sha384:
hash_alg = CALG_SHA_384;
break;
case NID_sha512:
hash_alg = CALG_SHA_512;
break;
default:
OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
HCRYPTHASH hash;
if (!CryptCreateHash(ex_data->key->hCryptProv, hash_alg, 0, 0, &hash)) {
PLOG(ERROR) << "CreateCreateHash failed";
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
DWORD hash_len;
DWORD arg_len = sizeof(hash_len);
if (!CryptGetHashParam(hash, HP_HASHSIZE, reinterpret_cast<BYTE*>(&hash_len),
&arg_len, 0)) {
PLOG(ERROR) << "CryptGetHashParam HP_HASHSIZE failed";
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
if (hash_len != in_len) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
if (!CryptSetHashParam(hash, HP_HASHVAL, const_cast<BYTE*>(in), 0)) {
PLOG(ERROR) << "CryptSetHashParam HP_HASHVAL failed";
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
DWORD signature_len = RSA_size(rsa);
if (!CryptSignHash(hash, ex_data->key->dwKeySpec, nullptr, 0, out,
&signature_len)) {
PLOG(ERROR) << "CryptSignHash failed";
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
/* CryptoAPI signs in little-endian, so reverse it. */
std::reverse(out, out + signature_len);
*out_len = signature_len;
return 1;
}
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) {
NOTIMPLEMENTED();
OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
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;
}
int RsaMethodSupportsDigest(const RSA* rsa, const EVP_MD* md) {
const KeyExData* ex_data = RsaGetExData(rsa);
if (!ex_data) {
NOTREACHED();
return 0;
}
int hash_nid = EVP_MD_type(md);
if (ex_data->key->dwKeySpec == CERT_NCRYPT_KEY_SPEC) {
// Only hashes which appear in RsaSignPKCS1 are supported.
if (hash_nid != NID_sha1 && hash_nid != NID_sha256 &&
hash_nid != NID_sha384 && hash_nid != NID_sha512) {
return 0;
}
// If the key is a 1024-bit RSA, assume conservatively that it may only be
// able to sign SHA-1 hashes. This is the case for older Estonian ID cards
// that have 1024-bit RSA keys.
//
// CNG does provide NCryptIsAlgSupported and NCryptEnumAlgorithms functions,
// however they seem to both return NTE_NOT_SUPPORTED when querying the
// NCRYPT_PROV_HANDLE at the key's NCRYPT_PROVIDER_HANDLE_PROPERTY.
if (ex_data->key_length <= 1024 && hash_nid != NID_sha1)
return 0;
return 1;
} else {
// If the key is in CAPI, assume conservatively that the CAPI service
// provider may only be able to sign SHA-1 hashes.
return hash_nid == NID_sha1;
}
}
const RSA_METHOD win_rsa_method = {
{
0, // references
1, // is_static
},
nullptr, // app_data
nullptr, // init
nullptr, // finish
RsaMethodSize,
RsaMethodSign,
nullptr, // verify
RsaMethodEncrypt,
RsaMethodSignRaw,
RsaMethodDecrypt,
RsaMethodVerifyRaw,
nullptr, // private_transform
nullptr, // mod_exp
nullptr, // bn_mod_exp
RSA_FLAG_OPAQUE,
nullptr, // keygen
RsaMethodSupportsDigest,
};
// 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);
// Windows doesn't distinguish the sizes of the curve's degree (which
// determines the size of a point on the curve) and the base point's order
// (which determines the size of a scalar). For P-256, P-384, and P-521, these
// two sizes are the same.
//
// See
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375520(v=vs.85).aspx
// which uses the same length for both.
return (ex_data->key_length + 7) / 8;
}
int EcdsaMethodSign(const uint8_t* digest,
size_t digest_len,
uint8_t* out_sig,
unsigned int* out_sig_len,
EC_KEY* ec_key) {
const KeyExData* ex_data = EcKeyGetExData(ec_key);
// Only CNG supports ECDSA.
if (!ex_data || ex_data->key->dwKeySpec != CERT_NCRYPT_KEY_SPEC) {
NOTREACHED();
OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
return 0;
}
size_t degree = (ex_data->key_length + 7) / 8;
if (degree == 0) {
NOTREACHED();
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
std::vector<uint8_t> raw_sig(degree * 2);
DWORD signature_len;
SECURITY_STATUS ncrypt_status = g_cng_functions.Get().ncrypt_sign_hash()(
ex_data->key->hNCryptKey, nullptr, const_cast<PBYTE>(digest), digest_len,
&raw_sig[0], raw_sig.size(), &signature_len, 0);
if (FAILED(ncrypt_status) || signature_len != raw_sig.size()) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
// Convert the RAW ECDSA signature to a DER-encoded ECDSA-Sig-Value.
crypto::ScopedECDSA_SIG sig(ECDSA_SIG_new());
if (!sig) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
sig->r = BN_bin2bn(&raw_sig[0], degree, nullptr);
sig->s = BN_bin2bn(&raw_sig[degree], degree, nullptr);
if (!sig->r || !sig->s) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
// Ensure the DER-encoded signature fits in the bounds.
int len = i2d_ECDSA_SIG(sig.get(), nullptr);
if (len < 0 || static_cast<size_t>(len) > ECDSA_size(ec_key)) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
len = i2d_ECDSA_SIG(sig.get(), &out_sig);
if (len < 0) {
OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED);
return 0;
}
*out_sig_len = 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 win_ecdsa_method = {
{
0, // references
1, // is_static
},
nullptr, // app_data
nullptr, // init
nullptr, // finish
EcdsaMethodGroupOrderSize,
EcdsaMethodSign,
EcdsaMethodVerify,
ECDSA_FLAG_OPAQUE,
};
// Determines the key type and length of |key|. The type is returned as an
// OpenSSL EVP_PKEY type. The key length for RSA key is the size of the RSA
// modulus in bits. For an ECDSA key, it is the number of bits to represent the
// group order. It returns true on success and false on failure.
bool GetKeyInfo(PCERT_KEY_CONTEXT key, int* out_type, DWORD* out_length) {
if (key->dwKeySpec == CERT_NCRYPT_KEY_SPEC) {
DWORD prop_len;
SECURITY_STATUS status = g_cng_functions.Get().ncrypt_get_property()(
key->hNCryptKey, NCRYPT_ALGORITHM_GROUP_PROPERTY, nullptr, 0, &prop_len,
0);
if (FAILED(status) || prop_len == 0 || prop_len % 2 != 0) {
LOG(ERROR) << "Could not query CNG key type: " << status;
return false;
}
std::vector<BYTE> prop_buf(prop_len);
status = g_cng_functions.Get().ncrypt_get_property()(
key->hNCryptKey, NCRYPT_ALGORITHM_GROUP_PROPERTY, &prop_buf[0],
prop_buf.size(), &prop_len, 0);
if (FAILED(status) || prop_len == 0 || prop_len % 2 != 0) {
LOG(ERROR) << "Could not query CNG key type: " << status;
return false;
}
int type;
const wchar_t* alg = reinterpret_cast<const wchar_t*>(&prop_buf[0]);
if (wcsncmp(NCRYPT_RSA_ALGORITHM_GROUP, alg, prop_len / 2) == 0) {
type = EVP_PKEY_RSA;
} else if (wcsncmp(NCRYPT_ECDSA_ALGORITHM_GROUP, alg, prop_len / 2) == 0 ||
wcsncmp(NCRYPT_ECDH_ALGORITHM_GROUP, alg, prop_len / 2) == 0) {
// Importing an ECDSA key via PKCS #12 seems to label it as ECDH rather
// than ECDSA, so also allow ECDH.
type = EVP_PKEY_EC;
} else {
LOG(ERROR) << "Unknown CNG key type: "
<< std::wstring(alg, wcsnlen(alg, prop_len / 2));
return false;
}
DWORD length;
prop_len;
status = g_cng_functions.Get().ncrypt_get_property()(
key->hNCryptKey, NCRYPT_LENGTH_PROPERTY,
reinterpret_cast<BYTE*>(&length), sizeof(DWORD), &prop_len, 0);
if (FAILED(status)) {
LOG(ERROR) << "Could not get CNG key length " << status;
return false;
}
DCHECK_EQ(sizeof(DWORD), prop_len);
*out_type = type;
*out_length = length;
return true;
}
crypto::ScopedHCRYPTKEY hcryptkey;
if (!CryptGetUserKey(key->hCryptProv, key->dwKeySpec, hcryptkey.receive())) {
PLOG(ERROR) << "Could not get CAPI key handle";
return false;
}
ALG_ID alg_id;
DWORD prop_len = sizeof(alg_id);
if (!CryptGetKeyParam(hcryptkey.get(), KP_ALGID,
reinterpret_cast<BYTE*>(&alg_id), &prop_len, 0)) {
PLOG(ERROR) << "Could not query CAPI key type";
return false;
}
if (alg_id != CALG_RSA_SIGN && alg_id != CALG_RSA_KEYX) {
LOG(ERROR) << "Unknown CAPI key type: " << alg_id;
return false;
}
DWORD length;
prop_len = sizeof(DWORD);
if (!CryptGetKeyParam(hcryptkey.get(), KP_KEYLEN,
reinterpret_cast<BYTE*>(&length), &prop_len, 0)) {
PLOG(ERROR) << "Could not get CAPI key length";
return false;
}
DCHECK_EQ(sizeof(DWORD), prop_len);
*out_type = EVP_PKEY_RSA;
*out_length = length;
return true;
}
crypto::ScopedEVP_PKEY CreateRSAWrapper(ScopedCERT_KEY_CONTEXT key,
DWORD key_length) {
crypto::ScopedRSA rsa(RSA_new_method(global_boringssl_engine.Get().engine()));
if (!rsa)
return nullptr;
RSA_set_ex_data(rsa.get(), global_boringssl_engine.Get().rsa_ex_index(),
new KeyExData(key.Pass(), key_length));
crypto::ScopedEVP_PKEY pkey(EVP_PKEY_new());
if (!pkey || !EVP_PKEY_set1_RSA(pkey.get(), rsa.get()))
return nullptr;
return pkey.Pass();
}
crypto::ScopedEVP_PKEY CreateECDSAWrapper(ScopedCERT_KEY_CONTEXT key,
DWORD key_length) {
crypto::ScopedEC_KEY ec_key(
EC_KEY_new_method(global_boringssl_engine.Get().engine()));
if (!ec_key)
return nullptr;
EC_KEY_set_ex_data(ec_key.get(),
global_boringssl_engine.Get().ec_key_ex_index(),
new KeyExData(key.Pass(), key_length));
crypto::ScopedEVP_PKEY pkey(EVP_PKEY_new());
if (!pkey || !EVP_PKEY_set1_EC_KEY(pkey.get(), ec_key.get()))
return nullptr;
return pkey.Pass();
}
} // namespace
crypto::ScopedEVP_PKEY FetchClientCertPrivateKey(
const X509Certificate* certificate) {
PCCERT_CONTEXT cert_context = certificate->os_cert_handle();
HCRYPTPROV_OR_NCRYPT_KEY_HANDLE crypt_prov = 0;
DWORD key_spec = 0;
BOOL must_free = FALSE;
DWORD flags = 0;
if (base::win::GetVersion() >= base::win::VERSION_VISTA)
flags |= CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG;
if (!CryptAcquireCertificatePrivateKey(cert_context, flags, nullptr,
&crypt_prov, &key_spec, &must_free)) {
PLOG(WARNING) << "Could not acquire private key";
return nullptr;
}
// Should never get a cached handle back - ownership must always be
// transferred.
CHECK_EQ(must_free, TRUE);
ScopedCERT_KEY_CONTEXT key(new CERT_KEY_CONTEXT);
key->dwKeySpec = key_spec;
key->hCryptProv = crypt_prov;
int key_type;
DWORD key_length;
if (!GetKeyInfo(key.get(), &key_type, &key_length))
return nullptr;
switch (key_type) {
case EVP_PKEY_RSA:
return CreateRSAWrapper(key.Pass(), key_length);
case EVP_PKEY_EC:
return CreateECDSAWrapper(key.Pass(), key_length);
default:
return nullptr;
}
}
} // namespace net