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