blob: a389cc42e550af51b2d345680a643ffed174ec09 [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 Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <errno.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400117#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118
119#include <openssl/bio.h>
120#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>
126#include <openssl/obj.h>
127#include <openssl/pem.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
131#include "../crypto/dh/internal.h"
132#include "../crypto/directory.h"
Adam Langley0da323a2015-05-15 12:49:30 -0700133#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400134#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langley95c29f32014-06-20 12:00:00 -0700136
Adam Langley4bdb6e42015-05-15 15:29:21 -0700137static CRYPTO_once_t g_x509_store_ex_data_index_once;
138static int g_x509_store_ex_data_index;
139
140static void ssl_x509_store_ex_data_index_init(void) {
141 g_x509_store_ex_data_index = X509_STORE_CTX_get_ex_new_index(
142 0, "SSL for verify callback", NULL, NULL, NULL);
143}
144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
Adam Langley4bdb6e42015-05-15 15:29:21 -0700146 CRYPTO_once(&g_x509_store_ex_data_index_once,
147 ssl_x509_store_ex_data_index_init);
148 return g_x509_store_ex_data_index;
Adam Langleyfcf25832014-12-18 17:42:32 -0800149}
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langleyfcf25832014-12-18 17:42:32 -0800151CERT *ssl_cert_new(void) {
David Benjamind1d80782015-07-05 11:54:09 -0400152 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 if (ret == NULL) {
154 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
155 return NULL;
156 }
157 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langleyfcf25832014-12-18 17:42:32 -0800159 return ret;
160}
Adam Langley95c29f32014-06-20 12:00:00 -0700161
Adam Langleyfcf25832014-12-18 17:42:32 -0800162CERT *ssl_cert_dup(CERT *cert) {
David Benjamind1d80782015-07-05 11:54:09 -0400163 CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
Adam Langleyfcf25832014-12-18 17:42:32 -0800164 if (ret == NULL) {
165 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
166 return NULL;
167 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 ret->mask_k = cert->mask_k;
171 ret->mask_a = cert->mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700172
Adam Langleyfcf25832014-12-18 17:42:32 -0800173 if (cert->dh_tmp != NULL) {
174 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
175 if (ret->dh_tmp == NULL) {
176 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
177 goto err;
178 }
179 if (cert->dh_tmp->priv_key) {
180 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
181 if (!b) {
182 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
183 goto err;
184 }
185 ret->dh_tmp->priv_key = b;
186 }
187 if (cert->dh_tmp->pub_key) {
188 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
189 if (!b) {
190 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
191 goto err;
192 }
193 ret->dh_tmp->pub_key = b;
194 }
195 }
196 ret->dh_tmp_cb = cert->dh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjaminc0f763b2015-03-27 02:05:39 -0400198 ret->ecdh_nid = cert->ecdh_nid;
Adam Langleyfcf25832014-12-18 17:42:32 -0800199 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjamind1d80782015-07-05 11:54:09 -0400201 if (cert->x509 != NULL) {
202 ret->x509 = X509_up_ref(cert->x509);
203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
David Benjamind1d80782015-07-05 11:54:09 -0400205 if (cert->privatekey != NULL) {
206 ret->privatekey = EVP_PKEY_up_ref(cert->privatekey);
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamind1d80782015-07-05 11:54:09 -0400209 if (cert->chain) {
210 ret->chain = X509_chain_up_ref(cert->chain);
211 if (!ret->chain) {
212 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
213 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 }
215 }
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjaminb85a4c22015-03-19 14:47:27 -0400217 /* Copy over signature algorithm configuration. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 if (cert->conf_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400219 ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800220 if (!ret->conf_sigalgs) {
221 goto err;
222 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 ret->conf_sigalgslen = cert->conf_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (cert->client_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400227 ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
228 cert->client_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 if (!ret->client_sigalgs) {
230 goto err;
231 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 ret->client_sigalgslen = cert->client_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 }
David Benjaminb85a4c22015-03-19 14:47:27 -0400234
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 /* Copy any custom client certificate types */
236 if (cert->client_certificate_types) {
237 ret->client_certificate_types = BUF_memdup(
238 cert->client_certificate_types, cert->num_client_certificate_types);
239 if (!ret->client_certificate_types) {
240 goto err;
241 }
242 ret->num_client_certificate_types = cert->num_client_certificate_types;
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ret->cert_cb = cert->cert_cb;
246 ret->cert_cb_arg = cert->cert_cb_arg;
247
248 if (cert->verify_store) {
Adam Langley0da323a2015-05-15 12:49:30 -0700249 CRYPTO_refcount_inc(&cert->verify_store->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 ret->verify_store = cert->verify_store;
251 }
252
253 if (cert->chain_store) {
Adam Langley0da323a2015-05-15 12:49:30 -0700254 CRYPTO_refcount_inc(&cert->chain_store->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 ret->chain_store = cert->chain_store;
256 }
257
Adam Langleyfcf25832014-12-18 17:42:32 -0800258 return ret;
259
Adam Langley95c29f32014-06-20 12:00:00 -0700260err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 ssl_cert_free(ret);
262 return NULL;
263}
Adam Langley95c29f32014-06-20 12:00:00 -0700264
265/* Free up and clear all certificates and chains */
David Benjamind1d80782015-07-05 11:54:09 -0400266void ssl_cert_clear_certs(CERT *cert) {
267 if (cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 return;
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270
David Benjamind1d80782015-07-05 11:54:09 -0400271 X509_free(cert->x509);
272 cert->x509 = NULL;
273 EVP_PKEY_free(cert->privatekey);
274 cert->privatekey = NULL;
275 sk_X509_pop_free(cert->chain, X509_free);
276 cert->chain = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800277}
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langleyfcf25832014-12-18 17:42:32 -0800279void ssl_cert_free(CERT *c) {
280 if (c == NULL) {
281 return;
282 }
Adam Langley95c29f32014-06-20 12:00:00 -0700283
David Benjamin2755a3e2015-04-22 16:17:58 -0400284 DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langleyfcf25832014-12-18 17:42:32 -0800286 ssl_cert_clear_certs(c);
David Benjamin2755a3e2015-04-22 16:17:58 -0400287 OPENSSL_free(c->peer_sigalgs);
288 OPENSSL_free(c->conf_sigalgs);
289 OPENSSL_free(c->client_sigalgs);
290 OPENSSL_free(c->shared_sigalgs);
291 OPENSSL_free(c->client_certificate_types);
292 X509_STORE_free(c->verify_store);
293 X509_STORE_free(c->chain_store);
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 OPENSSL_free(c);
296}
Adam Langley95c29f32014-06-20 12:00:00 -0700297
David Benjamind1d80782015-07-05 11:54:09 -0400298int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
299 sk_X509_pop_free(cert->chain, X509_free);
300 cert->chain = chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800301 return 1;
302}
Adam Langley95c29f32014-06-20 12:00:00 -0700303
David Benjamind1d80782015-07-05 11:54:09 -0400304int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
David Benjamin60da0cd2015-05-03 15:21:28 -0400305 STACK_OF(X509) *dchain;
David Benjamind1d80782015-07-05 11:54:09 -0400306 if (chain == NULL) {
307 return ssl_cert_set0_chain(cert, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 dchain = X509_chain_up_ref(chain);
David Benjamind1d80782015-07-05 11:54:09 -0400311 if (dchain == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800312 return 0;
313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjamind1d80782015-07-05 11:54:09 -0400315 if (!ssl_cert_set0_chain(cert, dchain)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 sk_X509_pop_free(dchain, X509_free);
317 return 0;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 return 1;
321}
Adam Langley95c29f32014-06-20 12:00:00 -0700322
David Benjamind1d80782015-07-05 11:54:09 -0400323int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
324 if (cert->chain == NULL) {
325 cert->chain = sk_X509_new_null();
Adam Langleyfcf25832014-12-18 17:42:32 -0800326 }
David Benjamind1d80782015-07-05 11:54:09 -0400327 if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 return 0;
329 }
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 return 1;
332}
Adam Langley95c29f32014-06-20 12:00:00 -0700333
David Benjamind1d80782015-07-05 11:54:09 -0400334int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
335 if (!ssl_cert_add0_chain_cert(cert, x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 return 0;
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamind1d80782015-07-05 11:54:09 -0400339 X509_up_ref(x509);
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 return 1;
341}
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langleyfcf25832014-12-18 17:42:32 -0800343void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
344 c->cert_cb = cb;
345 c->cert_cb_arg = arg;
346}
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langleyfcf25832014-12-18 17:42:32 -0800348SESS_CERT *ssl_sess_cert_new(void) {
349 SESS_CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 ret = OPENSSL_malloc(sizeof *ret);
352 if (ret == NULL) {
353 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
354 return NULL;
355 }
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 memset(ret, 0, sizeof *ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700358
Adam Langleyfcf25832014-12-18 17:42:32 -0800359 return ret;
360}
Adam Langley95c29f32014-06-20 12:00:00 -0700361
David Benjamin680ca962015-06-18 12:37:23 -0400362SESS_CERT *ssl_sess_cert_dup(const SESS_CERT *sess_cert) {
363 SESS_CERT *ret = ssl_sess_cert_new();
364 if (ret == NULL) {
365 return NULL;
366 }
367
368 if (sess_cert->cert_chain != NULL) {
369 ret->cert_chain = X509_chain_up_ref(sess_cert->cert_chain);
370 if (ret->cert_chain == NULL) {
371 ssl_sess_cert_free(ret);
372 return NULL;
373 }
374 }
375 if (sess_cert->peer_cert != NULL) {
376 ret->peer_cert = X509_up_ref(sess_cert->peer_cert);
377 }
378 if (sess_cert->peer_dh_tmp != NULL) {
379 ret->peer_dh_tmp = sess_cert->peer_dh_tmp;
380 DH_up_ref(ret->peer_dh_tmp);
381 }
382 if (sess_cert->peer_ecdh_tmp != NULL) {
383 ret->peer_ecdh_tmp = sess_cert->peer_ecdh_tmp;
384 EC_KEY_up_ref(ret->peer_ecdh_tmp);
385 }
386 return ret;
387}
388
389void ssl_sess_cert_free(SESS_CERT *sess_cert) {
390 if (sess_cert == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
David Benjamin680ca962015-06-18 12:37:23 -0400394 sk_X509_pop_free(sess_cert->cert_chain, X509_free);
395 X509_free(sess_cert->peer_cert);
396 DH_free(sess_cert->peer_dh_tmp);
397 EC_KEY_free(sess_cert->peer_ecdh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700398
David Benjamin680ca962015-06-18 12:37:23 -0400399 OPENSSL_free(sess_cert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800400}
Adam Langley95c29f32014-06-20 12:00:00 -0700401
David Benjamin60da0cd2015-05-03 15:21:28 -0400402int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 X509 *x;
404 int i;
405 X509_STORE *verify_store;
406 X509_STORE_CTX ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langleyfcf25832014-12-18 17:42:32 -0800408 if (s->cert->verify_store) {
409 verify_store = s->cert->verify_store;
410 } else {
411 verify_store = s->ctx->cert_store;
412 }
Adam Langley95c29f32014-06-20 12:00:00 -0700413
Adam Langleyfcf25832014-12-18 17:42:32 -0800414 if (sk == NULL || sk_X509_num(sk) == 0) {
415 return 0;
416 }
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 x = sk_X509_value(sk, 0);
419 if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
420 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
421 return 0;
422 }
423 X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 /* We need to inherit the verify parameters. These can be determined by the
426 * context: if its a server it will verify SSL client certificates or vice
427 * versa. */
428 X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 /* Anything non-default in "param" should overwrite anything in the ctx. */
431 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 if (s->verify_callback) {
434 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
435 }
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 if (s->ctx->app_verify_callback != NULL) {
438 i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
439 } else {
440 i = X509_verify_cert(&ctx);
441 }
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langleyfcf25832014-12-18 17:42:32 -0800443 s->verify_result = ctx.error;
444 X509_STORE_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 return i;
447}
Adam Langley95c29f32014-06-20 12:00:00 -0700448
David Benjamin60da0cd2015-05-03 15:21:28 -0400449static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
450 STACK_OF(X509_NAME) *name_list) {
David Benjamin2755a3e2015-04-22 16:17:58 -0400451 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 *ca_list = name_list;
453}
Adam Langley95c29f32014-06-20 12:00:00 -0700454
David Benjamin60da0cd2015-05-03 15:21:28 -0400455STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 size_t i;
David Benjamin60da0cd2015-05-03 15:21:28 -0400457 STACK_OF(X509_NAME) *ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 X509_NAME *name;
459
460 ret = sk_X509_NAME_new_null();
461 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
462 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
463 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
464 sk_X509_NAME_pop_free(ret, X509_NAME_free);
465 return NULL;
466 }
467 }
468
469 return ret;
470}
471
David Benjamin60da0cd2015-05-03 15:21:28 -0400472void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 set_client_CA_list(&(s->client_CA), name_list);
474}
475
David Benjamin60da0cd2015-05-03 15:21:28 -0400476void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 set_client_CA_list(&(ctx->client_CA), name_list);
478}
479
David Benjamin60da0cd2015-05-03 15:21:28 -0400480STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 return ctx->client_CA;
482}
483
David Benjamin60da0cd2015-05-03 15:21:28 -0400484STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800485 if (s->server) {
486 if (s->client_CA != NULL) {
487 return s->client_CA;
488 } else {
489 return s->ctx->client_CA;
490 }
491 } else {
492 if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
493 return s->s3->tmp.ca_names;
494 } else {
495 return NULL;
496 }
497 }
498}
499
David Benjamin60da0cd2015-05-03 15:21:28 -0400500static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800501 X509_NAME *name;
502
503 if (x == NULL) {
504 return 0;
505 }
506 if (*sk == NULL) {
507 *sk = sk_X509_NAME_new_null();
508 if (*sk == NULL) {
509 return 0;
510 }
511 }
512
513 name = X509_NAME_dup(X509_get_subject_name(x));
514 if (name == NULL) {
515 return 0;
516 }
517
518 if (!sk_X509_NAME_push(*sk, name)) {
519 X509_NAME_free(name);
520 return 0;
521 }
522
523 return 1;
524}
525
526int SSL_add_client_CA(SSL *ssl, X509 *x) {
527 return add_client_CA(&(ssl->client_CA), x);
528}
529
530int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
531 return add_client_CA(&(ctx->client_CA), x);
532}
533
534static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
535 return X509_NAME_cmp(*a, *b);
536}
537
538/* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
Adam Langley95c29f32014-06-20 12:00:00 -0700539 * it doesn't really have anything to do with clients (except that a common use
540 * for a stack of CAs is to send it to the client). Actually, it doesn't have
541 * much to do with CAs, either, since it will load any old cert.
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 *
Adam Langley95c29f32014-06-20 12:00:00 -0700543 * \param file the file containing one or more certs.
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 * \return a ::STACK containing the certs. */
David Benjamin60da0cd2015-05-03 15:21:28 -0400545STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 BIO *in;
547 X509 *x = NULL;
548 X509_NAME *xn = NULL;
549 STACK_OF(X509_NAME) *ret = NULL, *sk;
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 sk = sk_X509_NAME_new(xname_cmp);
552 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 if (sk == NULL || in == NULL) {
555 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
556 goto err;
557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 if (!BIO_read_filename(in, file)) {
560 goto err;
561 }
Adam Langley95c29f32014-06-20 12:00:00 -0700562
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 for (;;) {
564 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
565 break;
566 }
567 if (ret == NULL) {
568 ret = sk_X509_NAME_new_null();
569 if (ret == NULL) {
570 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
571 goto err;
572 }
573 }
574 xn = X509_get_subject_name(x);
575 if (xn == NULL) {
576 goto err;
577 }
Adam Langley95c29f32014-06-20 12:00:00 -0700578
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 /* check for duplicates */
580 xn = X509_NAME_dup(xn);
581 if (xn == NULL) {
582 goto err;
583 }
584 if (sk_X509_NAME_find(sk, NULL, xn)) {
585 X509_NAME_free(xn);
586 } else {
587 sk_X509_NAME_push(sk, xn);
588 sk_X509_NAME_push(ret, xn);
589 }
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 if (0) {
593 err:
David Benjamin2755a3e2015-04-22 16:17:58 -0400594 sk_X509_NAME_pop_free(ret, X509_NAME_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800595 ret = NULL;
596 }
597
David Benjamin2755a3e2015-04-22 16:17:58 -0400598 sk_X509_NAME_free(sk);
599 BIO_free(in);
600 X509_free(x);
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 if (ret != NULL) {
602 ERR_clear_error();
603 }
604 return ret;
605}
606
607/* Add a file of certs to a stack.
608 *
Adam Langley95c29f32014-06-20 12:00:00 -0700609 * \param stack the stack to add to.
610 * \param file the file to add from. All certs in this file that are not
Adam Langleyfcf25832014-12-18 17:42:32 -0800611 * already in the stack will be added.
Adam Langley95c29f32014-06-20 12:00:00 -0700612 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800613 * certs may have been added to \c stack. */
David Benjamin60da0cd2015-05-03 15:21:28 -0400614int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 const char *file) {
616 BIO *in;
617 X509 *x = NULL;
618 X509_NAME *xn = NULL;
619 int ret = 1;
620 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
Adam Langley95c29f32014-06-20 12:00:00 -0700621
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
623 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 if (in == NULL) {
626 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack,
627 ERR_R_MALLOC_FAILURE);
628 goto err;
629 }
Adam Langley95c29f32014-06-20 12:00:00 -0700630
Adam Langleyfcf25832014-12-18 17:42:32 -0800631 if (!BIO_read_filename(in, file)) {
632 goto err;
633 }
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 for (;;) {
636 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
637 break;
638 }
639 xn = X509_get_subject_name(x);
640 if (xn == NULL) {
641 goto err;
642 }
643 xn = X509_NAME_dup(xn);
644 if (xn == NULL) {
645 goto err;
646 }
647 if (sk_X509_NAME_find(stack, NULL, xn)) {
648 X509_NAME_free(xn);
649 } else {
650 sk_X509_NAME_push(stack, xn);
651 }
652 }
Adam Langley95c29f32014-06-20 12:00:00 -0700653
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 ERR_clear_error();
655
656 if (0) {
657 err:
658 ret = 0;
659 }
660
David Benjamin2755a3e2015-04-22 16:17:58 -0400661 BIO_free(in);
662 X509_free(x);
Adam Langleyfcf25832014-12-18 17:42:32 -0800663
Adam Langleya307dfd2015-01-09 15:42:58 -0800664 (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800665
666 return ret;
667}
668
669/* Add a directory of certs to a stack.
670 *
Adam Langley95c29f32014-06-20 12:00:00 -0700671 * \param stack the stack to append to.
672 * \param dir the directory to append from. All files in this directory will be
Adam Langleyfcf25832014-12-18 17:42:32 -0800673 * examined as potential certs. Any that are acceptable to
674 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
675 * be included.
Adam Langley95c29f32014-06-20 12:00:00 -0700676 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 * certs may have been added to \c stack. */
David Benjamin60da0cd2015-05-03 15:21:28 -0400678int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
Adam Langleyfcf25832014-12-18 17:42:32 -0800679 const char *dir) {
680 OPENSSL_DIR_CTX *d = NULL;
681 const char *filename;
682 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 /* Note that a side effect is that the CAs will be sorted by name */
685 while ((filename = OPENSSL_DIR_read(&d, dir))) {
686 char buf[1024];
687 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700688
Adam Langleyfcf25832014-12-18 17:42:32 -0800689 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
690 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack,
691 SSL_R_PATH_TOO_LONG);
692 goto err;
693 }
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
696 if (r <= 0 || r >= (int)sizeof(buf) ||
697 !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
698 goto err;
699 }
700 }
Adam Langley95c29f32014-06-20 12:00:00 -0700701
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 if (errno) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100703 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, ERR_R_SYS_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
705 goto err;
706 }
Adam Langley95c29f32014-06-20 12:00:00 -0700707
Adam Langleyfcf25832014-12-18 17:42:32 -0800708 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700709
710err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800711 if (d) {
712 OPENSSL_DIR_end(&d);
713 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800714 return ret;
715}
Adam Langley95c29f32014-06-20 12:00:00 -0700716
717/* Add a certificate to a BUF_MEM structure */
Adam Langleyfcf25832014-12-18 17:42:32 -0800718static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
719 int n;
720 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 n = i2d_X509(x, NULL);
723 if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
724 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
725 return 0;
726 }
727 p = (uint8_t *)&(buf->data[*l]);
728 l2n3(n, p);
729 i2d_X509(x, &p);
730 *l += n + 3;
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 return 1;
733}
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langley24819752014-12-15 18:42:07 -0800735/* Add certificate chain to internal SSL BUF_MEM structure. */
David Benjamind1d80782015-07-05 11:54:09 -0400736int ssl_add_cert_chain(SSL *ssl, unsigned long *l) {
737 CERT *cert = ssl->cert;
738 BUF_MEM *buf = ssl->init_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 int no_chain = 0;
740 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700741
David Benjamind1d80782015-07-05 11:54:09 -0400742 X509 *x = cert->x509;
743 STACK_OF(X509) *chain = cert->chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 X509_STORE *chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -0700745
David Benjamin605641e2015-05-03 15:14:04 -0400746 if (x == NULL) {
747 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, SSL_R_NO_CERTIFICATE_SET);
748 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 }
Adam Langley95c29f32014-06-20 12:00:00 -0700750
David Benjamind1d80782015-07-05 11:54:09 -0400751 if (ssl->cert->chain_store) {
752 chain_store = ssl->cert->chain_store;
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 } else {
David Benjamind1d80782015-07-05 11:54:09 -0400754 chain_store = ssl->ctx->cert_store;
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 }
Adam Langley95c29f32014-06-20 12:00:00 -0700756
David Benjamind1d80782015-07-05 11:54:09 -0400757 if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800758 no_chain = 1;
759 }
Adam Langley95c29f32014-06-20 12:00:00 -0700760
David Benjamin605641e2015-05-03 15:14:04 -0400761 if (no_chain) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 if (!ssl_add_cert_to_buf(buf, l, x)) {
763 return 0;
764 }
David Benjamin605641e2015-05-03 15:14:04 -0400765
David Benjamind1d80782015-07-05 11:54:09 -0400766 for (i = 0; i < sk_X509_num(chain); i++) {
767 x = sk_X509_value(chain, i);
David Benjamin605641e2015-05-03 15:14:04 -0400768 if (!ssl_add_cert_to_buf(buf, l, x)) {
769 return 0;
770 }
771 }
772 } else {
773 X509_STORE_CTX xs_ctx;
774
775 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
776 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
777 return 0;
778 }
779 X509_verify_cert(&xs_ctx);
780 /* Don't leave errors in the queue */
781 ERR_clear_error();
782 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
783 x = sk_X509_value(xs_ctx.chain, i);
784
785 if (!ssl_add_cert_to_buf(buf, l, x)) {
786 X509_STORE_CTX_cleanup(&xs_ctx);
787 return 0;
788 }
789 }
790 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800791 }
792
793 return 1;
794}
Adam Langley95c29f32014-06-20 12:00:00 -0700795
796/* Build a certificate chain for current certificate */
David Benjamind1d80782015-07-05 11:54:09 -0400797int ssl_build_cert_chain(CERT *cert, X509_STORE *chain_store, int flags) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 X509_STORE_CTX xs_ctx;
799 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
800 X509 *x;
801 int i, rv = 0;
David Benjamin9362b6e2015-04-26 14:14:15 -0400802 uint32_t error;
Adam Langley95c29f32014-06-20 12:00:00 -0700803
David Benjamind1d80782015-07-05 11:54:09 -0400804 if (cert->x509 == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
806 goto err;
807 }
Adam Langley95c29f32014-06-20 12:00:00 -0700808
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 /* Rearranging and check the chain: add everything to a store */
810 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
811 size_t j;
812 chain_store = X509_STORE_new();
813 if (!chain_store) {
814 goto err;
815 }
Adam Langley95c29f32014-06-20 12:00:00 -0700816
David Benjamind1d80782015-07-05 11:54:09 -0400817 for (j = 0; j < sk_X509_num(cert->chain); j++) {
818 x = sk_X509_value(cert->chain, j);
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 if (!X509_STORE_add_cert(chain_store, x)) {
820 error = ERR_peek_last_error();
821 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
822 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
823 goto err;
824 }
825 ERR_clear_error();
826 }
827 }
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 /* Add EE cert too: it might be self signed */
David Benjamind1d80782015-07-05 11:54:09 -0400830 if (!X509_STORE_add_cert(chain_store, cert->x509)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 error = ERR_peek_last_error();
832 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
833 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
834 goto err;
835 }
836 ERR_clear_error();
837 }
838 } else {
David Benjamind1d80782015-07-05 11:54:09 -0400839 if (cert->chain_store) {
840 chain_store = cert->chain_store;
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 }
Adam Langleyf669c2d2014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) {
David Benjamind1d80782015-07-05 11:54:09 -0400844 untrusted = cert->chain;
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 }
846 }
Adam Langley95c29f32014-06-20 12:00:00 -0700847
David Benjamind1d80782015-07-05 11:54:09 -0400848 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cert->x509, untrusted)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800849 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
850 goto err;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 i = X509_verify_cert(&xs_ctx);
854 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
855 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) {
856 ERR_clear_error();
857 }
858 i = 1;
859 rv = 2;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 if (i > 0) {
863 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
864 }
865 if (i <= 0) {
866 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain,
867 SSL_R_CERTIFICATE_VERIFY_FAILED);
868 i = X509_STORE_CTX_get_error(&xs_ctx);
869 ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i));
870
871 X509_STORE_CTX_cleanup(&xs_ctx);
872 goto err;
873 }
874
875 X509_STORE_CTX_cleanup(&xs_ctx);
David Benjamind1d80782015-07-05 11:54:09 -0400876 if (cert->chain) {
877 sk_X509_pop_free(cert->chain, X509_free);
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 }
879
880 /* Remove EE certificate from chain */
881 x = sk_X509_shift(chain);
882 X509_free(x);
883 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
884 if (sk_X509_num(chain) > 0) {
885 /* See if last cert is self signed */
886 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
887 X509_check_purpose(x, -1, 0);
888 if (x->ex_flags & EXFLAG_SS) {
889 x = sk_X509_pop(chain);
890 X509_free(x);
891 }
892 }
893 }
894
David Benjamind1d80782015-07-05 11:54:09 -0400895 cert->chain = chain;
David Benjamin6eb000d2015-02-11 01:17:41 -0500896 if (rv == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 rv = 1;
David Benjamin6eb000d2015-02-11 01:17:41 -0500898 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800899
900err:
901 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
902 X509_STORE_free(chain_store);
903 }
904
905 return rv;
906}
907
908int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
909 X509_STORE **pstore;
910 if (chain) {
911 pstore = &c->chain_store;
912 } else {
913 pstore = &c->verify_store;
914 }
915
David Benjamin2755a3e2015-04-22 16:17:58 -0400916 X509_STORE_free(*pstore);
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 *pstore = store;
918
919 if (ref && store) {
Adam Langley0da323a2015-05-15 12:49:30 -0700920 CRYPTO_refcount_inc(&store->references);
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 }
922 return 1;
923}