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