2 * Copyright 2012-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "ssl_local.h"
12 #include <openssl/conf.h>
13 #include <openssl/objects.h>
14 #include <openssl/dh.h>
15 #include "internal/nelem.h"
17 DEFINE_STACK_OF(X509_NAME
)
20 * structure holding name tables. This is used for permitted elements in lists
27 unsigned int name_flags
;
28 unsigned long option_value
;
31 /* Switch table: use for single command line switches like no_tls2 */
33 unsigned long option_value
;
34 unsigned int name_flags
;
37 /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
38 #define SSL_TFLAG_INV 0x1
39 /* Mask for type of flag referred to */
40 #define SSL_TFLAG_TYPE_MASK 0xf00
41 /* Flag is for options */
42 #define SSL_TFLAG_OPTION 0x000
43 /* Flag is for cert_flags */
44 #define SSL_TFLAG_CERT 0x100
45 /* Flag is for verify mode */
46 #define SSL_TFLAG_VFY 0x200
47 /* Option can only be used for clients */
48 #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
49 /* Option can only be used for servers */
50 #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
51 #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
53 #define SSL_FLAG_TBL(str, flag) \
54 {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
55 #define SSL_FLAG_TBL_SRV(str, flag) \
56 {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
57 #define SSL_FLAG_TBL_CLI(str, flag) \
58 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
59 #define SSL_FLAG_TBL_INV(str, flag) \
60 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
61 #define SSL_FLAG_TBL_SRV_INV(str, flag) \
62 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
63 #define SSL_FLAG_TBL_CERT(str, flag) \
64 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
66 #define SSL_FLAG_VFY_CLI(str, flag) \
67 {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
68 #define SSL_FLAG_VFY_SRV(str, flag) \
69 {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
72 * Opaque structure containing SSL configuration context.
75 struct ssl_conf_ctx_st
{
77 * Various flags indicating (among other things) which options we will
81 /* Prefix and length of commands */
84 /* SSL_CTX or SSL structure to perform operations on */
87 /* Pointer to SSL or SSL_CTX options field or NULL if none */
89 /* Certificate filenames for each type */
90 char *cert_filename
[SSL_PKEY_NUM
];
91 /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
92 uint32_t *pcert_flags
;
93 /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
95 /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
97 /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
99 /* Current flag table being worked on */
100 const ssl_flag_tbl
*tbl
;
103 /* Client CA names */
104 STACK_OF(X509_NAME
) *canames
;
107 static void ssl_set_option(SSL_CONF_CTX
*cctx
, unsigned int name_flags
,
108 unsigned long option_value
, int onoff
)
111 if (cctx
->poptions
== NULL
)
113 if (name_flags
& SSL_TFLAG_INV
)
115 switch (name_flags
& SSL_TFLAG_TYPE_MASK
) {
118 pflags
= cctx
->pcert_flags
;
122 pflags
= cctx
->pvfy_flags
;
125 case SSL_TFLAG_OPTION
:
126 pflags
= cctx
->poptions
;
134 *pflags
|= option_value
;
136 *pflags
&= ~option_value
;
139 static int ssl_match_option(SSL_CONF_CTX
*cctx
, const ssl_flag_tbl
*tbl
,
140 const char *name
, int namelen
, int onoff
)
142 /* If name not relevant for context skip */
143 if (!(cctx
->flags
& tbl
->name_flags
& SSL_TFLAG_BOTH
))
146 if (strcmp(tbl
->name
, name
))
148 } else if (tbl
->namelen
!= namelen
|| strncasecmp(tbl
->name
, name
, namelen
))
150 ssl_set_option(cctx
, tbl
->name_flags
, tbl
->option_value
, onoff
);
154 static int ssl_set_option_list(const char *elem
, int len
, void *usr
)
156 SSL_CONF_CTX
*cctx
= usr
;
158 const ssl_flag_tbl
*tbl
;
161 * len == -1 indicates not being called in list context, just for single
162 * command line switches, so don't allow +, -.
171 } else if (*elem
== '-') {
177 for (i
= 0, tbl
= cctx
->tbl
; i
< cctx
->ntbl
; i
++, tbl
++) {
178 if (ssl_match_option(cctx
, tbl
, elem
, len
, onoff
))
184 /* Set supported signature algorithms */
185 static int cmd_SignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
189 rv
= SSL_set1_sigalgs_list(cctx
->ssl
, value
);
190 /* NB: ctx == NULL performs syntax checking only */
192 rv
= SSL_CTX_set1_sigalgs_list(cctx
->ctx
, value
);
196 /* Set supported client signature algorithms */
197 static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
201 rv
= SSL_set1_client_sigalgs_list(cctx
->ssl
, value
);
202 /* NB: ctx == NULL performs syntax checking only */
204 rv
= SSL_CTX_set1_client_sigalgs_list(cctx
->ctx
, value
);
208 static int cmd_Groups(SSL_CONF_CTX
*cctx
, const char *value
)
212 rv
= SSL_set1_groups_list(cctx
->ssl
, value
);
213 /* NB: ctx == NULL performs syntax checking only */
215 rv
= SSL_CTX_set1_groups_list(cctx
->ctx
, value
);
219 /* This is the old name for cmd_Groups - retained for backwards compatibility */
220 static int cmd_Curves(SSL_CONF_CTX
*cctx
, const char *value
)
222 return cmd_Groups(cctx
, value
);
225 #ifndef OPENSSL_NO_EC
226 /* ECDH temporary parameters */
227 static int cmd_ECDHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
232 /* Ignore values supported by 1.0.2 for the automatic selection */
233 if ((cctx
->flags
& SSL_CONF_FLAG_FILE
)
234 && (strcasecmp(value
, "+automatic") == 0
235 || strcasecmp(value
, "automatic") == 0))
237 if ((cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) &&
238 strcmp(value
, "auto") == 0)
241 nid
= EC_curve_nist2nid(value
);
242 if (nid
== NID_undef
)
243 nid
= OBJ_sn2nid(value
);
248 rv
= SSL_CTX_set1_groups(cctx
->ctx
, &nid
, 1);
250 rv
= SSL_set1_groups(cctx
->ssl
, &nid
, 1);
255 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_Ciphersuites(SSL_CONF_CTX
*cctx
, const char *value
)
271 rv
= SSL_CTX_set_ciphersuites(cctx
->ctx
, value
);
273 rv
= SSL_set_ciphersuites(cctx
->ssl
, value
);
277 static int cmd_Protocol(SSL_CONF_CTX
*cctx
, const char *value
)
279 static const ssl_flag_tbl ssl_protocol_list
[] = {
280 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK
),
281 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2
),
282 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3
),
283 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1
),
284 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1
),
285 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2
),
286 SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3
),
287 SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1
),
288 SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2
)
290 cctx
->tbl
= ssl_protocol_list
;
291 cctx
->ntbl
= OSSL_NELEM(ssl_protocol_list
);
292 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
296 * protocol_from_string - converts a protocol version string to a number
298 * Returns -1 on failure or the version on success
300 static int protocol_from_string(const char *value
)
302 struct protocol_versions
{
306 static const struct protocol_versions versions
[] = {
308 {"SSLv3", SSL3_VERSION
},
309 {"TLSv1", TLS1_VERSION
},
310 {"TLSv1.1", TLS1_1_VERSION
},
311 {"TLSv1.2", TLS1_2_VERSION
},
312 {"TLSv1.3", TLS1_3_VERSION
},
313 {"DTLSv1", DTLS1_VERSION
},
314 {"DTLSv1.2", DTLS1_2_VERSION
}
317 size_t n
= OSSL_NELEM(versions
);
319 for (i
= 0; i
< n
; i
++)
320 if (strcmp(versions
[i
].name
, value
) == 0)
321 return versions
[i
].version
;
325 static int min_max_proto(SSL_CONF_CTX
*cctx
, const char *value
, int *bound
)
330 if (cctx
->ctx
!= NULL
)
331 method_version
= cctx
->ctx
->method
->version
;
332 else if (cctx
->ssl
!= NULL
)
333 method_version
= cctx
->ssl
->ctx
->method
->version
;
336 if ((new_version
= protocol_from_string(value
)) < 0)
338 return ssl_set_version_bound(method_version
, new_version
, bound
);
342 * cmd_MinProtocol - Set min protocol version
343 * @cctx: config structure to save settings in
344 * @value: The min protocol version in string form
346 * Returns 1 on success and 0 on failure.
348 static int cmd_MinProtocol(SSL_CONF_CTX
*cctx
, const char *value
)
350 return min_max_proto(cctx
, value
, cctx
->min_version
);
354 * cmd_MaxProtocol - Set max protocol version
355 * @cctx: config structure to save settings in
356 * @value: The max protocol version in string form
358 * Returns 1 on success and 0 on failure.
360 static int cmd_MaxProtocol(SSL_CONF_CTX
*cctx
, const char *value
)
362 return min_max_proto(cctx
, value
, cctx
->max_version
);
365 static int cmd_Options(SSL_CONF_CTX
*cctx
, const char *value
)
367 static const ssl_flag_tbl ssl_option_list
[] = {
368 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET
),
369 SSL_FLAG_TBL_INV("EmptyFragments",
370 SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
),
371 SSL_FLAG_TBL("Bugs", SSL_OP_ALL
),
372 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION
),
373 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE
),
374 SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
375 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
),
376 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE
),
377 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE
),
378 SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
379 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
),
380 SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC
),
381 SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION
),
382 SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX
),
383 SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA
),
384 SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT
),
385 SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY
),
386 SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET
)
390 cctx
->tbl
= ssl_option_list
;
391 cctx
->ntbl
= OSSL_NELEM(ssl_option_list
);
392 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
395 static int cmd_VerifyMode(SSL_CONF_CTX
*cctx
, const char *value
)
397 static const ssl_flag_tbl ssl_vfy_list
[] = {
398 SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER
),
399 SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER
),
400 SSL_FLAG_VFY_SRV("Require",
401 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
),
402 SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
),
403 SSL_FLAG_VFY_SRV("RequestPostHandshake",
404 SSL_VERIFY_PEER
| SSL_VERIFY_POST_HANDSHAKE
),
405 SSL_FLAG_VFY_SRV("RequirePostHandshake",
406 SSL_VERIFY_PEER
| SSL_VERIFY_POST_HANDSHAKE
|
407 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
),
411 cctx
->tbl
= ssl_vfy_list
;
412 cctx
->ntbl
= OSSL_NELEM(ssl_vfy_list
);
413 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
416 static int cmd_Certificate(SSL_CONF_CTX
*cctx
, const char *value
)
421 rv
= SSL_CTX_use_certificate_chain_file(cctx
->ctx
, value
);
425 rv
= SSL_use_certificate_chain_file(cctx
->ssl
, value
);
428 if (rv
> 0 && c
&& cctx
->flags
& SSL_CONF_FLAG_REQUIRE_PRIVATE
) {
429 char **pfilename
= &cctx
->cert_filename
[c
->key
- c
->pkeys
];
430 OPENSSL_free(*pfilename
);
431 *pfilename
= OPENSSL_strdup(value
);
432 if (*pfilename
== NULL
)
439 static int cmd_PrivateKey(SSL_CONF_CTX
*cctx
, const char *value
)
442 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
445 rv
= SSL_CTX_use_PrivateKey_file(cctx
->ctx
, value
, SSL_FILETYPE_PEM
);
447 rv
= SSL_use_PrivateKey_file(cctx
->ssl
, value
, SSL_FILETYPE_PEM
);
451 static int cmd_ServerInfoFile(SSL_CONF_CTX
*cctx
, const char *value
)
455 rv
= SSL_CTX_use_serverinfo_file(cctx
->ctx
, value
);
459 static int do_store(SSL_CONF_CTX
*cctx
,
460 const char *CAfile
, const char *CApath
, const char *CAstore
,
467 cert
= cctx
->ctx
->cert
;
469 cert
= cctx
->ssl
->cert
;
472 st
= verify_store
? &cert
->verify_store
: &cert
->chain_store
;
474 *st
= X509_STORE_new();
479 if (CAfile
!= NULL
&& !X509_STORE_load_file(*st
, CAfile
))
481 if (CApath
!= NULL
&& !X509_STORE_load_path(*st
, CApath
))
483 if (CAstore
!= NULL
&& !X509_STORE_load_store(*st
, CAstore
))
488 static int cmd_ChainCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
490 return do_store(cctx
, NULL
, value
, NULL
, 0);
493 static int cmd_ChainCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
495 return do_store(cctx
, value
, NULL
, NULL
, 0);
498 static int cmd_ChainCAStore(SSL_CONF_CTX
*cctx
, const char *value
)
500 return do_store(cctx
, NULL
, NULL
, value
, 0);
503 static int cmd_VerifyCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
505 return do_store(cctx
, NULL
, value
, NULL
, 1);
508 static int cmd_VerifyCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
510 return do_store(cctx
, value
, NULL
, NULL
, 1);
513 static int cmd_VerifyCAStore(SSL_CONF_CTX
*cctx
, const char *value
)
515 return do_store(cctx
, NULL
, NULL
, value
, 1);
518 static int cmd_RequestCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
520 if (cctx
->canames
== NULL
)
521 cctx
->canames
= sk_X509_NAME_new_null();
522 if (cctx
->canames
== NULL
)
524 return SSL_add_file_cert_subjects_to_stack(cctx
->canames
, value
);
527 static int cmd_ClientCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
529 return cmd_RequestCAFile(cctx
, value
);
532 static int cmd_RequestCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
534 if (cctx
->canames
== NULL
)
535 cctx
->canames
= sk_X509_NAME_new_null();
536 if (cctx
->canames
== NULL
)
538 return SSL_add_dir_cert_subjects_to_stack(cctx
->canames
, value
);
541 static int cmd_ClientCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
543 return cmd_RequestCAPath(cctx
, value
);
546 static int cmd_RequestCAStore(SSL_CONF_CTX
*cctx
, const char *value
)
548 if (cctx
->canames
== NULL
)
549 cctx
->canames
= sk_X509_NAME_new_null();
550 if (cctx
->canames
== NULL
)
552 return SSL_add_store_cert_subjects_to_stack(cctx
->canames
, value
);
555 static int cmd_ClientCAStore(SSL_CONF_CTX
*cctx
, const char *value
)
557 return cmd_RequestCAStore(cctx
, value
);
560 #ifndef OPENSSL_NO_DH
561 static int cmd_DHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
566 if (cctx
->ctx
|| cctx
->ssl
) {
567 in
= BIO_new(BIO_s_file());
570 if (BIO_read_filename(in
, value
) <= 0)
572 dh
= PEM_read_bio_DHparams(in
, NULL
, NULL
, NULL
);
578 rv
= SSL_CTX_set_tmp_dh(cctx
->ctx
, dh
);
580 rv
= SSL_set_tmp_dh(cctx
->ssl
, dh
);
588 static int cmd_RecordPadding(SSL_CONF_CTX
*cctx
, const char *value
)
591 int block_size
= atoi(value
);
594 * All we care about is a non-negative value,
595 * the setters check the range
597 if (block_size
>= 0) {
599 rv
= SSL_CTX_set_block_padding(cctx
->ctx
, block_size
);
601 rv
= SSL_set_block_padding(cctx
->ssl
, block_size
);
607 static int cmd_NumTickets(SSL_CONF_CTX
*cctx
, const char *value
)
610 int num_tickets
= atoi(value
);
612 if (num_tickets
>= 0) {
614 rv
= SSL_CTX_set_num_tickets(cctx
->ctx
, num_tickets
);
616 rv
= SSL_set_num_tickets(cctx
->ssl
, num_tickets
);
622 int (*cmd
) (SSL_CONF_CTX
*cctx
, const char *value
);
623 const char *str_file
;
624 const char *str_cmdline
;
625 unsigned short flags
;
626 unsigned short value_type
;
629 /* Table of supported parameters */
631 #define SSL_CONF_CMD(name, cmdopt, flags, type) \
632 {cmd_##name, #name, cmdopt, flags, type}
634 #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
635 SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
637 #define SSL_CONF_CMD_SWITCH(name, flags) \
638 {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
640 /* See apps/apps.h if you change this table. */
641 static const ssl_conf_cmd_tbl ssl_conf_cmds
[] = {
642 SSL_CONF_CMD_SWITCH("no_ssl3", 0),
643 SSL_CONF_CMD_SWITCH("no_tls1", 0),
644 SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
645 SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
646 SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
647 SSL_CONF_CMD_SWITCH("bugs", 0),
648 SSL_CONF_CMD_SWITCH("no_comp", 0),
649 SSL_CONF_CMD_SWITCH("comp", 0),
650 SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER
),
651 SSL_CONF_CMD_SWITCH("no_ticket", 0),
652 SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER
),
653 SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
654 SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER
),
655 SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
656 SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER
),
657 SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER
),
658 SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
659 SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER
),
660 SSL_CONF_CMD_SWITCH("strict", 0),
661 SSL_CONF_CMD_SWITCH("no_middlebox", 0),
662 SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER
),
663 SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER
),
664 SSL_CONF_CMD_STRING(SignatureAlgorithms
, "sigalgs", 0),
665 SSL_CONF_CMD_STRING(ClientSignatureAlgorithms
, "client_sigalgs", 0),
666 SSL_CONF_CMD_STRING(Curves
, "curves", 0),
667 SSL_CONF_CMD_STRING(Groups
, "groups", 0),
668 #ifndef OPENSSL_NO_EC
669 SSL_CONF_CMD_STRING(ECDHParameters
, "named_curve", SSL_CONF_FLAG_SERVER
),
671 SSL_CONF_CMD_STRING(CipherString
, "cipher", 0),
672 SSL_CONF_CMD_STRING(Ciphersuites
, "ciphersuites", 0),
673 SSL_CONF_CMD_STRING(Protocol
, NULL
, 0),
674 SSL_CONF_CMD_STRING(MinProtocol
, "min_protocol", 0),
675 SSL_CONF_CMD_STRING(MaxProtocol
, "max_protocol", 0),
676 SSL_CONF_CMD_STRING(Options
, NULL
, 0),
677 SSL_CONF_CMD_STRING(VerifyMode
, NULL
, 0),
678 SSL_CONF_CMD(Certificate
, "cert", SSL_CONF_FLAG_CERTIFICATE
,
680 SSL_CONF_CMD(PrivateKey
, "key", SSL_CONF_FLAG_CERTIFICATE
,
682 SSL_CONF_CMD(ServerInfoFile
, NULL
,
683 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
685 SSL_CONF_CMD(ChainCAPath
, "chainCApath", SSL_CONF_FLAG_CERTIFICATE
,
687 SSL_CONF_CMD(ChainCAFile
, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE
,
689 SSL_CONF_CMD(ChainCAStore
, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE
,
690 SSL_CONF_TYPE_STORE
),
691 SSL_CONF_CMD(VerifyCAPath
, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE
,
693 SSL_CONF_CMD(VerifyCAFile
, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE
,
695 SSL_CONF_CMD(VerifyCAStore
, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE
,
696 SSL_CONF_TYPE_STORE
),
697 SSL_CONF_CMD(RequestCAFile
, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE
,
699 SSL_CONF_CMD(ClientCAFile
, NULL
,
700 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
702 SSL_CONF_CMD(RequestCAPath
, NULL
, SSL_CONF_FLAG_CERTIFICATE
,
704 SSL_CONF_CMD(ClientCAPath
, NULL
,
705 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
707 SSL_CONF_CMD(RequestCAStore
, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE
,
708 SSL_CONF_TYPE_STORE
),
709 SSL_CONF_CMD(ClientCAStore
, NULL
,
710 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
711 SSL_CONF_TYPE_STORE
),
712 #ifndef OPENSSL_NO_DH
713 SSL_CONF_CMD(DHParameters
, "dhparam",
714 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
717 SSL_CONF_CMD_STRING(RecordPadding
, "record_padding", 0),
718 SSL_CONF_CMD_STRING(NumTickets
, "num_tickets", SSL_CONF_FLAG_SERVER
),
721 /* Supported switches: must match order of switches in ssl_conf_cmds */
722 static const ssl_switch_tbl ssl_cmd_switches
[] = {
723 {SSL_OP_NO_SSLv3
, 0}, /* no_ssl3 */
724 {SSL_OP_NO_TLSv1
, 0}, /* no_tls1 */
725 {SSL_OP_NO_TLSv1_1
, 0}, /* no_tls1_1 */
726 {SSL_OP_NO_TLSv1_2
, 0}, /* no_tls1_2 */
727 {SSL_OP_NO_TLSv1_3
, 0}, /* no_tls1_3 */
728 {SSL_OP_ALL
, 0}, /* bugs */
729 {SSL_OP_NO_COMPRESSION
, 0}, /* no_comp */
730 {SSL_OP_NO_COMPRESSION
, SSL_TFLAG_INV
}, /* comp */
731 {SSL_OP_SINGLE_ECDH_USE
, 0}, /* ecdh_single */
732 {SSL_OP_NO_TICKET
, 0}, /* no_ticket */
733 {SSL_OP_CIPHER_SERVER_PREFERENCE
, 0}, /* serverpref */
734 /* legacy_renegotiation */
735 {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
, 0},
736 /* legacy_server_connect */
737 {SSL_OP_LEGACY_SERVER_CONNECT
, 0},
738 /* no_renegotiation */
739 {SSL_OP_NO_RENEGOTIATION
, 0},
740 /* no_resumption_on_reneg */
741 {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
, 0},
742 /* no_legacy_server_connect */
743 {SSL_OP_LEGACY_SERVER_CONNECT
, SSL_TFLAG_INV
},
744 /* allow_no_dhe_kex */
745 {SSL_OP_ALLOW_NO_DHE_KEX
, 0},
746 /* chacha reprioritization */
747 {SSL_OP_PRIORITIZE_CHACHA
, 0},
748 {SSL_CERT_FLAG_TLS_STRICT
, SSL_TFLAG_CERT
}, /* strict */
750 {SSL_OP_ENABLE_MIDDLEBOX_COMPAT
, SSL_TFLAG_INV
},
752 {SSL_OP_NO_ANTI_REPLAY
, SSL_TFLAG_INV
},
754 {SSL_OP_NO_ANTI_REPLAY
, 0},
757 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX
*cctx
, const char **pcmd
)
759 if (pcmd
== NULL
|| *pcmd
== NULL
)
761 /* If a prefix is set, check and skip */
763 if (strlen(*pcmd
) <= cctx
->prefixlen
)
765 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
&&
766 strncmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
768 if (cctx
->flags
& SSL_CONF_FLAG_FILE
&&
769 strncasecmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
771 *pcmd
+= cctx
->prefixlen
;
772 } else if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
773 if (**pcmd
!= '-' || !(*pcmd
)[1])
780 /* Determine if a command is allowed according to cctx flags */
781 static int ssl_conf_cmd_allowed(SSL_CONF_CTX
*cctx
, const ssl_conf_cmd_tbl
* t
)
783 unsigned int tfl
= t
->flags
;
784 unsigned int cfl
= cctx
->flags
;
785 if ((tfl
& SSL_CONF_FLAG_SERVER
) && !(cfl
& SSL_CONF_FLAG_SERVER
))
787 if ((tfl
& SSL_CONF_FLAG_CLIENT
) && !(cfl
& SSL_CONF_FLAG_CLIENT
))
789 if ((tfl
& SSL_CONF_FLAG_CERTIFICATE
)
790 && !(cfl
& SSL_CONF_FLAG_CERTIFICATE
))
795 static const ssl_conf_cmd_tbl
*ssl_conf_cmd_lookup(SSL_CONF_CTX
*cctx
,
798 const ssl_conf_cmd_tbl
*t
;
803 /* Look for matching parameter name in table */
804 for (i
= 0, t
= ssl_conf_cmds
; i
< OSSL_NELEM(ssl_conf_cmds
); i
++, t
++) {
805 if (ssl_conf_cmd_allowed(cctx
, t
)) {
806 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
807 if (t
->str_cmdline
&& strcmp(t
->str_cmdline
, cmd
) == 0)
810 if (cctx
->flags
& SSL_CONF_FLAG_FILE
) {
811 if (t
->str_file
&& strcasecmp(t
->str_file
, cmd
) == 0)
819 static int ctrl_switch_option(SSL_CONF_CTX
*cctx
, const ssl_conf_cmd_tbl
* cmd
)
821 /* Find index of command in table */
822 size_t idx
= cmd
- ssl_conf_cmds
;
823 const ssl_switch_tbl
*scmd
;
824 /* Sanity check index */
825 if (idx
>= OSSL_NELEM(ssl_cmd_switches
))
827 /* Obtain switches entry with same index */
828 scmd
= ssl_cmd_switches
+ idx
;
829 ssl_set_option(cctx
, scmd
->name_flags
, scmd
->option_value
, 1);
833 int SSL_CONF_cmd(SSL_CONF_CTX
*cctx
, const char *cmd
, const char *value
)
835 const ssl_conf_cmd_tbl
*runcmd
;
837 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_INVALID_NULL_CMD_NAME
);
841 if (!ssl_conf_cmd_skip_prefix(cctx
, &cmd
))
844 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
848 if (runcmd
->value_type
== SSL_CONF_TYPE_NONE
) {
849 return ctrl_switch_option(cctx
, runcmd
);
853 rv
= runcmd
->cmd(cctx
, value
);
858 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
859 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_BAD_VALUE
);
860 ERR_add_error_data(4, "cmd=", cmd
, ", value=", value
);
865 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
866 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_UNKNOWN_CMD_NAME
);
867 ERR_add_error_data(2, "cmd=", cmd
);
873 int SSL_CONF_cmd_argv(SSL_CONF_CTX
*cctx
, int *pargc
, char ***pargv
)
876 const char *arg
= NULL
, *argn
;
878 if (pargc
!= NULL
&& *pargc
== 0)
880 if (pargc
== NULL
|| *pargc
> 0)
884 if (pargc
== NULL
|| *pargc
> 1)
888 cctx
->flags
&= ~SSL_CONF_FLAG_FILE
;
889 cctx
->flags
|= SSL_CONF_FLAG_CMDLINE
;
890 rv
= SSL_CONF_cmd(cctx
, arg
, argn
);
892 /* Success: update pargc, pargv */
898 /* Unknown switch: indicate no arguments processed */
901 /* Some error occurred processing command, return fatal error */
907 int SSL_CONF_cmd_value_type(SSL_CONF_CTX
*cctx
, const char *cmd
)
909 if (ssl_conf_cmd_skip_prefix(cctx
, &cmd
)) {
910 const ssl_conf_cmd_tbl
*runcmd
;
911 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
913 return runcmd
->value_type
;
915 return SSL_CONF_TYPE_UNKNOWN
;
918 SSL_CONF_CTX
*SSL_CONF_CTX_new(void)
920 SSL_CONF_CTX
*ret
= OPENSSL_zalloc(sizeof(*ret
));
925 int SSL_CONF_CTX_finish(SSL_CONF_CTX
*cctx
)
927 /* See if any certificates are missing private keys */
934 if (c
&& cctx
->flags
& SSL_CONF_FLAG_REQUIRE_PRIVATE
) {
935 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
936 const char *p
= cctx
->cert_filename
[i
];
938 * If missing private key try to load one from certificate file
940 if (p
&& !c
->pkeys
[i
].privatekey
) {
941 if (!cmd_PrivateKey(cctx
, p
))
948 SSL_set0_CA_list(cctx
->ssl
, cctx
->canames
);
950 SSL_CTX_set0_CA_list(cctx
->ctx
, cctx
->canames
);
952 sk_X509_NAME_pop_free(cctx
->canames
, X509_NAME_free
);
953 cctx
->canames
= NULL
;
958 void SSL_CONF_CTX_free(SSL_CONF_CTX
*cctx
)
962 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
963 OPENSSL_free(cctx
->cert_filename
[i
]);
964 OPENSSL_free(cctx
->prefix
);
965 sk_X509_NAME_pop_free(cctx
->canames
, X509_NAME_free
);
970 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
972 cctx
->flags
|= flags
;
976 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
978 cctx
->flags
&= ~flags
;
982 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX
*cctx
, const char *pre
)
986 tmp
= OPENSSL_strdup(pre
);
990 OPENSSL_free(cctx
->prefix
);
993 cctx
->prefixlen
= strlen(tmp
);
999 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX
*cctx
, SSL
*ssl
)
1004 cctx
->poptions
= &ssl
->options
;
1005 cctx
->min_version
= &ssl
->min_proto_version
;
1006 cctx
->max_version
= &ssl
->max_proto_version
;
1007 cctx
->pcert_flags
= &ssl
->cert
->cert_flags
;
1008 cctx
->pvfy_flags
= &ssl
->verify_mode
;
1010 cctx
->poptions
= NULL
;
1011 cctx
->min_version
= NULL
;
1012 cctx
->max_version
= NULL
;
1013 cctx
->pcert_flags
= NULL
;
1014 cctx
->pvfy_flags
= NULL
;
1018 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX
*cctx
, SSL_CTX
*ctx
)
1023 cctx
->poptions
= &ctx
->options
;
1024 cctx
->min_version
= &ctx
->min_proto_version
;
1025 cctx
->max_version
= &ctx
->max_proto_version
;
1026 cctx
->pcert_flags
= &ctx
->cert
->cert_flags
;
1027 cctx
->pvfy_flags
= &ctx
->verify_mode
;
1029 cctx
->poptions
= NULL
;
1030 cctx
->min_version
= NULL
;
1031 cctx
->max_version
= NULL
;
1032 cctx
->pcert_flags
= NULL
;
1033 cctx
->pvfy_flags
= NULL
;