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