Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/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
static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
661
0
{
662
0
    int rv = 0;
663
0
    int block_size = atoi(value);
664
665
    /*
666
     * All we care about is a non-negative value,
667
     * the setters check the range
668
     */
669
0
    if (block_size >= 0) {
670
0
        if (cctx->ctx)
671
0
            rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
672
0
        if (cctx->ssl)
673
0
            rv = SSL_set_block_padding(cctx->ssl, block_size);
674
0
    }
675
0
    return rv;
676
0
}
677
678
679
static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
680
0
{
681
0
    int rv = 0;
682
0
    int num_tickets = atoi(value);
683
684
0
    if (num_tickets >= 0) {
685
0
        if (cctx->ctx)
686
0
            rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
687
0
        if (cctx->ssl)
688
0
            rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
689
0
    }
690
0
    return rv;
691
0
}
692
693
typedef struct {
694
    int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
695
    const char *str_file;
696
    const char *str_cmdline;
697
    unsigned short flags;
698
    unsigned short value_type;
699
} ssl_conf_cmd_tbl;
700
701
/* Table of supported parameters */
702
703
#define SSL_CONF_CMD(name, cmdopt, flags, type) \
704
        {cmd_##name, #name, cmdopt, flags, type}
705
706
#define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
707
        SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
708
709
#define SSL_CONF_CMD_SWITCH(name, flags) \
710
        {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
711
712
/* See apps/include/opt.h if you change this table. */
713
/* The SSL_CONF_CMD_SWITCH should be the same order as ssl_cmd_switches */
714
static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
715
    SSL_CONF_CMD_SWITCH("no_ssl3", 0),
716
    SSL_CONF_CMD_SWITCH("no_tls1", 0),
717
    SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
718
    SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
719
    SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
720
    SSL_CONF_CMD_SWITCH("bugs", 0),
721
    SSL_CONF_CMD_SWITCH("no_comp", 0),
722
    SSL_CONF_CMD_SWITCH("comp", 0),
723
    SSL_CONF_CMD_SWITCH("no_tx_cert_comp", 0),
724
    SSL_CONF_CMD_SWITCH("tx_cert_comp", 0),
725
    SSL_CONF_CMD_SWITCH("no_rx_cert_comp", 0),
726
    SSL_CONF_CMD_SWITCH("rx_cert_comp", 0),
727
    SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
728
    SSL_CONF_CMD_SWITCH("no_ticket", 0),
729
    SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
730
    SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
731
    SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER),
732
    SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT),
733
    SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
734
    SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
735
    SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_CLIENT),
736
    SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
737
    SSL_CONF_CMD_SWITCH("prefer_no_dhe_kex", 0),
738
    SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
739
    SSL_CONF_CMD_SWITCH("strict", 0),
740
    SSL_CONF_CMD_SWITCH("no_middlebox", 0),
741
    SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
742
    SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
743
    SSL_CONF_CMD_SWITCH("no_etm", 0),
744
    SSL_CONF_CMD_SWITCH("no_ems", 0),
745
    SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
746
    SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
747
    SSL_CONF_CMD_STRING(Curves, "curves", 0),
748
    SSL_CONF_CMD_STRING(Groups, "groups", 0),
749
    SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
750
    SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
751
    SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
752
    SSL_CONF_CMD_STRING(Protocol, NULL, 0),
753
    SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
754
    SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
755
    SSL_CONF_CMD_STRING(Options, NULL, 0),
756
    SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
757
    SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
758
                 SSL_CONF_TYPE_FILE),
759
    SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
760
                 SSL_CONF_TYPE_FILE),
761
    SSL_CONF_CMD(ServerInfoFile, NULL,
762
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
763
                 SSL_CONF_TYPE_FILE),
764
    SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
765
                 SSL_CONF_TYPE_DIR),
766
    SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
767
                 SSL_CONF_TYPE_FILE),
768
    SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
769
                 SSL_CONF_TYPE_STORE),
770
    SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
771
                 SSL_CONF_TYPE_DIR),
772
    SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
773
                 SSL_CONF_TYPE_FILE),
774
    SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
775
                 SSL_CONF_TYPE_STORE),
776
    SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
777
                 SSL_CONF_TYPE_FILE),
778
    SSL_CONF_CMD(ClientCAFile, NULL,
779
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
780
                 SSL_CONF_TYPE_FILE),
781
    SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
782
                 SSL_CONF_TYPE_DIR),
783
    SSL_CONF_CMD(ClientCAPath, NULL,
784
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
785
                 SSL_CONF_TYPE_DIR),
786
    SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
787
                 SSL_CONF_TYPE_STORE),
788
    SSL_CONF_CMD(ClientCAStore, NULL,
789
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
790
                 SSL_CONF_TYPE_STORE),
791
    SSL_CONF_CMD(DHParameters, "dhparam",
792
                 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
793
                 SSL_CONF_TYPE_FILE),
794
    SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
795
    SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
796
};
797
798
/* Supported switches: must match order of switches in ssl_conf_cmds */
799
static const ssl_switch_tbl ssl_cmd_switches[] = {
800
    {SSL_OP_NO_SSLv3, 0},       /* no_ssl3 */
801
    {SSL_OP_NO_TLSv1, 0},       /* no_tls1 */
802
    {SSL_OP_NO_TLSv1_1, 0},     /* no_tls1_1 */
803
    {SSL_OP_NO_TLSv1_2, 0},     /* no_tls1_2 */
804
    {SSL_OP_NO_TLSv1_3, 0},     /* no_tls1_3 */
805
    {SSL_OP_ALL, 0},            /* bugs */
806
    {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
807
    {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
808
    {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, 0}, /* no_tx_cert_comp */
809
    {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* tx_cert_comp */
810
    {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, 0}, /* no_rx_cert_comp */
811
    {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* rx_cert_comp */
812
    {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
813
    {SSL_OP_NO_TICKET, 0},      /* no_ticket */
814
    {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
815
    /* legacy_renegotiation */
816
    {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
817
    /* Allow client renegotiation */
818
    {SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0},
819
    /* legacy_server_connect */
820
    {SSL_OP_LEGACY_SERVER_CONNECT, 0},
821
    /* no_renegotiation */
822
    {SSL_OP_NO_RENEGOTIATION, 0},
823
    /* no_resumption_on_reneg */
824
    {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
825
    /* no_legacy_server_connect */
826
    {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
827
    /* allow_no_dhe_kex */
828
    {SSL_OP_ALLOW_NO_DHE_KEX, 0},
829
    /* prefer_no_dhe_kex */
830
    {SSL_OP_PREFER_NO_DHE_KEX, 0},
831
    /* chacha reprioritization */
832
    {SSL_OP_PRIORITIZE_CHACHA, 0},
833
    {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
834
    /* no_middlebox */
835
    {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
836
    /* anti_replay */
837
    {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
838
    /* no_anti_replay */
839
    {SSL_OP_NO_ANTI_REPLAY, 0},
840
    /* no Encrypt-then-Mac */
841
    {SSL_OP_NO_ENCRYPT_THEN_MAC, 0},
842
    /* no Extended master secret */
843
    {SSL_OP_NO_EXTENDED_MASTER_SECRET, 0},
844
};
845
846
static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
847
0
{
848
0
    if (pcmd == NULL || *pcmd == NULL)
849
0
        return 0;
850
    /* If a prefix is set, check and skip */
851
0
    if (cctx->prefix) {
852
0
        if (strlen(*pcmd) <= cctx->prefixlen)
853
0
            return 0;
854
0
        if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
855
0
            strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
856
0
            return 0;
857
0
        if (cctx->flags & SSL_CONF_FLAG_FILE &&
858
0
            OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
859
0
            return 0;
860
0
        *pcmd += cctx->prefixlen;
861
0
    } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
862
0
        if (**pcmd != '-' || !(*pcmd)[1])
863
0
            return 0;
864
0
        *pcmd += 1;
865
0
    }
866
0
    return 1;
867
0
}
868
869
/* Determine if a command is allowed according to cctx flags */
870
static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *t)
871
0
{
872
0
    unsigned int tfl = t->flags;
873
0
    unsigned int cfl = cctx->flags;
874
0
    if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
875
0
        return 0;
876
0
    if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
877
0
        return 0;
878
0
    if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
879
0
        && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
880
0
        return 0;
881
0
    return 1;
882
0
}
883
884
static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
885
                                                   const char *cmd)
886
0
{
887
0
    const ssl_conf_cmd_tbl *t;
888
0
    size_t i;
889
0
    if (cmd == NULL)
890
0
        return NULL;
891
892
    /* Look for matching parameter name in table */
893
0
    for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
894
0
        if (ssl_conf_cmd_allowed(cctx, t)) {
895
0
            if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
896
0
                if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
897
0
                    return t;
898
0
            }
899
0
            if (cctx->flags & SSL_CONF_FLAG_FILE) {
900
0
                if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
901
0
                    return t;
902
0
            }
903
0
        }
904
0
    }
905
0
    return NULL;
906
0
}
907
908
static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *cmd)
909
0
{
910
    /* Find index of command in table */
911
0
    size_t idx = cmd - ssl_conf_cmds;
912
0
    const ssl_switch_tbl *scmd;
913
914
    /* Sanity check index */
915
0
    if (idx >= OSSL_NELEM(ssl_cmd_switches)) {
916
0
        ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
917
0
        return 0;
918
0
    }
919
    /* Obtain switches entry with same index */
920
0
    scmd = ssl_cmd_switches + idx;
921
0
    ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
922
0
    return 1;
923
0
}
924
925
int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
926
0
{
927
0
    const ssl_conf_cmd_tbl *runcmd;
928
0
    if (cmd == NULL) {
929
0
        ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
930
0
        return 0;
931
0
    }
932
933
0
    if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
934
0
        goto unknown_cmd;
935
936
0
    runcmd = ssl_conf_cmd_lookup(cctx, cmd);
937
938
0
    if (runcmd) {
939
0
        int rv = -3;
940
941
0
        if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
942
0
            return ctrl_switch_option(cctx, runcmd);
943
0
        }
944
0
        if (value == NULL)
945
0
            goto bad_value;
946
0
        rv = runcmd->cmd(cctx, value);
947
0
        if (rv > 0)
948
0
            return 2;
949
0
        if (rv != -2)
950
0
            rv = 0;
951
952
0
 bad_value:
953
0
        if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
954
0
            ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
955
0
                           "cmd=%s, value=%s", cmd,
956
0
                           value != NULL ? value : "<EMPTY>");
957
0
        return rv;
958
0
    }
959
960
0
 unknown_cmd:
961
0
    if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
962
0
        ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
963
964
0
    return -2;
965
0
}
966
967
int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
968
0
{
969
0
    int rv;
970
0
    const char *arg = NULL, *argn;
971
972
0
    if (pargc != NULL && *pargc == 0)
973
0
        return 0;
974
0
    if (pargc == NULL || *pargc > 0)
975
0
        arg = **pargv;
976
0
    if (arg == NULL)
977
0
        return 0;
978
0
    if (pargc == NULL || *pargc > 1)
979
0
        argn = (*pargv)[1];
980
0
    else
981
0
        argn = NULL;
982
0
    cctx->flags &= ~SSL_CONF_FLAG_FILE;
983
0
    cctx->flags |= SSL_CONF_FLAG_CMDLINE;
984
0
    rv = SSL_CONF_cmd(cctx, arg, argn);
985
0
    if (rv > 0) {
986
        /* Success: update pargc, pargv */
987
0
        (*pargv) += rv;
988
0
        if (pargc)
989
0
            (*pargc) -= rv;
990
0
        return rv;
991
0
    }
992
    /* Unknown switch: indicate no arguments processed */
993
0
    if (rv == -2)
994
0
        return 0;
995
    /* Some error occurred processing command, return fatal error */
996
0
    if (rv == 0)
997
0
        return -1;
998
0
    return rv;
999
0
}
1000
1001
int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
1002
0
{
1003
0
    if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
1004
0
        const ssl_conf_cmd_tbl *runcmd;
1005
0
        runcmd = ssl_conf_cmd_lookup(cctx, cmd);
1006
0
        if (runcmd)
1007
0
            return runcmd->value_type;
1008
0
    }
1009
0
    return SSL_CONF_TYPE_UNKNOWN;
1010
0
}
1011
1012
SSL_CONF_CTX *SSL_CONF_CTX_new(void)
1013
0
{
1014
0
    SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
1015
1016
0
    return ret;
1017
0
}
1018
1019
int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
1020
0
{
1021
    /* See if any certificates are missing private keys */
1022
0
    size_t i;
1023
0
    CERT *c = NULL;
1024
1025
0
    if (cctx->ctx != NULL) {
1026
0
        c = cctx->ctx->cert;
1027
0
    } else if (cctx->ssl != NULL) {
1028
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
1029
1030
0
        if (sc != NULL)
1031
0
            c = sc->cert;
1032
0
    }
1033
0
    if (c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
1034
0
        for (i = 0; i < cctx->num_cert_filename; i++) {
1035
0
            const char *p = cctx->cert_filename[i];
1036
1037
            /*
1038
             * If missing private key try to load one from certificate file
1039
             */
1040
0
            if (p != NULL && c->pkeys[i].privatekey == NULL) {
1041
0
                if (!cmd_PrivateKey(cctx, p))
1042
0
                    return 0;
1043
0
            }
1044
0
        }
1045
0
    }
1046
0
    if (cctx->canames) {
1047
0
        if (cctx->ssl)
1048
0
            SSL_set0_CA_list(cctx->ssl, cctx->canames);
1049
0
        else if (cctx->ctx)
1050
0
            SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
1051
0
        else
1052
0
            sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1053
0
        cctx->canames = NULL;
1054
0
    }
1055
0
    return 1;
1056
0
}
1057
1058
static void free_cert_filename(SSL_CONF_CTX *cctx)
1059
0
{
1060
0
    size_t i;
1061
1062
0
    for (i = 0; i < cctx->num_cert_filename; i++)
1063
0
        OPENSSL_free(cctx->cert_filename[i]);
1064
0
    OPENSSL_free(cctx->cert_filename);
1065
0
    cctx->cert_filename = NULL;
1066
0
    cctx->num_cert_filename = 0;
1067
0
}
1068
1069
void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
1070
135k
{
1071
135k
    if (cctx) {
1072
0
        free_cert_filename(cctx);
1073
0
        OPENSSL_free(cctx->prefix);
1074
0
        sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
1075
0
        OPENSSL_free(cctx);
1076
0
    }
1077
135k
}
1078
1079
unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1080
0
{
1081
0
    cctx->flags |= flags;
1082
0
    return cctx->flags;
1083
0
}
1084
1085
unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1086
0
{
1087
0
    cctx->flags &= ~flags;
1088
0
    return cctx->flags;
1089
0
}
1090
1091
int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
1092
0
{
1093
0
    char *tmp = NULL;
1094
0
    if (pre) {
1095
0
        tmp = OPENSSL_strdup(pre);
1096
0
        if (tmp == NULL)
1097
0
            return 0;
1098
0
    }
1099
0
    OPENSSL_free(cctx->prefix);
1100
0
    cctx->prefix = tmp;
1101
0
    if (tmp)
1102
0
        cctx->prefixlen = strlen(tmp);
1103
0
    else
1104
0
        cctx->prefixlen = 0;
1105
0
    return 1;
1106
0
}
1107
1108
void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
1109
0
{
1110
0
    cctx->ssl = ssl;
1111
0
    cctx->ctx = NULL;
1112
0
    free_cert_filename(cctx);
1113
0
    if (ssl != NULL) {
1114
0
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1115
1116
0
        if (sc == NULL)
1117
0
            return;
1118
0
        cctx->poptions = &sc->options;
1119
0
        cctx->min_version = &sc->min_proto_version;
1120
0
        cctx->max_version = &sc->max_proto_version;
1121
0
        cctx->pcert_flags = &sc->cert->cert_flags;
1122
0
        cctx->pvfy_flags = &sc->verify_mode;
1123
0
        cctx->cert_filename = OPENSSL_zalloc(sc->ssl_pkey_num
1124
0
                                             * sizeof(*cctx->cert_filename));
1125
0
        if (cctx->cert_filename != NULL)
1126
0
            cctx->num_cert_filename = sc->ssl_pkey_num;
1127
0
    } else {
1128
0
        cctx->poptions = NULL;
1129
0
        cctx->min_version = NULL;
1130
0
        cctx->max_version = NULL;
1131
0
        cctx->pcert_flags = NULL;
1132
0
        cctx->pvfy_flags = NULL;
1133
0
    }
1134
0
}
1135
1136
void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
1137
0
{
1138
0
    cctx->ctx = ctx;
1139
0
    cctx->ssl = NULL;
1140
0
    free_cert_filename(cctx);
1141
0
    if (ctx) {
1142
0
        cctx->poptions = &ctx->options;
1143
0
        cctx->min_version = &ctx->min_proto_version;
1144
0
        cctx->max_version = &ctx->max_proto_version;
1145
0
        cctx->pcert_flags = &ctx->cert->cert_flags;
1146
0
        cctx->pvfy_flags = &ctx->verify_mode;
1147
0
        cctx->cert_filename = OPENSSL_zalloc((SSL_PKEY_NUM + ctx->sigalg_list_len)
1148
0
                                             * sizeof(*cctx->cert_filename));
1149
0
        if (cctx->cert_filename != NULL)
1150
0
            cctx->num_cert_filename = SSL_PKEY_NUM + ctx->sigalg_list_len;
1151
0
    } else {
1152
0
        cctx->poptions = NULL;
1153
0
        cctx->min_version = NULL;
1154
0
        cctx->max_version = NULL;
1155
0
        cctx->pcert_flags = NULL;
1156
0
        cctx->pvfy_flags = NULL;
1157
0
    }
1158
0
}