1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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. */
117 #include <openssl/bio.h>
118 #include <openssl/bn.h>
119 #include <openssl/buf.h>
120 #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>
127 #include "../crypto/dh/internal.h"
128 #include "../crypto/directory.h"
129 #include "ssl_locl.h"
131 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
133 static volatile int ssl_x509_store_ctx_idx= -1;
134 int got_write_lock = 0;
136 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
138 if (ssl_x509_store_ctx_idx < 0)
140 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
141 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
144 if (ssl_x509_store_ctx_idx < 0)
146 ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
147 0,"SSL for verify callback",NULL,NULL,NULL);
152 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
154 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
156 return ssl_x509_store_ctx_idx;
159 void ssl_cert_set_default_md(CERT *cert)
161 /* Set digest values to defaults */
162 cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
163 cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
164 cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
167 CERT *ssl_cert_new(void)
171 ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
174 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
177 memset(ret,0,sizeof(CERT));
179 ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
180 ssl_cert_set_default_md(ret);
184 CERT *ssl_cert_dup(CERT *cert)
189 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
192 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
196 memset(ret, 0, sizeof(CERT));
198 ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
199 /* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
200 * if you find that more readable */
202 ret->valid = cert->valid;
203 ret->mask_k = cert->mask_k;
204 ret->mask_a = cert->mask_a;
206 if (cert->dh_tmp != NULL)
208 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
209 if (ret->dh_tmp == NULL)
211 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
214 if (cert->dh_tmp->priv_key)
216 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
219 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
222 ret->dh_tmp->priv_key = b;
224 if (cert->dh_tmp->pub_key)
226 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
229 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
232 ret->dh_tmp->pub_key = b;
235 ret->dh_tmp_cb = cert->dh_tmp_cb;
239 ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
240 if (ret->ecdh_tmp == NULL)
242 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB);
246 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
247 ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
249 for (i = 0; i < SSL_PKEY_NUM; i++)
251 CERT_PKEY *cpk = cert->pkeys + i;
252 CERT_PKEY *rpk = ret->pkeys + i;
253 if (cpk->x509 != NULL)
255 rpk->x509 = X509_up_ref(cpk->x509);
258 if (cpk->privatekey != NULL)
260 rpk->privatekey = cpk->privatekey;
261 CRYPTO_add(&cpk->privatekey->references, 1,
262 CRYPTO_LOCK_EVP_PKEY);
266 /* If there was anything special to do for
267 * certain types of keys, we'd do it here.
268 * (Nothing at the moment, I think.) */
270 case SSL_PKEY_RSA_ENC:
271 case SSL_PKEY_RSA_SIGN:
272 /* We have an RSA key. */
276 /* We have an ECC key */
281 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, SSL_R_LIBRARY_BUG);
287 rpk->chain = X509_chain_up_ref(cpk->chain);
290 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
294 rpk->valid_flags = 0;
297 /* Set digests to defaults. NB: we don't copy existing values as they
298 * will be set during handshake.
300 ssl_cert_set_default_md(ret);
301 /* Peer sigalgs set to NULL as we get these from handshake too */
302 ret->peer_sigalgs = NULL;
303 ret->peer_sigalgslen = 0;
304 /* Configured sigalgs however we copy across */
306 if (cert->conf_sigalgs)
308 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
309 if (!ret->conf_sigalgs)
311 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
312 cert->conf_sigalgslen);
313 ret->conf_sigalgslen = cert->conf_sigalgslen;
316 ret->conf_sigalgs = NULL;
318 if (cert->client_sigalgs)
320 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
321 if (!ret->client_sigalgs)
323 memcpy(ret->client_sigalgs, cert->client_sigalgs,
324 cert->client_sigalgslen);
325 ret->client_sigalgslen = cert->client_sigalgslen;
328 ret->client_sigalgs = NULL;
329 /* Shared sigalgs also NULL */
330 ret->shared_sigalgs = NULL;
331 /* Copy any custom client certificate types */
332 if (cert->client_certificate_types)
334 ret->client_certificate_types = BUF_memdup(
335 cert->client_certificate_types,
336 cert->num_client_certificate_types);
337 if (!ret->client_certificate_types)
339 ret->num_client_certificate_types = cert->num_client_certificate_types;
342 ret->cert_flags = cert->cert_flags;
344 ret->cert_cb = cert->cert_cb;
345 ret->cert_cb_arg = cert->cert_cb_arg;
347 if (cert->verify_store)
349 CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
350 ret->verify_store = cert->verify_store;
353 if (cert->chain_store)
355 CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
356 ret->chain_store = cert->chain_store;
359 ret->ciphers_raw = NULL;
364 if (ret->dh_tmp != NULL)
365 DH_free(ret->dh_tmp);
366 if (ret->ecdh_tmp != NULL)
367 EC_KEY_free(ret->ecdh_tmp);
369 ssl_cert_clear_certs(ret);
374 /* Free up and clear all certificates and chains */
376 void ssl_cert_clear_certs(CERT *c)
381 for (i = 0; i<SSL_PKEY_NUM; i++)
383 CERT_PKEY *cpk = c->pkeys + i;
386 X509_free(cpk->x509);
391 EVP_PKEY_free(cpk->privatekey);
392 cpk->privatekey = NULL;
396 sk_X509_pop_free(cpk->chain, X509_free);
399 /* Clear all flags apart from explicit sign */
400 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
404 void ssl_cert_free(CERT *c)
409 if (c->dh_tmp) DH_free(c->dh_tmp);
410 if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
412 ssl_cert_clear_certs(c);
414 OPENSSL_free(c->peer_sigalgs);
416 OPENSSL_free(c->conf_sigalgs);
417 if (c->client_sigalgs)
418 OPENSSL_free(c->client_sigalgs);
419 if (c->shared_sigalgs)
420 OPENSSL_free(c->shared_sigalgs);
421 if (c->client_certificate_types)
422 OPENSSL_free(c->client_certificate_types);
424 X509_STORE_free(c->verify_store);
426 X509_STORE_free(c->chain_store);
428 OPENSSL_free(c->ciphers_raw);
432 int ssl_cert_inst(CERT **o)
434 /* Create a CERT if there isn't already one
435 * (which cannot really happen, as it is initially created in
436 * SSL_CTX_new; but the earlier code usually allows for that one
437 * being non-existant, so we follow that behaviour, as it might
438 * turn out that there actually is a reason for it -- but I'm
439 * not sure that *all* of the existing code could cope with
440 * s->cert being NULL, otherwise we could do without the
441 * initialization in SSL_CTX_new).
446 OPENSSL_PUT_ERROR(SSL, ssl_cert_inst, ERR_R_PASSED_NULL_PARAMETER);
451 if ((*o = ssl_cert_new()) == NULL)
453 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
460 int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
462 CERT_PKEY *cpk = c->key;
466 sk_X509_pop_free(cpk->chain, X509_free);
471 int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
473 STACK_OF(X509) *dchain;
475 return ssl_cert_set0_chain(c, NULL);
476 dchain = X509_chain_up_ref(chain);
479 if (!ssl_cert_set0_chain(c, dchain))
481 sk_X509_pop_free(dchain, X509_free);
487 int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
489 CERT_PKEY *cpk = c->key;
493 cpk->chain = sk_X509_new_null();
494 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
499 int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
501 if (!ssl_cert_add0_chain_cert(c, x))
507 int ssl_cert_select_current(CERT *c, X509 *x)
512 for (i = 0; i < SSL_PKEY_NUM; i++)
514 if (c->pkeys[i].x509 == x)
516 c->key = &c->pkeys[i];
521 for (i = 0; i < SSL_PKEY_NUM; i++)
523 if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x))
525 c->key = &c->pkeys[i];
532 void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg)
535 c->cert_cb_arg = arg;
538 SESS_CERT *ssl_sess_cert_new(void)
542 ret = OPENSSL_malloc(sizeof *ret);
545 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
549 memset(ret, 0 ,sizeof *ret);
550 ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
555 void ssl_sess_cert_free(SESS_CERT *sc)
562 if (sc->cert_chain != NULL)
563 sk_X509_pop_free(sc->cert_chain, X509_free);
564 for (i = 0; i < SSL_PKEY_NUM; i++)
566 if (sc->peer_pkeys[i].x509 != NULL)
567 X509_free(sc->peer_pkeys[i].x509);
568 #if 0 /* We don't have the peer's private key. These lines are just
569 * here as a reminder that we're still using a not-quite-appropriate
571 if (sc->peer_pkeys[i].privatekey != NULL)
572 EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
576 if (sc->peer_rsa_tmp != NULL)
577 RSA_free(sc->peer_rsa_tmp);
578 if (sc->peer_dh_tmp != NULL)
579 DH_free(sc->peer_dh_tmp);
580 if (sc->peer_ecdh_tmp != NULL)
581 EC_KEY_free(sc->peer_ecdh_tmp);
586 int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
588 sc->peer_cert_type = type;
592 int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
596 X509_STORE *verify_store;
599 if (s->cert->verify_store)
600 verify_store = s->cert->verify_store;
602 verify_store = s->ctx->cert_store;
604 if ((sk == NULL) || (sk_X509_num(sk) == 0))
607 x=sk_X509_value(sk,0);
608 if(!X509_STORE_CTX_init(&ctx,verify_store,x,sk))
610 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
614 if (SSL_get_verify_depth(s) >= 0)
615 X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
617 X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
619 /* We need to inherit the verify parameters. These can be determined by
620 * the context: if its a server it will verify SSL client certificates
624 X509_STORE_CTX_set_default(&ctx,
625 s->server ? "ssl_client" : "ssl_server");
626 /* Anything non-default in "param" should overwrite anything in the
629 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
631 if (s->verify_callback)
632 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
634 if (s->ctx->app_verify_callback != NULL)
635 #if 1 /* new with OpenSSL 0.9.7 */
636 i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
638 i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
642 #ifndef OPENSSL_NO_X509_VERIFY
643 i=X509_verify_cert(&ctx);
646 ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
647 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, SSL_R_NO_VERIFY_CALLBACK);
651 s->verify_result=ctx.error;
652 X509_STORE_CTX_cleanup(&ctx);
657 static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
659 if (*ca_list != NULL)
660 sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
665 STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
668 STACK_OF(X509_NAME) *ret;
671 ret=sk_X509_NAME_new_null();
672 for (i=0; i<sk_X509_NAME_num(sk); i++)
674 name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
675 if ((name == NULL) || !sk_X509_NAME_push(ret,name))
677 sk_X509_NAME_pop_free(ret,X509_NAME_free);
684 void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
686 set_client_CA_list(&(s->client_CA),name_list);
689 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
691 set_client_CA_list(&(ctx->client_CA),name_list);
694 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
696 return(ctx->client_CA);
699 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
701 if (s->type == SSL_ST_CONNECT)
702 { /* we are in the client */
703 if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
705 return(s->s3->tmp.ca_names);
711 if (s->client_CA != NULL)
712 return(s->client_CA);
714 return(s->ctx->client_CA);
718 static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
722 if (x == NULL) return(0);
723 if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
726 if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
729 if (!sk_X509_NAME_push(*sk,name))
731 X509_NAME_free(name);
737 int SSL_add_client_CA(SSL *ssl,X509 *x)
739 return(add_client_CA(&(ssl->client_CA),x));
742 int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
744 return(add_client_CA(&(ctx->client_CA),x));
747 static int xname_cmp(const X509_NAME **a, const X509_NAME **b)
749 return(X509_NAME_cmp(*a,*b));
752 #ifndef OPENSSL_NO_STDIO
754 * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
755 * it doesn't really have anything to do with clients (except that a common use
756 * for a stack of CAs is to send it to the client). Actually, it doesn't have
757 * much to do with CAs, either, since it will load any old cert.
758 * \param file the file containing one or more certs.
759 * \return a ::STACK containing the certs.
761 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
766 STACK_OF(X509_NAME) *ret = NULL,*sk;
768 sk=sk_X509_NAME_new(xname_cmp);
770 in=BIO_new(BIO_s_file());
772 if ((sk == NULL) || (in == NULL))
774 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
778 if (!BIO_read_filename(in,file))
783 if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
787 ret = sk_X509_NAME_new_null();
790 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
794 if ((xn=X509_get_subject_name(x)) == NULL) goto err;
795 /* check for duplicates */
796 xn=X509_NAME_dup(xn);
797 if (xn == NULL) goto err;
798 if (sk_X509_NAME_find(sk, NULL, xn))
802 sk_X509_NAME_push(sk,xn);
803 sk_X509_NAME_push(ret,xn);
810 if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
813 if (sk != NULL) sk_X509_NAME_free(sk);
814 if (in != NULL) BIO_free(in);
815 if (x != NULL) X509_free(x);
823 * Add a file of certs to a stack.
824 * \param stack the stack to add to.
825 * \param file the file to add from. All certs in this file that are not
826 * already in the stack will be added.
827 * \return 1 for success, 0 for failure. Note that in the case of failure some
828 * certs may have been added to \c stack.
831 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
838 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
840 oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
842 in=BIO_new(BIO_s_file());
846 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_MALLOC_FAILURE);
850 if (!BIO_read_filename(in,file))
855 if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
857 if ((xn=X509_get_subject_name(x)) == NULL) goto err;
858 xn=X509_NAME_dup(xn);
859 if (xn == NULL) goto err;
860 if (sk_X509_NAME_find(stack, NULL, xn))
863 sk_X509_NAME_push(stack,xn);
878 (void)sk_X509_NAME_set_cmp_func(stack,oldcmp);
884 * Add a directory of certs to a stack.
885 * \param stack the stack to append to.
886 * \param dir the directory to append from. All files in this directory will be
887 * examined as potential certs. Any that are acceptable to
888 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
890 * \return 1 for success, 0 for failure. Note that in the case of failure some
891 * certs may have been added to \c stack.
894 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
897 OPENSSL_DIR_CTX *d = NULL;
898 const char *filename;
901 CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
903 /* Note that a side effect is that the CAs will be sorted by name */
905 while((filename = OPENSSL_DIR_read(&d, dir)))
910 if(strlen(dir)+strlen(filename)+2 > sizeof buf)
912 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, SSL_R_PATH_TOO_LONG);
916 #ifdef OPENSSL_SYS_VMS
917 r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
919 r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
921 if (r <= 0 || r >= (int)sizeof(buf))
923 if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
929 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_SYS_LIB);
930 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
937 if (d) OPENSSL_DIR_end(&d);
938 CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
942 /* Add a certificate to a BUF_MEM structure */
944 static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
950 if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3)))
952 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
955 p=(unsigned char *)&(buf->data[*l]);
963 /* Add certificate chain to internal SSL BUF_MEM strcuture */
964 int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
966 BUF_MEM *buf = s->init_buf;
971 STACK_OF(X509) *extra_certs;
972 X509_STORE *chain_store;
979 if (s->cert->chain_store)
980 chain_store = s->cert->chain_store;
982 chain_store = s->ctx->cert_store;
984 /* If we have a certificate specific chain use it, else use
987 if (cpk && cpk->chain)
988 extra_certs = cpk->chain;
990 extra_certs = s->ctx->extra_certs;
992 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
997 /* TLSv1 sends a chain with nothing in it, instead of an alert */
998 if (!BUF_MEM_grow_clean(buf,10))
1000 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
1007 if (!ssl_add_cert_to_buf(buf, l, x))
1012 X509_STORE_CTX xs_ctx;
1014 if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL))
1016 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
1019 X509_verify_cert(&xs_ctx);
1020 /* Don't leave errors in the queue */
1022 for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
1024 x = sk_X509_value(xs_ctx.chain, i);
1026 if (!ssl_add_cert_to_buf(buf, l, x))
1028 X509_STORE_CTX_cleanup(&xs_ctx);
1032 X509_STORE_CTX_cleanup(&xs_ctx);
1035 for (i=0; i<sk_X509_num(extra_certs); i++)
1037 x=sk_X509_value(extra_certs,i);
1038 if (!ssl_add_cert_to_buf(buf, l, x))
1045 /* Build a certificate chain for current certificate */
1046 int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
1048 CERT_PKEY *cpk = c->key;
1049 X509_STORE_CTX xs_ctx;
1050 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
1053 unsigned long error;
1057 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
1060 /* Rearranging and check the chain: add everything to a store */
1061 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1064 chain_store = X509_STORE_new();
1067 for (j = 0; j < sk_X509_num(cpk->chain); j++)
1069 x = sk_X509_value(cpk->chain, j);
1070 if (!X509_STORE_add_cert(chain_store, x))
1072 error = ERR_peek_last_error();
1073 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1074 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1079 /* Add EE cert too: it might be self signed */
1080 if (!X509_STORE_add_cert(chain_store, cpk->x509))
1082 error = ERR_peek_last_error();
1083 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1084 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1092 chain_store = c->chain_store;
1094 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
1095 untrusted = cpk->chain;
1098 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted))
1100 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
1104 i = X509_verify_cert(&xs_ctx);
1105 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)
1107 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
1113 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
1116 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_CERTIFICATE_VERIFY_FAILED);
1117 i = X509_STORE_CTX_get_error(&xs_ctx);
1118 ERR_add_error_data(2, "Verify error:",
1119 X509_verify_cert_error_string(i));
1121 X509_STORE_CTX_cleanup(&xs_ctx);
1124 X509_STORE_CTX_cleanup(&xs_ctx);
1126 sk_X509_pop_free(cpk->chain, X509_free);
1127 /* Remove EE certificate from chain */
1128 x = sk_X509_shift(chain);
1130 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT)
1132 if (sk_X509_num(chain) > 0)
1134 /* See if last cert is self signed */
1135 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1136 X509_check_purpose(x, -1, 0);
1137 if (x->ex_flags & EXFLAG_SS)
1139 x = sk_X509_pop(chain);
1148 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1149 X509_STORE_free(chain_store);
1154 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1156 X509_STORE **pstore;
1158 pstore = &c->chain_store;
1160 pstore = &c->verify_store;
1162 X509_STORE_free(*pstore);
1165 CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);