blob: 52a386bd8aedff5f44ce0b0c5043e06e0ec1b1dc [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 Langleyfcf25832014-12-18 17:42:32 -0800144CERT *ssl_cert_new(void) {
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 Langley95c29f32014-06-20 12:00:00 -0700151
Adam Langleyfcf25832014-12-18 17:42:32 -0800152 return ret;
153}
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langley3a2b47a2017-01-24 13:59:42 -0800155static CRYPTO_BUFFER *buffer_up_ref(CRYPTO_BUFFER *buffer) {
156 CRYPTO_BUFFER_up_ref(buffer);
157 return buffer;
158}
159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160CERT *ssl_cert_dup(CERT *cert) {
Brian Smith5ba06892016-02-07 09:36:04 -1000161 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800162 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400163 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 return NULL;
165 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500166 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700167
Adam Langley3a2b47a2017-01-24 13:59:42 -0800168 ret->chain = sk_CRYPTO_BUFFER_deep_copy(cert->chain, buffer_up_ref,
169 CRYPTO_BUFFER_free);
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjamind1d80782015-07-05 11:54:09 -0400171 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700172 EVP_PKEY_up_ref(cert->privatekey);
173 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030176 ret->key_method = cert->key_method;
177
David Benjamin0fc37ef2016-08-17 15:29:46 -0400178 if (cert->dh_tmp != NULL) {
179 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
180 if (ret->dh_tmp == NULL) {
181 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
182 goto err;
183 }
184 }
185 ret->dh_tmp_cb = cert->dh_tmp_cb;
186
187 if (cert->sigalgs != NULL) {
188 ret->sigalgs =
189 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
190 if (ret->sigalgs == NULL) {
191 goto err;
192 }
193 }
194 ret->num_sigalgs = cert->num_sigalgs;
195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 ret->cert_cb = cert->cert_cb;
197 ret->cert_cb_arg = cert->cert_cb_arg;
198
Adam Langleyd323f4b2016-03-01 15:58:14 -0800199 if (cert->verify_store != NULL) {
200 X509_STORE_up_ref(cert->verify_store);
201 ret->verify_store = cert->verify_store;
202 }
203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 return ret;
205
Adam Langley95c29f32014-06-20 12:00:00 -0700206err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800207 ssl_cert_free(ret);
208 return NULL;
209}
Adam Langley95c29f32014-06-20 12:00:00 -0700210
Adam Langley3a2b47a2017-01-24 13:59:42 -0800211void ssl_cert_flush_cached_x509_leaf(CERT *cert) {
212 X509_free(cert->x509_leaf);
213 cert->x509_leaf = NULL;
214}
215
216static void ssl_cert_flush_cached_x509_chain(CERT *cert) {
217 sk_X509_pop_free(cert->x509_chain, X509_free);
218 cert->x509_chain = NULL;
219}
220
Adam Langley95c29f32014-06-20 12:00:00 -0700221/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400222void ssl_cert_clear_certs(CERT *cert) {
223 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 return;
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley3a2b47a2017-01-24 13:59:42 -0800227 ssl_cert_flush_cached_x509_leaf(cert);
228 ssl_cert_flush_cached_x509_chain(cert);
229
230 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
231 cert->chain = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400232 EVP_PKEY_free(cert->privatekey);
233 cert->privatekey = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400234 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800235}
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langleyfcf25832014-12-18 17:42:32 -0800237void ssl_cert_free(CERT *c) {
238 if (c == NULL) {
239 return;
240 }
Adam Langley95c29f32014-06-20 12:00:00 -0700241
David Benjamin2755a3e2015-04-22 16:17:58 -0400242 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 ssl_cert_clear_certs(c);
David Benjamind246b812016-07-08 15:07:02 -0700245 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800246 X509_STORE_free(c->verify_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 OPENSSL_free(c);
249}
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langley3a2b47a2017-01-24 13:59:42 -0800251/* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */
252static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) {
253 STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null();
254 if (chain == NULL) {
255 return NULL;
256 }
257
258 if (!sk_CRYPTO_BUFFER_push(chain, NULL)) {
259 sk_CRYPTO_BUFFER_free(chain);
260 return NULL;
261 }
262
263 return chain;
264}
265
266/* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised
267 * contents of |x509|. */
268CRYPTO_BUFFER *x509_to_buffer(X509 *x509) {
269 uint8_t *buf = NULL;
270 int cert_len = i2d_X509(x509, &buf);
271 if (cert_len <= 0) {
272 return 0;
273 }
274
275 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL);
276 OPENSSL_free(buf);
277
278 return buffer;
279}
280
281/* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised
282 * forms of elements of |chain|. It returns one on success or zero on error, in
283 * which case no change to |cert->chain| is made. It preverses the existing
284 * leaf from |cert->chain|, if any. */
285static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) {
286 STACK_OF(CRYPTO_BUFFER) *new_chain = NULL;
287
288 if (cert->chain != NULL) {
289 new_chain = sk_CRYPTO_BUFFER_new_null();
290 if (new_chain == NULL) {
291 return 0;
292 }
293
294 CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
295 if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) {
296 goto err;
297 }
298 CRYPTO_BUFFER_up_ref(leaf);
299 }
300
301 for (size_t i = 0; i < sk_X509_num(chain); i++) {
302 if (new_chain == NULL) {
303 new_chain = new_leafless_chain();
304 if (new_chain == NULL) {
305 goto err;
306 }
307 }
308
309 CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i));
310 if (buffer == NULL ||
311 !sk_CRYPTO_BUFFER_push(new_chain, buffer)) {
312 CRYPTO_BUFFER_free(buffer);
313 goto err;
314 }
315 }
316
317 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
318 cert->chain = new_chain;
319
320 return 1;
321
322err:
323 sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free);
324 return 0;
325}
326
David Benjamina2bda9f2016-12-03 23:29:05 -0500327static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800328 if (!ssl_cert_set_chain(cert, chain)) {
329 return 0;
330 }
331
332 sk_X509_pop_free(chain, X509_free);
333 ssl_cert_flush_cached_x509_chain(cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 return 1;
335}
Adam Langley95c29f32014-06-20 12:00:00 -0700336
David Benjamina2bda9f2016-12-03 23:29:05 -0500337static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800338 if (!ssl_cert_set_chain(cert, chain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 return 0;
340 }
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langley3a2b47a2017-01-24 13:59:42 -0800342 ssl_cert_flush_cached_x509_chain(cert);
343 return 1;
344}
345
346static int ssl_cert_append_cert(CERT *cert, X509 *x509) {
347 CRYPTO_BUFFER *buffer = x509_to_buffer(x509);
348 if (buffer == NULL) {
349 return 0;
350 }
351
352 if (cert->chain != NULL) {
353 if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
354 CRYPTO_BUFFER_free(buffer);
355 return 0;
356 }
357
358 return 1;
359 }
360
361 cert->chain = new_leafless_chain();
362 if (cert->chain == NULL ||
363 !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
364 CRYPTO_BUFFER_free(buffer);
365 sk_CRYPTO_BUFFER_free(cert->chain);
366 cert->chain = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800367 return 0;
368 }
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 return 1;
371}
Adam Langley95c29f32014-06-20 12:00:00 -0700372
David Benjamina2bda9f2016-12-03 23:29:05 -0500373static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800374 if (!ssl_cert_append_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 return 0;
376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langley3a2b47a2017-01-24 13:59:42 -0800378 X509_free(x509);
379 ssl_cert_flush_cached_x509_chain(cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return 1;
381}
Adam Langley95c29f32014-06-20 12:00:00 -0700382
David Benjamina2bda9f2016-12-03 23:29:05 -0500383static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800384 if (!ssl_cert_append_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800385 return 0;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley3a2b47a2017-01-24 13:59:42 -0800388 ssl_cert_flush_cached_x509_chain(cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 return 1;
390}
Adam Langley95c29f32014-06-20 12:00:00 -0700391
David Benjamina2bda9f2016-12-03 23:29:05 -0500392static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
393 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 c->cert_cb = cb;
395 c->cert_cb_arg = arg;
396}
Adam Langley95c29f32014-06-20 12:00:00 -0700397
David Benjamin7aa31d62016-08-08 21:38:32 -0400398int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
399 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400400 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 return 0;
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyd323f4b2016-03-01 15:58:14 -0800404 X509_STORE *verify_store = ssl->ctx->cert_store;
405 if (ssl->cert->verify_store != NULL) {
406 verify_store = ssl->cert->verify_store;
407 }
408
David Benjamin306ece32015-09-17 13:46:22 -0400409 X509 *leaf = sk_X509_value(cert_chain, 0);
410 int ret = 0;
411 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800412 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400413 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 return 0;
415 }
David Benjamin306ece32015-09-17 13:46:22 -0400416 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
417 ssl)) {
418 goto err;
419 }
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 /* We need to inherit the verify parameters. These can be determined by the
422 * context: if its a server it will verify SSL client certificates or vice
423 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400424 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400427 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700428
David Benjamin306ece32015-09-17 13:46:22 -0400429 if (ssl->verify_callback) {
430 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 }
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjamine455e512016-08-01 20:11:13 -0400433 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400434 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400435 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 } else {
David Benjamine455e512016-08-01 20:11:13 -0400437 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 }
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langleya6cd1852016-08-26 09:38:13 -0700440 *out_verify_result = ctx.error;
441
David Benjamine455e512016-08-01 20:11:13 -0400442 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
443 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400444 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400445 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
446 goto err;
447 }
448
449 ERR_clear_error();
450 ret = 1;
451
David Benjamin306ece32015-09-17 13:46:22 -0400452err:
453 X509_STORE_CTX_cleanup(&ctx);
454 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800455}
Adam Langley95c29f32014-06-20 12:00:00 -0700456
David Benjamin60da0cd2015-05-03 15:21:28 -0400457static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
458 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400459 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 *ca_list = name_list;
461}
Adam Langley95c29f32014-06-20 12:00:00 -0700462
David Benjamin59937042015-09-19 13:04:22 -0400463STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
464 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
465 if (ret == NULL) {
466 return NULL;
467 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800468
David Benjamin54091232016-09-05 12:47:25 -0400469 for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
David Benjamin59937042015-09-19 13:04:22 -0400470 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400472 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 sk_X509_NAME_pop_free(ret, X509_NAME_free);
474 return NULL;
475 }
476 }
477
478 return ret;
479}
480
David Benjamin59937042015-09-19 13:04:22 -0400481void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
482 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800483}
484
David Benjamin60da0cd2015-05-03 15:21:28 -0400485void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400486 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800487}
488
David Benjamin60da0cd2015-05-03 15:21:28 -0400489STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 return ctx->client_CA;
491}
492
David Benjamin1d128f32015-09-08 17:41:40 -0400493STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
494 /* For historical reasons, this function is used both to query configuration
495 * state on a server as well as handshake state on a client. However, whether
496 * |ssl| is a client or server is not known until explicitly configured with
497 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
498 * indeterminate mode and |ssl->server| is unset. */
499 if (ssl->handshake_func != NULL && !ssl->server) {
David Benjamina0486782016-10-06 19:11:32 -0400500 if (ssl->s3->hs != NULL) {
501 return ssl->s3->hs->ca_names;
502 }
503
504 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 }
David Benjamin1d128f32015-09-08 17:41:40 -0400506
507 if (ssl->client_CA != NULL) {
508 return ssl->client_CA;
509 }
510 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800511}
512
David Benjamin59937042015-09-19 13:04:22 -0400513static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 X509_NAME *name;
515
David Benjamin59937042015-09-19 13:04:22 -0400516 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 return 0;
518 }
519 if (*sk == NULL) {
520 *sk = sk_X509_NAME_new_null();
521 if (*sk == NULL) {
522 return 0;
523 }
524 }
525
David Benjamin59937042015-09-19 13:04:22 -0400526 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 if (name == NULL) {
528 return 0;
529 }
530
531 if (!sk_X509_NAME_push(*sk, name)) {
532 X509_NAME_free(name);
533 return 0;
534 }
535
536 return 1;
537}
538
David Benjamin59937042015-09-19 13:04:22 -0400539int SSL_add_client_CA(SSL *ssl, X509 *x509) {
540 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800541}
542
David Benjamin59937042015-09-19 13:04:22 -0400543int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
544 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800545}
546
David Benjamin32a66d52016-07-13 22:03:11 -0400547int ssl_has_certificate(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800548 return ssl->cert->chain != NULL &&
549 sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
550 ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400551}
552
Adam Langley68e71242016-12-12 11:06:16 -0800553STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
Adam Langleyd5157222016-12-12 11:37:43 -0800554 EVP_PKEY **out_pubkey,
Adam Langley68e71242016-12-12 11:06:16 -0800555 uint8_t *out_leaf_sha256,
Adam Langleyd519bf62016-12-12 11:16:44 -0800556 CBS *cbs,
557 CRYPTO_BUFFER_POOL *pool) {
Adam Langleyd5157222016-12-12 11:37:43 -0800558 *out_pubkey = NULL;
559
Adam Langley68e71242016-12-12 11:06:16 -0800560 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800561 if (ret == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -0400562 *out_alert = SSL_AD_INTERNAL_ERROR;
563 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800564 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400565 }
566
David Benjamin5c900c82016-07-13 23:03:26 -0400567 CBS certificate_list;
568 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
569 *out_alert = SSL_AD_DECODE_ERROR;
570 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
571 goto err;
572 }
573
574 while (CBS_len(&certificate_list) > 0) {
575 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800576 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
577 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400578 *out_alert = SSL_AD_DECODE_ERROR;
579 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
580 goto err;
581 }
582
Adam Langleyd5157222016-12-12 11:37:43 -0800583 if (sk_CRYPTO_BUFFER_num(ret) == 0) {
584 *out_pubkey = ssl_cert_parse_pubkey(&certificate);
585 if (*out_pubkey == NULL) {
586 goto err;
587 }
588
589 /* Retain the hash of the leaf certificate if requested. */
590 if (out_leaf_sha256 != NULL) {
591 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
592 }
David Benjamin5c900c82016-07-13 23:03:26 -0400593 }
594
Adam Langleyd519bf62016-12-12 11:16:44 -0800595 CRYPTO_BUFFER *buf =
596 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800597 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800598 *out_alert = SSL_AD_DECODE_ERROR;
599 goto err;
600 }
Adam Langley68e71242016-12-12 11:06:16 -0800601
602 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400603 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800604 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400605 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
606 goto err;
607 }
David Benjamin5c900c82016-07-13 23:03:26 -0400608 }
609
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800610 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400611
612err:
Adam Langleyd5157222016-12-12 11:37:43 -0800613 EVP_PKEY_free(*out_pubkey);
614 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800615 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800616 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400617}
618
David Benjamin75836432016-06-17 18:48:29 -0400619int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400620 if (!ssl_has_certificate(ssl)) {
621 return CBB_add_u24(cbb, 0);
622 }
623
Adam Langley3a2b47a2017-01-24 13:59:42 -0800624 CBB certs;
625 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
626 goto err;
David Benjamin75836432016-06-17 18:48:29 -0400627 }
628
Adam Langley3a2b47a2017-01-24 13:59:42 -0800629 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
630 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
631 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
632 CBB child;
633 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
634 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
635 CRYPTO_BUFFER_len(buffer)) ||
636 !CBB_flush(&certs)) {
637 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 }
639 }
640
David Benjamin75836432016-06-17 18:48:29 -0400641 return CBB_flush(cbb);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800642
643err:
644 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
645 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800646}
Adam Langley95c29f32014-06-20 12:00:00 -0700647
David Benjamin650aa1c2016-12-20 18:55:16 -0500648int ssl_auto_chain_if_needed(SSL *ssl) {
649 /* Only build a chain if there are no intermediates configured and the feature
650 * isn't disabled. */
651 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
652 !ssl_has_certificate(ssl) ||
Adam Langley3a2b47a2017-01-24 13:59:42 -0800653 ssl->cert->chain == NULL ||
654 sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
David Benjamin650aa1c2016-12-20 18:55:16 -0500655 return 1;
656 }
657
Adam Langley3a2b47a2017-01-24 13:59:42 -0800658 X509 *leaf =
659 X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
660 if (!leaf) {
661 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
662 return 0;
663 }
664
David Benjamin650aa1c2016-12-20 18:55:16 -0500665 X509_STORE_CTX ctx;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800666 if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
667 X509_free(leaf);
David Benjamin650aa1c2016-12-20 18:55:16 -0500668 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
669 return 0;
670 }
671
672 /* Attempt to build a chain, ignoring the result. */
673 X509_verify_cert(&ctx);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800674 X509_free(leaf);
David Benjamin650aa1c2016-12-20 18:55:16 -0500675 ERR_clear_error();
676
Adam Langley3a2b47a2017-01-24 13:59:42 -0800677 /* Remove the leaf from the generated chain. */
678 X509_free(sk_X509_shift(ctx.chain));
679
680 const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
681 X509_STORE_CTX_cleanup(&ctx);
682 if (!ok) {
683 return 0;
David Benjamin650aa1c2016-12-20 18:55:16 -0500684 }
685
Adam Langley3a2b47a2017-01-24 13:59:42 -0800686 ssl_cert_flush_cached_x509_chain(ssl->cert);
687
David Benjamin650aa1c2016-12-20 18:55:16 -0500688 return 1;
689}
690
Adam Langley05672202016-12-13 12:05:49 -0800691/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
692 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
693 * subjectPublicKeyInfo. */
694static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800695 /* From RFC 5280, section 4.1
696 * Certificate ::= SEQUENCE {
697 * tbsCertificate TBSCertificate,
698 * signatureAlgorithm AlgorithmIdentifier,
699 * signatureValue BIT STRING }
700
701 * TBSCertificate ::= SEQUENCE {
702 * version [0] EXPLICIT Version DEFAULT v1,
703 * serialNumber CertificateSerialNumber,
704 * signature AlgorithmIdentifier,
705 * issuer Name,
706 * validity Validity,
707 * subject Name,
708 * subjectPublicKeyInfo SubjectPublicKeyInfo,
709 * ... } */
710 CBS buf = *in;
711
Adam Langley05672202016-12-13 12:05:49 -0800712 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800713 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
714 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800715 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800716 /* version */
717 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800718 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800719 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
720 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800721 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800722 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800723 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800724 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800725 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800726 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800727 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800728 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800729 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
730 return 0;
731 }
732
733 return 1;
734}
735
736EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500737 CBS buf = *in, tbs_cert;
738 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800739 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
740 return NULL;
741 }
742
David Benjamin5b410b62017-01-03 08:08:57 -0500743 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800744}
745
Adam Langley3a2b47a2017-01-24 13:59:42 -0800746static int ssl_check_cert_and_private_key_match(const CRYPTO_BUFFER *cert,
747 const EVP_PKEY *privkey) {
748 CBS cert_cbs;
749 CRYPTO_BUFFER_init_CBS(cert, &cert_cbs);
750 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
751 if (!pubkey) {
752 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
753 return 0;
754 }
755
756 int ret = 0;
757
758 switch (EVP_PKEY_cmp(pubkey, privkey)) {
759 case 1:
760 ret = 1;
761 break;
762 case 0:
763 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
764 break;
765 case -1:
766 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
767 break;
768 case -2:
769 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
770 default:
771 assert(0);
772 break;
773 }
774
775 EVP_PKEY_free(pubkey);
776 return ret;
777}
778
779int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
780 if (privkey == NULL) {
781 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
782 return 0;
783 }
784
785 if (cert->chain == NULL ||
786 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
787 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
788 return 0;
789 }
790
791 return ssl_check_cert_and_private_key_match(
792 sk_CRYPTO_BUFFER_value(cert->chain, 0), privkey);
793}
794
Adam Langley05672202016-12-13 12:05:49 -0800795int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
796 CBS buf = *in;
797
798 CBS tbs_cert, outer_extensions;
799 int has_extensions;
800 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
801 /* subjectPublicKeyInfo */
802 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
803 /* issuerUniqueID */
804 !CBS_get_optional_asn1(
805 &tbs_cert, NULL, NULL,
806 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
807 /* subjectUniqueID */
808 !CBS_get_optional_asn1(
809 &tbs_cert, NULL, NULL,
810 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
811 !CBS_get_optional_asn1(
812 &tbs_cert, &outer_extensions, &has_extensions,
813 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
814 goto parse_err;
815 }
816
817 if (!has_extensions) {
818 return 1;
819 }
820
821 CBS extensions;
822 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
823 goto parse_err;
824 }
825
826 while (CBS_len(&extensions) > 0) {
827 CBS extension, oid, contents;
828 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
829 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
830 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
831 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
832 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
833 CBS_len(&extension) != 0) {
834 goto parse_err;
835 }
836
837 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
838 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500839 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
840 0) {
Adam Langley05672202016-12-13 12:05:49 -0800841 continue;
842 }
843
844 CBS bit_string;
845 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
846 CBS_len(&contents) != 0) {
847 goto parse_err;
848 }
849
850 /* This is the KeyUsage extension. See
851 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
852 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
853 goto parse_err;
854 }
855
856 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
857 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
858 return 0;
859 }
860
861 return 1;
862 }
863
864 /* No KeyUsage extension found. */
865 return 1;
866
867parse_err:
868 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
869 return 0;
870}
871
David Benjamine0332e82016-07-13 22:40:36 -0400872static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
873 return X509_NAME_cmp(*a, *b);
874}
875
876STACK_OF(X509_NAME) *
877 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
878 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
879 X509_NAME *name = NULL;
880 if (ret == NULL) {
881 *out_alert = SSL_AD_INTERNAL_ERROR;
882 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
883 return NULL;
884 }
885
886 CBS child;
887 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
888 *out_alert = SSL_AD_DECODE_ERROR;
889 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
890 goto err;
891 }
892
893 while (CBS_len(&child) > 0) {
894 CBS distinguished_name;
895 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
896 *out_alert = SSL_AD_DECODE_ERROR;
897 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
898 goto err;
899 }
900
901 const uint8_t *ptr = CBS_data(&distinguished_name);
902 /* A u16 length cannot overflow a long. */
903 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
904 if (name == NULL ||
905 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
906 *out_alert = SSL_AD_DECODE_ERROR;
907 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
908 goto err;
909 }
910
911 if (!sk_X509_NAME_push(ret, name)) {
912 *out_alert = SSL_AD_INTERNAL_ERROR;
913 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
914 goto err;
915 }
916 name = NULL;
917 }
918
919 return ret;
920
921err:
922 X509_NAME_free(name);
923 sk_X509_NAME_pop_free(ret, X509_NAME_free);
924 return NULL;
925}
926
David Benjamin32a66d52016-07-13 22:03:11 -0400927int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
928 CBB child, name_cbb;
929 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
930 return 0;
931 }
932
933 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
934 if (sk == NULL) {
935 return CBB_flush(cbb);
936 }
937
938 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
939 X509_NAME *name = sk_X509_NAME_value(sk, i);
940 int len = i2d_X509_NAME(name, NULL);
941 if (len < 0) {
942 return 0;
943 }
944 uint8_t *ptr;
945 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
946 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
947 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
948 return 0;
949 }
950 }
951
952 return CBB_flush(cbb);
953}
954
Adam Langleyd323f4b2016-03-01 15:58:14 -0800955static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
956 X509_STORE_free(*store_ptr);
957 *store_ptr = new_store;
958
959 if (new_store != NULL && take_ref) {
960 X509_STORE_up_ref(new_store);
961 }
962
963 return 1;
964}
965
966int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
967 return set_cert_store(&ctx->cert->verify_store, store, 0);
968}
969
970int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
971 return set_cert_store(&ctx->cert->verify_store, store, 1);
972}
973
974int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
975 return set_cert_store(&ssl->cert->verify_store, store, 0);
976}
977
978int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
979 return set_cert_store(&ssl->cert->verify_store, store, 1);
980}
981
David Benjamin11c0f8e2015-07-06 00:18:15 -0400982int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
983 return ssl_cert_set0_chain(ctx->cert, chain);
984}
985
986int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
987 return ssl_cert_set1_chain(ctx->cert, chain);
988}
989
990int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
991 return ssl_cert_set0_chain(ssl->cert, chain);
992}
993
994int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
995 return ssl_cert_set1_chain(ssl->cert, chain);
996}
997
998int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
999 return ssl_cert_add0_chain_cert(ctx->cert, x509);
1000}
1001
1002int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
1003 return ssl_cert_add1_chain_cert(ctx->cert, x509);
1004}
1005
1006int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
1007 return SSL_CTX_add0_chain_cert(ctx, x509);
1008}
1009
1010int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
1011 return ssl_cert_add0_chain_cert(ssl->cert, x509);
1012}
1013
1014int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
1015 return ssl_cert_add1_chain_cert(ssl->cert, x509);
1016}
1017
1018int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
1019 return SSL_CTX_set0_chain(ctx, NULL);
1020}
1021
1022int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
1023 return SSL_CTX_clear_chain_certs(ctx);
1024}
1025
1026int SSL_clear_chain_certs(SSL *ssl) {
1027 return SSL_set0_chain(ssl, NULL);
1028}
1029
David Benjamina2bda9f2016-12-03 23:29:05 -05001030void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
1031 void *arg) {
1032 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
1033}
1034
1035void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1036 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
1037}
1038
Adam Langley3a2b47a2017-01-24 13:59:42 -08001039/* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
1040 * first element of |cert->chain|. */
1041int ssl_cert_cache_leaf_cert(CERT *cert) {
1042 if (cert->x509_leaf != NULL ||
1043 cert->chain == NULL) {
1044 return 1;
1045 }
1046
1047 CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
1048 if (!leaf) {
1049 return 1;
1050 }
1051
1052 cert->x509_leaf = X509_parse_from_buffer(leaf);
1053 return cert->x509_leaf != NULL;
1054}
1055
1056/* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
1057 * |cert->chain|. */
1058static int ssl_cert_cache_chain_certs(CERT *cert) {
1059 if (cert->x509_chain != NULL ||
1060 cert->chain == NULL ||
1061 sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
1062 return 1;
1063 }
1064
1065 STACK_OF(X509) *chain = sk_X509_new_null();
1066 if (chain == NULL) {
1067 return 0;
1068 }
1069
1070 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
1071 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
1072 X509 *x509 = X509_parse_from_buffer(buffer);
1073 if (x509 == NULL ||
1074 !sk_X509_push(chain, x509)) {
1075 X509_free(x509);
1076 goto err;
1077 }
1078 }
1079
1080 cert->x509_chain = chain;
1081 return 1;
1082
1083err:
1084 sk_X509_pop_free(chain, X509_free);
1085 return 0;
1086}
1087
David Benjamin11c0f8e2015-07-06 00:18:15 -04001088int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001089 if (!ssl_cert_cache_chain_certs(ctx->cert)) {
1090 *out_chain = NULL;
1091 return 0;
1092 }
1093
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001094 *out_chain = ctx->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -04001095 return 1;
1096}
1097
1098int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
1099 STACK_OF(X509) **out_chain) {
1100 return SSL_CTX_get0_chain_certs(ctx, out_chain);
1101}
1102
1103int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001104 if (!ssl_cert_cache_chain_certs(ssl->cert)) {
1105 *out_chain = NULL;
1106 return 0;
1107 }
1108
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001109 *out_chain = ssl->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -04001110 return 1;
1111}
Steven Valdezbf5aa842016-07-15 07:07:40 -04001112
Adam Langley05672202016-12-13 12:05:49 -08001113int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey,
1114 const CRYPTO_BUFFER *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -04001115 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
1116
Steven Valdezbf5aa842016-07-15 07:07:40 -04001117 /* Check the certificate's type matches the cipher. */
1118 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
1119 int expected_type = ssl_cipher_get_key_type(cipher);
1120 assert(expected_type != EVP_PKEY_NONE);
1121 if (pkey->type != expected_type) {
1122 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -08001123 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001124 }
1125
1126 if (cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -08001127 CBS leaf_cbs;
1128 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
1129 /* ECDSA and ECDH certificates use the same public key format. Instead,
1130 * they are distinguished by the key usage extension in the certificate. */
1131 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
1132 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001133 }
1134
David Benjamin938fa7c2016-10-07 00:27:05 -04001135 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
1136 if (ec_key == NULL) {
1137 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -08001138 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -04001139 }
1140
1141 /* Check the key's group and point format are acceptable. */
1142 uint16_t group_id;
1143 if (!ssl_nid_to_group_id(
1144 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
1145 !tls1_check_group_id(ssl, group_id) ||
1146 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -04001147 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -08001148 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001149 }
1150 }
1151
Adam Langley05672202016-12-13 12:05:49 -08001152 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001153}
David Benjamin5edfc8c2016-12-10 15:46:58 -05001154
1155static int do_client_cert_cb(SSL *ssl, void *arg) {
1156 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
1157 return 1;
1158 }
1159
1160 X509 *x509 = NULL;
1161 EVP_PKEY *pkey = NULL;
1162 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
1163 if (ret < 0) {
1164 return -1;
1165 }
1166
1167 if (ret != 0) {
1168 if (!SSL_use_certificate(ssl, x509) ||
1169 !SSL_use_PrivateKey(ssl, pkey)) {
1170 return 0;
1171 }
1172 }
1173
1174 X509_free(x509);
1175 EVP_PKEY_free(pkey);
1176 return 1;
1177}
1178
1179void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
1180 X509 **out_x509,
1181 EVP_PKEY **out_pkey)) {
1182 /* Emulate the old client certificate callback with the new one. */
1183 SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
1184 ctx->client_cert_cb = cb;
1185}