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