blob: 14c5e319548c50bb878f5c57ae7bb41a4fa85f2a [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>
Adam Langley95c29f32014-06-20 12:00:00 -0700122#include <openssl/dh.h>
123#include <openssl/err.h>
124#include <openssl/mem.h>
125#include <openssl/obj.h>
126#include <openssl/pem.h>
127#include <openssl/x509v3.h>
128
129#include "../crypto/dh/internal.h"
130#include "../crypto/directory.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400131#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700132
Adam Langley95c29f32014-06-20 12:00:00 -0700133
Adam Langleyfcf25832014-12-18 17:42:32 -0800134int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
135 static int ssl_x509_store_ctx_idx = -1;
136 int got_write_lock = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700137
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
Adam Langley95c29f32014-06-20 12:00:00 -0700139
Adam Langleyfcf25832014-12-18 17:42:32 -0800140 if (ssl_x509_store_ctx_idx < 0) {
141 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
142 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
143 got_write_lock = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langleyfcf25832014-12-18 17:42:32 -0800145 if (ssl_x509_store_ctx_idx < 0) {
146 ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(
147 0, "SSL for verify callback", NULL, NULL, NULL);
148 }
149 }
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 if (got_write_lock) {
152 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
153 } else {
154 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
155 }
Adam Langley95c29f32014-06-20 12:00:00 -0700156
Adam Langleyfcf25832014-12-18 17:42:32 -0800157 return ssl_x509_store_ctx_idx;
158}
Adam Langley95c29f32014-06-20 12:00:00 -0700159
Adam Langleyfcf25832014-12-18 17:42:32 -0800160CERT *ssl_cert_new(void) {
161 CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700162
Adam Langleyfcf25832014-12-18 17:42:32 -0800163 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
164 if (ret == NULL) {
165 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
166 return NULL;
167 }
168 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700169
Adam Langleyfcf25832014-12-18 17:42:32 -0800170 ret->key = &ret->pkeys[SSL_PKEY_RSA_ENC];
171 return ret;
172}
Adam Langley95c29f32014-06-20 12:00:00 -0700173
Adam Langleyfcf25832014-12-18 17:42:32 -0800174CERT *ssl_cert_dup(CERT *cert) {
175 CERT *ret;
176 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langleyfcf25832014-12-18 17:42:32 -0800178 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
179 if (ret == NULL) {
180 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
181 return NULL;
182 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
186 /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
187 * more readable */
Adam Langley95c29f32014-06-20 12:00:00 -0700188
Adam Langleyfcf25832014-12-18 17:42:32 -0800189 ret->mask_k = cert->mask_k;
190 ret->mask_a = cert->mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langleyfcf25832014-12-18 17:42:32 -0800192 if (cert->dh_tmp != NULL) {
193 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
194 if (ret->dh_tmp == NULL) {
195 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
196 goto err;
197 }
198 if (cert->dh_tmp->priv_key) {
199 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
200 if (!b) {
201 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
202 goto err;
203 }
204 ret->dh_tmp->priv_key = b;
205 }
206 if (cert->dh_tmp->pub_key) {
207 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
208 if (!b) {
209 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
210 goto err;
211 }
212 ret->dh_tmp->pub_key = b;
213 }
214 }
215 ret->dh_tmp_cb = cert->dh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjaminc0f763b2015-03-27 02:05:39 -0400217 ret->ecdh_nid = cert->ecdh_nid;
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
219 ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 for (i = 0; i < SSL_PKEY_NUM; i++) {
222 CERT_PKEY *cpk = cert->pkeys + i;
223 CERT_PKEY *rpk = ret->pkeys + i;
224 if (cpk->x509 != NULL) {
225 rpk->x509 = X509_up_ref(cpk->x509);
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
Adam Langleyfcf25832014-12-18 17:42:32 -0800228 if (cpk->privatekey != NULL) {
229 rpk->privatekey = EVP_PKEY_dup(cpk->privatekey);
230 }
Adam Langley95c29f32014-06-20 12:00:00 -0700231
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 if (cpk->chain) {
233 rpk->chain = X509_chain_up_ref(cpk->chain);
234 if (!rpk->chain) {
235 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
236 goto err;
237 }
238 }
239 }
Adam Langley95c29f32014-06-20 12:00:00 -0700240
David Benjaminb85a4c22015-03-19 14:47:27 -0400241 /* Copy over signature algorithm configuration. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (cert->conf_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400243 ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800244 if (!ret->conf_sigalgs) {
245 goto err;
246 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 ret->conf_sigalgslen = cert->conf_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 if (cert->client_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400251 ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
252 cert->client_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800253 if (!ret->client_sigalgs) {
254 goto err;
255 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800256 ret->client_sigalgslen = cert->client_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 }
David Benjaminb85a4c22015-03-19 14:47:27 -0400258
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 /* Copy any custom client certificate types */
260 if (cert->client_certificate_types) {
261 ret->client_certificate_types = BUF_memdup(
262 cert->client_certificate_types, cert->num_client_certificate_types);
263 if (!ret->client_certificate_types) {
264 goto err;
265 }
266 ret->num_client_certificate_types = cert->num_client_certificate_types;
267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 ret->cert_flags = cert->cert_flags;
270
271 ret->cert_cb = cert->cert_cb;
272 ret->cert_cb_arg = cert->cert_cb_arg;
273
274 if (cert->verify_store) {
275 CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
276 ret->verify_store = cert->verify_store;
277 }
278
279 if (cert->chain_store) {
280 CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
281 ret->chain_store = cert->chain_store;
282 }
283
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 return ret;
285
Adam Langley95c29f32014-06-20 12:00:00 -0700286err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 ssl_cert_free(ret);
288 return NULL;
289}
Adam Langley95c29f32014-06-20 12:00:00 -0700290
291/* Free up and clear all certificates and chains */
Adam Langleyfcf25832014-12-18 17:42:32 -0800292void ssl_cert_clear_certs(CERT *c) {
293 int i;
294 if (c == NULL) {
295 return;
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 for (i = 0; i < SSL_PKEY_NUM; i++) {
299 CERT_PKEY *cpk = c->pkeys + i;
300 if (cpk->x509) {
301 X509_free(cpk->x509);
302 cpk->x509 = NULL;
303 }
304 if (cpk->privatekey) {
305 EVP_PKEY_free(cpk->privatekey);
306 cpk->privatekey = NULL;
307 }
308 if (cpk->chain) {
309 sk_X509_pop_free(cpk->chain, X509_free);
310 cpk->chain = NULL;
311 }
312 }
313}
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315void ssl_cert_free(CERT *c) {
316 if (c == NULL) {
317 return;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 if (c->dh_tmp) {
321 DH_free(c->dh_tmp);
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 ssl_cert_clear_certs(c);
325 if (c->peer_sigalgs) {
326 OPENSSL_free(c->peer_sigalgs);
327 }
328 if (c->conf_sigalgs) {
329 OPENSSL_free(c->conf_sigalgs);
330 }
331 if (c->client_sigalgs) {
332 OPENSSL_free(c->client_sigalgs);
333 }
334 if (c->shared_sigalgs) {
335 OPENSSL_free(c->shared_sigalgs);
336 }
337 if (c->client_certificate_types) {
338 OPENSSL_free(c->client_certificate_types);
339 }
340 if (c->verify_store) {
341 X509_STORE_free(c->verify_store);
342 }
343 if (c->chain_store) {
344 X509_STORE_free(c->chain_store);
345 }
346 if (c->ciphers_raw) {
347 OPENSSL_free(c->ciphers_raw);
348 }
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langleyfcf25832014-12-18 17:42:32 -0800350 OPENSSL_free(c);
351}
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain) {
354 CERT_PKEY *cpk = c->key;
355 if (!cpk) {
356 return 0;
357 }
358 if (cpk->chain) {
359 sk_X509_pop_free(cpk->chain, X509_free);
360 }
361 cpk->chain = chain;
362 return 1;
363}
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain) {
366 STACK_OF(X509) * dchain;
367 if (!chain) {
368 return ssl_cert_set0_chain(c, NULL);
369 }
Adam Langley95c29f32014-06-20 12:00:00 -0700370
Adam Langleyfcf25832014-12-18 17:42:32 -0800371 dchain = X509_chain_up_ref(chain);
372 if (!dchain) {
373 return 0;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 if (!ssl_cert_set0_chain(c, dchain)) {
377 sk_X509_pop_free(dchain, X509_free);
378 return 0;
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return 1;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384int ssl_cert_add0_chain_cert(CERT *c, X509 *x) {
385 CERT_PKEY *cpk = c->key;
386 if (!cpk) {
387 return 0;
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langleyfcf25832014-12-18 17:42:32 -0800390 if (!cpk->chain) {
391 cpk->chain = sk_X509_new_null();
392 }
393 if (!cpk->chain || !sk_X509_push(cpk->chain, x)) {
394 return 0;
395 }
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 return 1;
398}
Adam Langley95c29f32014-06-20 12:00:00 -0700399
Adam Langleyfcf25832014-12-18 17:42:32 -0800400int ssl_cert_add1_chain_cert(CERT *c, X509 *x) {
401 if (!ssl_cert_add0_chain_cert(c, x)) {
402 return 0;
403 }
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 X509_up_ref(x);
406 return 1;
407}
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409int ssl_cert_select_current(CERT *c, X509 *x) {
410 int i;
411 if (x == NULL) {
412 return 0;
413 }
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 for (i = 0; i < SSL_PKEY_NUM; i++) {
416 if (c->pkeys[i].x509 == x) {
417 c->key = &c->pkeys[i];
418 return 1;
419 }
420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 for (i = 0; i < SSL_PKEY_NUM; i++) {
423 if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) {
424 c->key = &c->pkeys[i];
425 return 1;
426 }
427 }
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 return 0;
430}
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langleyfcf25832014-12-18 17:42:32 -0800432void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
433 c->cert_cb = cb;
434 c->cert_cb_arg = arg;
435}
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langleyfcf25832014-12-18 17:42:32 -0800437SESS_CERT *ssl_sess_cert_new(void) {
438 SESS_CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langleyfcf25832014-12-18 17:42:32 -0800440 ret = OPENSSL_malloc(sizeof *ret);
441 if (ret == NULL) {
442 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
443 return NULL;
444 }
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 memset(ret, 0, sizeof *ret);
447 ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 return ret;
450}
Adam Langley95c29f32014-06-20 12:00:00 -0700451
Adam Langleyfcf25832014-12-18 17:42:32 -0800452void ssl_sess_cert_free(SESS_CERT *sc) {
453 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 if (sc == NULL) {
456 return;
457 }
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 if (sc->cert_chain != NULL) {
460 sk_X509_pop_free(sc->cert_chain, X509_free);
461 }
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 for (i = 0; i < SSL_PKEY_NUM; i++) {
464 if (sc->peer_pkeys[i].x509 != NULL) {
465 X509_free(sc->peer_pkeys[i].x509);
466 }
467 }
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 if (sc->peer_dh_tmp != NULL) {
470 DH_free(sc->peer_dh_tmp);
471 }
472 if (sc->peer_ecdh_tmp != NULL) {
473 EC_KEY_free(sc->peer_ecdh_tmp);
474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 OPENSSL_free(sc);
477}
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langleyfcf25832014-12-18 17:42:32 -0800479int ssl_set_peer_cert_type(SESS_CERT *sc, int type) {
480 sc->peer_cert_type = type;
481 return 1;
482}
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk) {
485 X509 *x;
486 int i;
487 X509_STORE *verify_store;
488 X509_STORE_CTX ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langleyfcf25832014-12-18 17:42:32 -0800490 if (s->cert->verify_store) {
491 verify_store = s->cert->verify_store;
492 } else {
493 verify_store = s->ctx->cert_store;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 if (sk == NULL || sk_X509_num(sk) == 0) {
497 return 0;
498 }
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyfcf25832014-12-18 17:42:32 -0800500 x = sk_X509_value(sk, 0);
501 if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
502 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
503 return 0;
504 }
505 X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 /* We need to inherit the verify parameters. These can be determined by the
508 * context: if its a server it will verify SSL client certificates or vice
509 * versa. */
510 X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 /* Anything non-default in "param" should overwrite anything in the ctx. */
513 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 if (s->verify_callback) {
516 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
517 }
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 if (s->ctx->app_verify_callback != NULL) {
520 i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
521 } else {
522 i = X509_verify_cert(&ctx);
523 }
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Adam Langleyfcf25832014-12-18 17:42:32 -0800525 s->verify_result = ctx.error;
526 X509_STORE_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700527
Adam Langleyfcf25832014-12-18 17:42:32 -0800528 return i;
529}
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531static void set_client_CA_list(STACK_OF(X509_NAME) * *ca_list,
532 STACK_OF(X509_NAME) * name_list) {
533 if (*ca_list != NULL) {
534 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
535 }
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 *ca_list = name_list;
538}
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540STACK_OF(X509_NAME) * SSL_dup_CA_list(STACK_OF(X509_NAME) * sk) {
541 size_t i;
542 STACK_OF(X509_NAME) * ret;
543 X509_NAME *name;
544
545 ret = sk_X509_NAME_new_null();
546 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
547 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
548 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
549 sk_X509_NAME_pop_free(ret, X509_NAME_free);
550 return NULL;
551 }
552 }
553
554 return ret;
555}
556
557void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) * name_list) {
558 set_client_CA_list(&(s->client_CA), name_list);
559}
560
561void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) * name_list) {
562 set_client_CA_list(&(ctx->client_CA), name_list);
563}
564
565STACK_OF(X509_NAME) * SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
566 return ctx->client_CA;
567}
568
569STACK_OF(X509_NAME) * SSL_get_client_CA_list(const SSL *s) {
570 if (s->server) {
571 if (s->client_CA != NULL) {
572 return s->client_CA;
573 } else {
574 return s->ctx->client_CA;
575 }
576 } else {
577 if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
578 return s->s3->tmp.ca_names;
579 } else {
580 return NULL;
581 }
582 }
583}
584
585static int add_client_CA(STACK_OF(X509_NAME) * *sk, X509 *x) {
586 X509_NAME *name;
587
588 if (x == NULL) {
589 return 0;
590 }
591 if (*sk == NULL) {
592 *sk = sk_X509_NAME_new_null();
593 if (*sk == NULL) {
594 return 0;
595 }
596 }
597
598 name = X509_NAME_dup(X509_get_subject_name(x));
599 if (name == NULL) {
600 return 0;
601 }
602
603 if (!sk_X509_NAME_push(*sk, name)) {
604 X509_NAME_free(name);
605 return 0;
606 }
607
608 return 1;
609}
610
611int SSL_add_client_CA(SSL *ssl, X509 *x) {
612 return add_client_CA(&(ssl->client_CA), x);
613}
614
615int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
616 return add_client_CA(&(ctx->client_CA), x);
617}
618
619static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
620 return X509_NAME_cmp(*a, *b);
621}
622
623/* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
Adam Langley95c29f32014-06-20 12:00:00 -0700624 * it doesn't really have anything to do with clients (except that a common use
625 * for a stack of CAs is to send it to the client). Actually, it doesn't have
626 * much to do with CAs, either, since it will load any old cert.
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 *
Adam Langley95c29f32014-06-20 12:00:00 -0700628 * \param file the file containing one or more certs.
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 * \return a ::STACK containing the certs. */
630STACK_OF(X509_NAME) * SSL_load_client_CA_file(const char *file) {
631 BIO *in;
632 X509 *x = NULL;
633 X509_NAME *xn = NULL;
634 STACK_OF(X509_NAME) *ret = NULL, *sk;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 sk = sk_X509_NAME_new(xname_cmp);
637 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 if (sk == NULL || in == NULL) {
640 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
641 goto err;
642 }
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 if (!BIO_read_filename(in, file)) {
645 goto err;
646 }
Adam Langley95c29f32014-06-20 12:00:00 -0700647
Adam Langleyfcf25832014-12-18 17:42:32 -0800648 for (;;) {
649 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
650 break;
651 }
652 if (ret == NULL) {
653 ret = sk_X509_NAME_new_null();
654 if (ret == NULL) {
655 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
656 goto err;
657 }
658 }
659 xn = X509_get_subject_name(x);
660 if (xn == NULL) {
661 goto err;
662 }
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langleyfcf25832014-12-18 17:42:32 -0800664 /* check for duplicates */
665 xn = X509_NAME_dup(xn);
666 if (xn == NULL) {
667 goto err;
668 }
669 if (sk_X509_NAME_find(sk, NULL, xn)) {
670 X509_NAME_free(xn);
671 } else {
672 sk_X509_NAME_push(sk, xn);
673 sk_X509_NAME_push(ret, xn);
674 }
675 }
Adam Langley95c29f32014-06-20 12:00:00 -0700676
Adam Langleyfcf25832014-12-18 17:42:32 -0800677 if (0) {
678 err:
679 if (ret != NULL) {
680 sk_X509_NAME_pop_free(ret, X509_NAME_free);
681 }
682 ret = NULL;
683 }
684
685 if (sk != NULL) {
686 sk_X509_NAME_free(sk);
687 }
688 if (in != NULL) {
689 BIO_free(in);
690 }
691 if (x != NULL) {
692 X509_free(x);
693 }
694 if (ret != NULL) {
695 ERR_clear_error();
696 }
697 return ret;
698}
699
700/* Add a file of certs to a stack.
701 *
Adam Langley95c29f32014-06-20 12:00:00 -0700702 * \param stack the stack to add to.
703 * \param file the file to add from. All certs in this file that are not
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 * already in the stack will be added.
Adam Langley95c29f32014-06-20 12:00:00 -0700705 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800706 * certs may have been added to \c stack. */
707int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
708 const char *file) {
709 BIO *in;
710 X509 *x = NULL;
711 X509_NAME *xn = NULL;
712 int ret = 1;
713 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
Adam Langley95c29f32014-06-20 12:00:00 -0700714
Adam Langleyfcf25832014-12-18 17:42:32 -0800715 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
716 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700717
Adam Langleyfcf25832014-12-18 17:42:32 -0800718 if (in == NULL) {
719 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack,
720 ERR_R_MALLOC_FAILURE);
721 goto err;
722 }
Adam Langley95c29f32014-06-20 12:00:00 -0700723
Adam Langleyfcf25832014-12-18 17:42:32 -0800724 if (!BIO_read_filename(in, file)) {
725 goto err;
726 }
Adam Langley95c29f32014-06-20 12:00:00 -0700727
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 for (;;) {
729 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
730 break;
731 }
732 xn = X509_get_subject_name(x);
733 if (xn == NULL) {
734 goto err;
735 }
736 xn = X509_NAME_dup(xn);
737 if (xn == NULL) {
738 goto err;
739 }
740 if (sk_X509_NAME_find(stack, NULL, xn)) {
741 X509_NAME_free(xn);
742 } else {
743 sk_X509_NAME_push(stack, xn);
744 }
745 }
Adam Langley95c29f32014-06-20 12:00:00 -0700746
Adam Langleyfcf25832014-12-18 17:42:32 -0800747 ERR_clear_error();
748
749 if (0) {
750 err:
751 ret = 0;
752 }
753
754 if (in != NULL) {
755 BIO_free(in);
756 }
757 if (x != NULL) {
758 X509_free(x);
759 }
760
Adam Langleya307dfd2015-01-09 15:42:58 -0800761 (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800762
763 return ret;
764}
765
766/* Add a directory of certs to a stack.
767 *
Adam Langley95c29f32014-06-20 12:00:00 -0700768 * \param stack the stack to append to.
769 * \param dir the directory to append from. All files in this directory will be
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 * examined as potential certs. Any that are acceptable to
771 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
772 * be included.
Adam Langley95c29f32014-06-20 12:00:00 -0700773 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800774 * certs may have been added to \c stack. */
775int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
776 const char *dir) {
777 OPENSSL_DIR_CTX *d = NULL;
778 const char *filename;
779 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
Adam Langley95c29f32014-06-20 12:00:00 -0700782
Adam Langleyfcf25832014-12-18 17:42:32 -0800783 /* Note that a side effect is that the CAs will be sorted by name */
784 while ((filename = OPENSSL_DIR_read(&d, dir))) {
785 char buf[1024];
786 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
789 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack,
790 SSL_R_PATH_TOO_LONG);
791 goto err;
792 }
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
795 if (r <= 0 || r >= (int)sizeof(buf) ||
796 !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
797 goto err;
798 }
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 if (errno) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100802 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, ERR_R_SYS_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
804 goto err;
805 }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700808
809err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 if (d) {
811 OPENSSL_DIR_end(&d);
812 }
813 CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
814 return ret;
815}
Adam Langley95c29f32014-06-20 12:00:00 -0700816
817/* Add a certificate to a BUF_MEM structure */
Adam Langleyfcf25832014-12-18 17:42:32 -0800818static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
819 int n;
820 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 n = i2d_X509(x, NULL);
823 if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
824 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
825 return 0;
826 }
827 p = (uint8_t *)&(buf->data[*l]);
828 l2n3(n, p);
829 i2d_X509(x, &p);
830 *l += n + 3;
Adam Langley95c29f32014-06-20 12:00:00 -0700831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 return 1;
833}
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langley24819752014-12-15 18:42:07 -0800835/* Add certificate chain to internal SSL BUF_MEM structure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800836int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) {
837 BUF_MEM *buf = s->init_buf;
838 int no_chain = 0;
839 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700840
Adam Langleyfcf25832014-12-18 17:42:32 -0800841 X509 *x = NULL;
842 STACK_OF(X509) * extra_certs;
843 X509_STORE *chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -0700844
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 if (cpk) {
846 x = cpk->x509;
847 }
Adam Langley95c29f32014-06-20 12:00:00 -0700848
Adam Langleyfcf25832014-12-18 17:42:32 -0800849 if (s->cert->chain_store) {
850 chain_store = s->cert->chain_store;
851 } else {
852 chain_store = s->ctx->cert_store;
853 }
Adam Langley95c29f32014-06-20 12:00:00 -0700854
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 /* If we have a certificate specific chain use it, else use parent ctx. */
856 if (cpk && cpk->chain) {
857 extra_certs = cpk->chain;
858 } else {
859 extra_certs = s->ctx->extra_certs;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) {
863 no_chain = 1;
864 }
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 /* TLSv1 sends a chain with nothing in it, instead of an alert. */
867 if (!BUF_MEM_grow_clean(buf, 10)) {
868 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
869 return 0;
870 }
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 if (x != NULL) {
873 if (no_chain) {
874 if (!ssl_add_cert_to_buf(buf, l, x)) {
875 return 0;
876 }
877 } else {
878 X509_STORE_CTX xs_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
881 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
882 return 0;
883 }
884 X509_verify_cert(&xs_ctx);
885 /* Don't leave errors in the queue */
886 ERR_clear_error();
887 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
888 x = sk_X509_value(xs_ctx.chain, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 if (!ssl_add_cert_to_buf(buf, l, x)) {
891 X509_STORE_CTX_cleanup(&xs_ctx);
892 return 0;
893 }
894 }
895 X509_STORE_CTX_cleanup(&xs_ctx);
896 }
897 }
898
899 for (i = 0; i < sk_X509_num(extra_certs); i++) {
900 x = sk_X509_value(extra_certs, i);
901 if (!ssl_add_cert_to_buf(buf, l, x)) {
902 return 0;
903 }
904 }
905
906 return 1;
907}
Adam Langley95c29f32014-06-20 12:00:00 -0700908
909/* Build a certificate chain for current certificate */
Adam Langleyfcf25832014-12-18 17:42:32 -0800910int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) {
911 CERT_PKEY *cpk = c->key;
912 X509_STORE_CTX xs_ctx;
913 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
914 X509 *x;
915 int i, rv = 0;
916 unsigned long error;
Adam Langley95c29f32014-06-20 12:00:00 -0700917
Adam Langleyfcf25832014-12-18 17:42:32 -0800918 if (!cpk->x509) {
919 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
920 goto err;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 /* Rearranging and check the chain: add everything to a store */
924 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
925 size_t j;
926 chain_store = X509_STORE_new();
927 if (!chain_store) {
928 goto err;
929 }
Adam Langley95c29f32014-06-20 12:00:00 -0700930
Adam Langleyfcf25832014-12-18 17:42:32 -0800931 for (j = 0; j < sk_X509_num(cpk->chain); j++) {
932 x = sk_X509_value(cpk->chain, j);
933 if (!X509_STORE_add_cert(chain_store, x)) {
934 error = ERR_peek_last_error();
935 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
936 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
937 goto err;
938 }
939 ERR_clear_error();
940 }
941 }
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 /* Add EE cert too: it might be self signed */
944 if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
945 error = ERR_peek_last_error();
946 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
947 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
948 goto err;
949 }
950 ERR_clear_error();
951 }
952 } else {
953 if (c->chain_store) {
954 chain_store = c->chain_store;
955 }
Adam Langleyf669c2d2014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) {
958 untrusted = cpk->chain;
959 }
960 }
Adam Langley95c29f32014-06-20 12:00:00 -0700961
Adam Langleyfcf25832014-12-18 17:42:32 -0800962 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
963 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
964 goto err;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 i = X509_verify_cert(&xs_ctx);
968 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
969 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) {
970 ERR_clear_error();
971 }
972 i = 1;
973 rv = 2;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 if (i > 0) {
977 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
978 }
979 if (i <= 0) {
980 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain,
981 SSL_R_CERTIFICATE_VERIFY_FAILED);
982 i = X509_STORE_CTX_get_error(&xs_ctx);
983 ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i));
984
985 X509_STORE_CTX_cleanup(&xs_ctx);
986 goto err;
987 }
988
989 X509_STORE_CTX_cleanup(&xs_ctx);
990 if (cpk->chain) {
991 sk_X509_pop_free(cpk->chain, X509_free);
992 }
993
994 /* Remove EE certificate from chain */
995 x = sk_X509_shift(chain);
996 X509_free(x);
997 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
998 if (sk_X509_num(chain) > 0) {
999 /* See if last cert is self signed */
1000 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1001 X509_check_purpose(x, -1, 0);
1002 if (x->ex_flags & EXFLAG_SS) {
1003 x = sk_X509_pop(chain);
1004 X509_free(x);
1005 }
1006 }
1007 }
1008
1009 cpk->chain = chain;
David Benjamin6eb000d2015-02-11 01:17:41 -05001010 if (rv == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001011 rv = 1;
David Benjamin6eb000d2015-02-11 01:17:41 -05001012 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001013
1014err:
1015 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
1016 X509_STORE_free(chain_store);
1017 }
1018
1019 return rv;
1020}
1021
1022int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
1023 X509_STORE **pstore;
1024 if (chain) {
1025 pstore = &c->chain_store;
1026 } else {
1027 pstore = &c->verify_store;
1028 }
1029
1030 if (*pstore) {
1031 X509_STORE_free(*pstore);
1032 }
1033 *pstore = store;
1034
1035 if (ref && store) {
1036 CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
1037 }
1038 return 1;
1039}