blob: 048bf95c07e07d871b247c1cb126644897ddafcf [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 Langleyfcf25832014-12-18 17:42:32 -0800155CERT *ssl_cert_dup(CERT *cert) {
Brian Smith5ba06892016-02-07 09:36:04 -1000156 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400158 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 return NULL;
160 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500161 OPENSSL_memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700162
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800163 if (cert->x509_leaf != NULL) {
164 X509_up_ref(cert->x509_leaf);
165 ret->x509_leaf = cert->x509_leaf;
David Benjamind1d80782015-07-05 11:54:09 -0400166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamind1d80782015-07-05 11:54:09 -0400168 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700169 EVP_PKEY_up_ref(cert->privatekey);
170 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800173 if (cert->x509_chain) {
174 ret->x509_chain = X509_chain_up_ref(cert->x509_chain);
175 if (!ret->x509_chain) {
David Benjamin3570d732015-06-29 00:28:17 -0400176 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind1d80782015-07-05 11:54:09 -0400177 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 }
179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030181 ret->key_method = cert->key_method;
182
David Benjamin0fc37ef2016-08-17 15:29:46 -0400183 if (cert->dh_tmp != NULL) {
184 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
185 if (ret->dh_tmp == NULL) {
186 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
187 goto err;
188 }
189 }
190 ret->dh_tmp_cb = cert->dh_tmp_cb;
191
192 if (cert->sigalgs != NULL) {
193 ret->sigalgs =
194 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
195 if (ret->sigalgs == NULL) {
196 goto err;
197 }
198 }
199 ret->num_sigalgs = cert->num_sigalgs;
200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 ret->cert_cb = cert->cert_cb;
202 ret->cert_cb_arg = cert->cert_cb_arg;
203
Adam Langleyd323f4b2016-03-01 15:58:14 -0800204 if (cert->verify_store != NULL) {
205 X509_STORE_up_ref(cert->verify_store);
206 ret->verify_store = cert->verify_store;
207 }
208
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 return ret;
210
Adam Langley95c29f32014-06-20 12:00:00 -0700211err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 ssl_cert_free(ret);
213 return NULL;
214}
Adam Langley95c29f32014-06-20 12:00:00 -0700215
216/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400217void ssl_cert_clear_certs(CERT *cert) {
218 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return;
220 }
Adam Langley95c29f32014-06-20 12:00:00 -0700221
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800222 X509_free(cert->x509_leaf);
223 cert->x509_leaf = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400224 EVP_PKEY_free(cert->privatekey);
225 cert->privatekey = NULL;
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800226 sk_X509_pop_free(cert->x509_chain, X509_free);
227 cert->x509_chain = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400228 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800229}
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231void ssl_cert_free(CERT *c) {
232 if (c == NULL) {
233 return;
234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjamin2755a3e2015-04-22 16:17:58 -0400236 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700237
Adam Langleyfcf25832014-12-18 17:42:32 -0800238 ssl_cert_clear_certs(c);
David Benjamind246b812016-07-08 15:07:02 -0700239 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800240 X509_STORE_free(c->verify_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 OPENSSL_free(c);
243}
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjamina2bda9f2016-12-03 23:29:05 -0500245static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800246 sk_X509_pop_free(cert->x509_chain, X509_free);
247 cert->x509_chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 return 1;
249}
Adam Langley95c29f32014-06-20 12:00:00 -0700250
David Benjamina2bda9f2016-12-03 23:29:05 -0500251static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400252 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400253 if (chain == NULL) {
254 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400258 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 return 0;
260 }
Adam Langley95c29f32014-06-20 12:00:00 -0700261
David Benjamind1d80782015-07-05 11:54:09 -0400262 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 sk_X509_pop_free(dchain, X509_free);
264 return 0;
265 }
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 return 1;
268}
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamina2bda9f2016-12-03 23:29:05 -0500270static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800271 if (cert->x509_chain == NULL) {
272 cert->x509_chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 }
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800274 if (cert->x509_chain == NULL || !sk_X509_push(cert->x509_chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 return 0;
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 return 1;
279}
Adam Langley95c29f32014-06-20 12:00:00 -0700280
David Benjamina2bda9f2016-12-03 23:29:05 -0500281static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
David Benjamind1d80782015-07-05 11:54:09 -0400282 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 return 0;
284 }
Adam Langley95c29f32014-06-20 12:00:00 -0700285
David Benjamind1d80782015-07-05 11:54:09 -0400286 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 return 1;
288}
Adam Langley95c29f32014-06-20 12:00:00 -0700289
David Benjamina2bda9f2016-12-03 23:29:05 -0500290static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
291 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 c->cert_cb = cb;
293 c->cert_cb_arg = arg;
294}
Adam Langley95c29f32014-06-20 12:00:00 -0700295
David Benjamin7aa31d62016-08-08 21:38:32 -0400296int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
297 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400298 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800299 return 0;
300 }
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langleyd323f4b2016-03-01 15:58:14 -0800302 X509_STORE *verify_store = ssl->ctx->cert_store;
303 if (ssl->cert->verify_store != NULL) {
304 verify_store = ssl->cert->verify_store;
305 }
306
David Benjamin306ece32015-09-17 13:46:22 -0400307 X509 *leaf = sk_X509_value(cert_chain, 0);
308 int ret = 0;
309 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800310 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400311 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 return 0;
313 }
David Benjamin306ece32015-09-17 13:46:22 -0400314 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
315 ssl)) {
316 goto err;
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 /* We need to inherit the verify parameters. These can be determined by the
320 * context: if its a server it will verify SSL client certificates or vice
321 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400322 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400325 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamin306ece32015-09-17 13:46:22 -0400327 if (ssl->verify_callback) {
328 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 }
Adam Langley95c29f32014-06-20 12:00:00 -0700330
David Benjamine455e512016-08-01 20:11:13 -0400331 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400332 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400333 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800334 } else {
David Benjamine455e512016-08-01 20:11:13 -0400335 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleya6cd1852016-08-26 09:38:13 -0700338 *out_verify_result = ctx.error;
339
David Benjamine455e512016-08-01 20:11:13 -0400340 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
341 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400342 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400343 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
344 goto err;
345 }
346
347 ERR_clear_error();
348 ret = 1;
349
David Benjamin306ece32015-09-17 13:46:22 -0400350err:
351 X509_STORE_CTX_cleanup(&ctx);
352 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800353}
Adam Langley95c29f32014-06-20 12:00:00 -0700354
David Benjamin60da0cd2015-05-03 15:21:28 -0400355static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
356 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400357 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 *ca_list = name_list;
359}
Adam Langley95c29f32014-06-20 12:00:00 -0700360
David Benjamin59937042015-09-19 13:04:22 -0400361STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
362 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
363 if (ret == NULL) {
364 return NULL;
365 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800366
David Benjamin54091232016-09-05 12:47:25 -0400367 for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
David Benjamin59937042015-09-19 13:04:22 -0400368 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400370 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 sk_X509_NAME_pop_free(ret, X509_NAME_free);
372 return NULL;
373 }
374 }
375
376 return ret;
377}
378
David Benjamin59937042015-09-19 13:04:22 -0400379void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
380 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800381}
382
David Benjamin60da0cd2015-05-03 15:21:28 -0400383void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400384 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800385}
386
David Benjamin60da0cd2015-05-03 15:21:28 -0400387STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return ctx->client_CA;
389}
390
David Benjamin1d128f32015-09-08 17:41:40 -0400391STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
392 /* For historical reasons, this function is used both to query configuration
393 * state on a server as well as handshake state on a client. However, whether
394 * |ssl| is a client or server is not known until explicitly configured with
395 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
396 * indeterminate mode and |ssl->server| is unset. */
397 if (ssl->handshake_func != NULL && !ssl->server) {
David Benjamina0486782016-10-06 19:11:32 -0400398 if (ssl->s3->hs != NULL) {
399 return ssl->s3->hs->ca_names;
400 }
401
402 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 }
David Benjamin1d128f32015-09-08 17:41:40 -0400404
405 if (ssl->client_CA != NULL) {
406 return ssl->client_CA;
407 }
408 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800409}
410
David Benjamin59937042015-09-19 13:04:22 -0400411static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 X509_NAME *name;
413
David Benjamin59937042015-09-19 13:04:22 -0400414 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 return 0;
416 }
417 if (*sk == NULL) {
418 *sk = sk_X509_NAME_new_null();
419 if (*sk == NULL) {
420 return 0;
421 }
422 }
423
David Benjamin59937042015-09-19 13:04:22 -0400424 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 if (name == NULL) {
426 return 0;
427 }
428
429 if (!sk_X509_NAME_push(*sk, name)) {
430 X509_NAME_free(name);
431 return 0;
432 }
433
434 return 1;
435}
436
David Benjamin59937042015-09-19 13:04:22 -0400437int SSL_add_client_CA(SSL *ssl, X509 *x509) {
438 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800439}
440
David Benjamin59937042015-09-19 13:04:22 -0400441int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
442 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800443}
444
David Benjamin32a66d52016-07-13 22:03:11 -0400445int ssl_has_certificate(const SSL *ssl) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800446 return ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400447}
448
Adam Langley68e71242016-12-12 11:06:16 -0800449STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
Adam Langleyd5157222016-12-12 11:37:43 -0800450 EVP_PKEY **out_pubkey,
Adam Langley68e71242016-12-12 11:06:16 -0800451 uint8_t *out_leaf_sha256,
Adam Langleyd519bf62016-12-12 11:16:44 -0800452 CBS *cbs,
453 CRYPTO_BUFFER_POOL *pool) {
Adam Langleyd5157222016-12-12 11:37:43 -0800454 *out_pubkey = NULL;
455
Adam Langley68e71242016-12-12 11:06:16 -0800456 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800457 if (ret == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -0400458 *out_alert = SSL_AD_INTERNAL_ERROR;
459 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800460 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400461 }
462
David Benjamin5c900c82016-07-13 23:03:26 -0400463 CBS certificate_list;
464 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
465 *out_alert = SSL_AD_DECODE_ERROR;
466 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
467 goto err;
468 }
469
470 while (CBS_len(&certificate_list) > 0) {
471 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800472 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
473 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400474 *out_alert = SSL_AD_DECODE_ERROR;
475 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
476 goto err;
477 }
478
Adam Langleyd5157222016-12-12 11:37:43 -0800479 if (sk_CRYPTO_BUFFER_num(ret) == 0) {
480 *out_pubkey = ssl_cert_parse_pubkey(&certificate);
481 if (*out_pubkey == NULL) {
482 goto err;
483 }
484
485 /* Retain the hash of the leaf certificate if requested. */
486 if (out_leaf_sha256 != NULL) {
487 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
488 }
David Benjamin5c900c82016-07-13 23:03:26 -0400489 }
490
Adam Langleyd519bf62016-12-12 11:16:44 -0800491 CRYPTO_BUFFER *buf =
492 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800493 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800494 *out_alert = SSL_AD_DECODE_ERROR;
495 goto err;
496 }
Adam Langley68e71242016-12-12 11:06:16 -0800497
498 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400499 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800500 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400501 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
502 goto err;
503 }
David Benjamin5c900c82016-07-13 23:03:26 -0400504 }
505
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800506 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400507
508err:
Adam Langleyd5157222016-12-12 11:37:43 -0800509 EVP_PKEY_free(*out_pubkey);
510 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800511 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800512 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400513}
514
David Benjamin75836432016-06-17 18:48:29 -0400515int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
516 int len = i2d_X509(x509, NULL);
517 if (len < 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return 0;
519 }
David Benjamin75836432016-06-17 18:48:29 -0400520 uint8_t *buf;
521 if (!CBB_add_space(cbb, &buf, len)) {
522 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
523 return 0;
Steven Valdezb32a9152016-04-26 12:57:22 -0400524 }
David Benjamin75836432016-06-17 18:48:29 -0400525 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
526 return 0;
527 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 return 1;
529}
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin75836432016-06-17 18:48:29 -0400531static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
532 CBB child;
533 return CBB_add_u24_length_prefixed(cbb, &child) &&
534 ssl_add_cert_to_cbb(&child, x509) &&
535 CBB_flush(cbb);
536}
537
538int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400539 if (!ssl_has_certificate(ssl)) {
540 return CBB_add_u24(cbb, 0);
541 }
542
David Benjamin75836432016-06-17 18:48:29 -0400543 CBB child;
David Benjamin650aa1c2016-12-20 18:55:16 -0500544 if (!CBB_add_u24_length_prefixed(cbb, &child) ||
545 !ssl_add_cert_with_length(&child, ssl->cert->x509_leaf)) {
David Benjamin75836432016-06-17 18:48:29 -0400546 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
547 return 0;
548 }
549
David Benjamin650aa1c2016-12-20 18:55:16 -0500550 STACK_OF(X509) *chain = ssl->cert->x509_chain;
551 for (size_t i = 0; i < sk_X509_num(chain); i++) {
552 if (!ssl_add_cert_with_length(&child, sk_X509_value(chain, i))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 return 0;
554 }
555 }
556
David Benjamin75836432016-06-17 18:48:29 -0400557 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800558}
Adam Langley95c29f32014-06-20 12:00:00 -0700559
David Benjamin650aa1c2016-12-20 18:55:16 -0500560int ssl_auto_chain_if_needed(SSL *ssl) {
561 /* Only build a chain if there are no intermediates configured and the feature
562 * isn't disabled. */
563 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
564 !ssl_has_certificate(ssl) ||
565 ssl->cert->x509_chain != NULL) {
566 return 1;
567 }
568
569 X509_STORE_CTX ctx;
570 if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, ssl->cert->x509_leaf,
571 NULL)) {
572 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
573 return 0;
574 }
575
576 /* Attempt to build a chain, ignoring the result. */
577 X509_verify_cert(&ctx);
578 ERR_clear_error();
579
580 /* Configure the intermediates from any partial chain we managed to build. */
581 for (size_t i = 1; i < sk_X509_num(ctx.chain); i++) {
582 if (!SSL_add1_chain_cert(ssl, sk_X509_value(ctx.chain, i))) {
583 X509_STORE_CTX_cleanup(&ctx);
584 return 0;
585 }
586 }
587
588 X509_STORE_CTX_cleanup(&ctx);
589 return 1;
590}
591
Adam Langley05672202016-12-13 12:05:49 -0800592/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
593 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
594 * subjectPublicKeyInfo. */
595static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800596 /* From RFC 5280, section 4.1
597 * Certificate ::= SEQUENCE {
598 * tbsCertificate TBSCertificate,
599 * signatureAlgorithm AlgorithmIdentifier,
600 * signatureValue BIT STRING }
601
602 * TBSCertificate ::= SEQUENCE {
603 * version [0] EXPLICIT Version DEFAULT v1,
604 * serialNumber CertificateSerialNumber,
605 * signature AlgorithmIdentifier,
606 * issuer Name,
607 * validity Validity,
608 * subject Name,
609 * subjectPublicKeyInfo SubjectPublicKeyInfo,
610 * ... } */
611 CBS buf = *in;
612
Adam Langley05672202016-12-13 12:05:49 -0800613 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800614 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
615 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800616 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800617 /* version */
618 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800619 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800620 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
621 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800622 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800623 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800624 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800625 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800626 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800627 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800628 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800629 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800630 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
631 return 0;
632 }
633
634 return 1;
635}
636
637EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500638 CBS buf = *in, tbs_cert;
639 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800640 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
641 return NULL;
642 }
643
David Benjamin5b410b62017-01-03 08:08:57 -0500644 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800645}
646
Adam Langley05672202016-12-13 12:05:49 -0800647int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
648 CBS buf = *in;
649
650 CBS tbs_cert, outer_extensions;
651 int has_extensions;
652 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
653 /* subjectPublicKeyInfo */
654 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
655 /* issuerUniqueID */
656 !CBS_get_optional_asn1(
657 &tbs_cert, NULL, NULL,
658 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
659 /* subjectUniqueID */
660 !CBS_get_optional_asn1(
661 &tbs_cert, NULL, NULL,
662 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
663 !CBS_get_optional_asn1(
664 &tbs_cert, &outer_extensions, &has_extensions,
665 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
666 goto parse_err;
667 }
668
669 if (!has_extensions) {
670 return 1;
671 }
672
673 CBS extensions;
674 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
675 goto parse_err;
676 }
677
678 while (CBS_len(&extensions) > 0) {
679 CBS extension, oid, contents;
680 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
681 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
682 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
683 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
684 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
685 CBS_len(&extension) != 0) {
686 goto parse_err;
687 }
688
689 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
690 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500691 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
692 0) {
Adam Langley05672202016-12-13 12:05:49 -0800693 continue;
694 }
695
696 CBS bit_string;
697 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
698 CBS_len(&contents) != 0) {
699 goto parse_err;
700 }
701
702 /* This is the KeyUsage extension. See
703 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
704 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
705 goto parse_err;
706 }
707
708 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
709 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
710 return 0;
711 }
712
713 return 1;
714 }
715
716 /* No KeyUsage extension found. */
717 return 1;
718
719parse_err:
720 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
721 return 0;
722}
723
David Benjamine0332e82016-07-13 22:40:36 -0400724static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
725 return X509_NAME_cmp(*a, *b);
726}
727
728STACK_OF(X509_NAME) *
729 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
730 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
731 X509_NAME *name = NULL;
732 if (ret == NULL) {
733 *out_alert = SSL_AD_INTERNAL_ERROR;
734 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
735 return NULL;
736 }
737
738 CBS child;
739 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
740 *out_alert = SSL_AD_DECODE_ERROR;
741 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
742 goto err;
743 }
744
745 while (CBS_len(&child) > 0) {
746 CBS distinguished_name;
747 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
748 *out_alert = SSL_AD_DECODE_ERROR;
749 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
750 goto err;
751 }
752
753 const uint8_t *ptr = CBS_data(&distinguished_name);
754 /* A u16 length cannot overflow a long. */
755 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
756 if (name == NULL ||
757 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
758 *out_alert = SSL_AD_DECODE_ERROR;
759 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
760 goto err;
761 }
762
763 if (!sk_X509_NAME_push(ret, name)) {
764 *out_alert = SSL_AD_INTERNAL_ERROR;
765 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
766 goto err;
767 }
768 name = NULL;
769 }
770
771 return ret;
772
773err:
774 X509_NAME_free(name);
775 sk_X509_NAME_pop_free(ret, X509_NAME_free);
776 return NULL;
777}
778
David Benjamin32a66d52016-07-13 22:03:11 -0400779int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
780 CBB child, name_cbb;
781 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
782 return 0;
783 }
784
785 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
786 if (sk == NULL) {
787 return CBB_flush(cbb);
788 }
789
790 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
791 X509_NAME *name = sk_X509_NAME_value(sk, i);
792 int len = i2d_X509_NAME(name, NULL);
793 if (len < 0) {
794 return 0;
795 }
796 uint8_t *ptr;
797 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
798 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
799 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
800 return 0;
801 }
802 }
803
804 return CBB_flush(cbb);
805}
806
Adam Langleyd323f4b2016-03-01 15:58:14 -0800807static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
808 X509_STORE_free(*store_ptr);
809 *store_ptr = new_store;
810
811 if (new_store != NULL && take_ref) {
812 X509_STORE_up_ref(new_store);
813 }
814
815 return 1;
816}
817
818int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
819 return set_cert_store(&ctx->cert->verify_store, store, 0);
820}
821
822int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
823 return set_cert_store(&ctx->cert->verify_store, store, 1);
824}
825
826int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
827 return set_cert_store(&ssl->cert->verify_store, store, 0);
828}
829
830int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
831 return set_cert_store(&ssl->cert->verify_store, store, 1);
832}
833
David Benjamin11c0f8e2015-07-06 00:18:15 -0400834int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
835 return ssl_cert_set0_chain(ctx->cert, chain);
836}
837
838int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
839 return ssl_cert_set1_chain(ctx->cert, chain);
840}
841
842int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
843 return ssl_cert_set0_chain(ssl->cert, chain);
844}
845
846int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
847 return ssl_cert_set1_chain(ssl->cert, chain);
848}
849
850int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
851 return ssl_cert_add0_chain_cert(ctx->cert, x509);
852}
853
854int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
855 return ssl_cert_add1_chain_cert(ctx->cert, x509);
856}
857
858int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
859 return SSL_CTX_add0_chain_cert(ctx, x509);
860}
861
862int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
863 return ssl_cert_add0_chain_cert(ssl->cert, x509);
864}
865
866int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
867 return ssl_cert_add1_chain_cert(ssl->cert, x509);
868}
869
870int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
871 return SSL_CTX_set0_chain(ctx, NULL);
872}
873
874int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
875 return SSL_CTX_clear_chain_certs(ctx);
876}
877
878int SSL_clear_chain_certs(SSL *ssl) {
879 return SSL_set0_chain(ssl, NULL);
880}
881
David Benjamina2bda9f2016-12-03 23:29:05 -0500882void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
883 void *arg) {
884 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
885}
886
887void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
888 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
889}
890
David Benjamin11c0f8e2015-07-06 00:18:15 -0400891int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800892 *out_chain = ctx->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -0400893 return 1;
894}
895
896int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
897 STACK_OF(X509) **out_chain) {
898 return SSL_CTX_get0_chain_certs(ctx, out_chain);
899}
900
901int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800902 *out_chain = ssl->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -0400903 return 1;
904}
Steven Valdezbf5aa842016-07-15 07:07:40 -0400905
Adam Langley05672202016-12-13 12:05:49 -0800906int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey,
907 const CRYPTO_BUFFER *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -0400908 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
909
Steven Valdezbf5aa842016-07-15 07:07:40 -0400910 /* Check the certificate's type matches the cipher. */
911 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
912 int expected_type = ssl_cipher_get_key_type(cipher);
913 assert(expected_type != EVP_PKEY_NONE);
914 if (pkey->type != expected_type) {
915 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -0800916 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400917 }
918
919 if (cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -0800920 CBS leaf_cbs;
921 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
922 /* ECDSA and ECDH certificates use the same public key format. Instead,
923 * they are distinguished by the key usage extension in the certificate. */
924 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
925 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400926 }
927
David Benjamin938fa7c2016-10-07 00:27:05 -0400928 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
929 if (ec_key == NULL) {
930 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800931 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -0400932 }
933
934 /* Check the key's group and point format are acceptable. */
935 uint16_t group_id;
936 if (!ssl_nid_to_group_id(
937 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
938 !tls1_check_group_id(ssl, group_id) ||
939 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400940 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800941 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400942 }
943 }
944
Adam Langley05672202016-12-13 12:05:49 -0800945 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400946}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500947
948static int do_client_cert_cb(SSL *ssl, void *arg) {
949 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
950 return 1;
951 }
952
953 X509 *x509 = NULL;
954 EVP_PKEY *pkey = NULL;
955 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
956 if (ret < 0) {
957 return -1;
958 }
959
960 if (ret != 0) {
961 if (!SSL_use_certificate(ssl, x509) ||
962 !SSL_use_PrivateKey(ssl, pkey)) {
963 return 0;
964 }
965 }
966
967 X509_free(x509);
968 EVP_PKEY_free(pkey);
969 return 1;
970}
971
972void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
973 X509 **out_x509,
974 EVP_PKEY **out_pkey)) {
975 /* Emulate the old client certificate callback with the new one. */
976 SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
977 ctx->client_cert_cb = cb;
978}