Coverage Report

Created: 2024-07-27 06:36

/src/openssl/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
0
{
47
0
    ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
48
0
                                                             "SSL for verify callback",
49
0
                                                             NULL, NULL, NULL);
50
0
    return ssl_x509_store_ctx_idx >= 0;
51
0
}
52
53
int SSL_get_ex_data_X509_STORE_CTX_idx(void)
54
0
{
55
56
0
    if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
57
0
        return -1;
58
0
    return ssl_x509_store_ctx_idx;
59
0
}
60
61
CERT *ssl_cert_new(size_t ssl_pkey_num)
62
0
{
63
0
    CERT *ret = NULL;
64
65
    /* Should never happen */
66
0
    if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM))
67
0
        return NULL;
68
69
0
    ret = OPENSSL_zalloc(sizeof(*ret));
70
0
    if (ret == NULL)
71
0
        return NULL;
72
73
0
    ret->ssl_pkey_num = ssl_pkey_num;
74
0
    ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
75
0
    if (ret->pkeys == NULL) {
76
0
        OPENSSL_free(ret);
77
0
        return NULL;
78
0
    }
79
80
0
    ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
81
0
    ret->sec_cb = ssl_security_default_callback;
82
0
    ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
83
0
    ret->sec_ex = NULL;
84
0
    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
0
    return ret;
91
0
}
92
93
CERT *ssl_cert_dup(CERT *cert)
94
0
{
95
0
    CERT *ret = OPENSSL_zalloc(sizeof(*ret));
96
0
    size_t i;
97
#ifndef OPENSSL_NO_COMP_ALG
98
    int j;
99
#endif
100
101
0
    if (ret == NULL)
102
0
        return NULL;
103
104
0
    ret->ssl_pkey_num = cert->ssl_pkey_num;
105
0
    ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
106
0
    if (ret->pkeys == NULL) {
107
0
        OPENSSL_free(ret);
108
0
        return NULL;
109
0
    }
110
111
0
    ret->key = &ret->pkeys[cert->key - cert->pkeys];
112
0
    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
0
    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
0
    ret->dh_tmp_cb = cert->dh_tmp_cb;
124
0
    ret->dh_tmp_auto = cert->dh_tmp_auto;
125
126
0
    for (i = 0; i < ret->ssl_pkey_num; i++) {
127
0
        CERT_PKEY *cpk = cert->pkeys + i;
128
0
        CERT_PKEY *rpk = ret->pkeys + i;
129
130
0
        if (cpk->x509 != NULL) {
131
0
            rpk->x509 = cpk->x509;
132
0
            X509_up_ref(rpk->x509);
133
0
        }
134
135
0
        if (cpk->privatekey != NULL) {
136
0
            rpk->privatekey = cpk->privatekey;
137
0
            EVP_PKEY_up_ref(cpk->privatekey);
138
0
        }
139
140
0
        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
0
        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
0
    }
164
165
    /* Configured sigalgs copied across */
166
0
    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
0
        ret->conf_sigalgs = NULL;
176
177
0
    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
0
        ret->client_sigalgs = NULL;
187
    /* Copy any custom client certificate types */
188
0
    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
0
    ret->cert_flags = cert->cert_flags;
196
197
0
    ret->cert_cb = cert->cert_cb;
198
0
    ret->cert_cb_arg = cert->cert_cb_arg;
199
200
0
    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
0
    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
0
    ret->sec_cb = cert->sec_cb;
211
0
    ret->sec_level = cert->sec_level;
212
0
    ret->sec_ex = cert->sec_ex;
213
214
0
    if (!custom_exts_copy(&ret->custext, &cert->custext))
215
0
        goto err;
216
0
#ifndef OPENSSL_NO_PSK
217
0
    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
0
#endif
223
0
    return ret;
224
225
0
 err:
226
0
    ssl_cert_free(ret);
227
228
0
    return NULL;
229
0
}
230
231
/* Free up and clear all certificates and chains */
232
233
void ssl_cert_clear_certs(CERT *c)
234
0
{
235
0
    size_t i;
236
#ifndef OPENSSL_NO_COMP_ALG
237
    int j;
238
#endif
239
240
0
    if (c == NULL)
241
0
        return;
242
0
    for (i = 0; i < c->ssl_pkey_num; i++) {
243
0
        CERT_PKEY *cpk = c->pkeys + i;
244
0
        X509_free(cpk->x509);
245
0
        cpk->x509 = NULL;
246
0
        EVP_PKEY_free(cpk->privatekey);
247
0
        cpk->privatekey = NULL;
248
0
        OSSL_STACK_OF_X509_free(cpk->chain);
249
0
        cpk->chain = NULL;
250
0
        OPENSSL_free(cpk->serverinfo);
251
0
        cpk->serverinfo = NULL;
252
0
        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
0
    }
261
0
}
262
263
void ssl_cert_free(CERT *c)
264
0
{
265
0
    int i;
266
267
0
    if (c == NULL)
268
0
        return;
269
0
    CRYPTO_DOWN_REF(&c->references, &i);
270
0
    REF_PRINT_COUNT("CERT", c);
271
0
    if (i > 0)
272
0
        return;
273
0
    REF_ASSERT_ISNT(i < 0);
274
275
0
    EVP_PKEY_free(c->dh_tmp);
276
277
0
    ssl_cert_clear_certs(c);
278
0
    OPENSSL_free(c->conf_sigalgs);
279
0
    OPENSSL_free(c->client_sigalgs);
280
0
    OPENSSL_free(c->ctype);
281
0
    X509_STORE_free(c->verify_store);
282
0
    X509_STORE_free(c->chain_store);
283
0
    custom_exts_free(&c->custext);
284
0
#ifndef OPENSSL_NO_PSK
285
0
    OPENSSL_free(c->psk_identity_hint);
286
0
#endif
287
0
    OPENSSL_free(c->pkeys);
288
0
    CRYPTO_FREE_REF(&c->references);
289
0
    OPENSSL_free(c);
290
0
}
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
0
{
419
0
    X509 *x;
420
0
    int i = 0;
421
0
    X509_STORE *verify_store;
422
0
    X509_STORE_CTX *ctx = NULL;
423
0
    X509_VERIFY_PARAM *param;
424
0
    SSL_CTX *sctx;
425
426
    /* Something must be passed in */
427
0
    if ((sk == NULL || sk_X509_num(sk) == 0) && rpk == NULL)
428
0
        return 0;
429
430
    /* Only one can be set */
431
0
    if (sk != NULL && rpk != NULL)
432
0
        return 0;
433
434
0
    sctx = SSL_CONNECTION_GET_CTX(s);
435
0
    if (s->cert->verify_store)
436
0
        verify_store = s->cert->verify_store;
437
0
    else
438
0
        verify_store = sctx->cert_store;
439
440
0
    ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq);
441
0
    if (ctx == NULL) {
442
0
        ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
443
0
        return 0;
444
0
    }
445
446
0
    if (sk != NULL) {
447
0
        x = sk_X509_value(sk, 0);
448
0
        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
0
    } 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
0
    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
0
    X509_VERIFY_PARAM_set_auth_level(param,
465
0
        SSL_get_security_level(SSL_CONNECTION_GET_SSL(s)));
466
467
    /* Set suite B flags if needed */
468
0
    X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
469
0
    if (!X509_STORE_CTX_set_ex_data(ctx,
470
0
            SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
471
0
        goto end;
472
0
    }
473
474
    /* Verify via DANE if enabled */
475
0
    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
0
    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
0
    X509_VERIFY_PARAM_set1(param, s->param);
489
490
0
    if (s->verify_callback)
491
0
        X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
492
493
0
    if (sctx->app_verify_callback != NULL) {
494
0
        i = sctx->app_verify_callback(ctx, sctx->app_verify_arg);
495
0
    } else {
496
0
        i = X509_verify_cert(ctx);
497
        /* We treat an error in the same way as a failure to verify */
498
0
        if (i < 0)
499
0
            i = 0;
500
0
    }
501
502
0
    s->verify_result = X509_STORE_CTX_get_error(ctx);
503
0
    OSSL_STACK_OF_X509_free(s->verified_chain);
504
0
    s->verified_chain = NULL;
505
506
0
    if (sk != NULL && X509_STORE_CTX_get0_chain(ctx) != NULL) {
507
0
        s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
508
0
        if (s->verified_chain == NULL) {
509
0
            ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
510
0
            i = 0;
511
0
        }
512
0
    }
513
514
    /* Move peername from the store context params to the SSL handle's */
515
0
    X509_VERIFY_PARAM_move_peername(s->param, param);
516
517
0
 end:
518
0
    X509_STORE_CTX_free(ctx);
519
0
    return i;
520
0
}
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
0
{
543
0
    return ssl_verify_internal(s, sk, NULL);
544
0
}
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
0
{
599
0
    const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
600
601
0
    if (sc == NULL)
602
0
        return NULL;
603
604
0
    return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names;
605
0
}
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
930
0
    if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
931
0
        goto err;
932
933
0
    while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
934
0
        OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
935
0
        int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
936
937
0
        if (info == NULL)
938
0
            continue;
939
940
0
        if (infotype == OSSL_STORE_INFO_NAME) {
941
            /*
942
             * This is an entry in the "directory" represented by the current
943
             * uri.  if |depth| allows, dive into it.
944
             */
945
0
            if (depth > 0)
946
0
                ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
947
0
                                        depth - 1);
948
0
        } else if (infotype == OSSL_STORE_INFO_CERT) {
949
0
            if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
950
0
                || (xn = X509_get_subject_name(x)) == NULL
951
0
                || (xn = X509_NAME_dup(xn)) == NULL)
952
0
                goto err;
953
0
            if (sk_X509_NAME_find(stack, xn) >= 0) {
954
                /* Duplicate. */
955
0
                X509_NAME_free(xn);
956
0
            } else if (!sk_X509_NAME_push(stack, xn)) {
957
0
                X509_NAME_free(xn);
958
0
                goto err;
959
0
            }
960
0
        }
961
962
0
        OSSL_STORE_INFO_free(info);
963
0
    }
964
965
0
    ERR_clear_error();
966
0
    goto done;
967
968
0
 err:
969
0
    ok = 0;
970
0
 done:
971
0
    OSSL_STORE_close(ctx);
972
973
0
    return ok;
974
0
}
975
976
int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
977
                                         const char *store)
978
0
{
979
0
    int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
980
0
        = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
981
0
    int ret = add_uris_recursive(stack, store, 1);
982
983
0
    (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
984
0
    return ret;
985
0
}
986
987
/* Build a certificate chain for current certificate */
988
int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags)
989
0
{
990
0
    CERT *c = s != NULL ? s->cert : ctx->cert;
991
0
    CERT_PKEY *cpk = c->key;
992
0
    X509_STORE *chain_store = NULL;
993
0
    X509_STORE_CTX *xs_ctx = NULL;
994
0
    STACK_OF(X509) *chain = NULL, *untrusted = NULL;
995
0
    X509 *x;
996
0
    SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s);
997
0
    int i, rv = 0;
998
999
0
    if (cpk->x509 == NULL) {
1000
0
        ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
1001
0
        goto err;
1002
0
    }
1003
    /* Rearranging and check the chain: add everything to a store */
1004
0
    if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
1005
0
        chain_store = X509_STORE_new();
1006
0
        if (chain_store == NULL)
1007
0
            goto err;
1008
0
        for (i = 0; i < sk_X509_num(cpk->chain); i++) {
1009
0
            x = sk_X509_value(cpk->chain, i);
1010
0
            if (!X509_STORE_add_cert(chain_store, x))
1011
0
                goto err;
1012
0
        }
1013
        /* Add EE cert too: it might be self signed */
1014
0
        if (!X509_STORE_add_cert(chain_store, cpk->x509))
1015
0
            goto err;
1016
0
    } else {
1017
0
        if (c->chain_store != NULL)
1018
0
            chain_store = c->chain_store;
1019
0
        else
1020
0
            chain_store = real_ctx->cert_store;
1021
1022
0
        if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
1023
0
            untrusted = cpk->chain;
1024
0
    }
1025
1026
0
    xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
1027
0
    if (xs_ctx == NULL) {
1028
0
        ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
1029
0
        goto err;
1030
0
    }
1031
0
    if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
1032
0
        ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
1033
0
        goto err;
1034
0
    }
1035
    /* Set suite B flags if needed */
1036
0
    X509_STORE_CTX_set_flags(xs_ctx,
1037
0
                             c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
1038
1039
0
    i = X509_verify_cert(xs_ctx);
1040
0
    if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
1041
0
        if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
1042
0
            ERR_clear_error();
1043
0
        i = 1;
1044
0
        rv = 2;
1045
0
    }
1046
0
    if (i > 0)
1047
0
        chain = X509_STORE_CTX_get1_chain(xs_ctx);
1048
0
    if (i <= 0) {
1049
0
        i = X509_STORE_CTX_get_error(xs_ctx);
1050
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
1051
0
                       "Verify error:%s", X509_verify_cert_error_string(i));
1052
1053
0
        goto err;
1054
0
    }
1055
    /* Remove EE certificate from chain */
1056
0
    x = sk_X509_shift(chain);
1057
0
    X509_free(x);
1058
0
    if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
1059
0
        if (sk_X509_num(chain) > 0) {
1060
            /* See if last cert is self signed */
1061
0
            x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1062
0
            if (X509_get_extension_flags(x) & EXFLAG_SS) {
1063
0
                x = sk_X509_pop(chain);
1064
0
                X509_free(x);
1065
0
            }
1066
0
        }
1067
0
    }
1068
    /*
1069
     * Check security level of all CA certificates: EE will have been checked
1070
     * already.
1071
     */
1072
0
    for (i = 0; i < sk_X509_num(chain); i++) {
1073
0
        x = sk_X509_value(chain, i);
1074
0
        rv = ssl_security_cert(s, ctx, x, 0, 0);
1075
0
        if (rv != 1) {
1076
0
            ERR_raise(ERR_LIB_SSL, rv);
1077
0
            OSSL_STACK_OF_X509_free(chain);
1078
0
            rv = 0;
1079
0
            goto err;
1080
0
        }
1081
0
    }
1082
0
    OSSL_STACK_OF_X509_free(cpk->chain);
1083
0
    cpk->chain = chain;
1084
0
    if (rv == 0)
1085
0
        rv = 1;
1086
0
 err:
1087
0
    if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1088
0
        X509_STORE_free(chain_store);
1089
0
    X509_STORE_CTX_free(xs_ctx);
1090
1091
0
    return rv;
1092
0
}
1093
1094
int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1095
0
{
1096
0
    X509_STORE **pstore;
1097
0
    if (chain)
1098
0
        pstore = &c->chain_store;
1099
0
    else
1100
0
        pstore = &c->verify_store;
1101
0
    X509_STORE_free(*pstore);
1102
0
    *pstore = store;
1103
0
    if (ref && store)
1104
0
        X509_STORE_up_ref(store);
1105
0
    return 1;
1106
0
}
1107
1108
int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)
1109
0
{
1110
0
    *pstore = (chain ? c->chain_store : c->verify_store);
1111
0
    return 1;
1112
0
}
1113
1114
int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
1115
0
{
1116
0
    int level;
1117
    /*
1118
     * note that there's a corresponding minbits_table
1119
     * in crypto/x509/x509_vfy.c that's used for checking the security level
1120
     * of RSA and DSA keys
1121
     */
1122
0
    static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
1123
1124
0
    if (ctx != NULL)
1125
0
        level = SSL_CTX_get_security_level(ctx);
1126
0
    else
1127
0
        level = SSL_get_security_level(s);
1128
1129
0
    if (level > 5)
1130
0
        level = 5;
1131
0
    else if (level < 0)
1132
0
        level = 0;
1133
1134
0
    if (levelp != NULL)
1135
0
        *levelp = level;
1136
1137
0
    return minbits_table[level];
1138
0
}
1139
1140
static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
1141
                                         int op, int bits, int nid, void *other,
1142
                                         void *ex)
1143
0
{
1144
0
    int level, minbits, pfs_mask;
1145
0
    const SSL_CONNECTION *sc;
1146
1147
0
    minbits = ssl_get_security_level_bits(s, ctx, &level);
1148
1149
0
    if (level == 0) {
1150
        /*
1151
         * No EDH keys weaker than 1024-bits even at level 0, otherwise,
1152
         * anything goes.
1153
         */
1154
0
        if (op == SSL_SECOP_TMP_DH && bits < 80)
1155
0
            return 0;
1156
0
        return 1;
1157
0
    }
1158
0
    switch (op) {
1159
0
    case SSL_SECOP_CIPHER_SUPPORTED:
1160
0
    case SSL_SECOP_CIPHER_SHARED:
1161
0
    case SSL_SECOP_CIPHER_CHECK:
1162
0
        {
1163
0
            const SSL_CIPHER *c = other;
1164
            /* No ciphers below security level */
1165
0
            if (bits < minbits)
1166
0
                return 0;
1167
            /* No unauthenticated ciphersuites */
1168
0
            if (c->algorithm_auth & SSL_aNULL)
1169
0
                return 0;
1170
            /* No MD5 mac ciphersuites */
1171
0
            if (c->algorithm_mac & SSL_MD5)
1172
0
                return 0;
1173
            /* SHA1 HMAC is 160 bits of security */
1174
0
            if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1175
0
                return 0;
1176
            /* Level 3: forward secure ciphersuites only */
1177
0
            pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;
1178
0
            if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
1179
0
                               !(c->algorithm_mkey & pfs_mask))
1180
0
                return 0;
1181
0
            break;
1182
0
        }
1183
0
    case SSL_SECOP_VERSION:
1184
0
        if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL)
1185
0
            return 0;
1186
0
        if (!SSL_CONNECTION_IS_DTLS(sc)) {
1187
            /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
1188
0
            if (nid <= TLS1_1_VERSION && level > 0)
1189
0
                return 0;
1190
0
        } else {
1191
            /* DTLS v1.0 only allowed at level 0 */
1192
0
            if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level > 0)
1193
0
                return 0;
1194
0
        }
1195
0
        break;
1196
1197
0
    case SSL_SECOP_COMPRESSION:
1198
0
        if (level >= 2)
1199
0
            return 0;
1200
0
        break;
1201
0
    case SSL_SECOP_TICKET:
1202
0
        if (level >= 3)
1203
0
            return 0;
1204
0
        break;
1205
0
    default:
1206
0
        if (bits < minbits)
1207
0
            return 0;
1208
0
    }
1209
0
    return 1;
1210
0
}
1211
1212
int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other)
1213
0
{
1214
0
    return s->cert->sec_cb(SSL_CONNECTION_GET_SSL(s), NULL, op, bits, nid,
1215
0
                           other, s->cert->sec_ex);
1216
0
}
1217
1218
int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
1219
0
{
1220
0
    return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1221
0
                             ctx->cert->sec_ex);
1222
0
}
1223
1224
int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx)
1225
0
{
1226
0
    size_t i;
1227
1228
0
    for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1229
0
        if (ssl_cert_info[i].nid == nid) {
1230
0
            *pidx = i;
1231
0
            return 1;
1232
0
        }
1233
0
    }
1234
0
    for (i = 0; i < ctx->sigalg_list_len; i++) {
1235
0
        if (ctx->ssl_cert_info[i].nid == nid) {
1236
0
            *pidx = SSL_PKEY_NUM + i;
1237
0
            return 1;
1238
0
        }
1239
0
    }
1240
0
    return 0;
1241
0
}
1242
1243
const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx)
1244
0
{
1245
0
    size_t i;
1246
1247
    /* check classic pk types */
1248
0
    for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
1249
0
        const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
1250
1251
0
        if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
1252
0
            || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
1253
0
            if (pidx != NULL)
1254
0
                *pidx = i;
1255
0
            return tmp_lu;
1256
0
        }
1257
0
    }
1258
    /* check provider-loaded pk types */
1259
0
    for (i = 0; ctx->sigalg_list_len; i++) {
1260
0
        SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]);
1261
1262
0
        if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
1263
0
            || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
1264
0
            if (pidx != NULL)
1265
0
                *pidx = SSL_PKEY_NUM + i;
1266
0
            return &ctx->ssl_cert_info[i];
1267
0
        }
1268
0
    }
1269
1270
0
    return NULL;
1271
0
}
1272
1273
const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx)
1274
0
{
1275
0
    if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len))
1276
0
        return NULL;
1277
0
    else if (idx >= (OSSL_NELEM(ssl_cert_info)))
1278
0
        return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]);
1279
0
    return &ssl_cert_info[idx];
1280
0
}