-
Notifications
You must be signed in to change notification settings - Fork 788
/
Copy pathssl_credential.cc
609 lines (515 loc) · 18 KB
/
ssl_credential.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
// Copyright 2024 The BoringSSL Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <openssl/ssl.h>
#include <assert.h>
#include <openssl/span.h>
#include "../crypto/internal.h"
#include "../crypto/spake2plus/internal.h"
#include "internal.h"
BSSL_NAMESPACE_BEGIN
// new_leafless_chain returns a fresh stack of buffers set to {nullptr}.
static UniquePtr<STACK_OF(CRYPTO_BUFFER)> new_leafless_chain(void) {
UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null());
if (!chain || !sk_CRYPTO_BUFFER_push(chain.get(), nullptr)) {
return nullptr;
}
return chain;
}
bool ssl_get_full_credential_list(SSL_HANDSHAKE *hs,
Array<SSL_CREDENTIAL *> *out) {
CERT *cert = hs->config->cert.get();
// Finish filling in the legacy credential if needed.
if (!cert->x509_method->ssl_auto_chain_if_needed(hs)) {
return false;
}
size_t num_creds = cert->credentials.size();
bool include_legacy = cert->legacy_credential->IsComplete();
if (include_legacy) {
num_creds++;
}
if (!out->InitForOverwrite(num_creds)) {
return false;
}
for (size_t i = 0; i < cert->credentials.size(); i++) {
(*out)[i] = cert->credentials[i].get();
}
if (include_legacy) {
(*out)[num_creds - 1] = cert->legacy_credential.get();
}
return true;
}
bool ssl_credential_matches_requested_issuers(SSL_HANDSHAKE *hs,
const SSL_CREDENTIAL *cred) {
if (!cred->must_match_issuer) {
// This credential does not need to match a requested issuer, so
// it is good to use without a match.
return true;
}
// If we have names sent by the CA extension, and this
// credential matches it, it is good.
if (hs->ca_names != nullptr) {
for (const CRYPTO_BUFFER *ca_name : hs->ca_names.get()) {
if (cred->ChainContainsIssuer(
Span(CRYPTO_BUFFER_data(ca_name), CRYPTO_BUFFER_len(ca_name)))) {
return true;
}
}
}
// TODO(bbe): Other forms of issuer matching go here.
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_MATCHING_ISSUER);
return false;
}
BSSL_NAMESPACE_END
using namespace bssl;
static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
ssl_credential_st::ssl_credential_st(SSLCredentialType type_arg)
: RefCounted(CheckSubClass()), type(type_arg) {
CRYPTO_new_ex_data(&ex_data);
}
ssl_credential_st::~ssl_credential_st() {
CRYPTO_free_ex_data(&g_ex_data_class, this, &ex_data);
}
static CRYPTO_BUFFER *buffer_up_ref(const CRYPTO_BUFFER *buffer) {
CRYPTO_BUFFER_up_ref(const_cast<CRYPTO_BUFFER *>(buffer));
return const_cast<CRYPTO_BUFFER *>(buffer);
}
UniquePtr<SSL_CREDENTIAL> ssl_credential_st::Dup() const {
assert(type == SSLCredentialType::kX509);
UniquePtr<SSL_CREDENTIAL> ret = MakeUnique<SSL_CREDENTIAL>(type);
if (ret == nullptr) {
return nullptr;
}
ret->pubkey = UpRef(pubkey);
ret->privkey = UpRef(privkey);
ret->key_method = key_method;
if (!ret->sigalgs.CopyFrom(sigalgs)) {
return nullptr;
}
if (chain) {
ret->chain.reset(sk_CRYPTO_BUFFER_deep_copy(chain.get(), buffer_up_ref,
CRYPTO_BUFFER_free));
if (!ret->chain) {
return nullptr;
}
}
ret->dc = UpRef(dc);
ret->signed_cert_timestamp_list = UpRef(signed_cert_timestamp_list);
ret->ocsp_response = UpRef(ocsp_response);
ret->dc_algorithm = dc_algorithm;
return ret;
}
void ssl_credential_st::ClearCertAndKey() {
pubkey = nullptr;
privkey = nullptr;
key_method = nullptr;
chain = nullptr;
}
bool ssl_credential_st::UsesX509() const {
switch (type) {
case SSLCredentialType::kX509:
case SSLCredentialType::kDelegated:
return true;
case SSLCredentialType::kSPAKE2PlusV1Client:
case SSLCredentialType::kSPAKE2PlusV1Server:
return false;
}
abort();
}
bool ssl_credential_st::UsesPrivateKey() const {
switch (type) {
case SSLCredentialType::kX509:
case SSLCredentialType::kDelegated:
return true;
case SSLCredentialType::kSPAKE2PlusV1Client:
case SSLCredentialType::kSPAKE2PlusV1Server:
return false;
}
abort();
}
bool ssl_credential_st::IsComplete() const {
// APIs like |SSL_use_certificate| and |SSL_set1_chain| configure the leaf and
// other certificates separately. It is possible for |chain| have a null leaf.
if (UsesX509() && (sk_CRYPTO_BUFFER_num(chain.get()) == 0 ||
sk_CRYPTO_BUFFER_value(chain.get(), 0) == nullptr)) {
return false;
}
// We must have successfully extracted a public key from the certificate,
// delegated credential, etc.
if (UsesPrivateKey() && pubkey == nullptr) {
return false;
}
if (UsesPrivateKey() && privkey == nullptr && key_method == nullptr) {
return false;
}
if (type == SSLCredentialType::kDelegated && dc == nullptr) {
return false;
}
return true;
}
bool ssl_credential_st::SetLeafCert(UniquePtr<CRYPTO_BUFFER> leaf,
bool discard_key_on_mismatch) {
if (!UsesX509()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return false;
}
const bool private_key_matches_leaf = type != SSLCredentialType::kDelegated;
CBS cbs;
CRYPTO_BUFFER_init_CBS(leaf.get(), &cbs);
UniquePtr<EVP_PKEY> new_pubkey = ssl_cert_parse_pubkey(&cbs);
if (new_pubkey == nullptr) {
return false;
}
if (!ssl_is_key_type_supported(EVP_PKEY_id(new_pubkey.get()))) {
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
return false;
}
// An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
// certificates, so sanity-check the key usage extension.
if (EVP_PKEY_id(new_pubkey.get()) == EVP_PKEY_EC &&
!ssl_cert_check_key_usage(&cbs, key_usage_digital_signature)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
return false;
}
if (private_key_matches_leaf && privkey != nullptr &&
!ssl_compare_public_and_private_key(new_pubkey.get(), privkey.get())) {
if (!discard_key_on_mismatch) {
return false;
}
ERR_clear_error();
privkey = nullptr;
}
if (chain == nullptr) {
chain = new_leafless_chain();
if (chain == nullptr) {
return false;
}
}
CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(chain.get(), 0));
sk_CRYPTO_BUFFER_set(chain.get(), 0, leaf.release());
if (private_key_matches_leaf) {
pubkey = std::move(new_pubkey);
}
return true;
}
void ssl_credential_st::ClearIntermediateCerts() {
if (chain == nullptr) {
return;
}
while (sk_CRYPTO_BUFFER_num(chain.get()) > 1) {
CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_pop(chain.get()));
}
}
bool ssl_credential_st::ChainContainsIssuer(
bssl::Span<const uint8_t> dn) const {
if (UsesX509()) {
// TODO(bbe) This is used for matching a chain by CA name for the CA
// extension. If we require a chain to be present, we could remove any
// remaining parts of the chain after the found issuer, on the assumption
// that the peer sending the CA extension has the issuer in their trust
// store and does not need us to waste bytes on the wire.
CBS dn_cbs;
CBS_init(&dn_cbs, dn.data(), dn.size());
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain.get()); i++) {
const CRYPTO_BUFFER *cert = sk_CRYPTO_BUFFER_value(chain.get(), i);
CBS cert_cbs;
CRYPTO_BUFFER_init_CBS(cert, &cert_cbs);
if (ssl_cert_matches_issuer(&cert_cbs, &dn_cbs)) {
return true;
}
}
}
return false;
}
bool ssl_credential_st::HasPAKEAttempts() const {
return pake_limit.load() != 0;
}
bool ssl_credential_st::ClaimPAKEAttempt() const {
uint32_t current = pake_limit.load(std::memory_order_relaxed);
for (;;) {
if (current == 0) {
return false;
}
if (pake_limit.compare_exchange_weak(current, current - 1)) {
break;
}
}
return true;
}
void ssl_credential_st::RestorePAKEAttempt() const {
// This should not overflow because it will only be paired with
// ClaimPAKEAttempt.
pake_limit.fetch_add(1);
}
bool ssl_credential_st::AppendIntermediateCert(UniquePtr<CRYPTO_BUFFER> cert) {
if (!UsesX509()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return false;
}
if (chain == nullptr) {
chain = new_leafless_chain();
if (chain == nullptr) {
return false;
}
}
return PushToStack(chain.get(), std::move(cert));
}
SSL_CREDENTIAL *SSL_CREDENTIAL_new_x509(void) {
return New<SSL_CREDENTIAL>(SSLCredentialType::kX509);
}
SSL_CREDENTIAL *SSL_CREDENTIAL_new_delegated(void) {
return New<SSL_CREDENTIAL>(SSLCredentialType::kDelegated);
}
void SSL_CREDENTIAL_up_ref(SSL_CREDENTIAL *cred) { cred->UpRefInternal(); }
void SSL_CREDENTIAL_free(SSL_CREDENTIAL *cred) {
if (cred != nullptr) {
cred->DecRefInternal();
}
}
int SSL_CREDENTIAL_set1_private_key(SSL_CREDENTIAL *cred, EVP_PKEY *key) {
if (!cred->UsesPrivateKey()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
// If the public half has been configured, check |key| matches. |pubkey| will
// have been extracted from the certificate, delegated credential, etc.
if (cred->pubkey != nullptr &&
!ssl_compare_public_and_private_key(cred->pubkey.get(), key)) {
return false;
}
cred->privkey = UpRef(key);
cred->key_method = nullptr;
return 1;
}
int SSL_CREDENTIAL_set_private_key_method(
SSL_CREDENTIAL *cred, const SSL_PRIVATE_KEY_METHOD *key_method) {
if (!cred->UsesPrivateKey()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
cred->privkey = nullptr;
cred->key_method = key_method;
return 1;
}
int SSL_CREDENTIAL_set1_cert_chain(SSL_CREDENTIAL *cred,
CRYPTO_BUFFER *const *certs,
size_t num_certs) {
if (!cred->UsesX509() || num_certs == 0) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
if (!cred->SetLeafCert(UpRef(certs[0]), /*discard_key_on_mismatch=*/false)) {
return 0;
}
cred->ClearIntermediateCerts();
for (size_t i = 1; i < num_certs; i++) {
if (!cred->AppendIntermediateCert(UpRef(certs[i]))) {
return 0;
}
}
return 1;
}
int SSL_CREDENTIAL_set1_delegated_credential(SSL_CREDENTIAL *cred,
CRYPTO_BUFFER *dc) {
if (cred->type != SSLCredentialType::kDelegated) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
// Parse the delegated credential to check for validity, and extract a few
// fields from it. See RFC 9345, section 4.
CBS cbs, spki, sig;
uint32_t valid_time;
uint16_t dc_cert_verify_algorithm, algorithm;
CRYPTO_BUFFER_init_CBS(dc, &cbs);
if (!CBS_get_u32(&cbs, &valid_time) ||
!CBS_get_u16(&cbs, &dc_cert_verify_algorithm) ||
!CBS_get_u24_length_prefixed(&cbs, &spki) ||
!CBS_get_u16(&cbs, &algorithm) ||
!CBS_get_u16_length_prefixed(&cbs, &sig) || //
CBS_len(&sig) == 0 || //
CBS_len(&cbs) != 0) {
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
return 0;
}
// RFC 9345 forbids algorithms that use the rsaEncryption OID. As the
// RSASSA-PSS OID is unusably complicated, this effectively means we will not
// support RSA delegated credentials.
if (SSL_get_signature_algorithm_key_type(dc_cert_verify_algorithm) ==
EVP_PKEY_RSA) {
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SIGNATURE_ALGORITHM);
return 0;
}
UniquePtr<EVP_PKEY> pubkey(EVP_parse_public_key(&spki));
if (pubkey == nullptr || CBS_len(&spki) != 0) {
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
return 0;
}
if (!cred->sigalgs.CopyFrom(Span(&dc_cert_verify_algorithm, 1))) {
return 0;
}
if (cred->privkey != nullptr &&
!ssl_compare_public_and_private_key(pubkey.get(), cred->privkey.get())) {
return 0;
}
cred->dc = UpRef(dc);
cred->pubkey = std::move(pubkey);
cred->dc_algorithm = algorithm;
return 1;
}
int SSL_CREDENTIAL_set1_ocsp_response(SSL_CREDENTIAL *cred,
CRYPTO_BUFFER *ocsp) {
if (!cred->UsesX509()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
cred->ocsp_response = UpRef(ocsp);
return 1;
}
int SSL_CREDENTIAL_set1_signed_cert_timestamp_list(SSL_CREDENTIAL *cred,
CRYPTO_BUFFER *sct_list) {
if (!cred->UsesX509()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
CBS cbs;
CRYPTO_BUFFER_init_CBS(sct_list, &cbs);
if (!ssl_is_sct_list_valid(&cbs)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
return 0;
}
cred->signed_cert_timestamp_list = UpRef(sct_list);
return 1;
}
int SSL_spake2plusv1_register(uint8_t out_w0[32], uint8_t out_w1[32],
uint8_t out_registration_record[65],
const uint8_t *password, size_t password_len,
const uint8_t *client_identity,
size_t client_identity_len,
const uint8_t *server_identity,
size_t server_identity_len) {
return spake2plus::Register(
Span(out_w0, 32), Span(out_w1, 32), Span(out_registration_record, 65),
Span(password, password_len), Span(client_identity, client_identity_len),
Span(server_identity, server_identity_len));
}
static UniquePtr<SSL_CREDENTIAL> ssl_credential_new_spake2plusv1(
SSLCredentialType type, Span<const uint8_t> context,
Span<const uint8_t> client_identity, Span<const uint8_t> server_identity,
uint32_t limit) {
assert(type == SSLCredentialType::kSPAKE2PlusV1Client ||
type == SSLCredentialType::kSPAKE2PlusV1Server);
auto cred = MakeUnique<SSL_CREDENTIAL>(type);
if (cred == nullptr) {
return nullptr;
}
if (!cred->pake_context.CopyFrom(context) ||
!cred->client_identity.CopyFrom(client_identity) ||
!cred->server_identity.CopyFrom(server_identity)) {
return nullptr;
}
cred->pake_limit.store(limit);
return cred;
}
SSL_CREDENTIAL *SSL_CREDENTIAL_new_spake2plusv1_client(
const uint8_t *context, size_t context_len, const uint8_t *client_identity,
size_t client_identity_len, const uint8_t *server_identity,
size_t server_identity_len, uint32_t error_limit, const uint8_t *w0,
size_t w0_len, const uint8_t *w1, size_t w1_len) {
if (w0_len != spake2plus::kVerifierSize ||
w1_len != spake2plus::kVerifierSize ||
(context == nullptr && context_len != 0)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SPAKE2PLUSV1_VALUE);
return nullptr;
}
UniquePtr<SSL_CREDENTIAL> cred = ssl_credential_new_spake2plusv1(
SSLCredentialType::kSPAKE2PlusV1Client, Span(context, context_len),
Span(client_identity, client_identity_len),
Span(server_identity, server_identity_len), error_limit);
if (!cred) {
return nullptr;
}
if (!cred->password_verifier_w0.CopyFrom(Span(w0, w0_len)) ||
!cred->password_verifier_w1.CopyFrom(Span(w1, w1_len))) {
return nullptr;
}
return cred.release();
}
SSL_CREDENTIAL *SSL_CREDENTIAL_new_spake2plusv1_server(
const uint8_t *context, size_t context_len, const uint8_t *client_identity,
size_t client_identity_len, const uint8_t *server_identity,
size_t server_identity_len, uint32_t rate_limit, const uint8_t *w0,
size_t w0_len, const uint8_t *registration_record,
size_t registration_record_len) {
if (w0_len != spake2plus::kVerifierSize ||
registration_record_len != spake2plus::kRegistrationRecordSize ||
(context == nullptr && context_len != 0)) {
return nullptr;
}
UniquePtr<SSL_CREDENTIAL> cred = ssl_credential_new_spake2plusv1(
SSLCredentialType::kSPAKE2PlusV1Server, Span(context, context_len),
Span(client_identity, client_identity_len),
Span(server_identity, server_identity_len), rate_limit);
if (!cred) {
return nullptr;
}
if (!cred->password_verifier_w0.CopyFrom(Span(w0, w0_len)) ||
!cred->registration_record.CopyFrom(
Span(registration_record, registration_record_len))) {
return nullptr;
}
return cred.release();
}
int SSL_CTX_add1_credential(SSL_CTX *ctx, SSL_CREDENTIAL *cred) {
if (!cred->IsComplete()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
return ctx->cert->credentials.Push(UpRef(cred));
}
int SSL_add1_credential(SSL *ssl, SSL_CREDENTIAL *cred) {
if (ssl->config == nullptr) {
return 0;
}
if (!cred->IsComplete()) {
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
return 0;
}
return ssl->config->cert->credentials.Push(UpRef(cred));
}
const SSL_CREDENTIAL *SSL_get0_selected_credential(const SSL *ssl) {
if (ssl->s3->hs == nullptr) {
return nullptr;
}
return ssl->s3->hs->credential.get();
}
int SSL_CREDENTIAL_get_ex_new_index(long argl, void *argp,
CRYPTO_EX_unused *unused,
CRYPTO_EX_dup *dup_unused,
CRYPTO_EX_free *free_func) {
return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func);
}
int SSL_CREDENTIAL_set_ex_data(SSL_CREDENTIAL *cred, int idx, void *arg) {
return CRYPTO_set_ex_data(&cred->ex_data, idx, arg);
}
void *SSL_CREDENTIAL_get_ex_data(const SSL_CREDENTIAL *cred, int idx) {
return CRYPTO_get_ex_data(&cred->ex_data, idx);
}
void SSL_CREDENTIAL_set_must_match_issuer(SSL_CREDENTIAL *cred) {
cred->must_match_issuer = true;
}
void SSL_CREDENTIAL_clear_must_match_issuer(SSL_CREDENTIAL *cred) {
cred->must_match_issuer = false;
}
int SSL_CREDENTIAL_must_match_issuer(const SSL_CREDENTIAL *cred) {
return cred->must_match_issuer ? 1 : 0;
}