blob: f3fc6633281465a4104147508d82e2d133af053a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
114
115#include <stdio.h>
116
117#include <openssl/bio.h>
118#include <openssl/bn.h>
David Benjamin676d1e72014-07-08 14:34:10 -0400119#include <openssl/buf.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/dh.h>
121#include <openssl/err.h>
122#include <openssl/mem.h>
123#include <openssl/obj.h>
124#include <openssl/pem.h>
125#include <openssl/x509v3.h>
126
127#include "../crypto/dh/internal.h"
128#include "../crypto/directory.h"
129#include "ssl_locl.h"
130
131int SSL_get_ex_data_X509_STORE_CTX_idx(void)
132 {
133 static volatile int ssl_x509_store_ctx_idx= -1;
134 int got_write_lock = 0;
135
136 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
137
138 if (ssl_x509_store_ctx_idx < 0)
139 {
140 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
141 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
142 got_write_lock = 1;
143
144 if (ssl_x509_store_ctx_idx < 0)
145 {
146 ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
147 0,"SSL for verify callback",NULL,NULL,NULL);
148 }
149 }
150
151 if (got_write_lock)
152 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
153 else
154 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
155
156 return ssl_x509_store_ctx_idx;
157 }
158
Adam Langley95c29f32014-06-20 12:00:00 -0700159CERT *ssl_cert_new(void)
160 {
161 CERT *ret;
162
163 ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
164 if (ret == NULL)
165 {
166 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
167 return(NULL);
168 }
169 memset(ret,0,sizeof(CERT));
170
171 ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
Adam Langley95c29f32014-06-20 12:00:00 -0700172 return(ret);
173 }
174
175CERT *ssl_cert_dup(CERT *cert)
176 {
177 CERT *ret;
178 int i;
179
180 ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
181 if (ret == NULL)
182 {
183 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
184 return(NULL);
185 }
186
187 memset(ret, 0, sizeof(CERT));
188
189 ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
190 /* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
191 * if you find that more readable */
192
Adam Langley95c29f32014-06-20 12:00:00 -0700193 ret->mask_k = cert->mask_k;
194 ret->mask_a = cert->mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley95c29f32014-06-20 12:00:00 -0700196 if (cert->dh_tmp != NULL)
197 {
198 ret->dh_tmp = DHparams_dup(cert->dh_tmp);
199 if (ret->dh_tmp == NULL)
200 {
201 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
202 goto err;
203 }
204 if (cert->dh_tmp->priv_key)
205 {
206 BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
207 if (!b)
208 {
209 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
210 goto err;
211 }
212 ret->dh_tmp->priv_key = b;
213 }
214 if (cert->dh_tmp->pub_key)
215 {
216 BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
217 if (!b)
218 {
219 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
220 goto err;
221 }
222 ret->dh_tmp->pub_key = b;
223 }
224 }
225 ret->dh_tmp_cb = cert->dh_tmp_cb;
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langley95c29f32014-06-20 12:00:00 -0700227 if (cert->ecdh_tmp)
228 {
229 ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
230 if (ret->ecdh_tmp == NULL)
231 {
232 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB);
233 goto err;
234 }
235 }
236 ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
237 ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
Adam Langley95c29f32014-06-20 12:00:00 -0700238
239 for (i = 0; i < SSL_PKEY_NUM; i++)
240 {
241 CERT_PKEY *cpk = cert->pkeys + i;
242 CERT_PKEY *rpk = ret->pkeys + i;
243 if (cpk->x509 != NULL)
244 {
David Benjamin150c6172014-08-05 22:22:49 -0400245 rpk->x509 = X509_up_ref(cpk->x509);
Adam Langley95c29f32014-06-20 12:00:00 -0700246 }
247
248 if (cpk->privatekey != NULL)
249 {
David Benjamin82781842014-12-04 16:26:47 -0500250 rpk->privatekey = EVP_PKEY_dup(cpk->privatekey);
Adam Langley95c29f32014-06-20 12:00:00 -0700251 }
252
253 if (cpk->chain)
254 {
255 rpk->chain = X509_chain_up_ref(cpk->chain);
256 if (!rpk->chain)
257 {
258 OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
259 goto err;
260 }
261 }
Adam Langley95c29f32014-06-20 12:00:00 -0700262 }
263
Adam Langley95c29f32014-06-20 12:00:00 -0700264 /* Peer sigalgs set to NULL as we get these from handshake too */
265 ret->peer_sigalgs = NULL;
266 ret->peer_sigalgslen = 0;
267 /* Configured sigalgs however we copy across */
268
269 if (cert->conf_sigalgs)
270 {
271 ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
272 if (!ret->conf_sigalgs)
273 goto err;
274 memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
275 cert->conf_sigalgslen);
276 ret->conf_sigalgslen = cert->conf_sigalgslen;
277 }
278 else
279 ret->conf_sigalgs = NULL;
280
281 if (cert->client_sigalgs)
282 {
283 ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
284 if (!ret->client_sigalgs)
285 goto err;
286 memcpy(ret->client_sigalgs, cert->client_sigalgs,
287 cert->client_sigalgslen);
288 ret->client_sigalgslen = cert->client_sigalgslen;
289 }
290 else
291 ret->client_sigalgs = NULL;
292 /* Shared sigalgs also NULL */
293 ret->shared_sigalgs = NULL;
294 /* Copy any custom client certificate types */
David Benjamin676d1e72014-07-08 14:34:10 -0400295 if (cert->client_certificate_types)
Adam Langley95c29f32014-06-20 12:00:00 -0700296 {
David Benjamin676d1e72014-07-08 14:34:10 -0400297 ret->client_certificate_types = BUF_memdup(
298 cert->client_certificate_types,
299 cert->num_client_certificate_types);
300 if (!ret->client_certificate_types)
Adam Langley95c29f32014-06-20 12:00:00 -0700301 goto err;
David Benjamin676d1e72014-07-08 14:34:10 -0400302 ret->num_client_certificate_types = cert->num_client_certificate_types;
Adam Langley95c29f32014-06-20 12:00:00 -0700303 }
304
305 ret->cert_flags = cert->cert_flags;
306
307 ret->cert_cb = cert->cert_cb;
308 ret->cert_cb_arg = cert->cert_cb_arg;
309
310 if (cert->verify_store)
311 {
David Benjamin67454b62014-08-06 22:15:46 -0400312 CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
313 ret->verify_store = cert->verify_store;
Adam Langley95c29f32014-06-20 12:00:00 -0700314 }
315
316 if (cert->chain_store)
317 {
David Benjamin67454b62014-08-06 22:15:46 -0400318 CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
319 ret->chain_store = cert->chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -0700320 }
321
322 ret->ciphers_raw = NULL;
323
324 return(ret);
325
Adam Langley95c29f32014-06-20 12:00:00 -0700326err:
David Benjamin3f383902014-11-02 16:00:28 -0500327 ssl_cert_free(ret);
Adam Langley95c29f32014-06-20 12:00:00 -0700328 return NULL;
329 }
330
331/* Free up and clear all certificates and chains */
332
333void ssl_cert_clear_certs(CERT *c)
334 {
335 int i;
336 if (c == NULL)
337 return;
338 for (i = 0; i<SSL_PKEY_NUM; i++)
339 {
340 CERT_PKEY *cpk = c->pkeys + i;
341 if (cpk->x509)
342 {
343 X509_free(cpk->x509);
344 cpk->x509 = NULL;
345 }
346 if (cpk->privatekey)
347 {
348 EVP_PKEY_free(cpk->privatekey);
349 cpk->privatekey = NULL;
350 }
351 if (cpk->chain)
352 {
353 sk_X509_pop_free(cpk->chain, X509_free);
354 cpk->chain = NULL;
355 }
Adam Langley95c29f32014-06-20 12:00:00 -0700356 }
357 }
358
359void ssl_cert_free(CERT *c)
360 {
Adam Langley95c29f32014-06-20 12:00:00 -0700361 if(c == NULL)
362 return;
363
Adam Langley95c29f32014-06-20 12:00:00 -0700364 if (c->dh_tmp) DH_free(c->dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700365 if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700366
367 ssl_cert_clear_certs(c);
368 if (c->peer_sigalgs)
369 OPENSSL_free(c->peer_sigalgs);
370 if (c->conf_sigalgs)
371 OPENSSL_free(c->conf_sigalgs);
372 if (c->client_sigalgs)
373 OPENSSL_free(c->client_sigalgs);
374 if (c->shared_sigalgs)
375 OPENSSL_free(c->shared_sigalgs);
David Benjamin676d1e72014-07-08 14:34:10 -0400376 if (c->client_certificate_types)
377 OPENSSL_free(c->client_certificate_types);
Adam Langley95c29f32014-06-20 12:00:00 -0700378 if (c->verify_store)
379 X509_STORE_free(c->verify_store);
380 if (c->chain_store)
381 X509_STORE_free(c->chain_store);
382 if (c->ciphers_raw)
383 OPENSSL_free(c->ciphers_raw);
384 OPENSSL_free(c);
385 }
386
387int ssl_cert_inst(CERT **o)
388 {
389 /* Create a CERT if there isn't already one
390 * (which cannot really happen, as it is initially created in
391 * SSL_CTX_new; but the earlier code usually allows for that one
392 * being non-existant, so we follow that behaviour, as it might
393 * turn out that there actually is a reason for it -- but I'm
394 * not sure that *all* of the existing code could cope with
395 * s->cert being NULL, otherwise we could do without the
396 * initialization in SSL_CTX_new).
397 */
398
399 if (o == NULL)
400 {
401 OPENSSL_PUT_ERROR(SSL, ssl_cert_inst, ERR_R_PASSED_NULL_PARAMETER);
402 return(0);
403 }
404 if (*o == NULL)
405 {
406 if ((*o = ssl_cert_new()) == NULL)
407 {
408 OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
409 return(0);
410 }
411 }
412 return(1);
413 }
414
415int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
416 {
417 CERT_PKEY *cpk = c->key;
418 if (!cpk)
419 return 0;
420 if (cpk->chain)
421 sk_X509_pop_free(cpk->chain, X509_free);
422 cpk->chain = chain;
423 return 1;
424 }
425
426int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
427 {
428 STACK_OF(X509) *dchain;
429 if (!chain)
430 return ssl_cert_set0_chain(c, NULL);
431 dchain = X509_chain_up_ref(chain);
432 if (!dchain)
433 return 0;
434 if (!ssl_cert_set0_chain(c, dchain))
435 {
436 sk_X509_pop_free(dchain, X509_free);
437 return 0;
438 }
439 return 1;
440 }
441
442int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
443 {
444 CERT_PKEY *cpk = c->key;
445 if (!cpk)
446 return 0;
447 if (!cpk->chain)
448 cpk->chain = sk_X509_new_null();
449 if (!cpk->chain || !sk_X509_push(cpk->chain, x))
450 return 0;
451 return 1;
452 }
453
454int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
455 {
456 if (!ssl_cert_add0_chain_cert(c, x))
457 return 0;
David Benjamin150c6172014-08-05 22:22:49 -0400458 X509_up_ref(x);
Adam Langley95c29f32014-06-20 12:00:00 -0700459 return 1;
460 }
461
462int ssl_cert_select_current(CERT *c, X509 *x)
463 {
464 int i;
465 if (x == NULL)
466 return 0;
467 for (i = 0; i < SSL_PKEY_NUM; i++)
468 {
469 if (c->pkeys[i].x509 == x)
470 {
471 c->key = &c->pkeys[i];
472 return 1;
473 }
474 }
475
476 for (i = 0; i < SSL_PKEY_NUM; i++)
477 {
478 if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x))
479 {
480 c->key = &c->pkeys[i];
481 return 1;
482 }
483 }
484 return 0;
485 }
486
487void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg)
488 {
489 c->cert_cb = cb;
490 c->cert_cb_arg = arg;
491 }
492
493SESS_CERT *ssl_sess_cert_new(void)
494 {
495 SESS_CERT *ret;
496
497 ret = OPENSSL_malloc(sizeof *ret);
498 if (ret == NULL)
499 {
500 OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
501 return NULL;
502 }
503
504 memset(ret, 0 ,sizeof *ret);
505 ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
Adam Langley95c29f32014-06-20 12:00:00 -0700506
507 return ret;
508 }
509
510void ssl_sess_cert_free(SESS_CERT *sc)
511 {
512 int i;
513
514 if (sc == NULL)
515 return;
516
Adam Langley95c29f32014-06-20 12:00:00 -0700517 if (sc->cert_chain != NULL)
518 sk_X509_pop_free(sc->cert_chain, X509_free);
519 for (i = 0; i < SSL_PKEY_NUM; i++)
520 {
521 if (sc->peer_pkeys[i].x509 != NULL)
522 X509_free(sc->peer_pkeys[i].x509);
523#if 0 /* We don't have the peer's private key. These lines are just
524 * here as a reminder that we're still using a not-quite-appropriate
525 * data structure. */
526 if (sc->peer_pkeys[i].privatekey != NULL)
527 EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
528#endif
529 }
530
Adam Langley95c29f32014-06-20 12:00:00 -0700531 if (sc->peer_dh_tmp != NULL)
532 DH_free(sc->peer_dh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700533 if (sc->peer_ecdh_tmp != NULL)
534 EC_KEY_free(sc->peer_ecdh_tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700535
536 OPENSSL_free(sc);
537 }
538
539int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
540 {
541 sc->peer_cert_type = type;
542 return(1);
543 }
544
Adam Langley95c29f32014-06-20 12:00:00 -0700545int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
546 {
547 X509 *x;
548 int i;
549 X509_STORE *verify_store;
550 X509_STORE_CTX ctx;
551
552 if (s->cert->verify_store)
553 verify_store = s->cert->verify_store;
554 else
555 verify_store = s->ctx->cert_store;
556
557 if ((sk == NULL) || (sk_X509_num(sk) == 0))
558 return(0);
559
560 x=sk_X509_value(sk,0);
561 if(!X509_STORE_CTX_init(&ctx,verify_store,x,sk))
562 {
563 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
564 return(0);
565 }
Adam Langley95c29f32014-06-20 12:00:00 -0700566#if 0
567 if (SSL_get_verify_depth(s) >= 0)
568 X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
569#endif
570 X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
571
572 /* We need to inherit the verify parameters. These can be determined by
573 * the context: if its a server it will verify SSL client certificates
574 * or vice versa.
575 */
576
577 X509_STORE_CTX_set_default(&ctx,
578 s->server ? "ssl_client" : "ssl_server");
579 /* Anything non-default in "param" should overwrite anything in the
580 * ctx.
581 */
582 X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
583
Adam Langley95c29f32014-06-20 12:00:00 -0700584 if (s->verify_callback)
585 X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
Adam Langley95c29f32014-06-20 12:00:00 -0700586
587 if (s->ctx->app_verify_callback != NULL)
588#if 1 /* new with OpenSSL 0.9.7 */
589 i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
590#else
591 i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
592#endif
593 else
594 {
595#ifndef OPENSSL_NO_X509_VERIFY
596 i=X509_verify_cert(&ctx);
597#else
598 i=0;
599 ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
600 OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, SSL_R_NO_VERIFY_CALLBACK);
601#endif
602 }
603
604 s->verify_result=ctx.error;
605 X509_STORE_CTX_cleanup(&ctx);
606
607 return(i);
608 }
609
610static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
611 {
612 if (*ca_list != NULL)
613 sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
614
615 *ca_list=name_list;
616 }
617
618STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
619 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400620 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700621 STACK_OF(X509_NAME) *ret;
622 X509_NAME *name;
623
624 ret=sk_X509_NAME_new_null();
625 for (i=0; i<sk_X509_NAME_num(sk); i++)
626 {
627 name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
628 if ((name == NULL) || !sk_X509_NAME_push(ret,name))
629 {
630 sk_X509_NAME_pop_free(ret,X509_NAME_free);
631 return(NULL);
632 }
633 }
634 return(ret);
635 }
636
637void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
638 {
639 set_client_CA_list(&(s->client_CA),name_list);
640 }
641
642void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
643 {
644 set_client_CA_list(&(ctx->client_CA),name_list);
645 }
646
647STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
648 {
649 return(ctx->client_CA);
650 }
651
652STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
653 {
David Benjamin63246e82014-11-23 16:23:35 -0500654 if (s->server)
Adam Langley95c29f32014-06-20 12:00:00 -0700655 {
656 if (s->client_CA != NULL)
657 return(s->client_CA);
658 else
659 return(s->ctx->client_CA);
660 }
David Benjamin63246e82014-11-23 16:23:35 -0500661 else
662 {
663 if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
664 (s->s3 != NULL))
665 return(s->s3->tmp.ca_names);
666 else
667 return(NULL);
668 }
Adam Langley95c29f32014-06-20 12:00:00 -0700669 }
670
671static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
672 {
673 X509_NAME *name;
674
675 if (x == NULL) return(0);
676 if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
677 return(0);
678
679 if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
680 return(0);
681
682 if (!sk_X509_NAME_push(*sk,name))
683 {
684 X509_NAME_free(name);
685 return(0);
686 }
687 return(1);
688 }
689
690int SSL_add_client_CA(SSL *ssl,X509 *x)
691 {
692 return(add_client_CA(&(ssl->client_CA),x));
693 }
694
695int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
696 {
697 return(add_client_CA(&(ctx->client_CA),x));
698 }
699
Adam Langley95c29f32014-06-20 12:00:00 -0700700static int xname_cmp(const X509_NAME **a, const X509_NAME **b)
701 {
702 return(X509_NAME_cmp(*a,*b));
703 }
704
705#ifndef OPENSSL_NO_STDIO
706/*!
707 * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
708 * it doesn't really have anything to do with clients (except that a common use
709 * for a stack of CAs is to send it to the client). Actually, it doesn't have
710 * much to do with CAs, either, since it will load any old cert.
711 * \param file the file containing one or more certs.
712 * \return a ::STACK containing the certs.
713 */
714STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
715 {
716 BIO *in;
717 X509 *x=NULL;
718 X509_NAME *xn=NULL;
719 STACK_OF(X509_NAME) *ret = NULL,*sk;
720
721 sk=sk_X509_NAME_new(xname_cmp);
722
723 in=BIO_new(BIO_s_file());
724
725 if ((sk == NULL) || (in == NULL))
726 {
727 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
728 goto err;
729 }
730
731 if (!BIO_read_filename(in,file))
732 goto err;
733
734 for (;;)
735 {
736 if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
737 break;
738 if (ret == NULL)
739 {
740 ret = sk_X509_NAME_new_null();
741 if (ret == NULL)
742 {
743 OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
744 goto err;
745 }
746 }
747 if ((xn=X509_get_subject_name(x)) == NULL) goto err;
748 /* check for duplicates */
749 xn=X509_NAME_dup(xn);
750 if (xn == NULL) goto err;
751 if (sk_X509_NAME_find(sk, NULL, xn))
752 X509_NAME_free(xn);
753 else
754 {
755 sk_X509_NAME_push(sk,xn);
756 sk_X509_NAME_push(ret,xn);
757 }
758 }
759
760 if (0)
761 {
762err:
763 if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
764 ret=NULL;
765 }
766 if (sk != NULL) sk_X509_NAME_free(sk);
767 if (in != NULL) BIO_free(in);
768 if (x != NULL) X509_free(x);
769 if (ret != NULL)
770 ERR_clear_error();
771 return(ret);
772 }
773#endif
774
775/*!
776 * Add a file of certs to a stack.
777 * \param stack the stack to add to.
778 * \param file the file to add from. All certs in this file that are not
779 * already in the stack will be added.
780 * \return 1 for success, 0 for failure. Note that in the case of failure some
781 * certs may have been added to \c stack.
782 */
783
784int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
785 const char *file)
786 {
787 BIO *in;
788 X509 *x=NULL;
789 X509_NAME *xn=NULL;
790 int ret=1;
791 int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
792
793 oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
794
795 in=BIO_new(BIO_s_file());
796
797 if (in == NULL)
798 {
799 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_MALLOC_FAILURE);
800 goto err;
801 }
802
803 if (!BIO_read_filename(in,file))
804 goto err;
805
806 for (;;)
807 {
808 if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
809 break;
810 if ((xn=X509_get_subject_name(x)) == NULL) goto err;
811 xn=X509_NAME_dup(xn);
812 if (xn == NULL) goto err;
813 if (sk_X509_NAME_find(stack, NULL, xn))
814 X509_NAME_free(xn);
815 else
816 sk_X509_NAME_push(stack,xn);
817 }
818
819 ERR_clear_error();
820
821 if (0)
822 {
823err:
824 ret=0;
825 }
826 if(in != NULL)
827 BIO_free(in);
828 if(x != NULL)
829 X509_free(x);
830
831 (void)sk_X509_NAME_set_cmp_func(stack,oldcmp);
832
833 return ret;
834 }
835
836/*!
837 * Add a directory of certs to a stack.
838 * \param stack the stack to append to.
839 * \param dir the directory to append from. All files in this directory will be
840 * examined as potential certs. Any that are acceptable to
841 * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
842 * included.
843 * \return 1 for success, 0 for failure. Note that in the case of failure some
844 * certs may have been added to \c stack.
845 */
846
847int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
848 const char *dir)
849 {
850 OPENSSL_DIR_CTX *d = NULL;
851 const char *filename;
852 int ret = 0;
853
854 CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
855
856 /* Note that a side effect is that the CAs will be sorted by name */
857
858 while((filename = OPENSSL_DIR_read(&d, dir)))
859 {
860 char buf[1024];
861 int r;
862
863 if(strlen(dir)+strlen(filename)+2 > sizeof buf)
864 {
865 OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, SSL_R_PATH_TOO_LONG);
866 goto err;
867 }
868
869#ifdef OPENSSL_SYS_VMS
870 r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
871#else
872 r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
873#endif
874 if (r <= 0 || r >= (int)sizeof(buf))
875 goto err;
876 if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
877 goto err;
878 }
879
880 if (errno)
881 {
882 OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_SYS_LIB);
883 ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
884 goto err;
885 }
886
887 ret = 1;
888
889err:
890 if (d) OPENSSL_DIR_end(&d);
891 CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
892 return ret;
893 }
894
895/* Add a certificate to a BUF_MEM structure */
896
897static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
898 {
899 int n;
900 unsigned char *p;
901
902 n=i2d_X509(x,NULL);
903 if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3)))
904 {
905 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
906 return 0;
907 }
908 p=(unsigned char *)&(buf->data[*l]);
909 l2n3(n,p);
910 i2d_X509(x,&p);
911 *l+=n+3;
912
913 return 1;
914 }
915
Adam Langley24819752014-12-15 18:42:07 -0800916/* Add certificate chain to internal SSL BUF_MEM structure. */
Adam Langley95c29f32014-06-20 12:00:00 -0700917int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
918 {
919 BUF_MEM *buf = s->init_buf;
920 int no_chain;
David Benjaminfb3ff2c2014-09-30 21:00:38 -0400921 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700922
923 X509 *x;
924 STACK_OF(X509) *extra_certs;
925 X509_STORE *chain_store;
926
927 if (cpk)
928 x = cpk->x509;
929 else
930 x = NULL;
931
932 if (s->cert->chain_store)
933 chain_store = s->cert->chain_store;
934 else
935 chain_store = s->ctx->cert_store;
936
937 /* If we have a certificate specific chain use it, else use
938 * parent ctx.
939 */
940 if (cpk && cpk->chain)
941 extra_certs = cpk->chain;
942 else
943 extra_certs = s->ctx->extra_certs;
944
945 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
946 no_chain = 1;
947 else
948 no_chain = 0;
949
950 /* TLSv1 sends a chain with nothing in it, instead of an alert */
951 if (!BUF_MEM_grow_clean(buf,10))
952 {
953 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
954 return 0;
955 }
956 if (x != NULL)
957 {
958 if (no_chain)
959 {
960 if (!ssl_add_cert_to_buf(buf, l, x))
961 return 0;
962 }
963 else
964 {
965 X509_STORE_CTX xs_ctx;
966
967 if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL))
968 {
969 OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
970 return(0);
971 }
972 X509_verify_cert(&xs_ctx);
973 /* Don't leave errors in the queue */
974 ERR_clear_error();
975 for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
976 {
977 x = sk_X509_value(xs_ctx.chain, i);
978
979 if (!ssl_add_cert_to_buf(buf, l, x))
980 {
981 X509_STORE_CTX_cleanup(&xs_ctx);
982 return 0;
983 }
984 }
985 X509_STORE_CTX_cleanup(&xs_ctx);
986 }
987 }
988 for (i=0; i<sk_X509_num(extra_certs); i++)
989 {
990 x=sk_X509_value(extra_certs,i);
991 if (!ssl_add_cert_to_buf(buf, l, x))
992 return 0;
993 }
994
995 return 1;
996 }
997
998/* Build a certificate chain for current certificate */
999int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
1000 {
1001 CERT_PKEY *cpk = c->key;
1002 X509_STORE_CTX xs_ctx;
1003 STACK_OF(X509) *chain = NULL, *untrusted = NULL;
1004 X509 *x;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001005 int i, rv = 0;
Adam Langleydff7b9e2014-06-20 12:00:00 -07001006 unsigned long error;
Adam Langley95c29f32014-06-20 12:00:00 -07001007
1008 if (!cpk->x509)
1009 {
1010 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001011 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001012 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001013 /* Rearranging and check the chain: add everything to a store */
1014 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1015 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001016 size_t j;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001017 chain_store = X509_STORE_new();
1018 if (!chain_store)
1019 goto err;
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001020 for (j = 0; j < sk_X509_num(cpk->chain); j++)
Adam Langleyf669c2d2014-06-20 12:00:00 -07001021 {
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001022 x = sk_X509_value(cpk->chain, j);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001023 if (!X509_STORE_add_cert(chain_store, x))
Adam Langleydff7b9e2014-06-20 12:00:00 -07001024 {
1025 error = ERR_peek_last_error();
1026 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1027 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1028 goto err;
1029 ERR_clear_error();
1030 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001031 }
1032 /* Add EE cert too: it might be self signed */
1033 if (!X509_STORE_add_cert(chain_store, cpk->x509))
Adam Langleydff7b9e2014-06-20 12:00:00 -07001034 {
1035 error = ERR_peek_last_error();
1036 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1037 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1038 goto err;
1039 ERR_clear_error();
1040 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001041 }
1042 else
1043 {
1044 if (c->chain_store)
1045 chain_store = c->chain_store;
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyf669c2d2014-06-20 12:00:00 -07001047 if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
1048 untrusted = cpk->chain;
1049 }
Adam Langley95c29f32014-06-20 12:00:00 -07001050
1051 if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted))
1052 {
1053 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001054 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001055 }
Adam Langley95c29f32014-06-20 12:00:00 -07001056
1057 i = X509_verify_cert(&xs_ctx);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001058 if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)
1059 {
Adam Langley64c22232014-06-20 12:00:00 -07001060 if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
1061 ERR_clear_error();
Adam Langleyf669c2d2014-06-20 12:00:00 -07001062 i = 1;
Adam Langley64c22232014-06-20 12:00:00 -07001063 rv = 2;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001064 }
Adam Langley95c29f32014-06-20 12:00:00 -07001065 if (i > 0)
1066 chain = X509_STORE_CTX_get1_chain(&xs_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001067 if (i <= 0)
1068 {
1069 OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_CERTIFICATE_VERIFY_FAILED);
Adam Langleyf669c2d2014-06-20 12:00:00 -07001070 i = X509_STORE_CTX_get_error(&xs_ctx);
1071 ERR_add_error_data(2, "Verify error:",
1072 X509_verify_cert_error_string(i));
1073
1074 X509_STORE_CTX_cleanup(&xs_ctx);
1075 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001076 }
Adam Langleyf669c2d2014-06-20 12:00:00 -07001077 X509_STORE_CTX_cleanup(&xs_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -07001078 if (cpk->chain)
1079 sk_X509_pop_free(cpk->chain, X509_free);
1080 /* Remove EE certificate from chain */
1081 x = sk_X509_shift(chain);
1082 X509_free(x);
1083 if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT)
1084 {
Adam Langleyf669c2d2014-06-20 12:00:00 -07001085 if (sk_X509_num(chain) > 0)
1086 {
1087 /* See if last cert is self signed */
1088 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1089 X509_check_purpose(x, -1, 0);
1090 if (x->ex_flags & EXFLAG_SS)
1091 {
1092 x = sk_X509_pop(chain);
1093 X509_free(x);
1094 }
1095 }
Adam Langley95c29f32014-06-20 12:00:00 -07001096 }
1097 cpk->chain = chain;
Adam Langley64c22232014-06-20 12:00:00 -07001098 if (rv == 0)
1099 rv = 1;
Adam Langleyf669c2d2014-06-20 12:00:00 -07001100 err:
1101 if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1102 X509_STORE_free(chain_store);
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyf669c2d2014-06-20 12:00:00 -07001104 return rv;
Adam Langley95c29f32014-06-20 12:00:00 -07001105 }
1106
1107int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1108 {
1109 X509_STORE **pstore;
1110 if (chain)
1111 pstore = &c->chain_store;
1112 else
1113 pstore = &c->verify_store;
1114 if (*pstore)
1115 X509_STORE_free(*pstore);
1116 *pstore = store;
1117 if (ref && store)
David Benjamin67454b62014-08-06 22:15:46 -04001118 CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
Adam Langley95c29f32014-06-20 12:00:00 -07001119 return 1;
1120 }
1121