blob: 68e010ad6f0f6d51ff27fc89a22caaba1f9ae4c1 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
David Benjamin443a1f62015-09-04 15:05:05 -0400115#include <openssl/ssl.h>
116
Steven Valdezbf5aa842016-07-15 07:07:40 -0400117#include <assert.h>
Steven Valdeza833c352016-11-01 13:39:36 -0400118#include <limits.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120
David Benjamin31b0c9b2017-07-20 14:49:15 -0400121#include <utility>
122
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/bn.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800124#include <openssl/bytestring.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800125#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/err.h>
127#include <openssl/mem.h>
David Benjamin5c900c82016-07-13 23:03:26 -0400128#include <openssl/sha.h>
David Benjamin680ca962015-06-18 12:37:23 -0400129#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley0da323a2015-05-15 12:49:30 -0700131#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400132#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700133
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700135BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400136
David Benjamine325c3f2018-04-12 16:11:15 -0400137CERT::CERT(const SSL_X509_METHOD *x509_method_arg)
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700138 : x509_method(x509_method_arg) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700139
David Benjamine325c3f2018-04-12 16:11:15 -0400140CERT::~CERT() {
141 ssl_cert_clear_certs(this);
142 x509_method->cert_free(this);
Adam Langleyfcf25832014-12-18 17:42:32 -0800143}
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langley3a2b47a2017-01-24 13:59:42 -0800145static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
146 CRYPTO_BUFFER_up_ref(buffer);
147 return buffer;
148}
149
David Benjamine325c3f2018-04-12 16:11:15 -0400150UniquePtr<CERT> ssl_cert_dup(CERT *cert) {
151 UniquePtr<CERT> ret = MakeUnique<CERT>(cert->x509_method);
152 if (!ret) {
153 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 }
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamine325c3f2018-04-12 16:11:15 -0400156 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 }
Adam Langley95c29f32014-06-20 12:00:00 -0700163
David Benjamin2908dd12018-06-29 17:46:42 -0400164 ret->privatekey = UpRef(cert->privatekey);
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030165 ret->key_method = cert->key_method;
166
David Benjamine325c3f2018-04-12 16:11:15 -0400167 if (!ret->sigalgs.CopyFrom(cert->sigalgs)) {
168 return nullptr;
David Benjamin0fc37ef2016-08-17 15:29:46 -0400169 }
David Benjamin0fc37ef2016-08-17 15:29:46 -0400170
Adam Langleyfcf25832014-12-18 17:42:32 -0800171 ret->cert_cb = cert->cert_cb;
172 ret->cert_cb_arg = cert->cert_cb_arg;
173
David Benjamine325c3f2018-04-12 16:11:15 -0400174 ret->x509_method->cert_dup(ret.get(), cert);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800175
David Benjamin2908dd12018-06-29 17:46:42 -0400176 ret->signed_cert_timestamp_list = UpRef(cert->signed_cert_timestamp_list);
177 ret->ocsp_response = UpRef(cert->ocsp_response);
David Benjamin83a32122017-02-14 18:34:54 -0500178
David Benjamin5960a902017-02-14 20:07:11 -0500179 ret->sid_ctx_length = cert->sid_ctx_length;
180 OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
181
Christopher Patton6c1b3762018-07-17 12:49:41 -0700182 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800193}
Adam Langley95c29f32014-06-20 12:00:00 -0700194
David Benjaminc11ea9422017-08-29 16:33:21 -0400195// Free up and clear all certificates and chains
David Benjamind1d80782015-07-05 11:54:09 -0400196void ssl_cert_clear_certs(CERT *cert) {
197 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 return;
199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langley3509dac2017-02-01 11:59:18 -0800201 cert->x509_method->cert_clear(cert);
Adam Langleye1e78132017-01-31 15:24:31 -0800202
David Benjamine325c3f2018-04-12 16:11:15 -0400203 cert->chain.reset();
204 cert->privatekey.reset();
205 cert->key_method = nullptr;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700206
207 cert->dc.reset();
208 cert->dc_privatekey.reset();
209 cert->dc_key_method = nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800210}
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin27e377e2017-07-31 19:09:42 -0400212static void ssl_cert_set_cert_cb(CERT *cert, int (*cb)(SSL *ssl, void *arg),
David Benjamina2bda9f2016-12-03 23:29:05 -0500213 void *arg) {
David Benjamin27e377e2017-07-31 19:09:42 -0400214 cert->cert_cb = cb;
215 cert->cert_cb_arg = arg;
Adam Langleyfcf25832014-12-18 17:42:32 -0800216}
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langleyd04ca952017-02-28 11:26:51 -0800218enum 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
David Benjaminc11ea9422017-08-29 16:33:21 -0400224// 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|.
Adam Langleyd04ca952017-02-28 11:26:51 -0800230static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey(
231 CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) {
Adam Langley52940c42017-02-01 12:40:31 -0800232 CBS cert_cbs;
Adam Langleyd04ca952017-02-28 11:26:51 -0800233 CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400234 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
235 if (!pubkey) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800236 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400237 return leaf_cert_and_privkey_error;
Adam Langley52940c42017-02-01 12:40:31 -0800238 }
239
240 if (!ssl_is_key_type_supported(pubkey->type)) {
241 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400242 return leaf_cert_and_privkey_error;
Adam Langley52940c42017-02-01 12:40:31 -0800243 }
244
David Benjaminc11ea9422017-08-29 16:33:21 -0400245 // An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
246 // certificates, so sanity-check the key usage extension.
Adam Langley52940c42017-02-01 12:40:31 -0800247 if (pubkey->type == EVP_PKEY_EC &&
Jesse Seloverd7266ec2019-01-30 16:06:10 -0500248 !ssl_cert_check_key_usage(&cert_cbs, key_usage_digital_signature)) {
Adam Langley52940c42017-02-01 12:40:31 -0800249 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400250 return leaf_cert_and_privkey_error;
Adam Langleyd04ca952017-02-28 11:26:51 -0800251 }
252
253 if (privkey != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400254 // Sanity-check that the private key and the certificate match.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400255 !ssl_compare_public_and_private_key(pubkey.get(), privkey)) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800256 ERR_clear_error();
David Benjamin31b0c9b2017-07-20 14:49:15 -0400257 return leaf_cert_and_privkey_mismatch;
Adam Langleyd04ca952017-02-28 11:26:51 -0800258 }
259
David Benjamin31b0c9b2017-07-20 14:49:15 -0400260 return leaf_cert_and_privkey_ok;
Adam Langleyd04ca952017-02-28 11:26:51 -0800261}
262
263static 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);
Adam Langley52940c42017-02-01 12:40:31 -0800269 return 0;
270 }
271
Adam Langleyd04ca952017-02-28 11:26:51 -0800272 if (privkey != NULL && privkey_method != NULL) {
273 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
274 return 0;
Adam Langley52940c42017-02-01 12:40:31 -0800275 }
276
Adam Langleyd04ca952017-02-28 11:26:51 -0800277 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
David Benjamine325c3f2018-04-12 16:11:15 -0400287 UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs_sk(sk_CRYPTO_BUFFER_new_null());
288 if (!certs_sk) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800289 return 0;
290 }
291
292 for (size_t i = 0; i < num_certs; i++) {
David Benjamin2908dd12018-06-29 17:46:42 -0400293 if (!PushToStack(certs_sk.get(), UpRef(certs[i]))) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800294 return 0;
295 }
Adam Langleyd04ca952017-02-28 11:26:51 -0800296 }
297
David Benjamin2908dd12018-06-29 17:46:42 -0400298 cert->privatekey = UpRef(privkey);
Adam Langleyd04ca952017-02-28 11:26:51 -0800299 cert->key_method = privkey_method;
300
David Benjamine325c3f2018-04-12 16:11:15 -0400301 cert->chain = std::move(certs_sk);
Adam Langleyd04ca952017-02-28 11:26:51 -0800302 return 1;
303}
304
David Benjamin8525ff32018-09-05 18:44:15 -0500305bool ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) {
David Benjamine325c3f2018-04-12 16:11:15 -0400306 switch (check_leaf_cert_and_privkey(buffer.get(), cert->privatekey.get())) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800307 case leaf_cert_and_privkey_error:
David Benjamin8525ff32018-09-05 18:44:15 -0500308 return false;
Adam Langleyd04ca952017-02-28 11:26:51 -0800309 case leaf_cert_and_privkey_mismatch:
David Benjaminc11ea9422017-08-29 16:33:21 -0400310 // 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|.
David Benjamine325c3f2018-04-12 16:11:15 -0400313 cert->privatekey.reset();
Adam Langleyd04ca952017-02-28 11:26:51 -0800314 break;
315 case leaf_cert_and_privkey_ok:
316 break;
317 }
Adam Langley52940c42017-02-01 12:40:31 -0800318
319 cert->x509_method->cert_flush_cached_leaf(cert);
320
David Benjamine325c3f2018-04-12 16:11:15 -0400321 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());
David Benjamin8525ff32018-09-05 18:44:15 -0500324 return true;
Adam Langley52940c42017-02-01 12:40:31 -0800325 }
326
David Benjamine325c3f2018-04-12 16:11:15 -0400327 cert->chain.reset(sk_CRYPTO_BUFFER_new_null());
328 if (cert->chain == nullptr) {
David Benjamin8525ff32018-09-05 18:44:15 -0500329 return false;
Adam Langley52940c42017-02-01 12:40:31 -0800330 }
331
David Benjamine325c3f2018-04-12 16:11:15 -0400332 if (!PushToStack(cert->chain.get(), std::move(buffer))) {
333 cert->chain.reset();
David Benjamin8525ff32018-09-05 18:44:15 -0500334 return false;
Adam Langley52940c42017-02-01 12:40:31 -0800335 }
Adam Langley52940c42017-02-01 12:40:31 -0800336
David Benjamin8525ff32018-09-05 18:44:15 -0500337 return true;
Adam Langley52940c42017-02-01 12:40:31 -0800338}
339
Christopher Patton9cde8482018-07-17 11:36:36 -0700340bool 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);
David Benjamin32a66d52016-07-13 22:03:11 -0400344}
345
David Benjamine664a532017-07-20 20:19:36 -0400346bool 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();
David Benjamin5c900c82016-07-13 23:03:26 -0400353
David Benjamin5c900c82016-07-13 23:03:26 -0400354 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);
David Benjamine664a532017-07-20 20:19:36 -0400358 return false;
David Benjamin5c900c82016-07-13 23:03:26 -0400359 }
360
David Benjamine664a532017-07-20 20:19:36 -0400361 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 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
369 return false;
370 }
371
372 UniquePtr<EVP_PKEY> pubkey;
David Benjamin5c900c82016-07-13 23:03:26 -0400373 while (CBS_len(&certificate_list) > 0) {
374 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800375 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
376 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400377 *out_alert = SSL_AD_DECODE_ERROR;
378 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
David Benjamine664a532017-07-20 20:19:36 -0400379 return false;
David Benjamin5c900c82016-07-13 23:03:26 -0400380 }
381
David Benjamine664a532017-07-20 20:19:36 -0400382 if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400383 pubkey = ssl_cert_parse_pubkey(&certificate);
384 if (!pubkey) {
Adam Langley7dccc712017-02-08 08:51:25 -0800385 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400386 return false;
Adam Langleyd5157222016-12-12 11:37:43 -0800387 }
388
David Benjaminc11ea9422017-08-29 16:33:21 -0400389 // Retain the hash of the leaf certificate if requested.
Adam Langleyd5157222016-12-12 11:37:43 -0800390 if (out_leaf_sha256 != NULL) {
391 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
392 }
David Benjamin5c900c82016-07-13 23:03:26 -0400393 }
394
David Benjaminee910bf2017-07-25 22:36:00 -0400395 UniquePtr<CRYPTO_BUFFER> buf(
396 CRYPTO_BUFFER_new_from_CBS(&certificate, pool));
397 if (!buf ||
398 !PushToStack(chain.get(), std::move(buf))) {
David Benjamin5c900c82016-07-13 23:03:26 -0400399 *out_alert = SSL_AD_INTERNAL_ERROR;
400 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamine664a532017-07-20 20:19:36 -0400401 return false;
David Benjamin5c900c82016-07-13 23:03:26 -0400402 }
David Benjamin5c900c82016-07-13 23:03:26 -0400403 }
404
David Benjamine664a532017-07-20 20:19:36 -0400405 *out_chain = std::move(chain);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400406 *out_pubkey = std::move(pubkey);
David Benjamine664a532017-07-20 20:19:36 -0400407 return true;
David Benjamin5c900c82016-07-13 23:03:26 -0400408}
409
David Benjamin8525ff32018-09-05 18:44:15 -0500410bool ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb) {
Christopher Patton9cde8482018-07-17 11:36:36 -0700411 if (!ssl_has_certificate(hs)) {
David Benjamin32a66d52016-07-13 22:03:11 -0400412 return CBB_add_u24(cbb, 0);
413 }
414
Adam Langley3a2b47a2017-01-24 13:59:42 -0800415 CBB certs;
416 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
David Benjamine64d2c72017-07-12 16:31:08 -0400417 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8525ff32018-09-05 18:44:15 -0500418 return false;
David Benjamin75836432016-06-17 18:48:29 -0400419 }
420
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700421 STACK_OF(CRYPTO_BUFFER) *chain = hs->config->cert->chain.get();
Adam Langley3a2b47a2017-01-24 13:59:42 -0800422 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
423 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
424 CBB child;
425 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
426 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
427 CRYPTO_BUFFER_len(buffer)) ||
428 !CBB_flush(&certs)) {
David Benjamine64d2c72017-07-12 16:31:08 -0400429 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin8525ff32018-09-05 18:44:15 -0500430 return false;
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 }
432 }
433
David Benjamin75836432016-06-17 18:48:29 -0400434 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800435}
Adam Langley95c29f32014-06-20 12:00:00 -0700436
David Benjaminc11ea9422017-08-29 16:33:21 -0400437// ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
438// positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
439// subjectPublicKeyInfo.
David Benjamin8525ff32018-09-05 18:44:15 -0500440static bool ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800441 /* From RFC 5280, section 4.1
442 * Certificate ::= SEQUENCE {
443 * tbsCertificate TBSCertificate,
444 * signatureAlgorithm AlgorithmIdentifier,
445 * signatureValue BIT STRING }
446
447 * TBSCertificate ::= SEQUENCE {
448 * version [0] EXPLICIT Version DEFAULT v1,
449 * serialNumber CertificateSerialNumber,
450 * signature AlgorithmIdentifier,
451 * issuer Name,
452 * validity Validity,
453 * subject Name,
454 * subjectPublicKeyInfo SubjectPublicKeyInfo,
455 * ... } */
456 CBS buf = *in;
457
Adam Langley05672202016-12-13 12:05:49 -0800458 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800459 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
460 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800461 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400462 // version
Adam Langleyd5157222016-12-12 11:37:43 -0800463 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800464 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800465 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400466 // serialNumber
Adam Langley05672202016-12-13 12:05:49 -0800467 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400468 // signature algorithm
Adam Langley05672202016-12-13 12:05:49 -0800469 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400470 // issuer
Adam Langley05672202016-12-13 12:05:49 -0800471 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400472 // validity
Adam Langley05672202016-12-13 12:05:49 -0800473 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400474 // subject
Adam Langley05672202016-12-13 12:05:49 -0800475 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500476 return false;
Adam Langley05672202016-12-13 12:05:49 -0800477 }
478
David Benjamin8525ff32018-09-05 18:44:15 -0500479 return true;
Adam Langley05672202016-12-13 12:05:49 -0800480}
481
David Benjamin31b0c9b2017-07-20 14:49:15 -0400482UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500483 CBS buf = *in, tbs_cert;
484 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800485 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400486 return nullptr;
Adam Langleyd5157222016-12-12 11:37:43 -0800487 }
488
David Benjamin31b0c9b2017-07-20 14:49:15 -0400489 return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert));
Adam Langleyd5157222016-12-12 11:37:43 -0800490}
491
David Benjamin8525ff32018-09-05 18:44:15 -0500492bool ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
493 const EVP_PKEY *privkey) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800494 if (EVP_PKEY_is_opaque(privkey)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400495 // We cannot check an opaque private key and have to trust that it
496 // matches.
David Benjamin8525ff32018-09-05 18:44:15 -0500497 return true;
Adam Langleyd04ca952017-02-28 11:26:51 -0800498 }
499
Adam Langley3a2b47a2017-01-24 13:59:42 -0800500 switch (EVP_PKEY_cmp(pubkey, privkey)) {
501 case 1:
David Benjamin8525ff32018-09-05 18:44:15 -0500502 return true;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800503 case 0:
504 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
David Benjamin8525ff32018-09-05 18:44:15 -0500505 return false;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800506 case -1:
507 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
David Benjamin8525ff32018-09-05 18:44:15 -0500508 return false;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800509 case -2:
510 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -0500511 return false;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800512 }
513
David Benjamin8525ff32018-09-05 18:44:15 -0500514 assert(0);
515 return false;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800516}
517
David Benjamin8525ff32018-09-05 18:44:15 -0500518bool ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
David Benjamine325c3f2018-04-12 16:11:15 -0400519 if (privkey == nullptr) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800520 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
David Benjamin8525ff32018-09-05 18:44:15 -0500521 return false;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800522 }
523
David Benjamine325c3f2018-04-12 16:11:15 -0400524 if (cert->chain == nullptr ||
525 sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800526 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
David Benjamin8525ff32018-09-05 18:44:15 -0500527 return false;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800528 }
529
Adam Langleyc26692c2017-01-25 09:34:42 -0800530 CBS cert_cbs;
David Benjamine325c3f2018-04-12 16:11:15 -0400531 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0),
532 &cert_cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400533 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
Adam Langleyc26692c2017-01-25 09:34:42 -0800534 if (!pubkey) {
535 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -0500536 return false;
Adam Langleyc26692c2017-01-25 09:34:42 -0800537 }
538
David Benjamin31b0c9b2017-07-20 14:49:15 -0400539 return ssl_compare_public_and_private_key(pubkey.get(), privkey);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800540}
541
Jesse Seloverd7266ec2019-01-30 16:06:10 -0500542bool ssl_cert_check_key_usage(const CBS *in, enum ssl_key_usage_t bit) {
Adam Langley05672202016-12-13 12:05:49 -0800543 CBS buf = *in;
544
545 CBS tbs_cert, outer_extensions;
546 int has_extensions;
547 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400548 // subjectPublicKeyInfo
Adam Langley05672202016-12-13 12:05:49 -0800549 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400550 // issuerUniqueID
David Benjamin2f3958a2021-04-16 11:55:23 -0400551 !CBS_get_optional_asn1(&tbs_cert, NULL, NULL,
552 CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400553 // subjectUniqueID
David Benjamin2f3958a2021-04-16 11:55:23 -0400554 !CBS_get_optional_asn1(&tbs_cert, NULL, NULL,
555 CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
Adam Langley05672202016-12-13 12:05:49 -0800556 !CBS_get_optional_asn1(
557 &tbs_cert, &outer_extensions, &has_extensions,
558 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
David Benjaminc64d1232017-10-04 18:14:28 -0400559 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin8525ff32018-09-05 18:44:15 -0500560 return false;
Adam Langley05672202016-12-13 12:05:49 -0800561 }
562
563 if (!has_extensions) {
David Benjamin8525ff32018-09-05 18:44:15 -0500564 return true;
Adam Langley05672202016-12-13 12:05:49 -0800565 }
566
567 CBS extensions;
568 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
David Benjaminc64d1232017-10-04 18:14:28 -0400569 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin8525ff32018-09-05 18:44:15 -0500570 return false;
Adam Langley05672202016-12-13 12:05:49 -0800571 }
572
573 while (CBS_len(&extensions) > 0) {
574 CBS extension, oid, contents;
575 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
576 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
577 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
578 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
579 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
580 CBS_len(&extension) != 0) {
David Benjaminc64d1232017-10-04 18:14:28 -0400581 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin8525ff32018-09-05 18:44:15 -0500582 return false;
Adam Langley05672202016-12-13 12:05:49 -0800583 }
584
585 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
586 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500587 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
588 0) {
Adam Langley05672202016-12-13 12:05:49 -0800589 continue;
590 }
591
592 CBS bit_string;
593 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
594 CBS_len(&contents) != 0) {
David Benjaminc64d1232017-10-04 18:14:28 -0400595 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin8525ff32018-09-05 18:44:15 -0500596 return false;
Adam Langley05672202016-12-13 12:05:49 -0800597 }
598
David Benjaminc11ea9422017-08-29 16:33:21 -0400599 // This is the KeyUsage extension. See
600 // https://tools.ietf.org/html/rfc5280#section-4.2.1.3
Adam Langley05672202016-12-13 12:05:49 -0800601 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
David Benjaminc64d1232017-10-04 18:14:28 -0400602 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin8525ff32018-09-05 18:44:15 -0500603 return false;
Adam Langley05672202016-12-13 12:05:49 -0800604 }
605
Jesse Seloverd7266ec2019-01-30 16:06:10 -0500606 if (!CBS_asn1_bitstring_has_bit(&bit_string, bit)) {
607 OPENSSL_PUT_ERROR(SSL, SSL_R_KEY_USAGE_BIT_INCORRECT);
David Benjamin8525ff32018-09-05 18:44:15 -0500608 return false;
Adam Langley05672202016-12-13 12:05:49 -0800609 }
610
David Benjamin8525ff32018-09-05 18:44:15 -0500611 return true;
Adam Langley05672202016-12-13 12:05:49 -0800612 }
613
David Benjaminc11ea9422017-08-29 16:33:21 -0400614 // No KeyUsage extension found.
David Benjamin8525ff32018-09-05 18:44:15 -0500615 return true;
Adam Langley05672202016-12-13 12:05:49 -0800616}
617
David Benjamin31b0c9b2017-07-20 14:49:15 -0400618UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
619 uint8_t *out_alert,
620 CBS *cbs) {
Adam Langley34b4c822017-02-02 10:57:17 -0800621 CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
622
David Benjamin31b0c9b2017-07-20 14:49:15 -0400623 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
624 if (!ret) {
David Benjamine0332e82016-07-13 22:40:36 -0400625 *out_alert = SSL_AD_INTERNAL_ERROR;
626 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400627 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400628 }
629
630 CBS child;
631 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
632 *out_alert = SSL_AD_DECODE_ERROR;
633 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400634 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400635 }
636
637 while (CBS_len(&child) > 0) {
638 CBS distinguished_name;
639 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
640 *out_alert = SSL_AD_DECODE_ERROR;
641 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400642 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400643 }
644
David Benjaminee910bf2017-07-25 22:36:00 -0400645 UniquePtr<CRYPTO_BUFFER> buffer(
646 CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool));
647 if (!buffer ||
648 !PushToStack(ret.get(), std::move(buffer))) {
David Benjamine0332e82016-07-13 22:40:36 -0400649 *out_alert = SSL_AD_INTERNAL_ERROR;
650 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400651 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400652 }
David Benjamine0332e82016-07-13 22:40:36 -0400653 }
654
David Benjamin31b0c9b2017-07-20 14:49:15 -0400655 if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) {
David Benjamin418cdc42018-05-09 13:56:09 -0400656 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley0bdef092017-02-23 15:02:58 -0800657 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400658 return nullptr;
Adam Langley0bdef092017-02-23 15:02:58 -0800659 }
660
David Benjamine0332e82016-07-13 22:40:36 -0400661 return ret;
David Benjamine0332e82016-07-13 22:40:36 -0400662}
663
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700664bool ssl_has_client_CAs(const SSL_CONFIG *cfg) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400665 const STACK_OF(CRYPTO_BUFFER) *names = cfg->client_CA.get();
666 if (names == nullptr) {
667 names = cfg->ssl->ctx->client_CA.get();
Steven Valdezcd8470f2017-10-11 12:29:36 -0400668 }
David Benjamin0ce090a2018-07-02 20:24:40 -0400669 if (names == nullptr) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400670 return false;
671 }
672 return sk_CRYPTO_BUFFER_num(names) > 0;
673}
674
David Benjamin8525ff32018-09-05 18:44:15 -0500675bool ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400676 CBB child, name_cbb;
677 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500678 return false;
David Benjamin32a66d52016-07-13 22:03:11 -0400679 }
680
David Benjamin0ce090a2018-07-02 20:24:40 -0400681 const STACK_OF(CRYPTO_BUFFER) *names = hs->config->client_CA.get();
Adam Langley34b4c822017-02-02 10:57:17 -0800682 if (names == NULL) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400683 names = hs->ssl->ctx->client_CA.get();
Adam Langley34b4c822017-02-02 10:57:17 -0800684 }
685 if (names == NULL) {
David Benjamin32a66d52016-07-13 22:03:11 -0400686 return CBB_flush(cbb);
687 }
688
David Benjaminec783832017-07-25 23:23:03 -0400689 for (const CRYPTO_BUFFER *name : names) {
David Benjamin32a66d52016-07-13 22:03:11 -0400690 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
Adam Langley34b4c822017-02-02 10:57:17 -0800691 !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
692 CRYPTO_BUFFER_len(name))) {
David Benjamin8525ff32018-09-05 18:44:15 -0500693 return false;
David Benjamin32a66d52016-07-13 22:03:11 -0400694 }
695 }
696
697 return CBB_flush(cbb);
698}
699
David Benjamin8525ff32018-09-05 18:44:15 -0500700bool ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
701 const CRYPTO_BUFFER *leaf) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700702 assert(ssl_protocol_version(hs->ssl) < TLS1_3_VERSION);
David Benjamin938fa7c2016-10-07 00:27:05 -0400703
David Benjaminc11ea9422017-08-29 16:33:21 -0400704 // Check the certificate's type matches the cipher.
David Benjamin69522112017-03-28 15:38:29 -0500705 if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400706 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
David Benjamin8525ff32018-09-05 18:44:15 -0500707 return false;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400708 }
709
David Benjamin69522112017-03-28 15:38:29 -0500710 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400711 // Check the key's group and point format are acceptable.
David Benjamin69522112017-03-28 15:38:29 -0500712 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
David Benjamin938fa7c2016-10-07 00:27:05 -0400713 uint16_t group_id;
714 if (!ssl_nid_to_group_id(
715 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700716 !tls1_check_group_id(hs, group_id) ||
David Benjamin938fa7c2016-10-07 00:27:05 -0400717 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400718 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
David Benjamin8525ff32018-09-05 18:44:15 -0500719 return false;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400720 }
721 }
722
David Benjamin8525ff32018-09-05 18:44:15 -0500723 return true;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400724}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500725
David Benjamin8525ff32018-09-05 18:44:15 -0500726bool ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
David Benjamina232a712017-03-30 15:51:53 -0500727 SSL *const ssl = hs->ssl;
Christopher Patton9cde8482018-07-17 11:36:36 -0700728 if (!ssl_has_certificate(hs)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400729 // Nothing to do.
David Benjamin8525ff32018-09-05 18:44:15 -0500730 return true;
David Benjamina232a712017-03-30 15:51:53 -0500731 }
732
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700733 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(hs)) {
David Benjamin8525ff32018-09-05 18:44:15 -0500734 return false;
David Benjamina232a712017-03-30 15:51:53 -0500735 }
736
737 CBS leaf;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700738 CRYPTO_BUFFER_init_CBS(
739 sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0), &leaf);
David Benjamina232a712017-03-30 15:51:53 -0500740
Christopher Patton6c1b3762018-07-17 12:49:41 -0700741 if (ssl_signing_with_dc(hs)) {
742 hs->local_pubkey = UpRef(hs->config->cert->dc->pkey);
743 } else {
744 hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
745 }
David Benjamina232a712017-03-30 15:51:53 -0500746 return hs->local_pubkey != NULL;
747}
748
Christopher Patton6c1b3762018-07-17 12:49:41 -0700749
750// Delegated credentials.
751
752DC::DC() = default;
753DC::~DC() = default;
754
755UniquePtr<DC> DC::Dup() {
756 bssl::UniquePtr<DC> ret = MakeUnique<DC>();
757 if (!ret) {
758 return nullptr;
759 }
760
761 ret->raw = UpRef(raw);
762 ret->expected_cert_verify_algorithm = expected_cert_verify_algorithm;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700763 ret->pkey = UpRef(pkey);
764 return ret;
765}
766
767// static
768UniquePtr<DC> DC::Parse(CRYPTO_BUFFER *in, uint8_t *out_alert) {
769 UniquePtr<DC> dc = MakeUnique<DC>();
770 if (!dc) {
771 *out_alert = SSL_AD_INTERNAL_ERROR;
772 return nullptr;
773 }
774
775 dc->raw = UpRef(in);
776
777 CBS pubkey, deleg, sig;
778 uint32_t valid_time;
779 uint16_t algorithm;
780 CRYPTO_BUFFER_init_CBS(dc->raw.get(), &deleg);
781 if (!CBS_get_u32(&deleg, &valid_time) ||
782 !CBS_get_u16(&deleg, &dc->expected_cert_verify_algorithm) ||
Christopher Patton6c1b3762018-07-17 12:49:41 -0700783 !CBS_get_u24_length_prefixed(&deleg, &pubkey) ||
784 !CBS_get_u16(&deleg, &algorithm) ||
785 !CBS_get_u16_length_prefixed(&deleg, &sig) ||
786 CBS_len(&deleg) != 0) {
787 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
788 *out_alert = SSL_AD_DECODE_ERROR;
789 return nullptr;
790 }
791
792 dc->pkey.reset(EVP_parse_public_key(&pubkey));
793 if (dc->pkey == nullptr) {
794 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
795 *out_alert = SSL_AD_DECODE_ERROR;
796 return nullptr;
797 }
798
799 return dc;
800}
801
802// ssl_can_serve_dc returns true if the host has configured a DC that it can
803// serve in the handshake. Specifically, it checks that a DC has been
Watson Ladd3390fd82019-03-07 13:06:00 -0800804// configured and that the DC signature algorithm is supported by the peer.
Christopher Patton6c1b3762018-07-17 12:49:41 -0700805static bool ssl_can_serve_dc(const SSL_HANDSHAKE *hs) {
806 // Check that a DC has been configured.
807 const CERT *cert = hs->config->cert.get();
808 if (cert->dc == nullptr ||
809 cert->dc->raw == nullptr ||
810 (cert->dc_privatekey == nullptr && cert->dc_key_method == nullptr)) {
811 return false;
812 }
813
Watson Ladd2f213f62019-02-12 16:59:54 -0800814 // Check that 1.3 or higher has been negotiated.
Christopher Patton6c1b3762018-07-17 12:49:41 -0700815 const DC *dc = cert->dc.get();
816 assert(hs->ssl->s3->have_version);
Watson Ladd2f213f62019-02-12 16:59:54 -0800817 if (ssl_protocol_version(hs->ssl) < TLS1_3_VERSION) {
Christopher Patton6c1b3762018-07-17 12:49:41 -0700818 return false;
819 }
820
821 // Check that the DC signature algorithm is supported by the peer.
Watson Ladddcd6e442020-08-10 15:12:45 -0400822 Span<const uint16_t> peer_sigalgs = hs->peer_delegated_credential_sigalgs;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700823 for (uint16_t peer_sigalg : peer_sigalgs) {
824 if (dc->expected_cert_verify_algorithm == peer_sigalg) {
Watson Ladddcd6e442020-08-10 15:12:45 -0400825 return true;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700826 }
827 }
Watson Ladddcd6e442020-08-10 15:12:45 -0400828 return false;
Christopher Patton6c1b3762018-07-17 12:49:41 -0700829}
830
831bool ssl_signing_with_dc(const SSL_HANDSHAKE *hs) {
Watson Ladd2f213f62019-02-12 16:59:54 -0800832 // As of draft-ietf-tls-subcert-03, only the server may use delegated
Christopher Patton6c1b3762018-07-17 12:49:41 -0700833 // credentials to authenticate itself.
834 return hs->ssl->server &&
835 hs->delegated_credential_requested &&
836 ssl_can_serve_dc(hs);
837}
838
839static int cert_set_dc(CERT *cert, CRYPTO_BUFFER *const raw, EVP_PKEY *privkey,
840 const SSL_PRIVATE_KEY_METHOD *key_method) {
841 if (privkey == nullptr && key_method == nullptr) {
842 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
843 return 0;
844 }
845
846 if (privkey != nullptr && key_method != nullptr) {
847 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
848 return 0;
849 }
850
851 uint8_t alert;
852 UniquePtr<DC> dc = DC::Parse(raw, &alert);
853 if (dc == nullptr) {
854 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_DELEGATED_CREDENTIAL);
855 return 0;
856 }
857
858 if (privkey) {
859 // Check that the public and private keys match.
860 if (!ssl_compare_public_and_private_key(dc->pkey.get(), privkey)) {
861 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
862 return 0;
863 }
864 }
865
866 cert->dc = std::move(dc);
867 cert->dc_privatekey = UpRef(privkey);
868 cert->dc_key_method = key_method;
869
870 return 1;
871}
872
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700873BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400874
875using namespace bssl;
876
877int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
878 size_t num_certs, EVP_PKEY *privkey,
879 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700880 if (!ssl->config) {
881 return 0;
882 }
David Benjamin0ce090a2018-07-02 20:24:40 -0400883 return cert_set_chain_and_key(ssl->config->cert.get(), certs, num_certs,
884 privkey, privkey_method);
David Benjamin86e95b82017-07-18 16:34:25 -0400885}
886
887int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
888 size_t num_certs, EVP_PKEY *privkey,
889 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400890 return cert_set_chain_and_key(ctx->cert.get(), certs, num_certs, privkey,
David Benjamin86e95b82017-07-18 16:34:25 -0400891 privkey_method);
892}
893
Adam Langley964256d2020-03-19 11:57:12 -0700894const STACK_OF(CRYPTO_BUFFER)* SSL_CTX_get0_chain(const SSL_CTX *ctx) {
895 return ctx->cert->chain.get();
896}
897
David Benjamin86e95b82017-07-18 16:34:25 -0400898int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
899 const uint8_t *der) {
David Benjaminee910bf2017-07-25 22:36:00 -0400900 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
901 if (!buffer) {
David Benjamin86e95b82017-07-18 16:34:25 -0400902 return 0;
903 }
904
David Benjamin0ce090a2018-07-02 20:24:40 -0400905 return ssl_set_cert(ctx->cert.get(), std::move(buffer));
David Benjamin86e95b82017-07-18 16:34:25 -0400906}
907
908int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
David Benjaminee910bf2017-07-25 22:36:00 -0400909 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700910 if (!buffer || !ssl->config) {
David Benjamin86e95b82017-07-18 16:34:25 -0400911 return 0;
912 }
913
David Benjamin0ce090a2018-07-02 20:24:40 -0400914 return ssl_set_cert(ssl->config->cert.get(), std::move(buffer));
David Benjamin86e95b82017-07-18 16:34:25 -0400915}
916
917void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
918 void *arg) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400919 ssl_cert_set_cert_cb(ctx->cert.get(), cb, arg);
David Benjamin86e95b82017-07-18 16:34:25 -0400920}
921
922void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700923 if (!ssl->config) {
924 return;
925 }
David Benjamin0ce090a2018-07-02 20:24:40 -0400926 ssl_cert_set_cert_cb(ssl->config->cert.get(), cb, arg);
David Benjamin86e95b82017-07-18 16:34:25 -0400927}
928
David Benjamin5f001d12018-05-08 16:46:48 -0400929const STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
David Benjamin86e95b82017-07-18 16:34:25 -0400930 SSL_SESSION *session = SSL_get_session(ssl);
931 if (session == NULL) {
932 return NULL;
933 }
934
David Benjaminbfdd1a92018-06-29 16:26:38 -0400935 return session->certs.get();
David Benjamin86e95b82017-07-18 16:34:25 -0400936}
937
David Benjamin5f001d12018-05-08 16:46:48 -0400938const STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
David Benjamin86e95b82017-07-18 16:34:25 -0400939 if (ssl->s3->hs == NULL) {
940 return NULL;
941 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400942 return ssl->s3->hs->ca_names.get();
David Benjamin86e95b82017-07-18 16:34:25 -0400943}
944
David Benjamin83a32122017-02-14 18:34:54 -0500945static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
David Benjamine325c3f2018-04-12 16:11:15 -0400946 size_t list_len) {
David Benjamin83a32122017-02-14 18:34:54 -0500947 CBS sct_list;
948 CBS_init(&sct_list, list, list_len);
949 if (!ssl_is_sct_list_valid(&sct_list)) {
950 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
951 return 0;
952 }
953
David Benjamine325c3f2018-04-12 16:11:15 -0400954 cert->signed_cert_timestamp_list.reset(
955 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), nullptr));
956 return cert->signed_cert_timestamp_list != nullptr;
David Benjamin83a32122017-02-14 18:34:54 -0500957}
958
959int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
960 size_t list_len) {
David Benjamin0ce090a2018-07-02 20:24:40 -0400961 return set_signed_cert_timestamp_list(ctx->cert.get(), list, list_len);
David Benjamin83a32122017-02-14 18:34:54 -0500962}
963
964int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
965 size_t list_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700966 if (!ssl->config) {
967 return 0;
968 }
David Benjamin0ce090a2018-07-02 20:24:40 -0400969 return set_signed_cert_timestamp_list(ssl->config->cert.get(), list,
970 list_len);
David Benjamin83a32122017-02-14 18:34:54 -0500971}
972
973int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
974 size_t response_len) {
David Benjamine325c3f2018-04-12 16:11:15 -0400975 ctx->cert->ocsp_response.reset(
976 CRYPTO_BUFFER_new(response, response_len, nullptr));
977 return ctx->cert->ocsp_response != nullptr;
David Benjamin83a32122017-02-14 18:34:54 -0500978}
979
980int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
981 size_t response_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700982 if (!ssl->config) {
983 return 0;
984 }
985 ssl->config->cert->ocsp_response.reset(
David Benjamine325c3f2018-04-12 16:11:15 -0400986 CRYPTO_BUFFER_new(response, response_len, nullptr));
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700987 return ssl->config->cert->ocsp_response != nullptr;
David Benjamin83a32122017-02-14 18:34:54 -0500988}
David Benjamin71dfad42017-07-16 17:27:39 -0400989
990void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) {
991 ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
David Benjamin0ce090a2018-07-02 20:24:40 -0400992 ctx->client_CA.reset(name_list);
David Benjamin71dfad42017-07-16 17:27:39 -0400993}
994
995void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700996 if (!ssl->config) {
997 return;
998 }
David Benjamin50596f82018-07-02 19:47:27 -0400999 ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get());
David Benjamin0ce090a2018-07-02 20:24:40 -04001000 ssl->config->client_CA.reset(name_list);
David Benjamin71dfad42017-07-16 17:27:39 -04001001}
Christopher Patton6c1b3762018-07-17 12:49:41 -07001002
1003int SSL_set1_delegated_credential(SSL *ssl, CRYPTO_BUFFER *dc, EVP_PKEY *pkey,
1004 const SSL_PRIVATE_KEY_METHOD *key_method) {
1005 if (!ssl->config) {
1006 return 0;
1007 }
1008
1009 return cert_set_dc(ssl->config->cert.get(), dc, pkey, key_method);
1010}
Watson Ladd629f3212019-06-11 17:46:52 -07001011
1012int SSL_delegated_credential_used(const SSL *ssl) {
1013 return ssl->s3->delegated_credential_used;
1014}