]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * ! \file ssl/ssl_cert.c | |
3 | */ | |
58964a49 | 4 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
5 | * All rights reserved. |
6 | * | |
7 | * This package is an SSL implementation written | |
8 | * by Eric Young (eay@cryptsoft.com). | |
9 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 10 | * |
d02b48c6 RE |
11 | * This library is free for commercial and non-commercial use as long as |
12 | * the following conditions are aheared to. The following conditions | |
13 | * apply to all code found in this distribution, be it the RC4, RSA, | |
14 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
15 | * included with this distribution is covered by the same copyright terms | |
16 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 17 | * |
d02b48c6 RE |
18 | * Copyright remains Eric Young's, and as such any Copyright notices in |
19 | * the code are not to be removed. | |
20 | * If this package is used in a product, Eric Young should be given attribution | |
21 | * as the author of the parts of the library used. | |
22 | * This can be in the form of a textual message at program startup or | |
23 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 24 | * |
d02b48c6 RE |
25 | * Redistribution and use in source and binary forms, with or without |
26 | * modification, are permitted provided that the following conditions | |
27 | * are met: | |
28 | * 1. Redistributions of source code must retain the copyright | |
29 | * notice, this list of conditions and the following disclaimer. | |
30 | * 2. Redistributions in binary form must reproduce the above copyright | |
31 | * notice, this list of conditions and the following disclaimer in the | |
32 | * documentation and/or other materials provided with the distribution. | |
33 | * 3. All advertising materials mentioning features or use of this software | |
34 | * must display the following acknowledgement: | |
35 | * "This product includes cryptographic software written by | |
36 | * Eric Young (eay@cryptsoft.com)" | |
37 | * The word 'cryptographic' can be left out if the rouines from the library | |
38 | * being used are not cryptographic related :-). | |
0f113f3e | 39 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
40 | * the apps directory (application code) you must include an acknowledgement: |
41 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 42 | * |
d02b48c6 RE |
43 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
53 | * SUCH DAMAGE. | |
0f113f3e | 54 | * |
d02b48c6 RE |
55 | * The licence and distribution terms for any publically available version or |
56 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
57 | * copied and put under another distribution licence | |
58 | * [including the GNU Public Licence.] | |
59 | */ | |
ca8e5b9b | 60 | /* ==================================================================== |
52b8dad8 | 61 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
ca8e5b9b BM |
62 | * |
63 | * Redistribution and use in source and binary forms, with or without | |
64 | * modification, are permitted provided that the following conditions | |
65 | * are met: | |
66 | * | |
67 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 68 | * notice, this list of conditions and the following disclaimer. |
ca8e5b9b BM |
69 | * |
70 | * 2. Redistributions in binary form must reproduce the above copyright | |
71 | * notice, this list of conditions and the following disclaimer in | |
72 | * the documentation and/or other materials provided with the | |
73 | * distribution. | |
74 | * | |
75 | * 3. All advertising materials mentioning features or use of this | |
76 | * software must display the following acknowledgment: | |
77 | * "This product includes software developed by the OpenSSL Project | |
675f605d | 78 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
ca8e5b9b BM |
79 | * |
80 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
81 | * endorse or promote products derived from this software without | |
82 | * prior written permission. For written permission, please contact | |
675f605d | 83 | * openssl-core@openssl.org. |
ca8e5b9b BM |
84 | * |
85 | * 5. Products derived from this software may not be called "OpenSSL" | |
86 | * nor may "OpenSSL" appear in their names without prior written | |
87 | * permission of the OpenSSL Project. | |
88 | * | |
89 | * 6. Redistributions of any form whatsoever must retain the following | |
90 | * acknowledgment: | |
91 | * "This product includes software developed by the OpenSSL Project | |
675f605d | 92 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
ca8e5b9b BM |
93 | * |
94 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
95 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
96 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
97 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
98 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
99 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
100 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
101 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
103 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
104 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
105 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
106 | * ==================================================================== | |
675f605d BM |
107 | * |
108 | * This product includes cryptographic software written by Eric Young | |
109 | * (eay@cryptsoft.com). This product includes software written by Tim | |
110 | * Hudson (tjh@cryptsoft.com). | |
111 | * | |
ca8e5b9b | 112 | */ |
ea262260 BM |
113 | /* ==================================================================== |
114 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
0f113f3e | 115 | * ECC cipher suite support in OpenSSL originally developed by |
ea262260 BM |
116 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
117 | */ | |
d02b48c6 RE |
118 | |
119 | #include <stdio.h> | |
17f389bb | 120 | |
41d2a336 | 121 | #include "e_os.h" |
17f389bb AP |
122 | #ifndef NO_SYS_TYPES_H |
123 | # include <sys/types.h> | |
124 | #endif | |
125 | ||
4083a229 | 126 | #include "o_dir.h" |
ec577822 BM |
127 | #include <openssl/objects.h> |
128 | #include <openssl/bio.h> | |
129 | #include <openssl/pem.h> | |
bb7cd4e3 | 130 | #include <openssl/x509v3.h> |
3eeaab4b | 131 | #ifndef OPENSSL_NO_DH |
0f113f3e | 132 | # include <openssl/dh.h> |
3eeaab4b | 133 | #endif |
d095b68d | 134 | #include <openssl/bn.h> |
d02b48c6 RE |
135 | #include "ssl_locl.h" |
136 | ||
0f113f3e MC |
137 | static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op, |
138 | int bits, int nid, void *other, | |
139 | void *ex); | |
b362ccab | 140 | |
6b691a5c | 141 | int SSL_get_ex_data_X509_STORE_CTX_idx(void) |
0f113f3e MC |
142 | { |
143 | static volatile int ssl_x509_store_ctx_idx = -1; | |
144 | int got_write_lock = 0; | |
145 | ||
146 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); | |
147 | ||
148 | if (ssl_x509_store_ctx_idx < 0) { | |
149 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); | |
150 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); | |
151 | got_write_lock = 1; | |
152 | ||
153 | if (ssl_x509_store_ctx_idx < 0) { | |
154 | ssl_x509_store_ctx_idx = | |
155 | X509_STORE_CTX_get_ex_new_index(0, "SSL for verify callback", | |
156 | NULL, NULL, NULL); | |
157 | } | |
158 | } | |
159 | ||
160 | if (got_write_lock) | |
161 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); | |
162 | else | |
163 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); | |
164 | ||
165 | return ssl_x509_store_ctx_idx; | |
166 | } | |
dfeab068 | 167 | |
4453cd8c | 168 | void ssl_cert_set_default_md(CERT *cert) |
0f113f3e MC |
169 | { |
170 | /* Set digest values to defaults */ | |
6b7be581 | 171 | #ifndef OPENSSL_NO_DSA |
0f113f3e | 172 | cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1(); |
6b7be581 DSH |
173 | #endif |
174 | #ifndef OPENSSL_NO_RSA | |
0f113f3e MC |
175 | cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); |
176 | cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); | |
6b7be581 | 177 | #endif |
10bf4fc2 | 178 | #ifndef OPENSSL_NO_EC |
0f113f3e | 179 | cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); |
6b7be581 | 180 | #endif |
0f113f3e | 181 | } |
6b7be581 | 182 | |
6b691a5c | 183 | CERT *ssl_cert_new(void) |
0f113f3e | 184 | { |
b4faea50 | 185 | CERT *ret = OPENSSL_malloc(sizeof(*ret)); |
0f113f3e | 186 | |
0f113f3e MC |
187 | if (ret == NULL) { |
188 | SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE); | |
189 | return (NULL); | |
190 | } | |
16f8d4eb | 191 | memset(ret, 0, sizeof(*ret)); |
0f113f3e MC |
192 | |
193 | ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]); | |
194 | ret->references = 1; | |
195 | ssl_cert_set_default_md(ret); | |
196 | ret->sec_cb = ssl_security_default_callback; | |
197 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL; | |
198 | ret->sec_ex = NULL; | |
199 | return (ret); | |
200 | } | |
d02b48c6 | 201 | |
ca8e5b9b | 202 | CERT *ssl_cert_dup(CERT *cert) |
0f113f3e | 203 | { |
b4faea50 | 204 | CERT *ret = OPENSSL_malloc(sizeof(*ret)); |
0f113f3e MC |
205 | int i; |
206 | ||
0f113f3e MC |
207 | if (ret == NULL) { |
208 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); | |
209 | return (NULL); | |
210 | } | |
211 | ||
16f8d4eb | 212 | memset(ret, 0, sizeof(*ret)); |
0f113f3e | 213 | |
16f8d4eb | 214 | ret->key = &ret->pkeys[cert->key - cert->pkeys]; |
0f113f3e MC |
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; | |
ca8e5b9b | 220 | |
bc36ee62 | 221 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
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; | |
ca8e5b9b BM |
227 | #endif |
228 | ||
bc36ee62 | 229 | #ifndef OPENSSL_NO_DH |
0f113f3e MC |
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 | ret->dh_tmp_auto = cert->dh_tmp_auto; | |
ca8e5b9b BM |
255 | #endif |
256 | ||
10bf4fc2 | 257 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
258 | if (cert->ecdh_tmp) { |
259 | ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp); | |
260 | if (ret->ecdh_tmp == NULL) { | |
261 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB); | |
262 | goto err; | |
263 | } | |
264 | } | |
265 | ret->ecdh_tmp_cb = cert->ecdh_tmp_cb; | |
266 | ret->ecdh_tmp_auto = cert->ecdh_tmp_auto; | |
ea262260 BM |
267 | #endif |
268 | ||
0f113f3e MC |
269 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
270 | CERT_PKEY *cpk = cert->pkeys + i; | |
271 | CERT_PKEY *rpk = ret->pkeys + i; | |
272 | if (cpk->x509 != NULL) { | |
273 | rpk->x509 = cpk->x509; | |
274 | CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509); | |
275 | } | |
276 | ||
277 | if (cpk->privatekey != NULL) { | |
278 | rpk->privatekey = cpk->privatekey; | |
279 | CRYPTO_add(&cpk->privatekey->references, 1, CRYPTO_LOCK_EVP_PKEY); | |
280 | } | |
281 | ||
282 | if (cpk->chain) { | |
283 | rpk->chain = X509_chain_up_ref(cpk->chain); | |
284 | if (!rpk->chain) { | |
285 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); | |
286 | goto err; | |
287 | } | |
288 | } | |
289 | rpk->valid_flags = 0; | |
a398f821 | 290 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
291 | if (cert->pkeys[i].serverinfo != NULL) { |
292 | /* Just copy everything. */ | |
293 | ret->pkeys[i].serverinfo = | |
294 | OPENSSL_malloc(cert->pkeys[i].serverinfo_length); | |
295 | if (ret->pkeys[i].serverinfo == NULL) { | |
296 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); | |
297 | goto err; | |
298 | } | |
299 | ret->pkeys[i].serverinfo_length = | |
300 | cert->pkeys[i].serverinfo_length; | |
301 | memcpy(ret->pkeys[i].serverinfo, | |
302 | cert->pkeys[i].serverinfo, | |
303 | cert->pkeys[i].serverinfo_length); | |
304 | } | |
a398f821 | 305 | #endif |
0f113f3e MC |
306 | } |
307 | ||
308 | ret->references = 1; | |
309 | /* | |
310 | * Set digests to defaults. NB: we don't copy existing values as they | |
311 | * will be set during handshake. | |
312 | */ | |
313 | ssl_cert_set_default_md(ret); | |
314 | /* Peer sigalgs set to NULL as we get these from handshake too */ | |
315 | ret->peer_sigalgs = NULL; | |
316 | ret->peer_sigalgslen = 0; | |
317 | /* Configured sigalgs however we copy across */ | |
318 | ||
319 | if (cert->conf_sigalgs) { | |
320 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); | |
321 | if (!ret->conf_sigalgs) | |
322 | goto err; | |
323 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen); | |
324 | ret->conf_sigalgslen = cert->conf_sigalgslen; | |
325 | } else | |
326 | ret->conf_sigalgs = NULL; | |
327 | ||
328 | if (cert->client_sigalgs) { | |
329 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); | |
330 | if (!ret->client_sigalgs) | |
331 | goto err; | |
332 | memcpy(ret->client_sigalgs, cert->client_sigalgs, | |
333 | cert->client_sigalgslen); | |
334 | ret->client_sigalgslen = cert->client_sigalgslen; | |
335 | } else | |
336 | ret->client_sigalgs = NULL; | |
337 | /* Shared sigalgs also NULL */ | |
338 | ret->shared_sigalgs = NULL; | |
339 | /* Copy any custom client certificate types */ | |
340 | if (cert->ctypes) { | |
341 | ret->ctypes = OPENSSL_malloc(cert->ctype_num); | |
342 | if (!ret->ctypes) | |
343 | goto err; | |
344 | memcpy(ret->ctypes, cert->ctypes, cert->ctype_num); | |
345 | ret->ctype_num = cert->ctype_num; | |
346 | } | |
347 | ||
348 | ret->cert_flags = cert->cert_flags; | |
349 | ||
350 | ret->cert_cb = cert->cert_cb; | |
351 | ret->cert_cb_arg = cert->cert_cb_arg; | |
352 | ||
353 | if (cert->verify_store) { | |
354 | CRYPTO_add(&cert->verify_store->references, 1, | |
355 | CRYPTO_LOCK_X509_STORE); | |
356 | ret->verify_store = cert->verify_store; | |
357 | } | |
358 | ||
359 | if (cert->chain_store) { | |
360 | CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE); | |
361 | ret->chain_store = cert->chain_store; | |
362 | } | |
363 | ||
364 | ret->ciphers_raw = NULL; | |
365 | ||
366 | ret->sec_cb = cert->sec_cb; | |
367 | ret->sec_level = cert->sec_level; | |
368 | ret->sec_ex = cert->sec_ex; | |
b362ccab | 369 | |
b83294fe | 370 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
371 | if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext)) |
372 | goto err; | |
373 | if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext)) | |
374 | goto err; | |
b83294fe DSH |
375 | #endif |
376 | ||
0f113f3e MC |
377 | return (ret); |
378 | ||
379 | err: | |
380 | ssl_cert_free(ret); | |
9ade64de | 381 | |
0f113f3e MC |
382 | return NULL; |
383 | } | |
ca8e5b9b | 384 | |
a5ee80b9 DSH |
385 | /* Free up and clear all certificates and chains */ |
386 | ||
387 | void ssl_cert_clear_certs(CERT *c) | |
0f113f3e MC |
388 | { |
389 | int i; | |
390 | if (c == NULL) | |
391 | return; | |
392 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
393 | CERT_PKEY *cpk = c->pkeys + i; | |
222561fe RS |
394 | X509_free(cpk->x509); |
395 | cpk->x509 = NULL; | |
c5ba2d99 RS |
396 | EVP_PKEY_free(cpk->privatekey); |
397 | cpk->privatekey = NULL; | |
222561fe RS |
398 | sk_X509_pop_free(cpk->chain, X509_free); |
399 | cpk->chain = NULL; | |
a5ee80b9 | 400 | #ifndef OPENSSL_NO_TLSEXT |
25aaa98a RS |
401 | OPENSSL_free(cpk->serverinfo); |
402 | cpk->serverinfo = NULL; | |
403 | cpk->serverinfo_length = 0; | |
a5ee80b9 | 404 | #endif |
0f113f3e MC |
405 | /* Clear all flags apart from explicit sign */ |
406 | cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN; | |
407 | } | |
408 | } | |
ca8e5b9b | 409 | |
eb90a483 | 410 | void ssl_cert_free(CERT *c) |
0f113f3e MC |
411 | { |
412 | int i; | |
d02b48c6 | 413 | |
0f113f3e MC |
414 | if (c == NULL) |
415 | return; | |
e03ddfae | 416 | |
0f113f3e | 417 | i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT); |
58964a49 | 418 | #ifdef REF_PRINT |
0f113f3e | 419 | REF_PRINT("CERT", c); |
58964a49 | 420 | #endif |
0f113f3e MC |
421 | if (i > 0) |
422 | return; | |
d02b48c6 | 423 | #ifdef REF_CHECK |
0f113f3e MC |
424 | if (i < 0) { |
425 | fprintf(stderr, "ssl_cert_free, bad reference count\n"); | |
426 | abort(); /* ok */ | |
427 | } | |
d02b48c6 RE |
428 | #endif |
429 | ||
bc36ee62 | 430 | #ifndef OPENSSL_NO_RSA |
d6407083 | 431 | RSA_free(c->rsa_tmp); |
d02b48c6 | 432 | #endif |
bc36ee62 | 433 | #ifndef OPENSSL_NO_DH |
d6407083 | 434 | DH_free(c->dh_tmp); |
d02b48c6 | 435 | #endif |
10bf4fc2 | 436 | #ifndef OPENSSL_NO_EC |
8fdc3734 | 437 | EC_KEY_free(c->ecdh_tmp); |
ea262260 | 438 | #endif |
d02b48c6 | 439 | |
0f113f3e | 440 | ssl_cert_clear_certs(c); |
25aaa98a RS |
441 | OPENSSL_free(c->peer_sigalgs); |
442 | OPENSSL_free(c->conf_sigalgs); | |
443 | OPENSSL_free(c->client_sigalgs); | |
444 | OPENSSL_free(c->shared_sigalgs); | |
445 | OPENSSL_free(c->ctypes); | |
222561fe RS |
446 | X509_STORE_free(c->verify_store); |
447 | X509_STORE_free(c->chain_store); | |
25aaa98a | 448 | OPENSSL_free(c->ciphers_raw); |
b83294fe | 449 | #ifndef OPENSSL_NO_TLSEXT |
0f113f3e MC |
450 | custom_exts_free(&c->cli_ext); |
451 | custom_exts_free(&c->srv_ext); | |
b83294fe | 452 | #endif |
4b45c6e5 RS |
453 | OPENSSL_clear_free(c->pms, c->pmslen); |
454 | c->pms = NULL; | |
0f113f3e MC |
455 | OPENSSL_free(c); |
456 | } | |
d02b48c6 | 457 | |
b362ccab | 458 | int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
0f113f3e MC |
459 | { |
460 | int i, r; | |
461 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; | |
462 | if (!cpk) | |
463 | return 0; | |
222561fe | 464 | sk_X509_pop_free(cpk->chain, X509_free); |
0f113f3e MC |
465 | for (i = 0; i < sk_X509_num(chain); i++) { |
466 | r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0); | |
467 | if (r != 1) { | |
468 | SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r); | |
469 | return 0; | |
470 | } | |
471 | } | |
472 | cpk->chain = chain; | |
473 | return 1; | |
474 | } | |
f71c6e52 | 475 | |
b362ccab | 476 | int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
0f113f3e MC |
477 | { |
478 | STACK_OF(X509) *dchain; | |
479 | if (!chain) | |
480 | return ssl_cert_set0_chain(s, ctx, NULL); | |
481 | dchain = X509_chain_up_ref(chain); | |
482 | if (!dchain) | |
483 | return 0; | |
484 | if (!ssl_cert_set0_chain(s, ctx, dchain)) { | |
485 | sk_X509_pop_free(dchain, X509_free); | |
486 | return 0; | |
487 | } | |
488 | return 1; | |
489 | } | |
f71c6e52 | 490 | |
b362ccab | 491 | int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
0f113f3e MC |
492 | { |
493 | int r; | |
494 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; | |
495 | if (!cpk) | |
496 | return 0; | |
497 | r = ssl_security_cert(s, ctx, x, 0, 0); | |
498 | if (r != 1) { | |
499 | SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r); | |
500 | return 0; | |
501 | } | |
502 | if (!cpk->chain) | |
503 | cpk->chain = sk_X509_new_null(); | |
504 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) | |
505 | return 0; | |
506 | return 1; | |
507 | } | |
f71c6e52 | 508 | |
b362ccab | 509 | int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
0f113f3e MC |
510 | { |
511 | if (!ssl_cert_add0_chain_cert(s, ctx, x)) | |
512 | return 0; | |
513 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | |
514 | return 1; | |
515 | } | |
7b6b246f RS |
516 | |
517 | int ssl_cert_select_current(CERT *c, X509 *x) | |
0f113f3e MC |
518 | { |
519 | int i; | |
520 | if (x == NULL) | |
521 | return 0; | |
522 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
523 | CERT_PKEY *cpk = c->pkeys + i; | |
524 | if (cpk->x509 == x && cpk->privatekey) { | |
525 | c->key = cpk; | |
526 | return 1; | |
527 | } | |
528 | } | |
529 | ||
530 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
531 | CERT_PKEY *cpk = c->pkeys + i; | |
532 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { | |
533 | c->key = cpk; | |
534 | return 1; | |
535 | } | |
536 | } | |
537 | return 0; | |
538 | } | |
0f78819c DSH |
539 | |
540 | int ssl_cert_set_current(CERT *c, long op) | |
0f113f3e MC |
541 | { |
542 | int i, idx; | |
543 | if (!c) | |
544 | return 0; | |
545 | if (op == SSL_CERT_SET_FIRST) | |
546 | idx = 0; | |
547 | else if (op == SSL_CERT_SET_NEXT) { | |
548 | idx = (int)(c->key - c->pkeys + 1); | |
549 | if (idx >= SSL_PKEY_NUM) | |
550 | return 0; | |
551 | } else | |
552 | return 0; | |
553 | for (i = idx; i < SSL_PKEY_NUM; i++) { | |
554 | CERT_PKEY *cpk = c->pkeys + i; | |
555 | if (cpk->x509 && cpk->privatekey) { | |
556 | c->key = cpk; | |
557 | return 1; | |
558 | } | |
559 | } | |
560 | return 0; | |
561 | } | |
562 | ||
563 | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg) | |
564 | { | |
565 | c->cert_cb = cb; | |
566 | c->cert_cb_arg = arg; | |
567 | } | |
18d71588 | 568 | |
b56bce4f | 569 | SESS_CERT *ssl_sess_cert_new(void) |
0f113f3e MC |
570 | { |
571 | SESS_CERT *ret; | |
b56bce4f | 572 | |
b4faea50 | 573 | ret = OPENSSL_malloc(sizeof(*ret)); |
0f113f3e MC |
574 | if (ret == NULL) { |
575 | SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE); | |
576 | return NULL; | |
577 | } | |
b56bce4f | 578 | |
b4faea50 | 579 | memset(ret, 0, sizeof(*ret)); |
0f113f3e MC |
580 | ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]); |
581 | ret->references = 1; | |
b56bce4f | 582 | |
0f113f3e MC |
583 | return ret; |
584 | } | |
b56bce4f BM |
585 | |
586 | void ssl_sess_cert_free(SESS_CERT *sc) | |
0f113f3e MC |
587 | { |
588 | int i; | |
b56bce4f | 589 | |
0f113f3e MC |
590 | if (sc == NULL) |
591 | return; | |
b56bce4f | 592 | |
0f113f3e | 593 | i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT); |
b56bce4f | 594 | #ifdef REF_PRINT |
0f113f3e | 595 | REF_PRINT("SESS_CERT", sc); |
b56bce4f | 596 | #endif |
0f113f3e MC |
597 | if (i > 0) |
598 | return; | |
b56bce4f | 599 | #ifdef REF_CHECK |
0f113f3e MC |
600 | if (i < 0) { |
601 | fprintf(stderr, "ssl_sess_cert_free, bad reference count\n"); | |
602 | abort(); /* ok */ | |
603 | } | |
ca8e5b9b | 604 | #endif |
15d21c2d | 605 | |
0f113f3e | 606 | /* i == 0 */ |
222561fe | 607 | sk_X509_pop_free(sc->cert_chain, X509_free); |
0f113f3e | 608 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
222561fe | 609 | X509_free(sc->peer_pkeys[i].x509); |
9e9858d1 RS |
610 | #if 0 |
611 | /* | |
25aaa98a | 612 | * We don't have the peer's private key. This line is just |
9e9858d1 RS |
613 | * here as a reminder that we're still using a not-quite-appropriate |
614 | * data structure. | |
615 | */ | |
25aaa98a | 616 | EVP_PKEY_free(sc->peer_pkeys[i].privatekey); |
b56bce4f | 617 | #endif |
0f113f3e | 618 | } |
b56bce4f | 619 | |
bc36ee62 | 620 | #ifndef OPENSSL_NO_RSA |
d6407083 | 621 | RSA_free(sc->peer_rsa_tmp); |
b56bce4f | 622 | #endif |
bc36ee62 | 623 | #ifndef OPENSSL_NO_DH |
d6407083 | 624 | DH_free(sc->peer_dh_tmp); |
b56bce4f | 625 | #endif |
10bf4fc2 | 626 | #ifndef OPENSSL_NO_EC |
8fdc3734 | 627 | EC_KEY_free(sc->peer_ecdh_tmp); |
ea262260 | 628 | #endif |
b56bce4f | 629 | |
0f113f3e MC |
630 | OPENSSL_free(sc); |
631 | } | |
632 | ||
633 | int ssl_set_peer_cert_type(SESS_CERT *sc, int type) | |
634 | { | |
635 | sc->peer_cert_type = type; | |
636 | return (1); | |
637 | } | |
638 | ||
639 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) | |
640 | { | |
641 | X509 *x; | |
642 | int i; | |
643 | X509_STORE *verify_store; | |
644 | X509_STORE_CTX ctx; | |
645 | ||
646 | if (s->cert->verify_store) | |
647 | verify_store = s->cert->verify_store; | |
648 | else | |
649 | verify_store = s->ctx->cert_store; | |
650 | ||
651 | if ((sk == NULL) || (sk_X509_num(sk) == 0)) | |
652 | return (0); | |
653 | ||
654 | x = sk_X509_value(sk, 0); | |
655 | if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) { | |
656 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB); | |
657 | return (0); | |
658 | } | |
659 | /* Set suite B flags if needed */ | |
660 | X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s)); | |
0f113f3e MC |
661 | X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s); |
662 | ||
663 | /* | |
664 | * We need to inherit the verify parameters. These can be determined by | |
665 | * the context: if its a server it will verify SSL client certificates or | |
666 | * vice versa. | |
667 | */ | |
668 | ||
669 | X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server"); | |
670 | /* | |
671 | * Anything non-default in "param" should overwrite anything in the ctx. | |
672 | */ | |
673 | X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param); | |
674 | ||
675 | if (s->verify_callback) | |
676 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); | |
677 | ||
678 | if (s->ctx->app_verify_callback != NULL) | |
0f113f3e | 679 | i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
0f113f3e | 680 | else { |
0f113f3e MC |
681 | i = X509_verify_cert(&ctx); |
682 | # if 0 | |
683 | /* Dummy error calls so mkerr generates them */ | |
684 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); | |
685 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); | |
686 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); | |
687 | # endif | |
688 | if (i > 0) | |
689 | i = ssl_security_cert_chain(s, ctx.chain, NULL, 1); | |
0f113f3e | 690 | } |
d02b48c6 | 691 | |
0f113f3e MC |
692 | s->verify_result = ctx.error; |
693 | X509_STORE_CTX_cleanup(&ctx); | |
d02b48c6 | 694 | |
0f113f3e MC |
695 | return (i); |
696 | } | |
d02b48c6 | 697 | |
0f113f3e MC |
698 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
699 | STACK_OF(X509_NAME) *name_list) | |
700 | { | |
222561fe | 701 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
0f113f3e MC |
702 | *ca_list = name_list; |
703 | } | |
d02b48c6 | 704 | |
838d25a1 | 705 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) |
0f113f3e MC |
706 | { |
707 | int i; | |
708 | STACK_OF(X509_NAME) *ret; | |
709 | X509_NAME *name; | |
710 | ||
711 | ret = sk_X509_NAME_new_null(); | |
712 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
713 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); | |
714 | if ((name == NULL) || !sk_X509_NAME_push(ret, name)) { | |
715 | sk_X509_NAME_pop_free(ret, X509_NAME_free); | |
716 | return (NULL); | |
717 | } | |
718 | } | |
719 | return (ret); | |
720 | } | |
721 | ||
722 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) | |
723 | { | |
724 | set_client_CA_list(&(s->client_CA), name_list); | |
725 | } | |
726 | ||
727 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) | |
728 | { | |
729 | set_client_CA_list(&(ctx->client_CA), name_list); | |
730 | } | |
d02b48c6 | 731 | |
0821bcd4 | 732 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) |
0f113f3e MC |
733 | { |
734 | return (ctx->client_CA); | |
735 | } | |
d02b48c6 | 736 | |
0821bcd4 | 737 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
0f113f3e MC |
738 | { |
739 | if (s->type == SSL_ST_CONNECT) { /* we are in the client */ | |
740 | if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL)) | |
741 | return (s->s3->tmp.ca_names); | |
742 | else | |
743 | return (NULL); | |
744 | } else { | |
745 | if (s->client_CA != NULL) | |
746 | return (s->client_CA); | |
747 | else | |
748 | return (s->ctx->client_CA); | |
749 | } | |
750 | } | |
751 | ||
752 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) | |
753 | { | |
754 | X509_NAME *name; | |
755 | ||
756 | if (x == NULL) | |
757 | return (0); | |
758 | if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL)) | |
759 | return (0); | |
760 | ||
761 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL) | |
762 | return (0); | |
763 | ||
764 | if (!sk_X509_NAME_push(*sk, name)) { | |
765 | X509_NAME_free(name); | |
766 | return (0); | |
767 | } | |
768 | return (1); | |
769 | } | |
770 | ||
771 | int SSL_add_client_CA(SSL *ssl, X509 *x) | |
772 | { | |
773 | return (add_client_CA(&(ssl->client_CA), x)); | |
774 | } | |
775 | ||
776 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) | |
777 | { | |
778 | return (add_client_CA(&(ctx->client_CA), x)); | |
779 | } | |
780 | ||
781 | static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
782 | { | |
783 | return (X509_NAME_cmp(*a, *b)); | |
784 | } | |
d02b48c6 | 785 | |
bc36ee62 | 786 | #ifndef OPENSSL_NO_STDIO |
0f113f3e | 787 | /** |
eb90a483 BL |
788 | * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; |
789 | * it doesn't really have anything to do with clients (except that a common use | |
790 | * for a stack of CAs is to send it to the client). Actually, it doesn't have | |
791 | * much to do with CAs, either, since it will load any old cert. | |
792 | * \param file the file containing one or more certs. | |
793 | * \return a ::STACK containing the certs. | |
794 | */ | |
f73e07cf | 795 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
0f113f3e MC |
796 | { |
797 | BIO *in; | |
798 | X509 *x = NULL; | |
799 | X509_NAME *xn = NULL; | |
800 | STACK_OF(X509_NAME) *ret = NULL, *sk; | |
801 | ||
802 | sk = sk_X509_NAME_new(xname_cmp); | |
803 | ||
804 | in = BIO_new(BIO_s_file_internal()); | |
805 | ||
806 | if ((sk == NULL) || (in == NULL)) { | |
807 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); | |
808 | goto err; | |
809 | } | |
810 | ||
811 | if (!BIO_read_filename(in, file)) | |
812 | goto err; | |
813 | ||
814 | for (;;) { | |
815 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) | |
816 | break; | |
817 | if (ret == NULL) { | |
818 | ret = sk_X509_NAME_new_null(); | |
819 | if (ret == NULL) { | |
820 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); | |
821 | goto err; | |
822 | } | |
823 | } | |
824 | if ((xn = X509_get_subject_name(x)) == NULL) | |
825 | goto err; | |
826 | /* check for duplicates */ | |
827 | xn = X509_NAME_dup(xn); | |
828 | if (xn == NULL) | |
829 | goto err; | |
830 | if (sk_X509_NAME_find(sk, xn) >= 0) | |
831 | X509_NAME_free(xn); | |
832 | else { | |
833 | sk_X509_NAME_push(sk, xn); | |
834 | sk_X509_NAME_push(ret, xn); | |
835 | } | |
836 | } | |
66696478 | 837 | goto done; |
0f113f3e | 838 | |
0f113f3e | 839 | err: |
66696478 RS |
840 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
841 | ret = NULL; | |
842 | done: | |
222561fe | 843 | sk_X509_NAME_free(sk); |
ca3a82c3 | 844 | BIO_free(in); |
222561fe | 845 | X509_free(x); |
0f113f3e MC |
846 | if (ret != NULL) |
847 | ERR_clear_error(); | |
848 | return (ret); | |
849 | } | |
58964a49 | 850 | #endif |
d02b48c6 | 851 | |
0f113f3e | 852 | /** |
eb90a483 BL |
853 | * Add a file of certs to a stack. |
854 | * \param stack the stack to add to. | |
855 | * \param file the file to add from. All certs in this file that are not | |
856 | * already in the stack will be added. | |
857 | * \return 1 for success, 0 for failure. Note that in the case of failure some | |
858 | * certs may have been added to \c stack. | |
859 | */ | |
860 | ||
661b361b | 861 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
0f113f3e MC |
862 | const char *file) |
863 | { | |
864 | BIO *in; | |
865 | X509 *x = NULL; | |
866 | X509_NAME *xn = NULL; | |
867 | int ret = 1; | |
868 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b); | |
869 | ||
870 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp); | |
871 | ||
872 | in = BIO_new(BIO_s_file_internal()); | |
873 | ||
874 | if (in == NULL) { | |
875 | SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, | |
876 | ERR_R_MALLOC_FAILURE); | |
877 | goto err; | |
878 | } | |
879 | ||
880 | if (!BIO_read_filename(in, file)) | |
881 | goto err; | |
882 | ||
883 | for (;;) { | |
884 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) | |
885 | break; | |
886 | if ((xn = X509_get_subject_name(x)) == NULL) | |
887 | goto err; | |
888 | xn = X509_NAME_dup(xn); | |
889 | if (xn == NULL) | |
890 | goto err; | |
891 | if (sk_X509_NAME_find(stack, xn) >= 0) | |
892 | X509_NAME_free(xn); | |
893 | else | |
894 | sk_X509_NAME_push(stack, xn); | |
895 | } | |
896 | ||
897 | ERR_clear_error(); | |
66696478 | 898 | goto done; |
0f113f3e | 899 | |
0f113f3e MC |
900 | err: |
901 | ret = 0; | |
66696478 | 902 | done: |
ca3a82c3 | 903 | BIO_free(in); |
25aaa98a | 904 | X509_free(x); |
0f113f3e | 905 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp); |
0f113f3e MC |
906 | return ret; |
907 | } | |
908 | ||
909 | /** | |
eb90a483 BL |
910 | * Add a directory of certs to a stack. |
911 | * \param stack the stack to append to. | |
912 | * \param dir the directory to append from. All files in this directory will be | |
913 | * examined as potential certs. Any that are acceptable to | |
72e442a3 | 914 | * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be |
eb90a483 BL |
915 | * included. |
916 | * \return 1 for success, 0 for failure. Note that in the case of failure some | |
917 | * certs may have been added to \c stack. | |
918 | */ | |
919 | ||
661b361b | 920 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
0f113f3e MC |
921 | const char *dir) |
922 | { | |
923 | OPENSSL_DIR_CTX *d = NULL; | |
924 | const char *filename; | |
925 | int ret = 0; | |
eb90a483 | 926 | |
0f113f3e | 927 | CRYPTO_w_lock(CRYPTO_LOCK_READDIR); |
4083a229 | 928 | |
0f113f3e | 929 | /* Note that a side effect is that the CAs will be sorted by name */ |
4083a229 | 930 | |
0f113f3e MC |
931 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
932 | char buf[1024]; | |
933 | int r; | |
4083a229 | 934 | |
0f113f3e MC |
935 | if (strlen(dir) + strlen(filename) + 2 > sizeof buf) { |
936 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, | |
937 | SSL_R_PATH_TOO_LONG); | |
938 | goto err; | |
939 | } | |
4083a229 | 940 | #ifdef OPENSSL_SYS_VMS |
0f113f3e | 941 | r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename); |
4083a229 | 942 | #else |
0f113f3e | 943 | r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename); |
4083a229 | 944 | #endif |
0f113f3e MC |
945 | if (r <= 0 || r >= (int)sizeof(buf)) |
946 | goto err; | |
947 | if (!SSL_add_file_cert_subjects_to_stack(stack, buf)) | |
948 | goto err; | |
949 | } | |
950 | ||
951 | if (errno) { | |
952 | SYSerr(SYS_F_OPENDIR, get_last_sys_error()); | |
953 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); | |
954 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); | |
955 | goto err; | |
956 | } | |
957 | ||
958 | ret = 1; | |
959 | ||
960 | err: | |
961 | if (d) | |
962 | OPENSSL_DIR_end(&d); | |
963 | CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); | |
964 | return ret; | |
965 | } | |
285046ec | 966 | |
4379d0e4 DSH |
967 | /* Add a certificate to a BUF_MEM structure */ |
968 | ||
969 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) | |
0f113f3e MC |
970 | { |
971 | int n; | |
972 | unsigned char *p; | |
973 | ||
974 | n = i2d_X509(x, NULL); | |
975 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { | |
976 | SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); | |
977 | return 0; | |
978 | } | |
979 | p = (unsigned char *)&(buf->data[*l]); | |
980 | l2n3(n, p); | |
981 | i2d_X509(x, &p); | |
982 | *l += n + 3; | |
983 | ||
984 | return 1; | |
985 | } | |
4379d0e4 DSH |
986 | |
987 | /* Add certificate chain to internal SSL BUF_MEM strcuture */ | |
c526ed41 | 988 | int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) |
0f113f3e MC |
989 | { |
990 | BUF_MEM *buf = s->init_buf; | |
991 | int i; | |
992 | ||
993 | X509 *x; | |
994 | STACK_OF(X509) *extra_certs; | |
995 | X509_STORE *chain_store; | |
996 | ||
997 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ | |
998 | if (!BUF_MEM_grow_clean(buf, 10)) { | |
999 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB); | |
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | if (!cpk || !cpk->x509) | |
1004 | return 1; | |
1005 | ||
1006 | x = cpk->x509; | |
1007 | ||
1008 | /* | |
1009 | * If we have a certificate specific chain use it, else use parent ctx. | |
1010 | */ | |
1011 | if (cpk->chain) | |
1012 | extra_certs = cpk->chain; | |
1013 | else | |
1014 | extra_certs = s->ctx->extra_certs; | |
1015 | ||
1016 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) | |
1017 | chain_store = NULL; | |
1018 | else if (s->cert->chain_store) | |
1019 | chain_store = s->cert->chain_store; | |
1020 | else | |
1021 | chain_store = s->ctx->cert_store; | |
1022 | ||
1023 | if (chain_store) { | |
1024 | X509_STORE_CTX xs_ctx; | |
1025 | ||
1026 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) { | |
1027 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); | |
1028 | return (0); | |
1029 | } | |
1030 | X509_verify_cert(&xs_ctx); | |
1031 | /* Don't leave errors in the queue */ | |
1032 | ERR_clear_error(); | |
1033 | i = ssl_security_cert_chain(s, xs_ctx.chain, NULL, 0); | |
1034 | if (i != 1) { | |
1035 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1036 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
1037 | return 0; | |
1038 | } | |
1039 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { | |
1040 | x = sk_X509_value(xs_ctx.chain, i); | |
1041 | ||
1042 | if (!ssl_add_cert_to_buf(buf, l, x)) { | |
1043 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1044 | return 0; | |
1045 | } | |
1046 | } | |
1047 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1048 | } else { | |
1049 | i = ssl_security_cert_chain(s, extra_certs, x, 0); | |
1050 | if (i != 1) { | |
1051 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
1052 | return 0; | |
1053 | } | |
1054 | if (!ssl_add_cert_to_buf(buf, l, x)) | |
1055 | return 0; | |
1056 | for (i = 0; i < sk_X509_num(extra_certs); i++) { | |
1057 | x = sk_X509_value(extra_certs, i); | |
1058 | if (!ssl_add_cert_to_buf(buf, l, x)) | |
1059 | return 0; | |
1060 | } | |
1061 | } | |
1062 | return 1; | |
1063 | } | |
4379d0e4 | 1064 | |
74ecfab4 | 1065 | /* Build a certificate chain for current certificate */ |
b362ccab | 1066 | int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags) |
0f113f3e MC |
1067 | { |
1068 | CERT *c = s ? s->cert : ctx->cert; | |
1069 | CERT_PKEY *cpk = c->key; | |
1070 | X509_STORE *chain_store = NULL; | |
1071 | X509_STORE_CTX xs_ctx; | |
1072 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; | |
1073 | X509 *x; | |
1074 | int i, rv = 0; | |
1075 | unsigned long error; | |
1076 | ||
1077 | if (!cpk->x509) { | |
1078 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET); | |
1079 | goto err; | |
1080 | } | |
1081 | /* Rearranging and check the chain: add everything to a store */ | |
1082 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { | |
1083 | chain_store = X509_STORE_new(); | |
1084 | if (!chain_store) | |
1085 | goto err; | |
1086 | for (i = 0; i < sk_X509_num(cpk->chain); i++) { | |
1087 | x = sk_X509_value(cpk->chain, i); | |
1088 | if (!X509_STORE_add_cert(chain_store, x)) { | |
1089 | error = ERR_peek_last_error(); | |
1090 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || | |
1091 | ERR_GET_REASON(error) != | |
1092 | X509_R_CERT_ALREADY_IN_HASH_TABLE) | |
1093 | goto err; | |
1094 | ERR_clear_error(); | |
1095 | } | |
1096 | } | |
1097 | /* Add EE cert too: it might be self signed */ | |
1098 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) { | |
1099 | error = ERR_peek_last_error(); | |
1100 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || | |
1101 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) | |
1102 | goto err; | |
1103 | ERR_clear_error(); | |
1104 | } | |
1105 | } else { | |
1106 | if (c->chain_store) | |
1107 | chain_store = c->chain_store; | |
1108 | else if (s) | |
1109 | chain_store = s->ctx->cert_store; | |
1110 | else | |
1111 | chain_store = ctx->cert_store; | |
1112 | ||
1113 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) | |
1114 | untrusted = cpk->chain; | |
1115 | } | |
1116 | ||
1117 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) { | |
1118 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB); | |
1119 | goto err; | |
1120 | } | |
1121 | /* Set suite B flags if needed */ | |
1122 | X509_STORE_CTX_set_flags(&xs_ctx, | |
1123 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); | |
1124 | ||
1125 | i = X509_verify_cert(&xs_ctx); | |
1126 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { | |
1127 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) | |
1128 | ERR_clear_error(); | |
1129 | i = 1; | |
1130 | rv = 2; | |
1131 | } | |
1132 | if (i > 0) | |
1133 | chain = X509_STORE_CTX_get1_chain(&xs_ctx); | |
1134 | if (i <= 0) { | |
1135 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED); | |
1136 | i = X509_STORE_CTX_get_error(&xs_ctx); | |
1137 | ERR_add_error_data(2, "Verify error:", | |
1138 | X509_verify_cert_error_string(i)); | |
1139 | ||
1140 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1141 | goto err; | |
1142 | } | |
1143 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1144 | /* Remove EE certificate from chain */ | |
1145 | x = sk_X509_shift(chain); | |
1146 | X509_free(x); | |
1147 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { | |
1148 | if (sk_X509_num(chain) > 0) { | |
1149 | /* See if last cert is self signed */ | |
1150 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); | |
1151 | X509_check_purpose(x, -1, 0); | |
1152 | if (x->ex_flags & EXFLAG_SS) { | |
1153 | x = sk_X509_pop(chain); | |
1154 | X509_free(x); | |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | /* | |
1159 | * Check security level of all CA certificates: EE will have been checked | |
1160 | * already. | |
1161 | */ | |
1162 | for (i = 0; i < sk_X509_num(chain); i++) { | |
1163 | x = sk_X509_value(chain, i); | |
1164 | rv = ssl_security_cert(s, ctx, x, 0, 0); | |
1165 | if (rv != 1) { | |
1166 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv); | |
1167 | sk_X509_pop_free(chain, X509_free); | |
1168 | rv = 0; | |
1169 | goto err; | |
1170 | } | |
1171 | } | |
222561fe | 1172 | sk_X509_pop_free(cpk->chain, X509_free); |
0f113f3e MC |
1173 | cpk->chain = chain; |
1174 | if (rv == 0) | |
1175 | rv = 1; | |
1176 | err: | |
1177 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) | |
1178 | X509_STORE_free(chain_store); | |
1179 | ||
1180 | return rv; | |
1181 | } | |
74ecfab4 DSH |
1182 | |
1183 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) | |
0f113f3e MC |
1184 | { |
1185 | X509_STORE **pstore; | |
1186 | if (chain) | |
1187 | pstore = &c->chain_store; | |
1188 | else | |
1189 | pstore = &c->verify_store; | |
222561fe | 1190 | X509_STORE_free(*pstore); |
0f113f3e MC |
1191 | *pstore = store; |
1192 | if (ref && store) | |
1193 | CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE); | |
1194 | return 1; | |
1195 | } | |
1196 | ||
1197 | static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op, | |
1198 | int bits, int nid, void *other, | |
1199 | void *ex) | |
1200 | { | |
1201 | int level, minbits; | |
1202 | static const int minbits_table[5] = { 80, 112, 128, 192, 256 }; | |
1203 | if (ctx) | |
1204 | level = SSL_CTX_get_security_level(ctx); | |
1205 | else | |
1206 | level = SSL_get_security_level(s); | |
1207 | /* Level 0: anything goes */ | |
1208 | if (level <= 0) | |
1209 | return 1; | |
1210 | if (level > 5) | |
1211 | level = 5; | |
1212 | minbits = minbits_table[level - 1]; | |
1213 | switch (op) { | |
1214 | case SSL_SECOP_CIPHER_SUPPORTED: | |
1215 | case SSL_SECOP_CIPHER_SHARED: | |
1216 | case SSL_SECOP_CIPHER_CHECK: | |
1217 | { | |
1218 | const SSL_CIPHER *c = other; | |
1219 | /* No ciphers below security level */ | |
1220 | if (bits < minbits) | |
1221 | return 0; | |
1222 | /* No unauthenticated ciphersuites */ | |
1223 | if (c->algorithm_auth & SSL_aNULL) | |
1224 | return 0; | |
1225 | /* No MD5 mac ciphersuites */ | |
1226 | if (c->algorithm_mac & SSL_MD5) | |
1227 | return 0; | |
1228 | /* SHA1 HMAC is 160 bits of security */ | |
1229 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1) | |
1230 | return 0; | |
1231 | /* Level 2: no RC4 */ | |
1232 | if (level >= 2 && c->algorithm_enc == SSL_RC4) | |
1233 | return 0; | |
1234 | /* Level 3: forward secure ciphersuites only */ | |
1235 | if (level >= 3 && !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH))) | |
1236 | return 0; | |
1237 | break; | |
1238 | } | |
1239 | case SSL_SECOP_VERSION: | |
1240 | /* SSLv3 not allowed on level 2 */ | |
1241 | if (nid <= SSL3_VERSION && level >= 2) | |
1242 | return 0; | |
1243 | /* TLS v1.1 and above only for level 3 */ | |
1244 | if (nid <= TLS1_VERSION && level >= 3) | |
1245 | return 0; | |
1246 | /* TLS v1.2 only for level 4 and above */ | |
1247 | if (nid <= TLS1_1_VERSION && level >= 4) | |
1248 | return 0; | |
1249 | break; | |
1250 | ||
1251 | case SSL_SECOP_COMPRESSION: | |
1252 | if (level >= 2) | |
1253 | return 0; | |
1254 | break; | |
1255 | case SSL_SECOP_TICKET: | |
1256 | if (level >= 3) | |
1257 | return 0; | |
1258 | break; | |
1259 | default: | |
1260 | if (bits < minbits) | |
1261 | return 0; | |
1262 | } | |
1263 | return 1; | |
1264 | } | |
b362ccab DSH |
1265 | |
1266 | int ssl_security(SSL *s, int op, int bits, int nid, void *other) | |
0f113f3e MC |
1267 | { |
1268 | return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex); | |
1269 | } | |
b362ccab DSH |
1270 | |
1271 | int ssl_ctx_security(SSL_CTX *ctx, int op, int bits, int nid, void *other) | |
0f113f3e MC |
1272 | { |
1273 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other, | |
1274 | ctx->cert->sec_ex); | |
1275 | } |