1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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. */
115 #include <openssl/ssl.h>
123 #include <openssl/bn.h>
124 #include <openssl/buf.h>
125 #include <openssl/bytestring.h>
126 #include <openssl/ec_key.h>
127 #include <openssl/err.h>
128 #include <openssl/mem.h>
129 #include <openssl/sha.h>
130 #include <openssl/x509.h>
132 #include "../crypto/internal.h"
133 #include "internal.h"
138 CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) {
139 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
141 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
144 OPENSSL_memset(ret, 0, sizeof(CERT));
145 ret->x509_method = x509_method;
150 static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
151 CRYPTO_BUFFER_up_ref(buffer);
155 CERT *ssl_cert_dup(CERT *cert) {
156 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
158 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
161 OPENSSL_memset(ret, 0, sizeof(CERT));
163 ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
166 if (cert->privatekey != NULL) {
167 EVP_PKEY_up_ref(cert->privatekey);
168 ret->privatekey = cert->privatekey;
171 ret->key_method = cert->key_method;
172 ret->x509_method = cert->x509_method;
174 if (cert->sigalgs != NULL) {
175 ret->sigalgs = (uint16_t *)BUF_memdup(
176 cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
177 if (ret->sigalgs == NULL) {
181 ret->num_sigalgs = cert->num_sigalgs;
183 ret->cert_cb = cert->cert_cb;
184 ret->cert_cb_arg = cert->cert_cb_arg;
186 ret->x509_method->cert_dup(ret, cert);
188 if (cert->signed_cert_timestamp_list != NULL) {
189 CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list);
190 ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list;
193 if (cert->ocsp_response != NULL) {
194 CRYPTO_BUFFER_up_ref(cert->ocsp_response);
195 ret->ocsp_response = cert->ocsp_response;
198 ret->sid_ctx_length = cert->sid_ctx_length;
199 OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
201 ret->enable_early_data = cert->enable_early_data;
210 // Free up and clear all certificates and chains
211 void ssl_cert_clear_certs(CERT *cert) {
216 cert->x509_method->cert_clear(cert);
218 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
220 EVP_PKEY_free(cert->privatekey);
221 cert->privatekey = NULL;
222 cert->key_method = NULL;
225 void ssl_cert_free(CERT *cert) {
230 ssl_cert_clear_certs(cert);
231 cert->x509_method->cert_free(cert);
232 OPENSSL_free(cert->sigalgs);
233 CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
234 CRYPTO_BUFFER_free(cert->ocsp_response);
239 static void ssl_cert_set_cert_cb(CERT *cert, int (*cb)(SSL *ssl, void *arg),
242 cert->cert_cb_arg = arg;
245 enum leaf_cert_and_privkey_result_t {
246 leaf_cert_and_privkey_error,
247 leaf_cert_and_privkey_ok,
248 leaf_cert_and_privkey_mismatch,
251 // check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer|
252 // and the private key in |privkey| are suitable and coherent. It returns
253 // |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is
254 // found. If the certificate and private key are valid, but incoherent, it
255 // returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns
256 // |leaf_cert_and_privkey_ok|.
257 static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey(
258 CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) {
260 CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs);
261 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
263 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
264 return leaf_cert_and_privkey_error;
267 if (!ssl_is_key_type_supported(pubkey->type)) {
268 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
269 return leaf_cert_and_privkey_error;
272 // An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
273 // certificates, so sanity-check the key usage extension.
274 if (pubkey->type == EVP_PKEY_EC &&
275 !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
276 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
277 return leaf_cert_and_privkey_error;
280 if (privkey != NULL &&
281 // Sanity-check that the private key and the certificate match.
282 !ssl_compare_public_and_private_key(pubkey.get(), privkey)) {
284 return leaf_cert_and_privkey_mismatch;
287 return leaf_cert_and_privkey_ok;
290 static int cert_set_chain_and_key(
291 CERT *cert, CRYPTO_BUFFER *const *certs, size_t num_certs,
292 EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) {
293 if (num_certs == 0 ||
294 (privkey == NULL && privkey_method == NULL)) {
295 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
299 if (privkey != NULL && privkey_method != NULL) {
300 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
304 switch (check_leaf_cert_and_privkey(certs[0], privkey)) {
305 case leaf_cert_and_privkey_error:
307 case leaf_cert_and_privkey_mismatch:
308 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
310 case leaf_cert_and_privkey_ok:
314 STACK_OF(CRYPTO_BUFFER) *certs_sk = sk_CRYPTO_BUFFER_new_null();
315 if (certs_sk == NULL) {
319 for (size_t i = 0; i < num_certs; i++) {
320 if (!sk_CRYPTO_BUFFER_push(certs_sk, certs[i])) {
321 sk_CRYPTO_BUFFER_pop_free(certs_sk, CRYPTO_BUFFER_free);
324 CRYPTO_BUFFER_up_ref(certs[i]);
327 EVP_PKEY_free(cert->privatekey);
328 cert->privatekey = privkey;
329 if (privkey != NULL) {
330 EVP_PKEY_up_ref(privkey);
332 cert->key_method = privkey_method;
334 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
335 cert->chain = certs_sk;
340 int ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) {
341 switch (check_leaf_cert_and_privkey(buffer.get(), cert->privatekey)) {
342 case leaf_cert_and_privkey_error:
344 case leaf_cert_and_privkey_mismatch:
345 // don't fail for a cert/key mismatch, just free current private key
346 // (when switching to a different cert & key, first this function should
347 // be used, then |ssl_set_pkey|.
348 EVP_PKEY_free(cert->privatekey);
349 cert->privatekey = NULL;
351 case leaf_cert_and_privkey_ok:
355 cert->x509_method->cert_flush_cached_leaf(cert);
357 if (cert->chain != NULL) {
358 CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0));
359 sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer.release());
363 cert->chain = sk_CRYPTO_BUFFER_new_null();
364 if (cert->chain == NULL) {
368 if (!PushToStack(cert->chain, std::move(buffer))) {
369 sk_CRYPTO_BUFFER_free(cert->chain);
377 int ssl_has_certificate(const SSL *ssl) {
378 return ssl->cert->chain != NULL &&
379 sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
380 ssl_has_private_key(ssl);
383 bool ssl_parse_cert_chain(uint8_t *out_alert,
384 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
385 UniquePtr<EVP_PKEY> *out_pubkey,
386 uint8_t *out_leaf_sha256, CBS *cbs,
387 CRYPTO_BUFFER_POOL *pool) {
391 CBS certificate_list;
392 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
393 *out_alert = SSL_AD_DECODE_ERROR;
394 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
398 if (CBS_len(&certificate_list) == 0) {
402 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null());
404 *out_alert = SSL_AD_INTERNAL_ERROR;
405 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
409 UniquePtr<EVP_PKEY> pubkey;
410 while (CBS_len(&certificate_list) > 0) {
412 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
413 CBS_len(&certificate) == 0) {
414 *out_alert = SSL_AD_DECODE_ERROR;
415 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
419 if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) {
420 pubkey = ssl_cert_parse_pubkey(&certificate);
422 *out_alert = SSL_AD_DECODE_ERROR;
426 // Retain the hash of the leaf certificate if requested.
427 if (out_leaf_sha256 != NULL) {
428 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
432 UniquePtr<CRYPTO_BUFFER> buf(
433 CRYPTO_BUFFER_new_from_CBS(&certificate, pool));
435 !PushToStack(chain.get(), std::move(buf))) {
436 *out_alert = SSL_AD_INTERNAL_ERROR;
437 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
442 *out_chain = std::move(chain);
443 *out_pubkey = std::move(pubkey);
447 int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
448 if (!ssl_has_certificate(ssl)) {
449 return CBB_add_u24(cbb, 0);
453 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
454 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
458 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
459 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
460 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
462 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
463 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
464 CRYPTO_BUFFER_len(buffer)) ||
465 !CBB_flush(&certs)) {
466 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
471 return CBB_flush(cbb);
474 // ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
475 // positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
476 // subjectPublicKeyInfo.
477 static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
478 /* From RFC 5280, section 4.1
479 * Certificate ::= SEQUENCE {
480 * tbsCertificate TBSCertificate,
481 * signatureAlgorithm AlgorithmIdentifier,
482 * signatureValue BIT STRING }
484 * TBSCertificate ::= SEQUENCE {
485 * version [0] EXPLICIT Version DEFAULT v1,
486 * serialNumber CertificateSerialNumber,
487 * signature AlgorithmIdentifier,
491 * subjectPublicKeyInfo SubjectPublicKeyInfo,
496 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
497 CBS_len(&buf) != 0 ||
498 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
500 !CBS_get_optional_asn1(
501 out_tbs_cert, NULL, NULL,
502 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
504 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
505 // signature algorithm
506 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
508 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
510 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
512 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
519 UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) {
520 CBS buf = *in, tbs_cert;
521 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
522 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
526 return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert));
529 int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
530 const EVP_PKEY *privkey) {
531 if (EVP_PKEY_is_opaque(privkey)) {
532 // We cannot check an opaque private key and have to trust that it
539 switch (EVP_PKEY_cmp(pubkey, privkey)) {
544 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
547 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
550 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
560 int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
561 if (privkey == NULL) {
562 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
566 if (cert->chain == NULL ||
567 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
568 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
573 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
574 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
576 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
580 return ssl_compare_public_and_private_key(pubkey.get(), privkey);
583 int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
586 CBS tbs_cert, outer_extensions;
588 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
589 // subjectPublicKeyInfo
590 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
592 !CBS_get_optional_asn1(
593 &tbs_cert, NULL, NULL,
594 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
596 !CBS_get_optional_asn1(
597 &tbs_cert, NULL, NULL,
598 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
599 !CBS_get_optional_asn1(
600 &tbs_cert, &outer_extensions, &has_extensions,
601 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
602 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
606 if (!has_extensions) {
611 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
612 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
616 while (CBS_len(&extensions) > 0) {
617 CBS extension, oid, contents;
618 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
619 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
620 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
621 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
622 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
623 CBS_len(&extension) != 0) {
624 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
628 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
629 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
630 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
636 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
637 CBS_len(&contents) != 0) {
638 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
642 // This is the KeyUsage extension. See
643 // https://tools.ietf.org/html/rfc5280#section-4.2.1.3
644 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
645 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
649 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
650 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
657 // No KeyUsage extension found.
661 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
664 CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
666 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
668 *out_alert = SSL_AD_INTERNAL_ERROR;
669 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
674 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
675 *out_alert = SSL_AD_DECODE_ERROR;
676 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
680 while (CBS_len(&child) > 0) {
681 CBS distinguished_name;
682 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
683 *out_alert = SSL_AD_DECODE_ERROR;
684 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
688 UniquePtr<CRYPTO_BUFFER> buffer(
689 CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool));
691 !PushToStack(ret.get(), std::move(buffer))) {
692 *out_alert = SSL_AD_INTERNAL_ERROR;
693 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
698 if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) {
699 *out_alert = SSL_AD_INTERNAL_ERROR;
700 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
707 bool ssl_has_client_CAs(SSL *ssl) {
708 STACK_OF(CRYPTO_BUFFER) *names = ssl->client_CA;
710 names = ssl->ctx->client_CA;
715 return sk_CRYPTO_BUFFER_num(names) > 0;
718 int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
720 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
724 STACK_OF(CRYPTO_BUFFER) *names = ssl->client_CA;
726 names = ssl->ctx->client_CA;
729 return CBB_flush(cbb);
732 for (const CRYPTO_BUFFER *name : names) {
733 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
734 !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
735 CRYPTO_BUFFER_len(name))) {
740 return CBB_flush(cbb);
743 int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
744 const CRYPTO_BUFFER *leaf) {
745 SSL *const ssl = hs->ssl;
746 assert(ssl_protocol_version(ssl) < TLS1_3_VERSION);
748 // Check the certificate's type matches the cipher.
749 if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
750 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
754 // Check key usages for all key types but RSA. This is needed to distinguish
755 // ECDH certificates, which we do not support, from ECDSA certificates. In
756 // principle, we should check RSA key usages based on cipher, but this breaks
757 // buggy antivirus deployments. Other key types are always used for signing.
759 // TODO(davidben): Get more recent data on RSA key usages.
760 if (EVP_PKEY_id(pkey) != EVP_PKEY_RSA) {
762 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
763 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
768 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
769 // Check the key's group and point format are acceptable.
770 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
772 if (!ssl_nid_to_group_id(
773 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
774 !tls1_check_group_id(ssl, group_id) ||
775 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
776 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
784 int ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
785 SSL *const ssl = hs->ssl;
786 if (!ssl_has_certificate(ssl)) {
791 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(ssl)) {
796 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0), &leaf);
798 hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
799 return hs->local_pubkey != NULL;
804 using namespace bssl;
806 int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
807 size_t num_certs, EVP_PKEY *privkey,
808 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
809 return cert_set_chain_and_key(ssl->cert, certs, num_certs, privkey,
813 int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
814 size_t num_certs, EVP_PKEY *privkey,
815 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
816 return cert_set_chain_and_key(ctx->cert, certs, num_certs, privkey,
820 int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
821 const uint8_t *der) {
822 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
827 return ssl_set_cert(ctx->cert, std::move(buffer));
830 int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
831 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
836 return ssl_set_cert(ssl->cert, std::move(buffer));
839 void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
841 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
844 void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
845 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
848 STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
849 SSL_SESSION *session = SSL_get_session(ssl);
850 if (session == NULL) {
854 return session->certs;
857 STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
858 if (ssl->s3->hs == NULL) {
861 return ssl->s3->hs->ca_names.get();
864 static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
867 CBS_init(&sct_list, list, list_len);
868 if (!ssl_is_sct_list_valid(&sct_list)) {
869 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
873 CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
874 cert->signed_cert_timestamp_list =
875 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL);
876 return cert->signed_cert_timestamp_list != NULL;
879 int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
881 return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
884 int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
886 return set_signed_cert_timestamp_list(ssl->cert, list, list_len);
889 int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
890 size_t response_len) {
891 CRYPTO_BUFFER_free(ctx->cert->ocsp_response);
892 ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
893 return ctx->cert->ocsp_response != NULL;
896 int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
897 size_t response_len) {
898 CRYPTO_BUFFER_free(ssl->cert->ocsp_response);
899 ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
900 return ssl->cert->ocsp_response != NULL;
903 void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) {
904 ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
905 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
906 ctx->client_CA = name_list;
909 void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) {
910 ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl);
911 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
912 ssl->client_CA = name_list;