Coverage Report

Created: 2025-08-28 07:07

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