blob: 4f457f617b5360a850ae4aa0407b1fb95be44d1e [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
Adam Langley3509dac2017-02-01 11:59:18 -0800138CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) {
David Benjamine64d2c72017-07-12 16:31:08 -0400139 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800140 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400141 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800142 return NULL;
143 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500144 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley3509dac2017-02-01 11:59:18 -0800145 ret->x509_method = x509_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700146
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 return ret;
148}
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley3a2b47a2017-01-24 13:59:42 -0800150static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
151 CRYPTO_BUFFER_up_ref(buffer);
152 return buffer;
153}
154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155CERT *ssl_cert_dup(CERT *cert) {
David Benjamine64d2c72017-07-12 16:31:08 -0400156 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400158 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 return NULL;
160 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500161 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700162
Adam Langley3a2b47a2017-01-24 13:59:42 -0800163 ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
164 CRYPTO_BUFFER_free);
Adam Langley95c29f32014-06-20 12:00:00 -0700165
David Benjamind1d80782015-07-05 11:54:09 -0400166 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700167 EVP_PKEY_up_ref(cert->privatekey);
168 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400169 }
Adam Langley95c29f32014-06-20 12:00:00 -0700170
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030171 ret->key_method = cert->key_method;
Adam Langley3509dac2017-02-01 11:59:18 -0800172 ret->x509_method = cert->x509_method;
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030173
David Benjamin0fc37ef2016-08-17 15:29:46 -0400174 if (cert->sigalgs != NULL) {
David Benjamine64d2c72017-07-12 16:31:08 -0400175 ret->sigalgs = (uint16_t *)BUF_memdup(
176 cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
David Benjamin0fc37ef2016-08-17 15:29:46 -0400177 if (ret->sigalgs == NULL) {
178 goto err;
179 }
180 }
181 ret->num_sigalgs = cert->num_sigalgs;
182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 ret->cert_cb = cert->cert_cb;
184 ret->cert_cb_arg = cert->cert_cb_arg;
185
Adam Langley2a3b3432017-02-10 13:56:48 -0800186 ret->x509_method->cert_dup(ret, cert);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800187
David Benjamin83a32122017-02-14 18:34:54 -0500188 if (cert->signed_cert_timestamp_list != NULL) {
189 CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list);
190 ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list;
191 }
192
193 if (cert->ocsp_response != NULL) {
194 CRYPTO_BUFFER_up_ref(cert->ocsp_response);
195 ret->ocsp_response = cert->ocsp_response;
196 }
197
David Benjamin5960a902017-02-14 20:07:11 -0500198 ret->sid_ctx_length = cert->sid_ctx_length;
199 OPENSSL_memcpy(ret->sid_ctx, cert->sid_ctx, sizeof(ret->sid_ctx));
200
Alessandro Ghedini67bb45f2017-03-30 16:33:24 -0500201 ret->enable_early_data = cert->enable_early_data;
202
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 return ret;
204
Adam Langley95c29f32014-06-20 12:00:00 -0700205err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 ssl_cert_free(ret);
207 return NULL;
208}
Adam Langley95c29f32014-06-20 12:00:00 -0700209
210/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400211void ssl_cert_clear_certs(CERT *cert) {
212 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 return;
214 }
Adam Langley95c29f32014-06-20 12:00:00 -0700215
Adam Langley3509dac2017-02-01 11:59:18 -0800216 cert->x509_method->cert_clear(cert);
Adam Langleye1e78132017-01-31 15:24:31 -0800217
Adam Langley3a2b47a2017-01-24 13:59:42 -0800218 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
219 cert->chain = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400220 EVP_PKEY_free(cert->privatekey);
221 cert->privatekey = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400222 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800223}
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225void ssl_cert_free(CERT *c) {
226 if (c == NULL) {
227 return;
228 }
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 ssl_cert_clear_certs(c);
Adam Langley2a3b3432017-02-10 13:56:48 -0800231 c->x509_method->cert_free(c);
David Benjamind246b812016-07-08 15:07:02 -0700232 OPENSSL_free(c->sigalgs);
David Benjamin83a32122017-02-14 18:34:54 -0500233 CRYPTO_BUFFER_free(c->signed_cert_timestamp_list);
234 CRYPTO_BUFFER_free(c->ocsp_response);
Adam Langley95c29f32014-06-20 12:00:00 -0700235
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 OPENSSL_free(c);
237}
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjamina2bda9f2016-12-03 23:29:05 -0500239static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
240 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 c->cert_cb = cb;
242 c->cert_cb_arg = arg;
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyd04ca952017-02-28 11:26:51 -0800245enum leaf_cert_and_privkey_result_t {
246 leaf_cert_and_privkey_error,
247 leaf_cert_and_privkey_ok,
248 leaf_cert_and_privkey_mismatch,
249};
250
251/* check_leaf_cert_and_privkey checks whether the certificate in |leaf_buffer|
252 * and the private key in |privkey| are suitable and coherent. It returns
253 * |leaf_cert_and_privkey_error| and pushes to the error queue if a problem is
254 * found. If the certificate and private key are valid, but incoherent, it
255 * returns |leaf_cert_and_privkey_mismatch|. Otherwise it returns
256 * |leaf_cert_and_privkey_ok|. */
257static enum leaf_cert_and_privkey_result_t check_leaf_cert_and_privkey(
258 CRYPTO_BUFFER *leaf_buffer, EVP_PKEY *privkey) {
Adam Langley52940c42017-02-01 12:40:31 -0800259 CBS cert_cbs;
Adam Langleyd04ca952017-02-28 11:26:51 -0800260 CRYPTO_BUFFER_init_CBS(leaf_buffer, &cert_cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400261 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
262 if (!pubkey) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800263 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400264 return leaf_cert_and_privkey_error;
Adam Langley52940c42017-02-01 12:40:31 -0800265 }
266
267 if (!ssl_is_key_type_supported(pubkey->type)) {
268 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400269 return leaf_cert_and_privkey_error;
Adam Langley52940c42017-02-01 12:40:31 -0800270 }
271
272 /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
273 * certificates, so sanity-check the key usage extension. */
274 if (pubkey->type == EVP_PKEY_EC &&
275 !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
276 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400277 return leaf_cert_and_privkey_error;
Adam Langleyd04ca952017-02-28 11:26:51 -0800278 }
279
280 if (privkey != NULL &&
281 /* Sanity-check that the private key and the certificate match. */
David Benjamin31b0c9b2017-07-20 14:49:15 -0400282 !ssl_compare_public_and_private_key(pubkey.get(), privkey)) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800283 ERR_clear_error();
David Benjamin31b0c9b2017-07-20 14:49:15 -0400284 return leaf_cert_and_privkey_mismatch;
Adam Langleyd04ca952017-02-28 11:26:51 -0800285 }
286
David Benjamin31b0c9b2017-07-20 14:49:15 -0400287 return leaf_cert_and_privkey_ok;
Adam Langleyd04ca952017-02-28 11:26:51 -0800288}
289
290static int cert_set_chain_and_key(
291 CERT *cert, CRYPTO_BUFFER *const *certs, size_t num_certs,
292 EVP_PKEY *privkey, const SSL_PRIVATE_KEY_METHOD *privkey_method) {
293 if (num_certs == 0 ||
294 (privkey == NULL && privkey_method == NULL)) {
295 OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
Adam Langley52940c42017-02-01 12:40:31 -0800296 return 0;
297 }
298
Adam Langleyd04ca952017-02-28 11:26:51 -0800299 if (privkey != NULL && privkey_method != NULL) {
300 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_HAVE_BOTH_PRIVKEY_AND_METHOD);
301 return 0;
Adam Langley52940c42017-02-01 12:40:31 -0800302 }
303
Adam Langleyd04ca952017-02-28 11:26:51 -0800304 switch (check_leaf_cert_and_privkey(certs[0], privkey)) {
305 case leaf_cert_and_privkey_error:
306 return 0;
307 case leaf_cert_and_privkey_mismatch:
308 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_AND_PRIVATE_KEY_MISMATCH);
309 return 0;
310 case leaf_cert_and_privkey_ok:
311 break;
312 }
313
314 STACK_OF(CRYPTO_BUFFER) *certs_sk = sk_CRYPTO_BUFFER_new_null();
315 if (certs_sk == NULL) {
316 return 0;
317 }
318
319 for (size_t i = 0; i < num_certs; i++) {
320 if (!sk_CRYPTO_BUFFER_push(certs_sk, certs[i])) {
321 sk_CRYPTO_BUFFER_pop_free(certs_sk, CRYPTO_BUFFER_free);
322 return 0;
323 }
324 CRYPTO_BUFFER_up_ref(certs[i]);
325 }
326
327 EVP_PKEY_free(cert->privatekey);
328 cert->privatekey = privkey;
329 if (privkey != NULL) {
330 EVP_PKEY_up_ref(privkey);
331 }
332 cert->key_method = privkey_method;
333
334 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
335 cert->chain = certs_sk;
336
337 return 1;
338}
339
Adam Langleyd04ca952017-02-28 11:26:51 -0800340int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) {
341 switch (check_leaf_cert_and_privkey(buffer, cert->privatekey)) {
342 case leaf_cert_and_privkey_error:
343 return 0;
344 case leaf_cert_and_privkey_mismatch:
345 /* don't fail for a cert/key mismatch, just free current private key
346 * (when switching to a different cert & key, first this function should
347 * be used, then |ssl_set_pkey|. */
348 EVP_PKEY_free(cert->privatekey);
349 cert->privatekey = NULL;
350 break;
351 case leaf_cert_and_privkey_ok:
352 break;
353 }
Adam Langley52940c42017-02-01 12:40:31 -0800354
355 cert->x509_method->cert_flush_cached_leaf(cert);
356
357 if (cert->chain != NULL) {
358 CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0));
359 sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer);
360 CRYPTO_BUFFER_up_ref(buffer);
361 return 1;
362 }
363
364 cert->chain = sk_CRYPTO_BUFFER_new_null();
365 if (cert->chain == NULL) {
366 return 0;
367 }
368
369 if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
370 sk_CRYPTO_BUFFER_free(cert->chain);
371 cert->chain = NULL;
372 return 0;
373 }
374 CRYPTO_BUFFER_up_ref(buffer);
375
376 return 1;
377}
378
David Benjamin32a66d52016-07-13 22:03:11 -0400379int ssl_has_certificate(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800380 return ssl->cert->chain != NULL &&
381 sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
382 ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400383}
384
David Benjamin31b0c9b2017-07-20 14:49:15 -0400385UniquePtr<STACK_OF(CRYPTO_BUFFER)>
386 ssl_parse_cert_chain(uint8_t *out_alert, UniquePtr<EVP_PKEY> *out_pubkey,
387 uint8_t *out_leaf_sha256, CBS *cbs,
388 CRYPTO_BUFFER_POOL *pool) {
389 UniquePtr<EVP_PKEY> pubkey;
390 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
391 if (!ret) {
David Benjamin5c900c82016-07-13 23:03:26 -0400392 *out_alert = SSL_AD_INTERNAL_ERROR;
393 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400394 return nullptr;
David Benjamin5c900c82016-07-13 23:03:26 -0400395 }
396
David Benjamin5c900c82016-07-13 23:03:26 -0400397 CBS certificate_list;
398 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
399 *out_alert = SSL_AD_DECODE_ERROR;
400 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400401 return nullptr;
David Benjamin5c900c82016-07-13 23:03:26 -0400402 }
403
404 while (CBS_len(&certificate_list) > 0) {
405 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800406 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
407 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400408 *out_alert = SSL_AD_DECODE_ERROR;
409 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400410 return nullptr;
David Benjamin5c900c82016-07-13 23:03:26 -0400411 }
412
David Benjamin31b0c9b2017-07-20 14:49:15 -0400413 if (sk_CRYPTO_BUFFER_num(ret.get()) == 0) {
414 pubkey = ssl_cert_parse_pubkey(&certificate);
415 if (!pubkey) {
Adam Langley7dccc712017-02-08 08:51:25 -0800416 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamin31b0c9b2017-07-20 14:49:15 -0400417 return nullptr;
Adam Langleyd5157222016-12-12 11:37:43 -0800418 }
419
420 /* Retain the hash of the leaf certificate if requested. */
421 if (out_leaf_sha256 != NULL) {
422 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
423 }
David Benjamin5c900c82016-07-13 23:03:26 -0400424 }
425
Adam Langleyd519bf62016-12-12 11:16:44 -0800426 CRYPTO_BUFFER *buf =
427 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400428 if (buf == NULL ||
429 !sk_CRYPTO_BUFFER_push(ret.get(), buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400430 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800431 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400432 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400433 return nullptr;
David Benjamin5c900c82016-07-13 23:03:26 -0400434 }
David Benjamin5c900c82016-07-13 23:03:26 -0400435 }
436
David Benjamin31b0c9b2017-07-20 14:49:15 -0400437 *out_pubkey = std::move(pubkey);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800438 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400439}
440
David Benjamin75836432016-06-17 18:48:29 -0400441int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400442 if (!ssl_has_certificate(ssl)) {
443 return CBB_add_u24(cbb, 0);
444 }
445
Adam Langley3a2b47a2017-01-24 13:59:42 -0800446 CBB certs;
447 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
David Benjamine64d2c72017-07-12 16:31:08 -0400448 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
449 return 0;
David Benjamin75836432016-06-17 18:48:29 -0400450 }
451
Adam Langley3a2b47a2017-01-24 13:59:42 -0800452 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
453 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
454 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
455 CBB child;
456 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
457 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
458 CRYPTO_BUFFER_len(buffer)) ||
459 !CBB_flush(&certs)) {
David Benjamine64d2c72017-07-12 16:31:08 -0400460 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
461 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 }
463 }
464
David Benjamin75836432016-06-17 18:48:29 -0400465 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Adam Langley05672202016-12-13 12:05:49 -0800468/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
469 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
470 * subjectPublicKeyInfo. */
471static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800472 /* From RFC 5280, section 4.1
473 * Certificate ::= SEQUENCE {
474 * tbsCertificate TBSCertificate,
475 * signatureAlgorithm AlgorithmIdentifier,
476 * signatureValue BIT STRING }
477
478 * TBSCertificate ::= SEQUENCE {
479 * version [0] EXPLICIT Version DEFAULT v1,
480 * serialNumber CertificateSerialNumber,
481 * signature AlgorithmIdentifier,
482 * issuer Name,
483 * validity Validity,
484 * subject Name,
485 * subjectPublicKeyInfo SubjectPublicKeyInfo,
486 * ... } */
487 CBS buf = *in;
488
Adam Langley05672202016-12-13 12:05:49 -0800489 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800490 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
491 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800492 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800493 /* version */
494 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800495 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800496 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
497 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800498 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800499 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800500 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800501 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800502 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800503 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800504 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800505 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800506 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
507 return 0;
508 }
509
510 return 1;
511}
512
David Benjamin31b0c9b2017-07-20 14:49:15 -0400513UniquePtr<EVP_PKEY> ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500514 CBS buf = *in, tbs_cert;
515 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800516 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400517 return nullptr;
Adam Langleyd5157222016-12-12 11:37:43 -0800518 }
519
David Benjamin31b0c9b2017-07-20 14:49:15 -0400520 return UniquePtr<EVP_PKEY>(EVP_parse_public_key(&tbs_cert));
Adam Langleyd5157222016-12-12 11:37:43 -0800521}
522
Adam Langleyc26692c2017-01-25 09:34:42 -0800523int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
524 const EVP_PKEY *privkey) {
Adam Langleyd04ca952017-02-28 11:26:51 -0800525 if (EVP_PKEY_is_opaque(privkey)) {
526 /* We cannot check an opaque private key and have to trust that it
527 * matches. */
528 return 1;
529 }
530
Adam Langley3a2b47a2017-01-24 13:59:42 -0800531 int ret = 0;
532
533 switch (EVP_PKEY_cmp(pubkey, privkey)) {
534 case 1:
535 ret = 1;
536 break;
537 case 0:
538 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
539 break;
540 case -1:
541 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
542 break;
543 case -2:
544 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
Adam Langley11d11d62017-07-17 11:10:46 -0700545 break;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800546 default:
547 assert(0);
548 break;
549 }
550
Adam Langley3a2b47a2017-01-24 13:59:42 -0800551 return ret;
552}
553
554int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
555 if (privkey == NULL) {
556 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
557 return 0;
558 }
559
560 if (cert->chain == NULL ||
561 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
562 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
563 return 0;
564 }
565
Adam Langleyc26692c2017-01-25 09:34:42 -0800566 CBS cert_cbs;
567 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400568 UniquePtr<EVP_PKEY> pubkey = ssl_cert_parse_pubkey(&cert_cbs);
Adam Langleyc26692c2017-01-25 09:34:42 -0800569 if (!pubkey) {
570 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
571 return 0;
572 }
573
David Benjamin31b0c9b2017-07-20 14:49:15 -0400574 return ssl_compare_public_and_private_key(pubkey.get(), privkey);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800575}
576
Adam Langley05672202016-12-13 12:05:49 -0800577int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
578 CBS buf = *in;
579
580 CBS tbs_cert, outer_extensions;
581 int has_extensions;
582 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
583 /* subjectPublicKeyInfo */
584 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
585 /* issuerUniqueID */
586 !CBS_get_optional_asn1(
587 &tbs_cert, NULL, NULL,
588 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
589 /* subjectUniqueID */
590 !CBS_get_optional_asn1(
591 &tbs_cert, NULL, NULL,
592 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
593 !CBS_get_optional_asn1(
594 &tbs_cert, &outer_extensions, &has_extensions,
595 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
596 goto parse_err;
597 }
598
599 if (!has_extensions) {
600 return 1;
601 }
602
603 CBS extensions;
604 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
605 goto parse_err;
606 }
607
608 while (CBS_len(&extensions) > 0) {
609 CBS extension, oid, contents;
610 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
611 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
612 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
613 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
614 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
615 CBS_len(&extension) != 0) {
616 goto parse_err;
617 }
618
619 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
620 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500621 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
622 0) {
Adam Langley05672202016-12-13 12:05:49 -0800623 continue;
624 }
625
626 CBS bit_string;
627 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
628 CBS_len(&contents) != 0) {
629 goto parse_err;
630 }
631
632 /* This is the KeyUsage extension. See
633 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
634 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
635 goto parse_err;
636 }
637
638 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
639 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
640 return 0;
641 }
642
643 return 1;
644 }
645
646 /* No KeyUsage extension found. */
647 return 1;
648
649parse_err:
650 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
651 return 0;
652}
653
David Benjamin31b0c9b2017-07-20 14:49:15 -0400654UniquePtr<STACK_OF(CRYPTO_BUFFER)> ssl_parse_client_CA_list(SSL *ssl,
655 uint8_t *out_alert,
656 CBS *cbs) {
Adam Langley34b4c822017-02-02 10:57:17 -0800657 CRYPTO_BUFFER_POOL *const pool = ssl->ctx->pool;
658
David Benjamin31b0c9b2017-07-20 14:49:15 -0400659 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ret(sk_CRYPTO_BUFFER_new_null());
660 if (!ret) {
David Benjamine0332e82016-07-13 22:40:36 -0400661 *out_alert = SSL_AD_INTERNAL_ERROR;
662 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400663 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400664 }
665
666 CBS child;
667 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
668 *out_alert = SSL_AD_DECODE_ERROR;
669 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400670 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400671 }
672
673 while (CBS_len(&child) > 0) {
674 CBS distinguished_name;
675 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
676 *out_alert = SSL_AD_DECODE_ERROR;
677 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400678 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400679 }
680
Adam Langley34b4c822017-02-02 10:57:17 -0800681 CRYPTO_BUFFER *buffer =
682 CRYPTO_BUFFER_new_from_CBS(&distinguished_name, pool);
683 if (buffer == NULL ||
David Benjamin31b0c9b2017-07-20 14:49:15 -0400684 !sk_CRYPTO_BUFFER_push(ret.get(), buffer)) {
Adam Langley34b4c822017-02-02 10:57:17 -0800685 CRYPTO_BUFFER_free(buffer);
David Benjamine0332e82016-07-13 22:40:36 -0400686 *out_alert = SSL_AD_INTERNAL_ERROR;
687 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400688 return nullptr;
David Benjamine0332e82016-07-13 22:40:36 -0400689 }
David Benjamine0332e82016-07-13 22:40:36 -0400690 }
691
David Benjamin31b0c9b2017-07-20 14:49:15 -0400692 if (!ssl->ctx->x509_method->check_client_CA_list(ret.get())) {
Adam Langley0bdef092017-02-23 15:02:58 -0800693 *out_alert = SSL_AD_INTERNAL_ERROR;
694 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400695 return nullptr;
Adam Langley0bdef092017-02-23 15:02:58 -0800696 }
697
David Benjamine0332e82016-07-13 22:40:36 -0400698 return ret;
David Benjamine0332e82016-07-13 22:40:36 -0400699}
700
David Benjamin32a66d52016-07-13 22:03:11 -0400701int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
702 CBB child, name_cbb;
703 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
704 return 0;
705 }
706
Adam Langley34b4c822017-02-02 10:57:17 -0800707 STACK_OF(CRYPTO_BUFFER) *names = ssl->client_CA;
708 if (names == NULL) {
709 names = ssl->ctx->client_CA;
710 }
711 if (names == NULL) {
David Benjamin32a66d52016-07-13 22:03:11 -0400712 return CBB_flush(cbb);
713 }
714
Adam Langley34b4c822017-02-02 10:57:17 -0800715 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
716 const CRYPTO_BUFFER *name = sk_CRYPTO_BUFFER_value(names, i);
717
David Benjamin32a66d52016-07-13 22:03:11 -0400718 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
Adam Langley34b4c822017-02-02 10:57:17 -0800719 !CBB_add_bytes(&name_cbb, CRYPTO_BUFFER_data(name),
720 CRYPTO_BUFFER_len(name))) {
David Benjamin32a66d52016-07-13 22:03:11 -0400721 return 0;
722 }
723 }
724
725 return CBB_flush(cbb);
726}
727
David Benjamin45738dd2017-02-09 20:01:26 -0500728int ssl_check_leaf_certificate(SSL_HANDSHAKE *hs, EVP_PKEY *pkey,
Adam Langley05672202016-12-13 12:05:49 -0800729 const CRYPTO_BUFFER *leaf) {
David Benjamin45738dd2017-02-09 20:01:26 -0500730 SSL *const ssl = hs->ssl;
David Benjamin938fa7c2016-10-07 00:27:05 -0400731 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
732
Steven Valdezbf5aa842016-07-15 07:07:40 -0400733 /* Check the certificate's type matches the cipher. */
David Benjamin69522112017-03-28 15:38:29 -0500734 if (!(hs->new_cipher->algorithm_auth & ssl_cipher_auth_mask_for_key(pkey))) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400735 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -0800736 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400737 }
738
David Benjamin69522112017-03-28 15:38:29 -0500739 /* Check key usages for all key types but RSA. This is needed to distinguish
740 * ECDH certificates, which we do not support, from ECDSA certificates. In
741 * principle, we should check RSA key usages based on cipher, but this breaks
742 * buggy antivirus deployments. Other key types are always used for signing.
743 *
744 * TODO(davidben): Get more recent data on RSA key usages. */
745 if (EVP_PKEY_id(pkey) != EVP_PKEY_RSA) {
Adam Langley05672202016-12-13 12:05:49 -0800746 CBS leaf_cbs;
747 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
Adam Langley05672202016-12-13 12:05:49 -0800748 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
749 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400750 }
David Benjamin69522112017-03-28 15:38:29 -0500751 }
Steven Valdezbf5aa842016-07-15 07:07:40 -0400752
David Benjamin69522112017-03-28 15:38:29 -0500753 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
David Benjamin938fa7c2016-10-07 00:27:05 -0400754 /* Check the key's group and point format are acceptable. */
David Benjamin69522112017-03-28 15:38:29 -0500755 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
David Benjamin938fa7c2016-10-07 00:27:05 -0400756 uint16_t group_id;
757 if (!ssl_nid_to_group_id(
758 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
759 !tls1_check_group_id(ssl, group_id) ||
760 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400761 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800762 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400763 }
764 }
765
Adam Langley05672202016-12-13 12:05:49 -0800766 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400767}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500768
David Benjamina232a712017-03-30 15:51:53 -0500769int ssl_on_certificate_selected(SSL_HANDSHAKE *hs) {
770 SSL *const ssl = hs->ssl;
771 if (!ssl_has_certificate(ssl)) {
772 /* Nothing to do. */
773 return 1;
774 }
775
776 if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(ssl)) {
777 return 0;
778 }
779
780 CBS leaf;
781 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0), &leaf);
782
David Benjamina232a712017-03-30 15:51:53 -0500783 hs->local_pubkey = ssl_cert_parse_pubkey(&leaf);
784 return hs->local_pubkey != NULL;
785}
786
David Benjamin86e95b82017-07-18 16:34:25 -0400787} // namespace bssl
788
789using namespace bssl;
790
791int SSL_set_chain_and_key(SSL *ssl, CRYPTO_BUFFER *const *certs,
792 size_t num_certs, EVP_PKEY *privkey,
793 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
794 return cert_set_chain_and_key(ssl->cert, certs, num_certs, privkey,
795 privkey_method);
796}
797
798int SSL_CTX_set_chain_and_key(SSL_CTX *ctx, CRYPTO_BUFFER *const *certs,
799 size_t num_certs, EVP_PKEY *privkey,
800 const SSL_PRIVATE_KEY_METHOD *privkey_method) {
801 return cert_set_chain_and_key(ctx->cert, certs, num_certs, privkey,
802 privkey_method);
803}
804
805int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
806 const uint8_t *der) {
807 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
808 if (buffer == NULL) {
809 return 0;
810 }
811
812 const int ok = ssl_set_cert(ctx->cert, buffer);
813 CRYPTO_BUFFER_free(buffer);
814 return ok;
815}
816
817int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
818 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
819 if (buffer == NULL) {
820 return 0;
821 }
822
823 const int ok = ssl_set_cert(ssl->cert, buffer);
824 CRYPTO_BUFFER_free(buffer);
825 return ok;
826}
827
828void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
829 void *arg) {
830 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
831}
832
833void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
834 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
835}
836
837STACK_OF(CRYPTO_BUFFER) *SSL_get0_peer_certificates(const SSL *ssl) {
838 SSL_SESSION *session = SSL_get_session(ssl);
839 if (session == NULL) {
840 return NULL;
841 }
842
843 return session->certs;
844}
845
846STACK_OF(CRYPTO_BUFFER) *SSL_get0_server_requested_CAs(const SSL *ssl) {
847 if (ssl->s3->hs == NULL) {
848 return NULL;
849 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400850 return ssl->s3->hs->ca_names.get();
David Benjamin86e95b82017-07-18 16:34:25 -0400851}
852
David Benjamin83a32122017-02-14 18:34:54 -0500853static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
854 size_t list_len) {
855 CBS sct_list;
856 CBS_init(&sct_list, list, list_len);
857 if (!ssl_is_sct_list_valid(&sct_list)) {
858 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
859 return 0;
860 }
861
862 CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
863 cert->signed_cert_timestamp_list =
864 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL);
865 return cert->signed_cert_timestamp_list != NULL;
866}
867
868int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
869 size_t list_len) {
870 return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
871}
872
873int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
874 size_t list_len) {
875 return set_signed_cert_timestamp_list(ssl->cert, list, list_len);
876}
877
878int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
879 size_t response_len) {
880 CRYPTO_BUFFER_free(ctx->cert->ocsp_response);
881 ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
882 return ctx->cert->ocsp_response != NULL;
883}
884
885int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
886 size_t response_len) {
887 CRYPTO_BUFFER_free(ssl->cert->ocsp_response);
888 ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
889 return ssl->cert->ocsp_response != NULL;
890}
David Benjamin71dfad42017-07-16 17:27:39 -0400891
892void SSL_CTX_set0_client_CAs(SSL_CTX *ctx, STACK_OF(CRYPTO_BUFFER) *name_list) {
893 ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
894 sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
895 ctx->client_CA = name_list;
896}
897
898void SSL_set0_client_CAs(SSL *ssl, STACK_OF(CRYPTO_BUFFER) *name_list) {
899 ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl);
900 sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
901 ssl->client_CA = name_list;
902}