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