xref: /minix3/crypto/external/bsd/openssl/dist/ssl/ssl_cert.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*
2*0a6a1f1dSLionel Sambuc  * ! \file ssl/ssl_cert.c
3*0a6a1f1dSLionel Sambuc  */
4ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5ebfedea0SLionel Sambuc  * All rights reserved.
6ebfedea0SLionel Sambuc  *
7ebfedea0SLionel Sambuc  * This package is an SSL implementation written
8ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
9ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
10ebfedea0SLionel Sambuc  *
11ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
12ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
13ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
14ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
16ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17ebfedea0SLionel Sambuc  *
18ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
19ebfedea0SLionel Sambuc  * the code are not to be removed.
20ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
21ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
22ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
23ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
26ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
27ebfedea0SLionel Sambuc  * are met:
28ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
30ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
31ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
32ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
33ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
34ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
35ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
36ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
37ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
38ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
39ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
40ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
41ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42ebfedea0SLionel Sambuc  *
43ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53ebfedea0SLionel Sambuc  * SUCH DAMAGE.
54ebfedea0SLionel Sambuc  *
55ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
56ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57ebfedea0SLionel Sambuc  * copied and put under another distribution licence
58ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
59ebfedea0SLionel Sambuc  */
60ebfedea0SLionel Sambuc /* ====================================================================
61ebfedea0SLionel Sambuc  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
62ebfedea0SLionel Sambuc  *
63ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
64ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
65ebfedea0SLionel Sambuc  * are met:
66ebfedea0SLionel Sambuc  *
67ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
68ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
69ebfedea0SLionel Sambuc  *
70ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
71ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
72ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
73ebfedea0SLionel Sambuc  *    distribution.
74ebfedea0SLionel Sambuc  *
75ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
76ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
77ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
78ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79ebfedea0SLionel Sambuc  *
80ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
82ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
83ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
84ebfedea0SLionel Sambuc  *
85ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
86ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
87ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
88ebfedea0SLionel Sambuc  *
89ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
90ebfedea0SLionel Sambuc  *    acknowledgment:
91ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
92ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93ebfedea0SLionel Sambuc  *
94ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
106ebfedea0SLionel Sambuc  * ====================================================================
107ebfedea0SLionel Sambuc  *
108ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
109ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
110ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
111ebfedea0SLionel Sambuc  *
112ebfedea0SLionel Sambuc  */
113ebfedea0SLionel Sambuc /* ====================================================================
114ebfedea0SLionel Sambuc  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115ebfedea0SLionel Sambuc  * ECC cipher suite support in OpenSSL originally developed by
116ebfedea0SLionel Sambuc  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117ebfedea0SLionel Sambuc  */
118ebfedea0SLionel Sambuc 
119ebfedea0SLionel Sambuc #include <stdio.h>
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc #include "e_os.h"
122ebfedea0SLionel Sambuc #ifndef NO_SYS_TYPES_H
123ebfedea0SLionel Sambuc # include <sys/types.h>
124ebfedea0SLionel Sambuc #endif
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc #include "o_dir.h"
127ebfedea0SLionel Sambuc #include <openssl/objects.h>
128ebfedea0SLionel Sambuc #include <openssl/bio.h>
129ebfedea0SLionel Sambuc #include <openssl/pem.h>
130ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
131ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
132ebfedea0SLionel Sambuc # include <openssl/dh.h>
133ebfedea0SLionel Sambuc #endif
134ebfedea0SLionel Sambuc #include <openssl/bn.h>
135ebfedea0SLionel Sambuc #include "ssl_locl.h"
136ebfedea0SLionel Sambuc 
SSL_get_ex_data_X509_STORE_CTX_idx(void)137ebfedea0SLionel Sambuc int SSL_get_ex_data_X509_STORE_CTX_idx(void)
138ebfedea0SLionel Sambuc {
139ebfedea0SLionel Sambuc     static volatile int ssl_x509_store_ctx_idx = -1;
140ebfedea0SLionel Sambuc     int got_write_lock = 0;
141ebfedea0SLionel Sambuc 
142ebfedea0SLionel Sambuc     CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
143ebfedea0SLionel Sambuc 
144*0a6a1f1dSLionel Sambuc     if (ssl_x509_store_ctx_idx < 0) {
145ebfedea0SLionel Sambuc         CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
146ebfedea0SLionel Sambuc         CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
147ebfedea0SLionel Sambuc         got_write_lock = 1;
148ebfedea0SLionel Sambuc 
149*0a6a1f1dSLionel Sambuc         if (ssl_x509_store_ctx_idx < 0) {
150*0a6a1f1dSLionel Sambuc             ssl_x509_store_ctx_idx =
151*0a6a1f1dSLionel Sambuc                 X509_STORE_CTX_get_ex_new_index(0, "SSL for verify callback",
152*0a6a1f1dSLionel Sambuc                                                 NULL, NULL, NULL);
153ebfedea0SLionel Sambuc         }
154ebfedea0SLionel Sambuc     }
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc     if (got_write_lock)
157ebfedea0SLionel Sambuc         CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
158ebfedea0SLionel Sambuc     else
159ebfedea0SLionel Sambuc         CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
160ebfedea0SLionel Sambuc 
161ebfedea0SLionel Sambuc     return ssl_x509_store_ctx_idx;
162ebfedea0SLionel Sambuc }
163ebfedea0SLionel Sambuc 
ssl_cert_set_default_md(CERT * cert)164ebfedea0SLionel Sambuc static void ssl_cert_set_default_md(CERT *cert)
165ebfedea0SLionel Sambuc {
166ebfedea0SLionel Sambuc     /* Set digest values to defaults */
167ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
168ebfedea0SLionel Sambuc     cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
169ebfedea0SLionel Sambuc #endif
170ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
171ebfedea0SLionel Sambuc     cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
172ebfedea0SLionel Sambuc     cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
173ebfedea0SLionel Sambuc #endif
174ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA
175ebfedea0SLionel Sambuc     cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
176ebfedea0SLionel Sambuc #endif
177ebfedea0SLionel Sambuc }
178ebfedea0SLionel Sambuc 
ssl_cert_new(void)179ebfedea0SLionel Sambuc CERT *ssl_cert_new(void)
180ebfedea0SLionel Sambuc {
181ebfedea0SLionel Sambuc     CERT *ret;
182ebfedea0SLionel Sambuc 
183ebfedea0SLionel Sambuc     ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
184*0a6a1f1dSLionel Sambuc     if (ret == NULL) {
185ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
186ebfedea0SLionel Sambuc         return (NULL);
187ebfedea0SLionel Sambuc     }
188ebfedea0SLionel Sambuc     memset(ret, 0, sizeof(CERT));
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc     ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]);
191ebfedea0SLionel Sambuc     ret->references = 1;
192ebfedea0SLionel Sambuc     ssl_cert_set_default_md(ret);
193ebfedea0SLionel Sambuc     return (ret);
194ebfedea0SLionel Sambuc }
195ebfedea0SLionel Sambuc 
ssl_cert_dup(CERT * cert)196ebfedea0SLionel Sambuc CERT *ssl_cert_dup(CERT *cert)
197ebfedea0SLionel Sambuc {
198ebfedea0SLionel Sambuc     CERT *ret;
199ebfedea0SLionel Sambuc     int i;
200ebfedea0SLionel Sambuc 
201ebfedea0SLionel Sambuc     ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
202*0a6a1f1dSLionel Sambuc     if (ret == NULL) {
203ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
204ebfedea0SLionel Sambuc         return (NULL);
205ebfedea0SLionel Sambuc     }
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc     memset(ret, 0, sizeof(CERT));
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
210*0a6a1f1dSLionel Sambuc     /*
211*0a6a1f1dSLionel Sambuc      * or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
212*0a6a1f1dSLionel Sambuc      * more readable
213*0a6a1f1dSLionel Sambuc      */
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc     ret->valid = cert->valid;
216ebfedea0SLionel Sambuc     ret->mask_k = cert->mask_k;
217ebfedea0SLionel Sambuc     ret->mask_a = cert->mask_a;
218ebfedea0SLionel Sambuc     ret->export_mask_k = cert->export_mask_k;
219ebfedea0SLionel Sambuc     ret->export_mask_a = cert->export_mask_a;
220ebfedea0SLionel Sambuc 
221ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
222*0a6a1f1dSLionel Sambuc     if (cert->rsa_tmp != NULL) {
223ebfedea0SLionel Sambuc         RSA_up_ref(cert->rsa_tmp);
224ebfedea0SLionel Sambuc         ret->rsa_tmp = cert->rsa_tmp;
225ebfedea0SLionel Sambuc     }
226ebfedea0SLionel Sambuc     ret->rsa_tmp_cb = cert->rsa_tmp_cb;
227ebfedea0SLionel Sambuc #endif
228ebfedea0SLionel Sambuc 
229ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
230*0a6a1f1dSLionel Sambuc     if (cert->dh_tmp != NULL) {
231ebfedea0SLionel Sambuc         ret->dh_tmp = DHparams_dup(cert->dh_tmp);
232*0a6a1f1dSLionel Sambuc         if (ret->dh_tmp == NULL) {
233ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
234ebfedea0SLionel Sambuc             goto err;
235ebfedea0SLionel Sambuc         }
236*0a6a1f1dSLionel Sambuc         if (cert->dh_tmp->priv_key) {
237ebfedea0SLionel Sambuc             BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
238*0a6a1f1dSLionel Sambuc             if (!b) {
239ebfedea0SLionel Sambuc                 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
240ebfedea0SLionel Sambuc                 goto err;
241ebfedea0SLionel Sambuc             }
242ebfedea0SLionel Sambuc             ret->dh_tmp->priv_key = b;
243ebfedea0SLionel Sambuc         }
244*0a6a1f1dSLionel Sambuc         if (cert->dh_tmp->pub_key) {
245ebfedea0SLionel Sambuc             BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
246*0a6a1f1dSLionel Sambuc             if (!b) {
247ebfedea0SLionel Sambuc                 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
248ebfedea0SLionel Sambuc                 goto err;
249ebfedea0SLionel Sambuc             }
250ebfedea0SLionel Sambuc             ret->dh_tmp->pub_key = b;
251ebfedea0SLionel Sambuc         }
252ebfedea0SLionel Sambuc     }
253ebfedea0SLionel Sambuc     ret->dh_tmp_cb = cert->dh_tmp_cb;
254ebfedea0SLionel Sambuc #endif
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
257*0a6a1f1dSLionel Sambuc     if (cert->ecdh_tmp) {
258ebfedea0SLionel Sambuc         ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
259*0a6a1f1dSLionel Sambuc         if (ret->ecdh_tmp == NULL) {
260ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB);
261ebfedea0SLionel Sambuc             goto err;
262ebfedea0SLionel Sambuc         }
263ebfedea0SLionel Sambuc     }
264ebfedea0SLionel Sambuc     ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
265ebfedea0SLionel Sambuc #endif
266ebfedea0SLionel Sambuc 
267*0a6a1f1dSLionel Sambuc     for (i = 0; i < SSL_PKEY_NUM; i++) {
268*0a6a1f1dSLionel Sambuc         if (cert->pkeys[i].x509 != NULL) {
269ebfedea0SLionel Sambuc             ret->pkeys[i].x509 = cert->pkeys[i].x509;
270*0a6a1f1dSLionel Sambuc             CRYPTO_add(&ret->pkeys[i].x509->references, 1, CRYPTO_LOCK_X509);
271ebfedea0SLionel Sambuc         }
272ebfedea0SLionel Sambuc 
273*0a6a1f1dSLionel Sambuc         if (cert->pkeys[i].privatekey != NULL) {
274ebfedea0SLionel Sambuc             ret->pkeys[i].privatekey = cert->pkeys[i].privatekey;
275ebfedea0SLionel Sambuc             CRYPTO_add(&ret->pkeys[i].privatekey->references, 1,
276ebfedea0SLionel Sambuc                        CRYPTO_LOCK_EVP_PKEY);
277ebfedea0SLionel Sambuc         }
278ebfedea0SLionel Sambuc     }
279ebfedea0SLionel Sambuc 
280*0a6a1f1dSLionel Sambuc     /*
281*0a6a1f1dSLionel Sambuc      * ret->extra_certs *should* exist, but currently the own certificate
282*0a6a1f1dSLionel Sambuc      * chain is held inside SSL_CTX
283*0a6a1f1dSLionel Sambuc      */
284ebfedea0SLionel Sambuc 
285ebfedea0SLionel Sambuc     ret->references = 1;
286*0a6a1f1dSLionel Sambuc     /*
287*0a6a1f1dSLionel Sambuc      * Set digests to defaults. NB: we don't copy existing values as they
288ebfedea0SLionel Sambuc      * will be set during handshake.
289ebfedea0SLionel Sambuc      */
290ebfedea0SLionel Sambuc     ssl_cert_set_default_md(ret);
291ebfedea0SLionel Sambuc 
292ebfedea0SLionel Sambuc     return (ret);
293ebfedea0SLionel Sambuc 
294ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH)
295ebfedea0SLionel Sambuc  err:
296ebfedea0SLionel Sambuc #endif
297ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
298ebfedea0SLionel Sambuc     if (ret->rsa_tmp != NULL)
299ebfedea0SLionel Sambuc         RSA_free(ret->rsa_tmp);
300ebfedea0SLionel Sambuc #endif
301ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
302ebfedea0SLionel Sambuc     if (ret->dh_tmp != NULL)
303ebfedea0SLionel Sambuc         DH_free(ret->dh_tmp);
304ebfedea0SLionel Sambuc #endif
305ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
306ebfedea0SLionel Sambuc     if (ret->ecdh_tmp != NULL)
307ebfedea0SLionel Sambuc         EC_KEY_free(ret->ecdh_tmp);
308ebfedea0SLionel Sambuc #endif
309ebfedea0SLionel Sambuc 
310*0a6a1f1dSLionel Sambuc     for (i = 0; i < SSL_PKEY_NUM; i++) {
311ebfedea0SLionel Sambuc         if (ret->pkeys[i].x509 != NULL)
312ebfedea0SLionel Sambuc             X509_free(ret->pkeys[i].x509);
313ebfedea0SLionel Sambuc         if (ret->pkeys[i].privatekey != NULL)
314ebfedea0SLionel Sambuc             EVP_PKEY_free(ret->pkeys[i].privatekey);
315ebfedea0SLionel Sambuc     }
316ebfedea0SLionel Sambuc 
317ebfedea0SLionel Sambuc     return NULL;
318ebfedea0SLionel Sambuc }
319ebfedea0SLionel Sambuc 
ssl_cert_free(CERT * c)320ebfedea0SLionel Sambuc void ssl_cert_free(CERT *c)
321ebfedea0SLionel Sambuc {
322ebfedea0SLionel Sambuc     int i;
323ebfedea0SLionel Sambuc 
324ebfedea0SLionel Sambuc     if (c == NULL)
325ebfedea0SLionel Sambuc         return;
326ebfedea0SLionel Sambuc 
327ebfedea0SLionel Sambuc     i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT);
328ebfedea0SLionel Sambuc #ifdef REF_PRINT
329ebfedea0SLionel Sambuc     REF_PRINT("CERT", c);
330ebfedea0SLionel Sambuc #endif
331*0a6a1f1dSLionel Sambuc     if (i > 0)
332*0a6a1f1dSLionel Sambuc         return;
333ebfedea0SLionel Sambuc #ifdef REF_CHECK
334*0a6a1f1dSLionel Sambuc     if (i < 0) {
335ebfedea0SLionel Sambuc         fprintf(stderr, "ssl_cert_free, bad reference count\n");
336ebfedea0SLionel Sambuc         abort();                /* ok */
337ebfedea0SLionel Sambuc     }
338ebfedea0SLionel Sambuc #endif
339ebfedea0SLionel Sambuc 
340ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
341*0a6a1f1dSLionel Sambuc     if (c->rsa_tmp)
342*0a6a1f1dSLionel Sambuc         RSA_free(c->rsa_tmp);
343ebfedea0SLionel Sambuc #endif
344ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
345*0a6a1f1dSLionel Sambuc     if (c->dh_tmp)
346*0a6a1f1dSLionel Sambuc         DH_free(c->dh_tmp);
347ebfedea0SLionel Sambuc #endif
348ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
349*0a6a1f1dSLionel Sambuc     if (c->ecdh_tmp)
350*0a6a1f1dSLionel Sambuc         EC_KEY_free(c->ecdh_tmp);
351ebfedea0SLionel Sambuc #endif
352ebfedea0SLionel Sambuc 
353*0a6a1f1dSLionel Sambuc     for (i = 0; i < SSL_PKEY_NUM; i++) {
354ebfedea0SLionel Sambuc         if (c->pkeys[i].x509 != NULL)
355ebfedea0SLionel Sambuc             X509_free(c->pkeys[i].x509);
356ebfedea0SLionel Sambuc         if (c->pkeys[i].privatekey != NULL)
357ebfedea0SLionel Sambuc             EVP_PKEY_free(c->pkeys[i].privatekey);
358ebfedea0SLionel Sambuc #if 0
359ebfedea0SLionel Sambuc         if (c->pkeys[i].publickey != NULL)
360ebfedea0SLionel Sambuc             EVP_PKEY_free(c->pkeys[i].publickey);
361ebfedea0SLionel Sambuc #endif
362ebfedea0SLionel Sambuc     }
363ebfedea0SLionel Sambuc     OPENSSL_free(c);
364ebfedea0SLionel Sambuc }
365ebfedea0SLionel Sambuc 
ssl_cert_inst(CERT ** o)366ebfedea0SLionel Sambuc int ssl_cert_inst(CERT **o)
367ebfedea0SLionel Sambuc {
368*0a6a1f1dSLionel Sambuc     /*
369*0a6a1f1dSLionel Sambuc      * Create a CERT if there isn't already one (which cannot really happen,
370*0a6a1f1dSLionel Sambuc      * as it is initially created in SSL_CTX_new; but the earlier code
371*0a6a1f1dSLionel Sambuc      * usually allows for that one being non-existant, so we follow that
372*0a6a1f1dSLionel Sambuc      * behaviour, as it might turn out that there actually is a reason for it
373*0a6a1f1dSLionel Sambuc      * -- but I'm not sure that *all* of the existing code could cope with
374*0a6a1f1dSLionel Sambuc      * s->cert being NULL, otherwise we could do without the initialization
375*0a6a1f1dSLionel Sambuc      * in SSL_CTX_new).
376ebfedea0SLionel Sambuc      */
377ebfedea0SLionel Sambuc 
378*0a6a1f1dSLionel Sambuc     if (o == NULL) {
379ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
380ebfedea0SLionel Sambuc         return (0);
381ebfedea0SLionel Sambuc     }
382*0a6a1f1dSLionel Sambuc     if (*o == NULL) {
383*0a6a1f1dSLionel Sambuc         if ((*o = ssl_cert_new()) == NULL) {
384ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
385ebfedea0SLionel Sambuc             return (0);
386ebfedea0SLionel Sambuc         }
387ebfedea0SLionel Sambuc     }
388ebfedea0SLionel Sambuc     return (1);
389ebfedea0SLionel Sambuc }
390ebfedea0SLionel Sambuc 
ssl_sess_cert_new(void)391ebfedea0SLionel Sambuc SESS_CERT *ssl_sess_cert_new(void)
392ebfedea0SLionel Sambuc {
393ebfedea0SLionel Sambuc     SESS_CERT *ret;
394ebfedea0SLionel Sambuc 
395ebfedea0SLionel Sambuc     ret = OPENSSL_malloc(sizeof *ret);
396*0a6a1f1dSLionel Sambuc     if (ret == NULL) {
397ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
398ebfedea0SLionel Sambuc         return NULL;
399ebfedea0SLionel Sambuc     }
400ebfedea0SLionel Sambuc 
401ebfedea0SLionel Sambuc     memset(ret, 0, sizeof *ret);
402ebfedea0SLionel Sambuc     ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
403ebfedea0SLionel Sambuc     ret->references = 1;
404ebfedea0SLionel Sambuc 
405ebfedea0SLionel Sambuc     return ret;
406ebfedea0SLionel Sambuc }
407ebfedea0SLionel Sambuc 
ssl_sess_cert_free(SESS_CERT * sc)408ebfedea0SLionel Sambuc void ssl_sess_cert_free(SESS_CERT *sc)
409ebfedea0SLionel Sambuc {
410ebfedea0SLionel Sambuc     int i;
411ebfedea0SLionel Sambuc 
412ebfedea0SLionel Sambuc     if (sc == NULL)
413ebfedea0SLionel Sambuc         return;
414ebfedea0SLionel Sambuc 
415ebfedea0SLionel Sambuc     i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
416ebfedea0SLionel Sambuc #ifdef REF_PRINT
417ebfedea0SLionel Sambuc     REF_PRINT("SESS_CERT", sc);
418ebfedea0SLionel Sambuc #endif
419ebfedea0SLionel Sambuc     if (i > 0)
420ebfedea0SLionel Sambuc         return;
421ebfedea0SLionel Sambuc #ifdef REF_CHECK
422*0a6a1f1dSLionel Sambuc     if (i < 0) {
423ebfedea0SLionel Sambuc         fprintf(stderr, "ssl_sess_cert_free, bad reference count\n");
424ebfedea0SLionel Sambuc         abort();                /* ok */
425ebfedea0SLionel Sambuc     }
426ebfedea0SLionel Sambuc #endif
427ebfedea0SLionel Sambuc 
428ebfedea0SLionel Sambuc     /* i == 0 */
429ebfedea0SLionel Sambuc     if (sc->cert_chain != NULL)
430ebfedea0SLionel Sambuc         sk_X509_pop_free(sc->cert_chain, X509_free);
431*0a6a1f1dSLionel Sambuc     for (i = 0; i < SSL_PKEY_NUM; i++) {
432ebfedea0SLionel Sambuc         if (sc->peer_pkeys[i].x509 != NULL)
433ebfedea0SLionel Sambuc             X509_free(sc->peer_pkeys[i].x509);
434*0a6a1f1dSLionel Sambuc #if 0                           /* We don't have the peer's private key.
435*0a6a1f1dSLionel Sambuc                                  * These lines are just * here as a reminder
436*0a6a1f1dSLionel Sambuc                                  * that we're still using a
437*0a6a1f1dSLionel Sambuc                                  * not-quite-appropriate * data structure. */
438ebfedea0SLionel Sambuc         if (sc->peer_pkeys[i].privatekey != NULL)
439ebfedea0SLionel Sambuc             EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
440ebfedea0SLionel Sambuc #endif
441ebfedea0SLionel Sambuc     }
442ebfedea0SLionel Sambuc 
443ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
444ebfedea0SLionel Sambuc     if (sc->peer_rsa_tmp != NULL)
445ebfedea0SLionel Sambuc         RSA_free(sc->peer_rsa_tmp);
446ebfedea0SLionel Sambuc #endif
447ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
448ebfedea0SLionel Sambuc     if (sc->peer_dh_tmp != NULL)
449ebfedea0SLionel Sambuc         DH_free(sc->peer_dh_tmp);
450ebfedea0SLionel Sambuc #endif
451ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
452ebfedea0SLionel Sambuc     if (sc->peer_ecdh_tmp != NULL)
453ebfedea0SLionel Sambuc         EC_KEY_free(sc->peer_ecdh_tmp);
454ebfedea0SLionel Sambuc #endif
455ebfedea0SLionel Sambuc 
456ebfedea0SLionel Sambuc     OPENSSL_free(sc);
457ebfedea0SLionel Sambuc }
458ebfedea0SLionel Sambuc 
ssl_set_peer_cert_type(SESS_CERT * sc,int type)459ebfedea0SLionel Sambuc int ssl_set_peer_cert_type(SESS_CERT *sc, int type)
460ebfedea0SLionel Sambuc {
461ebfedea0SLionel Sambuc     sc->peer_cert_type = type;
462ebfedea0SLionel Sambuc     return (1);
463ebfedea0SLionel Sambuc }
464ebfedea0SLionel Sambuc 
ssl_verify_cert_chain(SSL * s,STACK_OF (X509)* sk)465ebfedea0SLionel Sambuc int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
466ebfedea0SLionel Sambuc {
467ebfedea0SLionel Sambuc     X509 *x;
468ebfedea0SLionel Sambuc     int i;
469ebfedea0SLionel Sambuc     X509_STORE_CTX ctx;
470ebfedea0SLionel Sambuc 
471ebfedea0SLionel Sambuc     if ((sk == NULL) || (sk_X509_num(sk) == 0))
472ebfedea0SLionel Sambuc         return (0);
473ebfedea0SLionel Sambuc 
474ebfedea0SLionel Sambuc     x = sk_X509_value(sk, 0);
475*0a6a1f1dSLionel Sambuc     if (!X509_STORE_CTX_init(&ctx, s->ctx->cert_store, x, sk)) {
476ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
477ebfedea0SLionel Sambuc         return (0);
478ebfedea0SLionel Sambuc     }
479ebfedea0SLionel Sambuc #if 0
480ebfedea0SLionel Sambuc     if (SSL_get_verify_depth(s) >= 0)
481ebfedea0SLionel Sambuc         X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
482ebfedea0SLionel Sambuc #endif
483ebfedea0SLionel Sambuc     X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
484ebfedea0SLionel Sambuc 
485*0a6a1f1dSLionel Sambuc     /*
486*0a6a1f1dSLionel Sambuc      * We need to inherit the verify parameters. These can be determined by
487*0a6a1f1dSLionel Sambuc      * the context: if its a server it will verify SSL client certificates or
488*0a6a1f1dSLionel Sambuc      * vice versa.
489ebfedea0SLionel Sambuc      */
490ebfedea0SLionel Sambuc 
491*0a6a1f1dSLionel Sambuc     X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
492*0a6a1f1dSLionel Sambuc     /*
493*0a6a1f1dSLionel Sambuc      * Anything non-default in "param" should overwrite anything in the ctx.
494ebfedea0SLionel Sambuc      */
495ebfedea0SLionel Sambuc     X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
496ebfedea0SLionel Sambuc 
497ebfedea0SLionel Sambuc     if (s->verify_callback)
498ebfedea0SLionel Sambuc         X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
499ebfedea0SLionel Sambuc 
500ebfedea0SLionel Sambuc     if (s->ctx->app_verify_callback != NULL)
501ebfedea0SLionel Sambuc #if 1                           /* new with OpenSSL 0.9.7 */
502ebfedea0SLionel Sambuc         i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
503ebfedea0SLionel Sambuc #else
504ebfedea0SLionel Sambuc         i = s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
505ebfedea0SLionel Sambuc #endif
506*0a6a1f1dSLionel Sambuc     else {
507ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_X509_VERIFY
508ebfedea0SLionel Sambuc         i = X509_verify_cert(&ctx);
509ebfedea0SLionel Sambuc #else
510ebfedea0SLionel Sambuc         i = 0;
511ebfedea0SLionel Sambuc         ctx.error = X509_V_ERR_APPLICATION_VERIFICATION;
512ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_NO_VERIFY_CALLBACK);
513ebfedea0SLionel Sambuc #endif
514ebfedea0SLionel Sambuc     }
515ebfedea0SLionel Sambuc 
516ebfedea0SLionel Sambuc     s->verify_result = ctx.error;
517ebfedea0SLionel Sambuc     X509_STORE_CTX_cleanup(&ctx);
518ebfedea0SLionel Sambuc 
519ebfedea0SLionel Sambuc     return (i);
520ebfedea0SLionel Sambuc }
521ebfedea0SLionel Sambuc 
set_client_CA_list(STACK_OF (X509_NAME)** ca_list,STACK_OF (X509_NAME)* name_list)522*0a6a1f1dSLionel Sambuc static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
523*0a6a1f1dSLionel Sambuc                                STACK_OF(X509_NAME) *name_list)
524ebfedea0SLionel Sambuc {
525ebfedea0SLionel Sambuc     if (*ca_list != NULL)
526ebfedea0SLionel Sambuc         sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
527ebfedea0SLionel Sambuc 
528ebfedea0SLionel Sambuc     *ca_list = name_list;
529ebfedea0SLionel Sambuc }
530ebfedea0SLionel Sambuc 
STACK_OF(X509_NAME)531ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
532ebfedea0SLionel Sambuc {
533ebfedea0SLionel Sambuc     int i;
534ebfedea0SLionel Sambuc     STACK_OF(X509_NAME) *ret;
535ebfedea0SLionel Sambuc     X509_NAME *name;
536ebfedea0SLionel Sambuc 
537ebfedea0SLionel Sambuc     ret = sk_X509_NAME_new_null();
538*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_X509_NAME_num(sk); i++) {
539ebfedea0SLionel Sambuc         name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
540*0a6a1f1dSLionel Sambuc         if ((name == NULL) || !sk_X509_NAME_push(ret, name)) {
541ebfedea0SLionel Sambuc             sk_X509_NAME_pop_free(ret, X509_NAME_free);
542ebfedea0SLionel Sambuc             return (NULL);
543ebfedea0SLionel Sambuc         }
544ebfedea0SLionel Sambuc     }
545ebfedea0SLionel Sambuc     return (ret);
546ebfedea0SLionel Sambuc }
547ebfedea0SLionel Sambuc 
SSL_set_client_CA_list(SSL * s,STACK_OF (X509_NAME)* name_list)548ebfedea0SLionel Sambuc void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
549ebfedea0SLionel Sambuc {
550ebfedea0SLionel Sambuc     set_client_CA_list(&(s->client_CA), name_list);
551ebfedea0SLionel Sambuc }
552ebfedea0SLionel Sambuc 
SSL_CTX_set_client_CA_list(SSL_CTX * ctx,STACK_OF (X509_NAME)* name_list)553ebfedea0SLionel Sambuc void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
554ebfedea0SLionel Sambuc {
555ebfedea0SLionel Sambuc     set_client_CA_list(&(ctx->client_CA), name_list);
556ebfedea0SLionel Sambuc }
557ebfedea0SLionel Sambuc 
STACK_OF(X509_NAME)558ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
559ebfedea0SLionel Sambuc {
560ebfedea0SLionel Sambuc     return (ctx->client_CA);
561ebfedea0SLionel Sambuc }
562ebfedea0SLionel Sambuc 
STACK_OF(X509_NAME)563ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
564ebfedea0SLionel Sambuc {
565*0a6a1f1dSLionel Sambuc     if (s->type == SSL_ST_CONNECT) { /* we are in the client */
566*0a6a1f1dSLionel Sambuc         if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL))
567ebfedea0SLionel Sambuc             return (s->s3->tmp.ca_names);
568ebfedea0SLionel Sambuc         else
569ebfedea0SLionel Sambuc             return (NULL);
570*0a6a1f1dSLionel Sambuc     } else {
571ebfedea0SLionel Sambuc         if (s->client_CA != NULL)
572ebfedea0SLionel Sambuc             return (s->client_CA);
573ebfedea0SLionel Sambuc         else
574ebfedea0SLionel Sambuc             return (s->ctx->client_CA);
575ebfedea0SLionel Sambuc     }
576ebfedea0SLionel Sambuc }
577ebfedea0SLionel Sambuc 
add_client_CA(STACK_OF (X509_NAME)** sk,X509 * x)578ebfedea0SLionel Sambuc static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x)
579ebfedea0SLionel Sambuc {
580ebfedea0SLionel Sambuc     X509_NAME *name;
581ebfedea0SLionel Sambuc 
582*0a6a1f1dSLionel Sambuc     if (x == NULL)
583*0a6a1f1dSLionel Sambuc         return (0);
584ebfedea0SLionel Sambuc     if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL))
585ebfedea0SLionel Sambuc         return (0);
586ebfedea0SLionel Sambuc 
587ebfedea0SLionel Sambuc     if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
588ebfedea0SLionel Sambuc         return (0);
589ebfedea0SLionel Sambuc 
590*0a6a1f1dSLionel Sambuc     if (!sk_X509_NAME_push(*sk, name)) {
591ebfedea0SLionel Sambuc         X509_NAME_free(name);
592ebfedea0SLionel Sambuc         return (0);
593ebfedea0SLionel Sambuc     }
594ebfedea0SLionel Sambuc     return (1);
595ebfedea0SLionel Sambuc }
596ebfedea0SLionel Sambuc 
SSL_add_client_CA(SSL * ssl,X509 * x)597ebfedea0SLionel Sambuc int SSL_add_client_CA(SSL *ssl, X509 *x)
598ebfedea0SLionel Sambuc {
599ebfedea0SLionel Sambuc     return (add_client_CA(&(ssl->client_CA), x));
600ebfedea0SLionel Sambuc }
601ebfedea0SLionel Sambuc 
SSL_CTX_add_client_CA(SSL_CTX * ctx,X509 * x)602ebfedea0SLionel Sambuc int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
603ebfedea0SLionel Sambuc {
604ebfedea0SLionel Sambuc     return (add_client_CA(&(ctx->client_CA), x));
605ebfedea0SLionel Sambuc }
606ebfedea0SLionel Sambuc 
xname_cmp(const X509_NAME * const * a,const X509_NAME * const * b)607ebfedea0SLionel Sambuc static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
608ebfedea0SLionel Sambuc {
609ebfedea0SLionel Sambuc     return (X509_NAME_cmp(*a, *b));
610ebfedea0SLionel Sambuc }
611ebfedea0SLionel Sambuc 
612ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
613*0a6a1f1dSLionel Sambuc /**
614ebfedea0SLionel Sambuc  * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
615ebfedea0SLionel Sambuc  * it doesn't really have anything to do with clients (except that a common use
616ebfedea0SLionel Sambuc  * for a stack of CAs is to send it to the client). Actually, it doesn't have
617ebfedea0SLionel Sambuc  * much to do with CAs, either, since it will load any old cert.
618ebfedea0SLionel Sambuc  * \param file the file containing one or more certs.
619ebfedea0SLionel Sambuc  * \return a ::STACK containing the certs.
620ebfedea0SLionel Sambuc  */
STACK_OF(X509_NAME)621ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
622ebfedea0SLionel Sambuc {
623ebfedea0SLionel Sambuc     BIO *in;
624ebfedea0SLionel Sambuc     X509 *x = NULL;
625ebfedea0SLionel Sambuc     X509_NAME *xn = NULL;
626ebfedea0SLionel Sambuc     STACK_OF(X509_NAME) *ret = NULL, *sk;
627ebfedea0SLionel Sambuc 
628ebfedea0SLionel Sambuc     sk = sk_X509_NAME_new(xname_cmp);
629ebfedea0SLionel Sambuc 
630ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file_internal());
631ebfedea0SLionel Sambuc 
632*0a6a1f1dSLionel Sambuc     if ((sk == NULL) || (in == NULL)) {
633ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
634ebfedea0SLionel Sambuc         goto err;
635ebfedea0SLionel Sambuc     }
636ebfedea0SLionel Sambuc 
637ebfedea0SLionel Sambuc     if (!BIO_read_filename(in, file))
638ebfedea0SLionel Sambuc         goto err;
639ebfedea0SLionel Sambuc 
640*0a6a1f1dSLionel Sambuc     for (;;) {
641ebfedea0SLionel Sambuc         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
642ebfedea0SLionel Sambuc             break;
643*0a6a1f1dSLionel Sambuc         if (ret == NULL) {
644ebfedea0SLionel Sambuc             ret = sk_X509_NAME_new_null();
645*0a6a1f1dSLionel Sambuc             if (ret == NULL) {
646ebfedea0SLionel Sambuc                 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
647ebfedea0SLionel Sambuc                 goto err;
648ebfedea0SLionel Sambuc             }
649ebfedea0SLionel Sambuc         }
650*0a6a1f1dSLionel Sambuc         if ((xn = X509_get_subject_name(x)) == NULL)
651*0a6a1f1dSLionel Sambuc             goto err;
652ebfedea0SLionel Sambuc         /* check for duplicates */
653ebfedea0SLionel Sambuc         xn = X509_NAME_dup(xn);
654*0a6a1f1dSLionel Sambuc         if (xn == NULL)
655*0a6a1f1dSLionel Sambuc             goto err;
656ebfedea0SLionel Sambuc         if (sk_X509_NAME_find(sk, xn) >= 0)
657ebfedea0SLionel Sambuc             X509_NAME_free(xn);
658*0a6a1f1dSLionel Sambuc         else {
659ebfedea0SLionel Sambuc             sk_X509_NAME_push(sk, xn);
660ebfedea0SLionel Sambuc             sk_X509_NAME_push(ret, xn);
661ebfedea0SLionel Sambuc         }
662ebfedea0SLionel Sambuc     }
663ebfedea0SLionel Sambuc 
664*0a6a1f1dSLionel Sambuc     if (0) {
665ebfedea0SLionel Sambuc  err:
666*0a6a1f1dSLionel Sambuc         if (ret != NULL)
667*0a6a1f1dSLionel Sambuc             sk_X509_NAME_pop_free(ret, X509_NAME_free);
668ebfedea0SLionel Sambuc         ret = NULL;
669ebfedea0SLionel Sambuc     }
670*0a6a1f1dSLionel Sambuc     if (sk != NULL)
671*0a6a1f1dSLionel Sambuc         sk_X509_NAME_free(sk);
672*0a6a1f1dSLionel Sambuc     if (in != NULL)
673*0a6a1f1dSLionel Sambuc         BIO_free(in);
674*0a6a1f1dSLionel Sambuc     if (x != NULL)
675*0a6a1f1dSLionel Sambuc         X509_free(x);
676ebfedea0SLionel Sambuc     if (ret != NULL)
677ebfedea0SLionel Sambuc         ERR_clear_error();
678ebfedea0SLionel Sambuc     return (ret);
679ebfedea0SLionel Sambuc }
680ebfedea0SLionel Sambuc #endif
681ebfedea0SLionel Sambuc 
682*0a6a1f1dSLionel Sambuc /**
683ebfedea0SLionel Sambuc  * Add a file of certs to a stack.
684ebfedea0SLionel Sambuc  * \param stack the stack to add to.
685ebfedea0SLionel Sambuc  * \param file the file to add from. All certs in this file that are not
686ebfedea0SLionel Sambuc  * already in the stack will be added.
687ebfedea0SLionel Sambuc  * \return 1 for success, 0 for failure. Note that in the case of failure some
688ebfedea0SLionel Sambuc  * certs may have been added to \c stack.
689ebfedea0SLionel Sambuc  */
690ebfedea0SLionel Sambuc 
SSL_add_file_cert_subjects_to_stack(STACK_OF (X509_NAME)* stack,const char * file)691ebfedea0SLionel Sambuc int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
692ebfedea0SLionel Sambuc                                         const char *file)
693ebfedea0SLionel Sambuc {
694ebfedea0SLionel Sambuc     BIO *in;
695ebfedea0SLionel Sambuc     X509 *x = NULL;
696ebfedea0SLionel Sambuc     X509_NAME *xn = NULL;
697ebfedea0SLionel Sambuc     int ret = 1;
698ebfedea0SLionel Sambuc     int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
699ebfedea0SLionel Sambuc 
700ebfedea0SLionel Sambuc     oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
701ebfedea0SLionel Sambuc 
702ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file_internal());
703ebfedea0SLionel Sambuc 
704*0a6a1f1dSLionel Sambuc     if (in == NULL) {
705*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,
706*0a6a1f1dSLionel Sambuc                ERR_R_MALLOC_FAILURE);
707ebfedea0SLionel Sambuc         goto err;
708ebfedea0SLionel Sambuc     }
709ebfedea0SLionel Sambuc 
710ebfedea0SLionel Sambuc     if (!BIO_read_filename(in, file))
711ebfedea0SLionel Sambuc         goto err;
712ebfedea0SLionel Sambuc 
713*0a6a1f1dSLionel Sambuc     for (;;) {
714ebfedea0SLionel Sambuc         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
715ebfedea0SLionel Sambuc             break;
716*0a6a1f1dSLionel Sambuc         if ((xn = X509_get_subject_name(x)) == NULL)
717*0a6a1f1dSLionel Sambuc             goto err;
718ebfedea0SLionel Sambuc         xn = X509_NAME_dup(xn);
719*0a6a1f1dSLionel Sambuc         if (xn == NULL)
720*0a6a1f1dSLionel Sambuc             goto err;
721ebfedea0SLionel Sambuc         if (sk_X509_NAME_find(stack, xn) >= 0)
722ebfedea0SLionel Sambuc             X509_NAME_free(xn);
723ebfedea0SLionel Sambuc         else
724ebfedea0SLionel Sambuc             sk_X509_NAME_push(stack, xn);
725ebfedea0SLionel Sambuc     }
726ebfedea0SLionel Sambuc 
727ebfedea0SLionel Sambuc     ERR_clear_error();
728ebfedea0SLionel Sambuc 
729*0a6a1f1dSLionel Sambuc     if (0) {
730ebfedea0SLionel Sambuc  err:
731ebfedea0SLionel Sambuc         ret = 0;
732ebfedea0SLionel Sambuc     }
733ebfedea0SLionel Sambuc     if (in != NULL)
734ebfedea0SLionel Sambuc         BIO_free(in);
735ebfedea0SLionel Sambuc     if (x != NULL)
736ebfedea0SLionel Sambuc         X509_free(x);
737ebfedea0SLionel Sambuc 
738ebfedea0SLionel Sambuc     (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
739ebfedea0SLionel Sambuc 
740ebfedea0SLionel Sambuc     return ret;
741ebfedea0SLionel Sambuc }
742ebfedea0SLionel Sambuc 
743*0a6a1f1dSLionel Sambuc /**
744ebfedea0SLionel Sambuc  * Add a directory of certs to a stack.
745ebfedea0SLionel Sambuc  * \param stack the stack to append to.
746ebfedea0SLionel Sambuc  * \param dir the directory to append from. All files in this directory will be
747ebfedea0SLionel Sambuc  * examined as potential certs. Any that are acceptable to
748ebfedea0SLionel Sambuc  * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
749ebfedea0SLionel Sambuc  * included.
750ebfedea0SLionel Sambuc  * \return 1 for success, 0 for failure. Note that in the case of failure some
751ebfedea0SLionel Sambuc  * certs may have been added to \c stack.
752ebfedea0SLionel Sambuc  */
753ebfedea0SLionel Sambuc 
SSL_add_dir_cert_subjects_to_stack(STACK_OF (X509_NAME)* stack,const char * dir)754ebfedea0SLionel Sambuc int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
755ebfedea0SLionel Sambuc                                        const char *dir)
756ebfedea0SLionel Sambuc {
757ebfedea0SLionel Sambuc     OPENSSL_DIR_CTX *d = NULL;
758ebfedea0SLionel Sambuc     const char *filename;
759ebfedea0SLionel Sambuc     int ret = 0;
760ebfedea0SLionel Sambuc 
761ebfedea0SLionel Sambuc     CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
762ebfedea0SLionel Sambuc 
763ebfedea0SLionel Sambuc     /* Note that a side effect is that the CAs will be sorted by name */
764ebfedea0SLionel Sambuc 
765*0a6a1f1dSLionel Sambuc     while ((filename = OPENSSL_DIR_read(&d, dir))) {
766ebfedea0SLionel Sambuc         char buf[1024];
767ebfedea0SLionel Sambuc         int r;
768ebfedea0SLionel Sambuc 
769*0a6a1f1dSLionel Sambuc         if (strlen(dir) + strlen(filename) + 2 > sizeof buf) {
770*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
771*0a6a1f1dSLionel Sambuc                    SSL_R_PATH_TOO_LONG);
772ebfedea0SLionel Sambuc             goto err;
773ebfedea0SLionel Sambuc         }
774ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
775ebfedea0SLionel Sambuc         r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename);
776ebfedea0SLionel Sambuc #else
777ebfedea0SLionel Sambuc         r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
778ebfedea0SLionel Sambuc #endif
779ebfedea0SLionel Sambuc         if (r <= 0 || r >= (int)sizeof(buf))
780ebfedea0SLionel Sambuc             goto err;
781ebfedea0SLionel Sambuc         if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
782ebfedea0SLionel Sambuc             goto err;
783ebfedea0SLionel Sambuc     }
784ebfedea0SLionel Sambuc 
785*0a6a1f1dSLionel Sambuc     if (errno) {
786ebfedea0SLionel Sambuc         SYSerr(SYS_F_OPENDIR, get_last_sys_error());
787ebfedea0SLionel Sambuc         ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
788ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
789ebfedea0SLionel Sambuc         goto err;
790ebfedea0SLionel Sambuc     }
791ebfedea0SLionel Sambuc 
792ebfedea0SLionel Sambuc     ret = 1;
793ebfedea0SLionel Sambuc 
794ebfedea0SLionel Sambuc  err:
795*0a6a1f1dSLionel Sambuc     if (d)
796*0a6a1f1dSLionel Sambuc         OPENSSL_DIR_end(&d);
797ebfedea0SLionel Sambuc     CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
798ebfedea0SLionel Sambuc     return ret;
799ebfedea0SLionel Sambuc }
800