Coverage Report

Created: 2025-12-31 06:58

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