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