blob: 3cfcd8d0fd93c975d3fd10470d3e9fb23d2844e9 [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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400121#include <openssl/buf.h>
David Benjamin680ca962015-06-18 12:37:23 -0400122#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/dh.h>
124#include <openssl/err.h>
125#include <openssl/mem.h>
David Benjamin5c900c82016-07-13 23:03:26 -0400126#include <openssl/sha.h>
David Benjamin680ca962015-06-18 12:37:23 -0400127#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700128#include <openssl/x509v3.h>
129
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) {
David Benjamin96a16cd2016-08-11 12:14:47 -0400174 X509_up_ref(cert->x509);
175 ret->x509 = cert->x509;
David Benjamind1d80782015-07-05 11:54:09 -0400176 }
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjamind1d80782015-07-05 11:54:09 -0400178 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700179 EVP_PKEY_up_ref(cert->privatekey);
180 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjamind1d80782015-07-05 11:54:09 -0400183 if (cert->chain) {
184 ret->chain = X509_chain_up_ref(cert->chain);
185 if (!ret->chain) {
David Benjamin3570d732015-06-29 00:28:17 -0400186 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind1d80782015-07-05 11:54:09 -0400187 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800188 }
189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030191 ret->key_method = cert->key_method;
192
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 ret->cert_cb = cert->cert_cb;
194 ret->cert_cb_arg = cert->cert_cb_arg;
195
Adam Langleyd323f4b2016-03-01 15:58:14 -0800196 if (cert->verify_store != NULL) {
197 X509_STORE_up_ref(cert->verify_store);
198 ret->verify_store = cert->verify_store;
199 }
200
Adam Langleyfcf25832014-12-18 17:42:32 -0800201 return ret;
202
Adam Langley95c29f32014-06-20 12:00:00 -0700203err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800204 ssl_cert_free(ret);
205 return NULL;
206}
Adam Langley95c29f32014-06-20 12:00:00 -0700207
208/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400209void ssl_cert_clear_certs(CERT *cert) {
210 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 return;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamind1d80782015-07-05 11:54:09 -0400214 X509_free(cert->x509);
215 cert->x509 = NULL;
216 EVP_PKEY_free(cert->privatekey);
217 cert->privatekey = NULL;
218 sk_X509_pop_free(cert->chain, X509_free);
219 cert->chain = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400220 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800221}
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langleyfcf25832014-12-18 17:42:32 -0800223void ssl_cert_free(CERT *c) {
224 if (c == NULL) {
225 return;
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
David Benjamin2755a3e2015-04-22 16:17:58 -0400228 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 ssl_cert_clear_certs(c);
David Benjamin2755a3e2015-04-22 16:17:58 -0400231 OPENSSL_free(c->peer_sigalgs);
David Benjamind246b812016-07-08 15:07:02 -0700232 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800233 X509_STORE_free(c->verify_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 OPENSSL_free(c);
236}
Adam Langley95c29f32014-06-20 12:00:00 -0700237
David Benjamind1d80782015-07-05 11:54:09 -0400238int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
239 sk_X509_pop_free(cert->chain, X509_free);
240 cert->chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800241 return 1;
242}
Adam Langley95c29f32014-06-20 12:00:00 -0700243
David Benjamind1d80782015-07-05 11:54:09 -0400244int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400245 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400246 if (chain == NULL) {
247 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400251 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 return 0;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
David Benjamind1d80782015-07-05 11:54:09 -0400255 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 sk_X509_pop_free(dchain, X509_free);
257 return 0;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 return 1;
261}
Adam Langley95c29f32014-06-20 12:00:00 -0700262
David Benjamind1d80782015-07-05 11:54:09 -0400263int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
264 if (cert->chain == NULL) {
265 cert->chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 }
David Benjamind1d80782015-07-05 11:54:09 -0400267 if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return 0;
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 return 1;
272}
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamind1d80782015-07-05 11:54:09 -0400274int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
275 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 return 0;
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
David Benjamind1d80782015-07-05 11:54:09 -0400279 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 return 1;
281}
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
284 c->cert_cb = cb;
285 c->cert_cb_arg = arg;
286}
Adam Langley95c29f32014-06-20 12:00:00 -0700287
David Benjamin7aa31d62016-08-08 21:38:32 -0400288int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
289 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400290 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 return 0;
292 }
Adam Langley95c29f32014-06-20 12:00:00 -0700293
Adam Langleyd323f4b2016-03-01 15:58:14 -0800294 X509_STORE *verify_store = ssl->ctx->cert_store;
295 if (ssl->cert->verify_store != NULL) {
296 verify_store = ssl->cert->verify_store;
297 }
298
David Benjamin306ece32015-09-17 13:46:22 -0400299 X509 *leaf = sk_X509_value(cert_chain, 0);
300 int ret = 0;
301 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800302 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400303 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 return 0;
305 }
David Benjamin306ece32015-09-17 13:46:22 -0400306 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
307 ssl)) {
308 goto err;
309 }
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 /* We need to inherit the verify parameters. These can be determined by the
312 * context: if its a server it will verify SSL client certificates or vice
313 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400314 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700315
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400317 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700318
David Benjamin306ece32015-09-17 13:46:22 -0400319 if (ssl->verify_callback) {
320 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 }
Adam Langley95c29f32014-06-20 12:00:00 -0700322
David Benjamine455e512016-08-01 20:11:13 -0400323 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400324 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400325 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800326 } else {
David Benjamine455e512016-08-01 20:11:13 -0400327 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjamine455e512016-08-01 20:11:13 -0400330 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
331 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400332 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400333 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
334 goto err;
335 }
336
337 ERR_clear_error();
David Benjamin7aa31d62016-08-08 21:38:32 -0400338 *out_verify_result = ctx.error;
David Benjamine455e512016-08-01 20:11:13 -0400339 ret = 1;
340
David Benjamin306ece32015-09-17 13:46:22 -0400341err:
342 X509_STORE_CTX_cleanup(&ctx);
343 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800344}
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin60da0cd2015-05-03 15:21:28 -0400346static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
347 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400348 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800349 *ca_list = name_list;
350}
Adam Langley95c29f32014-06-20 12:00:00 -0700351
David Benjamin59937042015-09-19 13:04:22 -0400352STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
353 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
354 if (ret == NULL) {
355 return NULL;
356 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800357
David Benjamin59937042015-09-19 13:04:22 -0400358 size_t i;
359 for (i = 0; i < sk_X509_NAME_num(list); i++) {
360 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400362 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 sk_X509_NAME_pop_free(ret, X509_NAME_free);
364 return NULL;
365 }
366 }
367
368 return ret;
369}
370
David Benjamin59937042015-09-19 13:04:22 -0400371void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
372 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800373}
374
David Benjamin60da0cd2015-05-03 15:21:28 -0400375void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400376 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800377}
378
David Benjamin60da0cd2015-05-03 15:21:28 -0400379STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 return ctx->client_CA;
381}
382
David Benjamin1d128f32015-09-08 17:41:40 -0400383STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
384 /* For historical reasons, this function is used both to query configuration
385 * state on a server as well as handshake state on a client. However, whether
386 * |ssl| is a client or server is not known until explicitly configured with
387 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
388 * indeterminate mode and |ssl->server| is unset. */
389 if (ssl->handshake_func != NULL && !ssl->server) {
390 return ssl->s3->tmp.ca_names;
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 }
David Benjamin1d128f32015-09-08 17:41:40 -0400392
393 if (ssl->client_CA != NULL) {
394 return ssl->client_CA;
395 }
396 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800397}
398
David Benjamin59937042015-09-19 13:04:22 -0400399static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800400 X509_NAME *name;
401
David Benjamin59937042015-09-19 13:04:22 -0400402 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 return 0;
404 }
405 if (*sk == NULL) {
406 *sk = sk_X509_NAME_new_null();
407 if (*sk == NULL) {
408 return 0;
409 }
410 }
411
David Benjamin59937042015-09-19 13:04:22 -0400412 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 if (name == NULL) {
414 return 0;
415 }
416
417 if (!sk_X509_NAME_push(*sk, name)) {
418 X509_NAME_free(name);
419 return 0;
420 }
421
422 return 1;
423}
424
David Benjamin59937042015-09-19 13:04:22 -0400425int SSL_add_client_CA(SSL *ssl, X509 *x509) {
426 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800427}
428
David Benjamin59937042015-09-19 13:04:22 -0400429int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
430 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800431}
432
David Benjamin32a66d52016-07-13 22:03:11 -0400433int ssl_has_certificate(const SSL *ssl) {
434 return ssl->cert->x509 != NULL && ssl_has_private_key(ssl);
435}
436
David Benjamin5c900c82016-07-13 23:03:26 -0400437STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert,
438 uint8_t *out_leaf_sha256, CBS *cbs) {
439 STACK_OF(X509) *ret = sk_X509_new_null();
440 if (ret == NULL) {
441 *out_alert = SSL_AD_INTERNAL_ERROR;
442 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
443 return NULL;
444 }
445
446 X509 *x = NULL;
447 CBS certificate_list;
448 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
449 *out_alert = SSL_AD_DECODE_ERROR;
450 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
451 goto err;
452 }
453
454 while (CBS_len(&certificate_list) > 0) {
455 CBS certificate;
456 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
457 *out_alert = SSL_AD_DECODE_ERROR;
458 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
459 goto err;
460 }
461
462 /* Retain the hash of the leaf certificate if requested. */
463 if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) {
464 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
465 }
466
467 /* A u24 length cannot overflow a long. */
468 const uint8_t *data = CBS_data(&certificate);
469 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
470 if (x == NULL || data != CBS_data(&certificate) + CBS_len(&certificate)) {
471 *out_alert = SSL_AD_DECODE_ERROR;
472 goto err;
473 }
474 if (!sk_X509_push(ret, x)) {
475 *out_alert = SSL_AD_INTERNAL_ERROR;
476 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
477 goto err;
478 }
479 x = NULL;
480 }
481
482 return ret;
483
484err:
485 X509_free(x);
486 sk_X509_pop_free(ret, X509_free);
487 return NULL;
488}
489
David Benjamin75836432016-06-17 18:48:29 -0400490int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
491 int len = i2d_X509(x509, NULL);
492 if (len < 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800493 return 0;
494 }
David Benjamin75836432016-06-17 18:48:29 -0400495 uint8_t *buf;
496 if (!CBB_add_space(cbb, &buf, len)) {
497 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
498 return 0;
Steven Valdezb32a9152016-04-26 12:57:22 -0400499 }
David Benjamin75836432016-06-17 18:48:29 -0400500 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
501 return 0;
502 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 return 1;
504}
Adam Langley95c29f32014-06-20 12:00:00 -0700505
David Benjamin75836432016-06-17 18:48:29 -0400506static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
507 CBB child;
508 return CBB_add_u24_length_prefixed(cbb, &child) &&
509 ssl_add_cert_to_cbb(&child, x509) &&
510 CBB_flush(cbb);
511}
512
513int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400514 if (!ssl_has_certificate(ssl)) {
515 return CBB_add_u24(cbb, 0);
516 }
517
David Benjamind1d80782015-07-05 11:54:09 -0400518 CERT *cert = ssl->cert;
David Benjamind1d80782015-07-05 11:54:09 -0400519 X509 *x = cert->x509;
Adam Langley95c29f32014-06-20 12:00:00 -0700520
David Benjamin75836432016-06-17 18:48:29 -0400521 CBB child;
522 if (!CBB_add_u24_length_prefixed(cbb, &child)) {
523 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
524 return 0;
525 }
526
527 int no_chain = 0;
528 STACK_OF(X509) *chain = cert->chain;
David Benjamind1d80782015-07-05 11:54:09 -0400529 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 no_chain = 1;
531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin605641e2015-05-03 15:14:04 -0400533 if (no_chain) {
Steven Valdez57a6f3c2016-06-28 15:37:37 -0400534 if (!ssl_add_cert_with_length(&child, x)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 return 0;
536 }
David Benjamin605641e2015-05-03 15:14:04 -0400537
David Benjamin75836432016-06-17 18:48:29 -0400538 size_t i;
David Benjamind1d80782015-07-05 11:54:09 -0400539 for (i = 0; i < sk_X509_num(chain); i++) {
540 x = sk_X509_value(chain, i);
David Benjamin75836432016-06-17 18:48:29 -0400541 if (!ssl_add_cert_with_length(&child, x)) {
David Benjamin605641e2015-05-03 15:14:04 -0400542 return 0;
543 }
544 }
545 } else {
546 X509_STORE_CTX xs_ctx;
547
David Benjamind27441a2015-08-09 11:05:17 -0400548 if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400549 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
David Benjamin605641e2015-05-03 15:14:04 -0400550 return 0;
551 }
552 X509_verify_cert(&xs_ctx);
553 /* Don't leave errors in the queue */
554 ERR_clear_error();
David Benjamin75836432016-06-17 18:48:29 -0400555
556 size_t i;
David Benjamin605641e2015-05-03 15:14:04 -0400557 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
558 x = sk_X509_value(xs_ctx.chain, i);
David Benjamin75836432016-06-17 18:48:29 -0400559 if (!ssl_add_cert_with_length(&child, x)) {
David Benjamin605641e2015-05-03 15:14:04 -0400560 X509_STORE_CTX_cleanup(&xs_ctx);
561 return 0;
562 }
563 }
564 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 }
566
David Benjamin75836432016-06-17 18:48:29 -0400567 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800568}
Adam Langley95c29f32014-06-20 12:00:00 -0700569
David Benjamine0332e82016-07-13 22:40:36 -0400570static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
571 return X509_NAME_cmp(*a, *b);
572}
573
574STACK_OF(X509_NAME) *
575 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
576 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
577 X509_NAME *name = NULL;
578 if (ret == NULL) {
579 *out_alert = SSL_AD_INTERNAL_ERROR;
580 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
581 return NULL;
582 }
583
584 CBS child;
585 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
586 *out_alert = SSL_AD_DECODE_ERROR;
587 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
588 goto err;
589 }
590
591 while (CBS_len(&child) > 0) {
592 CBS distinguished_name;
593 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
594 *out_alert = SSL_AD_DECODE_ERROR;
595 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
596 goto err;
597 }
598
599 const uint8_t *ptr = CBS_data(&distinguished_name);
600 /* A u16 length cannot overflow a long. */
601 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
602 if (name == NULL ||
603 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
604 *out_alert = SSL_AD_DECODE_ERROR;
605 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
606 goto err;
607 }
608
609 if (!sk_X509_NAME_push(ret, name)) {
610 *out_alert = SSL_AD_INTERNAL_ERROR;
611 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
612 goto err;
613 }
614 name = NULL;
615 }
616
617 return ret;
618
619err:
620 X509_NAME_free(name);
621 sk_X509_NAME_pop_free(ret, X509_NAME_free);
622 return NULL;
623}
624
David Benjamin32a66d52016-07-13 22:03:11 -0400625int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
626 CBB child, name_cbb;
627 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
628 return 0;
629 }
630
631 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
632 if (sk == NULL) {
633 return CBB_flush(cbb);
634 }
635
636 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
637 X509_NAME *name = sk_X509_NAME_value(sk, i);
638 int len = i2d_X509_NAME(name, NULL);
639 if (len < 0) {
640 return 0;
641 }
642 uint8_t *ptr;
643 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
644 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
645 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
646 return 0;
647 }
648 }
649
650 return CBB_flush(cbb);
651}
652
David Benjamin13f1ebe2016-07-20 10:11:04 +0200653int ssl_do_client_cert_cb(SSL *ssl, int *out_should_retry) {
654 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
655 return 1;
656 }
657
658 X509 *x509 = NULL;
659 EVP_PKEY *pkey = NULL;
660 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
661 if (ret < 0) {
662 *out_should_retry = 1;
663 return 0;
664 }
665
666 if (ret != 0) {
667 if (!SSL_use_certificate(ssl, x509) ||
668 !SSL_use_PrivateKey(ssl, pkey)) {
669 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
670 *out_should_retry = 0;
671 return 0;
672 }
673 }
674
675 X509_free(x509);
676 EVP_PKEY_free(pkey);
677 return 1;
678}
679
Adam Langleyd323f4b2016-03-01 15:58:14 -0800680static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
681 X509_STORE_free(*store_ptr);
682 *store_ptr = new_store;
683
684 if (new_store != NULL && take_ref) {
685 X509_STORE_up_ref(new_store);
686 }
687
688 return 1;
689}
690
691int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
692 return set_cert_store(&ctx->cert->verify_store, store, 0);
693}
694
695int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
696 return set_cert_store(&ctx->cert->verify_store, store, 1);
697}
698
699int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
700 return set_cert_store(&ssl->cert->verify_store, store, 0);
701}
702
703int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
704 return set_cert_store(&ssl->cert->verify_store, store, 1);
705}
706
David Benjamin11c0f8e2015-07-06 00:18:15 -0400707int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
708 return ssl_cert_set0_chain(ctx->cert, chain);
709}
710
711int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
712 return ssl_cert_set1_chain(ctx->cert, chain);
713}
714
715int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
716 return ssl_cert_set0_chain(ssl->cert, chain);
717}
718
719int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
720 return ssl_cert_set1_chain(ssl->cert, chain);
721}
722
723int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
724 return ssl_cert_add0_chain_cert(ctx->cert, x509);
725}
726
727int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
728 return ssl_cert_add1_chain_cert(ctx->cert, x509);
729}
730
731int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
732 return SSL_CTX_add0_chain_cert(ctx, x509);
733}
734
735int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
736 return ssl_cert_add0_chain_cert(ssl->cert, x509);
737}
738
739int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
740 return ssl_cert_add1_chain_cert(ssl->cert, x509);
741}
742
743int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
744 return SSL_CTX_set0_chain(ctx, NULL);
745}
746
747int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
748 return SSL_CTX_clear_chain_certs(ctx);
749}
750
751int SSL_clear_chain_certs(SSL *ssl) {
752 return SSL_set0_chain(ssl, NULL);
753}
754
755int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
756 *out_chain = ctx->cert->chain;
757 return 1;
758}
759
760int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
761 STACK_OF(X509) **out_chain) {
762 return SSL_CTX_get0_chain_certs(ctx, out_chain);
763}
764
765int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
766 *out_chain = ssl->cert->chain;
767 return 1;
768}
Steven Valdezbf5aa842016-07-15 07:07:40 -0400769
770int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf) {
771 int ret = 0;
772 EVP_PKEY *pkey = X509_get_pubkey(leaf);
773 if (pkey == NULL) {
774 goto err;
775 }
776
777 /* Check the certificate's type matches the cipher. */
778 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
779 int expected_type = ssl_cipher_get_key_type(cipher);
780 assert(expected_type != EVP_PKEY_NONE);
781 if (pkey->type != expected_type) {
782 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
783 goto err;
784 }
785
786 if (cipher->algorithm_auth & SSL_aECDSA) {
787 /* TODO(davidben): This behavior is preserved from upstream. Should key
788 * usages be checked in other cases as well? */
789 /* This call populates the ex_flags field correctly */
790 X509_check_purpose(leaf, -1, 0);
791 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
792 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
793 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
794 goto err;
795 }
796
797 if (!tls1_check_ec_cert(ssl, leaf)) {
798 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
799 goto err;
800 }
801 }
802
803 ret = 1;
804
805err:
806 EVP_PKEY_free(pkey);
807 return ret;
808}