]>
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 | ||
68570797 | 126 | #include "internal/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 | |
6b691a5c | 168 | CERT *ssl_cert_new(void) |
0f113f3e | 169 | { |
b51bce94 | 170 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 171 | |
0f113f3e MC |
172 | if (ret == NULL) { |
173 | SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE); | |
174 | return (NULL); | |
175 | } | |
0f113f3e MC |
176 | |
177 | ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]); | |
178 | ret->references = 1; | |
0f113f3e MC |
179 | ret->sec_cb = ssl_security_default_callback; |
180 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL; | |
181 | ret->sec_ex = NULL; | |
182 | return (ret); | |
183 | } | |
d02b48c6 | 184 | |
ca8e5b9b | 185 | CERT *ssl_cert_dup(CERT *cert) |
0f113f3e | 186 | { |
b51bce94 | 187 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
188 | int i; |
189 | ||
0f113f3e MC |
190 | if (ret == NULL) { |
191 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); | |
192 | return (NULL); | |
193 | } | |
194 | ||
0e04674e | 195 | ret->references = 1; |
16f8d4eb | 196 | ret->key = &ret->pkeys[cert->key - cert->pkeys]; |
ca8e5b9b | 197 | |
bc36ee62 | 198 | #ifndef OPENSSL_NO_DH |
0f113f3e | 199 | if (cert->dh_tmp != NULL) { |
e2b420fd DSH |
200 | ret->dh_tmp = cert->dh_tmp; |
201 | EVP_PKEY_up_ref(ret->dh_tmp); | |
0f113f3e MC |
202 | } |
203 | ret->dh_tmp_cb = cert->dh_tmp_cb; | |
204 | ret->dh_tmp_auto = cert->dh_tmp_auto; | |
ca8e5b9b BM |
205 | #endif |
206 | ||
0f113f3e MC |
207 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
208 | CERT_PKEY *cpk = cert->pkeys + i; | |
209 | CERT_PKEY *rpk = ret->pkeys + i; | |
210 | if (cpk->x509 != NULL) { | |
211 | rpk->x509 = cpk->x509; | |
05f0fb9f | 212 | X509_up_ref(rpk->x509); |
0f113f3e MC |
213 | } |
214 | ||
215 | if (cpk->privatekey != NULL) { | |
216 | rpk->privatekey = cpk->privatekey; | |
3aeb9348 | 217 | EVP_PKEY_up_ref(cpk->privatekey); |
0f113f3e MC |
218 | } |
219 | ||
220 | if (cpk->chain) { | |
221 | rpk->chain = X509_chain_up_ref(cpk->chain); | |
222 | if (!rpk->chain) { | |
223 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); | |
224 | goto err; | |
225 | } | |
226 | } | |
0f113f3e MC |
227 | if (cert->pkeys[i].serverinfo != NULL) { |
228 | /* Just copy everything. */ | |
229 | ret->pkeys[i].serverinfo = | |
230 | OPENSSL_malloc(cert->pkeys[i].serverinfo_length); | |
231 | if (ret->pkeys[i].serverinfo == NULL) { | |
232 | SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE); | |
233 | goto err; | |
234 | } | |
235 | ret->pkeys[i].serverinfo_length = | |
236 | cert->pkeys[i].serverinfo_length; | |
237 | memcpy(ret->pkeys[i].serverinfo, | |
238 | cert->pkeys[i].serverinfo, | |
239 | cert->pkeys[i].serverinfo_length); | |
240 | } | |
0f113f3e MC |
241 | } |
242 | ||
76106e60 | 243 | /* Configured sigalgs copied across */ |
0f113f3e MC |
244 | if (cert->conf_sigalgs) { |
245 | ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen); | |
a71edf3b | 246 | if (ret->conf_sigalgs == NULL) |
0f113f3e MC |
247 | goto err; |
248 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen); | |
249 | ret->conf_sigalgslen = cert->conf_sigalgslen; | |
250 | } else | |
251 | ret->conf_sigalgs = NULL; | |
252 | ||
253 | if (cert->client_sigalgs) { | |
254 | ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen); | |
a71edf3b | 255 | if (ret->client_sigalgs == NULL) |
0f113f3e MC |
256 | goto err; |
257 | memcpy(ret->client_sigalgs, cert->client_sigalgs, | |
258 | cert->client_sigalgslen); | |
259 | ret->client_sigalgslen = cert->client_sigalgslen; | |
260 | } else | |
261 | ret->client_sigalgs = NULL; | |
262 | /* Shared sigalgs also NULL */ | |
263 | ret->shared_sigalgs = NULL; | |
264 | /* Copy any custom client certificate types */ | |
265 | if (cert->ctypes) { | |
266 | ret->ctypes = OPENSSL_malloc(cert->ctype_num); | |
a71edf3b | 267 | if (ret->ctypes == NULL) |
0f113f3e MC |
268 | goto err; |
269 | memcpy(ret->ctypes, cert->ctypes, cert->ctype_num); | |
270 | ret->ctype_num = cert->ctype_num; | |
271 | } | |
272 | ||
273 | ret->cert_flags = cert->cert_flags; | |
274 | ||
275 | ret->cert_cb = cert->cert_cb; | |
276 | ret->cert_cb_arg = cert->cert_cb_arg; | |
277 | ||
278 | if (cert->verify_store) { | |
279 | CRYPTO_add(&cert->verify_store->references, 1, | |
280 | CRYPTO_LOCK_X509_STORE); | |
281 | ret->verify_store = cert->verify_store; | |
282 | } | |
283 | ||
284 | if (cert->chain_store) { | |
285 | CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE); | |
286 | ret->chain_store = cert->chain_store; | |
287 | } | |
288 | ||
0f113f3e MC |
289 | ret->sec_cb = cert->sec_cb; |
290 | ret->sec_level = cert->sec_level; | |
291 | ret->sec_ex = cert->sec_ex; | |
b362ccab | 292 | |
0f113f3e MC |
293 | if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext)) |
294 | goto err; | |
295 | if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext)) | |
296 | goto err; | |
9076bd25 | 297 | #ifndef OPENSSL_NO_PSK |
df6da24b | 298 | if (cert->psk_identity_hint) { |
7644a9ae | 299 | ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint); |
df6da24b DSH |
300 | if (ret->psk_identity_hint == NULL) |
301 | goto err; | |
302 | } | |
9076bd25 | 303 | #endif |
0f113f3e MC |
304 | return (ret); |
305 | ||
306 | err: | |
307 | ssl_cert_free(ret); | |
9ade64de | 308 | |
0f113f3e MC |
309 | return NULL; |
310 | } | |
ca8e5b9b | 311 | |
a5ee80b9 DSH |
312 | /* Free up and clear all certificates and chains */ |
313 | ||
314 | void ssl_cert_clear_certs(CERT *c) | |
0f113f3e MC |
315 | { |
316 | int i; | |
317 | if (c == NULL) | |
318 | return; | |
319 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
320 | CERT_PKEY *cpk = c->pkeys + i; | |
222561fe RS |
321 | X509_free(cpk->x509); |
322 | cpk->x509 = NULL; | |
c5ba2d99 RS |
323 | EVP_PKEY_free(cpk->privatekey); |
324 | cpk->privatekey = NULL; | |
222561fe RS |
325 | sk_X509_pop_free(cpk->chain, X509_free); |
326 | cpk->chain = NULL; | |
25aaa98a RS |
327 | OPENSSL_free(cpk->serverinfo); |
328 | cpk->serverinfo = NULL; | |
329 | cpk->serverinfo_length = 0; | |
0f113f3e MC |
330 | } |
331 | } | |
ca8e5b9b | 332 | |
eb90a483 | 333 | void ssl_cert_free(CERT *c) |
0f113f3e MC |
334 | { |
335 | int i; | |
d02b48c6 | 336 | |
0f113f3e MC |
337 | if (c == NULL) |
338 | return; | |
e03ddfae | 339 | |
0f113f3e | 340 | i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT); |
58964a49 | 341 | #ifdef REF_PRINT |
0f113f3e | 342 | REF_PRINT("CERT", c); |
58964a49 | 343 | #endif |
0f113f3e MC |
344 | if (i > 0) |
345 | return; | |
d02b48c6 | 346 | #ifdef REF_CHECK |
0f113f3e MC |
347 | if (i < 0) { |
348 | fprintf(stderr, "ssl_cert_free, bad reference count\n"); | |
349 | abort(); /* ok */ | |
350 | } | |
d02b48c6 RE |
351 | #endif |
352 | ||
bc36ee62 | 353 | #ifndef OPENSSL_NO_DH |
e2b420fd | 354 | EVP_PKEY_free(c->dh_tmp); |
d02b48c6 RE |
355 | #endif |
356 | ||
0f113f3e | 357 | ssl_cert_clear_certs(c); |
25aaa98a RS |
358 | OPENSSL_free(c->conf_sigalgs); |
359 | OPENSSL_free(c->client_sigalgs); | |
360 | OPENSSL_free(c->shared_sigalgs); | |
361 | OPENSSL_free(c->ctypes); | |
222561fe RS |
362 | X509_STORE_free(c->verify_store); |
363 | X509_STORE_free(c->chain_store); | |
0f113f3e MC |
364 | custom_exts_free(&c->cli_ext); |
365 | custom_exts_free(&c->srv_ext); | |
df6da24b DSH |
366 | #ifndef OPENSSL_NO_PSK |
367 | OPENSSL_free(c->psk_identity_hint); | |
368 | #endif | |
0f113f3e MC |
369 | OPENSSL_free(c); |
370 | } | |
d02b48c6 | 371 | |
b362ccab | 372 | int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
0f113f3e MC |
373 | { |
374 | int i, r; | |
375 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; | |
376 | if (!cpk) | |
377 | return 0; | |
222561fe | 378 | sk_X509_pop_free(cpk->chain, X509_free); |
0f113f3e MC |
379 | for (i = 0; i < sk_X509_num(chain); i++) { |
380 | r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0); | |
381 | if (r != 1) { | |
382 | SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r); | |
383 | return 0; | |
384 | } | |
385 | } | |
386 | cpk->chain = chain; | |
387 | return 1; | |
388 | } | |
f71c6e52 | 389 | |
b362ccab | 390 | int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
0f113f3e MC |
391 | { |
392 | STACK_OF(X509) *dchain; | |
393 | if (!chain) | |
394 | return ssl_cert_set0_chain(s, ctx, NULL); | |
395 | dchain = X509_chain_up_ref(chain); | |
396 | if (!dchain) | |
397 | return 0; | |
398 | if (!ssl_cert_set0_chain(s, ctx, dchain)) { | |
399 | sk_X509_pop_free(dchain, X509_free); | |
400 | return 0; | |
401 | } | |
402 | return 1; | |
403 | } | |
f71c6e52 | 404 | |
b362ccab | 405 | int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
0f113f3e MC |
406 | { |
407 | int r; | |
408 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; | |
409 | if (!cpk) | |
410 | return 0; | |
411 | r = ssl_security_cert(s, ctx, x, 0, 0); | |
412 | if (r != 1) { | |
413 | SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r); | |
414 | return 0; | |
415 | } | |
416 | if (!cpk->chain) | |
417 | cpk->chain = sk_X509_new_null(); | |
418 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) | |
419 | return 0; | |
420 | return 1; | |
421 | } | |
f71c6e52 | 422 | |
b362ccab | 423 | int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x) |
0f113f3e MC |
424 | { |
425 | if (!ssl_cert_add0_chain_cert(s, ctx, x)) | |
426 | return 0; | |
05f0fb9f | 427 | X509_up_ref(x); |
0f113f3e MC |
428 | return 1; |
429 | } | |
7b6b246f RS |
430 | |
431 | int ssl_cert_select_current(CERT *c, X509 *x) | |
0f113f3e MC |
432 | { |
433 | int i; | |
434 | if (x == NULL) | |
435 | return 0; | |
436 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
437 | CERT_PKEY *cpk = c->pkeys + i; | |
438 | if (cpk->x509 == x && cpk->privatekey) { | |
439 | c->key = cpk; | |
440 | return 1; | |
441 | } | |
442 | } | |
443 | ||
444 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
445 | CERT_PKEY *cpk = c->pkeys + i; | |
446 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { | |
447 | c->key = cpk; | |
448 | return 1; | |
449 | } | |
450 | } | |
451 | return 0; | |
452 | } | |
0f78819c DSH |
453 | |
454 | int ssl_cert_set_current(CERT *c, long op) | |
0f113f3e MC |
455 | { |
456 | int i, idx; | |
457 | if (!c) | |
458 | return 0; | |
459 | if (op == SSL_CERT_SET_FIRST) | |
460 | idx = 0; | |
461 | else if (op == SSL_CERT_SET_NEXT) { | |
462 | idx = (int)(c->key - c->pkeys + 1); | |
463 | if (idx >= SSL_PKEY_NUM) | |
464 | return 0; | |
465 | } else | |
466 | return 0; | |
467 | for (i = idx; i < SSL_PKEY_NUM; i++) { | |
468 | CERT_PKEY *cpk = c->pkeys + i; | |
469 | if (cpk->x509 && cpk->privatekey) { | |
470 | c->key = cpk; | |
471 | return 1; | |
472 | } | |
473 | } | |
474 | return 0; | |
475 | } | |
476 | ||
477 | void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg) | |
478 | { | |
479 | c->cert_cb = cb; | |
480 | c->cert_cb_arg = arg; | |
481 | } | |
18d71588 | 482 | |
0f113f3e MC |
483 | int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) |
484 | { | |
485 | X509 *x; | |
486 | int i; | |
487 | X509_STORE *verify_store; | |
488 | X509_STORE_CTX ctx; | |
919ba009 | 489 | X509_VERIFY_PARAM *param; |
0f113f3e MC |
490 | |
491 | if (s->cert->verify_store) | |
492 | verify_store = s->cert->verify_store; | |
493 | else | |
494 | verify_store = s->ctx->cert_store; | |
495 | ||
496 | if ((sk == NULL) || (sk_X509_num(sk) == 0)) | |
497 | return (0); | |
498 | ||
499 | x = sk_X509_value(sk, 0); | |
500 | if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) { | |
501 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB); | |
502 | return (0); | |
503 | } | |
919ba009 VD |
504 | param = X509_STORE_CTX_get0_param(&ctx); |
505 | ||
0f113f3e MC |
506 | /* Set suite B flags if needed */ |
507 | X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s)); | |
0f113f3e MC |
508 | X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s); |
509 | ||
919ba009 VD |
510 | /* Verify via DANE if enabled */ |
511 | if (DANETLS_ENABLED(&s->dane)) | |
512 | X509_STORE_CTX_set0_dane(&ctx, &s->dane); | |
513 | ||
0f113f3e MC |
514 | /* |
515 | * We need to inherit the verify parameters. These can be determined by | |
516 | * the context: if its a server it will verify SSL client certificates or | |
517 | * vice versa. | |
518 | */ | |
519 | ||
520 | X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server"); | |
521 | /* | |
919ba009 | 522 | * Anything non-default in "s->param" should overwrite anything in the ctx. |
0f113f3e | 523 | */ |
919ba009 | 524 | X509_VERIFY_PARAM_set1(param, s->param); |
0f113f3e MC |
525 | |
526 | if (s->verify_callback) | |
527 | X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback); | |
528 | ||
529 | if (s->ctx->app_verify_callback != NULL) | |
0f113f3e | 530 | i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg); |
0f113f3e | 531 | else { |
0f113f3e MC |
532 | i = X509_verify_cert(&ctx); |
533 | # if 0 | |
534 | /* Dummy error calls so mkerr generates them */ | |
535 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL); | |
536 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL); | |
537 | SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK); | |
538 | # endif | |
539 | if (i > 0) | |
540 | i = ssl_security_cert_chain(s, ctx.chain, NULL, 1); | |
0f113f3e | 541 | } |
d02b48c6 | 542 | |
0f113f3e | 543 | s->verify_result = ctx.error; |
919ba009 VD |
544 | |
545 | /* Move peername from the store context params to the SSL handle's */ | |
546 | X509_VERIFY_PARAM_move_peername(s->param, param); | |
547 | ||
0f113f3e | 548 | X509_STORE_CTX_cleanup(&ctx); |
d02b48c6 | 549 | |
0f113f3e MC |
550 | return (i); |
551 | } | |
d02b48c6 | 552 | |
0f113f3e MC |
553 | static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list, |
554 | STACK_OF(X509_NAME) *name_list) | |
555 | { | |
222561fe | 556 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
0f113f3e MC |
557 | *ca_list = name_list; |
558 | } | |
d02b48c6 | 559 | |
838d25a1 | 560 | STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) |
0f113f3e MC |
561 | { |
562 | int i; | |
563 | STACK_OF(X509_NAME) *ret; | |
564 | X509_NAME *name; | |
565 | ||
566 | ret = sk_X509_NAME_new_null(); | |
567 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { | |
568 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); | |
569 | if ((name == NULL) || !sk_X509_NAME_push(ret, name)) { | |
570 | sk_X509_NAME_pop_free(ret, X509_NAME_free); | |
571 | return (NULL); | |
572 | } | |
573 | } | |
574 | return (ret); | |
575 | } | |
576 | ||
577 | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) | |
578 | { | |
579 | set_client_CA_list(&(s->client_CA), name_list); | |
580 | } | |
581 | ||
582 | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) | |
583 | { | |
584 | set_client_CA_list(&(ctx->client_CA), name_list); | |
585 | } | |
d02b48c6 | 586 | |
0821bcd4 | 587 | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) |
0f113f3e MC |
588 | { |
589 | return (ctx->client_CA); | |
590 | } | |
d02b48c6 | 591 | |
0821bcd4 | 592 | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
0f113f3e | 593 | { |
23a635c0 | 594 | if (!s->server) { /* we are in the client */ |
0f113f3e MC |
595 | if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL)) |
596 | return (s->s3->tmp.ca_names); | |
597 | else | |
598 | return (NULL); | |
599 | } else { | |
600 | if (s->client_CA != NULL) | |
601 | return (s->client_CA); | |
602 | else | |
603 | return (s->ctx->client_CA); | |
604 | } | |
605 | } | |
606 | ||
607 | static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) | |
608 | { | |
609 | X509_NAME *name; | |
610 | ||
611 | if (x == NULL) | |
612 | return (0); | |
613 | if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL)) | |
614 | return (0); | |
615 | ||
616 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL) | |
617 | return (0); | |
618 | ||
619 | if (!sk_X509_NAME_push(*sk, name)) { | |
620 | X509_NAME_free(name); | |
621 | return (0); | |
622 | } | |
623 | return (1); | |
624 | } | |
625 | ||
626 | int SSL_add_client_CA(SSL *ssl, X509 *x) | |
627 | { | |
628 | return (add_client_CA(&(ssl->client_CA), x)); | |
629 | } | |
630 | ||
631 | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) | |
632 | { | |
633 | return (add_client_CA(&(ctx->client_CA), x)); | |
634 | } | |
635 | ||
636 | static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b) | |
637 | { | |
638 | return (X509_NAME_cmp(*a, *b)); | |
639 | } | |
d02b48c6 | 640 | |
0f113f3e | 641 | /** |
eb90a483 BL |
642 | * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed; |
643 | * it doesn't really have anything to do with clients (except that a common use | |
644 | * for a stack of CAs is to send it to the client). Actually, it doesn't have | |
645 | * much to do with CAs, either, since it will load any old cert. | |
646 | * \param file the file containing one or more certs. | |
647 | * \return a ::STACK containing the certs. | |
648 | */ | |
f73e07cf | 649 | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
0f113f3e MC |
650 | { |
651 | BIO *in; | |
652 | X509 *x = NULL; | |
653 | X509_NAME *xn = NULL; | |
654 | STACK_OF(X509_NAME) *ret = NULL, *sk; | |
655 | ||
656 | sk = sk_X509_NAME_new(xname_cmp); | |
657 | ||
9982cbbb | 658 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
659 | |
660 | if ((sk == NULL) || (in == NULL)) { | |
661 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); | |
662 | goto err; | |
663 | } | |
664 | ||
665 | if (!BIO_read_filename(in, file)) | |
666 | goto err; | |
667 | ||
668 | for (;;) { | |
669 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) | |
670 | break; | |
671 | if (ret == NULL) { | |
672 | ret = sk_X509_NAME_new_null(); | |
673 | if (ret == NULL) { | |
674 | SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE); | |
675 | goto err; | |
676 | } | |
677 | } | |
678 | if ((xn = X509_get_subject_name(x)) == NULL) | |
679 | goto err; | |
680 | /* check for duplicates */ | |
681 | xn = X509_NAME_dup(xn); | |
682 | if (xn == NULL) | |
683 | goto err; | |
684 | if (sk_X509_NAME_find(sk, xn) >= 0) | |
685 | X509_NAME_free(xn); | |
686 | else { | |
687 | sk_X509_NAME_push(sk, xn); | |
688 | sk_X509_NAME_push(ret, xn); | |
689 | } | |
690 | } | |
66696478 | 691 | goto done; |
0f113f3e | 692 | |
0f113f3e | 693 | err: |
66696478 RS |
694 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
695 | ret = NULL; | |
696 | done: | |
222561fe | 697 | sk_X509_NAME_free(sk); |
ca3a82c3 | 698 | BIO_free(in); |
222561fe | 699 | X509_free(x); |
0f113f3e MC |
700 | if (ret != NULL) |
701 | ERR_clear_error(); | |
702 | return (ret); | |
703 | } | |
d02b48c6 | 704 | |
0f113f3e | 705 | /** |
eb90a483 BL |
706 | * Add a file of certs to a stack. |
707 | * \param stack the stack to add to. | |
708 | * \param file the file to add from. All certs in this file that are not | |
709 | * already in the stack will be added. | |
710 | * \return 1 for success, 0 for failure. Note that in the case of failure some | |
711 | * certs may have been added to \c stack. | |
712 | */ | |
713 | ||
661b361b | 714 | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
0f113f3e MC |
715 | const char *file) |
716 | { | |
717 | BIO *in; | |
718 | X509 *x = NULL; | |
719 | X509_NAME *xn = NULL; | |
720 | int ret = 1; | |
721 | int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b); | |
722 | ||
723 | oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp); | |
724 | ||
9982cbbb | 725 | in = BIO_new(BIO_s_file()); |
0f113f3e MC |
726 | |
727 | if (in == NULL) { | |
728 | SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, | |
729 | ERR_R_MALLOC_FAILURE); | |
730 | goto err; | |
731 | } | |
732 | ||
733 | if (!BIO_read_filename(in, file)) | |
734 | goto err; | |
735 | ||
736 | for (;;) { | |
737 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) | |
738 | break; | |
739 | if ((xn = X509_get_subject_name(x)) == NULL) | |
740 | goto err; | |
741 | xn = X509_NAME_dup(xn); | |
742 | if (xn == NULL) | |
743 | goto err; | |
744 | if (sk_X509_NAME_find(stack, xn) >= 0) | |
745 | X509_NAME_free(xn); | |
746 | else | |
747 | sk_X509_NAME_push(stack, xn); | |
748 | } | |
749 | ||
750 | ERR_clear_error(); | |
66696478 | 751 | goto done; |
0f113f3e | 752 | |
0f113f3e MC |
753 | err: |
754 | ret = 0; | |
66696478 | 755 | done: |
ca3a82c3 | 756 | BIO_free(in); |
25aaa98a | 757 | X509_free(x); |
0f113f3e | 758 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp); |
0f113f3e MC |
759 | return ret; |
760 | } | |
761 | ||
762 | /** | |
eb90a483 BL |
763 | * Add a directory of certs to a stack. |
764 | * \param stack the stack to append to. | |
765 | * \param dir the directory to append from. All files in this directory will be | |
766 | * examined as potential certs. Any that are acceptable to | |
72e442a3 | 767 | * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be |
eb90a483 BL |
768 | * included. |
769 | * \return 1 for success, 0 for failure. Note that in the case of failure some | |
770 | * certs may have been added to \c stack. | |
771 | */ | |
772 | ||
661b361b | 773 | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
0f113f3e MC |
774 | const char *dir) |
775 | { | |
776 | OPENSSL_DIR_CTX *d = NULL; | |
777 | const char *filename; | |
778 | int ret = 0; | |
eb90a483 | 779 | |
0f113f3e | 780 | CRYPTO_w_lock(CRYPTO_LOCK_READDIR); |
4083a229 | 781 | |
0f113f3e | 782 | /* Note that a side effect is that the CAs will be sorted by name */ |
4083a229 | 783 | |
0f113f3e MC |
784 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
785 | char buf[1024]; | |
786 | int r; | |
4083a229 | 787 | |
0f113f3e MC |
788 | if (strlen(dir) + strlen(filename) + 2 > sizeof buf) { |
789 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, | |
790 | SSL_R_PATH_TOO_LONG); | |
791 | goto err; | |
792 | } | |
4083a229 | 793 | #ifdef OPENSSL_SYS_VMS |
0f113f3e | 794 | r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename); |
4083a229 | 795 | #else |
0f113f3e | 796 | r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename); |
4083a229 | 797 | #endif |
0f113f3e MC |
798 | if (r <= 0 || r >= (int)sizeof(buf)) |
799 | goto err; | |
800 | if (!SSL_add_file_cert_subjects_to_stack(stack, buf)) | |
801 | goto err; | |
802 | } | |
803 | ||
804 | if (errno) { | |
805 | SYSerr(SYS_F_OPENDIR, get_last_sys_error()); | |
806 | ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')"); | |
807 | SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB); | |
808 | goto err; | |
809 | } | |
810 | ||
811 | ret = 1; | |
812 | ||
813 | err: | |
814 | if (d) | |
815 | OPENSSL_DIR_end(&d); | |
816 | CRYPTO_w_unlock(CRYPTO_LOCK_READDIR); | |
817 | return ret; | |
818 | } | |
285046ec | 819 | |
4379d0e4 DSH |
820 | /* Add a certificate to a BUF_MEM structure */ |
821 | ||
822 | static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) | |
0f113f3e MC |
823 | { |
824 | int n; | |
825 | unsigned char *p; | |
826 | ||
827 | n = i2d_X509(x, NULL); | |
828 | if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) { | |
829 | SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB); | |
830 | return 0; | |
831 | } | |
832 | p = (unsigned char *)&(buf->data[*l]); | |
833 | l2n3(n, p); | |
834 | i2d_X509(x, &p); | |
835 | *l += n + 3; | |
836 | ||
837 | return 1; | |
838 | } | |
4379d0e4 DSH |
839 | |
840 | /* Add certificate chain to internal SSL BUF_MEM strcuture */ | |
c526ed41 | 841 | int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) |
0f113f3e MC |
842 | { |
843 | BUF_MEM *buf = s->init_buf; | |
844 | int i; | |
845 | ||
846 | X509 *x; | |
847 | STACK_OF(X509) *extra_certs; | |
848 | X509_STORE *chain_store; | |
849 | ||
850 | /* TLSv1 sends a chain with nothing in it, instead of an alert */ | |
851 | if (!BUF_MEM_grow_clean(buf, 10)) { | |
852 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB); | |
853 | return 0; | |
854 | } | |
855 | ||
856 | if (!cpk || !cpk->x509) | |
857 | return 1; | |
858 | ||
859 | x = cpk->x509; | |
860 | ||
861 | /* | |
862 | * If we have a certificate specific chain use it, else use parent ctx. | |
863 | */ | |
864 | if (cpk->chain) | |
865 | extra_certs = cpk->chain; | |
866 | else | |
867 | extra_certs = s->ctx->extra_certs; | |
868 | ||
869 | if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) | |
870 | chain_store = NULL; | |
871 | else if (s->cert->chain_store) | |
872 | chain_store = s->cert->chain_store; | |
873 | else | |
874 | chain_store = s->ctx->cert_store; | |
875 | ||
876 | if (chain_store) { | |
877 | X509_STORE_CTX xs_ctx; | |
878 | ||
879 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) { | |
880 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB); | |
881 | return (0); | |
882 | } | |
ae4d0c8d MC |
883 | /* |
884 | * It is valid for the chain not to be complete (because normally we | |
885 | * don't include the root cert in the chain). Therefore we deliberately | |
886 | * ignore the error return from this call. We're not actually verifying | |
887 | * the cert - we're just building as much of the chain as we can | |
888 | */ | |
0f113f3e MC |
889 | X509_verify_cert(&xs_ctx); |
890 | /* Don't leave errors in the queue */ | |
891 | ERR_clear_error(); | |
892 | i = ssl_security_cert_chain(s, xs_ctx.chain, NULL, 0); | |
893 | if (i != 1) { | |
894 | X509_STORE_CTX_cleanup(&xs_ctx); | |
895 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
896 | return 0; | |
897 | } | |
898 | for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) { | |
899 | x = sk_X509_value(xs_ctx.chain, i); | |
900 | ||
901 | if (!ssl_add_cert_to_buf(buf, l, x)) { | |
902 | X509_STORE_CTX_cleanup(&xs_ctx); | |
903 | return 0; | |
904 | } | |
905 | } | |
906 | X509_STORE_CTX_cleanup(&xs_ctx); | |
907 | } else { | |
908 | i = ssl_security_cert_chain(s, extra_certs, x, 0); | |
909 | if (i != 1) { | |
910 | SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i); | |
911 | return 0; | |
912 | } | |
913 | if (!ssl_add_cert_to_buf(buf, l, x)) | |
914 | return 0; | |
915 | for (i = 0; i < sk_X509_num(extra_certs); i++) { | |
916 | x = sk_X509_value(extra_certs, i); | |
917 | if (!ssl_add_cert_to_buf(buf, l, x)) | |
918 | return 0; | |
919 | } | |
920 | } | |
921 | return 1; | |
922 | } | |
4379d0e4 | 923 | |
74ecfab4 | 924 | /* Build a certificate chain for current certificate */ |
b362ccab | 925 | int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags) |
0f113f3e MC |
926 | { |
927 | CERT *c = s ? s->cert : ctx->cert; | |
928 | CERT_PKEY *cpk = c->key; | |
929 | X509_STORE *chain_store = NULL; | |
930 | X509_STORE_CTX xs_ctx; | |
931 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; | |
932 | X509 *x; | |
933 | int i, rv = 0; | |
934 | unsigned long error; | |
935 | ||
936 | if (!cpk->x509) { | |
937 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET); | |
938 | goto err; | |
939 | } | |
940 | /* Rearranging and check the chain: add everything to a store */ | |
941 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { | |
942 | chain_store = X509_STORE_new(); | |
a71edf3b | 943 | if (chain_store == NULL) |
0f113f3e MC |
944 | goto err; |
945 | for (i = 0; i < sk_X509_num(cpk->chain); i++) { | |
946 | x = sk_X509_value(cpk->chain, i); | |
947 | if (!X509_STORE_add_cert(chain_store, x)) { | |
948 | error = ERR_peek_last_error(); | |
949 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || | |
950 | ERR_GET_REASON(error) != | |
951 | X509_R_CERT_ALREADY_IN_HASH_TABLE) | |
952 | goto err; | |
953 | ERR_clear_error(); | |
954 | } | |
955 | } | |
956 | /* Add EE cert too: it might be self signed */ | |
957 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) { | |
958 | error = ERR_peek_last_error(); | |
959 | if (ERR_GET_LIB(error) != ERR_LIB_X509 || | |
960 | ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) | |
961 | goto err; | |
962 | ERR_clear_error(); | |
963 | } | |
964 | } else { | |
965 | if (c->chain_store) | |
966 | chain_store = c->chain_store; | |
967 | else if (s) | |
968 | chain_store = s->ctx->cert_store; | |
969 | else | |
970 | chain_store = ctx->cert_store; | |
971 | ||
972 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) | |
973 | untrusted = cpk->chain; | |
974 | } | |
975 | ||
976 | if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) { | |
977 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB); | |
978 | goto err; | |
979 | } | |
980 | /* Set suite B flags if needed */ | |
981 | X509_STORE_CTX_set_flags(&xs_ctx, | |
982 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); | |
983 | ||
984 | i = X509_verify_cert(&xs_ctx); | |
985 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { | |
986 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) | |
987 | ERR_clear_error(); | |
988 | i = 1; | |
989 | rv = 2; | |
990 | } | |
991 | if (i > 0) | |
992 | chain = X509_STORE_CTX_get1_chain(&xs_ctx); | |
993 | if (i <= 0) { | |
994 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED); | |
995 | i = X509_STORE_CTX_get_error(&xs_ctx); | |
996 | ERR_add_error_data(2, "Verify error:", | |
997 | X509_verify_cert_error_string(i)); | |
998 | ||
999 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1000 | goto err; | |
1001 | } | |
1002 | X509_STORE_CTX_cleanup(&xs_ctx); | |
1003 | /* Remove EE certificate from chain */ | |
1004 | x = sk_X509_shift(chain); | |
1005 | X509_free(x); | |
1006 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { | |
1007 | if (sk_X509_num(chain) > 0) { | |
1008 | /* See if last cert is self signed */ | |
1009 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); | |
a8d8e06b | 1010 | if (X509_get_extension_flags(x) & EXFLAG_SS) { |
0f113f3e MC |
1011 | x = sk_X509_pop(chain); |
1012 | X509_free(x); | |
1013 | } | |
1014 | } | |
1015 | } | |
1016 | /* | |
1017 | * Check security level of all CA certificates: EE will have been checked | |
1018 | * already. | |
1019 | */ | |
1020 | for (i = 0; i < sk_X509_num(chain); i++) { | |
1021 | x = sk_X509_value(chain, i); | |
1022 | rv = ssl_security_cert(s, ctx, x, 0, 0); | |
1023 | if (rv != 1) { | |
1024 | SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv); | |
1025 | sk_X509_pop_free(chain, X509_free); | |
1026 | rv = 0; | |
1027 | goto err; | |
1028 | } | |
1029 | } | |
222561fe | 1030 | sk_X509_pop_free(cpk->chain, X509_free); |
0f113f3e MC |
1031 | cpk->chain = chain; |
1032 | if (rv == 0) | |
1033 | rv = 1; | |
1034 | err: | |
1035 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) | |
1036 | X509_STORE_free(chain_store); | |
1037 | ||
1038 | return rv; | |
1039 | } | |
74ecfab4 DSH |
1040 | |
1041 | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) | |
0f113f3e MC |
1042 | { |
1043 | X509_STORE **pstore; | |
1044 | if (chain) | |
1045 | pstore = &c->chain_store; | |
1046 | else | |
1047 | pstore = &c->verify_store; | |
222561fe | 1048 | X509_STORE_free(*pstore); |
0f113f3e MC |
1049 | *pstore = store; |
1050 | if (ref && store) | |
1051 | CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE); | |
1052 | return 1; | |
1053 | } | |
1054 | ||
1055 | static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op, | |
1056 | int bits, int nid, void *other, | |
1057 | void *ex) | |
1058 | { | |
1059 | int level, minbits; | |
1060 | static const int minbits_table[5] = { 80, 112, 128, 192, 256 }; | |
1061 | if (ctx) | |
1062 | level = SSL_CTX_get_security_level(ctx); | |
1063 | else | |
1064 | level = SSL_get_security_level(s); | |
a7cf07b4 VD |
1065 | |
1066 | if (level <= 0) { | |
1067 | /* | |
1068 | * No EDH keys weaker than 1024-bits even at level 0, otherwise, | |
1069 | * anything goes. | |
1070 | */ | |
1071 | if (op == SSL_SECOP_TMP_DH && bits < 80) | |
1072 | return 0; | |
0f113f3e | 1073 | return 1; |
a7cf07b4 | 1074 | } |
0f113f3e MC |
1075 | if (level > 5) |
1076 | level = 5; | |
1077 | minbits = minbits_table[level - 1]; | |
1078 | switch (op) { | |
1079 | case SSL_SECOP_CIPHER_SUPPORTED: | |
1080 | case SSL_SECOP_CIPHER_SHARED: | |
1081 | case SSL_SECOP_CIPHER_CHECK: | |
1082 | { | |
1083 | const SSL_CIPHER *c = other; | |
1084 | /* No ciphers below security level */ | |
1085 | if (bits < minbits) | |
1086 | return 0; | |
1087 | /* No unauthenticated ciphersuites */ | |
1088 | if (c->algorithm_auth & SSL_aNULL) | |
1089 | return 0; | |
1090 | /* No MD5 mac ciphersuites */ | |
1091 | if (c->algorithm_mac & SSL_MD5) | |
1092 | return 0; | |
1093 | /* SHA1 HMAC is 160 bits of security */ | |
1094 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1) | |
1095 | return 0; | |
1096 | /* Level 2: no RC4 */ | |
1097 | if (level >= 2 && c->algorithm_enc == SSL_RC4) | |
1098 | return 0; | |
1099 | /* Level 3: forward secure ciphersuites only */ | |
1100 | if (level >= 3 && !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH))) | |
1101 | return 0; | |
1102 | break; | |
1103 | } | |
1104 | case SSL_SECOP_VERSION: | |
4fa52141 VD |
1105 | if (!SSL_IS_DTLS(s)) { |
1106 | /* SSLv3 not allowed at level 2 */ | |
1107 | if (nid <= SSL3_VERSION && level >= 2) | |
1108 | return 0; | |
1109 | /* TLS v1.1 and above only for level 3 */ | |
1110 | if (nid <= TLS1_VERSION && level >= 3) | |
1111 | return 0; | |
1112 | /* TLS v1.2 only for level 4 and above */ | |
1113 | if (nid <= TLS1_1_VERSION && level >= 4) | |
1114 | return 0; | |
1115 | } else { | |
1116 | /* DTLS v1.2 only for level 4 and above */ | |
1117 | if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4) | |
1118 | return 0; | |
1119 | } | |
0f113f3e MC |
1120 | break; |
1121 | ||
1122 | case SSL_SECOP_COMPRESSION: | |
1123 | if (level >= 2) | |
1124 | return 0; | |
1125 | break; | |
1126 | case SSL_SECOP_TICKET: | |
1127 | if (level >= 3) | |
1128 | return 0; | |
1129 | break; | |
1130 | default: | |
1131 | if (bits < minbits) | |
1132 | return 0; | |
1133 | } | |
1134 | return 1; | |
1135 | } | |
b362ccab DSH |
1136 | |
1137 | int ssl_security(SSL *s, int op, int bits, int nid, void *other) | |
0f113f3e MC |
1138 | { |
1139 | return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex); | |
1140 | } | |
b362ccab DSH |
1141 | |
1142 | int ssl_ctx_security(SSL_CTX *ctx, int op, int bits, int nid, void *other) | |
0f113f3e MC |
1143 | { |
1144 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other, | |
1145 | ctx->cert->sec_ex); | |
1146 | } |