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