2 * Copyright 2012-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include <openssl/conf.h>
13 #include <openssl/objects.h>
14 #include <openssl/dh.h>
15 #include "internal/nelem.h"
18 * structure holding name tables. This is used for permitted elements in lists
25 unsigned int name_flags
;
26 unsigned long option_value
;
29 /* Switch table: use for single command line switches like no_tls2 */
31 unsigned long option_value
;
32 unsigned int name_flags
;
35 /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
36 #define SSL_TFLAG_INV 0x1
37 /* Mask for type of flag referred to */
38 #define SSL_TFLAG_TYPE_MASK 0xf00
39 /* Flag is for options */
40 #define SSL_TFLAG_OPTION 0x000
41 /* Flag is for cert_flags */
42 #define SSL_TFLAG_CERT 0x100
43 /* Flag is for verify mode */
44 #define SSL_TFLAG_VFY 0x200
45 /* Option can only be used for clients */
46 #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
47 /* Option can only be used for servers */
48 #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
49 #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
51 #define SSL_FLAG_TBL(str, flag) \
52 {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
53 #define SSL_FLAG_TBL_SRV(str, flag) \
54 {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
55 #define SSL_FLAG_TBL_CLI(str, flag) \
56 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
57 #define SSL_FLAG_TBL_INV(str, flag) \
58 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
59 #define SSL_FLAG_TBL_SRV_INV(str, flag) \
60 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
61 #define SSL_FLAG_TBL_CERT(str, flag) \
62 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
64 #define SSL_FLAG_VFY_CLI(str, flag) \
65 {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
66 #define SSL_FLAG_VFY_SRV(str, flag) \
67 {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
70 * Opaque structure containing SSL configuration context.
73 struct ssl_conf_ctx_st
{
75 * Various flags indicating (among other things) which options we will
79 /* Prefix and length of commands */
82 /* SSL_CTX or SSL structure to perform operations on */
85 /* Pointer to SSL or SSL_CTX options field or NULL if none */
87 /* Certificate filenames for each type */
88 char *cert_filename
[SSL_PKEY_NUM
];
89 /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
90 uint32_t *pcert_flags
;
91 /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
93 /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
95 /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
97 /* Current flag table being worked on */
98 const ssl_flag_tbl
*tbl
;
101 /* Client CA names */
102 STACK_OF(X509_NAME
) *canames
;
105 static void ssl_set_option(SSL_CONF_CTX
*cctx
, unsigned int name_flags
,
106 unsigned long option_value
, int onoff
)
109 if (cctx
->poptions
== NULL
)
111 if (name_flags
& SSL_TFLAG_INV
)
113 switch (name_flags
& SSL_TFLAG_TYPE_MASK
) {
116 pflags
= cctx
->pcert_flags
;
120 pflags
= cctx
->pvfy_flags
;
123 case SSL_TFLAG_OPTION
:
124 pflags
= cctx
->poptions
;
132 *pflags
|= option_value
;
134 *pflags
&= ~option_value
;
137 static int ssl_match_option(SSL_CONF_CTX
*cctx
, const ssl_flag_tbl
*tbl
,
138 const char *name
, int namelen
, int onoff
)
140 /* If name not relevant for context skip */
141 if (!(cctx
->flags
& tbl
->name_flags
& SSL_TFLAG_BOTH
))
144 if (strcmp(tbl
->name
, name
))
146 } else if (tbl
->namelen
!= namelen
|| strncasecmp(tbl
->name
, name
, namelen
))
148 ssl_set_option(cctx
, tbl
->name_flags
, tbl
->option_value
, onoff
);
152 static int ssl_set_option_list(const char *elem
, int len
, void *usr
)
154 SSL_CONF_CTX
*cctx
= usr
;
156 const ssl_flag_tbl
*tbl
;
159 * len == -1 indicates not being called in list context, just for single
160 * command line switches, so don't allow +, -.
169 } else if (*elem
== '-') {
175 for (i
= 0, tbl
= cctx
->tbl
; i
< cctx
->ntbl
; i
++, tbl
++) {
176 if (ssl_match_option(cctx
, tbl
, elem
, len
, onoff
))
182 /* Set supported signature algorithms */
183 static int cmd_SignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
187 rv
= SSL_set1_sigalgs_list(cctx
->ssl
, value
);
188 /* NB: ctx == NULL performs syntax checking only */
190 rv
= SSL_CTX_set1_sigalgs_list(cctx
->ctx
, value
);
194 /* Set supported client signature algorithms */
195 static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
199 rv
= SSL_set1_client_sigalgs_list(cctx
->ssl
, value
);
200 /* NB: ctx == NULL performs syntax checking only */
202 rv
= SSL_CTX_set1_client_sigalgs_list(cctx
->ctx
, value
);
206 static int cmd_Groups(SSL_CONF_CTX
*cctx
, const char *value
)
210 rv
= SSL_set1_groups_list(cctx
->ssl
, value
);
211 /* NB: ctx == NULL performs syntax checking only */
213 rv
= SSL_CTX_set1_groups_list(cctx
->ctx
, value
);
217 /* This is the old name for cmd_Groups - retained for backwards compatibility */
218 static int cmd_Curves(SSL_CONF_CTX
*cctx
, const char *value
)
220 return cmd_Groups(cctx
, value
);
223 #ifndef OPENSSL_NO_EC
224 /* ECDH temporary parameters */
225 static int cmd_ECDHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
231 /* Ignore values supported by 1.0.2 for the automatic selection */
232 if ((cctx
->flags
& SSL_CONF_FLAG_FILE
) &&
233 strcasecmp(value
, "+automatic") == 0)
235 if ((cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) &&
236 strcmp(value
, "auto") == 0)
239 nid
= EC_curve_nist2nid(value
);
240 if (nid
== NID_undef
)
241 nid
= OBJ_sn2nid(value
);
244 ecdh
= EC_KEY_new_by_curve_name(nid
);
248 rv
= SSL_CTX_set_tmp_ecdh(cctx
->ctx
, ecdh
);
250 rv
= SSL_set_tmp_ecdh(cctx
->ssl
, ecdh
);
256 static int cmd_CipherString(SSL_CONF_CTX
*cctx
, const char *value
)
260 rv
= SSL_CTX_set_cipher_list(cctx
->ctx
, value
);
262 rv
= SSL_set_cipher_list(cctx
->ssl
, value
);
266 static int cmd_Protocol(SSL_CONF_CTX
*cctx
, const char *value
)
268 static const ssl_flag_tbl ssl_protocol_list
[] = {
269 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK
),
270 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2
),
271 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3
),
272 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1
),
273 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1
),
274 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2
),
275 SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3
),
276 SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1
),
277 SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2
)
279 cctx
->tbl
= ssl_protocol_list
;
280 cctx
->ntbl
= OSSL_NELEM(ssl_protocol_list
);
281 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
285 * protocol_from_string - converts a protocol version string to a number
287 * Returns -1 on failure or the version on success
289 static int protocol_from_string(const char *value
)
291 struct protocol_versions
{
295 static const struct protocol_versions versions
[] = {
297 {"SSLv3", SSL3_VERSION
},
298 {"TLSv1", TLS1_VERSION
},
299 {"TLSv1.1", TLS1_1_VERSION
},
300 {"TLSv1.2", TLS1_2_VERSION
},
301 {"TLSv1.3", TLS1_3_VERSION
},
302 {"DTLSv1", DTLS1_VERSION
},
303 {"DTLSv1.2", DTLS1_2_VERSION
}
306 size_t n
= OSSL_NELEM(versions
);
308 for (i
= 0; i
< n
; i
++)
309 if (strcmp(versions
[i
].name
, value
) == 0)
310 return versions
[i
].version
;
314 static int min_max_proto(SSL_CONF_CTX
*cctx
, const char *value
, int *bound
)
319 if (cctx
->ctx
!= NULL
)
320 method_version
= cctx
->ctx
->method
->version
;
321 else if (cctx
->ssl
!= NULL
)
322 method_version
= cctx
->ssl
->ctx
->method
->version
;
325 if ((new_version
= protocol_from_string(value
)) < 0)
327 return ssl_set_version_bound(method_version
, new_version
, bound
);
331 * cmd_MinProtocol - Set min protocol version
332 * @cctx: config structure to save settings in
333 * @value: The min protocol version in string form
335 * Returns 1 on success and 0 on failure.
337 static int cmd_MinProtocol(SSL_CONF_CTX
*cctx
, const char *value
)
339 return min_max_proto(cctx
, value
, cctx
->min_version
);
343 * cmd_MaxProtocol - Set max protocol version
344 * @cctx: config structure to save settings in
345 * @value: The max protocol version in string form
347 * Returns 1 on success and 0 on failure.
349 static int cmd_MaxProtocol(SSL_CONF_CTX
*cctx
, const char *value
)
351 return min_max_proto(cctx
, value
, cctx
->max_version
);
354 static int cmd_Options(SSL_CONF_CTX
*cctx
, const char *value
)
356 static const ssl_flag_tbl ssl_option_list
[] = {
357 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET
),
358 SSL_FLAG_TBL_INV("EmptyFragments",
359 SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
),
360 SSL_FLAG_TBL("Bugs", SSL_OP_ALL
),
361 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION
),
362 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE
),
363 SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
364 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
),
365 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE
),
366 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE
),
367 SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
368 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
),
369 SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC
),
370 SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION
),
371 SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX
),
372 SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA
),
373 SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT
)
377 cctx
->tbl
= ssl_option_list
;
378 cctx
->ntbl
= OSSL_NELEM(ssl_option_list
);
379 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
382 static int cmd_VerifyMode(SSL_CONF_CTX
*cctx
, const char *value
)
384 static const ssl_flag_tbl ssl_vfy_list
[] = {
385 SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER
),
386 SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER
),
387 SSL_FLAG_VFY_SRV("Require",
388 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
),
389 SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
)
393 cctx
->tbl
= ssl_vfy_list
;
394 cctx
->ntbl
= OSSL_NELEM(ssl_vfy_list
);
395 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
398 static int cmd_Certificate(SSL_CONF_CTX
*cctx
, const char *value
)
403 rv
= SSL_CTX_use_certificate_chain_file(cctx
->ctx
, value
);
407 rv
= SSL_use_certificate_chain_file(cctx
->ssl
, value
);
410 if (rv
> 0 && c
&& cctx
->flags
& SSL_CONF_FLAG_REQUIRE_PRIVATE
) {
411 char **pfilename
= &cctx
->cert_filename
[c
->key
- c
->pkeys
];
412 OPENSSL_free(*pfilename
);
413 *pfilename
= OPENSSL_strdup(value
);
421 static int cmd_PrivateKey(SSL_CONF_CTX
*cctx
, const char *value
)
424 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
427 rv
= SSL_CTX_use_PrivateKey_file(cctx
->ctx
, value
, SSL_FILETYPE_PEM
);
429 rv
= SSL_use_PrivateKey_file(cctx
->ssl
, value
, SSL_FILETYPE_PEM
);
433 static int cmd_ServerInfoFile(SSL_CONF_CTX
*cctx
, const char *value
)
437 rv
= SSL_CTX_use_serverinfo_file(cctx
->ctx
, value
);
441 static int do_store(SSL_CONF_CTX
*cctx
,
442 const char *CAfile
, const char *CApath
, int verify_store
)
447 cert
= cctx
->ctx
->cert
;
449 cert
= cctx
->ssl
->cert
;
452 st
= verify_store
? &cert
->verify_store
: &cert
->chain_store
;
454 *st
= X509_STORE_new();
458 return X509_STORE_load_locations(*st
, CAfile
, CApath
) > 0;
461 static int cmd_ChainCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
463 return do_store(cctx
, NULL
, value
, 0);
466 static int cmd_ChainCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
468 return do_store(cctx
, value
, NULL
, 0);
471 static int cmd_VerifyCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
473 return do_store(cctx
, NULL
, value
, 1);
476 static int cmd_VerifyCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
478 return do_store(cctx
, value
, NULL
, 1);
481 static int cmd_RequestCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
483 if (cctx
->canames
== NULL
)
484 cctx
->canames
= sk_X509_NAME_new_null();
485 if (cctx
->canames
== NULL
)
487 return SSL_add_file_cert_subjects_to_stack(cctx
->canames
, value
);
490 static int cmd_ClientCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
492 return cmd_RequestCAFile(cctx
, value
);
495 static int cmd_RequestCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
497 if (cctx
->canames
== NULL
)
498 cctx
->canames
= sk_X509_NAME_new_null();
499 if (cctx
->canames
== NULL
)
501 return SSL_add_dir_cert_subjects_to_stack(cctx
->canames
, value
);
504 static int cmd_ClientCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
506 return cmd_RequestCAPath(cctx
, value
);
509 #ifndef OPENSSL_NO_DH
510 static int cmd_DHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
515 if (cctx
->ctx
|| cctx
->ssl
) {
516 in
= BIO_new(BIO_s_file());
519 if (BIO_read_filename(in
, value
) <= 0)
521 dh
= PEM_read_bio_DHparams(in
, NULL
, NULL
, NULL
);
527 rv
= SSL_CTX_set_tmp_dh(cctx
->ctx
, dh
);
529 rv
= SSL_set_tmp_dh(cctx
->ssl
, dh
);
537 static int cmd_RecordPadding(SSL_CONF_CTX
*cctx
, const char *value
)
540 int block_size
= atoi(value
);
543 * All we care about is a non-negative value,
544 * the setters check the range
546 if (block_size
>= 0) {
548 rv
= SSL_CTX_set_block_padding(cctx
->ctx
, block_size
);
550 rv
= SSL_set_block_padding(cctx
->ssl
, block_size
);
556 int (*cmd
) (SSL_CONF_CTX
*cctx
, const char *value
);
557 const char *str_file
;
558 const char *str_cmdline
;
559 unsigned short flags
;
560 unsigned short value_type
;
563 /* Table of supported parameters */
565 #define SSL_CONF_CMD(name, cmdopt, flags, type) \
566 {cmd_##name, #name, cmdopt, flags, type}
568 #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
569 SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
571 #define SSL_CONF_CMD_SWITCH(name, flags) \
572 {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
574 /* See apps/apps.h if you change this table. */
575 static const ssl_conf_cmd_tbl ssl_conf_cmds
[] = {
576 SSL_CONF_CMD_SWITCH("no_ssl3", 0),
577 SSL_CONF_CMD_SWITCH("no_tls1", 0),
578 SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
579 SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
580 SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
581 SSL_CONF_CMD_SWITCH("bugs", 0),
582 SSL_CONF_CMD_SWITCH("no_comp", 0),
583 SSL_CONF_CMD_SWITCH("comp", 0),
584 SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER
),
585 SSL_CONF_CMD_SWITCH("no_ticket", 0),
586 SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER
),
587 SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
588 SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER
),
589 SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
590 SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER
),
591 SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER
),
592 SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
593 SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER
),
594 SSL_CONF_CMD_SWITCH("strict", 0),
595 SSL_CONF_CMD_SWITCH("no_middlebox", SSL_CONF_FLAG_CLIENT
),
596 SSL_CONF_CMD_STRING(SignatureAlgorithms
, "sigalgs", 0),
597 SSL_CONF_CMD_STRING(ClientSignatureAlgorithms
, "client_sigalgs", 0),
598 SSL_CONF_CMD_STRING(Curves
, "curves", 0),
599 SSL_CONF_CMD_STRING(Groups
, "groups", 0),
600 #ifndef OPENSSL_NO_EC
601 SSL_CONF_CMD_STRING(ECDHParameters
, "named_curve", SSL_CONF_FLAG_SERVER
),
603 SSL_CONF_CMD_STRING(CipherString
, "cipher", 0),
604 SSL_CONF_CMD_STRING(Protocol
, NULL
, 0),
605 SSL_CONF_CMD_STRING(MinProtocol
, "min_protocol", 0),
606 SSL_CONF_CMD_STRING(MaxProtocol
, "max_protocol", 0),
607 SSL_CONF_CMD_STRING(Options
, NULL
, 0),
608 SSL_CONF_CMD_STRING(VerifyMode
, NULL
, 0),
609 SSL_CONF_CMD(Certificate
, "cert", SSL_CONF_FLAG_CERTIFICATE
,
611 SSL_CONF_CMD(PrivateKey
, "key", SSL_CONF_FLAG_CERTIFICATE
,
613 SSL_CONF_CMD(ServerInfoFile
, NULL
,
614 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
616 SSL_CONF_CMD(ChainCAPath
, "chainCApath", SSL_CONF_FLAG_CERTIFICATE
,
618 SSL_CONF_CMD(ChainCAFile
, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE
,
620 SSL_CONF_CMD(VerifyCAPath
, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE
,
622 SSL_CONF_CMD(VerifyCAFile
, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE
,
624 SSL_CONF_CMD(RequestCAFile
, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE
,
626 SSL_CONF_CMD(ClientCAFile
, NULL
,
627 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
629 SSL_CONF_CMD(RequestCAPath
, NULL
, SSL_CONF_FLAG_CERTIFICATE
,
631 SSL_CONF_CMD(ClientCAPath
, NULL
,
632 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
634 #ifndef OPENSSL_NO_DH
635 SSL_CONF_CMD(DHParameters
, "dhparam",
636 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
639 SSL_CONF_CMD_STRING(RecordPadding
, "record_padding", 0)
642 /* Supported switches: must match order of switches in ssl_conf_cmds */
643 static const ssl_switch_tbl ssl_cmd_switches
[] = {
644 {SSL_OP_NO_SSLv3
, 0}, /* no_ssl3 */
645 {SSL_OP_NO_TLSv1
, 0}, /* no_tls1 */
646 {SSL_OP_NO_TLSv1_1
, 0}, /* no_tls1_1 */
647 {SSL_OP_NO_TLSv1_2
, 0}, /* no_tls1_2 */
648 {SSL_OP_NO_TLSv1_3
, 0}, /* no_tls1_3 */
649 {SSL_OP_ALL
, 0}, /* bugs */
650 {SSL_OP_NO_COMPRESSION
, 0}, /* no_comp */
651 {SSL_OP_NO_COMPRESSION
, SSL_TFLAG_INV
}, /* comp */
652 {SSL_OP_SINGLE_ECDH_USE
, 0}, /* ecdh_single */
653 {SSL_OP_NO_TICKET
, 0}, /* no_ticket */
654 {SSL_OP_CIPHER_SERVER_PREFERENCE
, 0}, /* serverpref */
655 /* legacy_renegotiation */
656 {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
, 0},
657 /* legacy_server_connect */
658 {SSL_OP_LEGACY_SERVER_CONNECT
, 0},
659 /* no_renegotiation */
660 {SSL_OP_NO_RENEGOTIATION
, 0},
661 /* no_resumption_on_reneg */
662 {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
, 0},
663 /* no_legacy_server_connect */
664 {SSL_OP_LEGACY_SERVER_CONNECT
, SSL_TFLAG_INV
},
665 /* allow_no_dhe_kex */
666 {SSL_OP_ALLOW_NO_DHE_KEX
, 0},
667 /* chacha reprioritization */
668 {SSL_OP_PRIORITIZE_CHACHA
, 0},
669 {SSL_CERT_FLAG_TLS_STRICT
, SSL_TFLAG_CERT
}, /* strict */
671 {SSL_OP_ENABLE_MIDDLEBOX_COMPAT
, SSL_TFLAG_INV
},
674 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX
*cctx
, const char **pcmd
)
678 /* If a prefix is set, check and skip */
680 if (strlen(*pcmd
) <= cctx
->prefixlen
)
682 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
&&
683 strncmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
685 if (cctx
->flags
& SSL_CONF_FLAG_FILE
&&
686 strncasecmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
688 *pcmd
+= cctx
->prefixlen
;
689 } else if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
690 if (**pcmd
!= '-' || !(*pcmd
)[1])
697 /* Determine if a command is allowed according to cctx flags */
698 static int ssl_conf_cmd_allowed(SSL_CONF_CTX
*cctx
, const ssl_conf_cmd_tbl
* t
)
700 unsigned int tfl
= t
->flags
;
701 unsigned int cfl
= cctx
->flags
;
702 if ((tfl
& SSL_CONF_FLAG_SERVER
) && !(cfl
& SSL_CONF_FLAG_SERVER
))
704 if ((tfl
& SSL_CONF_FLAG_CLIENT
) && !(cfl
& SSL_CONF_FLAG_CLIENT
))
706 if ((tfl
& SSL_CONF_FLAG_CERTIFICATE
)
707 && !(cfl
& SSL_CONF_FLAG_CERTIFICATE
))
712 static const ssl_conf_cmd_tbl
*ssl_conf_cmd_lookup(SSL_CONF_CTX
*cctx
,
715 const ssl_conf_cmd_tbl
*t
;
720 /* Look for matching parameter name in table */
721 for (i
= 0, t
= ssl_conf_cmds
; i
< OSSL_NELEM(ssl_conf_cmds
); i
++, t
++) {
722 if (ssl_conf_cmd_allowed(cctx
, t
)) {
723 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
724 if (t
->str_cmdline
&& strcmp(t
->str_cmdline
, cmd
) == 0)
727 if (cctx
->flags
& SSL_CONF_FLAG_FILE
) {
728 if (t
->str_file
&& strcasecmp(t
->str_file
, cmd
) == 0)
736 static int ctrl_switch_option(SSL_CONF_CTX
*cctx
, const ssl_conf_cmd_tbl
* cmd
)
738 /* Find index of command in table */
739 size_t idx
= cmd
- ssl_conf_cmds
;
740 const ssl_switch_tbl
*scmd
;
741 /* Sanity check index */
742 if (idx
>= OSSL_NELEM(ssl_cmd_switches
))
744 /* Obtain switches entry with same index */
745 scmd
= ssl_cmd_switches
+ idx
;
746 ssl_set_option(cctx
, scmd
->name_flags
, scmd
->option_value
, 1);
750 int SSL_CONF_cmd(SSL_CONF_CTX
*cctx
, const char *cmd
, const char *value
)
752 const ssl_conf_cmd_tbl
*runcmd
;
754 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_INVALID_NULL_CMD_NAME
);
758 if (!ssl_conf_cmd_skip_prefix(cctx
, &cmd
))
761 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
765 if (runcmd
->value_type
== SSL_CONF_TYPE_NONE
) {
766 return ctrl_switch_option(cctx
, runcmd
);
770 rv
= runcmd
->cmd(cctx
, value
);
775 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
776 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_BAD_VALUE
);
777 ERR_add_error_data(4, "cmd=", cmd
, ", value=", value
);
782 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
783 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_UNKNOWN_CMD_NAME
);
784 ERR_add_error_data(2, "cmd=", cmd
);
790 int SSL_CONF_cmd_argv(SSL_CONF_CTX
*cctx
, int *pargc
, char ***pargv
)
793 const char *arg
= NULL
, *argn
;
794 if (pargc
&& *pargc
== 0)
796 if (!pargc
|| *pargc
> 0)
800 if (!pargc
|| *pargc
> 1)
804 cctx
->flags
&= ~SSL_CONF_FLAG_FILE
;
805 cctx
->flags
|= SSL_CONF_FLAG_CMDLINE
;
806 rv
= SSL_CONF_cmd(cctx
, arg
, argn
);
808 /* Success: update pargc, pargv */
814 /* Unknown switch: indicate no arguments processed */
817 /* Some error occurred processing command, return fatal error */
823 int SSL_CONF_cmd_value_type(SSL_CONF_CTX
*cctx
, const char *cmd
)
825 if (ssl_conf_cmd_skip_prefix(cctx
, &cmd
)) {
826 const ssl_conf_cmd_tbl
*runcmd
;
827 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
829 return runcmd
->value_type
;
831 return SSL_CONF_TYPE_UNKNOWN
;
834 SSL_CONF_CTX
*SSL_CONF_CTX_new(void)
836 SSL_CONF_CTX
*ret
= OPENSSL_zalloc(sizeof(*ret
));
841 int SSL_CONF_CTX_finish(SSL_CONF_CTX
*cctx
)
843 /* See if any certificates are missing private keys */
850 if (c
&& cctx
->flags
& SSL_CONF_FLAG_REQUIRE_PRIVATE
) {
851 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
852 const char *p
= cctx
->cert_filename
[i
];
854 * If missing private key try to load one from certificate file
856 if (p
&& !c
->pkeys
[i
].privatekey
) {
857 if (!cmd_PrivateKey(cctx
, p
))
864 SSL_set0_CA_list(cctx
->ssl
, cctx
->canames
);
866 SSL_CTX_set0_CA_list(cctx
->ctx
, cctx
->canames
);
868 sk_X509_NAME_pop_free(cctx
->canames
, X509_NAME_free
);
869 cctx
->canames
= NULL
;
874 void SSL_CONF_CTX_free(SSL_CONF_CTX
*cctx
)
878 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
879 OPENSSL_free(cctx
->cert_filename
[i
]);
880 OPENSSL_free(cctx
->prefix
);
881 sk_X509_NAME_pop_free(cctx
->canames
, X509_NAME_free
);
886 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
888 cctx
->flags
|= flags
;
892 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
894 cctx
->flags
&= ~flags
;
898 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX
*cctx
, const char *pre
)
902 tmp
= OPENSSL_strdup(pre
);
906 OPENSSL_free(cctx
->prefix
);
909 cctx
->prefixlen
= strlen(tmp
);
915 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX
*cctx
, SSL
*ssl
)
920 cctx
->poptions
= &ssl
->options
;
921 cctx
->min_version
= &ssl
->min_proto_version
;
922 cctx
->max_version
= &ssl
->max_proto_version
;
923 cctx
->pcert_flags
= &ssl
->cert
->cert_flags
;
924 cctx
->pvfy_flags
= &ssl
->verify_mode
;
926 cctx
->poptions
= NULL
;
927 cctx
->min_version
= NULL
;
928 cctx
->max_version
= NULL
;
929 cctx
->pcert_flags
= NULL
;
930 cctx
->pvfy_flags
= NULL
;
934 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX
*cctx
, SSL_CTX
*ctx
)
939 cctx
->poptions
= &ctx
->options
;
940 cctx
->min_version
= &ctx
->min_proto_version
;
941 cctx
->max_version
= &ctx
->max_proto_version
;
942 cctx
->pcert_flags
= &ctx
->cert
->cert_flags
;
943 cctx
->pvfy_flags
= &ctx
->verify_mode
;
945 cctx
->poptions
= NULL
;
946 cctx
->min_version
= NULL
;
947 cctx
->max_version
= NULL
;
948 cctx
->pcert_flags
= NULL
;
949 cctx
->pvfy_flags
= NULL
;