Coverage Report

Created: 2023-04-12 06:22

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