blob: a089c40df4da0124e8f5f8878deac4923118c4e6 [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>
David Benjamin676d1e72014-07-08 14:34:10 -0400124#include <openssl/buf.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800125#include <openssl/bytestring.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800126#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700127#include <openssl/err.h>
128#include <openssl/mem.h>
David Benjamin5c900c82016-07-13 23:03:26 -0400129#include <openssl/sha.h>
David Benjamin680ca962015-06-18 12:37:23 -0400130#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langley0da323a2015-05-15 12:49:30 -0700132#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400133#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langley95c29f32014-06-20 12:00:00 -0700135
David Benjamin86e95b82017-07-18 16:34:25 -0400136namespace bssl {
137
David Benjamine325c3f2018-04-12 16:11:15 -0400138CERT::CERT(const SSL_X509_METHOD *x509_method_arg)
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700139 : x509_method(x509_method_arg) {}
Adam Langley95c29f32014-06-20 12:00:00 -0700140
David Benjamine325c3f2018-04-12 16:11:15 -0400141CERT::~CERT() {
142 ssl_cert_clear_certs(this);
143 x509_method->cert_free(this);
Adam Langleyfcf25832014-12-18 17:42:32 -0800144}
Adam Langley95c29f32014-06-20 12:00:00 -0700145
Adam Langley3a2b47a2017-01-24 13:59:42 -0800146static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
147 CRYPTO_BUFFER_up_ref(buffer);
148 return buffer;
149}
150
David Benjamine325c3f2018-04-12 16:11:15 -0400151UniquePtr<CERT> ssl_cert_dup(CERT *cert) {
152 UniquePtr<CERT> ret = MakeUnique<CERT>(cert->x509_method);
153 if (!ret) {
154 return nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 }
Adam Langley95c29f32014-06-20 12:00:00 -0700156
David Benjamine325c3f2018-04-12 16:11:15 -0400157 if (cert->chain) {
158 ret->chain.reset(sk_CRYPTO_BUFFER_deep_copy(
159 cert->chain.get(), buffer_up_ref, CRYPTO_BUFFER_free));
160 if (!ret->chain) {
161 return nullptr;
162 }
163 }
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamine325c3f2018-04-12 16:11:15 -0400165 if (cert->privatekey) {
166 EVP_PKEY_up_ref(cert->privatekey.get());
167 ret->privatekey.reset(cert->privatekey.get());
David Benjamind1d80782015-07-05 11:54:09 -0400168 }
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030170 ret->key_method = cert->key_method;
171
David Benjamine325c3f2018-04-12 16:11:15 -0400172 if (!ret->sigalgs.CopyFrom(cert->sigalgs)) {
173 return nullptr;
David Benjamin0fc37ef2016-08-17 15:29:46 -0400174 }
David Benjamin0fc37ef2016-08-17 15:29:46 -0400175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 ret->cert_cb = cert->cert_cb;
177 ret->cert_cb_arg = cert->cert_cb_arg;
178
David Benjamine325c3f2018-04-12 16:11:15 -0400179 ret->x509_method->cert_dup(ret.get(), cert);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800180
David Benjamine325c3f2018-04-12 16:11:15 -0400181 if (cert->signed_cert_timestamp_list) {
182 CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list.get());
183 ret->signed_cert_timestamp_list.reset(
184 cert->signed_cert_timestamp_list.get());
David Benjamin83a32122017-02-14 18:34:54 -0500185 }
186
David Benjamine325c3f2018-04-12 16:11:15 -0400187 if (cert->ocsp_response) {
188 CRYPTO_BUFFER_up_ref(cert->ocsp_response.get());
189 ret->ocsp_response.reset(cert->ocsp_response.get());
David Benjamin83a32122017-02-14 18:34:54 -0500190 }
191
David Benjamin5960a902017-02-14 20:07:11 -0500192 ret->sid_ctx_length = cert->sid_ctx_length;
193 OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
194
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800196}
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjaminc11ea9422017-08-29 16:33:21 -0400198// Free up and clear all certificates and chains
David Benjamind1d80782015-07-05 11:54:09 -0400199void ssl_cert_clear_certs(CERT *cert) {
200 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 return;
202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley3509dac2017-02-01 11:59:18 -0800204 cert->x509_method->cert_clear(cert);
Adam Langleye1e78132017-01-31 15:24:31 -0800205
David Benjamine325c3f2018-04-12 16:11:15 -0400206 cert->chain.reset();
207 cert->privatekey.reset();
208 cert->key_method = nullptr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800209}
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin27e377e2017-07-31 19:09:42 -0400211static void ssl_cert_set_cert_cb(CERT *cert, int (*cb)(SSL *ssl, void *arg),
David Benjamina2bda9f2016-12-03 23:29:05 -0500212 void *arg) {
David Benjamin27e377e2017-07-31 19:09:42 -0400213 cert->cert_cb = cb;
214 cert->cert_cb_arg = arg;
Adam Langleyfcf25832014-12-18 17:42:32 -0800215}
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langleyd04ca952017-02-28 11:26:51 -0800217enum leaf_cert_and_privkey_result_t {
218 leaf_cert_and_privkey_error,
219 leaf_cert_and_privkey_ok,
220 leaf_cert_and_privkey_mismatch,
221};
222
David Benjaminc11ea9422017-08-29 16:33:21 -0400223// check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer|
224// and the private key in |privkey| are suitable and coherent. It returns
225// |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is
226// found. If the certificate and private key are valid, but incoherent, it
227// returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns
228// |leaf_cert_and_privkey_ok|.
Adam Langleyd04ca952017-02-28 11:26:51 -0800229static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey(
230 CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) {
Adam Langley52940c42017-02-01 12:40:31 -0800231 CBS cert_cbs;
Adam Langleyd04ca952017-02-28 11:26:51 -0800232 CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400233 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
234 if (!pubkey) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800235 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400236 return leaf_cert_and_privkey_error;
Adam Langley52940c42017-02-01 12:40:31 -0800237 }
238
239 if (!ssl_is_key_type_supported(pubkey->type)) {
240 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400241 return leaf_cert_and_privkey_error;
Adam Langley52940c42017-02-01 12:40:31 -0800242 }
243
David Benjaminc11ea9422017-08-29 16:33:21 -0400244 // An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
245 // certificates, so sanity-check the key usage extension.
Adam Langley52940c42017-02-01 12:40:31 -0800246 if (pubkey->type == EVP_PKEY_EC &&
247 !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
248 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400249 return leaf_cert_and_privkey_error;
Adam Langleyd04ca952017-02-28 11:26:51 -0800250 }
251
252 if (privkey != NULL &&
David Benjaminc11ea9422017-08-29 16:33:21 -0400253 // Sanity-check that the private key and the certificate match.
David Benjamin31b0c9b2017-07-20 14:49:15 -0400254 !ssl_compare_public_and_private_key(pubkey.get(), privkey)) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800255 ERR_clear_error();
David Benjamin31b0c9b2017-07-20 14:49:15 -0400256 return leaf_cert_and_privkey_mismatch;
Adam Langleyd04ca952017-02-28 11:26:51 -0800257 }
258
David Benjamin31b0c9b2017-07-20 14:49:15 -0400259 return leaf_cert_and_privkey_ok;
Adam Langleyd04ca952017-02-28 11:26:51 -0800260}
261
262static int cert_set_chain_and_key(
263 CERT *cert, CRYPTO_BUFFER *const *certs, size_t num_certs,
264 EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) {
265 if (num_certs == 0 ||
266 (privkey == NULL && privkey_method == NULL)) {
267 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langley52940c42017-02-01 12:40:31 -0800268 return 0;
269 }
270
Adam Langleyd04ca952017-02-28 11:26:51 -0800271 if (privkey != NULL && privkey_method != NULL) {
272 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
273 return 0;
Adam Langley52940c42017-02-01 12:40:31 -0800274 }
275
Adam Langleyd04ca952017-02-28 11:26:51 -0800276 switch (check_leaf_cert_and_privkey(certs[0], privkey)) {
277 case leaf_cert_and_privkey_error:
278 return 0;
279 case leaf_cert_and_privkey_mismatch:
280 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
281 return 0;
282 case leaf_cert_and_privkey_ok:
283 break;
284 }
285
David Benjamine325c3f2018-04-12 16:11:15 -0400286 UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs_sk(sk_CRYPTO_BUFFER_new_null());
287 if (!certs_sk) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800288 return 0;
289 }
290
291 for (size_t i = 0; i < num_certs; i++) {
David Benjamine325c3f2018-04-12 16:11:15 -0400292 if (!sk_CRYPTO_BUFFER_push(certs_sk.get(), certs[i])) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800293 return 0;
294 }
295 CRYPTO_BUFFER_up_ref(certs[i]);
296 }
297
David Benjamine325c3f2018-04-12 16:11:15 -0400298 if (privkey != nullptr) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800299 EVP_PKEY_up_ref(privkey);
300 }
David Benjamine325c3f2018-04-12 16:11:15 -0400301 cert->privatekey.reset(privkey);
Adam Langleyd04ca952017-02-28 11:26:51 -0800302 cert->key_method = privkey_method;
303
David Benjamine325c3f2018-04-12 16:11:15 -0400304 cert->chain = std::move(certs_sk);
Adam Langleyd04ca952017-02-28 11:26:51 -0800305 return 1;
306}
307
David Benjaminee910bf2017-07-25 22:36:00 -0400308int ssl_set_cert(CERT *cert, UniquePtr<CRYPTO_BUFFER> buffer) {
David Benjamine325c3f2018-04-12 16:11:15 -0400309 switch (check_leaf_cert_and_privkey(buffer.get(), cert->privatekey.get())) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800310 case leaf_cert_and_privkey_error:
311 return 0;
312 case leaf_cert_and_privkey_mismatch:
David Benjaminc11ea9422017-08-29 16:33:21 -0400313 // don't fail for a cert/key mismatch, just free current private key
314 // (when switching to a different cert & key, first this function should
315 // be used, then |ssl_set_pkey|.
David Benjamine325c3f2018-04-12 16:11:15 -0400316 cert->privatekey.reset();
Adam Langleyd04ca952017-02-28 11:26:51 -0800317 break;
318 case leaf_cert_and_privkey_ok:
319 break;
320 }
Adam Langley52940c42017-02-01 12:40:31 -0800321
322 cert->x509_method->cert_flush_cached_leaf(cert);
323
David Benjamine325c3f2018-04-12 16:11:15 -0400324 if (cert->chain != nullptr) {
325 CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0));
326 sk_CRYPTO_BUFFER_set(cert->chain.get(), 0, buffer.release());
Adam Langley52940c42017-02-01 12:40:31 -0800327 return 1;
328 }
329
David Benjamine325c3f2018-04-12 16:11:15 -0400330 cert->chain.reset(sk_CRYPTO_BUFFER_new_null());
331 if (cert->chain == nullptr) {
Adam Langley52940c42017-02-01 12:40:31 -0800332 return 0;
333 }
334
David Benjamine325c3f2018-04-12 16:11:15 -0400335 if (!PushToStack(cert->chain.get(), std::move(buffer))) {
336 cert->chain.reset();
Adam Langley52940c42017-02-01 12:40:31 -0800337 return 0;
338 }
Adam Langley52940c42017-02-01 12:40:31 -0800339
340 return 1;
341}
342
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700343int ssl_has_certificate(const SSL_CONFIG *cfg) {
344 return cfg->cert->chain != nullptr &&
345 sk_CRYPTO_BUFFER_value(cfg->cert->chain.get(), 0) != nullptr &&
346 ssl_has_private_key(cfg);
David Benjamin32a66d52016-07-13 22:03:11 -0400347}
348
David Benjamine664a532017-07-20 20:19:36 -0400349bool ssl_parse_cert_chain(uint8_t *out_alert,
350 UniquePtr<STACK_OF(CRYPTO_BUFFER)> *out_chain,
351 UniquePtr<EVP_PKEY> *out_pubkey,
352 uint8_t *out_leaf_sha256, CBS *cbs,
353 CRYPTO_BUFFER_POOL *pool) {
354 out_chain->reset();
355 out_pubkey->reset();
David Benjamin5c900c82016-07-13 23:03:26 -0400356
David Benjamin5c900c82016-07-13 23:03:26 -0400357 CBS certificate_list;
358 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
359 *out_alert = SSL_AD_DECODE_ERROR;
360 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamine664a532017-07-20 20:19:36 -0400361 return false;
David Benjamin5c900c82016-07-13 23:03:26 -0400362 }
363
David Benjamine664a532017-07-20 20:19:36 -0400364 if (CBS_len(&certificate_list) == 0) {
365 return true;
366 }
367
368 UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain(sk_CRYPTO_BUFFER_new_null());
369 if (!chain) {
370 *out_alert = SSL_AD_INTERNAL_ERROR;
371 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
372 return false;
373 }
374
375 UniquePtr<EVP_PKEY> pubkey;
David Benjamin5c900c82016-07-13 23:03:26 -0400376 while (CBS_len(&certificate_list) > 0) {
377 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800378 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
379 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400380 *out_alert = SSL_AD_DECODE_ERROR;
381 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
David Benjamine664a532017-07-20 20:19:36 -0400382 return false;
David Benjamin5c900c82016-07-13 23:03:26 -0400383 }
384
David Benjamine664a532017-07-20 20:19:36 -0400385 if (sk_CRYPTO_BUFFER_num(chain.get()) == 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400386 pubkey = ssl_cert_parse_pubkey(&certificate);
387 if (!pubkey) {
Adam Langley7dccc712017-02-08 08:51:25 -0800388 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamine664a532017-07-20 20:19:36 -0400389 return false;
Adam Langleyd5157222016-12-12 11:37:43 -0800390 }
391
David Benjaminc11ea9422017-08-29 16:33:21 -0400392 // Retain the hash of the leaf certificate if requested.
Adam Langleyd5157222016-12-12 11:37:43 -0800393 if (out_leaf_sha256 != NULL) {
394 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
395 }
David Benjamin5c900c82016-07-13 23:03:26 -0400396 }
397
David Benjaminee910bf2017-07-25 22:36:00 -0400398 UniquePtr<CRYPTO_BUFFER> buf(
399 CRYPTO_BUFFER_new_from_CBS(&certificate, pool));
400 if (!buf ||
401 !PushToStack(chain.get(), std::move(buf))) {
David Benjamin5c900c82016-07-13 23:03:26 -0400402 *out_alert = SSL_AD_INTERNAL_ERROR;
403 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamine664a532017-07-20 20:19:36 -0400404 return false;
David Benjamin5c900c82016-07-13 23:03:26 -0400405 }
David Benjamin5c900c82016-07-13 23:03:26 -0400406 }
407
David Benjamine664a532017-07-20 20:19:36 -0400408 *out_chain = std::move(chain);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400409 *out_pubkey = std::move(pubkey);
David Benjamine664a532017-07-20 20:19:36 -0400410 return true;
David Benjamin5c900c82016-07-13 23:03:26 -0400411}
412
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700413int ssl_add_cert_chain(SSL_HANDSHAKE *hs, CBB *cbb) {
414 if (!ssl_has_certificate(hs->config)) {
David Benjamin32a66d52016-07-13 22:03:11 -0400415 return CBB_add_u24(cbb, 0);
416 }
417
Adam Langley3a2b47a2017-01-24 13:59:42 -0800418 CBB certs;
419 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
David Benjamine64d2c72017-07-12 16:31:08 -0400420 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
421 return 0;
David Benjamin75836432016-06-17 18:48:29 -0400422 }
423
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700424 STACK_OF(CRYPTO_BUFFER) *chain = hs->config->cert->chain.get();
Adam Langley3a2b47a2017-01-24 13:59:42 -0800425 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
426 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
427 CBB child;
428 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
429 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
430 CRYPTO_BUFFER_len(buffer)) ||
431 !CBB_flush(&certs)) {
David Benjamine64d2c72017-07-12 16:31:08 -0400432 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
433 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 }
435 }
436
David Benjamin75836432016-06-17 18:48:29 -0400437 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800438}
Adam Langley95c29f32014-06-20 12:00:00 -0700439
David Benjaminc11ea9422017-08-29 16:33:21 -0400440// ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
441// positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
442// subjectPublicKeyInfo.
Adam Langley05672202016-12-13 12:05:49 -0800443static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800444 /* From RFC 5280, section 4.1
445 * Certificate ::= SEQUENCE {
446 * tbsCertificate TBSCertificate,
447 * signatureAlgorithm AlgorithmIdentifier,
448 * signatureValue BIT STRING }
449
450 * TBSCertificate ::= SEQUENCE {
451 * version [0] EXPLICIT Version DEFAULT v1,
452 * serialNumber CertificateSerialNumber,
453 * signature AlgorithmIdentifier,
454 * issuer Name,
455 * validity Validity,
456 * subject Name,
457 * subjectPublicKeyInfo SubjectPublicKeyInfo,
458 * ... } */
459 CBS buf = *in;
460
Adam Langley05672202016-12-13 12:05:49 -0800461 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800462 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
463 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800464 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400465 // version
Adam Langleyd5157222016-12-12 11:37:43 -0800466 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800467 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800468 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400469 // serialNumber
Adam Langley05672202016-12-13 12:05:49 -0800470 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400471 // signature algorithm
Adam Langley05672202016-12-13 12:05:49 -0800472 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400473 // issuer
Adam Langley05672202016-12-13 12:05:49 -0800474 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400475 // validity
Adam Langley05672202016-12-13 12:05:49 -0800476 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400477 // subject
Adam Langley05672202016-12-13 12:05:49 -0800478 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
479 return 0;
480 }
481
482 return 1;
483}
484
David Benjamin31b0c9b2017-07-20 14:49:15 -0400485UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500486 CBS buf = *in, tbs_cert;
487 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800488 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400489 return nullptr;
Adam Langleyd5157222016-12-12 11:37:43 -0800490 }
491
David Benjamin31b0c9b2017-07-20 14:49:15 -0400492 return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert));
Adam Langleyd5157222016-12-12 11:37:43 -0800493}
494
Adam Langleyc26692c2017-01-25 09:34:42 -0800495int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
496 const EVP_PKEY *privkey) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800497 if (EVP_PKEY_is_opaque(privkey)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400498 // We cannot check an opaque private key and have to trust that it
499 // matches.
Adam Langleyd04ca952017-02-28 11:26:51 -0800500 return 1;
501 }
502
Adam Langley3a2b47a2017-01-24 13:59:42 -0800503 int ret = 0;
504
505 switch (EVP_PKEY_cmp(pubkey, privkey)) {
506 case 1:
507 ret = 1;
508 break;
509 case 0:
510 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
511 break;
512 case -1:
513 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
514 break;
515 case -2:
516 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
Adam Langley11d11d62017-07-17 11:10:46 -0700517 break;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800518 default:
519 assert(0);
520 break;
521 }
522
Adam Langley3a2b47a2017-01-24 13:59:42 -0800523 return ret;
524}
525
526int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
David Benjamine325c3f2018-04-12 16:11:15 -0400527 if (privkey == nullptr) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800528 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
529 return 0;
530 }
531
David Benjamine325c3f2018-04-12 16:11:15 -0400532 if (cert->chain == nullptr ||
533 sk_CRYPTO_BUFFER_value(cert->chain.get(), 0) == nullptr) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800534 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
535 return 0;
536 }
537
Adam Langleyc26692c2017-01-25 09:34:42 -0800538 CBS cert_cbs;
David Benjamine325c3f2018-04-12 16:11:15 -0400539 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain.get(), 0),
540 &cert_cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400541 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
Adam Langleyc26692c2017-01-25 09:34:42 -0800542 if (!pubkey) {
543 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
544 return 0;
545 }
546
David Benjamin31b0c9b2017-07-20 14:49:15 -0400547 return ssl_compare_public_and_private_key(pubkey.get(), privkey);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800548}
549
Adam Langley05672202016-12-13 12:05:49 -0800550int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
551 CBS buf = *in;
552
553 CBS tbs_cert, outer_extensions;
554 int has_extensions;
555 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400556 // subjectPublicKeyInfo
Adam Langley05672202016-12-13 12:05:49 -0800557 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400558 // issuerUniqueID
Adam Langley05672202016-12-13 12:05:49 -0800559 !CBS_get_optional_asn1(
560 &tbs_cert, NULL, NULL,
561 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400562 // subjectUniqueID
Adam Langley05672202016-12-13 12:05:49 -0800563 !CBS_get_optional_asn1(
564 &tbs_cert, NULL, NULL,
565 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
566 !CBS_get_optional_asn1(
567 &tbs_cert, &outer_extensions, &has_extensions,
568 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
David Benjaminc64d1232017-10-04 18:14:28 -0400569 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
570 return 0;
Adam Langley05672202016-12-13 12:05:49 -0800571 }
572
573 if (!has_extensions) {
574 return 1;
575 }
576
577 CBS extensions;
578 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
David Benjaminc64d1232017-10-04 18:14:28 -0400579 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
580 return 0;
Adam Langley05672202016-12-13 12:05:49 -0800581 }
582
583 while (CBS_len(&extensions) > 0) {
584 CBS extension, oid, contents;
585 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
586 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
587 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
588 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
589 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
590 CBS_len(&extension) != 0) {
David Benjaminc64d1232017-10-04 18:14:28 -0400591 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
592 return 0;
Adam Langley05672202016-12-13 12:05:49 -0800593 }
594
595 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
596 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500597 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
598 0) {
Adam Langley05672202016-12-13 12:05:49 -0800599 continue;
600 }
601
602 CBS bit_string;
603 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
604 CBS_len(&contents) != 0) {
David Benjaminc64d1232017-10-04 18:14:28 -0400605 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
606 return 0;
Adam Langley05672202016-12-13 12:05:49 -0800607 }
608
David Benjaminc11ea9422017-08-29 16:33:21 -0400609 // This is the KeyUsage extension. See
610 // https://tools.ietf.org/html/rfc5280#section-4.2.1.3
Adam Langley05672202016-12-13 12:05:49 -0800611 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
David Benjaminc64d1232017-10-04 18:14:28 -0400612 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
613 return 0;
Adam Langley05672202016-12-13 12:05:49 -0800614 }
615
616 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
617 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
618 return 0;
619 }
620
621 return 1;
622 }
623
David Benjaminc11ea9422017-08-29 16:33:21 -0400624 // No KeyUsage extension found.
Adam Langley05672202016-12-13 12:05:49 -0800625 return 1;
Adam Langley05672202016-12-13 12:05:49 -0800626}
627
David Benjamin31b0c9b2017-07-20 14:49:15 -0400628UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
629 uint8_t *out_alert,
630 CBS *cbs) {
Adam Langley34b4c822017-02-02 10:57:17 -0800631 CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
632
David Benjamin31b0c9b2017-07-20 14:49:15 -0400633 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
634 if (!ret) {
David Benjamine0332e82016-07-13 22:40:36 -0400635 *out_alert = SSL_AD_INTERNAL_ERROR;
636 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400637 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400638 }
639
640 CBS child;
641 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
642 *out_alert = SSL_AD_DECODE_ERROR;
643 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400644 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400645 }
646
647 while (CBS_len(&child) > 0) {
648 CBS distinguished_name;
649 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
650 *out_alert = SSL_AD_DECODE_ERROR;
651 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400652 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400653 }
654
David Benjaminee910bf2017-07-25 22:36:00 -0400655 UniquePtr<CRYPTO_BUFFER> buffer(
656 CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool));
657 if (!buffer ||
658 !PushToStack(ret.get(), std::move(buffer))) {
David Benjamine0332e82016-07-13 22:40:36 -0400659 *out_alert = SSL_AD_INTERNAL_ERROR;
660 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400661 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400662 }
David Benjamine0332e82016-07-13 22:40:36 -0400663 }
664
David Benjamin31b0c9b2017-07-20 14:49:15 -0400665 if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) {
David Benjamin418cdc42018-05-09 13:56:09 -0400666 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley0bdef092017-02-23 15:02:58 -0800667 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400668 return nullptr;
Adam Langley0bdef092017-02-23 15:02:58 -0800669 }
670
David Benjamine0332e82016-07-13 22:40:36 -0400671 return ret;
David Benjamine0332e82016-07-13 22:40:36 -0400672}
673
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700674bool ssl_has_client_CAs(const SSL_CONFIG *cfg) {
675 STACK_OF(CRYPTO_BUFFER) *names = cfg->client_CA;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400676 if (names == NULL) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700677 names = cfg->ssl->ctx->client_CA;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400678 }
679 if (names == NULL) {
680 return false;
681 }
682 return sk_CRYPTO_BUFFER_num(names) > 0;
683}
684
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700685int ssl_add_client_CA_list(SSL_HANDSHAKE *hs, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400686 CBB child, name_cbb;
687 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
688 return 0;
689 }
690
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700691 STACK_OF(CRYPTO_BUFFER) *names = hs->config->client_CA;
Adam Langley34b4c822017-02-02 10:57:17 -0800692 if (names == NULL) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700693 names = hs->ssl->ctx->client_CA;
Adam Langley34b4c822017-02-02 10:57:17 -0800694 }
695 if (names == NULL) {
David Benjamin32a66d52016-07-13 22:03:11 -0400696 return CBB_flush(cbb);
697 }
698
David Benjaminec783832017-07-25 23:23:03 -0400699 for (const CRYPTO_BUFFER *name : names) {
David Benjamin32a66d52016-07-13 22:03:11 -0400700 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
Adam Langley34b4c822017-02-02 10:57:17 -0800701 !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
702 CRYPTO_BUFFER_len(name))) {
David Benjamin32a66d52016-07-13 22:03:11 -0400703 return 0;
704 }
705 }
706
707 return CBB_flush(cbb);
708}
709
David Benjamin45738dd2017-02-09 20:01:26 -0500710int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -0800711 const CRYPTO_BUFFER *leaf) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700712 assert(ssl_protocol_version(hs->ssl) < TLS1_3_VERSION);
David Benjamin938fa7c2016-10-07 00:27:05 -0400713
David Benjaminc11ea9422017-08-29 16:33:21 -0400714 // Check the certificate's type matches the cipher.
David Benjamin69522112017-03-28 15:38:29 -0500715 if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400716 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -0800717 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400718 }
719
David Benjaminc11ea9422017-08-29 16:33:21 -0400720 // Check key usages for all key types but RSA. This is needed to distinguish
721 // ECDH certificates, which we do not support, from ECDSA certificates. In
722 // principle, we should check RSA key usages based on cipher, but this breaks
723 // buggy antivirus deployments. Other key types are always used for signing.
724 //
725 // TODO(davidben): Get more recent data on RSA key usages.
David Benjamin69522112017-03-28 15:38:29 -0500726 if (EVP_PKEY_id(pkey) != EVP_PKEY_RSA) {
Adam Langley05672202016-12-13 12:05:49 -0800727 CBS leaf_cbs;
728 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
Adam Langley05672202016-12-13 12:05:49 -0800729 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
730 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400731 }
David Benjamin69522112017-03-28 15:38:29 -0500732 }
Steven Valdezbf5aa842016-07-15 07:07:40 -0400733
David Benjamin69522112017-03-28 15:38:29 -0500734 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400735 // Check the key's group and point format are acceptable.
David Benjamin69522112017-03-28 15:38:29 -0500736 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
David Benjamin938fa7c2016-10-07 00:27:05 -0400737 uint16_t group_id;
738 if (!ssl_nid_to_group_id(
739 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700740 !tls1_check_group_id(hs, group_id) ||
David Benjamin938fa7c2016-10-07 00:27:05 -0400741 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400742 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800743 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400744 }
745 }
746
Adam Langley05672202016-12-13 12:05:49 -0800747 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400748}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500749
David Benjamina232a712017-03-30 15:51:53 -0500750int ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
751 SSL *const ssl = hs->ssl;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700752 if (!ssl_has_certificate(hs->config)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400753 // Nothing to do.
David Benjamina232a712017-03-30 15:51:53 -0500754 return 1;
755 }
756
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700757 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(hs)) {
David Benjamina232a712017-03-30 15:51:53 -0500758 return 0;
759 }
760
761 CBS leaf;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700762 CRYPTO_BUFFER_init_CBS(
763 sk_CRYPTO_BUFFER_value(hs->config->cert->chain.get(), 0), &leaf);
David Benjamina232a712017-03-30 15:51:53 -0500764
David Benjamina232a712017-03-30 15:51:53 -0500765 hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
766 return hs->local_pubkey != NULL;
767}
768
David Benjamin86e95b82017-07-18 16:34:25 -0400769} // namespace bssl
770
771using namespace bssl;
772
773int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
774 size_t num_certs, EVP_PKEY *privkey,
775 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700776 if (!ssl->config) {
777 return 0;
778 }
779 return cert_set_chain_and_key(ssl->config->cert, certs, num_certs, privkey,
David Benjamin86e95b82017-07-18 16:34:25 -0400780 privkey_method);
781}
782
783int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
784 size_t num_certs, EVP_PKEY *privkey,
785 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
786 return cert_set_chain_and_key(ctx->cert, certs, num_certs, privkey,
787 privkey_method);
788}
789
790int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
791 const uint8_t *der) {
David Benjaminee910bf2017-07-25 22:36:00 -0400792 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
793 if (!buffer) {
David Benjamin86e95b82017-07-18 16:34:25 -0400794 return 0;
795 }
796
David Benjaminee910bf2017-07-25 22:36:00 -0400797 return ssl_set_cert(ctx->cert, std::move(buffer));
David Benjamin86e95b82017-07-18 16:34:25 -0400798}
799
800int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
David Benjaminee910bf2017-07-25 22:36:00 -0400801 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(der, der_len, NULL));
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700802 if (!buffer || !ssl->config) {
David Benjamin86e95b82017-07-18 16:34:25 -0400803 return 0;
804 }
805
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700806 return ssl_set_cert(ssl->config->cert, std::move(buffer));
David Benjamin86e95b82017-07-18 16:34:25 -0400807}
808
809void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
810 void *arg) {
811 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
812}
813
814void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700815 if (!ssl->config) {
816 return;
817 }
818 ssl_cert_set_cert_cb(ssl->config->cert, cb, arg);
David Benjamin86e95b82017-07-18 16:34:25 -0400819}
820
David Benjamin5f001d12018-05-08 16:46:48 -0400821const STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
David Benjamin86e95b82017-07-18 16:34:25 -0400822 SSL_SESSION *session = SSL_get_session(ssl);
823 if (session == NULL) {
824 return NULL;
825 }
826
827 return session->certs;
828}
829
David Benjamin5f001d12018-05-08 16:46:48 -0400830const STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
David Benjamin86e95b82017-07-18 16:34:25 -0400831 if (ssl->s3->hs == NULL) {
832 return NULL;
833 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400834 return ssl->s3->hs->ca_names.get();
David Benjamin86e95b82017-07-18 16:34:25 -0400835}
836
David Benjamin83a32122017-02-14 18:34:54 -0500837static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
David Benjamine325c3f2018-04-12 16:11:15 -0400838 size_t list_len) {
David Benjamin83a32122017-02-14 18:34:54 -0500839 CBS sct_list;
840 CBS_init(&sct_list, list, list_len);
841 if (!ssl_is_sct_list_valid(&sct_list)) {
842 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
843 return 0;
844 }
845
David Benjamine325c3f2018-04-12 16:11:15 -0400846 cert->signed_cert_timestamp_list.reset(
847 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), nullptr));
848 return cert->signed_cert_timestamp_list != nullptr;
David Benjamin83a32122017-02-14 18:34:54 -0500849}
850
851int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
852 size_t list_len) {
853 return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
854}
855
856int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
857 size_t list_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700858 if (!ssl->config) {
859 return 0;
860 }
861 return set_signed_cert_timestamp_list(ssl->config->cert, list, list_len);
David Benjamin83a32122017-02-14 18:34:54 -0500862}
863
864int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
865 size_t response_len) {
David Benjamine325c3f2018-04-12 16:11:15 -0400866 ctx->cert->ocsp_response.reset(
867 CRYPTO_BUFFER_new(response, response_len, nullptr));
868 return ctx->cert->ocsp_response != nullptr;
David Benjamin83a32122017-02-14 18:34:54 -0500869}
870
871int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
872 size_t response_len) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700873 if (!ssl->config) {
874 return 0;
875 }
876 ssl->config->cert->ocsp_response.reset(
David Benjamine325c3f2018-04-12 16:11:15 -0400877 CRYPTO_BUFFER_new(response, response_len, nullptr));
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700878 return ssl->config->cert->ocsp_response != nullptr;
David Benjamin83a32122017-02-14 18:34:54 -0500879}
David Benjamin71dfad42017-07-16 17:27:39 -0400880
881void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) {
882 ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
883 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
884 ctx->client_CA = name_list;
885}
886
887void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700888 if (!ssl->config) {
889 return;
890 }
891 ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config);
892 sk_CRYPTO_BUFFER_pop_free(ssl->config->client_CA, CRYPTO_BUFFER_free);
893 ssl->config->client_CA = name_list;
David Benjamin71dfad42017-07-16 17:27:39 -0400894}