blob: fbe38ec4e926f5b4cdb8a8ab2cdaa26eb8da1c6f [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
115#include <stdio.h>
116
117#include <openssl/bio.h>
118#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400119#include <openssl/buf.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/dh.h>
121#include <openssl/err.h>
122#include <openssl/mem.h>
123#include <openssl/obj.h>
124#include <openssl/pem.h>
125#include <openssl/x509v3.h>
126
127#include "../crypto/dh/internal.h"
128#include "../crypto/directory.h"
129#include "ssl_locl.h"
130
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langleyfcf25832014-12-18 17:42:32 -0800132int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
133 static int ssl_x509_store_ctx_idx = -1;
134 int got_write_lock = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700135
Adam Langleyfcf25832014-12-18 17:42:32 -0800136 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
Adam Langley95c29f32014-06-20 12:00:00 -0700137
Adam Langleyfcf25832014-12-18 17:42:32 -0800138 if (ssl_x509_store_ctx_idx < 0) {
139 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
140 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
141 got_write_lock = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700142
Adam Langleyfcf25832014-12-18 17:42:32 -0800143 if (ssl_x509_store_ctx_idx < 0) {
144 ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(
145 0, "SSL for verify callback", NULL, NULL, NULL);
146 }
147 }
Adam Langley95c29f32014-06-20 12:00:00 -0700148
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 if (got_write_lock) {
150 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
151 } else {
152 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
153 }
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langleyfcf25832014-12-18 17:42:32 -0800155 return ssl_x509_store_ctx_idx;
156}
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158CERT *ssl_cert_new(void) {
159 CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700160
Adam Langleyfcf25832014-12-18 17:42:32 -0800161 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
162 if (ret == NULL) {
163 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
164 return NULL;
165 }
166 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700167
Adam Langleyfcf25832014-12-18 17:42:32 -0800168 ret->key = &ret->pkeys[SSL_PKEY_RSA_ENC];
169 return ret;
170}
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langleyfcf25832014-12-18 17:42:32 -0800172CERT *ssl_cert_dup(CERT *cert) {
173 CERT *ret;
174 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
177 if (ret == NULL) {
178 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
179 return NULL;
180 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800181 memset(ret, 0, sizeof(CERT));
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
184 /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
185 * more readable */
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 ret->mask_k = cert->mask_k;
188 ret->mask_a = cert->mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langleyfcf25832014-12-18 17:42:32 -0800190 if (cert->dh_tmp != NULL) {
191 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
192 if (ret->dh_tmp == NULL) {
193 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
194 goto err;
195 }
196 if (cert->dh_tmp->priv_key) {
197 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
198 if (!b) {
199 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
200 goto err;
201 }
202 ret->dh_tmp->priv_key = b;
203 }
204 if (cert->dh_tmp->pub_key) {
205 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
206 if (!b) {
207 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
208 goto err;
209 }
210 ret->dh_tmp->pub_key = b;
211 }
212 }
213 ret->dh_tmp_cb = cert->dh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langleyfcf25832014-12-18 17:42:32 -0800215 if (cert->ecdh_tmp) {
216 ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
217 if (ret->ecdh_tmp == NULL) {
218 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB);
219 goto err;
220 }
221 }
222 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
223 ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langleyfcf25832014-12-18 17:42:32 -0800225 for (i = 0; i < SSL_PKEY_NUM; i++) {
226 CERT_PKEY *cpk = cert->pkeys + i;
227 CERT_PKEY *rpk = ret->pkeys + i;
228 if (cpk->x509 != NULL) {
229 rpk->x509 = X509_up_ref(cpk->x509);
230 }
Adam Langley95c29f32014-06-20 12:00:00 -0700231
Adam Langleyfcf25832014-12-18 17:42:32 -0800232 if (cpk->privatekey != NULL) {
233 rpk->privatekey = EVP_PKEY_dup(cpk->privatekey);
234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235
Adam Langleyfcf25832014-12-18 17:42:32 -0800236 if (cpk->chain) {
237 rpk->chain = X509_chain_up_ref(cpk->chain);
238 if (!rpk->chain) {
239 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
240 goto err;
241 }
242 }
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjaminb85a4c22015-03-19 14:47:27 -0400245 /* Copy over signature algorithm configuration. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 if (cert->conf_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400247 ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (!ret->conf_sigalgs) {
249 goto err;
250 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 ret->conf_sigalgslen = cert->conf_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 if (cert->client_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400255 ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
256 cert->client_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 if (!ret->client_sigalgs) {
258 goto err;
259 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 ret->client_sigalgslen = cert->client_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 }
David Benjaminb85a4c22015-03-19 14:47:27 -0400262
Adam Langleyfcf25832014-12-18 17:42:32 -0800263 /* Copy any custom client certificate types */
264 if (cert->client_certificate_types) {
265 ret->client_certificate_types = BUF_memdup(
266 cert->client_certificate_types, cert->num_client_certificate_types);
267 if (!ret->client_certificate_types) {
268 goto err;
269 }
270 ret->num_client_certificate_types = cert->num_client_certificate_types;
271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 ret->cert_flags = cert->cert_flags;
274
275 ret->cert_cb = cert->cert_cb;
276 ret->cert_cb_arg = cert->cert_cb_arg;
277
278 if (cert->verify_store) {
279 CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
280 ret->verify_store = cert->verify_store;
281 }
282
283 if (cert->chain_store) {
284 CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
285 ret->chain_store = cert->chain_store;
286 }
287
Adam Langleyfcf25832014-12-18 17:42:32 -0800288 return ret;
289
Adam Langley95c29f32014-06-20 12:00:00 -0700290err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800291 ssl_cert_free(ret);
292 return NULL;
293}
Adam Langley95c29f32014-06-20 12:00:00 -0700294
295/* Free up and clear all certificates and chains */
Adam Langleyfcf25832014-12-18 17:42:32 -0800296void ssl_cert_clear_certs(CERT *c) {
297 int i;
298 if (c == NULL) {
299 return;
300 }
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 for (i = 0; i < SSL_PKEY_NUM; i++) {
303 CERT_PKEY *cpk = c->pkeys + i;
304 if (cpk->x509) {
305 X509_free(cpk->x509);
306 cpk->x509 = NULL;
307 }
308 if (cpk->privatekey) {
309 EVP_PKEY_free(cpk->privatekey);
310 cpk->privatekey = NULL;
311 }
312 if (cpk->chain) {
313 sk_X509_pop_free(cpk->chain, X509_free);
314 cpk->chain = NULL;
315 }
316 }
317}
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319void ssl_cert_free(CERT *c) {
320 if (c == NULL) {
321 return;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 if (c->dh_tmp) {
325 DH_free(c->dh_tmp);
326 }
327 if (c->ecdh_tmp) {
328 EC_KEY_free(c->ecdh_tmp);
329 }
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 ssl_cert_clear_certs(c);
332 if (c->peer_sigalgs) {
333 OPENSSL_free(c->peer_sigalgs);
334 }
335 if (c->conf_sigalgs) {
336 OPENSSL_free(c->conf_sigalgs);
337 }
338 if (c->client_sigalgs) {
339 OPENSSL_free(c->client_sigalgs);
340 }
341 if (c->shared_sigalgs) {
342 OPENSSL_free(c->shared_sigalgs);
343 }
344 if (c->client_certificate_types) {
345 OPENSSL_free(c->client_certificate_types);
346 }
347 if (c->verify_store) {
348 X509_STORE_free(c->verify_store);
349 }
350 if (c->chain_store) {
351 X509_STORE_free(c->chain_store);
352 }
353 if (c->ciphers_raw) {
354 OPENSSL_free(c->ciphers_raw);
355 }
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 OPENSSL_free(c);
358}
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langleyfcf25832014-12-18 17:42:32 -0800360int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain) {
361 CERT_PKEY *cpk = c->key;
362 if (!cpk) {
363 return 0;
364 }
365 if (cpk->chain) {
366 sk_X509_pop_free(cpk->chain, X509_free);
367 }
368 cpk->chain = chain;
369 return 1;
370}
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain) {
373 STACK_OF(X509) * dchain;
374 if (!chain) {
375 return ssl_cert_set0_chain(c, NULL);
376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 dchain = X509_chain_up_ref(chain);
379 if (!dchain) {
380 return 0;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 if (!ssl_cert_set0_chain(c, dchain)) {
384 sk_X509_pop_free(dchain, X509_free);
385 return 0;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 return 1;
389}
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391int ssl_cert_add0_chain_cert(CERT *c, X509 *x) {
392 CERT_PKEY *cpk = c->key;
393 if (!cpk) {
394 return 0;
395 }
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 if (!cpk->chain) {
398 cpk->chain = sk_X509_new_null();
399 }
400 if (!cpk->chain || !sk_X509_push(cpk->chain, x)) {
401 return 0;
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 return 1;
405}
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407int ssl_cert_add1_chain_cert(CERT *c, X509 *x) {
408 if (!ssl_cert_add0_chain_cert(c, x)) {
409 return 0;
410 }
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 X509_up_ref(x);
413 return 1;
414}
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langleyfcf25832014-12-18 17:42:32 -0800416int ssl_cert_select_current(CERT *c, X509 *x) {
417 int i;
418 if (x == NULL) {
419 return 0;
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 == 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 for (i = 0; i < SSL_PKEY_NUM; i++) {
430 if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) {
431 c->key = &c->pkeys[i];
432 return 1;
433 }
434 }
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 return 0;
437}
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langleyfcf25832014-12-18 17:42:32 -0800439void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
440 c->cert_cb = cb;
441 c->cert_cb_arg = arg;
442}
Adam Langley95c29f32014-06-20 12:00:00 -0700443
Adam Langleyfcf25832014-12-18 17:42:32 -0800444SESS_CERT *ssl_sess_cert_new(void) {
445 SESS_CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 ret = OPENSSL_malloc(sizeof *ret);
448 if (ret == NULL) {
449 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
450 return NULL;
451 }
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 memset(ret, 0, sizeof *ret);
454 ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 return ret;
457}
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langleyfcf25832014-12-18 17:42:32 -0800459void ssl_sess_cert_free(SESS_CERT *sc) {
460 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 if (sc == NULL) {
463 return;
464 }
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langleyfcf25832014-12-18 17:42:32 -0800466 if (sc->cert_chain != NULL) {
467 sk_X509_pop_free(sc->cert_chain, X509_free);
468 }
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 for (i = 0; i < SSL_PKEY_NUM; i++) {
471 if (sc->peer_pkeys[i].x509 != NULL) {
472 X509_free(sc->peer_pkeys[i].x509);
473 }
474 }
Adam Langley95c29f32014-06-20 12:00:00 -0700475
Adam Langleyfcf25832014-12-18 17:42:32 -0800476 if (sc->peer_dh_tmp != NULL) {
477 DH_free(sc->peer_dh_tmp);
478 }
479 if (sc->peer_ecdh_tmp != NULL) {
480 EC_KEY_free(sc->peer_ecdh_tmp);
481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482
Adam Langleyfcf25832014-12-18 17:42:32 -0800483 OPENSSL_free(sc);
484}
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langleyfcf25832014-12-18 17:42:32 -0800486int ssl_set_peer_cert_type(SESS_CERT *sc, int type) {
487 sc->peer_cert_type = type;
488 return 1;
489}
Adam Langley95c29f32014-06-20 12:00:00 -0700490
Adam Langleyfcf25832014-12-18 17:42:32 -0800491int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk) {
492 X509 *x;
493 int i;
494 X509_STORE *verify_store;
495 X509_STORE_CTX ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langleyfcf25832014-12-18 17:42:32 -0800497 if (s->cert->verify_store) {
498 verify_store = s->cert->verify_store;
499 } else {
500 verify_store = s->ctx->cert_store;
501 }
Adam Langley95c29f32014-06-20 12:00:00 -0700502
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 if (sk == NULL || sk_X509_num(sk) == 0) {
504 return 0;
505 }
Adam Langley95c29f32014-06-20 12:00:00 -0700506
Adam Langleyfcf25832014-12-18 17:42:32 -0800507 x = sk_X509_value(sk, 0);
508 if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
509 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
510 return 0;
511 }
512 X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
Adam Langley95c29f32014-06-20 12:00:00 -0700513
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 /* We need to inherit the verify parameters. These can be determined by the
515 * context: if its a server it will verify SSL client certificates or vice
516 * versa. */
517 X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langleyfcf25832014-12-18 17:42:32 -0800519 /* Anything non-default in "param" should overwrite anything in the ctx. */
520 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 if (s->verify_callback) {
523 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
524 }
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langleyfcf25832014-12-18 17:42:32 -0800526 if (s->ctx->app_verify_callback != NULL) {
527 i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
528 } else {
529 i = X509_verify_cert(&ctx);
530 }
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langleyfcf25832014-12-18 17:42:32 -0800532 s->verify_result = ctx.error;
533 X509_STORE_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 return i;
536}
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538static void set_client_CA_list(STACK_OF(X509_NAME) * *ca_list,
539 STACK_OF(X509_NAME) * name_list) {
540 if (*ca_list != NULL) {
541 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
Adam Langleyfcf25832014-12-18 17:42:32 -0800544 *ca_list = name_list;
545}
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langleyfcf25832014-12-18 17:42:32 -0800547STACK_OF(X509_NAME) * SSL_dup_CA_list(STACK_OF(X509_NAME) * sk) {
548 size_t i;
549 STACK_OF(X509_NAME) * ret;
550 X509_NAME *name;
551
552 ret = sk_X509_NAME_new_null();
553 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
554 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
555 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
556 sk_X509_NAME_pop_free(ret, X509_NAME_free);
557 return NULL;
558 }
559 }
560
561 return ret;
562}
563
564void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) * name_list) {
565 set_client_CA_list(&(s->client_CA), name_list);
566}
567
568void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) * name_list) {
569 set_client_CA_list(&(ctx->client_CA), name_list);
570}
571
572STACK_OF(X509_NAME) * SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
573 return ctx->client_CA;
574}
575
576STACK_OF(X509_NAME) * SSL_get_client_CA_list(const SSL *s) {
577 if (s->server) {
578 if (s->client_CA != NULL) {
579 return s->client_CA;
580 } else {
581 return s->ctx->client_CA;
582 }
583 } else {
584 if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
585 return s->s3->tmp.ca_names;
586 } else {
587 return NULL;
588 }
589 }
590}
591
592static int add_client_CA(STACK_OF(X509_NAME) * *sk, X509 *x) {
593 X509_NAME *name;
594
595 if (x == NULL) {
596 return 0;
597 }
598 if (*sk == NULL) {
599 *sk = sk_X509_NAME_new_null();
600 if (*sk == NULL) {
601 return 0;
602 }
603 }
604
605 name = X509_NAME_dup(X509_get_subject_name(x));
606 if (name == NULL) {
607 return 0;
608 }
609
610 if (!sk_X509_NAME_push(*sk, name)) {
611 X509_NAME_free(name);
612 return 0;
613 }
614
615 return 1;
616}
617
618int SSL_add_client_CA(SSL *ssl, X509 *x) {
619 return add_client_CA(&(ssl->client_CA), x);
620}
621
622int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
623 return add_client_CA(&(ctx->client_CA), x);
624}
625
626static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
627 return X509_NAME_cmp(*a, *b);
628}
629
630/* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
Adam Langley95c29f32014-06-20 12:00:00 -0700631 * it doesn't really have anything to do with clients (except that a common use
632 * for a stack of CAs is to send it to the client). Actually, it doesn't have
633 * much to do with CAs, either, since it will load any old cert.
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 *
Adam Langley95c29f32014-06-20 12:00:00 -0700635 * \param file the file containing one or more certs.
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 * \return a ::STACK containing the certs. */
637STACK_OF(X509_NAME) * SSL_load_client_CA_file(const char *file) {
638 BIO *in;
639 X509 *x = NULL;
640 X509_NAME *xn = NULL;
641 STACK_OF(X509_NAME) *ret = NULL, *sk;
Adam Langley95c29f32014-06-20 12:00:00 -0700642
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 sk = sk_X509_NAME_new(xname_cmp);
644 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 if (sk == NULL || in == NULL) {
647 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
648 goto err;
649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
Adam Langleyfcf25832014-12-18 17:42:32 -0800651 if (!BIO_read_filename(in, file)) {
652 goto err;
653 }
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 for (;;) {
656 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
657 break;
658 }
659 if (ret == NULL) {
660 ret = sk_X509_NAME_new_null();
661 if (ret == NULL) {
662 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
663 goto err;
664 }
665 }
666 xn = X509_get_subject_name(x);
667 if (xn == NULL) {
668 goto err;
669 }
Adam Langley95c29f32014-06-20 12:00:00 -0700670
Adam Langleyfcf25832014-12-18 17:42:32 -0800671 /* check for duplicates */
672 xn = X509_NAME_dup(xn);
673 if (xn == NULL) {
674 goto err;
675 }
676 if (sk_X509_NAME_find(sk, NULL, xn)) {
677 X509_NAME_free(xn);
678 } else {
679 sk_X509_NAME_push(sk, xn);
680 sk_X509_NAME_push(ret, xn);
681 }
682 }
Adam Langley95c29f32014-06-20 12:00:00 -0700683
Adam Langleyfcf25832014-12-18 17:42:32 -0800684 if (0) {
685 err:
686 if (ret != NULL) {
687 sk_X509_NAME_pop_free(ret, X509_NAME_free);
688 }
689 ret = NULL;
690 }
691
692 if (sk != NULL) {
693 sk_X509_NAME_free(sk);
694 }
695 if (in != NULL) {
696 BIO_free(in);
697 }
698 if (x != NULL) {
699 X509_free(x);
700 }
701 if (ret != NULL) {
702 ERR_clear_error();
703 }
704 return ret;
705}
706
707/* Add a file of certs to a stack.
708 *
Adam Langley95c29f32014-06-20 12:00:00 -0700709 * \param stack the stack to add to.
710 * \param file the file to add from. All certs in this file that are not
Adam Langleyfcf25832014-12-18 17:42:32 -0800711 * already in the stack will be added.
Adam Langley95c29f32014-06-20 12:00:00 -0700712 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 * certs may have been added to \c stack. */
714int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
715 const char *file) {
716 BIO *in;
717 X509 *x = NULL;
718 X509_NAME *xn = NULL;
719 int ret = 1;
720 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
723 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700724
Adam Langleyfcf25832014-12-18 17:42:32 -0800725 if (in == NULL) {
726 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack,
727 ERR_R_MALLOC_FAILURE);
728 goto err;
729 }
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 if (!BIO_read_filename(in, file)) {
732 goto err;
733 }
Adam Langley95c29f32014-06-20 12:00:00 -0700734
Adam Langleyfcf25832014-12-18 17:42:32 -0800735 for (;;) {
736 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
737 break;
738 }
739 xn = X509_get_subject_name(x);
740 if (xn == NULL) {
741 goto err;
742 }
743 xn = X509_NAME_dup(xn);
744 if (xn == NULL) {
745 goto err;
746 }
747 if (sk_X509_NAME_find(stack, NULL, xn)) {
748 X509_NAME_free(xn);
749 } else {
750 sk_X509_NAME_push(stack, xn);
751 }
752 }
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 ERR_clear_error();
755
756 if (0) {
757 err:
758 ret = 0;
759 }
760
761 if (in != NULL) {
762 BIO_free(in);
763 }
764 if (x != NULL) {
765 X509_free(x);
766 }
767
Adam Langleya307dfd2015-01-09 15:42:58 -0800768 (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800769
770 return ret;
771}
772
773/* Add a directory of certs to a stack.
774 *
Adam Langley95c29f32014-06-20 12:00:00 -0700775 * \param stack the stack to append to.
776 * \param dir the directory to append from. All files in this directory will be
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 * examined as potential certs. Any that are acceptable to
778 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
779 * be included.
Adam Langley95c29f32014-06-20 12:00:00 -0700780 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 * certs may have been added to \c stack. */
782int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
783 const char *dir) {
784 OPENSSL_DIR_CTX *d = NULL;
785 const char *filename;
786 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700787
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
Adam Langley95c29f32014-06-20 12:00:00 -0700789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 /* Note that a side effect is that the CAs will be sorted by name */
791 while ((filename = OPENSSL_DIR_read(&d, dir))) {
792 char buf[1024];
793 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
796 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack,
797 SSL_R_PATH_TOO_LONG);
798 goto err;
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
802 if (r <= 0 || r >= (int)sizeof(buf) ||
803 !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
804 goto err;
805 }
806 }
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 if (errno) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100809 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, ERR_R_SYS_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
811 goto err;
812 }
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700815
816err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 if (d) {
818 OPENSSL_DIR_end(&d);
819 }
820 CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
821 return ret;
822}
Adam Langley95c29f32014-06-20 12:00:00 -0700823
824/* Add a certificate to a BUF_MEM structure */
Adam Langleyfcf25832014-12-18 17:42:32 -0800825static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
826 int n;
827 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700828
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 n = i2d_X509(x, NULL);
830 if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
831 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
832 return 0;
833 }
834 p = (uint8_t *)&(buf->data[*l]);
835 l2n3(n, p);
836 i2d_X509(x, &p);
837 *l += n + 3;
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 return 1;
840}
Adam Langley95c29f32014-06-20 12:00:00 -0700841
Adam Langley24819752014-12-15 18:42:07 -0800842/* Add certificate chain to internal SSL BUF_MEM structure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800843int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) {
844 BUF_MEM *buf = s->init_buf;
845 int no_chain = 0;
846 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 X509 *x = NULL;
849 STACK_OF(X509) * extra_certs;
850 X509_STORE *chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -0700851
Adam Langleyfcf25832014-12-18 17:42:32 -0800852 if (cpk) {
853 x = cpk->x509;
854 }
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if (s->cert->chain_store) {
857 chain_store = s->cert->chain_store;
858 } else {
859 chain_store = s->ctx->cert_store;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 /* If we have a certificate specific chain use it, else use parent ctx. */
863 if (cpk && cpk->chain) {
864 extra_certs = cpk->chain;
865 } else {
866 extra_certs = s->ctx->extra_certs;
867 }
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) {
870 no_chain = 1;
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 /* TLSv1 sends a chain with nothing in it, instead of an alert. */
874 if (!BUF_MEM_grow_clean(buf, 10)) {
875 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
876 return 0;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 if (x != NULL) {
880 if (no_chain) {
881 if (!ssl_add_cert_to_buf(buf, l, x)) {
882 return 0;
883 }
884 } else {
885 X509_STORE_CTX xs_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
888 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
889 return 0;
890 }
891 X509_verify_cert(&xs_ctx);
892 /* Don't leave errors in the queue */
893 ERR_clear_error();
894 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
895 x = sk_X509_value(xs_ctx.chain, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700896
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 if (!ssl_add_cert_to_buf(buf, l, x)) {
898 X509_STORE_CTX_cleanup(&xs_ctx);
899 return 0;
900 }
901 }
902 X509_STORE_CTX_cleanup(&xs_ctx);
903 }
904 }
905
906 for (i = 0; i < sk_X509_num(extra_certs); i++) {
907 x = sk_X509_value(extra_certs, i);
908 if (!ssl_add_cert_to_buf(buf, l, x)) {
909 return 0;
910 }
911 }
912
913 return 1;
914}
Adam Langley95c29f32014-06-20 12:00:00 -0700915
916/* Build a certificate chain for current certificate */
Adam Langleyfcf25832014-12-18 17:42:32 -0800917int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) {
918 CERT_PKEY *cpk = c->key;
919 X509_STORE_CTX xs_ctx;
920 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
921 X509 *x;
922 int i, rv = 0;
923 unsigned long error;
Adam Langley95c29f32014-06-20 12:00:00 -0700924
Adam Langleyfcf25832014-12-18 17:42:32 -0800925 if (!cpk->x509) {
926 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
927 goto err;
928 }
Adam Langley95c29f32014-06-20 12:00:00 -0700929
Adam Langleyfcf25832014-12-18 17:42:32 -0800930 /* Rearranging and check the chain: add everything to a store */
931 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
932 size_t j;
933 chain_store = X509_STORE_new();
934 if (!chain_store) {
935 goto err;
936 }
Adam Langley95c29f32014-06-20 12:00:00 -0700937
Adam Langleyfcf25832014-12-18 17:42:32 -0800938 for (j = 0; j < sk_X509_num(cpk->chain); j++) {
939 x = sk_X509_value(cpk->chain, j);
940 if (!X509_STORE_add_cert(chain_store, x)) {
941 error = ERR_peek_last_error();
942 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
943 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
944 goto err;
945 }
946 ERR_clear_error();
947 }
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 /* Add EE cert too: it might be self signed */
951 if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
952 error = ERR_peek_last_error();
953 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
954 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
955 goto err;
956 }
957 ERR_clear_error();
958 }
959 } else {
960 if (c->chain_store) {
961 chain_store = c->chain_store;
962 }
Adam Langleyf669c2d2014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) {
965 untrusted = cpk->chain;
966 }
967 }
Adam Langley95c29f32014-06-20 12:00:00 -0700968
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
970 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
971 goto err;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 i = X509_verify_cert(&xs_ctx);
975 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
976 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) {
977 ERR_clear_error();
978 }
979 i = 1;
980 rv = 2;
981 }
Adam Langley95c29f32014-06-20 12:00:00 -0700982
Adam Langleyfcf25832014-12-18 17:42:32 -0800983 if (i > 0) {
984 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
985 }
986 if (i <= 0) {
987 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain,
988 SSL_R_CERTIFICATE_VERIFY_FAILED);
989 i = X509_STORE_CTX_get_error(&xs_ctx);
990 ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i));
991
992 X509_STORE_CTX_cleanup(&xs_ctx);
993 goto err;
994 }
995
996 X509_STORE_CTX_cleanup(&xs_ctx);
997 if (cpk->chain) {
998 sk_X509_pop_free(cpk->chain, X509_free);
999 }
1000
1001 /* Remove EE certificate from chain */
1002 x = sk_X509_shift(chain);
1003 X509_free(x);
1004 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
1005 if (sk_X509_num(chain) > 0) {
1006 /* See if last cert is self signed */
1007 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1008 X509_check_purpose(x, -1, 0);
1009 if (x->ex_flags & EXFLAG_SS) {
1010 x = sk_X509_pop(chain);
1011 X509_free(x);
1012 }
1013 }
1014 }
1015
1016 cpk->chain = chain;
David Benjamin6eb000d2015-02-11 01:17:41 -05001017 if (rv == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001018 rv = 1;
David Benjamin6eb000d2015-02-11 01:17:41 -05001019 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001020
1021err:
1022 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
1023 X509_STORE_free(chain_store);
1024 }
1025
1026 return rv;
1027}
1028
1029int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
1030 X509_STORE **pstore;
1031 if (chain) {
1032 pstore = &c->chain_store;
1033 } else {
1034 pstore = &c->verify_store;
1035 }
1036
1037 if (*pstore) {
1038 X509_STORE_free(*pstore);
1039 }
1040 *pstore = store;
1041
1042 if (ref && store) {
1043 CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
1044 }
1045 return 1;
1046}