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