Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/ssl/ssl_conf.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012-2023 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 (strstr(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("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
397
0
        SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
398
0
        SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
399
0
        SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
400
0
        SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES),
401
0
        SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS),
402
0
        SSL_FLAG_TBL_CERT("StrictCertCheck", SSL_CERT_FLAG_TLS_STRICT),
403
0
        SSL_FLAG_TBL_INV("TxCertificateCompression", SSL_OP_NO_TX_CERTIFICATE_COMPRESSION),
404
0
        SSL_FLAG_TBL_INV("RxCertificateCompression", SSL_OP_NO_RX_CERTIFICATE_COMPRESSION),
405
0
        SSL_FLAG_TBL("KTLSTxZerocopySendfile", SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE),
406
0
        SSL_FLAG_TBL("IgnoreUnexpectedEOF", SSL_OP_IGNORE_UNEXPECTED_EOF),
407
0
    };
408
0
    if (value == NULL)
409
0
        return -3;
410
0
    cctx->tbl = ssl_option_list;
411
0
    cctx->ntbl = OSSL_NELEM(ssl_option_list);
412
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
413
0
}
414
415
static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
416
0
{
417
0
    static const ssl_flag_tbl ssl_vfy_list[] = {
418
0
        SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
419
0
        SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
420
0
        SSL_FLAG_VFY_SRV("Require",
421
0
                         SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
422
0
        SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
423
0
        SSL_FLAG_VFY_SRV("RequestPostHandshake",
424
0
                         SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
425
0
        SSL_FLAG_VFY_SRV("RequirePostHandshake",
426
0
                         SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
427
0
                         SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
428
0
    };
429
0
    if (value == NULL)
430
0
        return -3;
431
0
    cctx->tbl = ssl_vfy_list;
432
0
    cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
433
0
    return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
434
0
}
435
436
static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
437
0
{
438
0
    int rv = 1;
439
0
    CERT *c = NULL;
440
0
    if (cctx->ctx != NULL) {
441
0
        rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
442
0
        c = cctx->ctx->cert;
443
0
    }
444
0
    if (cctx->ssl != NULL) {
445
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
446
447
0
        if (sc != NULL) {
448
0
            rv = SSL_use_certificate_chain_file(cctx->ssl, value);
449
0
            c = sc->cert;
450
0
        } else {
451
0
            rv = 0;
452
0
        }
453
0
    }
454
0
    if (rv > 0 && c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
455
0
        size_t fileidx = c->key - c->pkeys;
456
457
0
        if (fileidx >= cctx->num_cert_filename) {
458
0
            rv = 0;
459
0
        } else {
460
0
            char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
461
462
0
            OPENSSL_free(*pfilename);
463
0
            *pfilename = OPENSSL_strdup(value);
464
0
            if (*pfilename == NULL)
465
0
                rv = 0;
466
0
        }
467
0
    }
468
469
0
    return rv > 0;
470
0
}
471
472
static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
473
0
{
474
0
    int rv = 1;
475
0
    if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
476
0
        return -2;
477
0
    if (cctx->ctx)
478
0
        rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
479
0
    if (cctx->ssl)
480
0
        rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
481
0
    return rv > 0;
482
0
}
483
484
static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
485
0
{
486
0
    int rv = 1;
487
0
    if (cctx->ctx)
488
0
        rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
489
0
    return rv > 0;
490
0
}
491
492
static int do_store(SSL_CONF_CTX *cctx,
493
                    const char *CAfile, const char *CApath, const char *CAstore,
494
                    int verify_store)
495
0
{
496
0
    CERT *cert;
497
0
    X509_STORE **st;
498
0
    SSL_CTX *ctx;
499
0
    OSSL_LIB_CTX *libctx = NULL;
500
0
    const char *propq = NULL;
501
502
0
    if (cctx->ctx != NULL) {
503
0
        cert = cctx->ctx->cert;
504
0
        ctx = cctx->ctx;
505
0
    } else if (cctx->ssl != NULL) {
506
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
507
508
0
        if (sc == NULL)
509
0
            return 0;
510
511
0
        cert = sc->cert;
512
0
        ctx = cctx->ssl->ctx;
513
0
    } else {
514
0
        return 1;
515
0
    }
516
0
    if (ctx != NULL) {
517
0
        libctx = ctx->libctx;
518
0
        propq = ctx->propq;
519
0
    }
520
0
    st = verify_store ? &cert->verify_store : &cert->chain_store;
521
0
    if (*st == NULL) {
522
0
        *st = X509_STORE_new();
523
0
        if (*st == NULL)
524
0
            return 0;
525
0
    }
526
527
0
    if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
528
0
        return 0;
529
0
    if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
530
0
        return 0;
531
0
    if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx,
532
0
                                                     propq))
533
0
        return 0;
534
0
    return 1;
535
0
}
536
537
static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
538
0
{
539
0
    return do_store(cctx, NULL, value, NULL, 0);
540
0
}
541
542
static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
543
0
{
544
0
    return do_store(cctx, value, NULL, NULL, 0);
545
0
}
546
547
static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value)
548
0
{
549
0
    return do_store(cctx, NULL, NULL, value, 0);
550
0
}
551
552
static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
553
0
{
554
0
    return do_store(cctx, NULL, value, NULL, 1);
555
0
}
556
557
static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
558
0
{
559
0
    return do_store(cctx, value, NULL, NULL, 1);
560
0
}
561
562
static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value)
563
0
{
564
0
    return do_store(cctx, NULL, NULL, value, 1);
565
0
}
566
567
static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
568
0
{
569
0
    if (cctx->canames == NULL)
570
0
        cctx->canames = sk_X509_NAME_new_null();
571
0
    if (cctx->canames == NULL)
572
0
        return 0;
573
0
    return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
574
0
}
575
576
static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
577
0
{
578
0
    return cmd_RequestCAFile(cctx, value);
579
0
}
580
581
static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
582
0
{
583
0
    if (cctx->canames == NULL)
584
0
        cctx->canames = sk_X509_NAME_new_null();
585
0
    if (cctx->canames == NULL)
586
0
        return 0;
587
0
    return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
588
0
}
589
590
static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
591
0
{
592
0
    return cmd_RequestCAPath(cctx, value);
593
0
}
594
595
static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value)
596
0
{
597
0
    if (cctx->canames == NULL)
598
0
        cctx->canames = sk_X509_NAME_new_null();
599
0
    if (cctx->canames == NULL)
600
0
        return 0;
601
0
    return SSL_add_store_cert_subjects_to_stack(cctx->canames, value);
602
0
}
603
604
static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value)
605
0
{
606
0
    return cmd_RequestCAStore(cctx, value);
607
0
}
608
609
static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
610
0
{
611
0
    int rv = 0;
612
0
    EVP_PKEY *dhpkey = NULL;
613
0
    BIO *in = NULL;
614
0
    SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx;
615
0
    OSSL_DECODER_CTX *decoderctx = NULL;
616
617
0
    if (cctx->ctx != NULL || cctx->ssl != NULL) {
618
0
        in = BIO_new(BIO_s_file());
619
0
        if (in == NULL)
620
0
            goto end;
621
0
        if (BIO_read_filename(in, value) <= 0)
622
0
            goto end;
623
624
0
        decoderctx
625
0
            = OSSL_DECODER_CTX_new_for_pkey(&dhpkey, "PEM", NULL, "DH",
626
0
                                            OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
627
0
                                            sslctx->libctx, sslctx->propq);
628
0
        if (decoderctx == NULL)
629
0
            goto end;
630
0
        ERR_set_mark();
631
0
        while (!OSSL_DECODER_from_bio(decoderctx, in)
632
0
               && dhpkey == NULL
633
0
               && !BIO_eof(in));
634
0
        OSSL_DECODER_CTX_free(decoderctx);
635
636
0
        if (dhpkey == NULL) {
637
0
            ERR_clear_last_mark();
638
0
            goto end;
639
0
        }
640
0
        ERR_pop_to_mark();
641
0
    } else {
642
0
        return 1;
643
0
    }
644
645
0
    if (cctx->ctx != NULL) {
646
0
        if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
647
0
            dhpkey = NULL;
648
0
    }
649
0
    if (cctx->ssl != NULL) {
650
0
        if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
651
0
            dhpkey = NULL;
652
0
    }
653
0
 end:
654
0
    EVP_PKEY_free(dhpkey);
655
0
    BIO_free(in);
656
0
    return rv > 0;
657
0
}
658
659
static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
660
0
{
661
0
    int rv = 0;
662
0
    int block_size = atoi(value);
663
664
    /*
665
     * All we care about is a non-negative value,
666
     * the setters check the range
667
     */
668
0
    if (block_size >= 0) {
669
0
        if (cctx->ctx)
670
0
            rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
671
0
        if (cctx->ssl)
672
0
            rv = SSL_set_block_padding(cctx->ssl, block_size);
673
0
    }
674
0
    return rv;
675
0
}
676
677
678
static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
679
0
{
680
0
    int rv = 0;
681
0
    int num_tickets = atoi(value);
682
683
0
    if (num_tickets >= 0) {
684
0
        if (cctx->ctx)
685
0
            rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
686
0
        if (cctx->ssl)
687
0
            rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
688
0
    }
689
0
    return rv;
690
0
}
691
692
typedef struct {
693
    int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
694
    const char *str_file;
695
    const char *str_cmdline;
696
    unsigned short flags;
697
    unsigned short value_type;
698
} ssl_conf_cmd_tbl;
699
700
/* Table of supported parameters */
701
702
#define SSL_CONF_CMD(name, cmdopt, flags, type) \
703
        {cmd_##name, #name, cmdopt, flags, type}
704
705
#define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
706
        SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
707
708
#define SSL_CONF_CMD_SWITCH(name, flags) \
709
        {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
710
711
/* See apps/include/opt.h if you change this table. */
712
/* The SSL_CONF_CMD_SWITCH should be the same order as ssl_cmd_switches */
713
static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
714
    SSL_CONF_CMD_SWITCH("no_ssl3", 0),
715
    SSL_CONF_CMD_SWITCH("no_tls1", 0),
716
    SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
717
    SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
718
    SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
719
    SSL_CONF_CMD_SWITCH("bugs", 0),
720
    SSL_CONF_CMD_SWITCH("no_comp", 0),
721
    SSL_CONF_CMD_SWITCH("comp", 0),
722
    SSL_CONF_CMD_SWITCH("no_tx_cert_comp", 0),
723
    SSL_CONF_CMD_SWITCH("tx_cert_comp", 0),
724
    SSL_CONF_CMD_SWITCH("no_rx_cert_comp", 0),
725
    SSL_CONF_CMD_SWITCH("rx_cert_comp", 0),
726
    SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
727
    SSL_CONF_CMD_SWITCH("no_ticket", 0),
728
    SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
729
    SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
730
    SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER),
731
    SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT),
732
    SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
733
    SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
734
    SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_CLIENT),
735
    SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
736
    SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
737
    SSL_CONF_CMD_SWITCH("strict", 0),
738
    SSL_CONF_CMD_SWITCH("no_middlebox", 0),
739
    SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
740
    SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
741
    SSL_CONF_CMD_SWITCH("no_etm", 0),
742
    SSL_CONF_CMD_SWITCH("no_ems", 0),
743
    SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
744
    SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
745
    SSL_CONF_CMD_STRING(Curves, "curves", 0),
746
    SSL_CONF_CMD_STRING(Groups, "groups", 0),
747
    SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
748
    SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
749
    SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
750
    SSL_CONF_CMD_STRING(Protocol, NULL, 0),
751
    SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
752
    SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
753
    SSL_CONF_CMD_STRING(Options, NULL, 0),
754
    SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
755
    SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
756
                 SSL_CONF_TYPE_FILE),
757
    SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
758
                 SSL_CONF_TYPE_FILE),
759
    SSL_CONF_CMD(ServerInfoFile, NULL,
760
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
761
                 SSL_CONF_TYPE_FILE),
762
    SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
763
                 SSL_CONF_TYPE_DIR),
764
    SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
765
                 SSL_CONF_TYPE_FILE),
766
    SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
767
                 SSL_CONF_TYPE_STORE),
768
    SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
769
                 SSL_CONF_TYPE_DIR),
770
    SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
771
                 SSL_CONF_TYPE_FILE),
772
    SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
773
                 SSL_CONF_TYPE_STORE),
774
    SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
775
                 SSL_CONF_TYPE_FILE),
776
    SSL_CONF_CMD(ClientCAFile, NULL,
777
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
778
                 SSL_CONF_TYPE_FILE),
779
    SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
780
                 SSL_CONF_TYPE_DIR),
781
    SSL_CONF_CMD(ClientCAPath, NULL,
782
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
783
                 SSL_CONF_TYPE_DIR),
784
    SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
785
                 SSL_CONF_TYPE_STORE),
786
    SSL_CONF_CMD(ClientCAStore, NULL,
787
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
788
                 SSL_CONF_TYPE_STORE),
789
    SSL_CONF_CMD(DHParameters, "dhparam",
790
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
791
                 SSL_CONF_TYPE_FILE),
792
    SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
793
    SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
794
};
795
796
/* Supported switches: must match order of switches in ssl_conf_cmds */
797
static const ssl_switch_tbl ssl_cmd_switches[] = {
798
    {SSL_OP_NO_SSLv3, 0},       /* no_ssl3 */
799
    {SSL_OP_NO_TLSv1, 0},       /* no_tls1 */
800
    {SSL_OP_NO_TLSv1_1, 0},     /* no_tls1_1 */
801
    {SSL_OP_NO_TLSv1_2, 0},     /* no_tls1_2 */
802
    {SSL_OP_NO_TLSv1_3, 0},     /* no_tls1_3 */
803
    {SSL_OP_ALL, 0},            /* bugs */
804
    {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
805
    {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
806
    {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, 0}, /* no_tx_cert_comp */
807
    {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* tx_cert_comp */
808
    {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, 0}, /* no_rx_cert_comp */
809
    {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* rx_cert_comp */
810
    {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
811
    {SSL_OP_NO_TICKET, 0},      /* no_ticket */
812
    {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
813
    /* legacy_renegotiation */
814
    {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
815
    /* Allow client renegotiation */
816
    {SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0},
817
    /* legacy_server_connect */
818
    {SSL_OP_LEGACY_SERVER_CONNECT, 0},
819
    /* no_renegotiation */
820
    {SSL_OP_NO_RENEGOTIATION, 0},
821
    /* no_resumption_on_reneg */
822
    {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
823
    /* no_legacy_server_connect */
824
    {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
825
    /* allow_no_dhe_kex */
826
    {SSL_OP_ALLOW_NO_DHE_KEX, 0},
827
    /* chacha reprioritization */
828
    {SSL_OP_PRIORITIZE_CHACHA, 0},
829
    {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
830
    /* no_middlebox */
831
    {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
832
    /* anti_replay */
833
    {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
834
    /* no_anti_replay */
835
    {SSL_OP_NO_ANTI_REPLAY, 0},
836
    /* no Encrypt-then-Mac */
837
    {SSL_OP_NO_ENCRYPT_THEN_MAC, 0},
838
    /* no Extended master secret */
839
    {SSL_OP_NO_EXTENDED_MASTER_SECRET, 0},
840
};
841
842
static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
843
0
{
844
0
    if (pcmd == NULL || *pcmd == NULL)
845
0
        return 0;
846
    /* If a prefix is set, check and skip */
847
0
    if (cctx->prefix) {
848
0
        if (strlen(*pcmd) <= cctx->prefixlen)
849
0
            return 0;
850
0
        if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
851
0
            strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
852
0
            return 0;
853
0
        if (cctx->flags & SSL_CONF_FLAG_FILE &&
854
0
            OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
855
0
            return 0;
856
0
        *pcmd += cctx->prefixlen;
857
0
    } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
858
0
        if (**pcmd != '-' || !(*pcmd)[1])
859
0
            return 0;
860
0
        *pcmd += 1;
861
0
    }
862
0
    return 1;
863
0
}
864
865
/* Determine if a command is allowed according to cctx flags */
866
static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *t)
867
0
{
868
0
    unsigned int tfl = t->flags;
869
0
    unsigned int cfl = cctx->flags;
870
0
    if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
871
0
        return 0;
872
0
    if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
873
0
        return 0;
874
0
    if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
875
0
        && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
876
0
        return 0;
877
0
    return 1;
878
0
}
879
880
static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
881
                                                   const char *cmd)
882
0
{
883
0
    const ssl_conf_cmd_tbl *t;
884
0
    size_t i;
885
0
    if (cmd == NULL)
886
0
        return NULL;
887
888
    /* Look for matching parameter name in table */
889
0
    for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
890
0
        if (ssl_conf_cmd_allowed(cctx, t)) {
891
0
            if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
892
0
                if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
893
0
                    return t;
894
0
            }
895
0
            if (cctx->flags & SSL_CONF_FLAG_FILE) {
896
0
                if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
897
0
                    return t;
898
0
            }
899
0
        }
900
0
    }
901
0
    return NULL;
902
0
}
903
904
static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *cmd)
905
0
{
906
    /* Find index of command in table */
907
0
    size_t idx = cmd - ssl_conf_cmds;
908
0
    const ssl_switch_tbl *scmd;
909
910
    /* Sanity check index */
911
0
    if (idx >= OSSL_NELEM(ssl_cmd_switches)) {
912
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
913
0
        return 0;
914
0
    }
915
    /* Obtain switches entry with same index */
916
0
    scmd = ssl_cmd_switches + idx;
917
0
    ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
918
0
    return 1;
919
0
}
920
921
int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
922
0
{
923
0
    const ssl_conf_cmd_tbl *runcmd;
924
0
    if (cmd == NULL) {
925
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
926
0
        return 0;
927
0
    }
928
929
0
    if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
930
0
        goto unknown_cmd;
931
932
0
    runcmd = ssl_conf_cmd_lookup(cctx, cmd);
933
934
0
    if (runcmd) {
935
0
        int rv = -3;
936
937
0
        if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
938
0
            return ctrl_switch_option(cctx, runcmd);
939
0
        }
940
0
        if (value == NULL)
941
0
            goto bad_value;
942
0
        rv = runcmd->cmd(cctx, value);
943
0
        if (rv > 0)
944
0
            return 2;
945
0
        if (rv != -2)
946
0
            rv = 0;
947
948
0
 bad_value:
949
0
        if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
950
0
            ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
951
0
                           "cmd=%s, value=%s", cmd,
952
0
                           value != NULL ? value : "<EMPTY>");
953
0
        return rv;
954
0
    }
955
956
0
 unknown_cmd:
957
0
    if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
958
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
959
960
0
    return -2;
961
0
}
962
963
int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
964
0
{
965
0
    int rv;
966
0
    const char *arg = NULL, *argn;
967
968
0
    if (pargc != NULL && *pargc == 0)
969
0
        return 0;
970
0
    if (pargc == NULL || *pargc > 0)
971
0
        arg = **pargv;
972
0
    if (arg == NULL)
973
0
        return 0;
974
0
    if (pargc == NULL || *pargc > 1)
975
0
        argn = (*pargv)[1];
976
0
    else
977
0
        argn = NULL;
978
0
    cctx->flags &= ~SSL_CONF_FLAG_FILE;
979
0
    cctx->flags |= SSL_CONF_FLAG_CMDLINE;
980
0
    rv = SSL_CONF_cmd(cctx, arg, argn);
981
0
    if (rv > 0) {
982
        /* Success: update pargc, pargv */
983
0
        (*pargv) += rv;
984
0
        if (pargc)
985
0
            (*pargc) -= rv;
986
0
        return rv;
987
0
    }
988
    /* Unknown switch: indicate no arguments processed */
989
0
    if (rv == -2)
990
0
        return 0;
991
    /* Some error occurred processing command, return fatal error */
992
0
    if (rv == 0)
993
0
        return -1;
994
0
    return rv;
995
0
}
996
997
int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
998
0
{
999
0
    if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
1000
0
        const ssl_conf_cmd_tbl *runcmd;
1001
0
        runcmd = ssl_conf_cmd_lookup(cctx, cmd);
1002
0
        if (runcmd)
1003
0
            return runcmd->value_type;
1004
0
    }
1005
0
    return SSL_CONF_TYPE_UNKNOWN;
1006
0
}
1007
1008
SSL_CONF_CTX *SSL_CONF_CTX_new(void)
1009
0
{
1010
0
    SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
1011
1012
0
    return ret;
1013
0
}
1014
1015
int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
1016
0
{
1017
    /* See if any certificates are missing private keys */
1018
0
    size_t i;
1019
0
    CERT *c = NULL;
1020
1021
0
    if (cctx->ctx != NULL) {
1022
0
        c = cctx->ctx->cert;
1023
0
    } else if (cctx->ssl != NULL) {
1024
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
1025
1026
0
        if (sc != NULL)
1027
0
            c = sc->cert;
1028
0
    }
1029
0
    if (c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
1030
0
        for (i = 0; i < cctx->num_cert_filename; i++) {
1031
0
            const char *p = cctx->cert_filename[i];
1032
1033
            /*
1034
             * If missing private key try to load one from certificate file
1035
             */
1036
0
            if (p != NULL && c->pkeys[i].privatekey == NULL) {
1037
0
                if (!cmd_PrivateKey(cctx, p))
1038
0
                    return 0;
1039
0
            }
1040
0
        }
1041
0
    }
1042
0
    if (cctx->canames) {
1043
0
        if (cctx->ssl)
1044
0
            SSL_set0_CA_list(cctx->ssl, cctx->canames);
1045
0
        else if (cctx->ctx)
1046
0
            SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
1047
0
        else
1048
0
            sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1049
0
        cctx->canames = NULL;
1050
0
    }
1051
0
    return 1;
1052
0
}
1053
1054
static void free_cert_filename(SSL_CONF_CTX *cctx)
1055
0
{
1056
0
    size_t i;
1057
1058
0
    for (i = 0; i < cctx->num_cert_filename; i++)
1059
0
        OPENSSL_free(cctx->cert_filename[i]);
1060
0
    OPENSSL_free(cctx->cert_filename);
1061
0
    cctx->cert_filename = NULL;
1062
0
    cctx->num_cert_filename = 0;
1063
0
}
1064
1065
void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
1066
54.8k
{
1067
54.8k
    if (cctx) {
1068
0
        free_cert_filename(cctx);
1069
0
        OPENSSL_free(cctx->prefix);
1070
0
        sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1071
0
        OPENSSL_free(cctx);
1072
0
    }
1073
54.8k
}
1074
1075
unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1076
0
{
1077
0
    cctx->flags |= flags;
1078
0
    return cctx->flags;
1079
0
}
1080
1081
unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1082
0
{
1083
0
    cctx->flags &= ~flags;
1084
0
    return cctx->flags;
1085
0
}
1086
1087
int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
1088
0
{
1089
0
    char *tmp = NULL;
1090
0
    if (pre) {
1091
0
        tmp = OPENSSL_strdup(pre);
1092
0
        if (tmp == NULL)
1093
0
            return 0;
1094
0
    }
1095
0
    OPENSSL_free(cctx->prefix);
1096
0
    cctx->prefix = tmp;
1097
0
    if (tmp)
1098
0
        cctx->prefixlen = strlen(tmp);
1099
0
    else
1100
0
        cctx->prefixlen = 0;
1101
0
    return 1;
1102
0
}
1103
1104
void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
1105
0
{
1106
0
    cctx->ssl = ssl;
1107
0
    cctx->ctx = NULL;
1108
0
    free_cert_filename(cctx);
1109
0
    if (ssl != NULL) {
1110
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1111
1112
0
        if (sc == NULL)
1113
0
            return;
1114
0
        cctx->poptions = &sc->options;
1115
0
        cctx->min_version = &sc->min_proto_version;
1116
0
        cctx->max_version = &sc->max_proto_version;
1117
0
        cctx->pcert_flags = &sc->cert->cert_flags;
1118
0
        cctx->pvfy_flags = &sc->verify_mode;
1119
0
        cctx->cert_filename = OPENSSL_zalloc(sc->ssl_pkey_num
1120
0
                                             * sizeof(*cctx->cert_filename));
1121
0
        if (cctx->cert_filename != NULL)
1122
0
            cctx->num_cert_filename = sc->ssl_pkey_num;
1123
0
    } else {
1124
0
        cctx->poptions = NULL;
1125
0
        cctx->min_version = NULL;
1126
0
        cctx->max_version = NULL;
1127
0
        cctx->pcert_flags = NULL;
1128
0
        cctx->pvfy_flags = NULL;
1129
0
    }
1130
0
}
1131
1132
void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
1133
0
{
1134
0
    cctx->ctx = ctx;
1135
0
    cctx->ssl = NULL;
1136
0
    free_cert_filename(cctx);
1137
0
    if (ctx) {
1138
0
        cctx->poptions = &ctx->options;
1139
0
        cctx->min_version = &ctx->min_proto_version;
1140
0
        cctx->max_version = &ctx->max_proto_version;
1141
0
        cctx->pcert_flags = &ctx->cert->cert_flags;
1142
0
        cctx->pvfy_flags = &ctx->verify_mode;
1143
0
        cctx->cert_filename = OPENSSL_zalloc((SSL_PKEY_NUM + ctx->sigalg_list_len)
1144
0
                                             * sizeof(*cctx->cert_filename));
1145
0
        if (cctx->cert_filename != NULL)
1146
0
            cctx->num_cert_filename = SSL_PKEY_NUM + ctx->sigalg_list_len;
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
}