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