Coverage Report

Created: 2026-08-18 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/ssl_conf.c
Line
Count
Source
1
/*
2
 * Copyright 2012-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "internal/e_os.h"
11
12
#include <limits.h>
13
#include <stdio.h>
14
#include "ssl_local.h"
15
#include <openssl/conf.h>
16
#include <openssl/objects.h>
17
#include <openssl/decoder.h>
18
#include <openssl/core_dispatch.h>
19
#include "internal/nelem.h"
20
#include "internal/ssl_unwrap.h"
21
22
/*
23
 * structure holding name tables. This is used for permitted elements in lists
24
 * such as TLSv1.
25
 */
26
27
typedef struct {
28
    const char *name;
29
    int namelen;
30
    unsigned int name_flags;
31
    uint64_t option_value;
32
} ssl_flag_tbl;
33
34
/* Switch table: use for single command line switches like no_tls2 */
35
typedef struct {
36
    uint64_t option_value;
37
    unsigned int name_flags;
38
} ssl_switch_tbl;
39
40
/* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
41
0
#define SSL_TFLAG_INV 0x1
42
/* Mask for type of flag referred to */
43
0
#define SSL_TFLAG_TYPE_MASK 0xf00
44
/* Flag is for options */
45
0
#define SSL_TFLAG_OPTION 0x000
46
/* Flag is for cert_flags */
47
0
#define SSL_TFLAG_CERT 0x100
48
/* Flag is for verify mode */
49
0
#define SSL_TFLAG_VFY 0x200
50
/* Option can only be used for clients */
51
0
#define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
52
/* Option can only be used for servers */
53
0
#define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
54
0
#define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT | SSL_TFLAG_SERVER)
55
56
#define SSL_FLAG_TBL(str, flag) \
57
0
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag }
58
#define SSL_FLAG_TBL_SRV(str, flag) \
59
0
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag }
60
#define SSL_FLAG_TBL_CLI(str, flag) \
61
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag }
62
#define SSL_FLAG_TBL_INV(str, flag) \
63
0
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_INV | SSL_TFLAG_BOTH, flag }
64
#define SSL_FLAG_TBL_SRV_INV(str, flag) \
65
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_INV | SSL_TFLAG_SERVER, flag }
66
#define SSL_FLAG_TBL_CERT(str, flag) \
67
0
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT | SSL_TFLAG_BOTH, flag }
68
69
#define SSL_FLAG_VFY_CLI(str, flag) \
70
0
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag }
71
#define SSL_FLAG_VFY_SRV(str, flag) \
72
0
    { str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag }
73
74
/*
75
 * Opaque structure containing SSL configuration context.
76
 */
77
78
struct ssl_conf_ctx_st {
79
    /*
80
     * Various flags indicating (among other things) which options we will
81
     * recognise.
82
     */
83
    unsigned int flags;
84
    /* Prefix and length of commands */
85
    char *prefix;
86
    size_t prefixlen;
87
    /* SSL_CTX or SSL structure to perform operations on */
88
    SSL_CTX *ctx;
89
    SSL *ssl;
90
    /* Pointer to SSL or SSL_CTX options field or NULL if none */
91
    uint64_t *poptions;
92
    /* Certificate filenames for each type */
93
    char **cert_filename;
94
    /* Number of elements in the cert_filename array */
95
    size_t num_cert_filename;
96
    /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
97
    uint32_t *pcert_flags;
98
    /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
99
    uint32_t *pvfy_flags;
100
    /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
101
    int *min_version;
102
    /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
103
    int *max_version;
104
    /* Current flag table being worked on */
105
    const ssl_flag_tbl *tbl;
106
    /* Size of table */
107
    size_t ntbl;
108
    /* Client CA names */
109
    STACK_OF(X509_NAME) *canames;
110
};
111
112
static void ssl_set_option(SSL_CONF_CTX *cctx, unsigned int name_flags,
113
    uint64_t option_value, int onoff)
114
0
{
115
0
    uint32_t *pflags;
116
117
0
    if (cctx->poptions == NULL)
118
0
        return;
119
0
    if (name_flags & SSL_TFLAG_INV)
120
0
        onoff ^= 1;
121
0
    switch (name_flags & SSL_TFLAG_TYPE_MASK) {
122
123
0
    case SSL_TFLAG_CERT:
124
0
        pflags = cctx->pcert_flags;
125
0
        break;
126
127
0
    case SSL_TFLAG_VFY:
128
0
        pflags = cctx->pvfy_flags;
129
0
        break;
130
131
0
    case SSL_TFLAG_OPTION:
132
0
        if (onoff)
133
0
            *cctx->poptions |= option_value;
134
0
        else
135
0
            *cctx->poptions &= ~option_value;
136
0
        return;
137
138
0
    default:
139
0
        return;
140
0
    }
141
0
    if (onoff)
142
0
        *pflags |= option_value;
143
0
    else
144
0
        *pflags &= ~option_value;
145
0
}
146
147
static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
148
    const char *name, int namelen, int onoff)
149
0
{
150
    /* If name not relevant for context skip */
151
0
    if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
152
0
        return 0;
153
0
    if (namelen == -1) {
154
0
        if (strcmp(tbl->name, name))
155
0
            return 0;
156
0
    } else if (tbl->namelen != namelen
157
0
        || OPENSSL_strncasecmp(tbl->name, name, namelen))
158
0
        return 0;
159
0
    ssl_set_option(cctx, tbl->name_flags, tbl->option_value, onoff);
160
0
    return 1;
161
0
}
162
163
static int ssl_set_option_list(const char *elem, int len, void *usr)
164
0
{
165
0
    SSL_CONF_CTX *cctx = usr;
166
0
    size_t i;
167
0
    const ssl_flag_tbl *tbl;
168
0
    int onoff = 1;
169
    /*
170
     * len == -1 indicates not being called in list context, just for single
171
     * command line switches, so don't allow +, -.
172
     */
173
0
    if (elem == NULL)
174
0
        return 0;
175
0
    if (len != -1) {
176
0
        if (*elem == '+') {
177
0
            elem++;
178
0
            len--;
179
0
            onoff = 1;
180
0
        } else if (*elem == '-') {
181
0
            elem++;
182
0
            len--;
183
0
            onoff = 0;
184
0
        }
185
0
    }
186
0
    for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++) {
187
0
        if (ssl_match_option(cctx, tbl, elem, len, onoff))
188
0
            return 1;
189
0
    }
190
0
    return 0;
191
0
}
192
193
/* Set supported signature algorithms */
194
static int cmd_SignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
195
0
{
196
0
    int rv;
197
0
    if (cctx->ssl)
198
0
        rv = SSL_set1_sigalgs_list(cctx->ssl, value);
199
    /* NB: ctx == NULL performs syntax checking only */
200
0
    else
201
0
        rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
202
0
    return rv > 0;
203
0
}
204
205
/* Set supported client signature algorithms */
206
static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
207
0
{
208
0
    int rv;
209
0
    if (cctx->ssl)
210
0
        rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
211
    /* NB: ctx == NULL performs syntax checking only */
212
0
    else
213
0
        rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
214
0
    return rv > 0;
215
0
}
216
217
static int cmd_Groups(SSL_CONF_CTX *cctx, const char *value)
218
0
{
219
0
    int rv;
220
0
    if (cctx->ssl)
221
0
        rv = SSL_set1_groups_list(cctx->ssl, value);
222
    /* NB: ctx == NULL performs syntax checking only */
223
0
    else
224
0
        rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
225
0
    return rv > 0;
226
0
}
227
228
/* This is the old name for cmd_Groups - retained for backwards compatibility */
229
static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value)
230
0
{
231
0
    return cmd_Groups(cctx, value);
232
0
}
233
234
/* ECDH temporary parameters */
235
static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value)
236
0
{
237
0
    int rv = 1;
238
239
    /* Ignore values supported by 1.0.2 for the automatic selection */
240
0
    if ((cctx->flags & SSL_CONF_FLAG_FILE)
241
0
        && (OPENSSL_strcasecmp(value, "+automatic") == 0
242
0
            || OPENSSL_strcasecmp(value, "automatic") == 0))
243
0
        return 1;
244
0
    if ((cctx->flags & SSL_CONF_FLAG_CMDLINE) && strcmp(value, "auto") == 0)
245
0
        return 1;
246
247
    /* ECDHParameters accepts a single group name */
248
0
    if (strchr(value, ':') != NULL)
249
0
        return 0;
250
251
0
    if (cctx->ctx)
252
0
        rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
253
0
    else if (cctx->ssl)
254
0
        rv = SSL_set1_groups_list(cctx->ssl, value);
255
256
0
    return rv > 0;
257
0
}
258
259
static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
260
0
{
261
0
    int rv = 1;
262
263
0
    if (cctx->ctx)
264
0
        rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
265
0
    if (cctx->ssl)
266
0
        rv = SSL_set_cipher_list(cctx->ssl, value);
267
0
    return rv > 0;
268
0
}
269
270
static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
271
0
{
272
0
    int rv = 1;
273
274
0
    if (cctx->ctx)
275
0
        rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
276
0
    if (cctx->ssl)
277
0
        rv = SSL_set_ciphersuites(cctx->ssl, value);
278
0
    return rv > 0;
279
0
}
280
281
static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
282
0
{
283
0
    static const ssl_flag_tbl ssl_protocol_list[] = {
284
0
        SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
285
0
        SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
286
0
        SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
287
0
        SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
288
0
        SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
289
0
        SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
290
0
        SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
291
0
        SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
292
0
        SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
293
0
    };
294
0
    cctx->tbl = ssl_protocol_list;
295
0
    cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
296
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
297
0
}
298
299
/*
300
 * protocol_from_string - converts a protocol version string to a number
301
 *
302
 * Returns -1 on failure or the version on success
303
 */
304
static int protocol_from_string(const char *value)
305
0
{
306
0
    struct protocol_versions {
307
0
        const char *name;
308
0
        int version;
309
0
    };
310
    /*
311
     * Note: To avoid breaking previously valid configurations, we must retain
312
     * legacy entries in this table even if the underlying protocol is no
313
     * longer supported.  This also means that the constants SSL3_VERSION, ...
314
     * need to be retained indefinitely.  This table can only grow, never
315
     * shrink.
316
     */
317
0
    static const struct protocol_versions versions[] = {
318
0
        { "None", 0 },
319
0
        { "SSLv3", SSL3_VERSION },
320
0
        { "TLSv1", TLS1_VERSION },
321
0
        { "TLSv1.1", TLS1_1_VERSION },
322
0
        { "TLSv1.2", TLS1_2_VERSION },
323
0
        { "TLSv1.3", TLS1_3_VERSION },
324
0
        { "DTLSv1", DTLS1_VERSION },
325
0
        { "DTLSv1.2", DTLS1_2_VERSION }
326
0
    };
327
0
    size_t i;
328
0
    size_t n = OSSL_NELEM(versions);
329
330
0
    for (i = 0; i < n; i++)
331
0
        if (strcmp(versions[i].name, value) == 0)
332
0
            return versions[i].version;
333
0
    return -1;
334
0
}
335
336
static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
337
0
{
338
0
    int method_version;
339
0
    int new_version;
340
341
0
    if (cctx->ctx != NULL)
342
0
        method_version = cctx->ctx->method->version;
343
0
    else if (cctx->ssl != NULL)
344
0
        method_version = cctx->ssl->defltmeth->version;
345
0
    else
346
0
        return 0;
347
0
    if ((new_version = protocol_from_string(value)) < 0)
348
0
        return 0;
349
0
    return ssl_set_version_bound(method_version, new_version, bound);
350
0
}
351
352
/*
353
 * cmd_MinProtocol - Set min protocol version
354
 * @cctx: config structure to save settings in
355
 * @value: The min protocol version in string form
356
 *
357
 * Returns 1 on success and 0 on failure.
358
 */
359
static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
360
0
{
361
0
    return min_max_proto(cctx, value, cctx->min_version);
362
0
}
363
364
/*
365
 * cmd_MaxProtocol - Set max protocol version
366
 * @cctx: config structure to save settings in
367
 * @value: The max protocol version in string form
368
 *
369
 * Returns 1 on success and 0 on failure.
370
 */
371
static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
372
0
{
373
0
    return min_max_proto(cctx, value, cctx->max_version);
374
0
}
375
376
static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
377
0
{
378
0
    static const ssl_flag_tbl ssl_option_list[] = {
379
0
        SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
380
0
        SSL_FLAG_TBL_INV("EmptyFragments",
381
0
            SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
382
0
        SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
383
0
        SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
384
0
        SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_SERVER_PREFERENCE),
385
0
        SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
386
0
            SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
387
0
        SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
388
0
        SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
389
0
        SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
390
0
            SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
391
0
        SSL_FLAG_TBL("UnsafeLegacyServerConnect",
392
0
            SSL_OP_LEGACY_SERVER_CONNECT),
393
0
        SSL_FLAG_TBL("ClientRenegotiation",
394
0
            SSL_OP_ALLOW_CLIENT_RENEGOTIATION),
395
0
        SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
396
0
        SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
397
0
        SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
398
0
        SSL_FLAG_TBL("PreferNoDHEKEX", SSL_OP_PREFER_NO_DHE_KEX),
399
0
        SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
400
0
        SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
401
0
        SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
402
0
        SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
403
0
        SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES),
404
0
        SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS),
405
0
        SSL_FLAG_TBL_CERT("StrictCertCheck", SSL_CERT_FLAG_TLS_STRICT),
406
0
        SSL_FLAG_TBL_INV("TxCertificateCompression", SSL_OP_NO_TX_CERTIFICATE_COMPRESSION),
407
0
        SSL_FLAG_TBL_INV("RxCertificateCompression", SSL_OP_NO_RX_CERTIFICATE_COMPRESSION),
408
0
        SSL_FLAG_TBL("KTLSTxZerocopySendfile", SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE),
409
0
        SSL_FLAG_TBL("IgnoreUnexpectedEOF", SSL_OP_IGNORE_UNEXPECTED_EOF),
410
0
        SSL_FLAG_TBL("LegacyECPointFormats", SSL_OP_LEGACY_EC_POINT_FORMATS),
411
0
    };
412
0
    if (value == NULL)
413
0
        return -3;
414
0
    cctx->tbl = ssl_option_list;
415
0
    cctx->ntbl = OSSL_NELEM(ssl_option_list);
416
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
417
0
}
418
419
static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
420
0
{
421
0
    static const ssl_flag_tbl ssl_vfy_list[] = {
422
0
        SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
423
0
        SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
424
0
        SSL_FLAG_VFY_SRV("Require",
425
0
            SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
426
0
        SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
427
0
        SSL_FLAG_VFY_SRV("RequestPostHandshake",
428
0
            SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
429
0
        SSL_FLAG_VFY_SRV("RequirePostHandshake",
430
0
            SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
431
0
    };
432
0
    if (value == NULL)
433
0
        return -3;
434
0
    cctx->tbl = ssl_vfy_list;
435
0
    cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
436
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
437
0
}
438
439
static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
440
0
{
441
0
    int rv = 1;
442
0
    CERT *c = NULL;
443
0
    if (cctx->ctx != NULL) {
444
0
        rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
445
0
        c = cctx->ctx->cert;
446
0
    }
447
0
    if (cctx->ssl != NULL) {
448
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
449
450
0
        if (sc != NULL) {
451
0
            rv = SSL_use_certificate_chain_file(cctx->ssl, value);
452
0
            c = sc->cert;
453
0
        } else {
454
0
            rv = 0;
455
0
        }
456
0
    }
457
0
    if (rv > 0 && c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
458
0
        size_t fileidx = c->key - c->pkeys;
459
460
0
        if (fileidx >= cctx->num_cert_filename) {
461
0
            rv = 0;
462
0
        } else {
463
0
            char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
464
465
0
            OPENSSL_free(*pfilename);
466
0
            *pfilename = OPENSSL_strdup(value);
467
0
            if (*pfilename == NULL)
468
0
                rv = 0;
469
0
        }
470
0
    }
471
472
0
    return rv > 0;
473
0
}
474
475
static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
476
0
{
477
0
    int rv = 1;
478
0
    if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
479
0
        return -2;
480
0
    if (cctx->ctx)
481
0
        rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
482
0
    if (cctx->ssl)
483
0
        rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
484
0
    return rv > 0;
485
0
}
486
487
static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
488
0
{
489
0
    int rv = 1;
490
0
    if (cctx->ctx)
491
0
        rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
492
0
    return rv > 0;
493
0
}
494
495
static int do_store(SSL_CONF_CTX *cctx,
496
    const char *CAfile, const char *CApath, const char *CAstore,
497
    int verify_store)
498
0
{
499
0
    CERT *cert;
500
0
    X509_STORE **st;
501
0
    SSL_CTX *ctx;
502
0
    OSSL_LIB_CTX *libctx = NULL;
503
0
    const char *propq = NULL;
504
505
0
    if (cctx->ctx != NULL) {
506
0
        cert = cctx->ctx->cert;
507
0
        ctx = cctx->ctx;
508
0
    } else if (cctx->ssl != NULL) {
509
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
510
511
0
        if (sc == NULL)
512
0
            return 0;
513
514
0
        cert = sc->cert;
515
0
        ctx = cctx->ssl->ctx;
516
0
    } else {
517
0
        return 1;
518
0
    }
519
0
    if (ctx != NULL) {
520
0
        libctx = ctx->libctx;
521
0
        propq = ctx->propq;
522
0
    }
523
0
    st = verify_store ? &cert->verify_store : &cert->chain_store;
524
0
    if (*st == NULL) {
525
0
        *st = X509_STORE_new();
526
0
        if (*st == NULL)
527
0
            return 0;
528
0
    }
529
530
0
    if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
531
0
        return 0;
532
0
    if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
533
0
        return 0;
534
0
    if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx, propq))
535
0
        return 0;
536
0
    return 1;
537
0
}
538
539
static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
540
0
{
541
0
    return do_store(cctx, NULL, value, NULL, 0);
542
0
}
543
544
static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
545
0
{
546
0
    return do_store(cctx, value, NULL, NULL, 0);
547
0
}
548
549
static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value)
550
0
{
551
0
    return do_store(cctx, NULL, NULL, value, 0);
552
0
}
553
554
static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
555
0
{
556
0
    return do_store(cctx, NULL, value, NULL, 1);
557
0
}
558
559
static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
560
0
{
561
0
    return do_store(cctx, value, NULL, NULL, 1);
562
0
}
563
564
static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value)
565
0
{
566
0
    return do_store(cctx, NULL, NULL, value, 1);
567
0
}
568
569
static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
570
0
{
571
0
    if (cctx->canames == NULL)
572
0
        cctx->canames = sk_X509_NAME_new_null();
573
0
    if (cctx->canames == NULL)
574
0
        return 0;
575
0
    return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
576
0
}
577
578
static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
579
0
{
580
0
    return cmd_RequestCAFile(cctx, value);
581
0
}
582
583
static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
584
0
{
585
0
    if (cctx->canames == NULL)
586
0
        cctx->canames = sk_X509_NAME_new_null();
587
0
    if (cctx->canames == NULL)
588
0
        return 0;
589
0
    return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
590
0
}
591
592
static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
593
0
{
594
0
    return cmd_RequestCAPath(cctx, value);
595
0
}
596
597
static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value)
598
0
{
599
0
    if (cctx->canames == NULL)
600
0
        cctx->canames = sk_X509_NAME_new_null();
601
0
    if (cctx->canames == NULL)
602
0
        return 0;
603
0
    return SSL_add_store_cert_subjects_to_stack(cctx->canames, value);
604
0
}
605
606
static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value)
607
0
{
608
0
    return cmd_RequestCAStore(cctx, value);
609
0
}
610
611
static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
612
0
{
613
0
    int rv = 0;
614
0
    EVP_PKEY *dhpkey = NULL;
615
0
    BIO *in = NULL;
616
0
    SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx;
617
0
    OSSL_DECODER_CTX *decoderctx = NULL;
618
619
0
    if (cctx->ctx != NULL || cctx->ssl != NULL) {
620
0
        in = BIO_new(BIO_s_file());
621
0
        if (in == NULL)
622
0
            goto end;
623
0
        if (BIO_read_filename(in, value) <= 0)
624
0
            goto end;
625
626
0
        decoderctx
627
0
            = OSSL_DECODER_CTX_new_for_pkey(&dhpkey, "PEM", NULL, "DH",
628
0
                OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
629
0
                sslctx->libctx, sslctx->propq);
630
0
        if (decoderctx == NULL)
631
0
            goto end;
632
0
        ERR_set_mark();
633
0
        while (!OSSL_DECODER_from_bio(decoderctx, in)
634
0
            && dhpkey == NULL
635
0
            && !BIO_eof(in))
636
0
            ;
637
0
        OSSL_DECODER_CTX_free(decoderctx);
638
639
0
        if (dhpkey == NULL) {
640
0
            ERR_clear_last_mark();
641
0
            goto end;
642
0
        }
643
0
        ERR_pop_to_mark();
644
0
    } else {
645
0
        return 1;
646
0
    }
647
648
0
    if (cctx->ctx != NULL) {
649
0
        if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
650
0
            dhpkey = NULL;
651
0
    }
652
0
    if (cctx->ssl != NULL) {
653
0
        if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
654
0
            dhpkey = NULL;
655
0
    }
656
0
end:
657
0
    EVP_PKEY_free(dhpkey);
658
0
    BIO_free(in);
659
0
    return rv > 0;
660
0
}
661
662
/*
663
 * |value| input is "<number[,number]>"
664
 * where the first number is the padding block size for
665
 * application data, and the optional second is the
666
 * padding block size for handshake messages
667
 */
668
static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
669
0
{
670
0
    int rv = 0;
671
0
    unsigned long block_padding = 0, hs_padding = 0;
672
0
    char *commap = NULL, *copy = NULL;
673
0
    char *endptr = NULL;
674
675
0
    copy = OPENSSL_strdup(value);
676
0
    if (copy == NULL)
677
0
        goto out;
678
0
    commap = strstr(copy, ",");
679
0
    if (commap != NULL) {
680
0
        *commap = '\0';
681
0
        if (*(commap + 1) == '\0')
682
0
            goto out;
683
0
        if (!OPENSSL_strtoul(commap + 1, &endptr, 0, &hs_padding))
684
0
            goto out;
685
0
    }
686
0
    if (!OPENSSL_strtoul(copy, &endptr, 0, &block_padding))
687
0
        goto out;
688
0
    if (commap == NULL)
689
0
        hs_padding = block_padding;
690
691
    /*
692
     * All we care about are non-negative values,
693
     * the setters check the range
694
     */
695
0
    if (cctx->ctx) {
696
        /*
697
         * QUIC always pads TLS data at the packet level, and as such, attempting
698
         * to set block padding at the record level fails in calls to SSL_CTX_set_block_padding_ex.
699
         * However, when configuring record padding via config file, we have no idea if we are
700
         * going to create TCP or QUIC based SSL's, so silently ignore this configuration option
701
         * for QUIC.
702
         */
703
0
        if (SSL_CTX_is_quic(cctx->ctx))
704
0
            rv = 1;
705
0
        else
706
0
            rv = SSL_CTX_set_block_padding_ex(cctx->ctx, (size_t)block_padding,
707
0
                (size_t)hs_padding);
708
0
    }
709
0
    if (cctx->ssl) {
710
        /*
711
         * As above, ignore this config option for QUIC
712
         */
713
0
        if (SSL_is_quic(cctx->ssl))
714
0
            rv = 1;
715
0
        else
716
0
            rv = SSL_set_block_padding_ex(cctx->ssl, (size_t)block_padding,
717
0
                (size_t)hs_padding);
718
0
    }
719
0
out:
720
0
    OPENSSL_free(copy);
721
0
    return rv;
722
0
}
723
724
static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
725
0
{
726
0
    int rv = 0;
727
0
    unsigned long ul;
728
729
0
    if (OPENSSL_strtoul(value, NULL, 10, &ul) && ul <= INT_MAX) {
730
0
        int num_tickets = (int)ul;
731
732
0
        if (cctx->ctx)
733
0
            rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
734
0
        if (cctx->ssl)
735
0
            rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
736
0
    }
737
0
    return rv;
738
0
}
739
740
typedef struct {
741
    int (*cmd)(SSL_CONF_CTX *cctx, const char *value);
742
    const char *str_file;
743
    const char *str_cmdline;
744
    unsigned short flags;
745
    unsigned short value_type;
746
} ssl_conf_cmd_tbl;
747
748
/* Table of supported parameters */
749
750
#define SSL_CONF_CMD(name, cmdopt, flags, type) \
751
    { cmd_##name, #name, cmdopt, flags, type }
752
753
#define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
754
    SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
755
756
#define SSL_CONF_CMD_SWITCH(name, flags) \
757
    { 0, NULL, name, flags, SSL_CONF_TYPE_NONE }
758
759
/* See apps/include/opt.h if you change this table. */
760
/* The SSL_CONF_CMD_SWITCH should be the same order as ssl_cmd_switches */
761
static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
762
    SSL_CONF_CMD_SWITCH("no_ssl3", 0),
763
    SSL_CONF_CMD_SWITCH("no_tls1", 0),
764
    SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
765
    SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
766
    SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
767
    SSL_CONF_CMD_SWITCH("bugs", 0),
768
    SSL_CONF_CMD_SWITCH("no_comp", 0),
769
    SSL_CONF_CMD_SWITCH("comp", 0),
770
    SSL_CONF_CMD_SWITCH("no_tx_cert_comp", 0),
771
    SSL_CONF_CMD_SWITCH("tx_cert_comp", 0),
772
    SSL_CONF_CMD_SWITCH("no_rx_cert_comp", 0),
773
    SSL_CONF_CMD_SWITCH("rx_cert_comp", 0),
774
    SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
775
    SSL_CONF_CMD_SWITCH("no_ticket", 0),
776
    SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
777
    SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
778
    SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER),
779
    SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT),
780
    SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
781
    SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
782
    SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_CLIENT),
783
    SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
784
    SSL_CONF_CMD_SWITCH("prefer_no_dhe_kex", 0),
785
    SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
786
    SSL_CONF_CMD_SWITCH("strict", 0),
787
    SSL_CONF_CMD_SWITCH("no_middlebox", 0),
788
    SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
789
    SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
790
    SSL_CONF_CMD_SWITCH("no_etm", 0),
791
    SSL_CONF_CMD_SWITCH("no_ems", 0),
792
    SSL_CONF_CMD_SWITCH("legacy_ec_point_formats", 0),
793
    SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
794
    SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
795
    SSL_CONF_CMD_STRING(Curves, "curves", 0),
796
    SSL_CONF_CMD_STRING(Groups, "groups", 0),
797
    SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
798
    SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
799
    SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
800
    SSL_CONF_CMD_STRING(Protocol, NULL, 0),
801
    SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
802
    SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
803
    SSL_CONF_CMD_STRING(Options, NULL, 0),
804
    SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
805
    SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
806
        SSL_CONF_TYPE_FILE),
807
    SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
808
        SSL_CONF_TYPE_FILE),
809
    SSL_CONF_CMD(ServerInfoFile, NULL,
810
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
811
        SSL_CONF_TYPE_FILE),
812
    SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
813
        SSL_CONF_TYPE_DIR),
814
    SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
815
        SSL_CONF_TYPE_FILE),
816
    SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
817
        SSL_CONF_TYPE_STORE),
818
    SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
819
        SSL_CONF_TYPE_DIR),
820
    SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
821
        SSL_CONF_TYPE_FILE),
822
    SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
823
        SSL_CONF_TYPE_STORE),
824
    SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
825
        SSL_CONF_TYPE_FILE),
826
    SSL_CONF_CMD(ClientCAFile, NULL,
827
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
828
        SSL_CONF_TYPE_FILE),
829
    SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
830
        SSL_CONF_TYPE_DIR),
831
    SSL_CONF_CMD(ClientCAPath, NULL,
832
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
833
        SSL_CONF_TYPE_DIR),
834
    SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
835
        SSL_CONF_TYPE_STORE),
836
    SSL_CONF_CMD(ClientCAStore, NULL,
837
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
838
        SSL_CONF_TYPE_STORE),
839
    SSL_CONF_CMD(DHParameters, "dhparam",
840
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
841
        SSL_CONF_TYPE_FILE),
842
    SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
843
    SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
844
};
845
846
/* Supported switches: must match order of switches in ssl_conf_cmds */
847
static const ssl_switch_tbl ssl_cmd_switches[] = {
848
    { SSL_OP_NO_SSLv3, 0 }, /* no_ssl3 */
849
    { SSL_OP_NO_TLSv1, 0 }, /* no_tls1 */
850
    { SSL_OP_NO_TLSv1_1, 0 }, /* no_tls1_1 */
851
    { SSL_OP_NO_TLSv1_2, 0 }, /* no_tls1_2 */
852
    { SSL_OP_NO_TLSv1_3, 0 }, /* no_tls1_3 */
853
    { SSL_OP_ALL, 0 }, /* bugs */
854
    { SSL_OP_NO_COMPRESSION, 0 }, /* no_comp */
855
    { SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV }, /* comp */
856
    { SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, 0 }, /* no_tx_cert_comp */
857
    { SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV }, /* tx_cert_comp */
858
    { SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, 0 }, /* no_rx_cert_comp */
859
    { SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV }, /* rx_cert_comp */
860
    { SSL_OP_SINGLE_ECDH_USE, 0 }, /* ecdh_single */
861
    { SSL_OP_NO_TICKET, 0 }, /* no_ticket */
862
    { SSL_OP_SERVER_PREFERENCE, 0 }, /* serverpref */
863
    /* legacy_renegotiation */
864
    { SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0 },
865
    /* Allow client renegotiation */
866
    { SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0 },
867
    /* legacy_server_connect */
868
    { SSL_OP_LEGACY_SERVER_CONNECT, 0 },
869
    /* no_renegotiation */
870
    { SSL_OP_NO_RENEGOTIATION, 0 },
871
    /* no_resumption_on_reneg */
872
    { SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0 },
873
    /* no_legacy_server_connect */
874
    { SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV },
875
    /* allow_no_dhe_kex */
876
    { SSL_OP_ALLOW_NO_DHE_KEX, 0 },
877
    /* prefer_no_dhe_kex */
878
    { SSL_OP_PREFER_NO_DHE_KEX, 0 },
879
    /* chacha reprioritization */
880
    { SSL_OP_PRIORITIZE_CHACHA, 0 },
881
    { SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT }, /* strict */
882
    /* no_middlebox */
883
    { SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV },
884
    /* anti_replay */
885
    { SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV },
886
    /* no_anti_replay */
887
    { SSL_OP_NO_ANTI_REPLAY, 0 },
888
    /* no Encrypt-then-Mac */
889
    { SSL_OP_NO_ENCRYPT_THEN_MAC, 0 },
890
    /* no Extended master secret */
891
    { SSL_OP_NO_EXTENDED_MASTER_SECRET, 0 },
892
    /* enable legacy EC point formats */
893
    { SSL_OP_LEGACY_EC_POINT_FORMATS, 0 }
894
};
895
896
static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
897
0
{
898
0
    if (pcmd == NULL || *pcmd == NULL)
899
0
        return 0;
900
    /* If a prefix is set, check and skip */
901
0
    if (cctx->prefix) {
902
0
        if (strlen(*pcmd) <= cctx->prefixlen)
903
0
            return 0;
904
0
        if (cctx->flags & SSL_CONF_FLAG_CMDLINE && strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
905
0
            return 0;
906
0
        if (cctx->flags & SSL_CONF_FLAG_FILE && OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
907
0
            return 0;
908
0
        *pcmd += cctx->prefixlen;
909
0
    } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
910
0
        if (**pcmd != '-' || !(*pcmd)[1])
911
0
            return 0;
912
0
        *pcmd += 1;
913
0
    }
914
0
    return 1;
915
0
}
916
917
/* Determine if a command is allowed according to cctx flags */
918
static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *t)
919
0
{
920
0
    unsigned int tfl = t->flags;
921
0
    unsigned int cfl = cctx->flags;
922
0
    if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
923
0
        return 0;
924
0
    if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
925
0
        return 0;
926
0
    if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
927
0
        && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
928
0
        return 0;
929
0
    return 1;
930
0
}
931
932
static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
933
    const char *cmd)
934
0
{
935
0
    const ssl_conf_cmd_tbl *t;
936
0
    size_t i;
937
0
    if (cmd == NULL)
938
0
        return NULL;
939
940
    /* Look for matching parameter name in table */
941
0
    for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
942
0
        if (ssl_conf_cmd_allowed(cctx, t)) {
943
0
            if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
944
0
                if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
945
0
                    return t;
946
0
            }
947
0
            if (cctx->flags & SSL_CONF_FLAG_FILE) {
948
0
                if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
949
0
                    return t;
950
0
            }
951
0
        }
952
0
    }
953
0
    return NULL;
954
0
}
955
956
static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *cmd)
957
0
{
958
    /* Find index of command in table */
959
0
    size_t idx = cmd - ssl_conf_cmds;
960
0
    const ssl_switch_tbl *scmd;
961
962
    /* Sanity check index */
963
0
    if (idx >= OSSL_NELEM(ssl_cmd_switches)) {
964
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
965
0
        return 0;
966
0
    }
967
    /* Obtain switches entry with same index */
968
0
    scmd = ssl_cmd_switches + idx;
969
0
    ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
970
0
    return 1;
971
0
}
972
973
int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
974
0
{
975
0
    const ssl_conf_cmd_tbl *runcmd;
976
0
    if (cmd == NULL) {
977
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
978
0
        return 0;
979
0
    }
980
981
0
    if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
982
0
        goto unknown_cmd;
983
984
0
    runcmd = ssl_conf_cmd_lookup(cctx, cmd);
985
986
0
    if (runcmd) {
987
0
        int rv = -3;
988
989
0
        if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
990
0
            return ctrl_switch_option(cctx, runcmd);
991
0
        }
992
0
        if (value == NULL)
993
0
            goto bad_value;
994
0
        rv = runcmd->cmd(cctx, value);
995
0
        if (rv > 0)
996
0
            return 2;
997
0
        if (rv != -2)
998
0
            rv = 0;
999
1000
0
    bad_value:
1001
0
        if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
1002
0
            ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
1003
0
                "cmd=%s, value=%s", cmd,
1004
0
                value != NULL ? value : "<EMPTY>");
1005
0
        return rv;
1006
0
    }
1007
1008
0
unknown_cmd:
1009
0
    if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
1010
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
1011
1012
0
    return -2;
1013
0
}
1014
1015
int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
1016
0
{
1017
0
    int rv;
1018
0
    const char *arg = NULL, *argn;
1019
1020
0
    if (pargc != NULL && *pargc == 0)
1021
0
        return 0;
1022
0
    if (pargc == NULL || *pargc > 0)
1023
0
        arg = **pargv;
1024
0
    if (arg == NULL)
1025
0
        return 0;
1026
0
    if (pargc == NULL || *pargc > 1)
1027
0
        argn = (*pargv)[1];
1028
0
    else
1029
0
        argn = NULL;
1030
0
    cctx->flags &= ~SSL_CONF_FLAG_FILE;
1031
0
    cctx->flags |= SSL_CONF_FLAG_CMDLINE;
1032
0
    rv = SSL_CONF_cmd(cctx, arg, argn);
1033
0
    if (rv > 0) {
1034
        /* Success: update pargc, pargv */
1035
0
        (*pargv) += rv;
1036
0
        if (pargc)
1037
0
            (*pargc) -= rv;
1038
0
        return rv;
1039
0
    }
1040
    /* Unknown switch: indicate no arguments processed */
1041
0
    if (rv == -2)
1042
0
        return 0;
1043
    /* Some error occurred processing command, return fatal error */
1044
0
    if (rv == 0)
1045
0
        return -1;
1046
0
    return rv;
1047
0
}
1048
1049
int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
1050
0
{
1051
0
    if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
1052
0
        const ssl_conf_cmd_tbl *runcmd;
1053
0
        runcmd = ssl_conf_cmd_lookup(cctx, cmd);
1054
0
        if (runcmd)
1055
0
            return runcmd->value_type;
1056
0
    }
1057
0
    return SSL_CONF_TYPE_UNKNOWN;
1058
0
}
1059
1060
SSL_CONF_CTX *SSL_CONF_CTX_new(void)
1061
0
{
1062
0
    SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
1063
1064
0
    return ret;
1065
0
}
1066
1067
int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
1068
0
{
1069
    /* See if any certificates are missing private keys */
1070
0
    size_t i;
1071
0
    CERT *c = NULL;
1072
1073
0
    if (cctx->ctx != NULL) {
1074
0
        c = cctx->ctx->cert;
1075
0
    } else if (cctx->ssl != NULL) {
1076
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
1077
1078
0
        if (sc != NULL)
1079
0
            c = sc->cert;
1080
0
    }
1081
0
    if (c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
1082
0
        for (i = 0; i < cctx->num_cert_filename; i++) {
1083
0
            const char *p = cctx->cert_filename[i];
1084
1085
            /*
1086
             * If missing private key try to load one from certificate file
1087
             */
1088
0
            if (p != NULL && c->pkeys[i].privatekey == NULL) {
1089
0
                if (!cmd_PrivateKey(cctx, p))
1090
0
                    return 0;
1091
0
            }
1092
0
        }
1093
0
    }
1094
0
    if (cctx->canames) {
1095
0
        if (cctx->ssl)
1096
0
            SSL_set0_CA_list(cctx->ssl, cctx->canames);
1097
0
        else if (cctx->ctx)
1098
0
            SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
1099
0
        else
1100
0
            sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1101
0
        cctx->canames = NULL;
1102
0
    }
1103
0
    return 1;
1104
0
}
1105
1106
static void free_cert_filename(SSL_CONF_CTX *cctx)
1107
0
{
1108
0
    size_t i;
1109
1110
0
    for (i = 0; i < cctx->num_cert_filename; i++)
1111
0
        OPENSSL_free(cctx->cert_filename[i]);
1112
0
    OPENSSL_free(cctx->cert_filename);
1113
0
    cctx->cert_filename = NULL;
1114
0
    cctx->num_cert_filename = 0;
1115
0
}
1116
1117
void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
1118
0
{
1119
0
    if (cctx) {
1120
0
        free_cert_filename(cctx);
1121
0
        OPENSSL_free(cctx->prefix);
1122
0
        sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1123
0
        OPENSSL_free(cctx);
1124
0
    }
1125
0
}
1126
1127
unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1128
0
{
1129
0
    if ((cctx->flags & SSL_CONF_FLAG_CMDLINE)
1130
0
        && (flags & SSL_CONF_FLAG_FILE))
1131
0
        flags &= ~SSL_CONF_FLAG_FILE;
1132
1133
0
    if ((cctx->flags & SSL_CONF_FLAG_FILE)
1134
0
        && (flags & SSL_CONF_FLAG_CMDLINE))
1135
0
        flags &= ~SSL_CONF_FLAG_CMDLINE;
1136
1137
0
    cctx->flags |= flags;
1138
0
    return cctx->flags;
1139
0
}
1140
1141
unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1142
0
{
1143
0
    cctx->flags &= ~flags;
1144
0
    return cctx->flags;
1145
0
}
1146
1147
int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
1148
0
{
1149
0
    char *tmp = NULL;
1150
0
    if (pre) {
1151
0
        tmp = OPENSSL_strdup(pre);
1152
0
        if (tmp == NULL)
1153
0
            return 0;
1154
0
    }
1155
0
    OPENSSL_free(cctx->prefix);
1156
0
    cctx->prefix = tmp;
1157
0
    if (tmp)
1158
0
        cctx->prefixlen = strlen(tmp);
1159
0
    else
1160
0
        cctx->prefixlen = 0;
1161
0
    return 1;
1162
0
}
1163
1164
void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
1165
0
{
1166
0
    cctx->ssl = ssl;
1167
0
    cctx->ctx = NULL;
1168
0
    free_cert_filename(cctx);
1169
0
    if (ssl != NULL) {
1170
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1171
1172
0
        if (sc == NULL)
1173
0
            return;
1174
0
        cctx->poptions = &sc->options;
1175
0
        cctx->min_version = &sc->min_proto_version;
1176
0
        cctx->max_version = &sc->max_proto_version;
1177
0
        cctx->pcert_flags = &sc->cert->cert_flags;
1178
0
        cctx->pvfy_flags = &sc->verify_mode;
1179
0
        cctx->cert_filename = OPENSSL_zalloc(sc->ssl_pkey_num
1180
0
            * sizeof(*cctx->cert_filename));
1181
0
        if (cctx->cert_filename != NULL)
1182
0
            cctx->num_cert_filename = sc->ssl_pkey_num;
1183
0
    } else {
1184
0
        cctx->poptions = NULL;
1185
0
        cctx->min_version = NULL;
1186
0
        cctx->max_version = NULL;
1187
0
        cctx->pcert_flags = NULL;
1188
0
        cctx->pvfy_flags = NULL;
1189
0
    }
1190
0
}
1191
1192
void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
1193
0
{
1194
0
    cctx->ctx = ctx;
1195
0
    cctx->ssl = NULL;
1196
0
    free_cert_filename(cctx);
1197
0
    if (ctx) {
1198
0
        cctx->poptions = &ctx->options;
1199
0
        cctx->min_version = &ctx->min_proto_version;
1200
0
        cctx->max_version = &ctx->max_proto_version;
1201
0
        cctx->pcert_flags = &ctx->cert->cert_flags;
1202
0
        cctx->pvfy_flags = &ctx->verify_mode;
1203
0
        cctx->cert_filename = OPENSSL_zalloc((SSL_PKEY_NUM + ctx->sigalg_list_len)
1204
0
            * sizeof(*cctx->cert_filename));
1205
0
        if (cctx->cert_filename != NULL)
1206
0
            cctx->num_cert_filename = SSL_PKEY_NUM + ctx->sigalg_list_len;
1207
0
    } else {
1208
0
        cctx->poptions = NULL;
1209
0
        cctx->min_version = NULL;
1210
0
        cctx->max_version = NULL;
1211
0
        cctx->pcert_flags = NULL;
1212
        cctx->pvfy_flags = NULL;
1213
0
    }
1214
0
}