blob: 5c5fc544475fa89cbad6dc0c3b622e8a74ecaed8 [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) {
Adam Langley7dccc712017-02-08 08:51:25 -0800593 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langleyd5157222016-12-12 11:37:43 -0800594 goto err;
595 }
596
597 /* Retain the hash of the leaf certificate if requested. */
598 if (out_leaf_sha256 != NULL) {
599 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
600 }
David Benjamin5c900c82016-07-13 23:03:26 -0400601 }
602
Adam Langleyd519bf62016-12-12 11:16:44 -0800603 CRYPTO_BUFFER *buf =
604 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800605 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800606 *out_alert = SSL_AD_DECODE_ERROR;
607 goto err;
608 }
Adam Langley68e71242016-12-12 11:06:16 -0800609
610 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400611 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800612 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400613 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
614 goto err;
615 }
David Benjamin5c900c82016-07-13 23:03:26 -0400616 }
617
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800618 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400619
620err:
Adam Langleyd5157222016-12-12 11:37:43 -0800621 EVP_PKEY_free(*out_pubkey);
622 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800623 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800624 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400625}
626
David Benjamin75836432016-06-17 18:48:29 -0400627int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400628 if (!ssl_has_certificate(ssl)) {
629 return CBB_add_u24(cbb, 0);
630 }
631
Adam Langley3a2b47a2017-01-24 13:59:42 -0800632 CBB certs;
633 if (!CBB_add_u24_length_prefixed(cbb, &certs)) {
634 goto err;
David Benjamin75836432016-06-17 18:48:29 -0400635 }
636
Adam Langley3a2b47a2017-01-24 13:59:42 -0800637 STACK_OF(CRYPTO_BUFFER) *chain = ssl->cert->chain;
638 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) {
639 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i);
640 CBB child;
641 if (!CBB_add_u24_length_prefixed(&certs, &child) ||
642 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
643 CRYPTO_BUFFER_len(buffer)) ||
644 !CBB_flush(&certs)) {
645 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 }
647 }
648
David Benjamin75836432016-06-17 18:48:29 -0400649 return CBB_flush(cbb);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800650
651err:
652 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
653 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800654}
Adam Langley95c29f32014-06-20 12:00:00 -0700655
David Benjamin650aa1c2016-12-20 18:55:16 -0500656int ssl_auto_chain_if_needed(SSL *ssl) {
657 /* Only build a chain if there are no intermediates configured and the feature
658 * isn't disabled. */
659 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
660 !ssl_has_certificate(ssl) ||
Adam Langley3a2b47a2017-01-24 13:59:42 -0800661 ssl->cert->chain == NULL ||
662 sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
David Benjamin650aa1c2016-12-20 18:55:16 -0500663 return 1;
664 }
665
Adam Langley3a2b47a2017-01-24 13:59:42 -0800666 X509 *leaf =
667 X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
668 if (!leaf) {
669 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
670 return 0;
671 }
672
David Benjamin650aa1c2016-12-20 18:55:16 -0500673 X509_STORE_CTX ctx;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800674 if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
675 X509_free(leaf);
David Benjamin650aa1c2016-12-20 18:55:16 -0500676 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
677 return 0;
678 }
679
680 /* Attempt to build a chain, ignoring the result. */
681 X509_verify_cert(&ctx);
Adam Langley3a2b47a2017-01-24 13:59:42 -0800682 X509_free(leaf);
David Benjamin650aa1c2016-12-20 18:55:16 -0500683 ERR_clear_error();
684
Adam Langley3a2b47a2017-01-24 13:59:42 -0800685 /* Remove the leaf from the generated chain. */
686 X509_free(sk_X509_shift(ctx.chain));
687
688 const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
689 X509_STORE_CTX_cleanup(&ctx);
690 if (!ok) {
691 return 0;
David Benjamin650aa1c2016-12-20 18:55:16 -0500692 }
693
Adam Langley3a2b47a2017-01-24 13:59:42 -0800694 ssl_cert_flush_cached_x509_chain(ssl->cert);
695
David Benjamin650aa1c2016-12-20 18:55:16 -0500696 return 1;
697}
698
Adam Langley05672202016-12-13 12:05:49 -0800699/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
700 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
701 * subjectPublicKeyInfo. */
702static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800703 /* From RFC 5280, section 4.1
704 * Certificate ::= SEQUENCE {
705 * tbsCertificate TBSCertificate,
706 * signatureAlgorithm AlgorithmIdentifier,
707 * signatureValue BIT STRING }
708
709 * TBSCertificate ::= SEQUENCE {
710 * version [0] EXPLICIT Version DEFAULT v1,
711 * serialNumber CertificateSerialNumber,
712 * signature AlgorithmIdentifier,
713 * issuer Name,
714 * validity Validity,
715 * subject Name,
716 * subjectPublicKeyInfo SubjectPublicKeyInfo,
717 * ... } */
718 CBS buf = *in;
719
Adam Langley05672202016-12-13 12:05:49 -0800720 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800721 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
722 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800723 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800724 /* version */
725 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800726 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800727 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
728 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800729 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800730 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800731 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800732 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800733 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800734 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800735 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800736 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800737 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
738 return 0;
739 }
740
741 return 1;
742}
743
744EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500745 CBS buf = *in, tbs_cert;
746 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800747 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
748 return NULL;
749 }
750
David Benjamin5b410b62017-01-03 08:08:57 -0500751 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800752}
753
Adam Langleyc26692c2017-01-25 09:34:42 -0800754int ssl_compare_public_and_private_key(const EVP_PKEY *pubkey,
755 const EVP_PKEY *privkey) {
Adam Langley3a2b47a2017-01-24 13:59:42 -0800756 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
Adam Langley3a2b47a2017-01-24 13:59:42 -0800775 return ret;
776}
777
778int ssl_cert_check_private_key(const CERT *cert, const EVP_PKEY *privkey) {
779 if (privkey == NULL) {
780 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
781 return 0;
782 }
783
784 if (cert->chain == NULL ||
785 sk_CRYPTO_BUFFER_value(cert->chain, 0) == NULL) {
786 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
787 return 0;
788 }
789
Adam Langleyc26692c2017-01-25 09:34:42 -0800790 CBS cert_cbs;
791 CRYPTO_BUFFER_init_CBS(sk_CRYPTO_BUFFER_value(cert->chain, 0), &cert_cbs);
792 EVP_PKEY *pubkey = ssl_cert_parse_pubkey(&cert_cbs);
793 if (!pubkey) {
794 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
795 return 0;
796 }
797
798 const int ok = ssl_compare_public_and_private_key(pubkey, privkey);
799 EVP_PKEY_free(pubkey);
800 return ok;
Adam Langley3a2b47a2017-01-24 13:59:42 -0800801}
802
Adam Langley05672202016-12-13 12:05:49 -0800803int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
804 CBS buf = *in;
805
806 CBS tbs_cert, outer_extensions;
807 int has_extensions;
808 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
809 /* subjectPublicKeyInfo */
810 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
811 /* issuerUniqueID */
812 !CBS_get_optional_asn1(
813 &tbs_cert, NULL, NULL,
814 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
815 /* subjectUniqueID */
816 !CBS_get_optional_asn1(
817 &tbs_cert, NULL, NULL,
818 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
819 !CBS_get_optional_asn1(
820 &tbs_cert, &outer_extensions, &has_extensions,
821 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
822 goto parse_err;
823 }
824
825 if (!has_extensions) {
826 return 1;
827 }
828
829 CBS extensions;
830 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
831 goto parse_err;
832 }
833
834 while (CBS_len(&extensions) > 0) {
835 CBS extension, oid, contents;
836 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
837 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
838 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
839 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
840 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
841 CBS_len(&extension) != 0) {
842 goto parse_err;
843 }
844
845 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
846 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500847 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
848 0) {
Adam Langley05672202016-12-13 12:05:49 -0800849 continue;
850 }
851
852 CBS bit_string;
853 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
854 CBS_len(&contents) != 0) {
855 goto parse_err;
856 }
857
858 /* This is the KeyUsage extension. See
859 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
860 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
861 goto parse_err;
862 }
863
864 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
865 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
866 return 0;
867 }
868
869 return 1;
870 }
871
872 /* No KeyUsage extension found. */
873 return 1;
874
875parse_err:
876 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
877 return 0;
878}
879
David Benjamine0332e82016-07-13 22:40:36 -0400880static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
881 return X509_NAME_cmp(*a, *b);
882}
883
884STACK_OF(X509_NAME) *
885 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
886 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
887 X509_NAME *name = NULL;
888 if (ret == NULL) {
889 *out_alert = SSL_AD_INTERNAL_ERROR;
890 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
891 return NULL;
892 }
893
894 CBS child;
895 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
896 *out_alert = SSL_AD_DECODE_ERROR;
897 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
898 goto err;
899 }
900
901 while (CBS_len(&child) > 0) {
902 CBS distinguished_name;
903 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
904 *out_alert = SSL_AD_DECODE_ERROR;
905 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
906 goto err;
907 }
908
909 const uint8_t *ptr = CBS_data(&distinguished_name);
910 /* A u16 length cannot overflow a long. */
911 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
912 if (name == NULL ||
913 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
914 *out_alert = SSL_AD_DECODE_ERROR;
915 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
916 goto err;
917 }
918
919 if (!sk_X509_NAME_push(ret, name)) {
920 *out_alert = SSL_AD_INTERNAL_ERROR;
921 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
922 goto err;
923 }
924 name = NULL;
925 }
926
927 return ret;
928
929err:
930 X509_NAME_free(name);
931 sk_X509_NAME_pop_free(ret, X509_NAME_free);
932 return NULL;
933}
934
David Benjamin32a66d52016-07-13 22:03:11 -0400935int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
936 CBB child, name_cbb;
937 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
938 return 0;
939 }
940
941 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
942 if (sk == NULL) {
943 return CBB_flush(cbb);
944 }
945
946 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
947 X509_NAME *name = sk_X509_NAME_value(sk, i);
948 int len = i2d_X509_NAME(name, NULL);
949 if (len < 0) {
950 return 0;
951 }
952 uint8_t *ptr;
953 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
954 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
955 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
956 return 0;
957 }
958 }
959
960 return CBB_flush(cbb);
961}
962
Adam Langleyd323f4b2016-03-01 15:58:14 -0800963static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
964 X509_STORE_free(*store_ptr);
965 *store_ptr = new_store;
966
967 if (new_store != NULL && take_ref) {
968 X509_STORE_up_ref(new_store);
969 }
970
971 return 1;
972}
973
974int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
975 return set_cert_store(&ctx->cert->verify_store, store, 0);
976}
977
978int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
979 return set_cert_store(&ctx->cert->verify_store, store, 1);
980}
981
982int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
983 return set_cert_store(&ssl->cert->verify_store, store, 0);
984}
985
986int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
987 return set_cert_store(&ssl->cert->verify_store, store, 1);
988}
989
David Benjamin11c0f8e2015-07-06 00:18:15 -0400990int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
991 return ssl_cert_set0_chain(ctx->cert, chain);
992}
993
994int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
995 return ssl_cert_set1_chain(ctx->cert, chain);
996}
997
998int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
999 return ssl_cert_set0_chain(ssl->cert, chain);
1000}
1001
1002int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
1003 return ssl_cert_set1_chain(ssl->cert, chain);
1004}
1005
1006int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
1007 return ssl_cert_add0_chain_cert(ctx->cert, x509);
1008}
1009
1010int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
1011 return ssl_cert_add1_chain_cert(ctx->cert, x509);
1012}
1013
1014int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
1015 return SSL_CTX_add0_chain_cert(ctx, x509);
1016}
1017
1018int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
1019 return ssl_cert_add0_chain_cert(ssl->cert, x509);
1020}
1021
1022int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
1023 return ssl_cert_add1_chain_cert(ssl->cert, x509);
1024}
1025
1026int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
1027 return SSL_CTX_set0_chain(ctx, NULL);
1028}
1029
1030int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
1031 return SSL_CTX_clear_chain_certs(ctx);
1032}
1033
1034int SSL_clear_chain_certs(SSL *ssl) {
1035 return SSL_set0_chain(ssl, NULL);
1036}
1037
David Benjamina2bda9f2016-12-03 23:29:05 -05001038void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
1039 void *arg) {
1040 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
1041}
1042
1043void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
1044 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
1045}
1046
Adam Langley3a2b47a2017-01-24 13:59:42 -08001047/* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
1048 * first element of |cert->chain|. */
1049int ssl_cert_cache_leaf_cert(CERT *cert) {
1050 if (cert->x509_leaf != NULL ||
1051 cert->chain == NULL) {
1052 return 1;
1053 }
1054
1055 CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
1056 if (!leaf) {
1057 return 1;
1058 }
1059
1060 cert->x509_leaf = X509_parse_from_buffer(leaf);
1061 return cert->x509_leaf != NULL;
1062}
1063
1064/* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
1065 * |cert->chain|. */
1066static int ssl_cert_cache_chain_certs(CERT *cert) {
1067 if (cert->x509_chain != NULL ||
1068 cert->chain == NULL ||
1069 sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
1070 return 1;
1071 }
1072
1073 STACK_OF(X509) *chain = sk_X509_new_null();
1074 if (chain == NULL) {
1075 return 0;
1076 }
1077
1078 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
1079 CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
1080 X509 *x509 = X509_parse_from_buffer(buffer);
1081 if (x509 == NULL ||
1082 !sk_X509_push(chain, x509)) {
1083 X509_free(x509);
1084 goto err;
1085 }
1086 }
1087
1088 cert->x509_chain = chain;
1089 return 1;
1090
1091err:
1092 sk_X509_pop_free(chain, X509_free);
1093 return 0;
1094}
1095
David Benjamin11c0f8e2015-07-06 00:18:15 -04001096int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001097 if (!ssl_cert_cache_chain_certs(ctx->cert)) {
1098 *out_chain = NULL;
1099 return 0;
1100 }
1101
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001102 *out_chain = ctx->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -04001103 return 1;
1104}
1105
1106int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
1107 STACK_OF(X509) **out_chain) {
1108 return SSL_CTX_get0_chain_certs(ctx, out_chain);
1109}
1110
1111int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
Adam Langley3a2b47a2017-01-24 13:59:42 -08001112 if (!ssl_cert_cache_chain_certs(ssl->cert)) {
1113 *out_chain = NULL;
1114 return 0;
1115 }
1116
Adam Langleyc5ac2b62016-11-07 12:02:35 -08001117 *out_chain = ssl->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -04001118 return 1;
1119}
Steven Valdezbf5aa842016-07-15 07:07:40 -04001120
Adam Langley05672202016-12-13 12:05:49 -08001121int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey,
1122 const CRYPTO_BUFFER *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -04001123 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
1124
Steven Valdezbf5aa842016-07-15 07:07:40 -04001125 /* Check the certificate's type matches the cipher. */
1126 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
1127 int expected_type = ssl_cipher_get_key_type(cipher);
1128 assert(expected_type != EVP_PKEY_NONE);
1129 if (pkey->type != expected_type) {
1130 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -08001131 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001132 }
1133
1134 if (cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -08001135 CBS leaf_cbs;
1136 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
1137 /* ECDSA and ECDH certificates use the same public key format. Instead,
1138 * they are distinguished by the key usage extension in the certificate. */
1139 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
1140 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001141 }
1142
David Benjamin938fa7c2016-10-07 00:27:05 -04001143 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
1144 if (ec_key == NULL) {
1145 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -08001146 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -04001147 }
1148
1149 /* Check the key's group and point format are acceptable. */
1150 uint16_t group_id;
1151 if (!ssl_nid_to_group_id(
1152 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
1153 !tls1_check_group_id(ssl, group_id) ||
1154 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -04001155 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -08001156 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001157 }
1158 }
1159
Adam Langley05672202016-12-13 12:05:49 -08001160 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -04001161}
David Benjamin5edfc8c2016-12-10 15:46:58 -05001162
1163static int do_client_cert_cb(SSL *ssl, void *arg) {
1164 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
1165 return 1;
1166 }
1167
1168 X509 *x509 = NULL;
1169 EVP_PKEY *pkey = NULL;
1170 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
1171 if (ret < 0) {
1172 return -1;
1173 }
1174
1175 if (ret != 0) {
1176 if (!SSL_use_certificate(ssl, x509) ||
1177 !SSL_use_PrivateKey(ssl, pkey)) {
1178 return 0;
1179 }
1180 }
1181
1182 X509_free(x509);
1183 EVP_PKEY_free(pkey);
1184 return 1;
1185}
1186
1187void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
1188 X509 **out_x509,
1189 EVP_PKEY **out_pkey)) {
1190 /* Emulate the old client certificate callback with the new one. */
1191 SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
1192 ctx->client_cert_cb = cb;
1193}