blob: e77027999732e7e8e0497a7b14826816bca665e2 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +0000115#include <openssl/ssl.h>
116
David Benjaminc895d6b2016-08-11 13:26:41 -0400117#include <assert.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700118#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800119
Adam Langleyd9e397b2015-01-22 14:27:53 -0800120#include <openssl/bn.h>
121#include <openssl/buf.h>
Adam Langley56d25032015-06-23 16:20:13 -0700122#include <openssl/ec_key.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123#include <openssl/dh.h>
124#include <openssl/err.h>
125#include <openssl/mem.h>
David Benjaminc895d6b2016-08-11 13:26:41 -0400126#include <openssl/sha.h>
Adam Langley56d25032015-06-23 16:20:13 -0700127#include <openssl/x509.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800128#include <openssl/x509v3.h>
129
Adam Langleyf4e42722015-06-04 17:45:09 -0700130#include "../crypto/internal.h"
Adam Langleye9ada862015-05-11 17:20:37 -0700131#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800132
133
134int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
Kenny Rootb8494592015-09-25 02:29:14 +0000135 /* 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 Langleyd9e397b2015-01-22 14:27:53 -0800140}
141
142CERT *ssl_cert_new(void) {
David Benjamin4969cc92016-04-22 15:02:23 -0400143 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144 if (ret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000145 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146 return NULL;
147 }
148 memset(ret, 0, sizeof(CERT));
149
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 return ret;
151}
152
153CERT *ssl_cert_dup(CERT *cert) {
David Benjamin4969cc92016-04-22 15:02:23 -0400154 CERT *ret = OPENSSL_malloc(sizeof(CERT));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800155 if (ret == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000156 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157 return NULL;
158 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159 memset(ret, 0, sizeof(CERT));
160
Kenny Rootb8494592015-09-25 02:29:14 +0000161 if (cert->x509 != NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400162 X509_up_ref(cert->x509);
163 ret->x509 = cert->x509;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164 }
165
Kenny Rootb8494592015-09-25 02:29:14 +0000166 if (cert->privatekey != NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400167 EVP_PKEY_up_ref(cert->privatekey);
168 ret->privatekey = cert->privatekey;
Kenny Roota04d78d2015-09-25 00:26:37 +0000169 }
170
Kenny Rootb8494592015-09-25 02:29:14 +0000171 if (cert->chain) {
172 ret->chain = X509_chain_up_ref(cert->chain);
173 if (!ret->chain) {
174 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Kenny Roota04d78d2015-09-25 00:26:37 +0000175 goto err;
176 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800177 }
178
David Benjamin4969cc92016-04-22 15:02:23 -0400179 ret->key_method = cert->key_method;
180
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400181 ret->mask_k = cert->mask_k;
182 ret->mask_a = cert->mask_a;
183
184 if (cert->dh_tmp != NULL) {
185 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
186 if (ret->dh_tmp == NULL) {
187 OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
188 goto err;
189 }
190 }
191 ret->dh_tmp_cb = cert->dh_tmp_cb;
192
193 if (cert->sigalgs != NULL) {
194 ret->sigalgs =
195 BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
196 if (ret->sigalgs == NULL) {
197 goto err;
198 }
199 }
200 ret->num_sigalgs = cert->num_sigalgs;
201
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 ret->cert_cb = cert->cert_cb;
203 ret->cert_cb_arg = cert->cert_cb_arg;
204
David Benjamin4969cc92016-04-22 15:02:23 -0400205 if (cert->verify_store != NULL) {
206 X509_STORE_up_ref(cert->verify_store);
207 ret->verify_store = cert->verify_store;
208 }
209
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 return ret;
211
212err:
213 ssl_cert_free(ret);
214 return NULL;
215}
216
217/* Free up and clear all certificates and chains */
Kenny Rootb8494592015-09-25 02:29:14 +0000218void ssl_cert_clear_certs(CERT *cert) {
219 if (cert == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 return;
221 }
222
Kenny Rootb8494592015-09-25 02:29:14 +0000223 X509_free(cert->x509);
224 cert->x509 = NULL;
225 EVP_PKEY_free(cert->privatekey);
226 cert->privatekey = NULL;
227 sk_X509_pop_free(cert->chain, X509_free);
228 cert->chain = NULL;
229 cert->key_method = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800230}
231
232void ssl_cert_free(CERT *c) {
233 if (c == NULL) {
234 return;
235 }
236
Adam Langleye9ada862015-05-11 17:20:37 -0700237 DH_free(c->dh_tmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238
239 ssl_cert_clear_certs(c);
David Benjaminc895d6b2016-08-11 13:26:41 -0400240 OPENSSL_free(c->sigalgs);
David Benjamin4969cc92016-04-22 15:02:23 -0400241 X509_STORE_free(c->verify_store);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800242
243 OPENSSL_free(c);
244}
245
Kenny Rootb8494592015-09-25 02:29:14 +0000246int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
247 sk_X509_pop_free(cert->chain, X509_free);
248 cert->chain = chain;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800249 return 1;
250}
251
Kenny Rootb8494592015-09-25 02:29:14 +0000252int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
Adam Langleye9ada862015-05-11 17:20:37 -0700253 STACK_OF(X509) *dchain;
Kenny Rootb8494592015-09-25 02:29:14 +0000254 if (chain == NULL) {
255 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 }
257
258 dchain = X509_chain_up_ref(chain);
Kenny Rootb8494592015-09-25 02:29:14 +0000259 if (dchain == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800260 return 0;
261 }
262
Kenny Rootb8494592015-09-25 02:29:14 +0000263 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 sk_X509_pop_free(dchain, X509_free);
265 return 0;
266 }
267
268 return 1;
269}
270
Kenny Rootb8494592015-09-25 02:29:14 +0000271int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
272 if (cert->chain == NULL) {
273 cert->chain = sk_X509_new_null();
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274 }
Kenny Rootb8494592015-09-25 02:29:14 +0000275 if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800276 return 0;
277 }
278
279 return 1;
280}
281
Kenny Rootb8494592015-09-25 02:29:14 +0000282int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
283 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800284 return 0;
285 }
286
Kenny Rootb8494592015-09-25 02:29:14 +0000287 X509_up_ref(x509);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800288 return 1;
289}
290
Adam Langleyd9e397b2015-01-22 14:27:53 -0800291void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
292 c->cert_cb = cb;
293 c->cert_cb_arg = arg;
294}
295
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400296int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
297 STACK_OF(X509) *cert_chain) {
Kenny Rootb8494592015-09-25 02:29:14 +0000298 if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
299 return 0;
Kenny Roota04d78d2015-09-25 00:26:37 +0000300 }
301
David Benjamin4969cc92016-04-22 15:02:23 -0400302 X509_STORE *verify_store = ssl->ctx->cert_store;
303 if (ssl->cert->verify_store != NULL) {
304 verify_store = ssl->cert->verify_store;
305 }
306
Kenny Rootb8494592015-09-25 02:29:14 +0000307 X509 *leaf = sk_X509_value(cert_chain, 0);
308 int ret = 0;
Kenny Roota04d78d2015-09-25 00:26:37 +0000309 X509_STORE_CTX ctx;
David Benjamin4969cc92016-04-22 15:02:23 -0400310 if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000311 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langley1e4884f2015-09-24 10:57:52 -0700312 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800313 }
Kenny Rootb8494592015-09-25 02:29:14 +0000314 if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
315 ssl)) {
316 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800318
319 /* We need to inherit the verify parameters. These can be determined by the
320 * context: if its a server it will verify SSL client certificates or vice
321 * versa. */
Kenny Rootb8494592015-09-25 02:29:14 +0000322 X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
Adam Langleyd9e397b2015-01-22 14:27:53 -0800323
324 /* Anything non-default in "param" should overwrite anything in the ctx. */
Kenny Rootb8494592015-09-25 02:29:14 +0000325 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800326
Kenny Rootb8494592015-09-25 02:29:14 +0000327 if (ssl->verify_callback) {
328 X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800329 }
330
David Benjaminc895d6b2016-08-11 13:26:41 -0400331 int verify_ret;
Kenny Rootb8494592015-09-25 02:29:14 +0000332 if (ssl->ctx->app_verify_callback != NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400333 verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400335 verify_ret = X509_verify_cert(&ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800336 }
337
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400338 *out_verify_result = ctx.error;
Kenny Roota04d78d2015-09-25 00:26:37 +0000339
David Benjaminc895d6b2016-08-11 13:26:41 -0400340 /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
341 if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400342 ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
David Benjaminc895d6b2016-08-11 13:26:41 -0400343 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
344 goto err;
345 }
346
347 ERR_clear_error();
348 ret = 1;
349
Kenny Rootb8494592015-09-25 02:29:14 +0000350err:
351 X509_STORE_CTX_cleanup(&ctx);
352 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800353}
354
Adam Langleye9ada862015-05-11 17:20:37 -0700355static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
356 STACK_OF(X509_NAME) *name_list) {
357 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800358 *ca_list = name_list;
359}
360
Kenny Rootb8494592015-09-25 02:29:14 +0000361STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
362 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
363 if (ret == NULL) {
364 return NULL;
365 }
Kenny Roota04d78d2015-09-25 00:26:37 +0000366
Kenny Rootb8494592015-09-25 02:29:14 +0000367 size_t i;
368 for (i = 0; i < sk_X509_NAME_num(list); i++) {
369 X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000371 X509_NAME_free(name);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 sk_X509_NAME_pop_free(ret, X509_NAME_free);
373 return NULL;
374 }
375 }
376
377 return ret;
378}
379
Kenny Rootb8494592015-09-25 02:29:14 +0000380void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
381 set_client_CA_list(&ssl->client_CA, name_list);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382}
383
Adam Langleye9ada862015-05-11 17:20:37 -0700384void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
Kenny Rootb8494592015-09-25 02:29:14 +0000385 set_client_CA_list(&ctx->client_CA, name_list);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800386}
387
Adam Langleye9ada862015-05-11 17:20:37 -0700388STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800389 return ctx->client_CA;
390}
391
Kenny Rootb8494592015-09-25 02:29:14 +0000392STACK_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) {
399 return ssl->s3->tmp.ca_names;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800400 }
Kenny Rootb8494592015-09-25 02:29:14 +0000401
402 if (ssl->client_CA != NULL) {
403 return ssl->client_CA;
404 }
405 return ssl->ctx->client_CA;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800406}
407
Kenny Rootb8494592015-09-25 02:29:14 +0000408static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409 X509_NAME *name;
410
Kenny Rootb8494592015-09-25 02:29:14 +0000411 if (x509 == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800412 return 0;
413 }
414 if (*sk == NULL) {
415 *sk = sk_X509_NAME_new_null();
416 if (*sk == NULL) {
417 return 0;
418 }
419 }
420
Kenny Rootb8494592015-09-25 02:29:14 +0000421 name = X509_NAME_dup(X509_get_subject_name(x509));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800422 if (name == NULL) {
423 return 0;
424 }
425
426 if (!sk_X509_NAME_push(*sk, name)) {
427 X509_NAME_free(name);
428 return 0;
429 }
430
431 return 1;
432}
433
Kenny Rootb8494592015-09-25 02:29:14 +0000434int SSL_add_client_CA(SSL *ssl, X509 *x509) {
435 return add_client_CA(&ssl->client_CA, x509);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800436}
437
Kenny Rootb8494592015-09-25 02:29:14 +0000438int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
439 return add_client_CA(&ctx->client_CA, x509);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800440}
441
David Benjaminc895d6b2016-08-11 13:26:41 -0400442int ssl_has_certificate(const SSL *ssl) {
443 return ssl->cert->x509 != NULL && ssl_has_private_key(ssl);
444}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800445
David Benjaminc895d6b2016-08-11 13:26:41 -0400446STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert,
447 uint8_t *out_leaf_sha256, CBS *cbs) {
448 STACK_OF(X509) *ret = sk_X509_new_null();
449 if (ret == NULL) {
450 *out_alert = SSL_AD_INTERNAL_ERROR;
451 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
452 return NULL;
453 }
454
455 X509 *x = NULL;
456 CBS certificate_list;
457 if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
458 *out_alert = SSL_AD_DECODE_ERROR;
459 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
460 goto err;
461 }
462
463 while (CBS_len(&certificate_list) > 0) {
464 CBS certificate;
465 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
466 *out_alert = SSL_AD_DECODE_ERROR;
467 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
468 goto err;
469 }
470
471 /* Retain the hash of the leaf certificate if requested. */
472 if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) {
473 SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
474 }
475
476 /* A u24 length cannot overflow a long. */
477 const uint8_t *data = CBS_data(&certificate);
478 x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
479 if (x == NULL || data != CBS_data(&certificate) + CBS_len(&certificate)) {
480 *out_alert = SSL_AD_DECODE_ERROR;
481 goto err;
482 }
483 if (!sk_X509_push(ret, x)) {
484 *out_alert = SSL_AD_INTERNAL_ERROR;
485 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
486 goto err;
487 }
488 x = NULL;
489 }
490
491 return ret;
492
493err:
494 X509_free(x);
495 sk_X509_pop_free(ret, X509_free);
496 return NULL;
497}
498
499int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
500 int len = i2d_X509(x509, NULL);
501 if (len < 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800502 return 0;
503 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400504 uint8_t *buf;
505 if (!CBB_add_space(cbb, &buf, len)) {
506 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
507 return 0;
David Benjamin4969cc92016-04-22 15:02:23 -0400508 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400509 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
510 return 0;
511 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800512 return 1;
513}
514
David Benjaminc895d6b2016-08-11 13:26:41 -0400515static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
516 CBB child;
517 return CBB_add_u24_length_prefixed(cbb, &child) &&
518 ssl_add_cert_to_cbb(&child, x509) &&
519 CBB_flush(cbb);
520}
521
522int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
523 if (!ssl_has_certificate(ssl)) {
524 return CBB_add_u24(cbb, 0);
525 }
526
Kenny Rootb8494592015-09-25 02:29:14 +0000527 CERT *cert = ssl->cert;
Kenny Rootb8494592015-09-25 02:29:14 +0000528 X509 *x = cert->x509;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800529
David Benjaminc895d6b2016-08-11 13:26:41 -0400530 CBB child;
531 if (!CBB_add_u24_length_prefixed(cbb, &child)) {
532 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleye9ada862015-05-11 17:20:37 -0700533 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800534 }
535
David Benjaminc895d6b2016-08-11 13:26:41 -0400536 int no_chain = 0;
537 STACK_OF(X509) *chain = cert->chain;
Kenny Rootb8494592015-09-25 02:29:14 +0000538 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800539 no_chain = 1;
540 }
541
Adam Langleye9ada862015-05-11 17:20:37 -0700542 if (no_chain) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400543 if (!ssl_add_cert_with_length(&child, x)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800544 return 0;
545 }
Adam Langleye9ada862015-05-11 17:20:37 -0700546
David Benjaminc895d6b2016-08-11 13:26:41 -0400547 size_t i;
Kenny Rootb8494592015-09-25 02:29:14 +0000548 for (i = 0; i < sk_X509_num(chain); i++) {
549 x = sk_X509_value(chain, i);
David Benjaminc895d6b2016-08-11 13:26:41 -0400550 if (!ssl_add_cert_with_length(&child, x)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700551 return 0;
552 }
553 }
554 } else {
555 X509_STORE_CTX xs_ctx;
556
Kenny Rootb8494592015-09-25 02:29:14 +0000557 if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
558 OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
Adam Langleye9ada862015-05-11 17:20:37 -0700559 return 0;
560 }
561 X509_verify_cert(&xs_ctx);
562 /* Don't leave errors in the queue */
563 ERR_clear_error();
David Benjaminc895d6b2016-08-11 13:26:41 -0400564
565 size_t i;
Adam Langleye9ada862015-05-11 17:20:37 -0700566 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
567 x = sk_X509_value(xs_ctx.chain, i);
David Benjaminc895d6b2016-08-11 13:26:41 -0400568 if (!ssl_add_cert_with_length(&child, x)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700569 X509_STORE_CTX_cleanup(&xs_ctx);
570 return 0;
571 }
572 }
573 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800574 }
575
David Benjaminc895d6b2016-08-11 13:26:41 -0400576 return CBB_flush(cbb);
577}
578
579static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
580 return X509_NAME_cmp(*a, *b);
581}
582
583STACK_OF(X509_NAME) *
584 ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
585 STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
586 X509_NAME *name = NULL;
587 if (ret == NULL) {
588 *out_alert = SSL_AD_INTERNAL_ERROR;
589 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
590 return NULL;
591 }
592
593 CBS child;
594 if (!CBS_get_u16_length_prefixed(cbs, &child)) {
595 *out_alert = SSL_AD_DECODE_ERROR;
596 OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
597 goto err;
598 }
599
600 while (CBS_len(&child) > 0) {
601 CBS distinguished_name;
602 if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
603 *out_alert = SSL_AD_DECODE_ERROR;
604 OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
605 goto err;
606 }
607
608 const uint8_t *ptr = CBS_data(&distinguished_name);
609 /* A u16 length cannot overflow a long. */
610 name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
611 if (name == NULL ||
612 ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
613 *out_alert = SSL_AD_DECODE_ERROR;
614 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
615 goto err;
616 }
617
618 if (!sk_X509_NAME_push(ret, name)) {
619 *out_alert = SSL_AD_INTERNAL_ERROR;
620 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
621 goto err;
622 }
623 name = NULL;
624 }
625
626 return ret;
627
628err:
629 X509_NAME_free(name);
630 sk_X509_NAME_pop_free(ret, X509_NAME_free);
631 return NULL;
632}
633
634int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
635 CBB child, name_cbb;
636 if (!CBB_add_u16_length_prefixed(cbb, &child)) {
637 return 0;
638 }
639
640 STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
641 if (sk == NULL) {
642 return CBB_flush(cbb);
643 }
644
645 for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
646 X509_NAME *name = sk_X509_NAME_value(sk, i);
647 int len = i2d_X509_NAME(name, NULL);
648 if (len < 0) {
649 return 0;
650 }
651 uint8_t *ptr;
652 if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
653 !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
654 (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
655 return 0;
656 }
657 }
658
659 return CBB_flush(cbb);
660}
661
662int ssl_do_client_cert_cb(SSL *ssl, int *out_should_retry) {
663 if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
664 return 1;
665 }
666
667 X509 *x509 = NULL;
668 EVP_PKEY *pkey = NULL;
669 int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
670 if (ret < 0) {
671 *out_should_retry = 1;
672 return 0;
673 }
674
675 if (ret != 0) {
676 if (!SSL_use_certificate(ssl, x509) ||
677 !SSL_use_PrivateKey(ssl, pkey)) {
678 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
679 *out_should_retry = 0;
680 return 0;
681 }
682 }
683
684 X509_free(x509);
685 EVP_PKEY_free(pkey);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800686 return 1;
687}
688
David Benjamin4969cc92016-04-22 15:02:23 -0400689static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
690 X509_STORE_free(*store_ptr);
691 *store_ptr = new_store;
692
693 if (new_store != NULL && take_ref) {
694 X509_STORE_up_ref(new_store);
695 }
696
697 return 1;
698}
699
700int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
701 return set_cert_store(&ctx->cert->verify_store, store, 0);
702}
703
704int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
705 return set_cert_store(&ctx->cert->verify_store, store, 1);
706}
707
708int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
709 return set_cert_store(&ssl->cert->verify_store, store, 0);
710}
711
712int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
713 return set_cert_store(&ssl->cert->verify_store, store, 1);
714}
715
Kenny Rootb8494592015-09-25 02:29:14 +0000716int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
717 return ssl_cert_set0_chain(ctx->cert, chain);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800718}
719
Kenny Rootb8494592015-09-25 02:29:14 +0000720int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
721 return ssl_cert_set1_chain(ctx->cert, chain);
722}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800723
Kenny Rootb8494592015-09-25 02:29:14 +0000724int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
725 return ssl_cert_set0_chain(ssl->cert, chain);
726}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800727
Kenny Rootb8494592015-09-25 02:29:14 +0000728int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
729 return ssl_cert_set1_chain(ssl->cert, chain);
730}
731
732int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
733 return ssl_cert_add0_chain_cert(ctx->cert, x509);
734}
735
736int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
737 return ssl_cert_add1_chain_cert(ctx->cert, x509);
738}
739
740int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
741 return SSL_CTX_add0_chain_cert(ctx, x509);
742}
743
744int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
745 return ssl_cert_add0_chain_cert(ssl->cert, x509);
746}
747
748int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
749 return ssl_cert_add1_chain_cert(ssl->cert, x509);
750}
751
752int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
753 return SSL_CTX_set0_chain(ctx, NULL);
754}
755
756int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
757 return SSL_CTX_clear_chain_certs(ctx);
758}
759
760int SSL_clear_chain_certs(SSL *ssl) {
761 return SSL_set0_chain(ssl, NULL);
762}
763
764int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
765 *out_chain = ctx->cert->chain;
766 return 1;
767}
768
769int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
770 STACK_OF(X509) **out_chain) {
771 return SSL_CTX_get0_chain_certs(ctx, out_chain);
772}
773
774int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
775 *out_chain = ssl->cert->chain;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800776 return 1;
777}
David Benjaminc895d6b2016-08-11 13:26:41 -0400778
779int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf) {
780 int ret = 0;
781 EVP_PKEY *pkey = X509_get_pubkey(leaf);
782 if (pkey == NULL) {
783 goto err;
784 }
785
786 /* Check the certificate's type matches the cipher. */
787 const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
788 int expected_type = ssl_cipher_get_key_type(cipher);
789 assert(expected_type != EVP_PKEY_NONE);
790 if (pkey->type != expected_type) {
791 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
792 goto err;
793 }
794
795 if (cipher->algorithm_auth & SSL_aECDSA) {
796 /* TODO(davidben): This behavior is preserved from upstream. Should key
797 * usages be checked in other cases as well? */
798 /* This call populates the ex_flags field correctly */
799 X509_check_purpose(leaf, -1, 0);
800 if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
801 !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
802 OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
803 goto err;
804 }
805
806 if (!tls1_check_ec_cert(ssl, leaf)) {
807 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
808 goto err;
809 }
810 }
811
812 ret = 1;
813
814err:
815 EVP_PKEY_free(pkey);
816 return ret;
817}