blob: 7f6cf63668e0fd93b626ac8aa6deb16c7d12f979 [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>
David Benjamin680ca962015-06-18 12:37:23 -0400123#include <openssl/ec_key.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124#include <openssl/dh.h>
125#include <openssl/err.h>
126#include <openssl/mem.h>
David Benjamin5c900c82016-07-13 23:03:26 -0400127#include <openssl/sha.h>
David Benjamin680ca962015-06-18 12:37:23 -0400128#include <openssl/x509.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700129#include <openssl/x509v3.h>
130
Adam Langley0da323a2015-05-15 12:49:30 -0700131#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400132#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700133
Adam Langley95c29f32014-06-20 12:00:00 -0700134
Adam Langleyfcf25832014-12-18 17:42:32 -0800135int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
David Benjaminaa585132015-06-29 23:36:17 -0400136 /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
137 * reserved app_data slot. Before ex_data was introduced, app_data was used.
138 * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
139 * works. */
140 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800141}
Adam Langley95c29f32014-06-20 12:00:00 -0700142
Adam Langleyfcf25832014-12-18 17:42:32 -0800143CERT *ssl_cert_new(void) {
Brian Smith5ba06892016-02-07 09:36:04 -1000144 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800145 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400146 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800147 return NULL;
148 }
149 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 return ret;
152}
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154CERT *ssl_cert_dup(CERT *cert) {
Brian Smith5ba06892016-02-07 09:36:04 -1000155 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800156 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400157 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800158 return NULL;
159 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800160 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700161
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800162 if (cert->x509_leaf != NULL) {
163 X509_up_ref(cert->x509_leaf);
164 ret->x509_leaf = cert->x509_leaf;
David Benjamind1d80782015-07-05 11:54:09 -0400165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamind1d80782015-07-05 11:54:09 -0400167 if (cert->privatekey != NULL) {
Adam Langley310d3f62016-07-12 10:39:20 -0700168 EVP_PKEY_up_ref(cert->privatekey);
169 ret->privatekey = cert->privatekey;
David Benjamind1d80782015-07-05 11:54:09 -0400170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800172 if (cert->x509_chain) {
173 ret->x509_chain = X509_chain_up_ref(cert->x509_chain);
174 if (!ret->x509_chain) {
David Benjamin3570d732015-06-29 00:28:17 -0400175 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamind1d80782015-07-05 11:54:09 -0400176 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800177 }
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Tom Thorogood66b2fe82016-03-06 20:08:38 +1030180 ret->key_method = cert->key_method;
181
David Benjamin0fc37ef2016-08-17 15:29:46 -0400182 ret->mask_k = cert->mask_k;
183 ret->mask_a = cert->mask_a;
184
185 if (cert->dh_tmp != NULL) {
186 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
187 if (ret->dh_tmp == NULL) {
188 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
189 goto err;
190 }
191 }
192 ret->dh_tmp_cb = cert->dh_tmp_cb;
193
194 if (cert->sigalgs != NULL) {
195 ret->sigalgs =
196 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
197 if (ret->sigalgs == NULL) {
198 goto err;
199 }
200 }
201 ret->num_sigalgs = cert->num_sigalgs;
202
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 ret->cert_cb = cert->cert_cb;
204 ret->cert_cb_arg = cert->cert_cb_arg;
205
Adam Langleyd323f4b2016-03-01 15:58:14 -0800206 if (cert->verify_store != NULL) {
207 X509_STORE_up_ref(cert->verify_store);
208 ret->verify_store = cert->verify_store;
209 }
210
Adam Langleyfcf25832014-12-18 17:42:32 -0800211 return ret;
212
Adam Langley95c29f32014-06-20 12:00:00 -0700213err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 ssl_cert_free(ret);
215 return NULL;
216}
Adam Langley95c29f32014-06-20 12:00:00 -0700217
218/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400219void ssl_cert_clear_certs(CERT *cert) {
220 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 return;
222 }
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800224 X509_free(cert->x509_leaf);
225 cert->x509_leaf = NULL;
David Benjamind1d80782015-07-05 11:54:09 -0400226 EVP_PKEY_free(cert->privatekey);
227 cert->privatekey = NULL;
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800228 sk_X509_pop_free(cert->x509_chain, X509_free);
229 cert->x509_chain = NULL;
David Benjamin71d2e542015-07-06 00:30:25 -0400230 cert->key_method = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800231}
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langleyfcf25832014-12-18 17:42:32 -0800233void ssl_cert_free(CERT *c) {
234 if (c == NULL) {
235 return;
236 }
Adam Langley95c29f32014-06-20 12:00:00 -0700237
David Benjamin2755a3e2015-04-22 16:17:58 -0400238 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 ssl_cert_clear_certs(c);
David Benjamind246b812016-07-08 15:07:02 -0700241 OPENSSL_free(c->sigalgs);
Adam Langleyd323f4b2016-03-01 15:58:14 -0800242 X509_STORE_free(c->verify_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 OPENSSL_free(c);
245}
Adam Langley95c29f32014-06-20 12:00:00 -0700246
David Benjamind1d80782015-07-05 11:54:09 -0400247int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800248 sk_X509_pop_free(cert->x509_chain, X509_free);
249 cert->x509_chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 return 1;
251}
Adam Langley95c29f32014-06-20 12:00:00 -0700252
David Benjamind1d80782015-07-05 11:54:09 -0400253int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400254 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400255 if (chain == NULL) {
256 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400260 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 return 0;
262 }
Adam Langley95c29f32014-06-20 12:00:00 -0700263
David Benjamind1d80782015-07-05 11:54:09 -0400264 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 sk_X509_pop_free(dchain, X509_free);
266 return 0;
267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 return 1;
270}
Adam Langley95c29f32014-06-20 12:00:00 -0700271
David Benjamind1d80782015-07-05 11:54:09 -0400272int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800273 if (cert->x509_chain == NULL) {
274 cert->x509_chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 }
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800276 if (cert->x509_chain == NULL || !sk_X509_push(cert->x509_chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 return 0;
278 }
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 return 1;
281}
Adam Langley95c29f32014-06-20 12:00:00 -0700282
David Benjamind1d80782015-07-05 11:54:09 -0400283int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
284 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 return 0;
286 }
Adam Langley95c29f32014-06-20 12:00:00 -0700287
David Benjamind1d80782015-07-05 11:54:09 -0400288 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return 1;
290}
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
293 c->cert_cb = cb;
294 c->cert_cb_arg = arg;
295}
Adam Langley95c29f32014-06-20 12:00:00 -0700296
David Benjamin7aa31d62016-08-08 21:38:32 -0400297int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
298 STACK_OF(X509) *cert_chain) {
David Benjamin306ece32015-09-17 13:46:22 -0400299 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800300 return 0;
301 }
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langleyd323f4b2016-03-01 15:58:14 -0800303 X509_STORE *verify_store = ssl->ctx->cert_store;
304 if (ssl->cert->verify_store != NULL) {
305 verify_store = ssl->cert->verify_store;
306 }
307
David Benjamin306ece32015-09-17 13:46:22 -0400308 X509 *leaf = sk_X509_value(cert_chain, 0);
309 int ret = 0;
310 X509_STORE_CTX ctx;
Adam Langleyd323f4b2016-03-01 15:58:14 -0800311 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
David Benjamin3570d732015-06-29 00:28:17 -0400312 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800313 return 0;
314 }
David Benjamin306ece32015-09-17 13:46:22 -0400315 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
316 ssl)) {
317 goto err;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 /* We need to inherit the verify parameters. These can be determined by the
321 * context: if its a server it will verify SSL client certificates or vice
322 * versa. */
David Benjamin306ece32015-09-17 13:46:22 -0400323 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 /* Anything non-default in "param" should overwrite anything in the ctx. */
David Benjamin306ece32015-09-17 13:46:22 -0400326 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700327
David Benjamin306ece32015-09-17 13:46:22 -0400328 if (ssl->verify_callback) {
329 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 }
Adam Langley95c29f32014-06-20 12:00:00 -0700331
David Benjamine455e512016-08-01 20:11:13 -0400332 int verify_ret;
David Benjamin306ece32015-09-17 13:46:22 -0400333 if (ssl->ctx->app_verify_callback != NULL) {
David Benjamine455e512016-08-01 20:11:13 -0400334 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 } else {
David Benjamine455e512016-08-01 20:11:13 -0400336 verify_ret = X509_verify_cert(&ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langleya6cd1852016-08-26 09:38:13 -0700339 *out_verify_result = ctx.error;
340
David Benjamine455e512016-08-01 20:11:13 -0400341 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
342 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjamin7aa31d62016-08-08 21:38:32 -0400343 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjamine455e512016-08-01 20:11:13 -0400344 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
345 goto err;
346 }
347
348 ERR_clear_error();
349 ret = 1;
350
David Benjamin306ece32015-09-17 13:46:22 -0400351err:
352 X509_STORE_CTX_cleanup(&ctx);
353 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800354}
Adam Langley95c29f32014-06-20 12:00:00 -0700355
David Benjamin60da0cd2015-05-03 15:21:28 -0400356static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
357 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400358 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 *ca_list = name_list;
360}
Adam Langley95c29f32014-06-20 12:00:00 -0700361
David Benjamin59937042015-09-19 13:04:22 -0400362STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
363 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
364 if (ret == NULL) {
365 return NULL;
366 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800367
David Benjamin54091232016-09-05 12:47:25 -0400368 for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
David Benjamin59937042015-09-19 13:04:22 -0400369 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
David Benjamin59937042015-09-19 13:04:22 -0400371 X509_NAME_free(name);
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 sk_X509_NAME_pop_free(ret, X509_NAME_free);
373 return NULL;
374 }
375 }
376
377 return ret;
378}
379
David Benjamin59937042015-09-19 13:04:22 -0400380void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
381 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800382}
383
David Benjamin60da0cd2015-05-03 15:21:28 -0400384void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
David Benjamin59937042015-09-19 13:04:22 -0400385 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyfcf25832014-12-18 17:42:32 -0800386}
387
David Benjamin60da0cd2015-05-03 15:21:28 -0400388STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 return ctx->client_CA;
390}
391
David Benjamin1d128f32015-09-08 17:41:40 -0400392STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
393 /* For historical reasons, this function is used both to query configuration
394 * state on a server as well as handshake state on a client. However, whether
395 * |ssl| is a client or server is not known until explicitly configured with
396 * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
397 * indeterminate mode and |ssl->server| is unset. */
398 if (ssl->handshake_func != NULL && !ssl->server) {
David Benjamina0486782016-10-06 19:11:32 -0400399 if (ssl->s3->hs != NULL) {
400 return ssl->s3->hs->ca_names;
401 }
402
403 return NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 }
David Benjamin1d128f32015-09-08 17:41:40 -0400405
406 if (ssl->client_CA != NULL) {
407 return ssl->client_CA;
408 }
409 return ssl->ctx->client_CA;
Adam Langleyfcf25832014-12-18 17:42:32 -0800410}
411
David Benjamin59937042015-09-19 13:04:22 -0400412static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 X509_NAME *name;
414
David Benjamin59937042015-09-19 13:04:22 -0400415 if (x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 return 0;
417 }
418 if (*sk == NULL) {
419 *sk = sk_X509_NAME_new_null();
420 if (*sk == NULL) {
421 return 0;
422 }
423 }
424
David Benjamin59937042015-09-19 13:04:22 -0400425 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyfcf25832014-12-18 17:42:32 -0800426 if (name == NULL) {
427 return 0;
428 }
429
430 if (!sk_X509_NAME_push(*sk, name)) {
431 X509_NAME_free(name);
432 return 0;
433 }
434
435 return 1;
436}
437
David Benjamin59937042015-09-19 13:04:22 -0400438int SSL_add_client_CA(SSL *ssl, X509 *x509) {
439 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800440}
441
David Benjamin59937042015-09-19 13:04:22 -0400442int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
443 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800444}
445
David Benjamin32a66d52016-07-13 22:03:11 -0400446int ssl_has_certificate(const SSL *ssl) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800447 return ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl);
David Benjamin32a66d52016-07-13 22:03:11 -0400448}
449
Steven Valdeza833c352016-11-01 13:39:36 -0400450X509 *ssl_parse_x509(CBS *cbs) {
451 if (CBS_len(cbs) > LONG_MAX) {
452 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
453 return NULL;
454 }
455 const uint8_t *ptr = CBS_data(cbs);
456 X509 *ret = d2i_X509(NULL, &ptr, (long)CBS_len(cbs));
457 if (ret == NULL) {
458 return NULL;
459 }
460 CBS_skip(cbs, ptr - CBS_data(cbs));
461 return ret;
462}
463
David Benjamin5c900c82016-07-13 23:03:26 -0400464STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert,
465 uint8_t *out_leaf_sha256, CBS *cbs) {
466 STACK_OF(X509) *ret = sk_X509_new_null();
467 if (ret == NULL) {
468 *out_alert = SSL_AD_INTERNAL_ERROR;
469 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
470 return NULL;
471 }
472
473 X509 *x = NULL;
474 CBS certificate_list;
475 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
476 *out_alert = SSL_AD_DECODE_ERROR;
477 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
478 goto err;
479 }
480
481 while (CBS_len(&certificate_list) > 0) {
482 CBS certificate;
483 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
484 *out_alert = SSL_AD_DECODE_ERROR;
485 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
486 goto err;
487 }
488
489 /* Retain the hash of the leaf certificate if requested. */
490 if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) {
491 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
492 }
493
Steven Valdeza833c352016-11-01 13:39:36 -0400494 x = ssl_parse_x509(&certificate);
495 if (x == NULL || CBS_len(&certificate) != 0) {
David Benjamin5c900c82016-07-13 23:03:26 -0400496 *out_alert = SSL_AD_DECODE_ERROR;
497 goto err;
498 }
499 if (!sk_X509_push(ret, x)) {
500 *out_alert = SSL_AD_INTERNAL_ERROR;
501 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
502 goto err;
503 }
504 x = NULL;
505 }
506
507 return ret;
508
509err:
510 X509_free(x);
511 sk_X509_pop_free(ret, X509_free);
512 return NULL;
513}
514
David Benjamin75836432016-06-17 18:48:29 -0400515int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
516 int len = i2d_X509(x509, NULL);
517 if (len < 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 return 0;
519 }
David Benjamin75836432016-06-17 18:48:29 -0400520 uint8_t *buf;
521 if (!CBB_add_space(cbb, &buf, len)) {
522 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
523 return 0;
Steven Valdezb32a9152016-04-26 12:57:22 -0400524 }
David Benjamin75836432016-06-17 18:48:29 -0400525 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
526 return 0;
527 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 return 1;
529}
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin75836432016-06-17 18:48:29 -0400531static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
532 CBB child;
533 return CBB_add_u24_length_prefixed(cbb, &child) &&
534 ssl_add_cert_to_cbb(&child, x509) &&
535 CBB_flush(cbb);
536}
537
538int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
David Benjamin32a66d52016-07-13 22:03:11 -0400539 if (!ssl_has_certificate(ssl)) {
540 return CBB_add_u24(cbb, 0);
541 }
542
David Benjamind1d80782015-07-05 11:54:09 -0400543 CERT *cert = ssl->cert;
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800544 X509 *x = cert->x509_leaf;
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamin75836432016-06-17 18:48:29 -0400546 CBB child;
547 if (!CBB_add_u24_length_prefixed(cbb, &child)) {
548 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
549 return 0;
550 }
551
552 int no_chain = 0;
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800553 STACK_OF(X509) *chain = cert->x509_chain;
David Benjamind1d80782015-07-05 11:54:09 -0400554 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800555 no_chain = 1;
556 }
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin605641e2015-05-03 15:14:04 -0400558 if (no_chain) {
Steven Valdez57a6f3c2016-06-28 15:37:37 -0400559 if (!ssl_add_cert_with_length(&child, x)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 return 0;
561 }
David Benjamin605641e2015-05-03 15:14:04 -0400562
David Benjamin54091232016-09-05 12:47:25 -0400563 for (size_t i = 0; i < sk_X509_num(chain); i++) {
David Benjamind1d80782015-07-05 11:54:09 -0400564 x = sk_X509_value(chain, i);
David Benjamin75836432016-06-17 18:48:29 -0400565 if (!ssl_add_cert_with_length(&child, x)) {
David Benjamin605641e2015-05-03 15:14:04 -0400566 return 0;
567 }
568 }
569 } else {
570 X509_STORE_CTX xs_ctx;
571
David Benjamind27441a2015-08-09 11:05:17 -0400572 if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400573 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
David Benjamin605641e2015-05-03 15:14:04 -0400574 return 0;
575 }
576 X509_verify_cert(&xs_ctx);
577 /* Don't leave errors in the queue */
578 ERR_clear_error();
David Benjamin75836432016-06-17 18:48:29 -0400579
David Benjamin54091232016-09-05 12:47:25 -0400580 for (size_t i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
David Benjamin605641e2015-05-03 15:14:04 -0400581 x = sk_X509_value(xs_ctx.chain, i);
David Benjamin75836432016-06-17 18:48:29 -0400582 if (!ssl_add_cert_with_length(&child, x)) {
David Benjamin605641e2015-05-03 15:14:04 -0400583 X509_STORE_CTX_cleanup(&xs_ctx);
584 return 0;
585 }
586 }
587 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 }
589
David Benjamin75836432016-06-17 18:48:29 -0400590 return CBB_flush(cbb);
Adam Langleyfcf25832014-12-18 17:42:32 -0800591}
Adam Langley95c29f32014-06-20 12:00:00 -0700592
David Benjamine0332e82016-07-13 22:40:36 -0400593static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
594 return X509_NAME_cmp(*a, *b);
595}
596
597STACK_OF(X509_NAME) *
598 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
599 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
600 X509_NAME *name = NULL;
601 if (ret == NULL) {
602 *out_alert = SSL_AD_INTERNAL_ERROR;
603 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
604 return NULL;
605 }
606
607 CBS child;
608 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
609 *out_alert = SSL_AD_DECODE_ERROR;
610 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
611 goto err;
612 }
613
614 while (CBS_len(&child) > 0) {
615 CBS distinguished_name;
616 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
617 *out_alert = SSL_AD_DECODE_ERROR;
618 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
619 goto err;
620 }
621
622 const uint8_t *ptr = CBS_data(&distinguished_name);
623 /* A u16 length cannot overflow a long. */
624 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
625 if (name == NULL ||
626 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
627 *out_alert = SSL_AD_DECODE_ERROR;
628 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
629 goto err;
630 }
631
632 if (!sk_X509_NAME_push(ret, name)) {
633 *out_alert = SSL_AD_INTERNAL_ERROR;
634 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
635 goto err;
636 }
637 name = NULL;
638 }
639
640 return ret;
641
642err:
643 X509_NAME_free(name);
644 sk_X509_NAME_pop_free(ret, X509_NAME_free);
645 return NULL;
646}
647
David Benjamin32a66d52016-07-13 22:03:11 -0400648int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
649 CBB child, name_cbb;
650 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
651 return 0;
652 }
653
654 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
655 if (sk == NULL) {
656 return CBB_flush(cbb);
657 }
658
659 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
660 X509_NAME *name = sk_X509_NAME_value(sk, i);
661 int len = i2d_X509_NAME(name, NULL);
662 if (len < 0) {
663 return 0;
664 }
665 uint8_t *ptr;
666 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
667 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
668 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
669 return 0;
670 }
671 }
672
673 return CBB_flush(cbb);
674}
675
David Benjamin13f1ebe2016-07-20 10:11:04 +0200676int ssl_do_client_cert_cb(SSL *ssl, int *out_should_retry) {
677 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
678 return 1;
679 }
680
681 X509 *x509 = NULL;
682 EVP_PKEY *pkey = NULL;
683 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
684 if (ret < 0) {
685 *out_should_retry = 1;
686 return 0;
687 }
688
689 if (ret != 0) {
690 if (!SSL_use_certificate(ssl, x509) ||
691 !SSL_use_PrivateKey(ssl, pkey)) {
692 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
693 *out_should_retry = 0;
694 return 0;
695 }
696 }
697
698 X509_free(x509);
699 EVP_PKEY_free(pkey);
700 return 1;
701}
702
Adam Langleyd323f4b2016-03-01 15:58:14 -0800703static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
704 X509_STORE_free(*store_ptr);
705 *store_ptr = new_store;
706
707 if (new_store != NULL && take_ref) {
708 X509_STORE_up_ref(new_store);
709 }
710
711 return 1;
712}
713
714int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
715 return set_cert_store(&ctx->cert->verify_store, store, 0);
716}
717
718int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
719 return set_cert_store(&ctx->cert->verify_store, store, 1);
720}
721
722int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
723 return set_cert_store(&ssl->cert->verify_store, store, 0);
724}
725
726int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
727 return set_cert_store(&ssl->cert->verify_store, store, 1);
728}
729
David Benjamin11c0f8e2015-07-06 00:18:15 -0400730int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
731 return ssl_cert_set0_chain(ctx->cert, chain);
732}
733
734int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
735 return ssl_cert_set1_chain(ctx->cert, chain);
736}
737
738int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
739 return ssl_cert_set0_chain(ssl->cert, chain);
740}
741
742int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
743 return ssl_cert_set1_chain(ssl->cert, chain);
744}
745
746int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
747 return ssl_cert_add0_chain_cert(ctx->cert, x509);
748}
749
750int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
751 return ssl_cert_add1_chain_cert(ctx->cert, x509);
752}
753
754int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
755 return SSL_CTX_add0_chain_cert(ctx, x509);
756}
757
758int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
759 return ssl_cert_add0_chain_cert(ssl->cert, x509);
760}
761
762int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
763 return ssl_cert_add1_chain_cert(ssl->cert, x509);
764}
765
766int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
767 return SSL_CTX_set0_chain(ctx, NULL);
768}
769
770int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
771 return SSL_CTX_clear_chain_certs(ctx);
772}
773
774int SSL_clear_chain_certs(SSL *ssl) {
775 return SSL_set0_chain(ssl, NULL);
776}
777
778int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800779 *out_chain = ctx->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -0400780 return 1;
781}
782
783int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
784 STACK_OF(X509) **out_chain) {
785 return SSL_CTX_get0_chain_certs(ctx, out_chain);
786}
787
788int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
Adam Langleyc5ac2b62016-11-07 12:02:35 -0800789 *out_chain = ssl->cert->x509_chain;
David Benjamin11c0f8e2015-07-06 00:18:15 -0400790 return 1;
791}
Steven Valdezbf5aa842016-07-15 07:07:40 -0400792
793int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf) {
David Benjamin938fa7c2016-10-07 00:27:05 -0400794 assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
795
Steven Valdezbf5aa842016-07-15 07:07:40 -0400796 int ret = 0;
797 EVP_PKEY *pkey = X509_get_pubkey(leaf);
798 if (pkey == NULL) {
799 goto err;
800 }
801
802 /* Check the certificate's type matches the cipher. */
803 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
804 int expected_type = ssl_cipher_get_key_type(cipher);
805 assert(expected_type != EVP_PKEY_NONE);
806 if (pkey->type != expected_type) {
807 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
808 goto err;
809 }
810
811 if (cipher->algorithm_auth & SSL_aECDSA) {
812 /* TODO(davidben): This behavior is preserved from upstream. Should key
813 * usages be checked in other cases as well? */
814 /* This call populates the ex_flags field correctly */
815 X509_check_purpose(leaf, -1, 0);
816 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
817 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
818 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
819 goto err;
820 }
821
David Benjamin938fa7c2016-10-07 00:27:05 -0400822 EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
823 if (ec_key == NULL) {
824 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
825 goto err;
826 }
827
828 /* Check the key's group and point format are acceptable. */
829 uint16_t group_id;
830 if (!ssl_nid_to_group_id(
831 &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
832 !tls1_check_group_id(ssl, group_id) ||
833 EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
Steven Valdezbf5aa842016-07-15 07:07:40 -0400834 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
835 goto err;
836 }
837 }
838
839 ret = 1;
840
841err:
842 EVP_PKEY_free(pkey);
843 return ret;
844}