blob: 325da2fbc23f7fc2c831419313755f287d7226d0 [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 ret->mask_k = cert->mask_k;
184 ret->mask_a = cert->mask_a;
185
186 if (cert->dh_tmp != NULL) {
187 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
188 if (ret->dh_tmp == NULL) {
189 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
190 goto err;
191 }
192 }
193 ret->dh_tmp_cb = cert->dh_tmp_cb;
194
195 if (cert->sigalgs != NULL) {
196 ret->sigalgs =
197 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
198 if (ret->sigalgs == NULL) {
199 goto err;
200 }
201 }
202 ret->num_sigalgs = cert->num_sigalgs;
203
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 ret->cert_cb = cert->cert_cb;
205 ret->cert_cb_arg = cert->cert_cb_arg;
206
Adam Langleyd323f4b2016-03-01 15:58:14 -0800207 if (cert->verify_store != NULL) {
208 X509_STORE_up_ref(cert->verify_store);
209 ret->verify_store = cert->verify_store;
210 }
211
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 return ret;
213
Adam Langley95c29f32014-06-20 12:00:00 -0700214err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 ssl_cert_free(ret);
216 return NULL;
217}
Adam Langley95c29f32014-06-20 12:00:00 -0700218
219/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400220void ssl_cert_clear_certs(CERT *cert) {
221 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 return;
223 }
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800225 X509_free(cert->x509_leaf);
226 cert->x509_leaf = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400227 EVP_PKEY_free(cert->privatekey);
228 cert->privatekey = NULL;
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800229 sk_X509_pop_free(cert->x509_chain, X509_free);
230 cert->x509_chain = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400231 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800232}
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234void ssl_cert_free(CERT *c) {
235 if (c == NULL) {
236 return;
237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjamin2755a3e2015-04-22 16:17:58 -0400239 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 ssl_cert_clear_certs(c);
David Benjamind246b812016-07-08 15:07:02 -0700242 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800243 X509_STORE_free(c->verify_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 OPENSSL_free(c);
246}
Adam Langley95c29f32014-06-20 12:00:00 -0700247
David Benjamina2bda9f2016-12-03 23:29:05 -0500248static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800249 sk_X509_pop_free(cert->x509_chain, X509_free);
250 cert->x509_chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 return 1;
252}
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamina2bda9f2016-12-03 23:29:05 -0500254static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400255 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400256 if (chain == NULL) {
257 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400261 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800262 return 0;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
David Benjamind1d80782015-07-05 11:54:09 -0400265 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 sk_X509_pop_free(dchain, X509_free);
267 return 0;
268 }
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 return 1;
271}
Adam Langley95c29f32014-06-20 12:00:00 -0700272
David Benjamina2bda9f2016-12-03 23:29:05 -0500273static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800274 if (cert->x509_chain == NULL) {
275 cert->x509_chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 }
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800277 if (cert->x509_chain == NULL || !sk_X509_push(cert->x509_chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 return 0;
279 }
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 return 1;
282}
Adam Langley95c29f32014-06-20 12:00:00 -0700283
David Benjamina2bda9f2016-12-03 23:29:05 -0500284static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
David Benjamind1d80782015-07-05 11:54:09 -0400285 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 return 0;
287 }
Adam Langley95c29f32014-06-20 12:00:00 -0700288
David Benjamind1d80782015-07-05 11:54:09 -0400289 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 return 1;
291}
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamina2bda9f2016-12-03 23:29:05 -0500293static void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg),
294 void *arg) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 c->cert_cb = cb;
296 c->cert_cb_arg = arg;
297}
Adam Langley95c29f32014-06-20 12:00:00 -0700298
David Benjamin7aa31d62016-08-08 21:38:32 -0400299int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
300 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400301 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 return 0;
303 }
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleyd323f4b2016-03-01 15:58:14 -0800305 X509_STORE *verify_store = ssl->ctx->cert_store;
306 if (ssl->cert->verify_store != NULL) {
307 verify_store = ssl->cert->verify_store;
308 }
309
David Benjamin306ece32015-09-17 13:46:22 -0400310 X509 *leaf = sk_X509_value(cert_chain, 0);
311 int ret = 0;
312 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800313 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400314 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 return 0;
316 }
David Benjamin306ece32015-09-17 13:46:22 -0400317 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
318 ssl)) {
319 goto err;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 /* We need to inherit the verify parameters. These can be determined by the
323 * context: if its a server it will verify SSL client certificates or vice
324 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400325 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400328 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjamin306ece32015-09-17 13:46:22 -0400330 if (ssl->verify_callback) {
331 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 }
Adam Langley95c29f32014-06-20 12:00:00 -0700333
David Benjamine455e512016-08-01 20:11:13 -0400334 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400335 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400336 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 } else {
David Benjamine455e512016-08-01 20:11:13 -0400338 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 }
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleya6cd1852016-08-26 09:38:13 -0700341 *out_verify_result = ctx.error;
342
David Benjamine455e512016-08-01 20:11:13 -0400343 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
344 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400345 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400346 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
347 goto err;
348 }
349
350 ERR_clear_error();
351 ret = 1;
352
David Benjamin306ece32015-09-17 13:46:22 -0400353err:
354 X509_STORE_CTX_cleanup(&ctx);
355 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800356}
Adam Langley95c29f32014-06-20 12:00:00 -0700357
David Benjamin60da0cd2015-05-03 15:21:28 -0400358static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
359 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400360 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 *ca_list = name_list;
362}
Adam Langley95c29f32014-06-20 12:00:00 -0700363
David Benjamin59937042015-09-19 13:04:22 -0400364STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
365 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
366 if (ret == NULL) {
367 return NULL;
368 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800369
David Benjamin54091232016-09-05 12:47:25 -0400370 for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
David Benjamin59937042015-09-19 13:04:22 -0400371 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400373 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 sk_X509_NAME_pop_free(ret, X509_NAME_free);
375 return NULL;
376 }
377 }
378
379 return ret;
380}
381
David Benjamin59937042015-09-19 13:04:22 -0400382void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
383 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800384}
385
David Benjamin60da0cd2015-05-03 15:21:28 -0400386void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400387 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800388}
389
David Benjamin60da0cd2015-05-03 15:21:28 -0400390STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return ctx->client_CA;
392}
393
David Benjamin1d128f32015-09-08 17:41:40 -0400394STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
395 /* For historical reasons, this function is used both to query configuration
396 * state on a server as well as handshake state on a client. However, whether
397 * |ssl| is a client or server is not known until explicitly configured with
398 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
399 * indeterminate mode and |ssl->server| is unset. */
400 if (ssl->handshake_func != NULL && !ssl->server) {
David Benjamina0486782016-10-06 19:11:32 -0400401 if (ssl->s3->hs != NULL) {
402 return ssl->s3->hs->ca_names;
403 }
404
405 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 }
David Benjamin1d128f32015-09-08 17:41:40 -0400407
408 if (ssl->client_CA != NULL) {
409 return ssl->client_CA;
410 }
411 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800412}
413
David Benjamin59937042015-09-19 13:04:22 -0400414static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 X509_NAME *name;
416
David Benjamin59937042015-09-19 13:04:22 -0400417 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return 0;
419 }
420 if (*sk == NULL) {
421 *sk = sk_X509_NAME_new_null();
422 if (*sk == NULL) {
423 return 0;
424 }
425 }
426
David Benjamin59937042015-09-19 13:04:22 -0400427 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 if (name == NULL) {
429 return 0;
430 }
431
432 if (!sk_X509_NAME_push(*sk, name)) {
433 X509_NAME_free(name);
434 return 0;
435 }
436
437 return 1;
438}
439
David Benjamin59937042015-09-19 13:04:22 -0400440int SSL_add_client_CA(SSL *ssl, X509 *x509) {
441 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800442}
443
David Benjamin59937042015-09-19 13:04:22 -0400444int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
445 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800446}
447
David Benjamin32a66d52016-07-13 22:03:11 -0400448int ssl_has_certificate(const SSL *ssl) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800449 return ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400450}
451
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800452X509 *ssl_parse_x509(CBS *cbs) {
453 if (CBS_len(cbs) > LONG_MAX) {
454 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
455 return NULL;
456 }
457 const uint8_t *ptr = CBS_data(cbs);
458 X509 *ret = d2i_X509(NULL, &ptr, (long)CBS_len(cbs));
459 if (ret == NULL) {
460 return NULL;
461 }
462 CBS_skip(cbs, ptr - CBS_data(cbs));
463 return ret;
464}
Steven Valdeza833c352016-11-01 13:39:36 -0400465
Adam Langley68e71242016-12-12 11:06:16 -0800466STACK_OF(CRYPTO_BUFFER) *ssl_parse_cert_chain(uint8_t *out_alert,
Adam Langleyd5157222016-12-12 11:37:43 -0800467 EVP_PKEY **out_pubkey,
Adam Langley68e71242016-12-12 11:06:16 -0800468 uint8_t *out_leaf_sha256,
Adam Langleyd519bf62016-12-12 11:16:44 -0800469 CBS *cbs,
470 CRYPTO_BUFFER_POOL *pool) {
Adam Langleyd5157222016-12-12 11:37:43 -0800471 *out_pubkey = NULL;
472
Adam Langley68e71242016-12-12 11:06:16 -0800473 STACK_OF(CRYPTO_BUFFER) *ret = sk_CRYPTO_BUFFER_new_null();
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800474 if (ret == NULL) {
David Benjamin5c900c82016-07-13 23:03:26 -0400475 *out_alert = SSL_AD_INTERNAL_ERROR;
476 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800477 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400478 }
479
David Benjamin5c900c82016-07-13 23:03:26 -0400480 CBS certificate_list;
481 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
482 *out_alert = SSL_AD_DECODE_ERROR;
483 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
484 goto err;
485 }
486
487 while (CBS_len(&certificate_list) > 0) {
488 CBS certificate;
Adam Langley68e71242016-12-12 11:06:16 -0800489 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
490 CBS_len(&certificate) == 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400491 *out_alert = SSL_AD_DECODE_ERROR;
492 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
493 goto err;
494 }
495
Adam Langleyd5157222016-12-12 11:37:43 -0800496 if (sk_CRYPTO_BUFFER_num(ret) == 0) {
497 *out_pubkey = ssl_cert_parse_pubkey(&certificate);
498 if (*out_pubkey == NULL) {
499 goto err;
500 }
501
502 /* Retain the hash of the leaf certificate if requested. */
503 if (out_leaf_sha256 != NULL) {
504 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
505 }
David Benjamin5c900c82016-07-13 23:03:26 -0400506 }
507
Adam Langleyd519bf62016-12-12 11:16:44 -0800508 CRYPTO_BUFFER *buf =
509 CRYPTO_BUFFER_new_from_CBS(&certificate, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800510 if (buf == NULL) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800511 *out_alert = SSL_AD_DECODE_ERROR;
512 goto err;
513 }
Adam Langley68e71242016-12-12 11:06:16 -0800514
515 if (!sk_CRYPTO_BUFFER_push(ret, buf)) {
David Benjamin5c900c82016-07-13 23:03:26 -0400516 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley68e71242016-12-12 11:06:16 -0800517 CRYPTO_BUFFER_free(buf);
David Benjamin5c900c82016-07-13 23:03:26 -0400518 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
519 goto err;
520 }
David Benjamin5c900c82016-07-13 23:03:26 -0400521 }
522
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800523 return ret;
David Benjamin5c900c82016-07-13 23:03:26 -0400524
525err:
Adam Langleyd5157222016-12-12 11:37:43 -0800526 EVP_PKEY_free(*out_pubkey);
527 *out_pubkey = NULL;
Adam Langley68e71242016-12-12 11:06:16 -0800528 sk_CRYPTO_BUFFER_pop_free(ret, CRYPTO_BUFFER_free);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800529 return NULL;
David Benjamin5c900c82016-07-13 23:03:26 -0400530}
531
David Benjamin75836432016-06-17 18:48:29 -0400532int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
533 int len = i2d_X509(x509, NULL);
534 if (len < 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 return 0;
536 }
David Benjamin75836432016-06-17 18:48:29 -0400537 uint8_t *buf;
538 if (!CBB_add_space(cbb, &buf, len)) {
539 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
540 return 0;
Steven Valdezb32a9152016-04-26 12:57:22 -0400541 }
David Benjamin75836432016-06-17 18:48:29 -0400542 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
543 return 0;
544 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 return 1;
546}
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin75836432016-06-17 18:48:29 -0400548static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
549 CBB child;
550 return CBB_add_u24_length_prefixed(cbb, &child) &&
551 ssl_add_cert_to_cbb(&child, x509) &&
552 CBB_flush(cbb);
553}
554
555int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400556 if (!ssl_has_certificate(ssl)) {
557 return CBB_add_u24(cbb, 0);
558 }
559
David Benjamin75836432016-06-17 18:48:29 -0400560 CBB child;
David Benjamin650aa1c2016-12-20 18:55:16 -0500561 if (!CBB_add_u24_length_prefixed(cbb, &child) ||
562 !ssl_add_cert_with_length(&child, ssl->cert->x509_leaf)) {
David Benjamin75836432016-06-17 18:48:29 -0400563 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
564 return 0;
565 }
566
David Benjamin650aa1c2016-12-20 18:55:16 -0500567 STACK_OF(X509) *chain = ssl->cert->x509_chain;
568 for (size_t i = 0; i < sk_X509_num(chain); i++) {
569 if (!ssl_add_cert_with_length(&child, sk_X509_value(chain, i))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 return 0;
571 }
572 }
573
David Benjamin75836432016-06-17 18:48:29 -0400574 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800575}
Adam Langley95c29f32014-06-20 12:00:00 -0700576
David Benjamin650aa1c2016-12-20 18:55:16 -0500577int ssl_auto_chain_if_needed(SSL *ssl) {
578 /* Only build a chain if there are no intermediates configured and the feature
579 * isn't disabled. */
580 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
581 !ssl_has_certificate(ssl) ||
582 ssl->cert->x509_chain != NULL) {
583 return 1;
584 }
585
586 X509_STORE_CTX ctx;
587 if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, ssl->cert->x509_leaf,
588 NULL)) {
589 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
590 return 0;
591 }
592
593 /* Attempt to build a chain, ignoring the result. */
594 X509_verify_cert(&ctx);
595 ERR_clear_error();
596
597 /* Configure the intermediates from any partial chain we managed to build. */
598 for (size_t i = 1; i < sk_X509_num(ctx.chain); i++) {
599 if (!SSL_add1_chain_cert(ssl, sk_X509_value(ctx.chain, i))) {
600 X509_STORE_CTX_cleanup(&ctx);
601 return 0;
602 }
603 }
604
605 X509_STORE_CTX_cleanup(&ctx);
606 return 1;
607}
608
Adam Langley05672202016-12-13 12:05:49 -0800609/* ssl_cert_skip_to_spki parses a DER-encoded, X.509 certificate from |in| and
610 * positions |*out_tbs_cert| to cover the TBSCertificate, starting at the
611 * subjectPublicKeyInfo. */
612static int ssl_cert_skip_to_spki(const CBS *in, CBS *out_tbs_cert) {
Adam Langleyd5157222016-12-12 11:37:43 -0800613 /* From RFC 5280, section 4.1
614 * Certificate ::= SEQUENCE {
615 * tbsCertificate TBSCertificate,
616 * signatureAlgorithm AlgorithmIdentifier,
617 * signatureValue BIT STRING }
618
619 * TBSCertificate ::= SEQUENCE {
620 * version [0] EXPLICIT Version DEFAULT v1,
621 * serialNumber CertificateSerialNumber,
622 * signature AlgorithmIdentifier,
623 * issuer Name,
624 * validity Validity,
625 * subject Name,
626 * subjectPublicKeyInfo SubjectPublicKeyInfo,
627 * ... } */
628 CBS buf = *in;
629
Adam Langley05672202016-12-13 12:05:49 -0800630 CBS toplevel;
Adam Langleyd5157222016-12-12 11:37:43 -0800631 if (!CBS_get_asn1(&buf, &toplevel, CBS_ASN1_SEQUENCE) ||
632 CBS_len(&buf) != 0 ||
Adam Langley05672202016-12-13 12:05:49 -0800633 !CBS_get_asn1(&toplevel, out_tbs_cert, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800634 /* version */
635 !CBS_get_optional_asn1(
Adam Langley05672202016-12-13 12:05:49 -0800636 out_tbs_cert, NULL, NULL,
Adam Langleyd5157222016-12-12 11:37:43 -0800637 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0) ||
638 /* serialNumber */
Adam Langley05672202016-12-13 12:05:49 -0800639 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_INTEGER) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800640 /* signature algorithm */
Adam Langley05672202016-12-13 12:05:49 -0800641 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800642 /* issuer */
Adam Langley05672202016-12-13 12:05:49 -0800643 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800644 /* validity */
Adam Langley05672202016-12-13 12:05:49 -0800645 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
Adam Langleyd5157222016-12-12 11:37:43 -0800646 /* subject */
Adam Langley05672202016-12-13 12:05:49 -0800647 !CBS_get_asn1(out_tbs_cert, NULL, CBS_ASN1_SEQUENCE)) {
648 return 0;
649 }
650
651 return 1;
652}
653
654EVP_PKEY *ssl_cert_parse_pubkey(const CBS *in) {
David Benjamin5b410b62017-01-03 08:08:57 -0500655 CBS buf = *in, tbs_cert;
656 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert)) {
Adam Langleyd5157222016-12-12 11:37:43 -0800657 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
658 return NULL;
659 }
660
David Benjamin5b410b62017-01-03 08:08:57 -0500661 return EVP_parse_public_key(&tbs_cert);
Adam Langleyd5157222016-12-12 11:37:43 -0800662}
663
Adam Langley05672202016-12-13 12:05:49 -0800664int ssl_cert_check_digital_signature_key_usage(const CBS *in) {
665 CBS buf = *in;
666
667 CBS tbs_cert, outer_extensions;
668 int has_extensions;
669 if (!ssl_cert_skip_to_spki(&buf, &tbs_cert) ||
670 /* subjectPublicKeyInfo */
671 !CBS_get_asn1(&tbs_cert, NULL, CBS_ASN1_SEQUENCE) ||
672 /* issuerUniqueID */
673 !CBS_get_optional_asn1(
674 &tbs_cert, NULL, NULL,
675 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1) ||
676 /* subjectUniqueID */
677 !CBS_get_optional_asn1(
678 &tbs_cert, NULL, NULL,
679 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2) ||
680 !CBS_get_optional_asn1(
681 &tbs_cert, &outer_extensions, &has_extensions,
682 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3)) {
683 goto parse_err;
684 }
685
686 if (!has_extensions) {
687 return 1;
688 }
689
690 CBS extensions;
691 if (!CBS_get_asn1(&outer_extensions, &extensions, CBS_ASN1_SEQUENCE)) {
692 goto parse_err;
693 }
694
695 while (CBS_len(&extensions) > 0) {
696 CBS extension, oid, contents;
697 if (!CBS_get_asn1(&extensions, &extension, CBS_ASN1_SEQUENCE) ||
698 !CBS_get_asn1(&extension, &oid, CBS_ASN1_OBJECT) ||
699 (CBS_peek_asn1_tag(&extension, CBS_ASN1_BOOLEAN) &&
700 !CBS_get_asn1(&extension, NULL, CBS_ASN1_BOOLEAN)) ||
701 !CBS_get_asn1(&extension, &contents, CBS_ASN1_OCTETSTRING) ||
702 CBS_len(&extension) != 0) {
703 goto parse_err;
704 }
705
706 static const uint8_t kKeyUsageOID[3] = {0x55, 0x1d, 0x0f};
707 if (CBS_len(&oid) != sizeof(kKeyUsageOID) ||
David Benjamin17cf2cb2016-12-13 01:07:13 -0500708 OPENSSL_memcmp(CBS_data(&oid), kKeyUsageOID, sizeof(kKeyUsageOID)) !=
709 0) {
Adam Langley05672202016-12-13 12:05:49 -0800710 continue;
711 }
712
713 CBS bit_string;
714 if (!CBS_get_asn1(&contents, &bit_string, CBS_ASN1_BITSTRING) ||
715 CBS_len(&contents) != 0) {
716 goto parse_err;
717 }
718
719 /* This is the KeyUsage extension. See
720 * https://tools.ietf.org/html/rfc5280#section-4.2.1.3 */
721 if (!CBS_is_valid_asn1_bitstring(&bit_string)) {
722 goto parse_err;
723 }
724
725 if (!CBS_asn1_bitstring_has_bit(&bit_string, 0)) {
726 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
727 return 0;
728 }
729
730 return 1;
731 }
732
733 /* No KeyUsage extension found. */
734 return 1;
735
736parse_err:
737 OPENSSL_PUT_ERROR(SSL, SSL_R_CANNOT_PARSE_LEAF_CERT);
738 return 0;
739}
740
David Benjamine0332e82016-07-13 22:40:36 -0400741static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
742 return X509_NAME_cmp(*a, *b);
743}
744
745STACK_OF(X509_NAME) *
746 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
747 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
748 X509_NAME *name = NULL;
749 if (ret == NULL) {
750 *out_alert = SSL_AD_INTERNAL_ERROR;
751 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
752 return NULL;
753 }
754
755 CBS child;
756 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
757 *out_alert = SSL_AD_DECODE_ERROR;
758 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
759 goto err;
760 }
761
762 while (CBS_len(&child) > 0) {
763 CBS distinguished_name;
764 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
765 *out_alert = SSL_AD_DECODE_ERROR;
766 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
767 goto err;
768 }
769
770 const uint8_t *ptr = CBS_data(&distinguished_name);
771 /* A u16 length cannot overflow a long. */
772 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
773 if (name == NULL ||
774 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
775 *out_alert = SSL_AD_DECODE_ERROR;
776 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
777 goto err;
778 }
779
780 if (!sk_X509_NAME_push(ret, name)) {
781 *out_alert = SSL_AD_INTERNAL_ERROR;
782 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
783 goto err;
784 }
785 name = NULL;
786 }
787
788 return ret;
789
790err:
791 X509_NAME_free(name);
792 sk_X509_NAME_pop_free(ret, X509_NAME_free);
793 return NULL;
794}
795
David Benjamin32a66d52016-07-13 22:03:11 -0400796int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
797 CBB child, name_cbb;
798 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
799 return 0;
800 }
801
802 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
803 if (sk == NULL) {
804 return CBB_flush(cbb);
805 }
806
807 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
808 X509_NAME *name = sk_X509_NAME_value(sk, i);
809 int len = i2d_X509_NAME(name, NULL);
810 if (len < 0) {
811 return 0;
812 }
813 uint8_t *ptr;
814 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
815 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
816 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
817 return 0;
818 }
819 }
820
821 return CBB_flush(cbb);
822}
823
Adam Langleyd323f4b2016-03-01 15:58:14 -0800824static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
825 X509_STORE_free(*store_ptr);
826 *store_ptr = new_store;
827
828 if (new_store != NULL && take_ref) {
829 X509_STORE_up_ref(new_store);
830 }
831
832 return 1;
833}
834
835int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
836 return set_cert_store(&ctx->cert->verify_store, store, 0);
837}
838
839int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
840 return set_cert_store(&ctx->cert->verify_store, store, 1);
841}
842
843int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
844 return set_cert_store(&ssl->cert->verify_store, store, 0);
845}
846
847int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
848 return set_cert_store(&ssl->cert->verify_store, store, 1);
849}
850
David Benjamin11c0f8e2015-07-06 00:18:15 -0400851int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
852 return ssl_cert_set0_chain(ctx->cert, chain);
853}
854
855int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
856 return ssl_cert_set1_chain(ctx->cert, chain);
857}
858
859int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
860 return ssl_cert_set0_chain(ssl->cert, chain);
861}
862
863int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
864 return ssl_cert_set1_chain(ssl->cert, chain);
865}
866
867int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
868 return ssl_cert_add0_chain_cert(ctx->cert, x509);
869}
870
871int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
872 return ssl_cert_add1_chain_cert(ctx->cert, x509);
873}
874
875int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
876 return SSL_CTX_add0_chain_cert(ctx, x509);
877}
878
879int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
880 return ssl_cert_add0_chain_cert(ssl->cert, x509);
881}
882
883int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
884 return ssl_cert_add1_chain_cert(ssl->cert, x509);
885}
886
887int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
888 return SSL_CTX_set0_chain(ctx, NULL);
889}
890
891int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
892 return SSL_CTX_clear_chain_certs(ctx);
893}
894
895int SSL_clear_chain_certs(SSL *ssl) {
896 return SSL_set0_chain(ssl, NULL);
897}
898
David Benjamina2bda9f2016-12-03 23:29:05 -0500899void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
900 void *arg) {
901 ssl_cert_set_cert_cb(ctx->cert, cb, arg);
902}
903
904void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
905 ssl_cert_set_cert_cb(ssl->cert, cb, arg);
906}
907
David Benjamin11c0f8e2015-07-06 00:18:15 -0400908int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800909 *out_chain = ctx->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -0400910 return 1;
911}
912
913int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
914 STACK_OF(X509) **out_chain) {
915 return SSL_CTX_get0_chain_certs(ctx, out_chain);
916}
917
918int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800919 *out_chain = ssl->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -0400920 return 1;
921}
Steven Valdezbf5aa842016-07-15 07:07:40 -0400922
Adam Langley05672202016-12-13 12:05:49 -0800923int ssl_check_leaf_certificate(SSL *ssl, EVP_PKEY *pkey,
924 const CRYPTO_BUFFER *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -0400925 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
926
Steven Valdezbf5aa842016-07-15 07:07:40 -0400927 /* Check the certificate's type matches the cipher. */
928 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
929 int expected_type = ssl_cipher_get_key_type(cipher);
930 assert(expected_type != EVP_PKEY_NONE);
931 if (pkey->type != expected_type) {
932 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
Adam Langley05672202016-12-13 12:05:49 -0800933 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400934 }
935
936 if (cipher->algorithm_auth & SSL_aECDSA) {
Adam Langley05672202016-12-13 12:05:49 -0800937 CBS leaf_cbs;
938 CBS_init(&leaf_cbs, CRYPTO_BUFFER_data(leaf), CRYPTO_BUFFER_len(leaf));
939 /* ECDSA and ECDH certificates use the same public key format. Instead,
940 * they are distinguished by the key usage extension in the certificate. */
941 if (!ssl_cert_check_digital_signature_key_usage(&leaf_cbs)) {
942 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400943 }
944
David Benjamin938fa7c2016-10-07 00:27:05 -0400945 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
946 if (ec_key == NULL) {
947 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800948 return 0;
David Benjamin938fa7c2016-10-07 00:27:05 -0400949 }
950
951 /* Check the key's group and point format are acceptable. */
952 uint16_t group_id;
953 if (!ssl_nid_to_group_id(
954 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
955 !tls1_check_group_id(ssl, group_id) ||
956 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400957 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
Adam Langley05672202016-12-13 12:05:49 -0800958 return 0;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400959 }
960 }
961
Adam Langley05672202016-12-13 12:05:49 -0800962 return 1;
Steven Valdezbf5aa842016-07-15 07:07:40 -0400963}
David Benjamin5edfc8c2016-12-10 15:46:58 -0500964
965static int do_client_cert_cb(SSL *ssl, void *arg) {
966 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
967 return 1;
968 }
969
970 X509 *x509 = NULL;
971 EVP_PKEY *pkey = NULL;
972 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
973 if (ret < 0) {
974 return -1;
975 }
976
977 if (ret != 0) {
978 if (!SSL_use_certificate(ssl, x509) ||
979 !SSL_use_PrivateKey(ssl, pkey)) {
980 return 0;
981 }
982 }
983
984 X509_free(x509);
985 EVP_PKEY_free(pkey);
986 return 1;
987}
988
989void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
990 X509 **out_x509,
991 EVP_PKEY **out_pkey)) {
992 /* Emulate the old client certificate callback with the new one. */
993 SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
994 ctx->client_cert_cb = cb;
995}