blob: 1ebe4b62b9f6779d2513f086b1205083fa8e6ee6 [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
Adam Langleye1e78132017-01-31 15:24:31 -0800230 X509_free(cert->x509_stash);
231 cert->x509_stash = NULL;
232
Adam Langley3a2b47a2017-01-24 13:59:42 -0800233 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
234 cert->chain = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400235 EVP_PKEY_free(cert->privatekey);
236 cert->privatekey = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400237 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800238}
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240void ssl_cert_free(CERT *c) {
241 if (c == NULL) {
242 return;
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjamin2755a3e2015-04-22 16:17:58 -0400245 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 ssl_cert_clear_certs(c);
David Benjamind246b812016-07-08 15:07:02 -0700248 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800249 X509_STORE_free(c->verify_store);
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
Adam Langley3a2b47a2017-01-24 13:59:42 -0800254/* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */
255static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) {
256 STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null();
257 if (chain == NULL) {
258 return NULL;
259 }
260
261 if (!sk_CRYPTO_BUFFER_push(chain, NULL)) {
262 sk_CRYPTO_BUFFER_free(chain);
263 return NULL;
264 }
265
266 return chain;
267}
268
269/* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised
270 * contents of |x509|. */
271CRYPTO_BUFFER *x509_to_buffer(X509 *x509) {
272 uint8_t *buf = NULL;
273 int cert_len = i2d_X509(x509, &buf);
274 if (cert_len <= 0) {
275 return 0;
276 }
277
278 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL);
279 OPENSSL_free(buf);
280
281 return buffer;
282}
283
284/* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised
285 * forms of elements of |chain|. It returns one on success or zero on error, in
286 * which case no change to |cert->chain| is made. It preverses the existing
287 * leaf from |cert->chain|, if any. */
288static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) {
289 STACK_OF(CRYPTO_BUFFER) *new_chain = NULL;
290
291 if (cert->chain != NULL) {
292 new_chain = sk_CRYPTO_BUFFER_new_null();
293 if (new_chain == NULL) {
294 return 0;
295 }
296
297 CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
298 if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) {
299 goto err;
300 }
Adam Langley6f07d722017-01-27 13:58:00 -0800301 /* |leaf| might be NULL if it's a “leafless” chain. */
302 if (leaf != NULL) {
303 CRYPTO_BUFFER_up_ref(leaf);
304 }
Adam Langley3a2b47a2017-01-24 13:59:42 -0800305 }
306
307 for (size_t i = 0; i < sk_X509_num(chain); i++) {
308 if (new_chain == NULL) {
309 new_chain = new_leafless_chain();
310 if (new_chain == NULL) {
311 goto err;
312 }
313 }
314
315 CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i));
316 if (buffer == NULL ||
317 !sk_CRYPTO_BUFFER_push(new_chain, buffer)) {
318 CRYPTO_BUFFER_free(buffer);
319 goto err;
320 }
321 }
322
323 sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
324 cert->chain = new_chain;
325
326 return 1;
327
328err:
329 sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free);
330 return 0;
331}
332
David Benjamina2bda9f2016-12-03 23:29:05 -0500333static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800334 if (!ssl_cert_set_chain(cert, chain)) {
335 return 0;
336 }
337
338 sk_X509_pop_free(chain, X509_free);
339 ssl_cert_flush_cached_x509_chain(cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 return 1;
341}
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamina2bda9f2016-12-03 23:29:05 -0500343static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800344 if (!ssl_cert_set_chain(cert, chain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800345 return 0;
346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley3a2b47a2017-01-24 13:59:42 -0800348 ssl_cert_flush_cached_x509_chain(cert);
349 return 1;
350}
351
352static int ssl_cert_append_cert(CERT *cert, X509 *x509) {
353 CRYPTO_BUFFER *buffer = x509_to_buffer(x509);
354 if (buffer == NULL) {
355 return 0;
356 }
357
358 if (cert->chain != NULL) {
359 if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
360 CRYPTO_BUFFER_free(buffer);
361 return 0;
362 }
363
364 return 1;
365 }
366
367 cert->chain = new_leafless_chain();
368 if (cert->chain == NULL ||
369 !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
370 CRYPTO_BUFFER_free(buffer);
371 sk_CRYPTO_BUFFER_free(cert->chain);
372 cert->chain = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 return 0;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 return 1;
377}
Adam Langley95c29f32014-06-20 12:00:00 -0700378
David Benjamina2bda9f2016-12-03 23:29:05 -0500379static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800380 if (!ssl_cert_append_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 0;
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleye1e78132017-01-31 15:24:31 -0800384 X509_free(cert->x509_stash);
385 cert->x509_stash = x509;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800386 ssl_cert_flush_cached_x509_chain(cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800387 return 1;
388}
Adam Langley95c29f32014-06-20 12:00:00 -0700389
David Benjamina2bda9f2016-12-03 23:29:05 -0500390static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800391 if (!ssl_cert_append_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 return 0;
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langley3a2b47a2017-01-24 13:59:42 -0800395 ssl_cert_flush_cached_x509_chain(cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 return 1;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
David Benjamina2bda9f2016-12-03 23:29:05 -0500399static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
400 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 c->cert_cb = cb;
402 c->cert_cb_arg = arg;
403}
Adam Langley95c29f32014-06-20 12:00:00 -0700404
David Benjamin7aa31d62016-08-08 21:38:32 -0400405int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
406 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400407 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 return 0;
409 }
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langleyd323f4b2016-03-01 15:58:14 -0800411 X509_STORE *verify_store = ssl->ctx->cert_store;
412 if (ssl->cert->verify_store != NULL) {
413 verify_store = ssl->cert->verify_store;
414 }
415
David Benjamin306ece32015-09-17 13:46:22 -0400416 X509 *leaf = sk_X509_value(cert_chain, 0);
417 int ret = 0;
418 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800419 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400420 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800421 return 0;
422 }
David Benjamin306ece32015-09-17 13:46:22 -0400423 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
424 ssl)) {
425 goto err;
426 }
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 /* We need to inherit the verify parameters. These can be determined by the
429 * context: if its a server it will verify SSL client certificates or vice
430 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400431 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400434 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700435
David Benjamin306ece32015-09-17 13:46:22 -0400436 if (ssl->verify_callback) {
437 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 }
Adam Langley95c29f32014-06-20 12:00:00 -0700439
David Benjamine455e512016-08-01 20:11:13 -0400440 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400441 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400442 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 } else {
David Benjamine455e512016-08-01 20:11:13 -0400444 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800445 }
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleya6cd1852016-08-26 09:38:13 -0700447 *out_verify_result = ctx.error;
448
David Benjamine455e512016-08-01 20:11:13 -0400449 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
450 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400451 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400452 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
453 goto err;
454 }
455
456 ERR_clear_error();
457 ret = 1;
458
David Benjamin306ece32015-09-17 13:46:22 -0400459err:
460 X509_STORE_CTX_cleanup(&ctx);
461 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800462}
Adam Langley95c29f32014-06-20 12:00:00 -0700463
David Benjamin60da0cd2015-05-03 15:21:28 -0400464static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
465 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400466 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 *ca_list = name_list;
468}
Adam Langley95c29f32014-06-20 12:00:00 -0700469
David Benjamin59937042015-09-19 13:04:22 -0400470STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
471 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
472 if (ret == NULL) {
473 return NULL;
474 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800475
David Benjamin54091232016-09-05 12:47:25 -0400476 for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
David Benjamin59937042015-09-19 13:04:22 -0400477 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400479 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 sk_X509_NAME_pop_free(ret, X509_NAME_free);
481 return NULL;
482 }
483 }
484
485 return ret;
486}
487
David Benjamin59937042015-09-19 13:04:22 -0400488void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
489 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800490}
491
David Benjamin60da0cd2015-05-03 15:21:28 -0400492void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400493 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800494}
495
David Benjamin60da0cd2015-05-03 15:21:28 -0400496STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 return ctx->client_CA;
498}
499
David Benjamin1d128f32015-09-08 17:41:40 -0400500STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
501 /* For historical reasons, this function is used both to query configuration
502 * state on a server as well as handshake state on a client. However, whether
503 * |ssl| is a client or server is not known until explicitly configured with
504 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
505 * indeterminate mode and |ssl->server| is unset. */
506 if (ssl->handshake_func != NULL && !ssl->server) {
David Benjamina0486782016-10-06 19:11:32 -0400507 if (ssl->s3->hs != NULL) {
508 return ssl->s3->hs->ca_names;
509 }
510
511 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 }
David Benjamin1d128f32015-09-08 17:41:40 -0400513
514 if (ssl->client_CA != NULL) {
515 return ssl->client_CA;
516 }
517 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800518}
519
David Benjamin59937042015-09-19 13:04:22 -0400520static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 X509_NAME *name;
522
David Benjamin59937042015-09-19 13:04:22 -0400523 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800524 return 0;
525 }
526 if (*sk == NULL) {
527 *sk = sk_X509_NAME_new_null();
528 if (*sk == NULL) {
529 return 0;
530 }
531 }
532
David Benjamin59937042015-09-19 13:04:22 -0400533 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 if (name == NULL) {
535 return 0;
536 }
537
538 if (!sk_X509_NAME_push(*sk, name)) {
539 X509_NAME_free(name);
540 return 0;
541 }
542
543 return 1;
544}
545
David Benjamin59937042015-09-19 13:04:22 -0400546int SSL_add_client_CA(SSL *ssl, X509 *x509) {
547 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800548}
549
David Benjamin59937042015-09-19 13:04:22 -0400550int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
551 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800552}
553
David Benjamin32a66d52016-07-13 22:03:11 -0400554int ssl_has_certificate(const SSL *ssl) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800555 return ssl->cert->chain != NULL &&
556 sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0) != NULL &&
557 ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400558}
559
Adam Langley68e71242016-12-12 11:06:16 -0800560STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
Adam Langleyd5157222016-12-12 11:37:43 -0800561 EVP_PKEY **out_pubkey,
Adam Langley68e71242016-12-12 11:06:16 -0800562 uint8_t *out_leaf_sha256,
Adam Langleyd519bf62016-12-12 11:16:44 -0800563 CBS *cbs,
564 CRYPTO_BUFFER_POOL *pool) {
Adam Langleyd5157222016-12-12 11:37:43 -0800565 *out_pubkey = NULL;
566
Adam Langley68e71242016-12-12 11:06:16 -0800567 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800568 if (ret == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -0400569 *out_alert = SSL_AD_INTERNAL_ERROR;
570 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800571 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400572 }
573
David Benjamin5c900c82016-07-13 23:03:26 -0400574 CBS certificate_list;
575 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
576 *out_alert = SSL_AD_DECODE_ERROR;
577 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
578 goto err;
579 }
580
581 while (CBS_len(&certificate_list) > 0) {
582 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800583 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
584 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400585 *out_alert = SSL_AD_DECODE_ERROR;
586 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
587 goto err;
588 }
589
Adam Langleyd5157222016-12-12 11:37:43 -0800590 if (sk_CRYPTO_BUFFER_num(ret) == 0) {
591 *out_pubkey = ssl_cert_parse_pubkey(&certificate);
592 if (*out_pubkey == NULL) {
593 goto err;
594 }
595
596 /* Retain the hash of the leaf certificate if requested. */
597 if (out_leaf_sha256 != NULL) {
598 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
599 }
David Benjamin5c900c82016-07-13 23:03:26 -0400600 }
601
Adam Langleyd519bf62016-12-12 11:16:44 -0800602 CRYPTO_BUFFER *buf =
603 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800604 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800605 *out_alert = SSL_AD_DECODE_ERROR;
606 goto err;
607 }
Adam Langley68e71242016-12-12 11:06:16 -0800608
609 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400610 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800611 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400612 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
613 goto err;
614 }
David Benjamin5c900c82016-07-13 23:03:26 -0400615 }
616
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800617 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400618
619err:
Adam Langleyd5157222016-12-12 11:37:43 -0800620 EVP_PKEY_free(*out_pubkey);
621 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800622 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800623 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400624}
625
David Benjamin75836432016-06-17 18:48:29 -0400626int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400627 if (!ssl_has_certificate(ssl)) {
628 return CBB_add_u24(cbb, 0);
629 }
630
Adam Langley3a2b47a2017-01-24 13:59:42 -0800631 CBB certs;
632 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
633 goto err;
David Benjamin75836432016-06-17 18:48:29 -0400634 }
635
Adam Langley3a2b47a2017-01-24 13:59:42 -0800636 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
637 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
638 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
639 CBB child;
640 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
641 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
642 CRYPTO_BUFFER_len(buffer)) ||
643 !CBB_flush(&certs)) {
644 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 }
646 }
647
David Benjamin75836432016-06-17 18:48:29 -0400648 return CBB_flush(cbb);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800649
650err:
651 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
652 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800653}
Adam Langley95c29f32014-06-20 12:00:00 -0700654
David Benjamin650aa1c2016-12-20 18:55:16 -0500655int ssl_auto_chain_if_needed(SSL *ssl) {
656 /* Only build a chain if there are no intermediates configured and the feature
657 * isn't disabled. */
658 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
659 !ssl_has_certificate(ssl) ||
Adam Langley3a2b47a2017-01-24 13:59:42 -0800660 ssl->cert->chain == NULL ||
661 sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
David Benjamin650aa1c2016-12-20 18:55:16 -0500662 return 1;
663 }
664
Adam Langley3a2b47a2017-01-24 13:59:42 -0800665 X509 *leaf =
666 X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
667 if (!leaf) {
668 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
669 return 0;
670 }
671
David Benjamin650aa1c2016-12-20 18:55:16 -0500672 X509_STORE_CTX ctx;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800673 if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
674 X509_free(leaf);
David Benjamin650aa1c2016-12-20 18:55:16 -0500675 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
676 return 0;
677 }
678
679 /* Attempt to build a chain, ignoring the result. */
680 X509_verify_cert(&ctx);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800681 X509_free(leaf);
David Benjamin650aa1c2016-12-20 18:55:16 -0500682 ERR_clear_error();
683
Adam Langley3a2b47a2017-01-24 13:59:42 -0800684 /* Remove the leaf from the generated chain. */
685 X509_free(sk_X509_shift(ctx.chain));
686
687 const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
688 X509_STORE_CTX_cleanup(&ctx);
689 if (!ok) {
690 return 0;
David Benjamin650aa1c2016-12-20 18:55:16 -0500691 }
692
Adam Langley3a2b47a2017-01-24 13:59:42 -0800693 ssl_cert_flush_cached_x509_chain(ssl->cert);
694
David Benjamin650aa1c2016-12-20 18:55:16 -0500695 return 1;
696}
697
Adam Langley05672202016-12-13 12:05:49 -0800698/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
699 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
700 * subjectPublicKeyInfo. */
701static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800702 /* From RFC 5280, section 4.1
703 * Certificate ::= SEQUENCE {
704 * tbsCertificate TBSCertificate,
705 * signatureAlgorithm AlgorithmIdentifier,
706 * signatureValue BIT STRING }
707
708 * TBSCertificate ::= SEQUENCE {
709 * version [0] EXPLICIT Version DEFAULT v1,
710 * serialNumber CertificateSerialNumber,
711 * signature AlgorithmIdentifier,
712 * issuer Name,
713 * validity Validity,
714 * subject Name,
715 * subjectPublicKeyInfo SubjectPublicKeyInfo,
716 * ... } */
717 CBS buf = *in;
718
Adam Langley05672202016-12-13 12:05:49 -0800719 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800720 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
721 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800722 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800723 /* version */
724 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800725 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800726 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
727 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800728 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800729 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800730 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800731 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800732 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800733 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800734 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800735 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800736 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
737 return 0;
738 }
739
740 return 1;
741}
742
743EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500744 CBS buf = *in, tbs_cert;
745 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800746 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
747 return NULL;
748 }
749
David Benjamin5b410b62017-01-03 08:08:57 -0500750 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800751}
752
Adam Langley3a2b47a2017-01-24 13:59:42 -0800753static int ssl_check_cert_and_private_key_match(const CRYPTO_BUFFER *cert,
754 const EVP_PKEY *privkey) {
755 CBS cert_cbs;
756 CRYPTO_BUFFER_init_CBS(cert, &cert_cbs);
757 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
758 if (!pubkey) {
759 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
760 return 0;
761 }
762
763 int ret = 0;
764
765 switch (EVP_PKEY_cmp(pubkey, privkey)) {
766 case 1:
767 ret = 1;
768 break;
769 case 0:
770 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
771 break;
772 case -1:
773 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
774 break;
775 case -2:
776 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
777 default:
778 assert(0);
779 break;
780 }
781
782 EVP_PKEY_free(pubkey);
783 return ret;
784}
785
786int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
787 if (privkey == NULL) {
788 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
789 return 0;
790 }
791
792 if (cert->chain == NULL ||
793 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
794 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
795 return 0;
796 }
797
798 return ssl_check_cert_and_private_key_match(
799 sk_CRYPTO_BUFFER_value(cert->chain, 0), privkey);
800}
801
Adam Langley05672202016-12-13 12:05:49 -0800802int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
803 CBS buf = *in;
804
805 CBS tbs_cert, outer_extensions;
806 int has_extensions;
807 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
808 /* subjectPublicKeyInfo */
809 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
810 /* issuerUniqueID */
811 !CBS_get_optional_asn1(
812 &tbs_cert, NULL, NULL,
813 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
814 /* subjectUniqueID */
815 !CBS_get_optional_asn1(
816 &tbs_cert, NULL, NULL,
817 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
818 !CBS_get_optional_asn1(
819 &tbs_cert, &outer_extensions, &has_extensions,
820 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
821 goto parse_err;
822 }
823
824 if (!has_extensions) {
825 return 1;
826 }
827
828 CBS extensions;
829 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
830 goto parse_err;
831 }
832
833 while (CBS_len(&extensions) > 0) {
834 CBS extension, oid, contents;
835 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
836 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
837 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
838 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
839 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
840 CBS_len(&extension) != 0) {
841 goto parse_err;
842 }
843
844 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
845 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500846 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
847 0) {
Adam Langley05672202016-12-13 12:05:49 -0800848 continue;
849 }
850
851 CBS bit_string;
852 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
853 CBS_len(&contents) != 0) {
854 goto parse_err;
855 }
856
857 /* This is the KeyUsage extension. See
858 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
859 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
860 goto parse_err;
861 }
862
863 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
864 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
865 return 0;
866 }
867
868 return 1;
869 }
870
871 /* No KeyUsage extension found. */
872 return 1;
873
874parse_err:
875 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
876 return 0;
877}
878
David Benjamine0332e82016-07-13 22:40:36 -0400879static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
880 return X509_NAME_cmp(*a, *b);
881}
882
883STACK_OF(X509_NAME) *
884 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
885 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
886 X509_NAME *name = NULL;
887 if (ret == NULL) {
888 *out_alert = SSL_AD_INTERNAL_ERROR;
889 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
890 return NULL;
891 }
892
893 CBS child;
894 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
895 *out_alert = SSL_AD_DECODE_ERROR;
896 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
897 goto err;
898 }
899
900 while (CBS_len(&child) > 0) {
901 CBS distinguished_name;
902 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
903 *out_alert = SSL_AD_DECODE_ERROR;
904 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
905 goto err;
906 }
907
908 const uint8_t *ptr = CBS_data(&distinguished_name);
909 /* A u16 length cannot overflow a long. */
910 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
911 if (name == NULL ||
912 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
913 *out_alert = SSL_AD_DECODE_ERROR;
914 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
915 goto err;
916 }
917
918 if (!sk_X509_NAME_push(ret, name)) {
919 *out_alert = SSL_AD_INTERNAL_ERROR;
920 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
921 goto err;
922 }
923 name = NULL;
924 }
925
926 return ret;
927
928err:
929 X509_NAME_free(name);
930 sk_X509_NAME_pop_free(ret, X509_NAME_free);
931 return NULL;
932}
933
David Benjamin32a66d52016-07-13 22:03:11 -0400934int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
935 CBB child, name_cbb;
936 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
937 return 0;
938 }
939
940 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
941 if (sk == NULL) {
942 return CBB_flush(cbb);
943 }
944
945 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
946 X509_NAME *name = sk_X509_NAME_value(sk, i);
947 int len = i2d_X509_NAME(name, NULL);
948 if (len < 0) {
949 return 0;
950 }
951 uint8_t *ptr;
952 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
953 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
954 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
955 return 0;
956 }
957 }
958
959 return CBB_flush(cbb);
960}
961
Adam Langleyd323f4b2016-03-01 15:58:14 -0800962static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
963 X509_STORE_free(*store_ptr);
964 *store_ptr = new_store;
965
966 if (new_store != NULL && take_ref) {
967 X509_STORE_up_ref(new_store);
968 }
969
970 return 1;
971}
972
973int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
974 return set_cert_store(&ctx->cert->verify_store, store, 0);
975}
976
977int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
978 return set_cert_store(&ctx->cert->verify_store, store, 1);
979}
980
981int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
982 return set_cert_store(&ssl->cert->verify_store, store, 0);
983}
984
985int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
986 return set_cert_store(&ssl->cert->verify_store, store, 1);
987}
988
David Benjamin11c0f8e2015-07-06 00:18:15 -0400989int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
990 return ssl_cert_set0_chain(ctx->cert, chain);
991}
992
993int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
994 return ssl_cert_set1_chain(ctx->cert, chain);
995}
996
997int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
998 return ssl_cert_set0_chain(ssl->cert, chain);
999}
1000
1001int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
1002 return ssl_cert_set1_chain(ssl->cert, chain);
1003}
1004
1005int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
1006 return ssl_cert_add0_chain_cert(ctx->cert, x509);
1007}
1008
1009int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
1010 return ssl_cert_add1_chain_cert(ctx->cert, x509);
1011}
1012
1013int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
1014 return SSL_CTX_add0_chain_cert(ctx, x509);
1015}
1016
1017int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
1018 return ssl_cert_add0_chain_cert(ssl->cert, x509);
1019}
1020
1021int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
1022 return ssl_cert_add1_chain_cert(ssl->cert, x509);
1023}
1024
1025int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
1026 return SSL_CTX_set0_chain(ctx, NULL);
1027}
1028
1029int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
1030 return SSL_CTX_clear_chain_certs(ctx);
1031}
1032
1033int SSL_clear_chain_certs(SSL *ssl) {
1034 return SSL_set0_chain(ssl, NULL);
1035}
1036
David Benjamina2bda9f2016-12-03 23:29:05 -05001037void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
1038 void *arg) {
1039 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
1040}
1041
1042void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1043 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
1044}
1045
Adam Langley3a2b47a2017-01-24 13:59:42 -08001046/* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
1047 * first element of |cert->chain|. */
1048int ssl_cert_cache_leaf_cert(CERT *cert) {
1049 if (cert->x509_leaf != NULL ||
1050 cert->chain == NULL) {
1051 return 1;
1052 }
1053
1054 CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
1055 if (!leaf) {
1056 return 1;
1057 }
1058
1059 cert->x509_leaf = X509_parse_from_buffer(leaf);
1060 return cert->x509_leaf != NULL;
1061}
1062
1063/* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
1064 * |cert->chain|. */
1065static int ssl_cert_cache_chain_certs(CERT *cert) {
1066 if (cert->x509_chain != NULL ||
1067 cert->chain == NULL ||
1068 sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
1069 return 1;
1070 }
1071
1072 STACK_OF(X509) *chain = sk_X509_new_null();
1073 if (chain == NULL) {
1074 return 0;
1075 }
1076
1077 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
1078 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
1079 X509 *x509 = X509_parse_from_buffer(buffer);
1080 if (x509 == NULL ||
1081 !sk_X509_push(chain, x509)) {
1082 X509_free(x509);
1083 goto err;
1084 }
1085 }
1086
1087 cert->x509_chain = chain;
1088 return 1;
1089
1090err:
1091 sk_X509_pop_free(chain, X509_free);
1092 return 0;
1093}
1094
David Benjamin11c0f8e2015-07-06 00:18:15 -04001095int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001096 if (!ssl_cert_cache_chain_certs(ctx->cert)) {
1097 *out_chain = NULL;
1098 return 0;
1099 }
1100
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001101 *out_chain = ctx->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -04001102 return 1;
1103}
1104
1105int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
1106 STACK_OF(X509) **out_chain) {
1107 return SSL_CTX_get0_chain_certs(ctx, out_chain);
1108}
1109
1110int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001111 if (!ssl_cert_cache_chain_certs(ssl->cert)) {
1112 *out_chain = NULL;
1113 return 0;
1114 }
1115
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001116 *out_chain = ssl->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -04001117 return 1;
1118}
Steven Valdezbf5aa842016-07-15 07:07:40 -04001119
Adam Langley05672202016-12-13 12:05:49 -08001120int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey,
1121 const CRYPTO_BUFFER *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -04001122 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
1123
Steven Valdezbf5aa842016-07-15 07:07:40 -04001124 /* Check the certificate's type matches the cipher. */
1125 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
1126 int expected_type = ssl_cipher_get_key_type(cipher);
1127 assert(expected_type != EVP_PKEY_NONE);
1128 if (pkey->type != expected_type) {
1129 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -08001130 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001131 }
1132
1133 if (cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -08001134 CBS leaf_cbs;
1135 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
1136 /* ECDSA and ECDH certificates use the same public key format. Instead,
1137 * they are distinguished by the key usage extension in the certificate. */
1138 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
1139 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001140 }
1141
David Benjamin938fa7c2016-10-07 00:27:05 -04001142 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
1143 if (ec_key == NULL) {
1144 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -08001145 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -04001146 }
1147
1148 /* Check the key's group and point format are acceptable. */
1149 uint16_t group_id;
1150 if (!ssl_nid_to_group_id(
1151 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
1152 !tls1_check_group_id(ssl, group_id) ||
1153 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -04001154 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -08001155 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001156 }
1157 }
1158
Adam Langley05672202016-12-13 12:05:49 -08001159 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001160}
David Benjamin5edfc8c2016-12-10 15:46:58 -05001161
1162static int do_client_cert_cb(SSL *ssl, void *arg) {
1163 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
1164 return 1;
1165 }
1166
1167 X509 *x509 = NULL;
1168 EVP_PKEY *pkey = NULL;
1169 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
1170 if (ret < 0) {
1171 return -1;
1172 }
1173
1174 if (ret != 0) {
1175 if (!SSL_use_certificate(ssl, x509) ||
1176 !SSL_use_PrivateKey(ssl, pkey)) {
1177 return 0;
1178 }
1179 }
1180
1181 X509_free(x509);
1182 EVP_PKEY_free(pkey);
1183 return 1;
1184}
1185
1186void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
1187 X509 **out_x509,
1188 EVP_PKEY **out_pkey)) {
1189 /* Emulate the old client certificate callback with the new one. */
1190 SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
1191 ctx->client_cert_cb = cb;
1192}