/src/openssl/ssl/ssl_cert.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
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 |
9 | | */ |
10 | | |
11 | | #include "internal/e_os.h" |
12 | | |
13 | | #include <stdio.h> |
14 | | #include <sys/types.h> |
15 | | |
16 | | #include "internal/nelem.h" |
17 | | #include "internal/o_dir.h" |
18 | | #include <openssl/bio.h> |
19 | | #include <openssl/pem.h> |
20 | | #include <openssl/store.h> |
21 | | #include <openssl/x509v3.h> |
22 | | #include <openssl/dh.h> |
23 | | #include <openssl/bn.h> |
24 | | #include <openssl/crypto.h> |
25 | | #include "internal/refcount.h" |
26 | | #include "ssl_local.h" |
27 | | #include "ssl_cert_table.h" |
28 | | #include "internal/thread_once.h" |
29 | | #include "internal/ssl_unwrap.h" |
30 | | #ifndef OPENSSL_NO_POSIX_IO |
31 | | #include <sys/stat.h> |
32 | | #ifdef _WIN32 |
33 | | #define stat _stat |
34 | | #endif |
35 | | #ifndef S_ISDIR |
36 | | #define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) |
37 | | #endif |
38 | | #endif |
39 | | |
40 | | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, |
41 | | int op, int bits, int nid, void *other, |
42 | | void *ex); |
43 | | |
44 | | static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT; |
45 | | static volatile int ssl_x509_store_ctx_idx = -1; |
46 | | |
47 | | DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init) |
48 | 0 | { |
49 | 0 | ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0, |
50 | 0 | "SSL for verify callback", |
51 | 0 | NULL, NULL, NULL); |
52 | 0 | return ssl_x509_store_ctx_idx >= 0; |
53 | 0 | } |
54 | | |
55 | | int SSL_get_ex_data_X509_STORE_CTX_idx(void) |
56 | 0 | { |
57 | |
|
58 | 0 | if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init)) |
59 | 0 | return -1; |
60 | 0 | return ssl_x509_store_ctx_idx; |
61 | 0 | } |
62 | | |
63 | | CERT *ssl_cert_new(size_t ssl_pkey_num) |
64 | 0 | { |
65 | 0 | CERT *ret = NULL; |
66 | | |
67 | | /* Should never happen */ |
68 | 0 | if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM)) |
69 | 0 | return NULL; |
70 | | |
71 | 0 | ret = OPENSSL_zalloc(sizeof(*ret)); |
72 | 0 | if (ret == NULL) |
73 | 0 | return NULL; |
74 | | |
75 | 0 | ret->ssl_pkey_num = ssl_pkey_num; |
76 | 0 | ret->pkeys = OPENSSL_calloc(ret->ssl_pkey_num, sizeof(CERT_PKEY)); |
77 | 0 | if (ret->pkeys == NULL) { |
78 | 0 | OPENSSL_free(ret); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | | |
82 | 0 | ret->key = &(ret->pkeys[SSL_PKEY_RSA]); |
83 | 0 | ret->sec_cb = ssl_security_default_callback; |
84 | 0 | ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL; |
85 | 0 | ret->sec_ex = NULL; |
86 | 0 | if (!CRYPTO_NEW_REF(&ret->references, 1)) { |
87 | 0 | OPENSSL_free(ret->pkeys); |
88 | 0 | OPENSSL_free(ret); |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | 0 | return ret; |
93 | 0 | } |
94 | | |
95 | | CERT *ssl_cert_dup(CERT *cert) |
96 | 0 | { |
97 | 0 | CERT *ret = OPENSSL_zalloc(sizeof(*ret)); |
98 | 0 | size_t i; |
99 | | #ifndef OPENSSL_NO_COMP_ALG |
100 | | int j; |
101 | | #endif |
102 | |
|
103 | 0 | if (ret == NULL) |
104 | 0 | return NULL; |
105 | | |
106 | 0 | ret->ssl_pkey_num = cert->ssl_pkey_num; |
107 | 0 | ret->pkeys = OPENSSL_calloc(ret->ssl_pkey_num, sizeof(CERT_PKEY)); |
108 | 0 | if (ret->pkeys == NULL) { |
109 | 0 | OPENSSL_free(ret); |
110 | 0 | return NULL; |
111 | 0 | } |
112 | | |
113 | 0 | ret->key = &ret->pkeys[cert->key - cert->pkeys]; |
114 | 0 | if (!CRYPTO_NEW_REF(&ret->references, 1)) { |
115 | 0 | OPENSSL_free(ret->pkeys); |
116 | 0 | OPENSSL_free(ret); |
117 | 0 | return NULL; |
118 | 0 | } |
119 | | |
120 | 0 | if (cert->dh_tmp != NULL) { |
121 | 0 | if (!EVP_PKEY_up_ref(cert->dh_tmp)) |
122 | 0 | goto err; |
123 | 0 | ret->dh_tmp = cert->dh_tmp; |
124 | 0 | } |
125 | | |
126 | 0 | ret->dh_tmp_cb = cert->dh_tmp_cb; |
127 | 0 | ret->dh_tmp_auto = cert->dh_tmp_auto; |
128 | |
|
129 | 0 | for (i = 0; i < ret->ssl_pkey_num; i++) { |
130 | 0 | CERT_PKEY *cpk = cert->pkeys + i; |
131 | 0 | CERT_PKEY *rpk = ret->pkeys + i; |
132 | |
|
133 | 0 | if (cpk->x509 != NULL) { |
134 | 0 | if (!X509_up_ref(cpk->x509)) |
135 | 0 | goto err; |
136 | 0 | rpk->x509 = cpk->x509; |
137 | 0 | } |
138 | | |
139 | 0 | if (cpk->privatekey != NULL) { |
140 | 0 | if (!EVP_PKEY_up_ref(cpk->privatekey)) |
141 | 0 | goto err; |
142 | 0 | rpk->privatekey = cpk->privatekey; |
143 | 0 | } |
144 | | |
145 | 0 | if (cpk->chain) { |
146 | 0 | rpk->chain = X509_chain_up_ref(cpk->chain); |
147 | 0 | if (!rpk->chain) { |
148 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
149 | 0 | goto err; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | if (cpk->serverinfo != NULL) { |
153 | | /* Just copy everything. */ |
154 | 0 | rpk->serverinfo = OPENSSL_memdup(cpk->serverinfo, cpk->serverinfo_length); |
155 | 0 | if (rpk->serverinfo == NULL) |
156 | 0 | goto err; |
157 | 0 | rpk->serverinfo_length = cpk->serverinfo_length; |
158 | 0 | } |
159 | | #ifndef OPENSSL_NO_COMP_ALG |
160 | | for (j = TLSEXT_comp_cert_none; j < TLSEXT_comp_cert_limit; j++) { |
161 | | if (cpk->comp_cert[j] != NULL) { |
162 | | if (!OSSL_COMP_CERT_up_ref(cpk->comp_cert[j])) |
163 | | goto err; |
164 | | rpk->comp_cert[j] = cpk->comp_cert[j]; |
165 | | } |
166 | | } |
167 | | #endif |
168 | 0 | } |
169 | | |
170 | | /* Configured sigalgs copied across */ |
171 | 0 | if (cert->conf_sigalgs) { |
172 | 0 | ret->conf_sigalgs = OPENSSL_malloc_array(cert->conf_sigalgslen, |
173 | 0 | sizeof(*cert->conf_sigalgs)); |
174 | 0 | if (ret->conf_sigalgs == NULL) |
175 | 0 | goto err; |
176 | 0 | memcpy(ret->conf_sigalgs, cert->conf_sigalgs, |
177 | 0 | cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs)); |
178 | 0 | ret->conf_sigalgslen = cert->conf_sigalgslen; |
179 | 0 | } else |
180 | 0 | ret->conf_sigalgs = NULL; |
181 | | |
182 | 0 | if (cert->client_sigalgs) { |
183 | 0 | ret->client_sigalgs = OPENSSL_malloc_array(cert->client_sigalgslen, |
184 | 0 | sizeof(*cert->client_sigalgs)); |
185 | 0 | if (ret->client_sigalgs == NULL) |
186 | 0 | goto err; |
187 | 0 | memcpy(ret->client_sigalgs, cert->client_sigalgs, |
188 | 0 | cert->client_sigalgslen * sizeof(*cert->client_sigalgs)); |
189 | 0 | ret->client_sigalgslen = cert->client_sigalgslen; |
190 | 0 | } else |
191 | 0 | ret->client_sigalgs = NULL; |
192 | | /* Copy any custom client certificate types */ |
193 | 0 | if (cert->ctype) { |
194 | 0 | ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len); |
195 | 0 | if (ret->ctype == NULL) |
196 | 0 | goto err; |
197 | 0 | ret->ctype_len = cert->ctype_len; |
198 | 0 | } |
199 | | |
200 | 0 | ret->cert_flags = cert->cert_flags; |
201 | |
|
202 | 0 | ret->cert_cb = cert->cert_cb; |
203 | 0 | ret->cert_cb_arg = cert->cert_cb_arg; |
204 | |
|
205 | 0 | if (cert->verify_store) { |
206 | 0 | if (!X509_STORE_up_ref(cert->verify_store)) |
207 | 0 | goto err; |
208 | 0 | ret->verify_store = cert->verify_store; |
209 | 0 | } |
210 | | |
211 | 0 | if (cert->chain_store) { |
212 | 0 | if (!X509_STORE_up_ref(cert->chain_store)) |
213 | 0 | goto err; |
214 | 0 | ret->chain_store = cert->chain_store; |
215 | 0 | } |
216 | | |
217 | 0 | ret->sec_cb = cert->sec_cb; |
218 | 0 | ret->sec_level = cert->sec_level; |
219 | 0 | ret->sec_ex = cert->sec_ex; |
220 | |
|
221 | 0 | if (!custom_exts_copy(&ret->custext, &cert->custext)) |
222 | 0 | goto err; |
223 | 0 | #ifndef OPENSSL_NO_PSK |
224 | 0 | if (cert->psk_identity_hint) { |
225 | 0 | ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint); |
226 | 0 | if (ret->psk_identity_hint == NULL) |
227 | 0 | goto err; |
228 | 0 | } |
229 | 0 | #endif |
230 | 0 | return ret; |
231 | | |
232 | 0 | err: |
233 | 0 | ssl_cert_free(ret); |
234 | |
|
235 | 0 | return NULL; |
236 | 0 | } |
237 | | |
238 | | /* Free up and clear all certificates and chains */ |
239 | | |
240 | | void ssl_cert_clear_certs(CERT *c) |
241 | 0 | { |
242 | 0 | size_t i; |
243 | | #ifndef OPENSSL_NO_COMP_ALG |
244 | | int j; |
245 | | #endif |
246 | |
|
247 | 0 | if (c == NULL) |
248 | 0 | return; |
249 | 0 | for (i = 0; i < c->ssl_pkey_num; i++) { |
250 | 0 | CERT_PKEY *cpk = c->pkeys + i; |
251 | 0 | X509_free(cpk->x509); |
252 | 0 | cpk->x509 = NULL; |
253 | 0 | EVP_PKEY_free(cpk->privatekey); |
254 | 0 | cpk->privatekey = NULL; |
255 | 0 | OSSL_STACK_OF_X509_free(cpk->chain); |
256 | 0 | cpk->chain = NULL; |
257 | 0 | OPENSSL_free(cpk->serverinfo); |
258 | 0 | cpk->serverinfo = NULL; |
259 | 0 | cpk->serverinfo_length = 0; |
260 | | #ifndef OPENSSL_NO_COMP_ALG |
261 | | for (j = 0; j < TLSEXT_comp_cert_limit; j++) { |
262 | | OSSL_COMP_CERT_free(cpk->comp_cert[j]); |
263 | | cpk->comp_cert[j] = NULL; |
264 | | cpk->cert_comp_used = 0; |
265 | | } |
266 | | #endif |
267 | 0 | } |
268 | 0 | } |
269 | | |
270 | | void ssl_cert_free(CERT *c) |
271 | 0 | { |
272 | 0 | int i; |
273 | |
|
274 | 0 | if (c == NULL) |
275 | 0 | return; |
276 | 0 | CRYPTO_DOWN_REF(&c->references, &i); |
277 | 0 | REF_PRINT_COUNT("CERT", i, c); |
278 | 0 | if (i > 0) |
279 | 0 | return; |
280 | 0 | REF_ASSERT_ISNT(i < 0); |
281 | |
|
282 | 0 | EVP_PKEY_free(c->dh_tmp); |
283 | |
|
284 | 0 | ssl_cert_clear_certs(c); |
285 | 0 | OPENSSL_free(c->conf_sigalgs); |
286 | 0 | OPENSSL_free(c->client_sigalgs); |
287 | 0 | OPENSSL_free(c->ctype); |
288 | 0 | X509_STORE_free(c->verify_store); |
289 | 0 | X509_STORE_free(c->chain_store); |
290 | 0 | custom_exts_free(&c->custext); |
291 | 0 | #ifndef OPENSSL_NO_PSK |
292 | 0 | OPENSSL_free(c->psk_identity_hint); |
293 | 0 | #endif |
294 | 0 | OPENSSL_free(c->pkeys); |
295 | 0 | CRYPTO_FREE_REF(&c->references); |
296 | 0 | OPENSSL_free(c); |
297 | 0 | } |
298 | | |
299 | | int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
300 | 0 | { |
301 | 0 | int i, r; |
302 | 0 | CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key; |
303 | |
|
304 | 0 | if (!cpk) |
305 | 0 | return 0; |
306 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
307 | 0 | X509 *x = sk_X509_value(chain, i); |
308 | |
|
309 | 0 | r = ssl_security_cert(s, ctx, x, 0); |
310 | 0 | if (r != 1) { |
311 | 0 | ERR_raise(ERR_LIB_SSL, r); |
312 | 0 | return 0; |
313 | 0 | } |
314 | 0 | } |
315 | 0 | OSSL_STACK_OF_X509_free(cpk->chain); |
316 | 0 | cpk->chain = chain; |
317 | 0 | return 1; |
318 | 0 | } |
319 | | |
320 | | int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain) |
321 | 0 | { |
322 | 0 | STACK_OF(X509) *dchain; |
323 | |
|
324 | 0 | if (!chain) |
325 | 0 | return ssl_cert_set0_chain(s, ctx, NULL); |
326 | 0 | dchain = X509_chain_up_ref(chain); |
327 | 0 | if (!dchain) |
328 | 0 | return 0; |
329 | 0 | if (!ssl_cert_set0_chain(s, ctx, dchain)) { |
330 | 0 | OSSL_STACK_OF_X509_free(dchain); |
331 | 0 | return 0; |
332 | 0 | } |
333 | 0 | return 1; |
334 | 0 | } |
335 | | |
336 | | int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x) |
337 | 0 | { |
338 | 0 | int r; |
339 | 0 | CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key; |
340 | |
|
341 | 0 | if (!cpk) |
342 | 0 | return 0; |
343 | 0 | r = ssl_security_cert(s, ctx, x, 0); |
344 | 0 | if (r != 1) { |
345 | 0 | ERR_raise(ERR_LIB_SSL, r); |
346 | 0 | return 0; |
347 | 0 | } |
348 | 0 | if (!cpk->chain) |
349 | 0 | cpk->chain = sk_X509_new_null(); |
350 | 0 | if (!cpk->chain || !sk_X509_push(cpk->chain, x)) |
351 | 0 | return 0; |
352 | 0 | return 1; |
353 | 0 | } |
354 | | |
355 | | int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x) |
356 | 0 | { |
357 | 0 | if (!X509_up_ref(x)) |
358 | 0 | return 0; |
359 | 0 | if (!ssl_cert_add0_chain_cert(s, ctx, x)) { |
360 | 0 | X509_free(x); |
361 | 0 | return 0; |
362 | 0 | } |
363 | 0 | return 1; |
364 | 0 | } |
365 | | |
366 | | int ssl_cert_select_current(CERT *c, X509 *x) |
367 | 0 | { |
368 | 0 | size_t i; |
369 | |
|
370 | 0 | if (x == NULL) |
371 | 0 | return 0; |
372 | 0 | for (i = 0; i < c->ssl_pkey_num; i++) { |
373 | 0 | CERT_PKEY *cpk = c->pkeys + i; |
374 | 0 | if (cpk->x509 == x && cpk->privatekey) { |
375 | 0 | c->key = cpk; |
376 | 0 | return 1; |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | 0 | for (i = 0; i < c->ssl_pkey_num; i++) { |
381 | 0 | CERT_PKEY *cpk = c->pkeys + i; |
382 | 0 | if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) { |
383 | 0 | c->key = cpk; |
384 | 0 | return 1; |
385 | 0 | } |
386 | 0 | } |
387 | 0 | return 0; |
388 | 0 | } |
389 | | |
390 | | int ssl_cert_set_current(CERT *c, long op) |
391 | 0 | { |
392 | 0 | size_t i, idx; |
393 | |
|
394 | 0 | if (!c) |
395 | 0 | return 0; |
396 | 0 | if (op == SSL_CERT_SET_FIRST) |
397 | 0 | idx = 0; |
398 | 0 | else if (op == SSL_CERT_SET_NEXT) { |
399 | 0 | idx = (size_t)(c->key - c->pkeys + 1); |
400 | 0 | if (idx >= c->ssl_pkey_num) |
401 | 0 | return 0; |
402 | 0 | } else |
403 | 0 | return 0; |
404 | 0 | for (i = idx; i < c->ssl_pkey_num; i++) { |
405 | 0 | CERT_PKEY *cpk = c->pkeys + i; |
406 | 0 | if (cpk->x509 && cpk->privatekey) { |
407 | 0 | c->key = cpk; |
408 | 0 | return 1; |
409 | 0 | } |
410 | 0 | } |
411 | 0 | return 0; |
412 | 0 | } |
413 | | |
414 | | void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) |
415 | 0 | { |
416 | 0 | c->cert_cb = cb; |
417 | 0 | c->cert_cb_arg = arg; |
418 | 0 | } |
419 | | |
420 | | /* |
421 | | * Verify a certificate chain/raw public key |
422 | | * Return codes: |
423 | | * 1: Verify success |
424 | | * 0: Verify failure or error |
425 | | * -1: Retry required |
426 | | */ |
427 | | static int ssl_verify_internal(SSL_CONNECTION *s, STACK_OF(X509) *sk, EVP_PKEY *rpk) |
428 | 0 | { |
429 | 0 | X509 *x; |
430 | 0 | int i = 0; |
431 | 0 | X509_STORE *verify_store; |
432 | 0 | X509_STORE_CTX *ctx = NULL; |
433 | 0 | X509_VERIFY_PARAM *param; |
434 | 0 | SSL_CTX *sctx; |
435 | 0 | #ifndef OPENSSL_NO_OCSP |
436 | 0 | SSL *ssl; |
437 | 0 | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
438 | 0 | #endif |
439 | | |
440 | | /* Something must be passed in */ |
441 | 0 | if ((sk == NULL || sk_X509_num(sk) == 0) && rpk == NULL) |
442 | 0 | return 0; |
443 | | |
444 | | /* Only one can be set */ |
445 | 0 | if (sk != NULL && rpk != NULL) |
446 | 0 | return 0; |
447 | | |
448 | 0 | sctx = SSL_CONNECTION_GET_CTX(s); |
449 | 0 | if (s->cert->verify_store) |
450 | 0 | verify_store = s->cert->verify_store; |
451 | 0 | else |
452 | 0 | verify_store = sctx->cert_store; |
453 | |
|
454 | 0 | ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq); |
455 | 0 | if (ctx == NULL) { |
456 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
457 | 0 | return 0; |
458 | 0 | } |
459 | | |
460 | 0 | if (sk != NULL) { |
461 | 0 | x = sk_X509_value(sk, 0); |
462 | 0 | if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) { |
463 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
464 | 0 | goto end; |
465 | 0 | } |
466 | 0 | } else { |
467 | 0 | if (!X509_STORE_CTX_init_rpk(ctx, verify_store, rpk)) { |
468 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
469 | 0 | goto end; |
470 | 0 | } |
471 | 0 | } |
472 | 0 | param = X509_STORE_CTX_get0_param(ctx); |
473 | | /* |
474 | | * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some |
475 | | * point, for now a single @SECLEVEL sets the same policy for TLS crypto |
476 | | * and PKI authentication. |
477 | | */ |
478 | 0 | X509_VERIFY_PARAM_set_auth_level(param, |
479 | 0 | SSL_get_security_level(SSL_CONNECTION_GET_SSL(s))); |
480 | | |
481 | | /* Set suite B flags if needed */ |
482 | 0 | X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s)); |
483 | 0 | if (!X509_STORE_CTX_set_ex_data(ctx, |
484 | 0 | SSL_get_ex_data_X509_STORE_CTX_idx(), |
485 | 0 | SSL_CONNECTION_GET_USER_SSL(s))) |
486 | 0 | goto end; |
487 | | |
488 | | /* Verify via DANE if enabled */ |
489 | 0 | if (DANETLS_ENABLED(&s->dane)) |
490 | 0 | X509_STORE_CTX_set0_dane(ctx, &s->dane); |
491 | | |
492 | | /* |
493 | | * Set OCSP Responses for verification: |
494 | | * This function is called in the SERVER_CERTIFICATE message, in TLS 1.2 |
495 | | * the OCSP responses are sent in the CERT_STATUS message after that. |
496 | | * Therefore the verification code currently only works in TLS 1.3. |
497 | | */ |
498 | 0 | #ifndef OPENSSL_NO_OCSP |
499 | 0 | ssl = SSL_CONNECTION_GET_SSL(s); |
500 | |
|
501 | 0 | if (ssl_version_cmp(s, SSL_version(ssl), version1_3) >= 0) { |
502 | | /* ignore status_request_v2 if TLS version < 1.3 */ |
503 | 0 | int status = SSL_get_tlsext_status_type(ssl); |
504 | |
|
505 | 0 | if (status == TLSEXT_STATUSTYPE_ocsp) |
506 | 0 | X509_STORE_CTX_set_ocsp_resp(ctx, s->ext.ocsp.resp_ex); |
507 | 0 | } |
508 | 0 | #endif |
509 | | |
510 | | /* |
511 | | * We need to inherit the verify parameters. These can be determined by |
512 | | * the context: if its a server it will verify SSL client certificates or |
513 | | * vice versa. |
514 | | */ |
515 | |
|
516 | 0 | X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server"); |
517 | | /* |
518 | | * Anything non-default in "s->param" should overwrite anything in the ctx. |
519 | | */ |
520 | 0 | X509_VERIFY_PARAM_set1(param, s->param); |
521 | |
|
522 | 0 | if (s->verify_callback) |
523 | 0 | X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback); |
524 | |
|
525 | 0 | if (sctx->app_verify_callback != NULL) { |
526 | 0 | i = sctx->app_verify_callback(ctx, sctx->app_verify_arg); |
527 | 0 | } else { |
528 | 0 | i = X509_verify_cert(ctx); |
529 | | /* We treat an error in the same way as a failure to verify */ |
530 | 0 | if (i < 0) |
531 | 0 | i = 0; |
532 | 0 | } |
533 | |
|
534 | 0 | s->verify_result = X509_STORE_CTX_get_error(ctx); |
535 | 0 | OSSL_STACK_OF_X509_free(s->verified_chain); |
536 | 0 | s->verified_chain = NULL; |
537 | |
|
538 | 0 | if (sk != NULL && X509_STORE_CTX_get0_chain(ctx) != NULL) { |
539 | 0 | s->verified_chain = X509_STORE_CTX_get1_chain(ctx); |
540 | 0 | if (s->verified_chain == NULL) { |
541 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
542 | 0 | i = 0; |
543 | 0 | } |
544 | 0 | } |
545 | | |
546 | | /* Move peername from the store context params to the SSL handle's */ |
547 | 0 | X509_VERIFY_PARAM_move_peername(s->param, param); |
548 | |
|
549 | 0 | end: |
550 | 0 | X509_STORE_CTX_free(ctx); |
551 | 0 | return i; |
552 | 0 | } |
553 | | |
554 | | /* |
555 | | * Verify a raw public key |
556 | | * Return codes: |
557 | | * 1: Verify success |
558 | | * 0: Verify failure or error |
559 | | * -1: Retry required |
560 | | */ |
561 | | int ssl_verify_rpk(SSL_CONNECTION *s, EVP_PKEY *rpk) |
562 | 0 | { |
563 | 0 | return ssl_verify_internal(s, NULL, rpk); |
564 | 0 | } |
565 | | |
566 | | /* |
567 | | * Verify a certificate chain |
568 | | * Return codes: |
569 | | * 1: Verify success |
570 | | * 0: Verify failure or error |
571 | | * -1: Retry required |
572 | | */ |
573 | | int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk) |
574 | 0 | { |
575 | 0 | return ssl_verify_internal(s, sk, NULL); |
576 | 0 | } |
577 | | |
578 | | static void set0_CA_list(STACK_OF(X509_NAME) **ca_list, |
579 | | STACK_OF(X509_NAME) *name_list) |
580 | 0 | { |
581 | 0 | sk_X509_NAME_pop_free(*ca_list, X509_NAME_free); |
582 | 0 | *ca_list = name_list; |
583 | 0 | } |
584 | | |
585 | | STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk) |
586 | 0 | { |
587 | 0 | int i; |
588 | 0 | const int num = sk_X509_NAME_num(sk); |
589 | 0 | STACK_OF(X509_NAME) *ret; |
590 | 0 | X509_NAME *name; |
591 | |
|
592 | 0 | ret = sk_X509_NAME_new_reserve(NULL, num); |
593 | 0 | if (ret == NULL) { |
594 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
595 | 0 | return NULL; |
596 | 0 | } |
597 | 0 | for (i = 0; i < num; i++) { |
598 | 0 | name = X509_NAME_dup(sk_X509_NAME_value(sk, i)); |
599 | 0 | if (name == NULL) { |
600 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
601 | 0 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
602 | 0 | return NULL; |
603 | 0 | } |
604 | 0 | sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */ |
605 | 0 | } |
606 | 0 | return ret; |
607 | 0 | } |
608 | | |
609 | | void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) |
610 | 0 | { |
611 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
612 | |
|
613 | 0 | if (sc == NULL) |
614 | 0 | return; |
615 | | |
616 | 0 | set0_CA_list(&sc->ca_names, name_list); |
617 | 0 | } |
618 | | |
619 | | void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) |
620 | 0 | { |
621 | 0 | set0_CA_list(&ctx->ca_names, name_list); |
622 | 0 | } |
623 | | |
624 | | const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx) |
625 | 0 | { |
626 | 0 | return ctx->ca_names; |
627 | 0 | } |
628 | | |
629 | | const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s) |
630 | 0 | { |
631 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
632 | |
|
633 | 0 | if (sc == NULL) |
634 | 0 | return NULL; |
635 | | |
636 | 0 | return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names; |
637 | 0 | } |
638 | | |
639 | | void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) |
640 | 0 | { |
641 | 0 | set0_CA_list(&ctx->client_ca_names, name_list); |
642 | 0 | } |
643 | | |
644 | | STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) |
645 | 0 | { |
646 | 0 | return ctx->client_ca_names; |
647 | 0 | } |
648 | | |
649 | | void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) |
650 | 0 | { |
651 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
652 | |
|
653 | 0 | if (sc == NULL) |
654 | 0 | return; |
655 | | |
656 | 0 | set0_CA_list(&sc->client_ca_names, name_list); |
657 | 0 | } |
658 | | |
659 | | const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s) |
660 | 0 | { |
661 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
662 | |
|
663 | 0 | if (sc == NULL) |
664 | 0 | return NULL; |
665 | | |
666 | 0 | return sc->s3.tmp.peer_ca_names; |
667 | 0 | } |
668 | | |
669 | | STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) |
670 | 0 | { |
671 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
672 | |
|
673 | 0 | if (sc == NULL) |
674 | 0 | return NULL; |
675 | | |
676 | 0 | if (!sc->server) |
677 | 0 | return sc->s3.tmp.peer_ca_names; |
678 | 0 | return sc->client_ca_names != NULL ? sc->client_ca_names |
679 | 0 | : s->ctx->client_ca_names; |
680 | 0 | } |
681 | | |
682 | | static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x) |
683 | 0 | { |
684 | 0 | X509_NAME *name; |
685 | |
|
686 | 0 | if (x == NULL) |
687 | 0 | return 0; |
688 | 0 | if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL)) |
689 | 0 | return 0; |
690 | | |
691 | 0 | if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL) |
692 | 0 | return 0; |
693 | | |
694 | 0 | if (!sk_X509_NAME_push(*sk, name)) { |
695 | 0 | X509_NAME_free(name); |
696 | 0 | return 0; |
697 | 0 | } |
698 | 0 | return 1; |
699 | 0 | } |
700 | | |
701 | | int SSL_add1_to_CA_list(SSL *ssl, const X509 *x) |
702 | 0 | { |
703 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
704 | |
|
705 | 0 | if (sc == NULL) |
706 | 0 | return 0; |
707 | | |
708 | 0 | return add_ca_name(&sc->ca_names, x); |
709 | 0 | } |
710 | | |
711 | | int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x) |
712 | 0 | { |
713 | 0 | return add_ca_name(&ctx->ca_names, x); |
714 | 0 | } |
715 | | |
716 | | /* |
717 | | * The following two are older names are to be replaced with |
718 | | * SSL(_CTX)_add1_to_CA_list |
719 | | */ |
720 | | int SSL_add_client_CA(SSL *ssl, X509 *x) |
721 | 0 | { |
722 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
723 | |
|
724 | 0 | if (sc == NULL) |
725 | 0 | return 0; |
726 | | |
727 | 0 | return add_ca_name(&sc->client_ca_names, x); |
728 | 0 | } |
729 | | |
730 | | int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) |
731 | 0 | { |
732 | 0 | return add_ca_name(&ctx->client_ca_names, x); |
733 | 0 | } |
734 | | |
735 | | static int xname_cmp(const X509_NAME *a, const X509_NAME *b) |
736 | 0 | { |
737 | 0 | unsigned char *abuf = NULL, *bbuf = NULL; |
738 | 0 | int alen, blen, ret; |
739 | | |
740 | | /* X509_NAME_cmp() itself casts away constness in this way, so |
741 | | * assume it's safe: |
742 | | */ |
743 | 0 | alen = i2d_X509_NAME(a, &abuf); |
744 | 0 | blen = i2d_X509_NAME(b, &bbuf); |
745 | |
|
746 | 0 | if (alen < 0 || blen < 0) |
747 | 0 | ret = -2; |
748 | 0 | else if (alen != blen) |
749 | 0 | ret = alen - blen; |
750 | 0 | else /* alen == blen */ |
751 | 0 | ret = memcmp(abuf, bbuf, alen); |
752 | |
|
753 | 0 | OPENSSL_free(abuf); |
754 | 0 | OPENSSL_free(bbuf); |
755 | |
|
756 | 0 | return ret; |
757 | 0 | } |
758 | | |
759 | | static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
760 | 0 | { |
761 | 0 | return xname_cmp(*a, *b); |
762 | 0 | } |
763 | | |
764 | | static unsigned long xname_hash(const X509_NAME *a) |
765 | 0 | { |
766 | | /* This returns 0 also if SHA1 is not available */ |
767 | 0 | return X509_NAME_hash_ex(a, NULL, NULL, NULL); |
768 | 0 | } |
769 | | |
770 | | STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file, |
771 | | OSSL_LIB_CTX *libctx, |
772 | | const char *propq) |
773 | 0 | { |
774 | 0 | BIO *in = BIO_new(BIO_s_file()); |
775 | 0 | X509 *x = NULL; |
776 | 0 | const X509_NAME *cxn = NULL; |
777 | 0 | X509_NAME *xn = NULL; |
778 | 0 | STACK_OF(X509_NAME) *ret = NULL; |
779 | 0 | LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp); |
780 | 0 | OSSL_LIB_CTX *prev_libctx = NULL; |
781 | |
|
782 | 0 | if (file == NULL) { |
783 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
784 | 0 | goto err; |
785 | 0 | } |
786 | 0 | if (name_hash == NULL) { |
787 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
788 | 0 | goto err; |
789 | 0 | } |
790 | 0 | if (in == NULL) { |
791 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB); |
792 | 0 | goto err; |
793 | 0 | } |
794 | | |
795 | 0 | x = X509_new_ex(libctx, propq); |
796 | 0 | if (x == NULL) { |
797 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
798 | 0 | goto err; |
799 | 0 | } |
800 | 0 | if (BIO_read_filename(in, file) <= 0) |
801 | 0 | goto err; |
802 | | |
803 | | /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */ |
804 | 0 | prev_libctx = OSSL_LIB_CTX_set0_default(libctx); |
805 | 0 | for (;;) { |
806 | 0 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
807 | 0 | break; |
808 | 0 | if (ret == NULL) { |
809 | 0 | ret = sk_X509_NAME_new_null(); |
810 | 0 | if (ret == NULL) { |
811 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
812 | 0 | goto err; |
813 | 0 | } |
814 | 0 | } |
815 | 0 | if ((cxn = X509_get_subject_name(x)) == NULL) |
816 | 0 | goto err; |
817 | | /* check for duplicates */ |
818 | 0 | xn = X509_NAME_dup(cxn); |
819 | 0 | if (xn == NULL) |
820 | 0 | goto err; |
821 | 0 | if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) { |
822 | | /* Duplicate. */ |
823 | 0 | X509_NAME_free(xn); |
824 | 0 | xn = NULL; |
825 | 0 | } else { |
826 | 0 | lh_X509_NAME_insert(name_hash, xn); |
827 | 0 | if (!sk_X509_NAME_push(ret, xn)) |
828 | 0 | goto err; |
829 | 0 | } |
830 | 0 | } |
831 | 0 | goto done; |
832 | | |
833 | 0 | err: |
834 | 0 | X509_NAME_free(xn); |
835 | 0 | sk_X509_NAME_pop_free(ret, X509_NAME_free); |
836 | 0 | ret = NULL; |
837 | 0 | done: |
838 | | /* restore the old libctx */ |
839 | 0 | OSSL_LIB_CTX_set0_default(prev_libctx); |
840 | 0 | BIO_free(in); |
841 | 0 | X509_free(x); |
842 | 0 | lh_X509_NAME_free(name_hash); |
843 | 0 | if (ret != NULL) |
844 | 0 | ERR_clear_error(); |
845 | 0 | return ret; |
846 | 0 | } |
847 | | |
848 | | STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) |
849 | 0 | { |
850 | 0 | return SSL_load_client_CA_file_ex(file, NULL, NULL); |
851 | 0 | } |
852 | | |
853 | | static int add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
854 | | const char *file, |
855 | | LHASH_OF(X509_NAME) *name_hash) |
856 | 0 | { |
857 | 0 | BIO *in; |
858 | 0 | X509 *x = NULL; |
859 | 0 | const X509_NAME *cxn = NULL; |
860 | 0 | X509_NAME *xn = NULL; |
861 | 0 | int ret = 1; |
862 | |
|
863 | 0 | in = BIO_new(BIO_s_file()); |
864 | |
|
865 | 0 | if (in == NULL) { |
866 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB); |
867 | 0 | goto err; |
868 | 0 | } |
869 | | |
870 | 0 | if (BIO_read_filename(in, file) <= 0) |
871 | 0 | goto err; |
872 | | |
873 | 0 | for (;;) { |
874 | 0 | if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) |
875 | 0 | break; |
876 | 0 | if ((cxn = X509_get_subject_name(x)) == NULL) |
877 | 0 | goto err; |
878 | 0 | xn = X509_NAME_dup(cxn); |
879 | 0 | if (xn == NULL) |
880 | 0 | goto err; |
881 | 0 | if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) { |
882 | | /* Duplicate. */ |
883 | 0 | X509_NAME_free(xn); |
884 | 0 | } else if (!sk_X509_NAME_push(stack, xn)) { |
885 | 0 | X509_NAME_free(xn); |
886 | 0 | goto err; |
887 | 0 | } else { |
888 | | /* Successful insert, add to hash table */ |
889 | 0 | lh_X509_NAME_insert(name_hash, xn); |
890 | 0 | } |
891 | 0 | } |
892 | | |
893 | 0 | ERR_clear_error(); |
894 | 0 | goto done; |
895 | | |
896 | 0 | err: |
897 | 0 | ret = 0; |
898 | 0 | done: |
899 | 0 | BIO_free(in); |
900 | 0 | X509_free(x); |
901 | 0 | return ret; |
902 | 0 | } |
903 | | |
904 | | int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
905 | | const char *file) |
906 | 0 | { |
907 | 0 | X509_NAME *xn = NULL; |
908 | 0 | int ret = 1; |
909 | 0 | int idx = 0; |
910 | 0 | int num = 0; |
911 | 0 | LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp); |
912 | |
|
913 | 0 | if (file == NULL) { |
914 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
915 | 0 | goto err; |
916 | 0 | } |
917 | | |
918 | 0 | if (name_hash == NULL) { |
919 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
920 | 0 | goto err; |
921 | 0 | } |
922 | | |
923 | | /* |
924 | | * Pre-populate the lhash with the existing entries of the stack, since |
925 | | * using the LHASH_OF is much faster for duplicate checking. That's because |
926 | | * xname_cmp converts the X509_NAMEs to DER involving a memory allocation |
927 | | * for every single invocation of the comparison function. |
928 | | */ |
929 | 0 | num = sk_X509_NAME_num(stack); |
930 | 0 | for (idx = 0; idx < num; idx++) { |
931 | 0 | xn = sk_X509_NAME_value(stack, idx); |
932 | 0 | lh_X509_NAME_insert(name_hash, xn); |
933 | 0 | } |
934 | |
|
935 | 0 | ret = add_file_cert_subjects_to_stack(stack, file, name_hash); |
936 | 0 | goto done; |
937 | | |
938 | 0 | err: |
939 | 0 | ret = 0; |
940 | 0 | done: |
941 | 0 | lh_X509_NAME_free(name_hash); |
942 | 0 | return ret; |
943 | 0 | } |
944 | | |
945 | | int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
946 | | const char *dir) |
947 | 0 | { |
948 | 0 | OPENSSL_DIR_CTX *d = NULL; |
949 | 0 | const char *filename; |
950 | 0 | int ret = 0; |
951 | 0 | X509_NAME *xn = NULL; |
952 | 0 | int idx = 0; |
953 | 0 | int num = 0; |
954 | 0 | LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp); |
955 | |
|
956 | 0 | if (name_hash == NULL) { |
957 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
958 | 0 | goto err; |
959 | 0 | } |
960 | | |
961 | | /* |
962 | | * Pre-populate the lhash with the existing entries of the stack, since |
963 | | * using the LHASH_OF is much faster for duplicate checking. That's because |
964 | | * xname_cmp converts the X509_NAMEs to DER involving a memory allocation |
965 | | * for every single invocation of the comparison function. |
966 | | */ |
967 | 0 | num = sk_X509_NAME_num(stack); |
968 | 0 | for (idx = 0; idx < num; idx++) { |
969 | 0 | xn = sk_X509_NAME_value(stack, idx); |
970 | 0 | lh_X509_NAME_insert(name_hash, xn); |
971 | 0 | } |
972 | |
|
973 | 0 | while ((filename = OPENSSL_DIR_read(&d, dir))) { |
974 | 0 | char buf[1024]; |
975 | 0 | int r; |
976 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
977 | 0 | struct stat st; |
978 | |
|
979 | | #else |
980 | | /* Cannot use stat so just skip current and parent directories */ |
981 | | if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) |
982 | | continue; |
983 | | #endif |
984 | | #ifdef OPENSSL_SYS_VMS |
985 | | r = snprintf(buf, sizeof(buf), "%s%s", dir, filename); |
986 | | #else |
987 | 0 | r = snprintf(buf, sizeof(buf), "%s/%s", dir, filename); |
988 | 0 | #endif |
989 | 0 | if (r < 0 || (size_t)r >= sizeof(buf)) { |
990 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG); |
991 | 0 | goto err; |
992 | 0 | } |
993 | 0 | #ifndef OPENSSL_NO_POSIX_IO |
994 | | /* Skip subdirectories */ |
995 | 0 | if (!stat(buf, &st) && S_ISDIR(st.st_mode)) |
996 | 0 | continue; |
997 | 0 | #endif |
998 | 0 | if (!add_file_cert_subjects_to_stack(stack, buf, name_hash)) |
999 | 0 | goto err; |
1000 | 0 | } |
1001 | | |
1002 | 0 | if (errno) { |
1003 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
1004 | 0 | "calling OPENSSL_dir_read(%s)", dir); |
1005 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB); |
1006 | 0 | goto err; |
1007 | 0 | } |
1008 | | |
1009 | 0 | ret = 1; |
1010 | |
|
1011 | 0 | err: |
1012 | 0 | if (d) |
1013 | 0 | OPENSSL_DIR_end(&d); |
1014 | 0 | lh_X509_NAME_free(name_hash); |
1015 | |
|
1016 | 0 | return ret; |
1017 | 0 | } |
1018 | | |
1019 | | static int add_uris_recursive(STACK_OF(X509_NAME) *stack, |
1020 | | const char *uri, int depth) |
1021 | 0 | { |
1022 | 0 | int ok = 1; |
1023 | 0 | OSSL_STORE_CTX *ctx = NULL; |
1024 | 0 | X509 *x = NULL; |
1025 | 0 | const X509_NAME *cxn = NULL; |
1026 | 0 | X509_NAME *xn = NULL; |
1027 | 0 | OSSL_STORE_INFO *info = NULL; |
1028 | |
|
1029 | 0 | if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL) |
1030 | 0 | goto err; |
1031 | | |
1032 | 0 | while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) { |
1033 | 0 | int infotype; |
1034 | |
|
1035 | 0 | if ((info = OSSL_STORE_load(ctx)) == NULL) |
1036 | 0 | continue; |
1037 | 0 | infotype = OSSL_STORE_INFO_get_type(info); |
1038 | |
|
1039 | 0 | if (infotype == OSSL_STORE_INFO_NAME) { |
1040 | | /* |
1041 | | * This is an entry in the "directory" represented by the current |
1042 | | * uri. if |depth| allows, dive into it. |
1043 | | */ |
1044 | 0 | if (depth > 0) |
1045 | 0 | ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info), |
1046 | 0 | depth - 1); |
1047 | 0 | } else if (infotype == OSSL_STORE_INFO_CERT) { |
1048 | 0 | if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL |
1049 | 0 | || (cxn = X509_get_subject_name(x)) == NULL |
1050 | 0 | || (xn = X509_NAME_dup(cxn)) == NULL) |
1051 | 0 | goto err; |
1052 | 0 | if (sk_X509_NAME_find(stack, xn) >= 0) { |
1053 | | /* Duplicate. */ |
1054 | 0 | X509_NAME_free(xn); |
1055 | 0 | } else if (!sk_X509_NAME_push(stack, xn)) { |
1056 | 0 | X509_NAME_free(xn); |
1057 | 0 | goto err; |
1058 | 0 | } |
1059 | 0 | } |
1060 | | |
1061 | 0 | OSSL_STORE_INFO_free(info); |
1062 | 0 | info = NULL; |
1063 | 0 | } |
1064 | | |
1065 | 0 | ERR_clear_error(); |
1066 | 0 | goto done; |
1067 | | |
1068 | 0 | err: |
1069 | 0 | ok = 0; |
1070 | 0 | OSSL_STORE_INFO_free(info); |
1071 | 0 | done: |
1072 | 0 | OSSL_STORE_close(ctx); |
1073 | |
|
1074 | 0 | return ok; |
1075 | 0 | } |
1076 | | |
1077 | | int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack, |
1078 | | const char *store) |
1079 | 0 | { |
1080 | 0 | int (*oldcmp)(const X509_NAME *const *a, const X509_NAME *const *b) |
1081 | 0 | = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp); |
1082 | 0 | int ret = add_uris_recursive(stack, store, 1); |
1083 | |
|
1084 | 0 | (void)sk_X509_NAME_set_cmp_func(stack, oldcmp); |
1085 | 0 | return ret; |
1086 | 0 | } |
1087 | | |
1088 | | /* Build a certificate chain for current certificate */ |
1089 | | int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags) |
1090 | 0 | { |
1091 | 0 | CERT *c = s != NULL ? s->cert : ctx->cert; |
1092 | 0 | CERT_PKEY *cpk = c->key; |
1093 | 0 | X509_STORE *chain_store = NULL; |
1094 | 0 | X509_STORE_CTX *xs_ctx = NULL; |
1095 | 0 | STACK_OF(X509) *chain = NULL, *untrusted = NULL; |
1096 | 0 | X509 *x; |
1097 | 0 | SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s); |
1098 | 0 | int i, rv = 0; |
1099 | |
|
1100 | 0 | if (cpk->x509 == NULL) { |
1101 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET); |
1102 | 0 | goto err; |
1103 | 0 | } |
1104 | | /* Rearranging and check the chain: add everything to a store */ |
1105 | 0 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) { |
1106 | 0 | chain_store = X509_STORE_new(); |
1107 | 0 | if (chain_store == NULL) |
1108 | 0 | goto err; |
1109 | 0 | for (i = 0; i < sk_X509_num(cpk->chain); i++) { |
1110 | 0 | x = sk_X509_value(cpk->chain, i); |
1111 | 0 | if (!X509_STORE_add_cert(chain_store, x)) |
1112 | 0 | goto err; |
1113 | 0 | } |
1114 | | /* Add EE cert too: it might be self signed */ |
1115 | 0 | if (!X509_STORE_add_cert(chain_store, cpk->x509)) |
1116 | 0 | goto err; |
1117 | 0 | } else { |
1118 | 0 | if (c->chain_store != NULL) |
1119 | 0 | chain_store = c->chain_store; |
1120 | 0 | else |
1121 | 0 | chain_store = real_ctx->cert_store; |
1122 | |
|
1123 | 0 | if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) |
1124 | 0 | untrusted = cpk->chain; |
1125 | 0 | } |
1126 | | |
1127 | 0 | xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq); |
1128 | 0 | if (xs_ctx == NULL) { |
1129 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
1130 | 0 | goto err; |
1131 | 0 | } |
1132 | 0 | if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) { |
1133 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
1134 | 0 | goto err; |
1135 | 0 | } |
1136 | | /* Set suite B flags if needed */ |
1137 | 0 | X509_STORE_CTX_set_flags(xs_ctx, |
1138 | 0 | c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS); |
1139 | |
|
1140 | 0 | i = X509_verify_cert(xs_ctx); |
1141 | 0 | if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) { |
1142 | 0 | if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) |
1143 | 0 | ERR_clear_error(); |
1144 | 0 | i = 1; |
1145 | 0 | rv = 2; |
1146 | 0 | } |
1147 | 0 | if (i > 0) |
1148 | 0 | chain = X509_STORE_CTX_get1_chain(xs_ctx); |
1149 | 0 | if (i <= 0) { |
1150 | 0 | i = X509_STORE_CTX_get_error(xs_ctx); |
1151 | 0 | ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED, |
1152 | 0 | "Verify error:%s", X509_verify_cert_error_string(i)); |
1153 | |
|
1154 | 0 | goto err; |
1155 | 0 | } |
1156 | | /* Remove EE certificate from chain */ |
1157 | 0 | x = sk_X509_shift(chain); |
1158 | 0 | X509_free(x); |
1159 | 0 | if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) { |
1160 | 0 | if (sk_X509_num(chain) > 0) { |
1161 | | /* See if last cert is self signed */ |
1162 | 0 | x = sk_X509_value(chain, sk_X509_num(chain) - 1); |
1163 | 0 | if (X509_get_extension_flags(x) & EXFLAG_SS) { |
1164 | 0 | x = sk_X509_pop(chain); |
1165 | 0 | X509_free(x); |
1166 | 0 | } |
1167 | 0 | } |
1168 | 0 | } |
1169 | | /* |
1170 | | * Check security level of all CA certificates: EE will have been checked |
1171 | | * already. |
1172 | | */ |
1173 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
1174 | 0 | x = sk_X509_value(chain, i); |
1175 | 0 | rv = ssl_security_cert(s, ctx, x, 0); |
1176 | 0 | if (rv != 1) { |
1177 | 0 | ERR_raise(ERR_LIB_SSL, rv); |
1178 | 0 | OSSL_STACK_OF_X509_free(chain); |
1179 | 0 | rv = 0; |
1180 | 0 | goto err; |
1181 | 0 | } |
1182 | 0 | } |
1183 | 0 | OSSL_STACK_OF_X509_free(cpk->chain); |
1184 | 0 | cpk->chain = chain; |
1185 | 0 | if (rv == 0) |
1186 | 0 | rv = 1; |
1187 | 0 | err: |
1188 | 0 | if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) |
1189 | 0 | X509_STORE_free(chain_store); |
1190 | 0 | X509_STORE_CTX_free(xs_ctx); |
1191 | |
|
1192 | 0 | return rv; |
1193 | 0 | } |
1194 | | |
1195 | | int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) |
1196 | 0 | { |
1197 | 0 | X509_STORE **pstore; |
1198 | |
|
1199 | 0 | if (ref && store && !X509_STORE_up_ref(store)) |
1200 | 0 | return 0; |
1201 | | |
1202 | 0 | if (chain) |
1203 | 0 | pstore = &c->chain_store; |
1204 | 0 | else |
1205 | 0 | pstore = &c->verify_store; |
1206 | 0 | X509_STORE_free(*pstore); |
1207 | 0 | *pstore = store; |
1208 | |
|
1209 | 0 | return 1; |
1210 | 0 | } |
1211 | | |
1212 | | int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain) |
1213 | 0 | { |
1214 | 0 | *pstore = (chain ? c->chain_store : c->verify_store); |
1215 | 0 | return 1; |
1216 | 0 | } |
1217 | | |
1218 | | int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp) |
1219 | 0 | { |
1220 | 0 | int level; |
1221 | | /* |
1222 | | * note that there's a corresponding minbits_table |
1223 | | * in crypto/x509/x509_vfy.c that's used for checking the security level |
1224 | | * of RSA and DSA keys |
1225 | | */ |
1226 | 0 | static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 }; |
1227 | |
|
1228 | 0 | if (ctx != NULL) |
1229 | 0 | level = SSL_CTX_get_security_level(ctx); |
1230 | 0 | else |
1231 | 0 | level = SSL_get_security_level(s); |
1232 | |
|
1233 | 0 | if (level > 5) |
1234 | 0 | level = 5; |
1235 | 0 | else if (level < 0) |
1236 | 0 | level = 0; |
1237 | |
|
1238 | 0 | if (levelp != NULL) |
1239 | 0 | *levelp = level; |
1240 | |
|
1241 | 0 | return minbits_table[level]; |
1242 | 0 | } |
1243 | | |
1244 | | static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx, |
1245 | | int op, int bits, int nid, void *other, |
1246 | | void *ex) |
1247 | 0 | { |
1248 | 0 | int level, minbits, pfs_mask, minversion; |
1249 | 0 | const SSL_CONNECTION *sc; |
1250 | 0 | const int version1_3 = SSL_is_dtls(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
1251 | 0 | const int version1_2 = SSL_is_dtls(s) ? DTLS1_2_VERSION : TLS1_2_VERSION; |
1252 | |
|
1253 | 0 | minbits = ssl_get_security_level_bits(s, ctx, &level); |
1254 | |
|
1255 | 0 | if (level == 0) { |
1256 | | /* |
1257 | | * No EDH keys weaker than 1024-bits even at level 0, otherwise, |
1258 | | * anything goes. |
1259 | | */ |
1260 | 0 | if (op == SSL_SECOP_TMP_DH && bits < 80) |
1261 | 0 | return 0; |
1262 | 0 | return 1; |
1263 | 0 | } |
1264 | 0 | switch (op) { |
1265 | 0 | case SSL_SECOP_CIPHER_SUPPORTED: |
1266 | 0 | case SSL_SECOP_CIPHER_SHARED: |
1267 | 0 | case SSL_SECOP_CIPHER_CHECK: { |
1268 | 0 | const SSL_CIPHER *c = other; |
1269 | | /* No ciphers below security level */ |
1270 | 0 | if (bits < minbits) |
1271 | 0 | return 0; |
1272 | | /* No unauthenticated ciphersuites */ |
1273 | 0 | if (c->algorithm_auth & SSL_aNULL) |
1274 | 0 | return 0; |
1275 | | /* No MD5 mac ciphersuites */ |
1276 | 0 | if (c->algorithm_mac & SSL_MD5) |
1277 | 0 | return 0; |
1278 | | /* SHA1 HMAC is 160 bits of security */ |
1279 | 0 | if (minbits > 160 && c->algorithm_mac & SSL_SHA1) |
1280 | 0 | return 0; |
1281 | | |
1282 | | /* Level 3: forward secure ciphersuites only */ |
1283 | 0 | pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK; |
1284 | 0 | minversion = SSL_is_dtls(s) ? c->min_dtls : c->min_tls; |
1285 | |
|
1286 | 0 | if (level >= 3 && minversion != version1_3 && !(c->algorithm_mkey & pfs_mask)) |
1287 | 0 | return 0; |
1288 | 0 | break; |
1289 | 0 | } |
1290 | 0 | case SSL_SECOP_VERSION: |
1291 | 0 | if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL) |
1292 | 0 | return 0; |
1293 | | /* SSLv3, TLS v1.0 and TLS v1.1 and DTLS v1.0 only allowed at level 0 */ |
1294 | 0 | if (ssl_version_cmp(sc, nid, version1_2) < 0 && level > 0) |
1295 | 0 | return 0; |
1296 | 0 | break; |
1297 | | |
1298 | 0 | case SSL_SECOP_COMPRESSION: |
1299 | 0 | if (level >= 2) |
1300 | 0 | return 0; |
1301 | 0 | break; |
1302 | 0 | case SSL_SECOP_TICKET: |
1303 | 0 | if (level >= 3) |
1304 | 0 | return 0; |
1305 | 0 | break; |
1306 | 0 | default: |
1307 | 0 | if (bits < minbits) |
1308 | 0 | return 0; |
1309 | 0 | } |
1310 | 0 | return 1; |
1311 | 0 | } |
1312 | | |
1313 | | int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other) |
1314 | 0 | { |
1315 | 0 | return s->cert->sec_cb(SSL_CONNECTION_GET_USER_SSL(s), NULL, op, bits, nid, |
1316 | 0 | other, s->cert->sec_ex); |
1317 | 0 | } |
1318 | | |
1319 | | int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other) |
1320 | 0 | { |
1321 | 0 | return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other, |
1322 | 0 | ctx->cert->sec_ex); |
1323 | 0 | } |
1324 | | |
1325 | | int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx) |
1326 | 0 | { |
1327 | 0 | size_t i; |
1328 | |
|
1329 | 0 | for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) { |
1330 | 0 | if (ssl_cert_info[i].pkey_nid == nid) { |
1331 | 0 | *pidx = i; |
1332 | 0 | return 1; |
1333 | 0 | } |
1334 | 0 | } |
1335 | 0 | for (i = 0; i < ctx->sigalg_list_len; i++) { |
1336 | 0 | if (ctx->ssl_cert_info[i].pkey_nid == nid) { |
1337 | 0 | *pidx = SSL_PKEY_NUM + i; |
1338 | 0 | return 1; |
1339 | 0 | } |
1340 | 0 | } |
1341 | 0 | return 0; |
1342 | 0 | } |
1343 | | |
1344 | | const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx) |
1345 | 0 | { |
1346 | 0 | size_t i; |
1347 | | |
1348 | | /* check classic pk types */ |
1349 | 0 | for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) { |
1350 | 0 | const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i]; |
1351 | |
|
1352 | 0 | if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->pkey_nid)) |
1353 | 0 | || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->pkey_nid))) { |
1354 | 0 | if (pidx != NULL) |
1355 | 0 | *pidx = i; |
1356 | 0 | return tmp_lu; |
1357 | 0 | } |
1358 | 0 | } |
1359 | | /* check provider-loaded pk types */ |
1360 | 0 | for (i = 0; i < ctx->sigalg_list_len; i++) { |
1361 | 0 | SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]); |
1362 | |
|
1363 | 0 | if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->pkey_nid)) |
1364 | 0 | || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->pkey_nid))) { |
1365 | 0 | if (pidx != NULL) |
1366 | 0 | *pidx = SSL_PKEY_NUM + i; |
1367 | 0 | return &ctx->ssl_cert_info[i]; |
1368 | 0 | } |
1369 | 0 | } |
1370 | | |
1371 | 0 | return NULL; |
1372 | 0 | } |
1373 | | |
1374 | | const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx) |
1375 | 0 | { |
1376 | 0 | if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len)) |
1377 | 0 | return NULL; |
1378 | 0 | else if (idx >= (OSSL_NELEM(ssl_cert_info))) |
1379 | 0 | return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]); |
1380 | 0 | return &ssl_cert_info[idx]; |
1381 | 0 | } |