1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110 /* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
114
115 #include <openssl/ssl.h>
116
117 #include <assert.h>
118 #include <limits.h>
119 #include <string.h>
120
121 #include <utility>
122
123 #include <openssl/bn.h>
124 #include <openssl/bytestring.h>
125 #include <openssl/ec_key.h>
126 #include <openssl/err.h>
127 #include <openssl/mem.h>
128 #include <openssl/sha.h>
129 #include <openssl/x509.h>
130
131 #include "../crypto/internal.h"
132 #include "internal.h"
133
134
135 BSSL_NAMESPACE_BEGIN
136
CERT(const SSL_X509_METHOD * x509_method_arg)137 CERT::CERT(const SSL_X509_METHOD *x509_method_arg)
138 : x509_method(x509_method_arg) {}
139
~CERT()140 CERT::~CERT() {
141 ssl_cert_clear_certs(this);
142 x509_method->cert_free(this);
143 }
144
buffer_up_ref(const CRYPTO_BUFFER * buffer)145 static CRYPTO_BUFFER *buffer_up_ref(const CRYPTO_BUFFER *buffer) {
146 CRYPTO_BUFFER_up_ref(const_cast<CRYPTO_BUFFER *>(buffer));
147 return const_cast<CRYPTO_BUFFER *>(buffer);
148 }
149
ssl_cert_dup(CERT * cert)150 UniquePtr<CERT> ssl_cert_dup(CERT *cert) {
151 UniquePtr<CERT> ret = MakeUnique<CERT>(cert->x509_method);
152 if (!ret) {
153 return nullptr;
154 }
155
156 if (cert->chain) {
157 ret->chain.reset(sk_CRYPTO_BUFFER_deep_copy(
158 cert->chain.get(), buffer_up_ref, CRYPTO_BUFFER_free));
159 if (!ret->chain) {
160 return nullptr;
161 }
162 }
163
164 ret->privatekey = UpRef(cert->privatekey);
165 ret->key_method = cert->key_method;
166
167 if (!ret->sigalgs.CopyFrom(cert->sigalgs)) {
168 return nullptr;
169 }
170
171 ret->cert_cb = cert->cert_cb;
172 ret->cert_cb_arg = cert->cert_cb_arg;
173
174 ret->x509_method->cert_dup(ret.get(), cert);
175
176 ret->signed_cert_timestamp_list = UpRef(cert->signed_cert_timestamp_list);
177 ret->ocsp_response = UpRef(cert->ocsp_response);
178
179 ret->sid_ctx_length = cert->sid_ctx_length;
180 OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
181
182 if (cert->dc) {
183 ret->dc = cert->dc->Dup();
184 if (!ret->dc) {
185 return nullptr;
186 }
187 }
188
189 ret->dc_privatekey = UpRef(cert->dc_privatekey);
190 ret->dc_key_method = cert->dc_key_method;
191
192 return ret;
193 }
194
195 // Free up and clear all certificates and chains
ssl_cert_clear_certs(CERT * cert)196 void ssl_cert_clear_certs(CERT *cert) {
197 if (cert == NULL) {
198 return;
199 }
200
201 cert->x509_method->cert_clear(cert);
202
203 cert->chain.reset();
204 cert->privatekey.reset();
205 cert->key_method = nullptr;
206
207 cert->dc.reset();
208 cert->dc_privatekey.reset();
209 cert->dc_key_method = nullptr;
210 }
211
ssl_cert_set_cert_cb(CERT * cert,int (* cb)(SSL * ssl,void * arg),void * arg)212 static void ssl_cert_set_cert_cb(CERT *cert, int (*cb)(SSL *ssl, void *arg),
213 void *arg) {
214 cert->cert_cb = cb;
215 cert->cert_cb_arg = arg;
216 }
217
218 enum leaf_cert_and_privkey_result_t {
219 leaf_cert_and_privkey_error,
220 leaf_cert_and_privkey_ok,
221 leaf_cert_and_privkey_mismatch,
222 };
223
224 // check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer|
225 // and the private key in |privkey| are suitable and coherent. It returns
226 // |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is
227 // found. If the certificate and private key are valid, but incoherent, it
228 // returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns
229 // |leaf_cert_and_privkey_ok|.
check_leaf_cert_and_privkey(CRYPTO_BUFFER * leaf_buffer,EVP_PKEY * privkey)230 static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey(
231 CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) {
232 CBS cert_cbs;
233 CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs);
234 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
235 if (!pubkey) {
236 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
237 return leaf_cert_and_privkey_error;
238 }
239
240 if (!ssl_is_key_type_supported(EVP_PKEY_id(pubkey.get()))) {
241 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
242 return leaf_cert_and_privkey_error;
243 }
244
245 // An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
246 // certificates, so sanity-check the key usage extension.
247 if (EVP_PKEY_id(pubkey.get()) == EVP_PKEY_EC &&
248 !ssl_cert_check_key_usage(&cert_cbs, key_usage_digital_signature)) {
249 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
250 return leaf_cert_and_privkey_error;
251 }
252
253 if (privkey != NULL &&
254 // Sanity-check that the private key and the certificate match.
255 !ssl_compare_public_and_private_key(pubkey.get(), privkey)) {
256 ERR_clear_error();
257 return leaf_cert_and_privkey_mismatch;
258 }
259
260 return leaf_cert_and_privkey_ok;
261 }
262
cert_set_chain_and_key(CERT * cert,CRYPTO_BUFFER * const * certs,size_t num_certs,EVP_PKEY * privkey,const SSL_PRIVATE_KEY_METHOD * privkey_method)263 static int cert_set_chain_and_key(
264 CERT *cert, CRYPTO_BUFFER *const *certs, size_t num_certs,
265 EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) {
266 if (num_certs == 0 ||
267 (privkey == NULL && privkey_method == NULL)) {
268 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
269 return 0;
270 }
271
272 if (privkey != NULL && privkey_method != NULL) {
273 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
274 return 0;
275 }
276
277 switch (check_leaf_cert_and_privkey(certs[0], privkey)) {
278 case leaf_cert_and_privkey_error:
279 return 0;
280 case leaf_cert_and_privkey_mismatch:
281 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
282 return 0;
283 case leaf_cert_and_privkey_ok:
284 break;
285 }
286
287 UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs_sk(sk_CRYPTO_BUFFER_new_null());
288 if (!certs_sk) {
289 return 0;
290 }
291
292 for (size_t i = 0; i < num_certs; i++) {
293 if (!PushToStack(certs_sk.get(), UpRef(certs[i]))) {
294 return 0;
295 }
296 }
297
298 cert->privatekey = UpRef(privkey);
299 cert->key_method = privkey_method;
300
301 cert->chain = std::move(certs_sk);
302 return 1;
303 }
304
ssl_set_cert(CERT * cert,UniquePtr<CRYPTO_BUFFER> buffer)305 bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) {
306 switch (check_leaf_cert_and_privkey(buffer.get(), cert->privatekey.get())) {
307 case leaf_cert_and_privkey_error:
308 return false;
309 case leaf_cert_and_privkey_mismatch:
310 // don't fail for a cert/key mismatch, just free current private key
311 // (when switching to a different cert & key, first this function should
312 // be used, then |ssl_set_pkey|.
313 cert->privatekey.reset();
314 break;
315 case leaf_cert_and_privkey_ok:
316 break;
317 }
318
319 cert->x509_method->cert_flush_cached_leaf(cert);
320
321 if (cert->chain != nullptr) {
322 CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0));
323 sk_CRYPTO_BUFFER_set(cert->chain.get(), 0, buffer.release());
324 return true;
325 }
326
327 cert->chain.reset(sk_CRYPTO_BUFFER_new_null());
328 if (cert->chain == nullptr) {
329 return false;
330 }
331
332 if (!PushToStack(cert->chain.get(), std::move(buffer))) {
333 cert->chain.reset();
334 return false;
335 }
336
337 return true;
338 }
339
ssl_has_certificate(const SSL_HANDSHAKE * hs)340 bool ssl_has_certificate(const SSL_HANDSHAKE *hs) {
341 return hs->config->cert->chain != nullptr &&
342 sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0) != nullptr &&
343 ssl_has_private_key(hs);
344 }
345
ssl_parse_cert_chain(uint8_t * out_alert,UniquePtr<STACK_OF (CRYPTO_BUFFER)> * out_chain,UniquePtr<EVP_PKEY> * out_pubkey,uint8_t * out_leaf_sha256,CBS * cbs,CRYPTO_BUFFER_POOL * pool)346 bool ssl_parse_cert_chain(uint8_t *out_alert,
347 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
348 UniquePtr<EVP_PKEY> *out_pubkey,
349 uint8_t *out_leaf_sha256, CBS *cbs,
350 CRYPTO_BUFFER_POOL *pool) {
351 out_chain->reset();
352 out_pubkey->reset();
353
354 CBS certificate_list;
355 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
356 *out_alert = SSL_AD_DECODE_ERROR;
357 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
358 return false;
359 }
360
361 if (CBS_len(&certificate_list) == 0) {
362 return true;
363 }
364
365 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null());
366 if (!chain) {
367 *out_alert = SSL_AD_INTERNAL_ERROR;
368 return false;
369 }
370
371 UniquePtr<EVP_PKEY> pubkey;
372 while (CBS_len(&certificate_list) > 0) {
373 CBS certificate;
374 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
375 CBS_len(&certificate) == 0) {
376 *out_alert = SSL_AD_DECODE_ERROR;
377 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
378 return false;
379 }
380
381 if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) {
382 pubkey = ssl_cert_parse_pubkey(&certificate);
383 if (!pubkey) {
384 *out_alert = SSL_AD_DECODE_ERROR;
385 return false;
386 }
387
388 // Retain the hash of the leaf certificate if requested.
389 if (out_leaf_sha256 != NULL) {
390 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
391 }
392 }
393
394 UniquePtr<CRYPTO_BUFFER> buf(
395 CRYPTO_BUFFER_new_from_CBS(&certificate, pool));
396 if (!buf ||
397 !PushToStack(chain.get(), std::move(buf))) {
398 *out_alert = SSL_AD_INTERNAL_ERROR;
399 return false;
400 }
401 }
402
403 *out_chain = std::move(chain);
404 *out_pubkey = std::move(pubkey);
405 return true;
406 }
407
ssl_add_cert_chain(SSL_HANDSHAKE * hs,CBB * cbb)408 bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb) {
409 if (!ssl_has_certificate(hs)) {
410 return CBB_add_u24(cbb, 0);
411 }
412
413 CBB certs;
414 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
415 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
416 return false;
417 }
418
419 STACK_OF(CRYPTO_BUFFER) *chain = hs->config->cert->chain.get();
420 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
421 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
422 CBB child;
423 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
424 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
425 CRYPTO_BUFFER_len(buffer)) ||
426 !CBB_flush(&certs)) {
427 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
428 return false;
429 }
430 }
431
432 return CBB_flush(cbb);
433 }
434
435 // ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
436 // positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
437 // subjectPublicKeyInfo.
ssl_cert_skip_to_spki(const CBS * in,CBS * out_tbs_cert)438 static bool ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
439 /* From RFC 5280, section 4.1
440 * Certificate ::= SEQUENCE {
441 * tbsCertificate TBSCertificate,
442 * signatureAlgorithm AlgorithmIdentifier,
443 * signatureValue BIT STRING }
444
445 * TBSCertificate ::= SEQUENCE {
446 * version [0] EXPLICIT Version DEFAULT v1,
447 * serialNumber CertificateSerialNumber,
448 * signature AlgorithmIdentifier,
449 * issuer Name,
450 * validity Validity,
451 * subject Name,
452 * subjectPublicKeyInfo SubjectPublicKeyInfo,
453 * ... } */
454 CBS buf = *in;
455
456 CBS toplevel;
457 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
458 CBS_len(&buf) != 0 ||
459 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
460 // version
461 !CBS_get_optional_asn1(
462 out_tbs_cert, NULL, NULL,
463 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
464 // serialNumber
465 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
466 // signature algorithm
467 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
468 // issuer
469 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
470 // validity
471 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
472 // subject
473 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
474 return false;
475 }
476
477 return true;
478 }
479
ssl_cert_parse_pubkey(const CBS * in)480 UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) {
481 CBS buf = *in, tbs_cert;
482 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
483 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
484 return nullptr;
485 }
486
487 return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert));
488 }
489
ssl_compare_public_and_private_key(const EVP_PKEY * pubkey,const EVP_PKEY * privkey)490 bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
491 const EVP_PKEY *privkey) {
492 if (EVP_PKEY_is_opaque(privkey)) {
493 // We cannot check an opaque private key and have to trust that it
494 // matches.
495 return true;
496 }
497
498 switch (EVP_PKEY_cmp(pubkey, privkey)) {
499 case 1:
500 return true;
501 case 0:
502 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
503 return false;
504 case -1:
505 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
506 return false;
507 case -2:
508 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
509 return false;
510 }
511
512 assert(0);
513 return false;
514 }
515
ssl_cert_check_private_key(const CERT * cert,const EVP_PKEY * privkey)516 bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
517 if (privkey == nullptr) {
518 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
519 return false;
520 }
521
522 if (cert->chain == nullptr ||
523 sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) {
524 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
525 return false;
526 }
527
528 CBS cert_cbs;
529 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0),
530 &cert_cbs);
531 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
532 if (!pubkey) {
533 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
534 return false;
535 }
536
537 return ssl_compare_public_and_private_key(pubkey.get(), privkey);
538 }
539
ssl_cert_check_key_usage(const CBS * in,enum ssl_key_usage_t bit)540 bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit) {
541 CBS buf = *in;
542
543 CBS tbs_cert, outer_extensions;
544 int has_extensions;
545 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
546 // subjectPublicKeyInfo
547 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
548 // issuerUniqueID
549 !CBS_get_optional_asn1(&tbs_cert, NULL, NULL,
550 CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
551 // subjectUniqueID
552 !CBS_get_optional_asn1(&tbs_cert, NULL, NULL,
553 CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
554 !CBS_get_optional_asn1(
555 &tbs_cert, &outer_extensions, &has_extensions,
556 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
557 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
558 return false;
559 }
560
561 if (!has_extensions) {
562 return true;
563 }
564
565 CBS extensions;
566 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
567 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
568 return false;
569 }
570
571 while (CBS_len(&extensions) > 0) {
572 CBS extension, oid, contents;
573 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
574 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
575 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
576 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
577 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
578 CBS_len(&extension) != 0) {
579 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
580 return false;
581 }
582
583 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
584 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
585 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
586 0) {
587 continue;
588 }
589
590 CBS bit_string;
591 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
592 CBS_len(&contents) != 0) {
593 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
594 return false;
595 }
596
597 // This is the KeyUsage extension. See
598 // https://tools.ietf.org/html/rfc5280#section-4.2.1.3
599 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
600 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
601 return false;
602 }
603
604 if (!CBS_asn1_bitstring_has_bit(&bit_string, bit)) {
605 OPENSSL_PUT_ERROR(SSL, SSL_R_KEY_USAGE_BIT_INCORRECT);
606 return false;
607 }
608
609 return true;
610 }
611
612 // No KeyUsage extension found.
613 return true;
614 }
615
ssl_parse_client_CA_list(SSL * ssl,uint8_t * out_alert,CBS * cbs)616 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
617 uint8_t *out_alert,
618 CBS *cbs) {
619 CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
620
621 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
622 if (!ret) {
623 *out_alert = SSL_AD_INTERNAL_ERROR;
624 return nullptr;
625 }
626
627 CBS child;
628 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
629 *out_alert = SSL_AD_DECODE_ERROR;
630 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
631 return nullptr;
632 }
633
634 while (CBS_len(&child) > 0) {
635 CBS distinguished_name;
636 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
637 *out_alert = SSL_AD_DECODE_ERROR;
638 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
639 return nullptr;
640 }
641
642 UniquePtr<CRYPTO_BUFFER> buffer(
643 CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool));
644 if (!buffer ||
645 !PushToStack(ret.get(), std::move(buffer))) {
646 *out_alert = SSL_AD_INTERNAL_ERROR;
647 return nullptr;
648 }
649 }
650
651 if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) {
652 *out_alert = SSL_AD_DECODE_ERROR;
653 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
654 return nullptr;
655 }
656
657 return ret;
658 }
659
ssl_has_client_CAs(const SSL_CONFIG * cfg)660 bool ssl_has_client_CAs(const SSL_CONFIG *cfg) {
661 const STACK_OF(CRYPTO_BUFFER) *names = cfg->client_CA.get();
662 if (names == nullptr) {
663 names = cfg->ssl->ctx->client_CA.get();
664 }
665 if (names == nullptr) {
666 return false;
667 }
668 return sk_CRYPTO_BUFFER_num(names) > 0;
669 }
670
ssl_add_client_CA_list(SSL_HANDSHAKE * hs,CBB * cbb)671 bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb) {
672 CBB child, name_cbb;
673 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
674 return false;
675 }
676
677 const STACK_OF(CRYPTO_BUFFER) *names = hs->config->client_CA.get();
678 if (names == NULL) {
679 names = hs->ssl->ctx->client_CA.get();
680 }
681 if (names == NULL) {
682 return CBB_flush(cbb);
683 }
684
685 for (const CRYPTO_BUFFER *name : names) {
686 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
687 !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
688 CRYPTO_BUFFER_len(name))) {
689 return false;
690 }
691 }
692
693 return CBB_flush(cbb);
694 }
695
ssl_check_leaf_certificate(SSL_HANDSHAKE * hs,EVP_PKEY * pkey,const CRYPTO_BUFFER * leaf)696 bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
697 const CRYPTO_BUFFER *leaf) {
698 assert(ssl_protocol_version(hs->ssl) < TLS1_3_VERSION);
699
700 // Check the certificate's type matches the cipher.
701 if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
702 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
703 return false;
704 }
705
706 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
707 // Check the key's group and point format are acceptable.
708 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
709 uint16_t group_id;
710 if (!ssl_nid_to_group_id(
711 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
712 !tls1_check_group_id(hs, group_id) ||
713 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
714 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
715 return false;
716 }
717 }
718
719 return true;
720 }
721
ssl_on_certificate_selected(SSL_HANDSHAKE * hs)722 bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
723 SSL *const ssl = hs->ssl;
724 if (!ssl_has_certificate(hs)) {
725 // Nothing to do.
726 return true;
727 }
728
729 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(hs)) {
730 return false;
731 }
732
733 CBS leaf;
734 CRYPTO_BUFFER_init_CBS(
735 sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0), &leaf);
736
737 if (ssl_signing_with_dc(hs)) {
738 hs->local_pubkey = UpRef(hs->config->cert->dc->pkey);
739 } else {
740 hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
741 }
742 return hs->local_pubkey != NULL;
743 }
744
745
746 // Delegated credentials.
747
748 DC::DC() = default;
749 DC::~DC() = default;
750
Dup()751 UniquePtr<DC> DC::Dup() {
752 bssl::UniquePtr<DC> ret = MakeUnique<DC>();
753 if (!ret) {
754 return nullptr;
755 }
756
757 ret->raw = UpRef(raw);
758 ret->expected_cert_verify_algorithm = expected_cert_verify_algorithm;
759 ret->pkey = UpRef(pkey);
760 return ret;
761 }
762
763 // static
Parse(CRYPTO_BUFFER * in,uint8_t * out_alert)764 UniquePtr<DC> DC::Parse(CRYPTO_BUFFER *in, uint8_t *out_alert) {
765 UniquePtr<DC> dc = MakeUnique<DC>();
766 if (!dc) {
767 *out_alert = SSL_AD_INTERNAL_ERROR;
768 return nullptr;
769 }
770
771 dc->raw = UpRef(in);
772
773 CBS pubkey, deleg, sig;
774 uint32_t valid_time;
775 uint16_t algorithm;
776 CRYPTO_BUFFER_init_CBS(dc->raw.get(), &deleg);
777 if (!CBS_get_u32(&deleg, &valid_time) ||
778 !CBS_get_u16(&deleg, &dc->expected_cert_verify_algorithm) ||
779 !CBS_get_u24_length_prefixed(&deleg, &pubkey) ||
780 !CBS_get_u16(&deleg, &algorithm) ||
781 !CBS_get_u16_length_prefixed(&deleg, &sig) ||
782 CBS_len(&deleg) != 0) {
783 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
784 *out_alert = SSL_AD_DECODE_ERROR;
785 return nullptr;
786 }
787
788 dc->pkey.reset(EVP_parse_public_key(&pubkey));
789 if (dc->pkey == nullptr) {
790 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
791 *out_alert = SSL_AD_DECODE_ERROR;
792 return nullptr;
793 }
794
795 return dc;
796 }
797
798 // ssl_can_serve_dc returns true if the host has configured a DC that it can
799 // serve in the handshake. Specifically, it checks that a DC has been
800 // configured and that the DC signature algorithm is supported by the peer.
ssl_can_serve_dc(const SSL_HANDSHAKE * hs)801 static bool ssl_can_serve_dc(const SSL_HANDSHAKE *hs) {
802 // Check that a DC has been configured.
803 const CERT *cert = hs->config->cert.get();
804 if (cert->dc == nullptr ||
805 cert->dc->raw == nullptr ||
806 (cert->dc_privatekey == nullptr && cert->dc_key_method == nullptr)) {
807 return false;
808 }
809
810 // Check that 1.3 or higher has been negotiated.
811 const DC *dc = cert->dc.get();
812 assert(hs->ssl->s3->have_version);
813 if (ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
814 return false;
815 }
816
817 // Check that the DC signature algorithm is supported by the peer.
818 Span<const uint16_t> peer_sigalgs = hs->peer_delegated_credential_sigalgs;
819 for (uint16_t peer_sigalg : peer_sigalgs) {
820 if (dc->expected_cert_verify_algorithm == peer_sigalg) {
821 return true;
822 }
823 }
824 return false;
825 }
826
ssl_signing_with_dc(const SSL_HANDSHAKE * hs)827 bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs) {
828 // As of draft-ietf-tls-subcert-03, only the server may use delegated
829 // credentials to authenticate itself.
830 return hs->ssl->server &&
831 hs->delegated_credential_requested &&
832 ssl_can_serve_dc(hs);
833 }
834
cert_set_dc(CERT * cert,CRYPTO_BUFFER * const raw,EVP_PKEY * privkey,const SSL_PRIVATE_KEY_METHOD * key_method)835 static int cert_set_dc(CERT *cert, CRYPTO_BUFFER *const raw, EVP_PKEY *privkey,
836 const SSL_PRIVATE_KEY_METHOD *key_method) {
837 if (privkey == nullptr && key_method == nullptr) {
838 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
839 return 0;
840 }
841
842 if (privkey != nullptr && key_method != nullptr) {
843 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
844 return 0;
845 }
846
847 uint8_t alert;
848 UniquePtr<DC> dc = DC::Parse(raw, &alert);
849 if (dc == nullptr) {
850 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_DELEGATED_CREDENTIAL);
851 return 0;
852 }
853
854 if (privkey) {
855 // Check that the public and private keys match.
856 if (!ssl_compare_public_and_private_key(dc->pkey.get(), privkey)) {
857 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
858 return 0;
859 }
860 }
861
862 cert->dc = std::move(dc);
863 cert->dc_privatekey = UpRef(privkey);
864 cert->dc_key_method = key_method;
865
866 return 1;
867 }
868
869 BSSL_NAMESPACE_END
870
871 using namespace bssl;
872
SSL_set_chain_and_key(SSL * ssl,CRYPTO_BUFFER * const * certs,size_t num_certs,EVP_PKEY * privkey,const SSL_PRIVATE_KEY_METHOD * privkey_method)873 int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
874 size_t num_certs, EVP_PKEY *privkey,
875 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
876 if (!ssl->config) {
877 return 0;
878 }
879 return cert_set_chain_and_key(ssl->config->cert.get(), certs, num_certs,
880 privkey, privkey_method);
881 }
882
SSL_CTX_set_chain_and_key(SSL_CTX * ctx,CRYPTO_BUFFER * const * certs,size_t num_certs,EVP_PKEY * privkey,const SSL_PRIVATE_KEY_METHOD * privkey_method)883 int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
884 size_t num_certs, EVP_PKEY *privkey,
885 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
886 return cert_set_chain_and_key(ctx->cert.get(), certs, num_certs, privkey,
887 privkey_method);
888 }
889
STACK_OF(CRYPTO_BUFFER)890 const STACK_OF(CRYPTO_BUFFER)* SSL_CTX_get0_chain(const SSL_CTX *ctx) {
891 return ctx->cert->chain.get();
892 }
893
SSL_CTX_use_certificate_ASN1(SSL_CTX * ctx,size_t der_len,const uint8_t * der)894 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
895 const uint8_t *der) {
896 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
897 if (!buffer) {
898 return 0;
899 }
900
901 return ssl_set_cert(ctx->cert.get(), std::move(buffer));
902 }
903
SSL_use_certificate_ASN1(SSL * ssl,const uint8_t * der,size_t der_len)904 int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
905 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
906 if (!buffer || !ssl->config) {
907 return 0;
908 }
909
910 return ssl_set_cert(ssl->config->cert.get(), std::move(buffer));
911 }
912
SSL_CTX_set_cert_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,void * arg),void * arg)913 void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
914 void *arg) {
915 ssl_cert_set_cert_cb(ctx->cert.get(), cb, arg);
916 }
917
SSL_set_cert_cb(SSL * ssl,int (* cb)(SSL * ssl,void * arg),void * arg)918 void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
919 if (!ssl->config) {
920 return;
921 }
922 ssl_cert_set_cert_cb(ssl->config->cert.get(), cb, arg);
923 }
924
STACK_OF(CRYPTO_BUFFER)925 const STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
926 SSL_SESSION *session = SSL_get_session(ssl);
927 if (session == NULL) {
928 return NULL;
929 }
930
931 return session->certs.get();
932 }
933
STACK_OF(CRYPTO_BUFFER)934 const STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
935 if (ssl->s3->hs == NULL) {
936 return NULL;
937 }
938 return ssl->s3->hs->ca_names.get();
939 }
940
set_signed_cert_timestamp_list(CERT * cert,const uint8_t * list,size_t list_len)941 static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
942 size_t list_len) {
943 CBS sct_list;
944 CBS_init(&sct_list, list, list_len);
945 if (!ssl_is_sct_list_valid(&sct_list)) {
946 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
947 return 0;
948 }
949
950 cert->signed_cert_timestamp_list.reset(
951 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), nullptr));
952 return cert->signed_cert_timestamp_list != nullptr;
953 }
954
SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX * ctx,const uint8_t * list,size_t list_len)955 int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
956 size_t list_len) {
957 return set_signed_cert_timestamp_list(ctx->cert.get(), list, list_len);
958 }
959
SSL_set_signed_cert_timestamp_list(SSL * ssl,const uint8_t * list,size_t list_len)960 int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
961 size_t list_len) {
962 if (!ssl->config) {
963 return 0;
964 }
965 return set_signed_cert_timestamp_list(ssl->config->cert.get(), list,
966 list_len);
967 }
968
SSL_CTX_set_ocsp_response(SSL_CTX * ctx,const uint8_t * response,size_t response_len)969 int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
970 size_t response_len) {
971 ctx->cert->ocsp_response.reset(
972 CRYPTO_BUFFER_new(response, response_len, nullptr));
973 return ctx->cert->ocsp_response != nullptr;
974 }
975
SSL_set_ocsp_response(SSL * ssl,const uint8_t * response,size_t response_len)976 int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
977 size_t response_len) {
978 if (!ssl->config) {
979 return 0;
980 }
981 ssl->config->cert->ocsp_response.reset(
982 CRYPTO_BUFFER_new(response, response_len, nullptr));
983 return ssl->config->cert->ocsp_response != nullptr;
984 }
985
SSL_CTX_set0_client_CAs(SSL_CTX * ctx,STACK_OF (CRYPTO_BUFFER)* name_list)986 void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) {
987 ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
988 ctx->client_CA.reset(name_list);
989 }
990
SSL_set0_client_CAs(SSL * ssl,STACK_OF (CRYPTO_BUFFER)* name_list)991 void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) {
992 if (!ssl->config) {
993 return;
994 }
995 ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get());
996 ssl->config->client_CA.reset(name_list);
997 }
998
SSL_set1_delegated_credential(SSL * ssl,CRYPTO_BUFFER * dc,EVP_PKEY * pkey,const SSL_PRIVATE_KEY_METHOD * key_method)999 int SSL_set1_delegated_credential(SSL *ssl, CRYPTO_BUFFER *dc, EVP_PKEY *pkey,
1000 const SSL_PRIVATE_KEY_METHOD *key_method) {
1001 if (!ssl->config) {
1002 return 0;
1003 }
1004
1005 return cert_set_dc(ssl->config->cert.get(), dc, pkey, key_method);
1006 }
1007
SSL_delegated_credential_used(const SSL * ssl)1008 int SSL_delegated_credential_used(const SSL *ssl) {
1009 return ssl->s3->delegated_credential_used;
1010 }
1011