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