blob: cb3a0392a9a0121990498bce072aea6a18c8f9a2 [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"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400129#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700130
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
David Benjaminc0f763b2015-03-27 02:05:39 -0400215 ret->ecdh_nid = cert->ecdh_nid;
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
217 ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 for (i = 0; i < SSL_PKEY_NUM; i++) {
220 CERT_PKEY *cpk = cert->pkeys + i;
221 CERT_PKEY *rpk = ret->pkeys + i;
222 if (cpk->x509 != NULL) {
223 rpk->x509 = X509_up_ref(cpk->x509);
224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (cpk->privatekey != NULL) {
227 rpk->privatekey = EVP_PKEY_dup(cpk->privatekey);
228 }
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 if (cpk->chain) {
231 rpk->chain = X509_chain_up_ref(cpk->chain);
232 if (!rpk->chain) {
233 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
234 goto err;
235 }
236 }
237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjaminb85a4c22015-03-19 14:47:27 -0400239 /* Copy over signature algorithm configuration. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800240 if (cert->conf_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400241 ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 if (!ret->conf_sigalgs) {
243 goto err;
244 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800245 ret->conf_sigalgslen = cert->conf_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 }
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langleyfcf25832014-12-18 17:42:32 -0800248 if (cert->client_sigalgs) {
David Benjaminb85a4c22015-03-19 14:47:27 -0400249 ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
250 cert->client_sigalgslen);
Adam Langleyfcf25832014-12-18 17:42:32 -0800251 if (!ret->client_sigalgs) {
252 goto err;
253 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800254 ret->client_sigalgslen = cert->client_sigalgslen;
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 }
David Benjaminb85a4c22015-03-19 14:47:27 -0400256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257 /* Copy any custom client certificate types */
258 if (cert->client_certificate_types) {
259 ret->client_certificate_types = BUF_memdup(
260 cert->client_certificate_types, cert->num_client_certificate_types);
261 if (!ret->client_certificate_types) {
262 goto err;
263 }
264 ret->num_client_certificate_types = cert->num_client_certificate_types;
265 }
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 ret->cert_flags = cert->cert_flags;
268
269 ret->cert_cb = cert->cert_cb;
270 ret->cert_cb_arg = cert->cert_cb_arg;
271
272 if (cert->verify_store) {
273 CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
274 ret->verify_store = cert->verify_store;
275 }
276
277 if (cert->chain_store) {
278 CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
279 ret->chain_store = cert->chain_store;
280 }
281
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 return ret;
283
Adam Langley95c29f32014-06-20 12:00:00 -0700284err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 ssl_cert_free(ret);
286 return NULL;
287}
Adam Langley95c29f32014-06-20 12:00:00 -0700288
289/* Free up and clear all certificates and chains */
Adam Langleyfcf25832014-12-18 17:42:32 -0800290void ssl_cert_clear_certs(CERT *c) {
291 int i;
292 if (c == NULL) {
293 return;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langleyfcf25832014-12-18 17:42:32 -0800296 for (i = 0; i < SSL_PKEY_NUM; i++) {
297 CERT_PKEY *cpk = c->pkeys + i;
298 if (cpk->x509) {
299 X509_free(cpk->x509);
300 cpk->x509 = NULL;
301 }
302 if (cpk->privatekey) {
303 EVP_PKEY_free(cpk->privatekey);
304 cpk->privatekey = NULL;
305 }
306 if (cpk->chain) {
307 sk_X509_pop_free(cpk->chain, X509_free);
308 cpk->chain = NULL;
309 }
310 }
311}
Adam Langley95c29f32014-06-20 12:00:00 -0700312
Adam Langleyfcf25832014-12-18 17:42:32 -0800313void ssl_cert_free(CERT *c) {
314 if (c == NULL) {
315 return;
316 }
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (c->dh_tmp) {
319 DH_free(c->dh_tmp);
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 ssl_cert_clear_certs(c);
323 if (c->peer_sigalgs) {
324 OPENSSL_free(c->peer_sigalgs);
325 }
326 if (c->conf_sigalgs) {
327 OPENSSL_free(c->conf_sigalgs);
328 }
329 if (c->client_sigalgs) {
330 OPENSSL_free(c->client_sigalgs);
331 }
332 if (c->shared_sigalgs) {
333 OPENSSL_free(c->shared_sigalgs);
334 }
335 if (c->client_certificate_types) {
336 OPENSSL_free(c->client_certificate_types);
337 }
338 if (c->verify_store) {
339 X509_STORE_free(c->verify_store);
340 }
341 if (c->chain_store) {
342 X509_STORE_free(c->chain_store);
343 }
344 if (c->ciphers_raw) {
345 OPENSSL_free(c->ciphers_raw);
346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langleyfcf25832014-12-18 17:42:32 -0800348 OPENSSL_free(c);
349}
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain) {
352 CERT_PKEY *cpk = c->key;
353 if (!cpk) {
354 return 0;
355 }
356 if (cpk->chain) {
357 sk_X509_pop_free(cpk->chain, X509_free);
358 }
359 cpk->chain = chain;
360 return 1;
361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain) {
364 STACK_OF(X509) * dchain;
365 if (!chain) {
366 return ssl_cert_set0_chain(c, NULL);
367 }
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 dchain = X509_chain_up_ref(chain);
370 if (!dchain) {
371 return 0;
372 }
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langleyfcf25832014-12-18 17:42:32 -0800374 if (!ssl_cert_set0_chain(c, dchain)) {
375 sk_X509_pop_free(dchain, X509_free);
376 return 0;
377 }
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 return 1;
380}
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382int ssl_cert_add0_chain_cert(CERT *c, X509 *x) {
383 CERT_PKEY *cpk = c->key;
384 if (!cpk) {
385 return 0;
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 if (!cpk->chain) {
389 cpk->chain = sk_X509_new_null();
390 }
391 if (!cpk->chain || !sk_X509_push(cpk->chain, x)) {
392 return 0;
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langleyfcf25832014-12-18 17:42:32 -0800395 return 1;
396}
Adam Langley95c29f32014-06-20 12:00:00 -0700397
Adam Langleyfcf25832014-12-18 17:42:32 -0800398int ssl_cert_add1_chain_cert(CERT *c, X509 *x) {
399 if (!ssl_cert_add0_chain_cert(c, x)) {
400 return 0;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
Adam Langleyfcf25832014-12-18 17:42:32 -0800403 X509_up_ref(x);
404 return 1;
405}
Adam Langley95c29f32014-06-20 12:00:00 -0700406
Adam Langleyfcf25832014-12-18 17:42:32 -0800407int ssl_cert_select_current(CERT *c, X509 *x) {
408 int i;
409 if (x == NULL) {
410 return 0;
411 }
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 for (i = 0; i < SSL_PKEY_NUM; i++) {
414 if (c->pkeys[i].x509 == x) {
415 c->key = &c->pkeys[i];
416 return 1;
417 }
418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langleyfcf25832014-12-18 17:42:32 -0800420 for (i = 0; i < SSL_PKEY_NUM; i++) {
421 if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) {
422 c->key = &c->pkeys[i];
423 return 1;
424 }
425 }
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langleyfcf25832014-12-18 17:42:32 -0800427 return 0;
428}
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langleyfcf25832014-12-18 17:42:32 -0800430void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
431 c->cert_cb = cb;
432 c->cert_cb_arg = arg;
433}
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langleyfcf25832014-12-18 17:42:32 -0800435SESS_CERT *ssl_sess_cert_new(void) {
436 SESS_CERT *ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 ret = OPENSSL_malloc(sizeof *ret);
439 if (ret == NULL) {
440 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
441 return NULL;
442 }
Adam Langley95c29f32014-06-20 12:00:00 -0700443
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 memset(ret, 0, sizeof *ret);
445 ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 return ret;
448}
Adam Langley95c29f32014-06-20 12:00:00 -0700449
Adam Langleyfcf25832014-12-18 17:42:32 -0800450void ssl_sess_cert_free(SESS_CERT *sc) {
451 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 if (sc == NULL) {
454 return;
455 }
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 if (sc->cert_chain != NULL) {
458 sk_X509_pop_free(sc->cert_chain, X509_free);
459 }
Adam Langley95c29f32014-06-20 12:00:00 -0700460
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 for (i = 0; i < SSL_PKEY_NUM; i++) {
462 if (sc->peer_pkeys[i].x509 != NULL) {
463 X509_free(sc->peer_pkeys[i].x509);
464 }
465 }
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 if (sc->peer_dh_tmp != NULL) {
468 DH_free(sc->peer_dh_tmp);
469 }
470 if (sc->peer_ecdh_tmp != NULL) {
471 EC_KEY_free(sc->peer_ecdh_tmp);
472 }
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 OPENSSL_free(sc);
475}
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langleyfcf25832014-12-18 17:42:32 -0800477int ssl_set_peer_cert_type(SESS_CERT *sc, int type) {
478 sc->peer_cert_type = type;
479 return 1;
480}
Adam Langley95c29f32014-06-20 12:00:00 -0700481
Adam Langleyfcf25832014-12-18 17:42:32 -0800482int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk) {
483 X509 *x;
484 int i;
485 X509_STORE *verify_store;
486 X509_STORE_CTX ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langleyfcf25832014-12-18 17:42:32 -0800488 if (s->cert->verify_store) {
489 verify_store = s->cert->verify_store;
490 } else {
491 verify_store = s->ctx->cert_store;
492 }
Adam Langley95c29f32014-06-20 12:00:00 -0700493
Adam Langleyfcf25832014-12-18 17:42:32 -0800494 if (sk == NULL || sk_X509_num(sk) == 0) {
495 return 0;
496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langleyfcf25832014-12-18 17:42:32 -0800498 x = sk_X509_value(sk, 0);
499 if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
500 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
501 return 0;
502 }
503 X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
Adam Langley95c29f32014-06-20 12:00:00 -0700504
Adam Langleyfcf25832014-12-18 17:42:32 -0800505 /* We need to inherit the verify parameters. These can be determined by the
506 * context: if its a server it will verify SSL client certificates or vice
507 * versa. */
508 X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langleyfcf25832014-12-18 17:42:32 -0800510 /* Anything non-default in "param" should overwrite anything in the ctx. */
511 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
Adam Langley95c29f32014-06-20 12:00:00 -0700512
Adam Langleyfcf25832014-12-18 17:42:32 -0800513 if (s->verify_callback) {
514 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
515 }
Adam Langley95c29f32014-06-20 12:00:00 -0700516
Adam Langleyfcf25832014-12-18 17:42:32 -0800517 if (s->ctx->app_verify_callback != NULL) {
518 i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
519 } else {
520 i = X509_verify_cert(&ctx);
521 }
Adam Langley95c29f32014-06-20 12:00:00 -0700522
Adam Langleyfcf25832014-12-18 17:42:32 -0800523 s->verify_result = ctx.error;
524 X509_STORE_CTX_cleanup(&ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langleyfcf25832014-12-18 17:42:32 -0800526 return i;
527}
Adam Langley95c29f32014-06-20 12:00:00 -0700528
Adam Langleyfcf25832014-12-18 17:42:32 -0800529static void set_client_CA_list(STACK_OF(X509_NAME) * *ca_list,
530 STACK_OF(X509_NAME) * name_list) {
531 if (*ca_list != NULL) {
532 sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 *ca_list = name_list;
536}
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538STACK_OF(X509_NAME) * SSL_dup_CA_list(STACK_OF(X509_NAME) * sk) {
539 size_t i;
540 STACK_OF(X509_NAME) * ret;
541 X509_NAME *name;
542
543 ret = sk_X509_NAME_new_null();
544 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
545 name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
546 if (name == NULL || !sk_X509_NAME_push(ret, name)) {
547 sk_X509_NAME_pop_free(ret, X509_NAME_free);
548 return NULL;
549 }
550 }
551
552 return ret;
553}
554
555void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) * name_list) {
556 set_client_CA_list(&(s->client_CA), name_list);
557}
558
559void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) * name_list) {
560 set_client_CA_list(&(ctx->client_CA), name_list);
561}
562
563STACK_OF(X509_NAME) * SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
564 return ctx->client_CA;
565}
566
567STACK_OF(X509_NAME) * SSL_get_client_CA_list(const SSL *s) {
568 if (s->server) {
569 if (s->client_CA != NULL) {
570 return s->client_CA;
571 } else {
572 return s->ctx->client_CA;
573 }
574 } else {
575 if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
576 return s->s3->tmp.ca_names;
577 } else {
578 return NULL;
579 }
580 }
581}
582
583static int add_client_CA(STACK_OF(X509_NAME) * *sk, X509 *x) {
584 X509_NAME *name;
585
586 if (x == NULL) {
587 return 0;
588 }
589 if (*sk == NULL) {
590 *sk = sk_X509_NAME_new_null();
591 if (*sk == NULL) {
592 return 0;
593 }
594 }
595
596 name = X509_NAME_dup(X509_get_subject_name(x));
597 if (name == NULL) {
598 return 0;
599 }
600
601 if (!sk_X509_NAME_push(*sk, name)) {
602 X509_NAME_free(name);
603 return 0;
604 }
605
606 return 1;
607}
608
609int SSL_add_client_CA(SSL *ssl, X509 *x) {
610 return add_client_CA(&(ssl->client_CA), x);
611}
612
613int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
614 return add_client_CA(&(ctx->client_CA), x);
615}
616
617static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
618 return X509_NAME_cmp(*a, *b);
619}
620
621/* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
Adam Langley95c29f32014-06-20 12:00:00 -0700622 * it doesn't really have anything to do with clients (except that a common use
623 * for a stack of CAs is to send it to the client). Actually, it doesn't have
624 * much to do with CAs, either, since it will load any old cert.
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 *
Adam Langley95c29f32014-06-20 12:00:00 -0700626 * \param file the file containing one or more certs.
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 * \return a ::STACK containing the certs. */
628STACK_OF(X509_NAME) * SSL_load_client_CA_file(const char *file) {
629 BIO *in;
630 X509 *x = NULL;
631 X509_NAME *xn = NULL;
632 STACK_OF(X509_NAME) *ret = NULL, *sk;
Adam Langley95c29f32014-06-20 12:00:00 -0700633
Adam Langleyfcf25832014-12-18 17:42:32 -0800634 sk = sk_X509_NAME_new(xname_cmp);
635 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700636
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 if (sk == NULL || in == NULL) {
638 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
639 goto err;
640 }
Adam Langley95c29f32014-06-20 12:00:00 -0700641
Adam Langleyfcf25832014-12-18 17:42:32 -0800642 if (!BIO_read_filename(in, file)) {
643 goto err;
644 }
Adam Langley95c29f32014-06-20 12:00:00 -0700645
Adam Langleyfcf25832014-12-18 17:42:32 -0800646 for (;;) {
647 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
648 break;
649 }
650 if (ret == NULL) {
651 ret = sk_X509_NAME_new_null();
652 if (ret == NULL) {
653 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
654 goto err;
655 }
656 }
657 xn = X509_get_subject_name(x);
658 if (xn == NULL) {
659 goto err;
660 }
Adam Langley95c29f32014-06-20 12:00:00 -0700661
Adam Langleyfcf25832014-12-18 17:42:32 -0800662 /* check for duplicates */
663 xn = X509_NAME_dup(xn);
664 if (xn == NULL) {
665 goto err;
666 }
667 if (sk_X509_NAME_find(sk, NULL, xn)) {
668 X509_NAME_free(xn);
669 } else {
670 sk_X509_NAME_push(sk, xn);
671 sk_X509_NAME_push(ret, xn);
672 }
673 }
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langleyfcf25832014-12-18 17:42:32 -0800675 if (0) {
676 err:
677 if (ret != NULL) {
678 sk_X509_NAME_pop_free(ret, X509_NAME_free);
679 }
680 ret = NULL;
681 }
682
683 if (sk != NULL) {
684 sk_X509_NAME_free(sk);
685 }
686 if (in != NULL) {
687 BIO_free(in);
688 }
689 if (x != NULL) {
690 X509_free(x);
691 }
692 if (ret != NULL) {
693 ERR_clear_error();
694 }
695 return ret;
696}
697
698/* Add a file of certs to a stack.
699 *
Adam Langley95c29f32014-06-20 12:00:00 -0700700 * \param stack the stack to add to.
701 * \param file the file to add from. All certs in this file that are not
Adam Langleyfcf25832014-12-18 17:42:32 -0800702 * already in the stack will be added.
Adam Langley95c29f32014-06-20 12:00:00 -0700703 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 * certs may have been added to \c stack. */
705int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
706 const char *file) {
707 BIO *in;
708 X509 *x = NULL;
709 X509_NAME *xn = NULL;
710 int ret = 1;
711 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
714 in = BIO_new(BIO_s_file());
Adam Langley95c29f32014-06-20 12:00:00 -0700715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716 if (in == NULL) {
717 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack,
718 ERR_R_MALLOC_FAILURE);
719 goto err;
720 }
Adam Langley95c29f32014-06-20 12:00:00 -0700721
Adam Langleyfcf25832014-12-18 17:42:32 -0800722 if (!BIO_read_filename(in, file)) {
723 goto err;
724 }
Adam Langley95c29f32014-06-20 12:00:00 -0700725
Adam Langleyfcf25832014-12-18 17:42:32 -0800726 for (;;) {
727 if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
728 break;
729 }
730 xn = X509_get_subject_name(x);
731 if (xn == NULL) {
732 goto err;
733 }
734 xn = X509_NAME_dup(xn);
735 if (xn == NULL) {
736 goto err;
737 }
738 if (sk_X509_NAME_find(stack, NULL, xn)) {
739 X509_NAME_free(xn);
740 } else {
741 sk_X509_NAME_push(stack, xn);
742 }
743 }
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745 ERR_clear_error();
746
747 if (0) {
748 err:
749 ret = 0;
750 }
751
752 if (in != NULL) {
753 BIO_free(in);
754 }
755 if (x != NULL) {
756 X509_free(x);
757 }
758
Adam Langleya307dfd2015-01-09 15:42:58 -0800759 (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
Adam Langleyfcf25832014-12-18 17:42:32 -0800760
761 return ret;
762}
763
764/* Add a directory of certs to a stack.
765 *
Adam Langley95c29f32014-06-20 12:00:00 -0700766 * \param stack the stack to append to.
767 * \param dir the directory to append from. All files in this directory will be
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 * examined as potential certs. Any that are acceptable to
769 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
770 * be included.
Adam Langley95c29f32014-06-20 12:00:00 -0700771 * \return 1 for success, 0 for failure. Note that in the case of failure some
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 * certs may have been added to \c stack. */
773int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
774 const char *dir) {
775 OPENSSL_DIR_CTX *d = NULL;
776 const char *filename;
777 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 /* Note that a side effect is that the CAs will be sorted by name */
782 while ((filename = OPENSSL_DIR_read(&d, dir))) {
783 char buf[1024];
784 int r;
Adam Langley95c29f32014-06-20 12:00:00 -0700785
Adam Langleyfcf25832014-12-18 17:42:32 -0800786 if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
787 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack,
788 SSL_R_PATH_TOO_LONG);
789 goto err;
790 }
Adam Langley95c29f32014-06-20 12:00:00 -0700791
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
793 if (r <= 0 || r >= (int)sizeof(buf) ||
794 !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
795 goto err;
796 }
797 }
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799 if (errno) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100800 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, ERR_R_SYS_LIB);
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
802 goto err;
803 }
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700806
807err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 if (d) {
809 OPENSSL_DIR_end(&d);
810 }
811 CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
812 return ret;
813}
Adam Langley95c29f32014-06-20 12:00:00 -0700814
815/* Add a certificate to a BUF_MEM structure */
Adam Langleyfcf25832014-12-18 17:42:32 -0800816static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
817 int n;
818 uint8_t *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700819
Adam Langleyfcf25832014-12-18 17:42:32 -0800820 n = i2d_X509(x, NULL);
821 if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
822 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
823 return 0;
824 }
825 p = (uint8_t *)&(buf->data[*l]);
826 l2n3(n, p);
827 i2d_X509(x, &p);
828 *l += n + 3;
Adam Langley95c29f32014-06-20 12:00:00 -0700829
Adam Langleyfcf25832014-12-18 17:42:32 -0800830 return 1;
831}
Adam Langley95c29f32014-06-20 12:00:00 -0700832
Adam Langley24819752014-12-15 18:42:07 -0800833/* Add certificate chain to internal SSL BUF_MEM structure. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800834int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) {
835 BUF_MEM *buf = s->init_buf;
836 int no_chain = 0;
837 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 X509 *x = NULL;
840 STACK_OF(X509) * extra_certs;
841 X509_STORE *chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -0700842
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 if (cpk) {
844 x = cpk->x509;
845 }
Adam Langley95c29f32014-06-20 12:00:00 -0700846
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 if (s->cert->chain_store) {
848 chain_store = s->cert->chain_store;
849 } else {
850 chain_store = s->ctx->cert_store;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 /* If we have a certificate specific chain use it, else use parent ctx. */
854 if (cpk && cpk->chain) {
855 extra_certs = cpk->chain;
856 } else {
857 extra_certs = s->ctx->extra_certs;
858 }
Adam Langley95c29f32014-06-20 12:00:00 -0700859
Adam Langleyfcf25832014-12-18 17:42:32 -0800860 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) {
861 no_chain = 1;
862 }
Adam Langley95c29f32014-06-20 12:00:00 -0700863
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 /* TLSv1 sends a chain with nothing in it, instead of an alert. */
865 if (!BUF_MEM_grow_clean(buf, 10)) {
866 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
867 return 0;
868 }
Adam Langley95c29f32014-06-20 12:00:00 -0700869
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 if (x != NULL) {
871 if (no_chain) {
872 if (!ssl_add_cert_to_buf(buf, l, x)) {
873 return 0;
874 }
875 } else {
876 X509_STORE_CTX xs_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
879 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
880 return 0;
881 }
882 X509_verify_cert(&xs_ctx);
883 /* Don't leave errors in the queue */
884 ERR_clear_error();
885 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
886 x = sk_X509_value(xs_ctx.chain, i);
Adam Langley95c29f32014-06-20 12:00:00 -0700887
Adam Langleyfcf25832014-12-18 17:42:32 -0800888 if (!ssl_add_cert_to_buf(buf, l, x)) {
889 X509_STORE_CTX_cleanup(&xs_ctx);
890 return 0;
891 }
892 }
893 X509_STORE_CTX_cleanup(&xs_ctx);
894 }
895 }
896
897 for (i = 0; i < sk_X509_num(extra_certs); i++) {
898 x = sk_X509_value(extra_certs, i);
899 if (!ssl_add_cert_to_buf(buf, l, x)) {
900 return 0;
901 }
902 }
903
904 return 1;
905}
Adam Langley95c29f32014-06-20 12:00:00 -0700906
907/* Build a certificate chain for current certificate */
Adam Langleyfcf25832014-12-18 17:42:32 -0800908int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) {
909 CERT_PKEY *cpk = c->key;
910 X509_STORE_CTX xs_ctx;
911 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
912 X509 *x;
913 int i, rv = 0;
914 unsigned long error;
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 if (!cpk->x509) {
917 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
918 goto err;
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 /* Rearranging and check the chain: add everything to a store */
922 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
923 size_t j;
924 chain_store = X509_STORE_new();
925 if (!chain_store) {
926 goto err;
927 }
Adam Langley95c29f32014-06-20 12:00:00 -0700928
Adam Langleyfcf25832014-12-18 17:42:32 -0800929 for (j = 0; j < sk_X509_num(cpk->chain); j++) {
930 x = sk_X509_value(cpk->chain, j);
931 if (!X509_STORE_add_cert(chain_store, x)) {
932 error = ERR_peek_last_error();
933 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
934 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
935 goto err;
936 }
937 ERR_clear_error();
938 }
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 /* Add EE cert too: it might be self signed */
942 if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
943 error = ERR_peek_last_error();
944 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
945 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
946 goto err;
947 }
948 ERR_clear_error();
949 }
950 } else {
951 if (c->chain_store) {
952 chain_store = c->chain_store;
953 }
Adam Langleyf669c2d2014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) {
956 untrusted = cpk->chain;
957 }
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
961 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
962 goto err;
963 }
Adam Langley95c29f32014-06-20 12:00:00 -0700964
Adam Langleyfcf25832014-12-18 17:42:32 -0800965 i = X509_verify_cert(&xs_ctx);
966 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
967 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) {
968 ERR_clear_error();
969 }
970 i = 1;
971 rv = 2;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 if (i > 0) {
975 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
976 }
977 if (i <= 0) {
978 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain,
979 SSL_R_CERTIFICATE_VERIFY_FAILED);
980 i = X509_STORE_CTX_get_error(&xs_ctx);
981 ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i));
982
983 X509_STORE_CTX_cleanup(&xs_ctx);
984 goto err;
985 }
986
987 X509_STORE_CTX_cleanup(&xs_ctx);
988 if (cpk->chain) {
989 sk_X509_pop_free(cpk->chain, X509_free);
990 }
991
992 /* Remove EE certificate from chain */
993 x = sk_X509_shift(chain);
994 X509_free(x);
995 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
996 if (sk_X509_num(chain) > 0) {
997 /* See if last cert is self signed */
998 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
999 X509_check_purpose(x, -1, 0);
1000 if (x->ex_flags & EXFLAG_SS) {
1001 x = sk_X509_pop(chain);
1002 X509_free(x);
1003 }
1004 }
1005 }
1006
1007 cpk->chain = chain;
David Benjamin6eb000d2015-02-11 01:17:41 -05001008 if (rv == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 rv = 1;
David Benjamin6eb000d2015-02-11 01:17:41 -05001010 }
Adam Langleyfcf25832014-12-18 17:42:32 -08001011
1012err:
1013 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
1014 X509_STORE_free(chain_store);
1015 }
1016
1017 return rv;
1018}
1019
1020int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
1021 X509_STORE **pstore;
1022 if (chain) {
1023 pstore = &c->chain_store;
1024 } else {
1025 pstore = &c->verify_store;
1026 }
1027
1028 if (*pstore) {
1029 X509_STORE_free(*pstore);
1030 }
1031 *pstore = store;
1032
1033 if (ref && store) {
1034 CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
1035 }
1036 return 1;
1037}