blob: 952bf4523d14ce601a3786ea0b83b3e32ccab87f [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
David Benjaminf0ae1702015-04-07 23:05:04 -0400117#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400120#include <openssl/buf.h>
David Benjamin680ca962015-06-18 12:37:23 -0400121#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700122#include <openssl/dh.h>
123#include <openssl/err.h>
124#include <openssl/mem.h>
David Benjamin5c900c82016-07-13 23:03:26 -0400125#include <openssl/sha.h>
David Benjamin680ca962015-06-18 12:37:23 -0400126#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700127#include <openssl/x509v3.h>
128
129#include "../crypto/dh/internal.h"
Adam Langley0da323a2015-05-15 12:49:30 -0700130#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400131#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langley95c29f32014-06-20 12:00:00 -0700133
Adam Langleyfcf25832014-12-18 17:42:32 -0800134int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
David Benjaminaa585132015-06-29 23:36:17 -0400135 /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
136 * reserved app_data slot. Before ex_data was introduced, app_data was used.
137 * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
138 * works. */
139 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800140}
Adam Langley95c29f32014-06-20 12:00:00 -0700141
Adam Langleyfcf25832014-12-18 17:42:32 -0800142CERT *ssl_cert_new(void) {
Brian Smith5ba06892016-02-07 09:36:04 -1000143 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800144 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400145 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800146 return NULL;
147 }
148 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langleyfcf25832014-12-18 17:42:32 -0800150 return ret;
151}
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153CERT *ssl_cert_dup(CERT *cert) {
Brian Smith5ba06892016-02-07 09:36:04 -1000154 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400156 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 return NULL;
158 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700160
Adam Langleyfcf25832014-12-18 17:42:32 -0800161 ret->mask_k = cert->mask_k;
162 ret->mask_a = cert->mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700163
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 if (cert->dh_tmp != NULL) {
165 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
166 if (ret->dh_tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400167 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 goto err;
169 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 }
171 ret->dh_tmp_cb = cert->dh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjamind1d80782015-07-05 11:54:09 -0400173 if (cert->x509 != NULL) {
174 ret->x509 = X509_up_ref(cert->x509);
175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
David Benjamind1d80782015-07-05 11:54:09 -0400177 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700178 EVP_PKEY_up_ref(cert->privatekey);
179 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjamind1d80782015-07-05 11:54:09 -0400182 if (cert->chain) {
183 ret->chain = X509_chain_up_ref(cert->chain);
184 if (!ret->chain) {
David Benjamin3570d732015-06-29 00:28:17 -0400185 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind1d80782015-07-05 11:54:09 -0400186 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 }
188 }
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030190 ret->key_method = cert->key_method;
191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 ret->cert_cb = cert->cert_cb;
193 ret->cert_cb_arg = cert->cert_cb_arg;
194
Adam Langleyd323f4b2016-03-01 15:58:14 -0800195 if (cert->verify_store != NULL) {
196 X509_STORE_up_ref(cert->verify_store);
197 ret->verify_store = cert->verify_store;
198 }
199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 return ret;
201
Adam Langley95c29f32014-06-20 12:00:00 -0700202err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 ssl_cert_free(ret);
204 return NULL;
205}
Adam Langley95c29f32014-06-20 12:00:00 -0700206
207/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400208void ssl_cert_clear_certs(CERT *cert) {
209 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 return;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamind1d80782015-07-05 11:54:09 -0400213 X509_free(cert->x509);
214 cert->x509 = NULL;
215 EVP_PKEY_free(cert->privatekey);
216 cert->privatekey = NULL;
217 sk_X509_pop_free(cert->chain, X509_free);
218 cert->chain = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400219 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800220}
Adam Langley95c29f32014-06-20 12:00:00 -0700221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222void ssl_cert_free(CERT *c) {
223 if (c == NULL) {
224 return;
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
David Benjamin2755a3e2015-04-22 16:17:58 -0400227 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 ssl_cert_clear_certs(c);
David Benjamin2755a3e2015-04-22 16:17:58 -0400230 OPENSSL_free(c->peer_sigalgs);
David Benjamind246b812016-07-08 15:07:02 -0700231 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800232 X509_STORE_free(c->verify_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 OPENSSL_free(c);
235}
Adam Langley95c29f32014-06-20 12:00:00 -0700236
David Benjamind1d80782015-07-05 11:54:09 -0400237int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
238 sk_X509_pop_free(cert->chain, X509_free);
239 cert->chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 return 1;
241}
Adam Langley95c29f32014-06-20 12:00:00 -0700242
David Benjamind1d80782015-07-05 11:54:09 -0400243int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400244 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400245 if (chain == NULL) {
246 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 }
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langleyfcf25832014-12-18 17:42:32 -0800249 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400250 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 return 0;
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamind1d80782015-07-05 11:54:09 -0400254 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 sk_X509_pop_free(dchain, X509_free);
256 return 0;
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 return 1;
260}
Adam Langley95c29f32014-06-20 12:00:00 -0700261
David Benjamind1d80782015-07-05 11:54:09 -0400262int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
263 if (cert->chain == NULL) {
264 cert->chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 }
David Benjamind1d80782015-07-05 11:54:09 -0400266 if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 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 Benjamind1d80782015-07-05 11:54:09 -0400273int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
274 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 return 0;
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamind1d80782015-07-05 11:54:09 -0400278 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 return 1;
280}
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langleyfcf25832014-12-18 17:42:32 -0800282void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
283 c->cert_cb = cb;
284 c->cert_cb_arg = arg;
285}
Adam Langley95c29f32014-06-20 12:00:00 -0700286
David Benjamin306ece32015-09-17 13:46:22 -0400287int ssl_verify_cert_chain(SSL *ssl, STACK_OF(X509) *cert_chain) {
288 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 0;
290 }
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langleyd323f4b2016-03-01 15:58:14 -0800292 X509_STORE *verify_store = ssl->ctx->cert_store;
293 if (ssl->cert->verify_store != NULL) {
294 verify_store = ssl->cert->verify_store;
295 }
296
David Benjamin306ece32015-09-17 13:46:22 -0400297 X509 *leaf = sk_X509_value(cert_chain, 0);
298 int ret = 0;
299 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800300 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400301 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 return 0;
303 }
David Benjamin306ece32015-09-17 13:46:22 -0400304 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
305 ssl)) {
306 goto err;
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langleyfcf25832014-12-18 17:42:32 -0800309 /* We need to inherit the verify parameters. These can be determined by the
310 * context: if its a server it will verify SSL client certificates or vice
311 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400312 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400315 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700316
David Benjamin306ece32015-09-17 13:46:22 -0400317 if (ssl->verify_callback) {
318 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 }
Adam Langley95c29f32014-06-20 12:00:00 -0700320
David Benjamin306ece32015-09-17 13:46:22 -0400321 if (ssl->ctx->app_verify_callback != NULL) {
322 ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800323 } else {
David Benjamin306ece32015-09-17 13:46:22 -0400324 ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamin306ece32015-09-17 13:46:22 -0400327 ssl->verify_result = ctx.error;
Adam Langley95c29f32014-06-20 12:00:00 -0700328
David Benjamin306ece32015-09-17 13:46:22 -0400329err:
330 X509_STORE_CTX_cleanup(&ctx);
331 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800332}
Adam Langley95c29f32014-06-20 12:00:00 -0700333
David Benjamin60da0cd2015-05-03 15:21:28 -0400334static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
335 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400336 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 *ca_list = name_list;
338}
Adam Langley95c29f32014-06-20 12:00:00 -0700339
David Benjamin59937042015-09-19 13:04:22 -0400340STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
341 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
342 if (ret == NULL) {
343 return NULL;
344 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800345
David Benjamin59937042015-09-19 13:04:22 -0400346 size_t i;
347 for (i = 0; i < sk_X509_NAME_num(list); i++) {
348 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400350 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 sk_X509_NAME_pop_free(ret, X509_NAME_free);
352 return NULL;
353 }
354 }
355
356 return ret;
357}
358
David Benjamin59937042015-09-19 13:04:22 -0400359void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
360 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800361}
362
David Benjamin60da0cd2015-05-03 15:21:28 -0400363void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400364 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800365}
366
David Benjamin60da0cd2015-05-03 15:21:28 -0400367STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800368 return ctx->client_CA;
369}
370
David Benjamin1d128f32015-09-08 17:41:40 -0400371STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
372 /* For historical reasons, this function is used both to query configuration
373 * state on a server as well as handshake state on a client. However, whether
374 * |ssl| is a client or server is not known until explicitly configured with
375 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
376 * indeterminate mode and |ssl->server| is unset. */
377 if (ssl->handshake_func != NULL && !ssl->server) {
378 return ssl->s3->tmp.ca_names;
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 }
David Benjamin1d128f32015-09-08 17:41:40 -0400380
381 if (ssl->client_CA != NULL) {
382 return ssl->client_CA;
383 }
384 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800385}
386
David Benjamin59937042015-09-19 13:04:22 -0400387static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 X509_NAME *name;
389
David Benjamin59937042015-09-19 13:04:22 -0400390 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 0;
392 }
393 if (*sk == NULL) {
394 *sk = sk_X509_NAME_new_null();
395 if (*sk == NULL) {
396 return 0;
397 }
398 }
399
David Benjamin59937042015-09-19 13:04:22 -0400400 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800401 if (name == NULL) {
402 return 0;
403 }
404
405 if (!sk_X509_NAME_push(*sk, name)) {
406 X509_NAME_free(name);
407 return 0;
408 }
409
410 return 1;
411}
412
David Benjamin59937042015-09-19 13:04:22 -0400413int SSL_add_client_CA(SSL *ssl, X509 *x509) {
414 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800415}
416
David Benjamin59937042015-09-19 13:04:22 -0400417int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
418 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800419}
420
David Benjamin32a66d52016-07-13 22:03:11 -0400421int ssl_has_certificate(const SSL *ssl) {
422 return ssl->cert->x509 != NULL && ssl_has_private_key(ssl);
423}
424
David Benjamin5c900c82016-07-13 23:03:26 -0400425STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert,
426 uint8_t *out_leaf_sha256, CBS *cbs) {
427 STACK_OF(X509) *ret = sk_X509_new_null();
428 if (ret == NULL) {
429 *out_alert = SSL_AD_INTERNAL_ERROR;
430 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
431 return NULL;
432 }
433
434 X509 *x = NULL;
435 CBS certificate_list;
436 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
437 *out_alert = SSL_AD_DECODE_ERROR;
438 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
439 goto err;
440 }
441
442 while (CBS_len(&certificate_list) > 0) {
443 CBS certificate;
444 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
445 *out_alert = SSL_AD_DECODE_ERROR;
446 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
447 goto err;
448 }
449
450 /* Retain the hash of the leaf certificate if requested. */
451 if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) {
452 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
453 }
454
455 /* A u24 length cannot overflow a long. */
456 const uint8_t *data = CBS_data(&certificate);
457 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
458 if (x == NULL || data != CBS_data(&certificate) + CBS_len(&certificate)) {
459 *out_alert = SSL_AD_DECODE_ERROR;
460 goto err;
461 }
462 if (!sk_X509_push(ret, x)) {
463 *out_alert = SSL_AD_INTERNAL_ERROR;
464 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
465 goto err;
466 }
467 x = NULL;
468 }
469
470 return ret;
471
472err:
473 X509_free(x);
474 sk_X509_pop_free(ret, X509_free);
475 return NULL;
476}
477
David Benjamin75836432016-06-17 18:48:29 -0400478int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
479 int len = i2d_X509(x509, NULL);
480 if (len < 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 return 0;
482 }
David Benjamin75836432016-06-17 18:48:29 -0400483 uint8_t *buf;
484 if (!CBB_add_space(cbb, &buf, len)) {
485 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
486 return 0;
Steven Valdezb32a9152016-04-26 12:57:22 -0400487 }
David Benjamin75836432016-06-17 18:48:29 -0400488 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
489 return 0;
490 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800491 return 1;
492}
Adam Langley95c29f32014-06-20 12:00:00 -0700493
David Benjamin75836432016-06-17 18:48:29 -0400494static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
495 CBB child;
496 return CBB_add_u24_length_prefixed(cbb, &child) &&
497 ssl_add_cert_to_cbb(&child, x509) &&
498 CBB_flush(cbb);
499}
500
501int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400502 if (!ssl_has_certificate(ssl)) {
503 return CBB_add_u24(cbb, 0);
504 }
505
David Benjamind1d80782015-07-05 11:54:09 -0400506 CERT *cert = ssl->cert;
David Benjamind1d80782015-07-05 11:54:09 -0400507 X509 *x = cert->x509;
Adam Langley95c29f32014-06-20 12:00:00 -0700508
David Benjamin75836432016-06-17 18:48:29 -0400509 CBB child;
510 if (!CBB_add_u24_length_prefixed(cbb, &child)) {
511 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
512 return 0;
513 }
514
515 int no_chain = 0;
516 STACK_OF(X509) *chain = cert->chain;
David Benjamind1d80782015-07-05 11:54:09 -0400517 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 no_chain = 1;
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin605641e2015-05-03 15:14:04 -0400521 if (no_chain) {
Steven Valdez57a6f3c2016-06-28 15:37:37 -0400522 if (!ssl_add_cert_with_length(&child, x)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 return 0;
524 }
David Benjamin605641e2015-05-03 15:14:04 -0400525
David Benjamin75836432016-06-17 18:48:29 -0400526 size_t i;
David Benjamind1d80782015-07-05 11:54:09 -0400527 for (i = 0; i < sk_X509_num(chain); i++) {
528 x = sk_X509_value(chain, i);
David Benjamin75836432016-06-17 18:48:29 -0400529 if (!ssl_add_cert_with_length(&child, x)) {
David Benjamin605641e2015-05-03 15:14:04 -0400530 return 0;
531 }
532 }
533 } else {
534 X509_STORE_CTX xs_ctx;
535
David Benjamind27441a2015-08-09 11:05:17 -0400536 if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400537 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
David Benjamin605641e2015-05-03 15:14:04 -0400538 return 0;
539 }
540 X509_verify_cert(&xs_ctx);
541 /* Don't leave errors in the queue */
542 ERR_clear_error();
David Benjamin75836432016-06-17 18:48:29 -0400543
544 size_t i;
David Benjamin605641e2015-05-03 15:14:04 -0400545 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
546 x = sk_X509_value(xs_ctx.chain, i);
David Benjamin75836432016-06-17 18:48:29 -0400547 if (!ssl_add_cert_with_length(&child, x)) {
David Benjamin605641e2015-05-03 15:14:04 -0400548 X509_STORE_CTX_cleanup(&xs_ctx);
549 return 0;
550 }
551 }
552 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 }
554
David Benjamin75836432016-06-17 18:48:29 -0400555 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800556}
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamine0332e82016-07-13 22:40:36 -0400558static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
559 return X509_NAME_cmp(*a, *b);
560}
561
562STACK_OF(X509_NAME) *
563 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
564 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
565 X509_NAME *name = NULL;
566 if (ret == NULL) {
567 *out_alert = SSL_AD_INTERNAL_ERROR;
568 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
569 return NULL;
570 }
571
572 CBS child;
573 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
574 *out_alert = SSL_AD_DECODE_ERROR;
575 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
576 goto err;
577 }
578
579 while (CBS_len(&child) > 0) {
580 CBS distinguished_name;
581 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
582 *out_alert = SSL_AD_DECODE_ERROR;
583 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
584 goto err;
585 }
586
587 const uint8_t *ptr = CBS_data(&distinguished_name);
588 /* A u16 length cannot overflow a long. */
589 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
590 if (name == NULL ||
591 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
592 *out_alert = SSL_AD_DECODE_ERROR;
593 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
594 goto err;
595 }
596
597 if (!sk_X509_NAME_push(ret, name)) {
598 *out_alert = SSL_AD_INTERNAL_ERROR;
599 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
600 goto err;
601 }
602 name = NULL;
603 }
604
605 return ret;
606
607err:
608 X509_NAME_free(name);
609 sk_X509_NAME_pop_free(ret, X509_NAME_free);
610 return NULL;
611}
612
David Benjamin32a66d52016-07-13 22:03:11 -0400613int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
614 CBB child, name_cbb;
615 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
616 return 0;
617 }
618
619 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
620 if (sk == NULL) {
621 return CBB_flush(cbb);
622 }
623
624 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
625 X509_NAME *name = sk_X509_NAME_value(sk, i);
626 int len = i2d_X509_NAME(name, NULL);
627 if (len < 0) {
628 return 0;
629 }
630 uint8_t *ptr;
631 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
632 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
633 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
634 return 0;
635 }
636 }
637
638 return CBB_flush(cbb);
639}
640
Adam Langleyd323f4b2016-03-01 15:58:14 -0800641static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
642 X509_STORE_free(*store_ptr);
643 *store_ptr = new_store;
644
645 if (new_store != NULL && take_ref) {
646 X509_STORE_up_ref(new_store);
647 }
648
649 return 1;
650}
651
652int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
653 return set_cert_store(&ctx->cert->verify_store, store, 0);
654}
655
656int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
657 return set_cert_store(&ctx->cert->verify_store, store, 1);
658}
659
660int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
661 return set_cert_store(&ssl->cert->verify_store, store, 0);
662}
663
664int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
665 return set_cert_store(&ssl->cert->verify_store, store, 1);
666}
667
David Benjamin11c0f8e2015-07-06 00:18:15 -0400668int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
669 return ssl_cert_set0_chain(ctx->cert, chain);
670}
671
672int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
673 return ssl_cert_set1_chain(ctx->cert, chain);
674}
675
676int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
677 return ssl_cert_set0_chain(ssl->cert, chain);
678}
679
680int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
681 return ssl_cert_set1_chain(ssl->cert, chain);
682}
683
684int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
685 return ssl_cert_add0_chain_cert(ctx->cert, x509);
686}
687
688int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
689 return ssl_cert_add1_chain_cert(ctx->cert, x509);
690}
691
692int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
693 return SSL_CTX_add0_chain_cert(ctx, x509);
694}
695
696int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
697 return ssl_cert_add0_chain_cert(ssl->cert, x509);
698}
699
700int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
701 return ssl_cert_add1_chain_cert(ssl->cert, x509);
702}
703
704int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
705 return SSL_CTX_set0_chain(ctx, NULL);
706}
707
708int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
709 return SSL_CTX_clear_chain_certs(ctx);
710}
711
712int SSL_clear_chain_certs(SSL *ssl) {
713 return SSL_set0_chain(ssl, NULL);
714}
715
716int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
717 *out_chain = ctx->cert->chain;
718 return 1;
719}
720
721int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
722 STACK_OF(X509) **out_chain) {
723 return SSL_CTX_get0_chain_certs(ctx, out_chain);
724}
725
726int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
727 *out_chain = ssl->cert->chain;
728 return 1;
729}