blob: 12ad094e9104c0faa5bad6fa7bdc8c58c59510d5 [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>
119#include <openssl/dh.h>
120#include <openssl/err.h>
121#include <openssl/mem.h>
122#include <openssl/obj.h>
123#include <openssl/pem.h>
124#include <openssl/x509v3.h>
125
126#include "../crypto/dh/internal.h"
127#include "../crypto/directory.h"
128#include "ssl_locl.h"
129
130int SSL_get_ex_data_X509_STORE_CTX_idx(void)
131 {
132 static volatile int ssl_x509_store_ctx_idx= -1;
133 int got_write_lock = 0;
134
135 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
136
137 if (ssl_x509_store_ctx_idx < 0)
138 {
139 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
140 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
141 got_write_lock = 1;
142
143 if (ssl_x509_store_ctx_idx < 0)
144 {
145 ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
146 0,"SSL for verify callback",NULL,NULL,NULL);
147 }
148 }
149
150 if (got_write_lock)
151 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
152 else
153 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
154
155 return ssl_x509_store_ctx_idx;
156 }
157
158void ssl_cert_set_default_md(CERT *cert)
159 {
160 /* Set digest values to defaults */
161#ifndef OPENSSL_NO_DSA
162 cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
163#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700164 cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
165 cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
Adam Langley95c29f32014-06-20 12:00:00 -0700166#ifndef OPENSSL_NO_ECDSA
167 cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
168#endif
169 }
170
171CERT *ssl_cert_new(void)
172 {
173 CERT *ret;
174
175 ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
176 if (ret == NULL)
177 {
178 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
179 return(NULL);
180 }
181 memset(ret,0,sizeof(CERT));
182
183 ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
184 ret->references=1;
185 ssl_cert_set_default_md(ret);
186 return(ret);
187 }
188
189CERT *ssl_cert_dup(CERT *cert)
190 {
191 CERT *ret;
192 int i;
193
194 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
195 if (ret == NULL)
196 {
197 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
198 return(NULL);
199 }
200
201 memset(ret, 0, sizeof(CERT));
202
203 ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
204 /* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
205 * if you find that more readable */
206
207 ret->valid = cert->valid;
208 ret->mask_k = cert->mask_k;
209 ret->mask_a = cert->mask_a;
210 ret->export_mask_k = cert->export_mask_k;
211 ret->export_mask_a = cert->export_mask_a;
212
Adam Langley95c29f32014-06-20 12:00:00 -0700213 if (cert->rsa_tmp != NULL)
214 {
215 RSA_up_ref(cert->rsa_tmp);
216 ret->rsa_tmp = cert->rsa_tmp;
217 }
218 ret->rsa_tmp_cb = cert->rsa_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700219
220#ifndef OPENSSL_NO_DH
221 if (cert->dh_tmp != NULL)
222 {
223 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
224 if (ret->dh_tmp == NULL)
225 {
226 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
227 goto err;
228 }
229 if (cert->dh_tmp->priv_key)
230 {
231 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
232 if (!b)
233 {
234 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
235 goto err;
236 }
237 ret->dh_tmp->priv_key = b;
238 }
239 if (cert->dh_tmp->pub_key)
240 {
241 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
242 if (!b)
243 {
244 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
245 goto err;
246 }
247 ret->dh_tmp->pub_key = b;
248 }
249 }
250 ret->dh_tmp_cb = cert->dh_tmp_cb;
251#endif
252
253#ifndef OPENSSL_NO_ECDH
254 if (cert->ecdh_tmp)
255 {
256 ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
257 if (ret->ecdh_tmp == NULL)
258 {
259 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB);
260 goto err;
261 }
262 }
263 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
264 ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
265#endif
266
267 for (i = 0; i < SSL_PKEY_NUM; i++)
268 {
269 CERT_PKEY *cpk = cert->pkeys + i;
270 CERT_PKEY *rpk = ret->pkeys + i;
271 if (cpk->x509 != NULL)
272 {
273 rpk->x509 = cpk->x509;
274 CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509);
275 }
276
277 if (cpk->privatekey != NULL)
278 {
279 rpk->privatekey = cpk->privatekey;
280 CRYPTO_add(&cpk->privatekey->references, 1,
281 CRYPTO_LOCK_EVP_PKEY);
282
283 switch(i)
284 {
285 /* If there was anything special to do for
286 * certain types of keys, we'd do it here.
287 * (Nothing at the moment, I think.) */
288
289 case SSL_PKEY_RSA_ENC:
290 case SSL_PKEY_RSA_SIGN:
291 /* We have an RSA key. */
292 break;
293
294 case SSL_PKEY_DSA_SIGN:
295 /* We have a DSA key. */
296 break;
297
298 case SSL_PKEY_DH_RSA:
299 case SSL_PKEY_DH_DSA:
300 /* We have a DH key. */
301 break;
302
303 case SSL_PKEY_ECC:
304 /* We have an ECC key */
305 break;
306
307 default:
308 /* Can't happen. */
309 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, SSL_R_LIBRARY_BUG);
310 }
311 }
312
313 if (cpk->chain)
314 {
315 rpk->chain = X509_chain_up_ref(cpk->chain);
316 if (!rpk->chain)
317 {
318 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
319 goto err;
320 }
321 }
322 rpk->valid_flags = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700323 }
324
325 ret->references=1;
326 /* Set digests to defaults. NB: we don't copy existing values as they
327 * will be set during handshake.
328 */
329 ssl_cert_set_default_md(ret);
330 /* Peer sigalgs set to NULL as we get these from handshake too */
331 ret->peer_sigalgs = NULL;
332 ret->peer_sigalgslen = 0;
333 /* Configured sigalgs however we copy across */
334
335 if (cert->conf_sigalgs)
336 {
337 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
338 if (!ret->conf_sigalgs)
339 goto err;
340 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
341 cert->conf_sigalgslen);
342 ret->conf_sigalgslen = cert->conf_sigalgslen;
343 }
344 else
345 ret->conf_sigalgs = NULL;
346
347 if (cert->client_sigalgs)
348 {
349 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
350 if (!ret->client_sigalgs)
351 goto err;
352 memcpy(ret->client_sigalgs, cert->client_sigalgs,
353 cert->client_sigalgslen);
354 ret->client_sigalgslen = cert->client_sigalgslen;
355 }
356 else
357 ret->client_sigalgs = NULL;
358 /* Shared sigalgs also NULL */
359 ret->shared_sigalgs = NULL;
360 /* Copy any custom client certificate types */
361 if (cert->ctypes)
362 {
363 ret->ctypes = OPENSSL_malloc(cert->ctype_num);
364 if (!ret->ctypes)
365 goto err;
366 memcpy(ret->ctypes, cert->ctypes, cert->ctype_num);
367 ret->ctype_num = cert->ctype_num;
368 }
369
370 ret->cert_flags = cert->cert_flags;
371
372 ret->cert_cb = cert->cert_cb;
373 ret->cert_cb_arg = cert->cert_cb_arg;
374
375 if (cert->verify_store)
376 {
377 CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
378 ret->verify_store = cert->verify_store;
379 }
380
381 if (cert->chain_store)
382 {
383 CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
384 ret->chain_store = cert->chain_store;
385 }
386
387 ret->ciphers_raw = NULL;
388
389 return(ret);
390
391#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH)
392err:
393#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700394 if (ret->rsa_tmp != NULL)
395 RSA_free(ret->rsa_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700396#ifndef OPENSSL_NO_DH
397 if (ret->dh_tmp != NULL)
398 DH_free(ret->dh_tmp);
399#endif
400#ifndef OPENSSL_NO_ECDH
401 if (ret->ecdh_tmp != NULL)
402 EC_KEY_free(ret->ecdh_tmp);
403#endif
404
405 ssl_cert_clear_certs(ret);
406
407 return NULL;
408 }
409
410/* Free up and clear all certificates and chains */
411
412void ssl_cert_clear_certs(CERT *c)
413 {
414 int i;
415 if (c == NULL)
416 return;
417 for (i = 0; i<SSL_PKEY_NUM; i++)
418 {
419 CERT_PKEY *cpk = c->pkeys + i;
420 if (cpk->x509)
421 {
422 X509_free(cpk->x509);
423 cpk->x509 = NULL;
424 }
425 if (cpk->privatekey)
426 {
427 EVP_PKEY_free(cpk->privatekey);
428 cpk->privatekey = NULL;
429 }
430 if (cpk->chain)
431 {
432 sk_X509_pop_free(cpk->chain, X509_free);
433 cpk->chain = NULL;
434 }
Adam Langley95c29f32014-06-20 12:00:00 -0700435 /* Clear all flags apart from explicit sign */
436 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
437 }
438 }
439
440void ssl_cert_free(CERT *c)
441 {
442 int i;
443
444 if(c == NULL)
445 return;
446
447 i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT);
448#ifdef REF_PRINT
449 REF_PRINT("CERT",c);
450#endif
451 if (i > 0) return;
452#ifdef REF_CHECK
453 if (i < 0)
454 {
455 fprintf(stderr,"ssl_cert_free, bad reference count\n");
456 abort(); /* ok */
457 }
458#endif
459
Adam Langley95c29f32014-06-20 12:00:00 -0700460 if (c->rsa_tmp) RSA_free(c->rsa_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700461#ifndef OPENSSL_NO_DH
462 if (c->dh_tmp) DH_free(c->dh_tmp);
463#endif
464#ifndef OPENSSL_NO_ECDH
465 if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
466#endif
467
468 ssl_cert_clear_certs(c);
469 if (c->peer_sigalgs)
470 OPENSSL_free(c->peer_sigalgs);
471 if (c->conf_sigalgs)
472 OPENSSL_free(c->conf_sigalgs);
473 if (c->client_sigalgs)
474 OPENSSL_free(c->client_sigalgs);
475 if (c->shared_sigalgs)
476 OPENSSL_free(c->shared_sigalgs);
477 if (c->ctypes)
478 OPENSSL_free(c->ctypes);
479 if (c->verify_store)
480 X509_STORE_free(c->verify_store);
481 if (c->chain_store)
482 X509_STORE_free(c->chain_store);
483 if (c->ciphers_raw)
484 OPENSSL_free(c->ciphers_raw);
485 OPENSSL_free(c);
486 }
487
488int ssl_cert_inst(CERT **o)
489 {
490 /* Create a CERT if there isn't already one
491 * (which cannot really happen, as it is initially created in
492 * SSL_CTX_new; but the earlier code usually allows for that one
493 * being non-existant, so we follow that behaviour, as it might
494 * turn out that there actually is a reason for it -- but I'm
495 * not sure that *all* of the existing code could cope with
496 * s->cert being NULL, otherwise we could do without the
497 * initialization in SSL_CTX_new).
498 */
499
500 if (o == NULL)
501 {
502 OPENSSL_PUT_ERROR(SSL, ssl_cert_inst, ERR_R_PASSED_NULL_PARAMETER);
503 return(0);
504 }
505 if (*o == NULL)
506 {
507 if ((*o = ssl_cert_new()) == NULL)
508 {
509 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
510 return(0);
511 }
512 }
513 return(1);
514 }
515
516int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
517 {
518 CERT_PKEY *cpk = c->key;
519 if (!cpk)
520 return 0;
521 if (cpk->chain)
522 sk_X509_pop_free(cpk->chain, X509_free);
523 cpk->chain = chain;
524 return 1;
525 }
526
527int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
528 {
529 STACK_OF(X509) *dchain;
530 if (!chain)
531 return ssl_cert_set0_chain(c, NULL);
532 dchain = X509_chain_up_ref(chain);
533 if (!dchain)
534 return 0;
535 if (!ssl_cert_set0_chain(c, dchain))
536 {
537 sk_X509_pop_free(dchain, X509_free);
538 return 0;
539 }
540 return 1;
541 }
542
543int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
544 {
545 CERT_PKEY *cpk = c->key;
546 if (!cpk)
547 return 0;
548 if (!cpk->chain)
549 cpk->chain = sk_X509_new_null();
550 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
551 return 0;
552 return 1;
553 }
554
555int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
556 {
557 if (!ssl_cert_add0_chain_cert(c, x))
558 return 0;
559 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
560 return 1;
561 }
562
563int ssl_cert_select_current(CERT *c, X509 *x)
564 {
565 int i;
566 if (x == NULL)
567 return 0;
568 for (i = 0; i < SSL_PKEY_NUM; i++)
569 {
570 if (c->pkeys[i].x509 == x)
571 {
572 c->key = &c->pkeys[i];
573 return 1;
574 }
575 }
576
577 for (i = 0; i < SSL_PKEY_NUM; i++)
578 {
579 if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x))
580 {
581 c->key = &c->pkeys[i];
582 return 1;
583 }
584 }
585 return 0;
586 }
587
588void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg)
589 {
590 c->cert_cb = cb;
591 c->cert_cb_arg = arg;
592 }
593
594SESS_CERT *ssl_sess_cert_new(void)
595 {
596 SESS_CERT *ret;
597
598 ret = OPENSSL_malloc(sizeof *ret);
599 if (ret == NULL)
600 {
601 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
602 return NULL;
603 }
604
605 memset(ret, 0 ,sizeof *ret);
606 ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
607 ret->references = 1;
608
609 return ret;
610 }
611
612void ssl_sess_cert_free(SESS_CERT *sc)
613 {
614 int i;
615
616 if (sc == NULL)
617 return;
618
619 i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
620#ifdef REF_PRINT
621 REF_PRINT("SESS_CERT", sc);
622#endif
623 if (i > 0)
624 return;
625#ifdef REF_CHECK
626 if (i < 0)
627 {
628 fprintf(stderr,"ssl_sess_cert_free, bad reference count\n");
629 abort(); /* ok */
630 }
631#endif
632
633 /* i == 0 */
634 if (sc->cert_chain != NULL)
635 sk_X509_pop_free(sc->cert_chain, X509_free);
636 for (i = 0; i < SSL_PKEY_NUM; i++)
637 {
638 if (sc->peer_pkeys[i].x509 != NULL)
639 X509_free(sc->peer_pkeys[i].x509);
640#if 0 /* We don't have the peer's private key. These lines are just
641 * here as a reminder that we're still using a not-quite-appropriate
642 * data structure. */
643 if (sc->peer_pkeys[i].privatekey != NULL)
644 EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
645#endif
646 }
647
Adam Langley95c29f32014-06-20 12:00:00 -0700648 if (sc->peer_rsa_tmp != NULL)
649 RSA_free(sc->peer_rsa_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700650#ifndef OPENSSL_NO_DH
651 if (sc->peer_dh_tmp != NULL)
652 DH_free(sc->peer_dh_tmp);
653#endif
654#ifndef OPENSSL_NO_ECDH
655 if (sc->peer_ecdh_tmp != NULL)
656 EC_KEY_free(sc->peer_ecdh_tmp);
657#endif
658
659 OPENSSL_free(sc);
660 }
661
662int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
663 {
664 sc->peer_cert_type = type;
665 return(1);
666 }
667
Adam Langley95c29f32014-06-20 12:00:00 -0700668int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
669 {
670 X509 *x;
671 int i;
672 X509_STORE *verify_store;
673 X509_STORE_CTX ctx;
674
675 if (s->cert->verify_store)
676 verify_store = s->cert->verify_store;
677 else
678 verify_store = s->ctx->cert_store;
679
680 if ((sk == NULL) || (sk_X509_num(sk) == 0))
681 return(0);
682
683 x=sk_X509_value(sk,0);
684 if(!X509_STORE_CTX_init(&ctx,verify_store,x,sk))
685 {
686 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
687 return(0);
688 }
689 /* Set suite B flags if needed */
690 X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s));
691#if 0
692 if (SSL_get_verify_depth(s) >= 0)
693 X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
694#endif
695 X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
696
697 /* We need to inherit the verify parameters. These can be determined by
698 * the context: if its a server it will verify SSL client certificates
699 * or vice versa.
700 */
701
702 X509_STORE_CTX_set_default(&ctx,
703 s->server ? "ssl_client" : "ssl_server");
704 /* Anything non-default in "param" should overwrite anything in the
705 * ctx.
706 */
707 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
708
Adam Langley95c29f32014-06-20 12:00:00 -0700709 if (s->verify_callback)
710 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
Adam Langley95c29f32014-06-20 12:00:00 -0700711
712 if (s->ctx->app_verify_callback != NULL)
713#if 1 /* new with OpenSSL 0.9.7 */
714 i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
715#else
716 i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
717#endif
718 else
719 {
720#ifndef OPENSSL_NO_X509_VERIFY
721 i=X509_verify_cert(&ctx);
722#else
723 i=0;
724 ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
725 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, SSL_R_NO_VERIFY_CALLBACK);
726#endif
727 }
728
729 s->verify_result=ctx.error;
730 X509_STORE_CTX_cleanup(&ctx);
731
732 return(i);
733 }
734
735static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
736 {
737 if (*ca_list != NULL)
738 sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
739
740 *ca_list=name_list;
741 }
742
743STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
744 {
745 int i;
746 STACK_OF(X509_NAME) *ret;
747 X509_NAME *name;
748
749 ret=sk_X509_NAME_new_null();
750 for (i=0; i<sk_X509_NAME_num(sk); i++)
751 {
752 name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
753 if ((name == NULL) || !sk_X509_NAME_push(ret,name))
754 {
755 sk_X509_NAME_pop_free(ret,X509_NAME_free);
756 return(NULL);
757 }
758 }
759 return(ret);
760 }
761
762void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
763 {
764 set_client_CA_list(&(s->client_CA),name_list);
765 }
766
767void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
768 {
769 set_client_CA_list(&(ctx->client_CA),name_list);
770 }
771
772STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
773 {
774 return(ctx->client_CA);
775 }
776
777STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
778 {
779 if (s->type == SSL_ST_CONNECT)
780 { /* we are in the client */
781 if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
782 (s->s3 != NULL))
783 return(s->s3->tmp.ca_names);
784 else
785 return(NULL);
786 }
787 else
788 {
789 if (s->client_CA != NULL)
790 return(s->client_CA);
791 else
792 return(s->ctx->client_CA);
793 }
794 }
795
796static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
797 {
798 X509_NAME *name;
799
800 if (x == NULL) return(0);
801 if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
802 return(0);
803
804 if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
805 return(0);
806
807 if (!sk_X509_NAME_push(*sk,name))
808 {
809 X509_NAME_free(name);
810 return(0);
811 }
812 return(1);
813 }
814
815int SSL_add_client_CA(SSL *ssl,X509 *x)
816 {
817 return(add_client_CA(&(ssl->client_CA),x));
818 }
819
820int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
821 {
822 return(add_client_CA(&(ctx->client_CA),x));
823 }
824
Adam Langley28acbbc2014-06-20 12:00:00 -0700825void SSL_get_client_certificate_types(const SSL *s, const unsigned char **ctype,
826 size_t *ctype_num)
827 {
David Benjamin5c57c602014-06-21 12:52:18 -0400828 /* TODO(fork): Remove this function once Chromium is updated
829 * to use the new one. */
830 *ctype_num = SSL_get0_certificate_types((SSL*)s, ctype);
Adam Langley28acbbc2014-06-20 12:00:00 -0700831 }
832
Adam Langley95c29f32014-06-20 12:00:00 -0700833static int xname_cmp(const X509_NAME **a, const X509_NAME **b)
834 {
835 return(X509_NAME_cmp(*a,*b));
836 }
837
838#ifndef OPENSSL_NO_STDIO
839/*!
840 * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
841 * it doesn't really have anything to do with clients (except that a common use
842 * for a stack of CAs is to send it to the client). Actually, it doesn't have
843 * much to do with CAs, either, since it will load any old cert.
844 * \param file the file containing one or more certs.
845 * \return a ::STACK containing the certs.
846 */
847STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
848 {
849 BIO *in;
850 X509 *x=NULL;
851 X509_NAME *xn=NULL;
852 STACK_OF(X509_NAME) *ret = NULL,*sk;
853
854 sk=sk_X509_NAME_new(xname_cmp);
855
856 in=BIO_new(BIO_s_file());
857
858 if ((sk == NULL) || (in == NULL))
859 {
860 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
861 goto err;
862 }
863
864 if (!BIO_read_filename(in,file))
865 goto err;
866
867 for (;;)
868 {
869 if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
870 break;
871 if (ret == NULL)
872 {
873 ret = sk_X509_NAME_new_null();
874 if (ret == NULL)
875 {
876 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
877 goto err;
878 }
879 }
880 if ((xn=X509_get_subject_name(x)) == NULL) goto err;
881 /* check for duplicates */
882 xn=X509_NAME_dup(xn);
883 if (xn == NULL) goto err;
884 if (sk_X509_NAME_find(sk, NULL, xn))
885 X509_NAME_free(xn);
886 else
887 {
888 sk_X509_NAME_push(sk,xn);
889 sk_X509_NAME_push(ret,xn);
890 }
891 }
892
893 if (0)
894 {
895err:
896 if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
897 ret=NULL;
898 }
899 if (sk != NULL) sk_X509_NAME_free(sk);
900 if (in != NULL) BIO_free(in);
901 if (x != NULL) X509_free(x);
902 if (ret != NULL)
903 ERR_clear_error();
904 return(ret);
905 }
906#endif
907
908/*!
909 * Add a file of certs to a stack.
910 * \param stack the stack to add to.
911 * \param file the file to add from. All certs in this file that are not
912 * already in the stack will be added.
913 * \return 1 for success, 0 for failure. Note that in the case of failure some
914 * certs may have been added to \c stack.
915 */
916
917int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
918 const char *file)
919 {
920 BIO *in;
921 X509 *x=NULL;
922 X509_NAME *xn=NULL;
923 int ret=1;
924 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
925
926 oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
927
928 in=BIO_new(BIO_s_file());
929
930 if (in == NULL)
931 {
932 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_MALLOC_FAILURE);
933 goto err;
934 }
935
936 if (!BIO_read_filename(in,file))
937 goto err;
938
939 for (;;)
940 {
941 if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
942 break;
943 if ((xn=X509_get_subject_name(x)) == NULL) goto err;
944 xn=X509_NAME_dup(xn);
945 if (xn == NULL) goto err;
946 if (sk_X509_NAME_find(stack, NULL, xn))
947 X509_NAME_free(xn);
948 else
949 sk_X509_NAME_push(stack,xn);
950 }
951
952 ERR_clear_error();
953
954 if (0)
955 {
956err:
957 ret=0;
958 }
959 if(in != NULL)
960 BIO_free(in);
961 if(x != NULL)
962 X509_free(x);
963
964 (void)sk_X509_NAME_set_cmp_func(stack,oldcmp);
965
966 return ret;
967 }
968
969/*!
970 * Add a directory of certs to a stack.
971 * \param stack the stack to append to.
972 * \param dir the directory to append from. All files in this directory will be
973 * examined as potential certs. Any that are acceptable to
974 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
975 * included.
976 * \return 1 for success, 0 for failure. Note that in the case of failure some
977 * certs may have been added to \c stack.
978 */
979
980int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
981 const char *dir)
982 {
983 OPENSSL_DIR_CTX *d = NULL;
984 const char *filename;
985 int ret = 0;
986
987 CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
988
989 /* Note that a side effect is that the CAs will be sorted by name */
990
991 while((filename = OPENSSL_DIR_read(&d, dir)))
992 {
993 char buf[1024];
994 int r;
995
996 if(strlen(dir)+strlen(filename)+2 > sizeof buf)
997 {
998 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, SSL_R_PATH_TOO_LONG);
999 goto err;
1000 }
1001
1002#ifdef OPENSSL_SYS_VMS
1003 r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
1004#else
1005 r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
1006#endif
1007 if (r <= 0 || r >= (int)sizeof(buf))
1008 goto err;
1009 if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
1010 goto err;
1011 }
1012
1013 if (errno)
1014 {
1015 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_SYS_LIB);
1016 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
1017 goto err;
1018 }
1019
1020 ret = 1;
1021
1022err:
1023 if (d) OPENSSL_DIR_end(&d);
1024 CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
1025 return ret;
1026 }
1027
1028/* Add a certificate to a BUF_MEM structure */
1029
1030static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
1031 {
1032 int n;
1033 unsigned char *p;
1034
1035 n=i2d_X509(x,NULL);
1036 if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3)))
1037 {
1038 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
1039 return 0;
1040 }
1041 p=(unsigned char *)&(buf->data[*l]);
1042 l2n3(n,p);
1043 i2d_X509(x,&p);
1044 *l+=n+3;
1045
1046 return 1;
1047 }
1048
1049/* Add certificate chain to internal SSL BUF_MEM strcuture */
1050int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
1051 {
1052 BUF_MEM *buf = s->init_buf;
1053 int no_chain;
1054 int i;
1055
1056 X509 *x;
1057 STACK_OF(X509) *extra_certs;
1058 X509_STORE *chain_store;
1059
1060 if (cpk)
1061 x = cpk->x509;
1062 else
1063 x = NULL;
1064
1065 if (s->cert->chain_store)
1066 chain_store = s->cert->chain_store;
1067 else
1068 chain_store = s->ctx->cert_store;
1069
1070 /* If we have a certificate specific chain use it, else use
1071 * parent ctx.
1072 */
1073 if (cpk && cpk->chain)
1074 extra_certs = cpk->chain;
1075 else
1076 extra_certs = s->ctx->extra_certs;
1077
1078 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
1079 no_chain = 1;
1080 else
1081 no_chain = 0;
1082
1083 /* TLSv1 sends a chain with nothing in it, instead of an alert */
1084 if (!BUF_MEM_grow_clean(buf,10))
1085 {
1086 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
1087 return 0;
1088 }
1089 if (x != NULL)
1090 {
1091 if (no_chain)
1092 {
1093 if (!ssl_add_cert_to_buf(buf, l, x))
1094 return 0;
1095 }
1096 else
1097 {
1098 X509_STORE_CTX xs_ctx;
1099
1100 if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL))
1101 {
1102 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
1103 return(0);
1104 }
1105 X509_verify_cert(&xs_ctx);
1106 /* Don't leave errors in the queue */
1107 ERR_clear_error();
1108 for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
1109 {
1110 x = sk_X509_value(xs_ctx.chain, i);
1111
1112 if (!ssl_add_cert_to_buf(buf, l, x))
1113 {
1114 X509_STORE_CTX_cleanup(&xs_ctx);
1115 return 0;
1116 }
1117 }
1118 X509_STORE_CTX_cleanup(&xs_ctx);
1119 }
1120 }
1121 for (i=0; i<sk_X509_num(extra_certs); i++)
1122 {
1123 x=sk_X509_value(extra_certs,i);
1124 if (!ssl_add_cert_to_buf(buf, l, x))
1125 return 0;
1126 }
1127
1128 return 1;
1129 }
1130
1131/* Build a certificate chain for current certificate */
1132int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
1133 {
1134 CERT_PKEY *cpk = c->key;
1135 X509_STORE_CTX xs_ctx;
1136 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
1137 X509 *x;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001138 int i, rv = 0;
Adam Langleydff7b9e2014-06-20 12:00:00 -07001139 unsigned long error;
Adam Langley95c29f32014-06-20 12:00:00 -07001140
1141 if (!cpk->x509)
1142 {
1143 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001144 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001145 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001146 /* Rearranging and check the chain: add everything to a store */
1147 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1148 {
1149 chain_store = X509_STORE_new();
1150 if (!chain_store)
1151 goto err;
1152 for (i = 0; i < sk_X509_num(cpk->chain); i++)
1153 {
1154 x = sk_X509_value(cpk->chain, i);
1155 if (!X509_STORE_add_cert(chain_store, x))
Adam Langleydff7b9e2014-06-20 12:00:00 -07001156 {
1157 error = ERR_peek_last_error();
1158 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1159 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1160 goto err;
1161 ERR_clear_error();
1162 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001163 }
1164 /* Add EE cert too: it might be self signed */
1165 if (!X509_STORE_add_cert(chain_store, cpk->x509))
Adam Langleydff7b9e2014-06-20 12:00:00 -07001166 {
1167 error = ERR_peek_last_error();
1168 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1169 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1170 goto err;
1171 ERR_clear_error();
1172 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001173 }
1174 else
1175 {
1176 if (c->chain_store)
1177 chain_store = c->chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -07001178
Adam Langleyf669c2d2014-06-20 12:00:00 -07001179 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
1180 untrusted = cpk->chain;
1181 }
Adam Langley95c29f32014-06-20 12:00:00 -07001182
1183 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted))
1184 {
1185 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001186 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001187 }
1188 /* Set suite B flags if needed */
1189 X509_STORE_CTX_set_flags(&xs_ctx, c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
1190
1191 i = X509_verify_cert(&xs_ctx);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001192 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)
1193 {
Adam Langley64c22232014-06-20 12:00:00 -07001194 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
1195 ERR_clear_error();
Adam Langleyf669c2d2014-06-20 12:00:00 -07001196 i = 1;
Adam Langley64c22232014-06-20 12:00:00 -07001197 rv = 2;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001198 }
Adam Langley95c29f32014-06-20 12:00:00 -07001199 if (i > 0)
1200 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001201 if (i <= 0)
1202 {
1203 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_CERTIFICATE_VERIFY_FAILED);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001204 i = X509_STORE_CTX_get_error(&xs_ctx);
1205 ERR_add_error_data(2, "Verify error:",
1206 X509_verify_cert_error_string(i));
1207
1208 X509_STORE_CTX_cleanup(&xs_ctx);
1209 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001210 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001211 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001212 if (cpk->chain)
1213 sk_X509_pop_free(cpk->chain, X509_free);
1214 /* Remove EE certificate from chain */
1215 x = sk_X509_shift(chain);
1216 X509_free(x);
1217 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT)
1218 {
Adam Langleyf669c2d2014-06-20 12:00:00 -07001219 if (sk_X509_num(chain) > 0)
1220 {
1221 /* See if last cert is self signed */
1222 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1223 X509_check_purpose(x, -1, 0);
1224 if (x->ex_flags & EXFLAG_SS)
1225 {
1226 x = sk_X509_pop(chain);
1227 X509_free(x);
1228 }
1229 }
Adam Langley95c29f32014-06-20 12:00:00 -07001230 }
1231 cpk->chain = chain;
Adam Langley64c22232014-06-20 12:00:00 -07001232 if (rv == 0)
1233 rv = 1;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001234 err:
1235 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1236 X509_STORE_free(chain_store);
Adam Langley95c29f32014-06-20 12:00:00 -07001237
Adam Langleyf669c2d2014-06-20 12:00:00 -07001238 return rv;
Adam Langley95c29f32014-06-20 12:00:00 -07001239 }
1240
1241int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1242 {
1243 X509_STORE **pstore;
1244 if (chain)
1245 pstore = &c->chain_store;
1246 else
1247 pstore = &c->verify_store;
1248 if (*pstore)
1249 X509_STORE_free(*pstore);
1250 *pstore = store;
1251 if (ref && store)
1252 CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
1253 return 1;
1254 }
1255