Coverage Report

Created: 2026-08-31 06:56

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
        SSL_FLAG_TBL_INV("DTLSv1.3", SSL_OP_NO_DTLSv1_3)
294
0
    };
295
0
    cctx->tbl = ssl_protocol_list;
296
0
    cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
297
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
298
0
}
299
300
/*
301
 * protocol_from_string - converts a protocol version string to a number
302
 *
303
 * Returns -1 on failure or the version on success
304
 */
305
static int protocol_from_string(const char *value)
306
0
{
307
0
    struct protocol_versions {
308
0
        const char *name;
309
0
        int version;
310
0
    };
311
    /*
312
     * Note: To avoid breaking previously valid configurations, we must retain
313
     * legacy entries in this table even if the underlying protocol is no
314
     * longer supported.  This also means that the constants SSL3_VERSION, ...
315
     * need to be retained indefinitely.  This table can only grow, never
316
     * shrink.
317
     */
318
0
    static const struct protocol_versions versions[] = {
319
0
        { "None", 0 },
320
0
        { "SSLv3", SSL3_VERSION },
321
0
        { "TLSv1", TLS1_VERSION },
322
0
        { "TLSv1.1", TLS1_1_VERSION },
323
0
        { "TLSv1.2", TLS1_2_VERSION },
324
0
        { "TLSv1.3", TLS1_3_VERSION },
325
0
        { "DTLSv1", DTLS1_VERSION },
326
0
        { "DTLSv1.2", DTLS1_2_VERSION },
327
0
        { "DTLSv1.3", DTLS1_3_VERSION }
328
0
    };
329
0
    size_t i;
330
0
    size_t n = OSSL_NELEM(versions);
331
332
0
    for (i = 0; i < n; i++)
333
0
        if (strcmp(versions[i].name, value) == 0)
334
0
            return versions[i].version;
335
0
    return -1;
336
0
}
337
338
static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
339
0
{
340
0
    int method_version;
341
0
    int new_version;
342
343
0
    if (cctx->ctx != NULL)
344
0
        method_version = cctx->ctx->method->version;
345
0
    else if (cctx->ssl != NULL)
346
0
        method_version = cctx->ssl->defltmeth->version;
347
0
    else
348
0
        return 0;
349
0
    if ((new_version = protocol_from_string(value)) < 0)
350
0
        return 0;
351
0
    return ssl_set_version_bound(method_version, new_version, bound);
352
0
}
353
354
/*
355
 * cmd_MinProtocol - Set min protocol version
356
 * @cctx: config structure to save settings in
357
 * @value: The min protocol version in string form
358
 *
359
 * Returns 1 on success and 0 on failure.
360
 */
361
static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
362
0
{
363
0
    return min_max_proto(cctx, value, cctx->min_version);
364
0
}
365
366
/*
367
 * cmd_MaxProtocol - Set max protocol version
368
 * @cctx: config structure to save settings in
369
 * @value: The max protocol version in string form
370
 *
371
 * Returns 1 on success and 0 on failure.
372
 */
373
static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
374
0
{
375
0
    return min_max_proto(cctx, value, cctx->max_version);
376
0
}
377
378
static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
379
0
{
380
0
    static const ssl_flag_tbl ssl_option_list[] = {
381
0
        SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
382
0
        SSL_FLAG_TBL_INV("EmptyFragments",
383
0
            SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
384
0
        SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
385
0
        SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
386
0
        SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_SERVER_PREFERENCE),
387
0
        SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
388
0
            SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
389
0
        SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
390
0
        SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
391
0
        SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
392
0
            SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
393
0
        SSL_FLAG_TBL("UnsafeLegacyServerConnect",
394
0
            SSL_OP_LEGACY_SERVER_CONNECT),
395
0
        SSL_FLAG_TBL("ClientRenegotiation",
396
0
            SSL_OP_ALLOW_CLIENT_RENEGOTIATION),
397
0
        SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
398
0
        SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
399
0
        SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
400
0
        SSL_FLAG_TBL("PreferNoDHEKEX", SSL_OP_PREFER_NO_DHE_KEX),
401
0
        SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
402
0
        SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
403
0
        SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
404
0
        SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
405
0
        SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES),
406
0
        SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS),
407
0
        SSL_FLAG_TBL_CERT("StrictCertCheck", SSL_CERT_FLAG_TLS_STRICT),
408
0
        SSL_FLAG_TBL_INV("TxCertificateCompression", SSL_OP_NO_TX_CERTIFICATE_COMPRESSION),
409
0
        SSL_FLAG_TBL_INV("RxCertificateCompression", SSL_OP_NO_RX_CERTIFICATE_COMPRESSION),
410
0
        SSL_FLAG_TBL("KTLSTxZerocopySendfile", SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE),
411
0
        SSL_FLAG_TBL("IgnoreUnexpectedEOF", SSL_OP_IGNORE_UNEXPECTED_EOF),
412
0
        SSL_FLAG_TBL("LegacyECPointFormats", SSL_OP_LEGACY_EC_POINT_FORMATS),
413
0
    };
414
0
    if (value == NULL)
415
0
        return -3;
416
0
    cctx->tbl = ssl_option_list;
417
0
    cctx->ntbl = OSSL_NELEM(ssl_option_list);
418
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
419
0
}
420
421
static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
422
0
{
423
0
    static const ssl_flag_tbl ssl_vfy_list[] = {
424
0
        SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
425
0
        SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
426
0
        SSL_FLAG_VFY_SRV("Require",
427
0
            SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
428
0
        SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
429
0
        SSL_FLAG_VFY_SRV("RequestPostHandshake",
430
0
            SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
431
0
        SSL_FLAG_VFY_SRV("RequirePostHandshake",
432
0
            SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
433
0
    };
434
0
    if (value == NULL)
435
0
        return -3;
436
0
    cctx->tbl = ssl_vfy_list;
437
0
    cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
438
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
439
0
}
440
441
static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
442
0
{
443
0
    int rv = 1;
444
0
    CERT *c = NULL;
445
0
    if (cctx->ctx != NULL) {
446
0
        rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
447
0
        c = cctx->ctx->cert;
448
0
    }
449
0
    if (cctx->ssl != NULL) {
450
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
451
452
0
        if (sc != NULL) {
453
0
            rv = SSL_use_certificate_chain_file(cctx->ssl, value);
454
0
            c = sc->cert;
455
0
        } else {
456
0
            rv = 0;
457
0
        }
458
0
    }
459
0
    if (rv > 0 && c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
460
0
        size_t fileidx = c->key - c->pkeys;
461
462
0
        if (fileidx >= cctx->num_cert_filename) {
463
0
            rv = 0;
464
0
        } else {
465
0
            char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
466
467
0
            OPENSSL_free(*pfilename);
468
0
            *pfilename = OPENSSL_strdup(value);
469
0
            if (*pfilename == NULL)
470
0
                rv = 0;
471
0
        }
472
0
    }
473
474
0
    return rv > 0;
475
0
}
476
477
static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
478
0
{
479
0
    int rv = 1;
480
0
    if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
481
0
        return -2;
482
0
    if (cctx->ctx)
483
0
        rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
484
0
    if (cctx->ssl)
485
0
        rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
486
0
    return rv > 0;
487
0
}
488
489
static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
490
0
{
491
0
    int rv = 1;
492
0
    if (cctx->ctx)
493
0
        rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
494
0
    return rv > 0;
495
0
}
496
497
static int do_store(SSL_CONF_CTX *cctx,
498
    const char *CAfile, const char *CApath, const char *CAstore,
499
    int verify_store)
500
0
{
501
0
    CERT *cert;
502
0
    X509_STORE **st;
503
0
    SSL_CTX *ctx;
504
0
    OSSL_LIB_CTX *libctx = NULL;
505
0
    const char *propq = NULL;
506
507
0
    if (cctx->ctx != NULL) {
508
0
        cert = cctx->ctx->cert;
509
0
        ctx = cctx->ctx;
510
0
    } else if (cctx->ssl != NULL) {
511
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
512
513
0
        if (sc == NULL)
514
0
            return 0;
515
516
0
        cert = sc->cert;
517
0
        ctx = cctx->ssl->ctx;
518
0
    } else {
519
0
        return 1;
520
0
    }
521
0
    if (ctx != NULL) {
522
0
        libctx = ctx->libctx;
523
0
        propq = ctx->propq;
524
0
    }
525
0
    st = verify_store ? &cert->verify_store : &cert->chain_store;
526
0
    if (*st == NULL) {
527
0
        *st = X509_STORE_new();
528
0
        if (*st == NULL)
529
0
            return 0;
530
0
    }
531
532
0
    if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
533
0
        return 0;
534
0
    if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
535
0
        return 0;
536
0
    if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx, propq))
537
0
        return 0;
538
0
    return 1;
539
0
}
540
541
static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
542
0
{
543
0
    return do_store(cctx, NULL, value, NULL, 0);
544
0
}
545
546
static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
547
0
{
548
0
    return do_store(cctx, value, NULL, NULL, 0);
549
0
}
550
551
static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value)
552
0
{
553
0
    return do_store(cctx, NULL, NULL, value, 0);
554
0
}
555
556
static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
557
0
{
558
0
    return do_store(cctx, NULL, value, NULL, 1);
559
0
}
560
561
static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
562
0
{
563
0
    return do_store(cctx, value, NULL, NULL, 1);
564
0
}
565
566
static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value)
567
0
{
568
0
    return do_store(cctx, NULL, NULL, value, 1);
569
0
}
570
571
static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
572
0
{
573
0
    if (cctx->canames == NULL)
574
0
        cctx->canames = sk_X509_NAME_new_null();
575
0
    if (cctx->canames == NULL)
576
0
        return 0;
577
0
    return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
578
0
}
579
580
static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
581
0
{
582
0
    return cmd_RequestCAFile(cctx, value);
583
0
}
584
585
static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
586
0
{
587
0
    if (cctx->canames == NULL)
588
0
        cctx->canames = sk_X509_NAME_new_null();
589
0
    if (cctx->canames == NULL)
590
0
        return 0;
591
0
    return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
592
0
}
593
594
static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
595
0
{
596
0
    return cmd_RequestCAPath(cctx, value);
597
0
}
598
599
static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value)
600
0
{
601
0
    if (cctx->canames == NULL)
602
0
        cctx->canames = sk_X509_NAME_new_null();
603
0
    if (cctx->canames == NULL)
604
0
        return 0;
605
0
    return SSL_add_store_cert_subjects_to_stack(cctx->canames, value);
606
0
}
607
608
static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value)
609
0
{
610
0
    return cmd_RequestCAStore(cctx, value);
611
0
}
612
613
static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
614
0
{
615
0
    int rv = 0;
616
0
    EVP_PKEY *dhpkey = NULL;
617
0
    BIO *in = NULL;
618
0
    SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx;
619
0
    OSSL_DECODER_CTX *decoderctx = NULL;
620
621
0
    if (cctx->ctx != NULL || cctx->ssl != NULL) {
622
0
        in = BIO_new(BIO_s_file());
623
0
        if (in == NULL)
624
0
            goto end;
625
0
        if (BIO_read_filename(in, value) <= 0)
626
0
            goto end;
627
628
0
        decoderctx
629
0
            = OSSL_DECODER_CTX_new_for_pkey(&dhpkey, "PEM", NULL, "DH",
630
0
                OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
631
0
                sslctx->libctx, sslctx->propq);
632
0
        if (decoderctx == NULL)
633
0
            goto end;
634
0
        ERR_set_mark();
635
0
        while (!OSSL_DECODER_from_bio(decoderctx, in)
636
0
            && dhpkey == NULL
637
0
            && !BIO_eof(in))
638
0
            ;
639
0
        OSSL_DECODER_CTX_free(decoderctx);
640
641
0
        if (dhpkey == NULL) {
642
0
            ERR_clear_last_mark();
643
0
            goto end;
644
0
        }
645
0
        ERR_pop_to_mark();
646
0
    } else {
647
0
        return 1;
648
0
    }
649
650
0
    if (cctx->ctx != NULL) {
651
0
        if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
652
0
            dhpkey = NULL;
653
0
    }
654
0
    if (cctx->ssl != NULL) {
655
0
        if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
656
0
            dhpkey = NULL;
657
0
    }
658
0
end:
659
0
    EVP_PKEY_free(dhpkey);
660
0
    BIO_free(in);
661
0
    return rv > 0;
662
0
}
663
664
/*
665
 * |value| input is "<number[,number]>"
666
 * where the first number is the padding block size for
667
 * application data, and the optional second is the
668
 * padding block size for handshake messages
669
 */
670
static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
671
0
{
672
0
    int rv = 0;
673
0
    unsigned long block_padding = 0, hs_padding = 0;
674
0
    char *commap = NULL, *copy = NULL;
675
0
    char *endptr = NULL;
676
677
0
    copy = OPENSSL_strdup(value);
678
0
    if (copy == NULL)
679
0
        goto out;
680
0
    commap = strstr(copy, ",");
681
0
    if (commap != NULL) {
682
0
        *commap = '\0';
683
0
        if (*(commap + 1) == '\0')
684
0
            goto out;
685
0
        if (!OPENSSL_strtoul(commap + 1, &endptr, 0, &hs_padding))
686
0
            goto out;
687
0
    }
688
0
    if (!OPENSSL_strtoul(copy, &endptr, 0, &block_padding))
689
0
        goto out;
690
0
    if (commap == NULL)
691
0
        hs_padding = block_padding;
692
693
    /*
694
     * All we care about are non-negative values,
695
     * the setters check the range
696
     */
697
0
    if (cctx->ctx) {
698
        /*
699
         * QUIC always pads TLS data at the packet level, and as such, attempting
700
         * to set block padding at the record level fails in calls to SSL_CTX_set_block_padding_ex.
701
         * However, when configuring record padding via config file, we have no idea if we are
702
         * going to create TCP or QUIC based SSL's, so silently ignore this configuration option
703
         * for QUIC.
704
         */
705
0
        if (SSL_CTX_is_quic(cctx->ctx))
706
0
            rv = 1;
707
0
        else
708
0
            rv = SSL_CTX_set_block_padding_ex(cctx->ctx, (size_t)block_padding,
709
0
                (size_t)hs_padding);
710
0
    }
711
0
    if (cctx->ssl) {
712
        /*
713
         * As above, ignore this config option for QUIC
714
         */
715
0
        if (SSL_is_quic(cctx->ssl))
716
0
            rv = 1;
717
0
        else
718
0
            rv = SSL_set_block_padding_ex(cctx->ssl, (size_t)block_padding,
719
0
                (size_t)hs_padding);
720
0
    }
721
0
out:
722
0
    OPENSSL_free(copy);
723
0
    return rv;
724
0
}
725
726
static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
727
0
{
728
0
    int rv = 0;
729
0
    unsigned long ul;
730
731
0
    if (OPENSSL_strtoul(value, NULL, 10, &ul) && ul <= INT_MAX) {
732
0
        int num_tickets = (int)ul;
733
734
0
        if (cctx->ctx)
735
0
            rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
736
0
        if (cctx->ssl)
737
0
            rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
738
0
    }
739
0
    return rv;
740
0
}
741
742
typedef struct {
743
    int (*cmd)(SSL_CONF_CTX *cctx, const char *value);
744
    const char *str_file;
745
    const char *str_cmdline;
746
    unsigned short flags;
747
    unsigned short value_type;
748
} ssl_conf_cmd_tbl;
749
750
/* Table of supported parameters */
751
752
#define SSL_CONF_CMD(name, cmdopt, flags, type) \
753
    { cmd_##name, #name, cmdopt, flags, type }
754
755
#define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
756
    SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
757
758
#define SSL_CONF_CMD_SWITCH(name, flags) \
759
    { 0, NULL, name, flags, SSL_CONF_TYPE_NONE }
760
761
/* See apps/include/opt.h if you change this table. */
762
/* The SSL_CONF_CMD_SWITCH should be the same order as ssl_cmd_switches */
763
static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
764
    SSL_CONF_CMD_SWITCH("no_ssl3", 0),
765
    SSL_CONF_CMD_SWITCH("no_tls1", 0),
766
    SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
767
    SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
768
    SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
769
    SSL_CONF_CMD_SWITCH("bugs", 0),
770
    SSL_CONF_CMD_SWITCH("no_comp", 0),
771
    SSL_CONF_CMD_SWITCH("comp", 0),
772
    SSL_CONF_CMD_SWITCH("no_tx_cert_comp", 0),
773
    SSL_CONF_CMD_SWITCH("tx_cert_comp", 0),
774
    SSL_CONF_CMD_SWITCH("no_rx_cert_comp", 0),
775
    SSL_CONF_CMD_SWITCH("rx_cert_comp", 0),
776
    SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
777
    SSL_CONF_CMD_SWITCH("no_ticket", 0),
778
    SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
779
    SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
780
    SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER),
781
    SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT),
782
    SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
783
    SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
784
    SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_CLIENT),
785
    SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
786
    SSL_CONF_CMD_SWITCH("prefer_no_dhe_kex", 0),
787
    SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
788
    SSL_CONF_CMD_SWITCH("strict", 0),
789
    SSL_CONF_CMD_SWITCH("no_middlebox", 0),
790
    SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
791
    SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
792
    SSL_CONF_CMD_SWITCH("no_etm", 0),
793
    SSL_CONF_CMD_SWITCH("no_ems", 0),
794
    SSL_CONF_CMD_SWITCH("legacy_ec_point_formats", 0),
795
    SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
796
    SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
797
    SSL_CONF_CMD_STRING(Curves, "curves", 0),
798
    SSL_CONF_CMD_STRING(Groups, "groups", 0),
799
    SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
800
    SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
801
    SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
802
    SSL_CONF_CMD_STRING(Protocol, NULL, 0),
803
    SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
804
    SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
805
    SSL_CONF_CMD_STRING(Options, NULL, 0),
806
    SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
807
    SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
808
        SSL_CONF_TYPE_FILE),
809
    SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
810
        SSL_CONF_TYPE_FILE),
811
    SSL_CONF_CMD(ServerInfoFile, NULL,
812
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
813
        SSL_CONF_TYPE_FILE),
814
    SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
815
        SSL_CONF_TYPE_DIR),
816
    SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
817
        SSL_CONF_TYPE_FILE),
818
    SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
819
        SSL_CONF_TYPE_STORE),
820
    SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
821
        SSL_CONF_TYPE_DIR),
822
    SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
823
        SSL_CONF_TYPE_FILE),
824
    SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
825
        SSL_CONF_TYPE_STORE),
826
    SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
827
        SSL_CONF_TYPE_FILE),
828
    SSL_CONF_CMD(ClientCAFile, NULL,
829
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
830
        SSL_CONF_TYPE_FILE),
831
    SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
832
        SSL_CONF_TYPE_DIR),
833
    SSL_CONF_CMD(ClientCAPath, NULL,
834
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
835
        SSL_CONF_TYPE_DIR),
836
    SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
837
        SSL_CONF_TYPE_STORE),
838
    SSL_CONF_CMD(ClientCAStore, NULL,
839
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
840
        SSL_CONF_TYPE_STORE),
841
    SSL_CONF_CMD(DHParameters, "dhparam",
842
        SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
843
        SSL_CONF_TYPE_FILE),
844
    SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
845
    SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
846
};
847
848
/* Supported switches: must match order of switches in ssl_conf_cmds */
849
static const ssl_switch_tbl ssl_cmd_switches[] = {
850
    { SSL_OP_NO_SSLv3, 0 }, /* no_ssl3 */
851
    { SSL_OP_NO_TLSv1, 0 }, /* no_tls1 */
852
    { SSL_OP_NO_TLSv1_1, 0 }, /* no_tls1_1 */
853
    { SSL_OP_NO_TLSv1_2, 0 }, /* no_tls1_2 */
854
    { SSL_OP_NO_TLSv1_3, 0 }, /* no_tls1_3 */
855
    { SSL_OP_ALL, 0 }, /* bugs */
856
    { SSL_OP_NO_COMPRESSION, 0 }, /* no_comp */
857
    { SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV }, /* comp */
858
    { SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, 0 }, /* no_tx_cert_comp */
859
    { SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV }, /* tx_cert_comp */
860
    { SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, 0 }, /* no_rx_cert_comp */
861
    { SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV }, /* rx_cert_comp */
862
    { SSL_OP_SINGLE_ECDH_USE, 0 }, /* ecdh_single */
863
    { SSL_OP_NO_TICKET, 0 }, /* no_ticket */
864
    { SSL_OP_SERVER_PREFERENCE, 0 }, /* serverpref */
865
    /* legacy_renegotiation */
866
    { SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0 },
867
    /* Allow client renegotiation */
868
    { SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0 },
869
    /* legacy_server_connect */
870
    { SSL_OP_LEGACY_SERVER_CONNECT, 0 },
871
    /* no_renegotiation */
872
    { SSL_OP_NO_RENEGOTIATION, 0 },
873
    /* no_resumption_on_reneg */
874
    { SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0 },
875
    /* no_legacy_server_connect */
876
    { SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV },
877
    /* allow_no_dhe_kex */
878
    { SSL_OP_ALLOW_NO_DHE_KEX, 0 },
879
    /* prefer_no_dhe_kex */
880
    { SSL_OP_PREFER_NO_DHE_KEX, 0 },
881
    /* chacha reprioritization */
882
    { SSL_OP_PRIORITIZE_CHACHA, 0 },
883
    { SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT }, /* strict */
884
    /* no_middlebox */
885
    { SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV },
886
    /* anti_replay */
887
    { SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV },
888
    /* no_anti_replay */
889
    { SSL_OP_NO_ANTI_REPLAY, 0 },
890
    /* no Encrypt-then-Mac */
891
    { SSL_OP_NO_ENCRYPT_THEN_MAC, 0 },
892
    /* no Extended master secret */
893
    { SSL_OP_NO_EXTENDED_MASTER_SECRET, 0 },
894
    /* enable legacy EC point formats */
895
    { SSL_OP_LEGACY_EC_POINT_FORMATS, 0 }
896
};
897
898
static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
899
0
{
900
0
    if (pcmd == NULL || *pcmd == NULL)
901
0
        return 0;
902
    /* If a prefix is set, check and skip */
903
0
    if (cctx->prefix) {
904
0
        if (strlen(*pcmd) <= cctx->prefixlen)
905
0
            return 0;
906
0
        if (cctx->flags & SSL_CONF_FLAG_CMDLINE && strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
907
0
            return 0;
908
0
        if (cctx->flags & SSL_CONF_FLAG_FILE && OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
909
0
            return 0;
910
0
        *pcmd += cctx->prefixlen;
911
0
    } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
912
0
        if (**pcmd != '-' || !(*pcmd)[1])
913
0
            return 0;
914
0
        *pcmd += 1;
915
0
    }
916
0
    return 1;
917
0
}
918
919
/* Determine if a command is allowed according to cctx flags */
920
static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *t)
921
0
{
922
0
    unsigned int tfl = t->flags;
923
0
    unsigned int cfl = cctx->flags;
924
0
    if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
925
0
        return 0;
926
0
    if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
927
0
        return 0;
928
0
    if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
929
0
        && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
930
0
        return 0;
931
0
    return 1;
932
0
}
933
934
static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
935
    const char *cmd)
936
0
{
937
0
    const ssl_conf_cmd_tbl *t;
938
0
    size_t i;
939
0
    if (cmd == NULL)
940
0
        return NULL;
941
942
    /* Look for matching parameter name in table */
943
0
    for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
944
0
        if (ssl_conf_cmd_allowed(cctx, t)) {
945
0
            if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
946
0
                if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
947
0
                    return t;
948
0
            }
949
0
            if (cctx->flags & SSL_CONF_FLAG_FILE) {
950
0
                if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
951
0
                    return t;
952
0
            }
953
0
        }
954
0
    }
955
0
    return NULL;
956
0
}
957
958
static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *cmd)
959
0
{
960
    /* Find index of command in table */
961
0
    size_t idx = cmd - ssl_conf_cmds;
962
0
    const ssl_switch_tbl *scmd;
963
964
    /* Sanity check index */
965
0
    if (idx >= OSSL_NELEM(ssl_cmd_switches)) {
966
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
967
0
        return 0;
968
0
    }
969
    /* Obtain switches entry with same index */
970
0
    scmd = ssl_cmd_switches + idx;
971
0
    ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
972
0
    return 1;
973
0
}
974
975
int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
976
0
{
977
0
    const ssl_conf_cmd_tbl *runcmd;
978
0
    if (cmd == NULL) {
979
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
980
0
        return 0;
981
0
    }
982
983
0
    if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
984
0
        goto unknown_cmd;
985
986
0
    runcmd = ssl_conf_cmd_lookup(cctx, cmd);
987
988
0
    if (runcmd) {
989
0
        int rv = -3;
990
991
0
        if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
992
0
            return ctrl_switch_option(cctx, runcmd);
993
0
        }
994
0
        if (value == NULL)
995
0
            goto bad_value;
996
0
        rv = runcmd->cmd(cctx, value);
997
0
        if (rv > 0)
998
0
            return 2;
999
0
        if (rv != -2)
1000
0
            rv = 0;
1001
1002
0
    bad_value:
1003
0
        if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
1004
0
            ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
1005
0
                "cmd=%s, value=%s", cmd,
1006
0
                value != NULL ? value : "<EMPTY>");
1007
0
        return rv;
1008
0
    }
1009
1010
0
unknown_cmd:
1011
0
    if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
1012
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
1013
1014
0
    return -2;
1015
0
}
1016
1017
int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
1018
0
{
1019
0
    int rv;
1020
0
    const char *arg = NULL, *argn;
1021
1022
0
    if (pargc != NULL && *pargc == 0)
1023
0
        return 0;
1024
0
    if (pargc == NULL || *pargc > 0)
1025
0
        arg = **pargv;
1026
0
    if (arg == NULL)
1027
0
        return 0;
1028
0
    if (pargc == NULL || *pargc > 1)
1029
0
        argn = (*pargv)[1];
1030
0
    else
1031
0
        argn = NULL;
1032
0
    cctx->flags &= ~SSL_CONF_FLAG_FILE;
1033
0
    cctx->flags |= SSL_CONF_FLAG_CMDLINE;
1034
0
    rv = SSL_CONF_cmd(cctx, arg, argn);
1035
0
    if (rv > 0) {
1036
        /* Success: update pargc, pargv */
1037
0
        (*pargv) += rv;
1038
0
        if (pargc)
1039
0
            (*pargc) -= rv;
1040
0
        return rv;
1041
0
    }
1042
    /* Unknown switch: indicate no arguments processed */
1043
0
    if (rv == -2)
1044
0
        return 0;
1045
    /* Some error occurred processing command, return fatal error */
1046
0
    if (rv == 0)
1047
0
        return -1;
1048
0
    return rv;
1049
0
}
1050
1051
int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
1052
0
{
1053
0
    if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
1054
0
        const ssl_conf_cmd_tbl *runcmd;
1055
0
        runcmd = ssl_conf_cmd_lookup(cctx, cmd);
1056
0
        if (runcmd)
1057
0
            return runcmd->value_type;
1058
0
    }
1059
0
    return SSL_CONF_TYPE_UNKNOWN;
1060
0
}
1061
1062
SSL_CONF_CTX *SSL_CONF_CTX_new(void)
1063
0
{
1064
0
    SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
1065
1066
0
    return ret;
1067
0
}
1068
1069
int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
1070
0
{
1071
    /* See if any certificates are missing private keys */
1072
0
    size_t i;
1073
0
    CERT *c = NULL;
1074
1075
0
    if (cctx->ctx != NULL) {
1076
0
        c = cctx->ctx->cert;
1077
0
    } else if (cctx->ssl != NULL) {
1078
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
1079
1080
0
        if (sc != NULL)
1081
0
            c = sc->cert;
1082
0
    }
1083
0
    if (c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
1084
0
        for (i = 0; i < cctx->num_cert_filename; i++) {
1085
0
            const char *p = cctx->cert_filename[i];
1086
1087
            /*
1088
             * If missing private key try to load one from certificate file
1089
             */
1090
0
            if (p != NULL && c->pkeys[i].privatekey == NULL) {
1091
0
                if (!cmd_PrivateKey(cctx, p))
1092
0
                    return 0;
1093
0
            }
1094
0
        }
1095
0
    }
1096
0
    if (cctx->canames) {
1097
0
        if (cctx->ssl)
1098
0
            SSL_set0_CA_list(cctx->ssl, cctx->canames);
1099
0
        else if (cctx->ctx)
1100
0
            SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
1101
0
        else
1102
0
            sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1103
0
        cctx->canames = NULL;
1104
0
    }
1105
0
    return 1;
1106
0
}
1107
1108
static void free_cert_filename(SSL_CONF_CTX *cctx)
1109
0
{
1110
0
    size_t i;
1111
1112
0
    for (i = 0; i < cctx->num_cert_filename; i++)
1113
0
        OPENSSL_free(cctx->cert_filename[i]);
1114
0
    OPENSSL_free(cctx->cert_filename);
1115
0
    cctx->cert_filename = NULL;
1116
0
    cctx->num_cert_filename = 0;
1117
0
}
1118
1119
void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
1120
0
{
1121
0
    if (cctx) {
1122
0
        free_cert_filename(cctx);
1123
0
        OPENSSL_free(cctx->prefix);
1124
0
        sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1125
0
        OPENSSL_free(cctx);
1126
0
    }
1127
0
}
1128
1129
unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1130
0
{
1131
0
    if ((cctx->flags & SSL_CONF_FLAG_CMDLINE)
1132
0
        && (flags & SSL_CONF_FLAG_FILE))
1133
0
        flags &= ~SSL_CONF_FLAG_FILE;
1134
1135
0
    if ((cctx->flags & SSL_CONF_FLAG_FILE)
1136
0
        && (flags & SSL_CONF_FLAG_CMDLINE))
1137
0
        flags &= ~SSL_CONF_FLAG_CMDLINE;
1138
1139
0
    cctx->flags |= flags;
1140
0
    return cctx->flags;
1141
0
}
1142
1143
unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1144
0
{
1145
0
    cctx->flags &= ~flags;
1146
0
    return cctx->flags;
1147
0
}
1148
1149
int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
1150
0
{
1151
0
    char *tmp = NULL;
1152
0
    if (pre) {
1153
0
        tmp = OPENSSL_strdup(pre);
1154
0
        if (tmp == NULL)
1155
0
            return 0;
1156
0
    }
1157
0
    OPENSSL_free(cctx->prefix);
1158
0
    cctx->prefix = tmp;
1159
0
    if (tmp)
1160
0
        cctx->prefixlen = strlen(tmp);
1161
0
    else
1162
0
        cctx->prefixlen = 0;
1163
0
    return 1;
1164
0
}
1165
1166
void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
1167
0
{
1168
0
    cctx->ssl = ssl;
1169
0
    cctx->ctx = NULL;
1170
0
    free_cert_filename(cctx);
1171
0
    if (ssl != NULL) {
1172
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1173
1174
0
        if (sc == NULL)
1175
0
            return;
1176
0
        cctx->poptions = &sc->options;
1177
0
        cctx->min_version = &sc->min_proto_version;
1178
0
        cctx->max_version = &sc->max_proto_version;
1179
0
        cctx->pcert_flags = &sc->cert->cert_flags;
1180
0
        cctx->pvfy_flags = &sc->verify_mode;
1181
0
        cctx->cert_filename = OPENSSL_zalloc(sc->ssl_pkey_num
1182
0
            * sizeof(*cctx->cert_filename));
1183
0
        if (cctx->cert_filename != NULL)
1184
0
            cctx->num_cert_filename = sc->ssl_pkey_num;
1185
0
    } else {
1186
0
        cctx->poptions = NULL;
1187
0
        cctx->min_version = NULL;
1188
0
        cctx->max_version = NULL;
1189
0
        cctx->pcert_flags = NULL;
1190
0
        cctx->pvfy_flags = NULL;
1191
0
    }
1192
0
}
1193
1194
void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
1195
0
{
1196
0
    cctx->ctx = ctx;
1197
0
    cctx->ssl = NULL;
1198
0
    free_cert_filename(cctx);
1199
0
    if (ctx) {
1200
0
        cctx->poptions = &ctx->options;
1201
0
        cctx->min_version = &ctx->min_proto_version;
1202
0
        cctx->max_version = &ctx->max_proto_version;
1203
0
        cctx->pcert_flags = &ctx->cert->cert_flags;
1204
0
        cctx->pvfy_flags = &ctx->verify_mode;
1205
0
        cctx->cert_filename = OPENSSL_zalloc((SSL_PKEY_NUM + ctx->sigalg_list_len)
1206
0
            * sizeof(*cctx->cert_filename));
1207
0
        if (cctx->cert_filename != NULL)
1208
0
            cctx->num_cert_filename = SSL_PKEY_NUM + ctx->sigalg_list_len;
1209
0
    } else {
1210
0
        cctx->poptions = NULL;
1211
0
        cctx->min_version = NULL;
1212
0
        cctx->max_version = NULL;
1213
0
        cctx->pcert_flags = NULL;
1214
        cctx->pvfy_flags = NULL;
1215
0
    }
1216
0
}