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