Coverage Report

Created: 2025-12-31 06:58

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