blob: c94ca4979328154da0ecbfb6b2d9ad23f8c818c3 [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
Adam Langley95c29f32014-06-20 12:00:00 -0700121#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400122#include <openssl/buf.h>
Adam Langleyd5157222016-12-12 11:37:43 -0800123#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124#include <openssl/dh.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#include <openssl/x509v3.h>
131
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
Adam Langleyfcf25832014-12-18 17:42:32 -0800136int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
David Benjaminaa585132015-06-29 23:36:17 -0400137 /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
138 * reserved app_data slot. Before ex_data was introduced, app_data was used.
139 * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
140 * works. */
141 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800142}
Adam Langley95c29f32014-06-20 12:00:00 -0700143
Adam Langley3509dac2017-02-01 11:59:18 -0800144CERT *ssl_cert_new(const SSL_X509_METHOD *x509_method) {
Brian Smith5ba06892016-02-07 09:36:04 -1000145 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800146 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400147 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800148 return NULL;
149 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500150 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley3509dac2017-02-01 11:59:18 -0800151 ret->x509_method = x509_method;
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 return ret;
154}
Adam Langley95c29f32014-06-20 12:00:00 -0700155
Adam Langley3a2b47a2017-01-24 13:59:42 -0800156static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
157 CRYPTO_BUFFER_up_ref(buffer);
158 return buffer;
159}
160
Adam Langleyfcf25832014-12-18 17:42:32 -0800161CERT *ssl_cert_dup(CERT *cert) {
Brian Smith5ba06892016-02-07 09:36:04 -1000162 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400164 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800165 return NULL;
166 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500167 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700168
Adam Langley3a2b47a2017-01-24 13:59:42 -0800169 ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
170 CRYPTO_BUFFER_free);
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjamind1d80782015-07-05 11:54:09 -0400172 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700173 EVP_PKEY_up_ref(cert->privatekey);
174 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030177 ret->key_method = cert->key_method;
Adam Langley3509dac2017-02-01 11:59:18 -0800178 ret->x509_method = cert->x509_method;
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030179
David Benjamin0fc37ef2016-08-17 15:29:46 -0400180 if (cert->dh_tmp != NULL) {
181 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
182 if (ret->dh_tmp == NULL) {
183 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
184 goto err;
185 }
186 }
187 ret->dh_tmp_cb = cert->dh_tmp_cb;
188
189 if (cert->sigalgs != NULL) {
190 ret->sigalgs =
191 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
192 if (ret->sigalgs == NULL) {
193 goto err;
194 }
195 }
196 ret->num_sigalgs = cert->num_sigalgs;
197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 ret->cert_cb = cert->cert_cb;
199 ret->cert_cb_arg = cert->cert_cb_arg;
200
Adam Langleyd323f4b2016-03-01 15:58:14 -0800201 if (cert->verify_store != NULL) {
202 X509_STORE_up_ref(cert->verify_store);
203 ret->verify_store = cert->verify_store;
204 }
205
David Benjamin83a32122017-02-14 18:34:54 -0500206 if (cert->signed_cert_timestamp_list != NULL) {
207 CRYPTO_BUFFER_up_ref(cert->signed_cert_timestamp_list);
208 ret->signed_cert_timestamp_list = cert->signed_cert_timestamp_list;
209 }
210
211 if (cert->ocsp_response != NULL) {
212 CRYPTO_BUFFER_up_ref(cert->ocsp_response);
213 ret->ocsp_response = cert->ocsp_response;
214 }
215
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 return ret;
217
Adam Langley95c29f32014-06-20 12:00:00 -0700218err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 ssl_cert_free(ret);
220 return NULL;
221}
Adam Langley95c29f32014-06-20 12:00:00 -0700222
223/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400224void ssl_cert_clear_certs(CERT *cert) {
225 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 return;
227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langley3509dac2017-02-01 11:59:18 -0800229 cert->x509_method->cert_clear(cert);
Adam Langleye1e78132017-01-31 15:24:31 -0800230
Adam Langley3a2b47a2017-01-24 13:59:42 -0800231 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
232 cert->chain = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400233 EVP_PKEY_free(cert->privatekey);
234 cert->privatekey = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400235 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800236}
Adam Langley95c29f32014-06-20 12:00:00 -0700237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238void ssl_cert_free(CERT *c) {
239 if (c == NULL) {
240 return;
241 }
Adam Langley95c29f32014-06-20 12:00:00 -0700242
David Benjamin2755a3e2015-04-22 16:17:58 -0400243 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ssl_cert_clear_certs(c);
David Benjamind246b812016-07-08 15:07:02 -0700246 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800247 X509_STORE_free(c->verify_store);
David Benjamin83a32122017-02-14 18:34:54 -0500248 CRYPTO_BUFFER_free(c->signed_cert_timestamp_list);
249 CRYPTO_BUFFER_free(c->ocsp_response);
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 OPENSSL_free(c);
252}
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamina2bda9f2016-12-03 23:29:05 -0500254static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
255 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 c->cert_cb = cb;
257 c->cert_cb_arg = arg;
258}
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley52940c42017-02-01 12:40:31 -0800260int ssl_set_cert(CERT *cert, CRYPTO_BUFFER *buffer) {
261 CBS cert_cbs;
262 CRYPTO_BUFFER_init_CBS(buffer, &cert_cbs);
263 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
264 if (pubkey == NULL) {
265 return 0;
266 }
267
268 if (!ssl_is_key_type_supported(pubkey->type)) {
269 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
270 EVP_PKEY_free(pubkey);
271 return 0;
272 }
273
274 /* An ECC certificate may be usable for ECDH or ECDSA. We only support ECDSA
275 * certificates, so sanity-check the key usage extension. */
276 if (pubkey->type == EVP_PKEY_EC &&
277 !ssl_cert_check_digital_signature_key_usage(&cert_cbs)) {
278 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
279 EVP_PKEY_free(pubkey);
280 return 0;
281 }
282
283 if (cert->privatekey != NULL) {
284 /* Sanity-check that the private key and the certificate match, unless the
285 * key is opaque (in case of, say, a smartcard). */
286 if (!EVP_PKEY_is_opaque(cert->privatekey) &&
287 !ssl_compare_public_and_private_key(pubkey, cert->privatekey)) {
288 /* don't fail for a cert/key mismatch, just free current private key
289 * (when switching to a different cert & key, first this function should
290 * be used, then ssl_set_pkey */
291 EVP_PKEY_free(cert->privatekey);
292 cert->privatekey = NULL;
293 /* clear error queue */
294 ERR_clear_error();
295 }
296 }
297
298 EVP_PKEY_free(pubkey);
299
300 cert->x509_method->cert_flush_cached_leaf(cert);
301
302 if (cert->chain != NULL) {
303 CRYPTO_BUFFER_free(sk_CRYPTO_BUFFER_value(cert->chain, 0));
304 sk_CRYPTO_BUFFER_set(cert->chain, 0, buffer);
305 CRYPTO_BUFFER_up_ref(buffer);
306 return 1;
307 }
308
309 cert->chain = sk_CRYPTO_BUFFER_new_null();
310 if (cert->chain == NULL) {
311 return 0;
312 }
313
314 if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
315 sk_CRYPTO_BUFFER_free(cert->chain);
316 cert->chain = NULL;
317 return 0;
318 }
319 CRYPTO_BUFFER_up_ref(buffer);
320
321 return 1;
322}
323
324int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
325 const uint8_t *der) {
326 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
327 if (buffer == NULL) {
328 return 0;
329 }
330
331 const int ok = ssl_set_cert(ctx->cert, buffer);
332 CRYPTO_BUFFER_free(buffer);
333 return ok;
334}
335
336int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
337 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(der, der_len, NULL);
338 if (buffer == NULL) {
339 return 0;
340 }
341
342 const int ok = ssl_set_cert(ssl->cert, buffer);
343 CRYPTO_BUFFER_free(buffer);
344 return ok;
345}
346
David Benjamin7aa31d62016-08-08 21:38:32 -0400347int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
348 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400349 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 return 0;
351 }
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyd323f4b2016-03-01 15:58:14 -0800353 X509_STORE *verify_store = ssl->ctx->cert_store;
354 if (ssl->cert->verify_store != NULL) {
355 verify_store = ssl->cert->verify_store;
356 }
357
David Benjamin306ece32015-09-17 13:46:22 -0400358 X509 *leaf = sk_X509_value(cert_chain, 0);
359 int ret = 0;
360 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800361 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400362 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 return 0;
364 }
David Benjamin306ece32015-09-17 13:46:22 -0400365 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
366 ssl)) {
367 goto err;
368 }
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 /* We need to inherit the verify parameters. These can be determined by the
371 * context: if its a server it will verify SSL client certificates or vice
372 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400373 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400376 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700377
David Benjamin306ece32015-09-17 13:46:22 -0400378 if (ssl->verify_callback) {
379 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 }
Adam Langley95c29f32014-06-20 12:00:00 -0700381
David Benjamine455e512016-08-01 20:11:13 -0400382 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400383 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400384 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 } else {
David Benjamine455e512016-08-01 20:11:13 -0400386 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 }
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleya6cd1852016-08-26 09:38:13 -0700389 *out_verify_result = ctx.error;
390
David Benjamine455e512016-08-01 20:11:13 -0400391 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
392 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400393 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400394 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
395 goto err;
396 }
397
398 ERR_clear_error();
399 ret = 1;
400
David Benjamin306ece32015-09-17 13:46:22 -0400401err:
402 X509_STORE_CTX_cleanup(&ctx);
403 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800404}
Adam Langley95c29f32014-06-20 12:00:00 -0700405
David Benjamin60da0cd2015-05-03 15:21:28 -0400406static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
407 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400408 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 *ca_list = name_list;
410}
Adam Langley95c29f32014-06-20 12:00:00 -0700411
David Benjamin59937042015-09-19 13:04:22 -0400412STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
413 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
414 if (ret == NULL) {
415 return NULL;
416 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800417
David Benjamin54091232016-09-05 12:47:25 -0400418 for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
David Benjamin59937042015-09-19 13:04:22 -0400419 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400421 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 sk_X509_NAME_pop_free(ret, X509_NAME_free);
423 return NULL;
424 }
425 }
426
427 return ret;
428}
429
David Benjamin59937042015-09-19 13:04:22 -0400430void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
431 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800432}
433
David Benjamin60da0cd2015-05-03 15:21:28 -0400434void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400435 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800436}
437
David Benjamin60da0cd2015-05-03 15:21:28 -0400438STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 return ctx->client_CA;
440}
441
David Benjamin1d128f32015-09-08 17:41:40 -0400442STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
443 /* For historical reasons, this function is used both to query configuration
444 * state on a server as well as handshake state on a client. However, whether
445 * |ssl| is a client or server is not known until explicitly configured with
446 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
447 * indeterminate mode and |ssl->server| is unset. */
448 if (ssl->handshake_func != NULL && !ssl->server) {
David Benjamina0486782016-10-06 19:11:32 -0400449 if (ssl->s3->hs != NULL) {
450 return ssl->s3->hs->ca_names;
451 }
452
453 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800454 }
David Benjamin1d128f32015-09-08 17:41:40 -0400455
456 if (ssl->client_CA != NULL) {
457 return ssl->client_CA;
458 }
459 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800460}
461
David Benjamin59937042015-09-19 13:04:22 -0400462static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 X509_NAME *name;
464
David Benjamin59937042015-09-19 13:04:22 -0400465 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 return 0;
467 }
468 if (*sk == NULL) {
469 *sk = sk_X509_NAME_new_null();
470 if (*sk == NULL) {
471 return 0;
472 }
473 }
474
David Benjamin59937042015-09-19 13:04:22 -0400475 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 if (name == NULL) {
477 return 0;
478 }
479
480 if (!sk_X509_NAME_push(*sk, name)) {
481 X509_NAME_free(name);
482 return 0;
483 }
484
485 return 1;
486}
487
David Benjamin59937042015-09-19 13:04:22 -0400488int SSL_add_client_CA(SSL *ssl, X509 *x509) {
489 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800490}
491
David Benjamin59937042015-09-19 13:04:22 -0400492int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
493 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800494}
495
David Benjamin32a66d52016-07-13 22:03:11 -0400496int ssl_has_certificate(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800497 return ssl->cert->chain != NULL &&
498 sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
499 ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400500}
501
Adam Langley68e71242016-12-12 11:06:16 -0800502STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
Adam Langleyd5157222016-12-12 11:37:43 -0800503 EVP_PKEY **out_pubkey,
Adam Langley68e71242016-12-12 11:06:16 -0800504 uint8_t *out_leaf_sha256,
Adam Langleyd519bf62016-12-12 11:16:44 -0800505 CBS *cbs,
506 CRYPTO_BUFFER_POOL *pool) {
Adam Langleyd5157222016-12-12 11:37:43 -0800507 *out_pubkey = NULL;
508
Adam Langley68e71242016-12-12 11:06:16 -0800509 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800510 if (ret == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -0400511 *out_alert = SSL_AD_INTERNAL_ERROR;
512 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800513 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400514 }
515
David Benjamin5c900c82016-07-13 23:03:26 -0400516 CBS certificate_list;
517 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
518 *out_alert = SSL_AD_DECODE_ERROR;
519 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
520 goto err;
521 }
522
523 while (CBS_len(&certificate_list) > 0) {
524 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800525 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
526 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400527 *out_alert = SSL_AD_DECODE_ERROR;
528 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
529 goto err;
530 }
531
Adam Langleyd5157222016-12-12 11:37:43 -0800532 if (sk_CRYPTO_BUFFER_num(ret) == 0) {
533 *out_pubkey = ssl_cert_parse_pubkey(&certificate);
534 if (*out_pubkey == NULL) {
Adam Langley7dccc712017-02-08 08:51:25 -0800535 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyd5157222016-12-12 11:37:43 -0800536 goto err;
537 }
538
539 /* Retain the hash of the leaf certificate if requested. */
540 if (out_leaf_sha256 != NULL) {
541 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
542 }
David Benjamin5c900c82016-07-13 23:03:26 -0400543 }
544
Adam Langleyd519bf62016-12-12 11:16:44 -0800545 CRYPTO_BUFFER *buf =
546 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800547 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800548 *out_alert = SSL_AD_DECODE_ERROR;
549 goto err;
550 }
Adam Langley68e71242016-12-12 11:06:16 -0800551
552 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400553 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800554 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400555 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
556 goto err;
557 }
David Benjamin5c900c82016-07-13 23:03:26 -0400558 }
559
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800560 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400561
562err:
Adam Langleyd5157222016-12-12 11:37:43 -0800563 EVP_PKEY_free(*out_pubkey);
564 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800565 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800566 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400567}
568
David Benjamin75836432016-06-17 18:48:29 -0400569int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400570 if (!ssl_has_certificate(ssl)) {
571 return CBB_add_u24(cbb, 0);
572 }
573
Adam Langley3a2b47a2017-01-24 13:59:42 -0800574 CBB certs;
575 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
576 goto err;
David Benjamin75836432016-06-17 18:48:29 -0400577 }
578
Adam Langley3a2b47a2017-01-24 13:59:42 -0800579 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
580 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
581 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
582 CBB child;
583 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
584 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
585 CRYPTO_BUFFER_len(buffer)) ||
586 !CBB_flush(&certs)) {
587 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 }
589 }
590
David Benjamin75836432016-06-17 18:48:29 -0400591 return CBB_flush(cbb);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800592
593err:
594 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
595 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800596}
Adam Langley95c29f32014-06-20 12:00:00 -0700597
Adam Langley05672202016-12-13 12:05:49 -0800598/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
599 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
600 * subjectPublicKeyInfo. */
601static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800602 /* From RFC 5280, section 4.1
603 * Certificate ::= SEQUENCE {
604 * tbsCertificate TBSCertificate,
605 * signatureAlgorithm AlgorithmIdentifier,
606 * signatureValue BIT STRING }
607
608 * TBSCertificate ::= SEQUENCE {
609 * version [0] EXPLICIT Version DEFAULT v1,
610 * serialNumber CertificateSerialNumber,
611 * signature AlgorithmIdentifier,
612 * issuer Name,
613 * validity Validity,
614 * subject Name,
615 * subjectPublicKeyInfo SubjectPublicKeyInfo,
616 * ... } */
617 CBS buf = *in;
618
Adam Langley05672202016-12-13 12:05:49 -0800619 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800620 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
621 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800622 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800623 /* version */
624 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800625 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800626 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
627 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800628 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800629 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800630 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800631 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800632 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800633 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800634 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800635 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800636 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
637 return 0;
638 }
639
640 return 1;
641}
642
643EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500644 CBS buf = *in, tbs_cert;
645 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800646 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
647 return NULL;
648 }
649
David Benjamin5b410b62017-01-03 08:08:57 -0500650 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800651}
652
Adam Langleyc26692c2017-01-25 09:34:42 -0800653int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
654 const EVP_PKEY *privkey) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800655 int ret = 0;
656
657 switch (EVP_PKEY_cmp(pubkey, privkey)) {
658 case 1:
659 ret = 1;
660 break;
661 case 0:
662 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
663 break;
664 case -1:
665 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
666 break;
667 case -2:
668 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
669 default:
670 assert(0);
671 break;
672 }
673
Adam Langley3a2b47a2017-01-24 13:59:42 -0800674 return ret;
675}
676
677int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
678 if (privkey == NULL) {
679 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
680 return 0;
681 }
682
683 if (cert->chain == NULL ||
684 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
685 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
686 return 0;
687 }
688
Adam Langleyc26692c2017-01-25 09:34:42 -0800689 CBS cert_cbs;
690 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
691 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
692 if (!pubkey) {
693 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
694 return 0;
695 }
696
697 const int ok = ssl_compare_public_and_private_key(pubkey, privkey);
698 EVP_PKEY_free(pubkey);
699 return ok;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800700}
701
Adam Langley05672202016-12-13 12:05:49 -0800702int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
703 CBS buf = *in;
704
705 CBS tbs_cert, outer_extensions;
706 int has_extensions;
707 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
708 /* subjectPublicKeyInfo */
709 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
710 /* issuerUniqueID */
711 !CBS_get_optional_asn1(
712 &tbs_cert, NULL, NULL,
713 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
714 /* subjectUniqueID */
715 !CBS_get_optional_asn1(
716 &tbs_cert, NULL, NULL,
717 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
718 !CBS_get_optional_asn1(
719 &tbs_cert, &outer_extensions, &has_extensions,
720 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
721 goto parse_err;
722 }
723
724 if (!has_extensions) {
725 return 1;
726 }
727
728 CBS extensions;
729 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
730 goto parse_err;
731 }
732
733 while (CBS_len(&extensions) > 0) {
734 CBS extension, oid, contents;
735 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
736 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
737 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
738 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
739 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
740 CBS_len(&extension) != 0) {
741 goto parse_err;
742 }
743
744 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
745 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500746 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
747 0) {
Adam Langley05672202016-12-13 12:05:49 -0800748 continue;
749 }
750
751 CBS bit_string;
752 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
753 CBS_len(&contents) != 0) {
754 goto parse_err;
755 }
756
757 /* This is the KeyUsage extension. See
758 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
759 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
760 goto parse_err;
761 }
762
763 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
764 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
765 return 0;
766 }
767
768 return 1;
769 }
770
771 /* No KeyUsage extension found. */
772 return 1;
773
774parse_err:
775 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
776 return 0;
777}
778
David Benjamine0332e82016-07-13 22:40:36 -0400779static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
780 return X509_NAME_cmp(*a, *b);
781}
782
783STACK_OF(X509_NAME) *
784 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
785 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
786 X509_NAME *name = NULL;
787 if (ret == NULL) {
788 *out_alert = SSL_AD_INTERNAL_ERROR;
789 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
790 return NULL;
791 }
792
793 CBS child;
794 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
795 *out_alert = SSL_AD_DECODE_ERROR;
796 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
797 goto err;
798 }
799
800 while (CBS_len(&child) > 0) {
801 CBS distinguished_name;
802 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
803 *out_alert = SSL_AD_DECODE_ERROR;
804 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
805 goto err;
806 }
807
808 const uint8_t *ptr = CBS_data(&distinguished_name);
809 /* A u16 length cannot overflow a long. */
810 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
811 if (name == NULL ||
812 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
813 *out_alert = SSL_AD_DECODE_ERROR;
814 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
815 goto err;
816 }
817
818 if (!sk_X509_NAME_push(ret, name)) {
819 *out_alert = SSL_AD_INTERNAL_ERROR;
820 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
821 goto err;
822 }
823 name = NULL;
824 }
825
826 return ret;
827
828err:
829 X509_NAME_free(name);
830 sk_X509_NAME_pop_free(ret, X509_NAME_free);
831 return NULL;
832}
833
David Benjamin32a66d52016-07-13 22:03:11 -0400834int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
835 CBB child, name_cbb;
836 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
837 return 0;
838 }
839
840 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
841 if (sk == NULL) {
842 return CBB_flush(cbb);
843 }
844
845 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
846 X509_NAME *name = sk_X509_NAME_value(sk, i);
847 int len = i2d_X509_NAME(name, NULL);
848 if (len < 0) {
849 return 0;
850 }
851 uint8_t *ptr;
852 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
853 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
854 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
855 return 0;
856 }
857 }
858
859 return CBB_flush(cbb);
860}
861
Adam Langleyd323f4b2016-03-01 15:58:14 -0800862static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
863 X509_STORE_free(*store_ptr);
864 *store_ptr = new_store;
865
866 if (new_store != NULL && take_ref) {
867 X509_STORE_up_ref(new_store);
868 }
869
870 return 1;
871}
872
873int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
874 return set_cert_store(&ctx->cert->verify_store, store, 0);
875}
876
877int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
878 return set_cert_store(&ctx->cert->verify_store, store, 1);
879}
880
881int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
882 return set_cert_store(&ssl->cert->verify_store, store, 0);
883}
884
885int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
886 return set_cert_store(&ssl->cert->verify_store, store, 1);
887}
888
David Benjamina2bda9f2016-12-03 23:29:05 -0500889void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
890 void *arg) {
891 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
892}
893
894void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
895 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
896}
897
Adam Langley05672202016-12-13 12:05:49 -0800898int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey,
899 const CRYPTO_BUFFER *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -0400900 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
901
Steven Valdezbf5aa842016-07-15 07:07:40 -0400902 /* Check the certificate's type matches the cipher. */
903 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
904 int expected_type = ssl_cipher_get_key_type(cipher);
905 assert(expected_type != EVP_PKEY_NONE);
906 if (pkey->type != expected_type) {
907 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -0800908 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400909 }
910
911 if (cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -0800912 CBS leaf_cbs;
913 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
914 /* ECDSA and ECDH certificates use the same public key format. Instead,
915 * they are distinguished by the key usage extension in the certificate. */
916 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
917 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400918 }
919
David Benjamin938fa7c2016-10-07 00:27:05 -0400920 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
921 if (ec_key == NULL) {
922 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800923 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -0400924 }
925
926 /* Check the key's group and point format are acceptable. */
927 uint16_t group_id;
928 if (!ssl_nid_to_group_id(
929 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
930 !tls1_check_group_id(ssl, group_id) ||
931 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400932 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800933 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400934 }
935 }
936
Adam Langley05672202016-12-13 12:05:49 -0800937 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400938}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500939
940static int do_client_cert_cb(SSL *ssl, void *arg) {
941 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
942 return 1;
943 }
944
945 X509 *x509 = NULL;
946 EVP_PKEY *pkey = NULL;
947 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
948 if (ret < 0) {
949 return -1;
950 }
951
952 if (ret != 0) {
953 if (!SSL_use_certificate(ssl, x509) ||
954 !SSL_use_PrivateKey(ssl, pkey)) {
955 return 0;
956 }
957 }
958
959 X509_free(x509);
960 EVP_PKEY_free(pkey);
961 return 1;
962}
963
964void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
965 X509 **out_x509,
966 EVP_PKEY **out_pkey)) {
967 /* Emulate the old client certificate callback with the new one. */
968 SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
969 ctx->client_cert_cb = cb;
970}
David Benjamin83a32122017-02-14 18:34:54 -0500971
972static int set_signed_cert_timestamp_list(CERT *cert, const uint8_t *list,
973 size_t list_len) {
974 CBS sct_list;
975 CBS_init(&sct_list, list, list_len);
976 if (!ssl_is_sct_list_valid(&sct_list)) {
977 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
978 return 0;
979 }
980
981 CRYPTO_BUFFER_free(cert->signed_cert_timestamp_list);
982 cert->signed_cert_timestamp_list =
983 CRYPTO_BUFFER_new(CBS_data(&sct_list), CBS_len(&sct_list), NULL);
984 return cert->signed_cert_timestamp_list != NULL;
985}
986
987int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
988 size_t list_len) {
989 return set_signed_cert_timestamp_list(ctx->cert, list, list_len);
990}
991
992int SSL_set_signed_cert_timestamp_list(SSL *ssl, const uint8_t *list,
993 size_t list_len) {
994 return set_signed_cert_timestamp_list(ssl->cert, list, list_len);
995}
996
997int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
998 size_t response_len) {
999 CRYPTO_BUFFER_free(ctx->cert->ocsp_response);
1000 ctx->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
1001 return ctx->cert->ocsp_response != NULL;
1002}
1003
1004int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
1005 size_t response_len) {
1006 CRYPTO_BUFFER_free(ssl->cert->ocsp_response);
1007 ssl->cert->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
1008 return ssl->cert->ocsp_response != NULL;
1009}