Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/ssl_conf.c
Line
Count
Source
1
/*
2
 * Copyright 2012-2025 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 <stdio.h>
13
#include "ssl_local.h"
14
#include <openssl/conf.h>
15
#include <openssl/objects.h>
16
#include <openssl/decoder.h>
17
#include <openssl/core_dispatch.h>
18
#include "internal/nelem.h"
19
#include "internal/ssl_unwrap.h"
20
21
/*
22
 * structure holding name tables. This is used for permitted elements in lists
23
 * such as TLSv1.
24
 */
25
26
typedef struct {
27
    const char *name;
28
    int namelen;
29
    unsigned int name_flags;
30
    uint64_t option_value;
31
} ssl_flag_tbl;
32
33
/* Switch table: use for single command line switches like no_tls2 */
34
typedef struct {
35
    uint64_t option_value;
36
    unsigned int name_flags;
37
} ssl_switch_tbl;
38
39
/* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
40
0
#define SSL_TFLAG_INV   0x1
41
/* Mask for type of flag referred to */
42
0
#define SSL_TFLAG_TYPE_MASK 0xf00
43
/* Flag is for options */
44
0
#define SSL_TFLAG_OPTION    0x000
45
/* Flag is for cert_flags */
46
0
#define SSL_TFLAG_CERT      0x100
47
/* Flag is for verify mode */
48
0
#define SSL_TFLAG_VFY       0x200
49
/* Option can only be used for clients */
50
0
#define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
51
/* Option can only be used for servers */
52
0
#define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
53
0
#define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
54
55
#define SSL_FLAG_TBL(str, flag) \
56
0
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
57
#define SSL_FLAG_TBL_SRV(str, flag) \
58
0
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
59
#define SSL_FLAG_TBL_CLI(str, flag) \
60
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
61
#define SSL_FLAG_TBL_INV(str, flag) \
62
0
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
63
#define SSL_FLAG_TBL_SRV_INV(str, flag) \
64
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
65
#define SSL_FLAG_TBL_CERT(str, flag) \
66
0
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
67
68
#define SSL_FLAG_VFY_CLI(str, flag) \
69
0
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
70
#define SSL_FLAG_VFY_SRV(str, flag) \
71
0
        {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
72
73
/*
74
 * Opaque structure containing SSL configuration context.
75
 */
76
77
struct ssl_conf_ctx_st {
78
    /*
79
     * Various flags indicating (among other things) which options we will
80
     * recognise.
81
     */
82
    unsigned int flags;
83
    /* Prefix and length of commands */
84
    char *prefix;
85
    size_t prefixlen;
86
    /* SSL_CTX or SSL structure to perform operations on */
87
    SSL_CTX *ctx;
88
    SSL *ssl;
89
    /* Pointer to SSL or SSL_CTX options field or NULL if none */
90
    uint64_t *poptions;
91
    /* Certificate filenames for each type */
92
    char **cert_filename;
93
    /* Number of elements in the cert_filename array */
94
    size_t num_cert_filename;
95
    /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
96
    uint32_t *pcert_flags;
97
    /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
98
    uint32_t *pvfy_flags;
99
    /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
100
    int *min_version;
101
    /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
102
    int *max_version;
103
    /* Current flag table being worked on */
104
    const ssl_flag_tbl *tbl;
105
    /* Size of table */
106
    size_t ntbl;
107
    /* Client CA names */
108
    STACK_OF(X509_NAME) *canames;
109
};
110
111
static void ssl_set_option(SSL_CONF_CTX *cctx, unsigned int name_flags,
112
                           uint64_t option_value, int onoff)
113
0
{
114
0
    uint32_t *pflags;
115
116
0
    if (cctx->poptions == NULL)
117
0
        return;
118
0
    if (name_flags & SSL_TFLAG_INV)
119
0
        onoff ^= 1;
120
0
    switch (name_flags & SSL_TFLAG_TYPE_MASK) {
121
122
0
    case SSL_TFLAG_CERT:
123
0
        pflags = cctx->pcert_flags;
124
0
        break;
125
126
0
    case SSL_TFLAG_VFY:
127
0
        pflags = cctx->pvfy_flags;
128
0
        break;
129
130
0
    case SSL_TFLAG_OPTION:
131
0
        if (onoff)
132
0
            *cctx->poptions |= option_value;
133
0
        else
134
0
            *cctx->poptions &= ~option_value;
135
0
        return;
136
137
0
    default:
138
0
        return;
139
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) &&
245
0
        strcmp(value, "auto") == 0)
246
0
        return 1;
247
248
    /* ECDHParameters accepts a single group name */
249
0
    if (strchr(value, ':') != NULL)
250
0
        return 0;
251
252
0
    if (cctx->ctx)
253
0
        rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
254
0
    else if (cctx->ssl)
255
0
        rv = SSL_set1_groups_list(cctx->ssl, value);
256
257
0
    return rv > 0;
258
0
}
259
260
static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
261
0
{
262
0
    int rv = 1;
263
264
0
    if (cctx->ctx)
265
0
        rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
266
0
    if (cctx->ssl)
267
0
        rv = SSL_set_cipher_list(cctx->ssl, value);
268
0
    return rv > 0;
269
0
}
270
271
static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
272
0
{
273
0
    int rv = 1;
274
275
0
    if (cctx->ctx)
276
0
        rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
277
0
    if (cctx->ssl)
278
0
        rv = SSL_set_ciphersuites(cctx->ssl, value);
279
0
    return rv > 0;
280
0
}
281
282
static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
283
0
{
284
0
    static const ssl_flag_tbl ssl_protocol_list[] = {
285
0
        SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
286
0
        SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
287
0
        SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
288
0
        SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
289
0
        SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
290
0
        SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
291
0
        SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
292
0
        SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
293
0
        SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
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
    };
328
0
    size_t i;
329
0
    size_t n = OSSL_NELEM(versions);
330
331
0
    for (i = 0; i < n; i++)
332
0
        if (strcmp(versions[i].name, value) == 0)
333
0
            return versions[i].version;
334
0
    return -1;
335
0
}
336
337
static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
338
0
{
339
0
    int method_version;
340
0
    int new_version;
341
342
0
    if (cctx->ctx != NULL)
343
0
        method_version = cctx->ctx->method->version;
344
0
    else if (cctx->ssl != NULL)
345
0
        method_version = cctx->ssl->defltmeth->version;
346
0
    else
347
0
        return 0;
348
0
    if ((new_version = protocol_from_string(value)) < 0)
349
0
        return 0;
350
0
    return ssl_set_version_bound(method_version, new_version, bound);
351
0
}
352
353
/*
354
 * cmd_MinProtocol - Set min protocol version
355
 * @cctx: config structure to save settings in
356
 * @value: The min protocol version in string form
357
 *
358
 * Returns 1 on success and 0 on failure.
359
 */
360
static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
361
0
{
362
0
    return min_max_proto(cctx, value, cctx->min_version);
363
0
}
364
365
/*
366
 * cmd_MaxProtocol - Set max protocol version
367
 * @cctx: config structure to save settings in
368
 * @value: The max protocol version in string form
369
 *
370
 * Returns 1 on success and 0 on failure.
371
 */
372
static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
373
0
{
374
0
    return min_max_proto(cctx, value, cctx->max_version);
375
0
}
376
377
static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
378
0
{
379
0
    static const ssl_flag_tbl ssl_option_list[] = {
380
0
        SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
381
0
        SSL_FLAG_TBL_INV("EmptyFragments",
382
0
                         SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
383
0
        SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
384
0
        SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
385
0
        SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
386
0
        SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
387
0
                         SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
388
0
        SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
389
0
        SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
390
0
        SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
391
0
                     SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
392
0
        SSL_FLAG_TBL("UnsafeLegacyServerConnect",
393
0
                     SSL_OP_LEGACY_SERVER_CONNECT),
394
0
        SSL_FLAG_TBL("ClientRenegotiation",
395
0
                     SSL_OP_ALLOW_CLIENT_RENEGOTIATION),
396
0
        SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
397
0
        SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
398
0
        SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
399
0
        SSL_FLAG_TBL("PreferNoDHEKEX", SSL_OP_PREFER_NO_DHE_KEX),
400
0
        SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
401
0
        SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
402
0
        SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
403
0
        SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
404
0
        SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES),
405
0
        SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS),
406
0
        SSL_FLAG_TBL_CERT("StrictCertCheck", SSL_CERT_FLAG_TLS_STRICT),
407
0
        SSL_FLAG_TBL_INV("TxCertificateCompression", SSL_OP_NO_TX_CERTIFICATE_COMPRESSION),
408
0
        SSL_FLAG_TBL_INV("RxCertificateCompression", SSL_OP_NO_RX_CERTIFICATE_COMPRESSION),
409
0
        SSL_FLAG_TBL("KTLSTxZerocopySendfile", SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE),
410
0
        SSL_FLAG_TBL("IgnoreUnexpectedEOF", SSL_OP_IGNORE_UNEXPECTED_EOF),
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 |
431
0
                         SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
432
0
    };
433
0
    if (value == NULL)
434
0
        return -3;
435
0
    cctx->tbl = ssl_vfy_list;
436
0
    cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
437
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
438
0
}
439
440
static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
441
0
{
442
0
    int rv = 1;
443
0
    CERT *c = NULL;
444
0
    if (cctx->ctx != NULL) {
445
0
        rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
446
0
        c = cctx->ctx->cert;
447
0
    }
448
0
    if (cctx->ssl != NULL) {
449
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
450
451
0
        if (sc != NULL) {
452
0
            rv = SSL_use_certificate_chain_file(cctx->ssl, value);
453
0
            c = sc->cert;
454
0
        } else {
455
0
            rv = 0;
456
0
        }
457
0
    }
458
0
    if (rv > 0 && c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
459
0
        size_t fileidx = c->key - c->pkeys;
460
461
0
        if (fileidx >= cctx->num_cert_filename) {
462
0
            rv = 0;
463
0
        } else {
464
0
            char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
465
466
0
            OPENSSL_free(*pfilename);
467
0
            *pfilename = OPENSSL_strdup(value);
468
0
            if (*pfilename == NULL)
469
0
                rv = 0;
470
0
        }
471
0
    }
472
473
0
    return rv > 0;
474
0
}
475
476
static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
477
0
{
478
0
    int rv = 1;
479
0
    if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
480
0
        return -2;
481
0
    if (cctx->ctx)
482
0
        rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
483
0
    if (cctx->ssl)
484
0
        rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
485
0
    return rv > 0;
486
0
}
487
488
static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
489
0
{
490
0
    int rv = 1;
491
0
    if (cctx->ctx)
492
0
        rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
493
0
    return rv > 0;
494
0
}
495
496
static int do_store(SSL_CONF_CTX *cctx,
497
                    const char *CAfile, const char *CApath, const char *CAstore,
498
                    int verify_store)
499
0
{
500
0
    CERT *cert;
501
0
    X509_STORE **st;
502
0
    SSL_CTX *ctx;
503
0
    OSSL_LIB_CTX *libctx = NULL;
504
0
    const char *propq = NULL;
505
506
0
    if (cctx->ctx != NULL) {
507
0
        cert = cctx->ctx->cert;
508
0
        ctx = cctx->ctx;
509
0
    } else if (cctx->ssl != NULL) {
510
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
511
512
0
        if (sc == NULL)
513
0
            return 0;
514
515
0
        cert = sc->cert;
516
0
        ctx = cctx->ssl->ctx;
517
0
    } else {
518
0
        return 1;
519
0
    }
520
0
    if (ctx != NULL) {
521
0
        libctx = ctx->libctx;
522
0
        propq = ctx->propq;
523
0
    }
524
0
    st = verify_store ? &cert->verify_store : &cert->chain_store;
525
0
    if (*st == NULL) {
526
0
        *st = X509_STORE_new();
527
0
        if (*st == NULL)
528
0
            return 0;
529
0
    }
530
531
0
    if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
532
0
        return 0;
533
0
    if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
534
0
        return 0;
535
0
    if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx,
536
0
                                                     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
        OSSL_DECODER_CTX_free(decoderctx);
639
640
0
        if (dhpkey == NULL) {
641
0
            ERR_clear_last_mark();
642
0
            goto end;
643
0
        }
644
0
        ERR_pop_to_mark();
645
0
    } else {
646
0
        return 1;
647
0
    }
648
649
0
    if (cctx->ctx != NULL) {
650
0
        if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
651
0
            dhpkey = NULL;
652
0
    }
653
0
    if (cctx->ssl != NULL) {
654
0
        if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
655
0
            dhpkey = NULL;
656
0
    }
657
0
 end:
658
0
    EVP_PKEY_free(dhpkey);
659
0
    BIO_free(in);
660
0
    return rv > 0;
661
0
}
662
663
/*
664
 * |value| input is "<number[,number]>"
665
 * where the first number is the padding block size for
666
 * application data, and the optional second is the
667
 * padding block size for handshake messages
668
 */
669
static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
670
0
{
671
0
    int rv = 0;
672
0
    unsigned long block_padding = 0, hs_padding = 0;
673
0
    char *commap = NULL, *copy = NULL;
674
0
    char *endptr = NULL;
675
676
0
    copy = OPENSSL_strdup(value);
677
0
    if (copy == NULL)
678
0
        goto out;
679
0
    commap = strstr(copy, ",");
680
0
    if (commap != NULL) {
681
0
        *commap = '\0';
682
0
        if (*(commap + 1) == '\0')
683
0
            goto out;
684
0
        if (!OPENSSL_strtoul(commap + 1, &endptr, 0, &hs_padding))
685
0
            goto out;
686
0
    }
687
0
    if (!OPENSSL_strtoul(copy, &endptr, 0, &block_padding))
688
0
        goto out;
689
0
    if (commap == NULL)
690
0
        hs_padding = block_padding;
691
692
    /*
693
     * All we care about are non-negative values,
694
     * the setters check the range
695
     */
696
0
    if (cctx->ctx) {
697
        /*
698
         * QUIC always pads TLS data at the packet level, and as such, attempting
699
         * to set block padding at the record level fails in calls to SSL_CTX_set_block_padding_ex.
700
         * However, when configuring record padding via config file, we have no idea if we are
701
         * going to create TCP or QUIC based SSL's, so silently ignore this configuration option
702
         * for QUIC.
703
         */
704
0
        if (SSL_CTX_is_quic(cctx->ctx))
705
0
            rv = 1;
706
0
        else
707
0
            rv = SSL_CTX_set_block_padding_ex(cctx->ctx, (size_t)block_padding,
708
0
                                              (size_t)hs_padding);
709
0
    }
710
0
    if (cctx->ssl) {
711
        /*
712
         * As above, ignore this config option for QUIC
713
         */
714
0
        if (SSL_is_quic(cctx->ssl))
715
0
            rv = 1;
716
0
        else
717
0
            rv = SSL_set_block_padding_ex(cctx->ssl, (size_t)block_padding,
718
0
                                          (size_t)hs_padding);
719
0
    }
720
0
out:
721
0
    OPENSSL_free(copy);
722
0
    return rv;
723
0
}
724
725
726
static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
727
0
{
728
0
    int rv = 0;
729
0
    int num_tickets = atoi(value);
730
731
0
    if (num_tickets >= 0) {
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_STRING(SignatureAlgorithms, "sigalgs", 0),
793
    SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
794
    SSL_CONF_CMD_STRING(Curves, "curves", 0),
795
    SSL_CONF_CMD_STRING(Groups, "groups", 0),
796
    SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
797
    SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
798
    SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
799
    SSL_CONF_CMD_STRING(Protocol, NULL, 0),
800
    SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
801
    SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
802
    SSL_CONF_CMD_STRING(Options, NULL, 0),
803
    SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
804
    SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
805
                 SSL_CONF_TYPE_FILE),
806
    SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
807
                 SSL_CONF_TYPE_FILE),
808
    SSL_CONF_CMD(ServerInfoFile, NULL,
809
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
810
                 SSL_CONF_TYPE_FILE),
811
    SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
812
                 SSL_CONF_TYPE_DIR),
813
    SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
814
                 SSL_CONF_TYPE_FILE),
815
    SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
816
                 SSL_CONF_TYPE_STORE),
817
    SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
818
                 SSL_CONF_TYPE_DIR),
819
    SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
820
                 SSL_CONF_TYPE_FILE),
821
    SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
822
                 SSL_CONF_TYPE_STORE),
823
    SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
824
                 SSL_CONF_TYPE_FILE),
825
    SSL_CONF_CMD(ClientCAFile, NULL,
826
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
827
                 SSL_CONF_TYPE_FILE),
828
    SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
829
                 SSL_CONF_TYPE_DIR),
830
    SSL_CONF_CMD(ClientCAPath, NULL,
831
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
832
                 SSL_CONF_TYPE_DIR),
833
    SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
834
                 SSL_CONF_TYPE_STORE),
835
    SSL_CONF_CMD(ClientCAStore, NULL,
836
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
837
                 SSL_CONF_TYPE_STORE),
838
    SSL_CONF_CMD(DHParameters, "dhparam",
839
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
840
                 SSL_CONF_TYPE_FILE),
841
    SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
842
    SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
843
};
844
845
/* Supported switches: must match order of switches in ssl_conf_cmds */
846
static const ssl_switch_tbl ssl_cmd_switches[] = {
847
    {SSL_OP_NO_SSLv3, 0},       /* no_ssl3 */
848
    {SSL_OP_NO_TLSv1, 0},       /* no_tls1 */
849
    {SSL_OP_NO_TLSv1_1, 0},     /* no_tls1_1 */
850
    {SSL_OP_NO_TLSv1_2, 0},     /* no_tls1_2 */
851
    {SSL_OP_NO_TLSv1_3, 0},     /* no_tls1_3 */
852
    {SSL_OP_ALL, 0},            /* bugs */
853
    {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
854
    {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
855
    {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, 0}, /* no_tx_cert_comp */
856
    {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* tx_cert_comp */
857
    {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, 0}, /* no_rx_cert_comp */
858
    {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* rx_cert_comp */
859
    {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
860
    {SSL_OP_NO_TICKET, 0},      /* no_ticket */
861
    {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
862
    /* legacy_renegotiation */
863
    {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
864
    /* Allow client renegotiation */
865
    {SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0},
866
    /* legacy_server_connect */
867
    {SSL_OP_LEGACY_SERVER_CONNECT, 0},
868
    /* no_renegotiation */
869
    {SSL_OP_NO_RENEGOTIATION, 0},
870
    /* no_resumption_on_reneg */
871
    {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
872
    /* no_legacy_server_connect */
873
    {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
874
    /* allow_no_dhe_kex */
875
    {SSL_OP_ALLOW_NO_DHE_KEX, 0},
876
    /* prefer_no_dhe_kex */
877
    {SSL_OP_PREFER_NO_DHE_KEX, 0},
878
    /* chacha reprioritization */
879
    {SSL_OP_PRIORITIZE_CHACHA, 0},
880
    {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
881
    /* no_middlebox */
882
    {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
883
    /* anti_replay */
884
    {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
885
    /* no_anti_replay */
886
    {SSL_OP_NO_ANTI_REPLAY, 0},
887
    /* no Encrypt-then-Mac */
888
    {SSL_OP_NO_ENCRYPT_THEN_MAC, 0},
889
    /* no Extended master secret */
890
    {SSL_OP_NO_EXTENDED_MASTER_SECRET, 0},
891
};
892
893
static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
894
0
{
895
0
    if (pcmd == NULL || *pcmd == NULL)
896
0
        return 0;
897
    /* If a prefix is set, check and skip */
898
0
    if (cctx->prefix) {
899
0
        if (strlen(*pcmd) <= cctx->prefixlen)
900
0
            return 0;
901
0
        if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
902
0
            strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
903
0
            return 0;
904
0
        if (cctx->flags & SSL_CONF_FLAG_FILE &&
905
0
            OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
906
0
            return 0;
907
0
        *pcmd += cctx->prefixlen;
908
0
    } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
909
0
        if (**pcmd != '-' || !(*pcmd)[1])
910
0
            return 0;
911
0
        *pcmd += 1;
912
0
    }
913
0
    return 1;
914
0
}
915
916
/* Determine if a command is allowed according to cctx flags */
917
static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *t)
918
0
{
919
0
    unsigned int tfl = t->flags;
920
0
    unsigned int cfl = cctx->flags;
921
0
    if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
922
0
        return 0;
923
0
    if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
924
0
        return 0;
925
0
    if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
926
0
        && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
927
0
        return 0;
928
0
    return 1;
929
0
}
930
931
static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
932
                                                   const char *cmd)
933
0
{
934
0
    const ssl_conf_cmd_tbl *t;
935
0
    size_t i;
936
0
    if (cmd == NULL)
937
0
        return NULL;
938
939
    /* Look for matching parameter name in table */
940
0
    for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
941
0
        if (ssl_conf_cmd_allowed(cctx, t)) {
942
0
            if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
943
0
                if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
944
0
                    return t;
945
0
            }
946
0
            if (cctx->flags & SSL_CONF_FLAG_FILE) {
947
0
                if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
948
0
                    return t;
949
0
            }
950
0
        }
951
0
    }
952
0
    return NULL;
953
0
}
954
955
static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *cmd)
956
0
{
957
    /* Find index of command in table */
958
0
    size_t idx = cmd - ssl_conf_cmds;
959
0
    const ssl_switch_tbl *scmd;
960
961
    /* Sanity check index */
962
0
    if (idx >= OSSL_NELEM(ssl_cmd_switches)) {
963
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
964
0
        return 0;
965
0
    }
966
    /* Obtain switches entry with same index */
967
0
    scmd = ssl_cmd_switches + idx;
968
0
    ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
969
0
    return 1;
970
0
}
971
972
int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
973
0
{
974
0
    const ssl_conf_cmd_tbl *runcmd;
975
0
    if (cmd == NULL) {
976
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
977
0
        return 0;
978
0
    }
979
980
0
    if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
981
0
        goto unknown_cmd;
982
983
0
    runcmd = ssl_conf_cmd_lookup(cctx, cmd);
984
985
0
    if (runcmd) {
986
0
        int rv = -3;
987
988
0
        if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
989
0
            return ctrl_switch_option(cctx, runcmd);
990
0
        }
991
0
        if (value == NULL)
992
0
            goto bad_value;
993
0
        rv = runcmd->cmd(cctx, value);
994
0
        if (rv > 0)
995
0
            return 2;
996
0
        if (rv != -2)
997
0
            rv = 0;
998
999
0
 bad_value:
1000
0
        if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
1001
0
            ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
1002
0
                           "cmd=%s, value=%s", cmd,
1003
0
                           value != NULL ? value : "<EMPTY>");
1004
0
        return rv;
1005
0
    }
1006
1007
0
 unknown_cmd:
1008
0
    if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
1009
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
1010
1011
0
    return -2;
1012
0
}
1013
1014
int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
1015
0
{
1016
0
    int rv;
1017
0
    const char *arg = NULL, *argn;
1018
1019
0
    if (pargc != NULL && *pargc == 0)
1020
0
        return 0;
1021
0
    if (pargc == NULL || *pargc > 0)
1022
0
        arg = **pargv;
1023
0
    if (arg == NULL)
1024
0
        return 0;
1025
0
    if (pargc == NULL || *pargc > 1)
1026
0
        argn = (*pargv)[1];
1027
0
    else
1028
0
        argn = NULL;
1029
0
    cctx->flags &= ~SSL_CONF_FLAG_FILE;
1030
0
    cctx->flags |= SSL_CONF_FLAG_CMDLINE;
1031
0
    rv = SSL_CONF_cmd(cctx, arg, argn);
1032
0
    if (rv > 0) {
1033
        /* Success: update pargc, pargv */
1034
0
        (*pargv) += rv;
1035
0
        if (pargc)
1036
0
            (*pargc) -= rv;
1037
0
        return rv;
1038
0
    }
1039
    /* Unknown switch: indicate no arguments processed */
1040
0
    if (rv == -2)
1041
0
        return 0;
1042
    /* Some error occurred processing command, return fatal error */
1043
0
    if (rv == 0)
1044
0
        return -1;
1045
0
    return rv;
1046
0
}
1047
1048
int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
1049
0
{
1050
0
    if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
1051
0
        const ssl_conf_cmd_tbl *runcmd;
1052
0
        runcmd = ssl_conf_cmd_lookup(cctx, cmd);
1053
0
        if (runcmd)
1054
0
            return runcmd->value_type;
1055
0
    }
1056
0
    return SSL_CONF_TYPE_UNKNOWN;
1057
0
}
1058
1059
SSL_CONF_CTX *SSL_CONF_CTX_new(void)
1060
0
{
1061
0
    SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
1062
1063
0
    return ret;
1064
0
}
1065
1066
int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
1067
0
{
1068
    /* See if any certificates are missing private keys */
1069
0
    size_t i;
1070
0
    CERT *c = NULL;
1071
1072
0
    if (cctx->ctx != NULL) {
1073
0
        c = cctx->ctx->cert;
1074
0
    } else if (cctx->ssl != NULL) {
1075
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
1076
1077
0
        if (sc != NULL)
1078
0
            c = sc->cert;
1079
0
    }
1080
0
    if (c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
1081
0
        for (i = 0; i < cctx->num_cert_filename; i++) {
1082
0
            const char *p = cctx->cert_filename[i];
1083
1084
            /*
1085
             * If missing private key try to load one from certificate file
1086
             */
1087
0
            if (p != NULL && c->pkeys[i].privatekey == NULL) {
1088
0
                if (!cmd_PrivateKey(cctx, p))
1089
0
                    return 0;
1090
0
            }
1091
0
        }
1092
0
    }
1093
0
    if (cctx->canames) {
1094
0
        if (cctx->ssl)
1095
0
            SSL_set0_CA_list(cctx->ssl, cctx->canames);
1096
0
        else if (cctx->ctx)
1097
0
            SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
1098
0
        else
1099
0
            sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1100
0
        cctx->canames = NULL;
1101
0
    }
1102
0
    return 1;
1103
0
}
1104
1105
static void free_cert_filename(SSL_CONF_CTX *cctx)
1106
0
{
1107
0
    size_t i;
1108
1109
0
    for (i = 0; i < cctx->num_cert_filename; i++)
1110
0
        OPENSSL_free(cctx->cert_filename[i]);
1111
0
    OPENSSL_free(cctx->cert_filename);
1112
0
    cctx->cert_filename = NULL;
1113
0
    cctx->num_cert_filename = 0;
1114
0
}
1115
1116
void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
1117
155k
{
1118
155k
    if (cctx) {
1119
0
        free_cert_filename(cctx);
1120
0
        OPENSSL_free(cctx->prefix);
1121
0
        sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1122
0
        OPENSSL_free(cctx);
1123
0
    }
1124
155k
}
1125
1126
unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1127
0
{
1128
0
    cctx->flags |= flags;
1129
0
    return cctx->flags;
1130
0
}
1131
1132
unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1133
0
{
1134
0
    cctx->flags &= ~flags;
1135
0
    return cctx->flags;
1136
0
}
1137
1138
int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
1139
0
{
1140
0
    char *tmp = NULL;
1141
0
    if (pre) {
1142
0
        tmp = OPENSSL_strdup(pre);
1143
0
        if (tmp == NULL)
1144
0
            return 0;
1145
0
    }
1146
0
    OPENSSL_free(cctx->prefix);
1147
0
    cctx->prefix = tmp;
1148
0
    if (tmp)
1149
0
        cctx->prefixlen = strlen(tmp);
1150
0
    else
1151
0
        cctx->prefixlen = 0;
1152
0
    return 1;
1153
0
}
1154
1155
void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
1156
0
{
1157
0
    cctx->ssl = ssl;
1158
0
    cctx->ctx = NULL;
1159
0
    free_cert_filename(cctx);
1160
0
    if (ssl != NULL) {
1161
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1162
1163
0
        if (sc == NULL)
1164
0
            return;
1165
0
        cctx->poptions = &sc->options;
1166
0
        cctx->min_version = &sc->min_proto_version;
1167
0
        cctx->max_version = &sc->max_proto_version;
1168
0
        cctx->pcert_flags = &sc->cert->cert_flags;
1169
0
        cctx->pvfy_flags = &sc->verify_mode;
1170
0
        cctx->cert_filename = OPENSSL_zalloc(sc->ssl_pkey_num
1171
0
                                             * sizeof(*cctx->cert_filename));
1172
0
        if (cctx->cert_filename != NULL)
1173
0
            cctx->num_cert_filename = sc->ssl_pkey_num;
1174
0
    } else {
1175
0
        cctx->poptions = NULL;
1176
0
        cctx->min_version = NULL;
1177
0
        cctx->max_version = NULL;
1178
0
        cctx->pcert_flags = NULL;
1179
0
        cctx->pvfy_flags = NULL;
1180
0
    }
1181
0
}
1182
1183
void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
1184
0
{
1185
0
    cctx->ctx = ctx;
1186
0
    cctx->ssl = NULL;
1187
0
    free_cert_filename(cctx);
1188
0
    if (ctx) {
1189
0
        cctx->poptions = &ctx->options;
1190
0
        cctx->min_version = &ctx->min_proto_version;
1191
0
        cctx->max_version = &ctx->max_proto_version;
1192
0
        cctx->pcert_flags = &ctx->cert->cert_flags;
1193
0
        cctx->pvfy_flags = &ctx->verify_mode;
1194
0
        cctx->cert_filename = OPENSSL_zalloc((SSL_PKEY_NUM + ctx->sigalg_list_len)
1195
0
                                             * sizeof(*cctx->cert_filename));
1196
0
        if (cctx->cert_filename != NULL)
1197
0
            cctx->num_cert_filename = SSL_PKEY_NUM + ctx->sigalg_list_len;
1198
0
    } else {
1199
0
        cctx->poptions = NULL;
1200
0
        cctx->min_version = NULL;
1201
0
        cctx->max_version = NULL;
1202
0
        cctx->pcert_flags = NULL;
1203
        cctx->pvfy_flags = NULL;
1204
0
    }
1205
0
}