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
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)
230 /* Ignore values supported by 1.0.2 for the automatic selection */
231 if ((cctx->flags & SSL_CONF_FLAG_FILE)
232 && (strcasecmp(value, "+automatic") == 0
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);
246 rv = SSL_CTX_set1_groups(cctx->ctx, &nid, 1);
248 rv = SSL_set1_groups(cctx->ssl, &nid, 1);
253 static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
258 rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
260 rv = SSL_set_cipher_list(cctx->ssl, value);
264 static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
269 rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
271 rv = SSL_set_ciphersuites(cctx->ssl, value);
275 static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
277 static const ssl_flag_tbl ssl_protocol_list[] = {
278 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
279 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
280 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
281 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
282 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
283 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
284 SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
285 SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
286 SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
288 cctx->tbl = ssl_protocol_list;
289 cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
290 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
294 * protocol_from_string - converts a protocol version string to a number
296 * Returns -1 on failure or the version on success
298 static int protocol_from_string(const char *value)
300 struct protocol_versions {
304 static const struct protocol_versions versions[] = {
306 {"SSLv3", SSL3_VERSION},
307 {"TLSv1", TLS1_VERSION},
308 {"TLSv1.1", TLS1_1_VERSION},
309 {"TLSv1.2", TLS1_2_VERSION},
310 {"TLSv1.3", TLS1_3_VERSION},
311 {"DTLSv1", DTLS1_VERSION},
312 {"DTLSv1.2", DTLS1_2_VERSION}
315 size_t n = OSSL_NELEM(versions);
317 for (i = 0; i < n; i++)
318 if (strcmp(versions[i].name, value) == 0)
319 return versions[i].version;
323 static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
328 if (cctx->ctx != NULL)
329 method_version = cctx->ctx->method->version;
330 else if (cctx->ssl != NULL)
331 method_version = cctx->ssl->ctx->method->version;
334 if ((new_version = protocol_from_string(value)) < 0)
336 return ssl_set_version_bound(method_version, new_version, bound);
340 * cmd_MinProtocol - Set min protocol version
341 * @cctx: config structure to save settings in
342 * @value: The min protocol version in string form
344 * Returns 1 on success and 0 on failure.
346 static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
348 return min_max_proto(cctx, value, cctx->min_version);
352 * cmd_MaxProtocol - Set max protocol version
353 * @cctx: config structure to save settings in
354 * @value: The max protocol version in string form
356 * Returns 1 on success and 0 on failure.
358 static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
360 return min_max_proto(cctx, value, cctx->max_version);
363 static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
365 static const ssl_flag_tbl ssl_option_list[] = {
366 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
367 SSL_FLAG_TBL_INV("EmptyFragments",
368 SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
369 SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
370 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
371 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
372 SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
373 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
374 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
375 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
376 SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
377 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
378 SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
379 SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
380 SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
381 SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
382 SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
383 SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
384 SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET)
388 cctx->tbl = ssl_option_list;
389 cctx->ntbl = OSSL_NELEM(ssl_option_list);
390 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
393 static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
395 static const ssl_flag_tbl ssl_vfy_list[] = {
396 SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
397 SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
398 SSL_FLAG_VFY_SRV("Require",
399 SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
400 SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
401 SSL_FLAG_VFY_SRV("RequestPostHandshake",
402 SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
403 SSL_FLAG_VFY_SRV("RequirePostHandshake",
404 SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
405 SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
409 cctx->tbl = ssl_vfy_list;
410 cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
411 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
414 static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
419 rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
423 rv = SSL_use_certificate_chain_file(cctx->ssl, value);
426 if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
427 char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
428 OPENSSL_free(*pfilename);
429 *pfilename = OPENSSL_strdup(value);
437 static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
440 if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
443 rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
445 rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
449 static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
453 rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
457 static int do_store(SSL_CONF_CTX *cctx,
458 const char *CAfile, const char *CApath, int verify_store)
463 cert = cctx->ctx->cert;
465 cert = cctx->ssl->cert;
468 st = verify_store ? &cert->verify_store : &cert->chain_store;
470 *st = X509_STORE_new();
474 return X509_STORE_load_locations(*st, CAfile, CApath) > 0;
477 static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
479 return do_store(cctx, NULL, value, 0);
482 static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
484 return do_store(cctx, value, NULL, 0);
487 static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
489 return do_store(cctx, NULL, value, 1);
492 static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
494 return do_store(cctx, value, NULL, 1);
497 static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
499 if (cctx->canames == NULL)
500 cctx->canames = sk_X509_NAME_new_null();
501 if (cctx->canames == NULL)
503 return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
506 static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
508 return cmd_RequestCAFile(cctx, value);
511 static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
513 if (cctx->canames == NULL)
514 cctx->canames = sk_X509_NAME_new_null();
515 if (cctx->canames == NULL)
517 return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
520 static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
522 return cmd_RequestCAPath(cctx, value);
525 #ifndef OPENSSL_NO_DH
526 static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
531 if (cctx->ctx || cctx->ssl) {
532 in = BIO_new(BIO_s_file());
535 if (BIO_read_filename(in, value) <= 0)
537 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
543 rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh);
545 rv = SSL_set_tmp_dh(cctx->ssl, dh);
553 static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
556 int block_size = atoi(value);
559 * All we care about is a non-negative value,
560 * the setters check the range
562 if (block_size >= 0) {
564 rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
566 rv = SSL_set_block_padding(cctx->ssl, block_size);
572 static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
575 int num_tickets = atoi(value);
577 if (num_tickets >= 0) {
579 rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
581 rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
587 int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
588 const char *str_file;
589 const char *str_cmdline;
590 unsigned short flags;
591 unsigned short value_type;
594 /* Table of supported parameters */
596 #define SSL_CONF_CMD(name, cmdopt, flags, type) \
597 {cmd_##name, #name, cmdopt, flags, type}
599 #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
600 SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
602 #define SSL_CONF_CMD_SWITCH(name, flags) \
603 {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
605 /* See apps/apps.h if you change this table. */
606 static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
607 SSL_CONF_CMD_SWITCH("no_ssl3", 0),
608 SSL_CONF_CMD_SWITCH("no_tls1", 0),
609 SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
610 SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
611 SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
612 SSL_CONF_CMD_SWITCH("bugs", 0),
613 SSL_CONF_CMD_SWITCH("no_comp", 0),
614 SSL_CONF_CMD_SWITCH("comp", 0),
615 SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
616 SSL_CONF_CMD_SWITCH("no_ticket", 0),
617 SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
618 SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
619 SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER),
620 SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
621 SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
622 SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER),
623 SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
624 SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
625 SSL_CONF_CMD_SWITCH("strict", 0),
626 SSL_CONF_CMD_SWITCH("no_middlebox", 0),
627 SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
628 SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
629 SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
630 SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
631 SSL_CONF_CMD_STRING(Curves, "curves", 0),
632 SSL_CONF_CMD_STRING(Groups, "groups", 0),
633 #ifndef OPENSSL_NO_EC
634 SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
636 SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
637 SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
638 SSL_CONF_CMD_STRING(Protocol, NULL, 0),
639 SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
640 SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
641 SSL_CONF_CMD_STRING(Options, NULL, 0),
642 SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
643 SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
645 SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
647 SSL_CONF_CMD(ServerInfoFile, NULL,
648 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
650 SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
652 SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
654 SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
656 SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
658 SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
660 SSL_CONF_CMD(ClientCAFile, NULL,
661 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
663 SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
665 SSL_CONF_CMD(ClientCAPath, NULL,
666 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
668 #ifndef OPENSSL_NO_DH
669 SSL_CONF_CMD(DHParameters, "dhparam",
670 SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
673 SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
674 SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
677 /* Supported switches: must match order of switches in ssl_conf_cmds */
678 static const ssl_switch_tbl ssl_cmd_switches[] = {
679 {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */
680 {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */
681 {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */
682 {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */
683 {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */
684 {SSL_OP_ALL, 0}, /* bugs */
685 {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
686 {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
687 {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
688 {SSL_OP_NO_TICKET, 0}, /* no_ticket */
689 {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
690 /* legacy_renegotiation */
691 {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
692 /* legacy_server_connect */
693 {SSL_OP_LEGACY_SERVER_CONNECT, 0},
694 /* no_renegotiation */
695 {SSL_OP_NO_RENEGOTIATION, 0},
696 /* no_resumption_on_reneg */
697 {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
698 /* no_legacy_server_connect */
699 {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
700 /* allow_no_dhe_kex */
701 {SSL_OP_ALLOW_NO_DHE_KEX, 0},
702 /* chacha reprioritization */
703 {SSL_OP_PRIORITIZE_CHACHA, 0},
704 {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
706 {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
708 {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
710 {SSL_OP_NO_ANTI_REPLAY, 0},
713 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
717 /* If a prefix is set, check and skip */
719 if (strlen(*pcmd) <= cctx->prefixlen)
721 if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
722 strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
724 if (cctx->flags & SSL_CONF_FLAG_FILE &&
725 strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
727 *pcmd += cctx->prefixlen;
728 } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
729 if (**pcmd != '-' || !(*pcmd)[1])
736 /* Determine if a command is allowed according to cctx flags */
737 static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t)
739 unsigned int tfl = t->flags;
740 unsigned int cfl = cctx->flags;
741 if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
743 if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
745 if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
746 && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
751 static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
754 const ssl_conf_cmd_tbl *t;
759 /* Look for matching parameter name in table */
760 for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
761 if (ssl_conf_cmd_allowed(cctx, t)) {
762 if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
763 if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
766 if (cctx->flags & SSL_CONF_FLAG_FILE) {
767 if (t->str_file && strcasecmp(t->str_file, cmd) == 0)
775 static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd)
777 /* Find index of command in table */
778 size_t idx = cmd - ssl_conf_cmds;
779 const ssl_switch_tbl *scmd;
780 /* Sanity check index */
781 if (idx >= OSSL_NELEM(ssl_cmd_switches))
783 /* Obtain switches entry with same index */
784 scmd = ssl_cmd_switches + idx;
785 ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
789 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
791 const ssl_conf_cmd_tbl *runcmd;
793 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
797 if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
800 runcmd = ssl_conf_cmd_lookup(cctx, cmd);
804 if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
805 return ctrl_switch_option(cctx, runcmd);
809 rv = runcmd->cmd(cctx, value);
814 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
815 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
816 ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
821 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
822 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
823 ERR_add_error_data(2, "cmd=", cmd);
829 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
832 const char *arg = NULL, *argn;
833 if (pargc && *pargc == 0)
835 if (!pargc || *pargc > 0)
839 if (!pargc || *pargc > 1)
843 cctx->flags &= ~SSL_CONF_FLAG_FILE;
844 cctx->flags |= SSL_CONF_FLAG_CMDLINE;
845 rv = SSL_CONF_cmd(cctx, arg, argn);
847 /* Success: update pargc, pargv */
853 /* Unknown switch: indicate no arguments processed */
856 /* Some error occurred processing command, return fatal error */
862 int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
864 if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
865 const ssl_conf_cmd_tbl *runcmd;
866 runcmd = ssl_conf_cmd_lookup(cctx, cmd);
868 return runcmd->value_type;
870 return SSL_CONF_TYPE_UNKNOWN;
873 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
875 SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
880 int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
882 /* See if any certificates are missing private keys */
889 if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
890 for (i = 0; i < SSL_PKEY_NUM; i++) {
891 const char *p = cctx->cert_filename[i];
893 * If missing private key try to load one from certificate file
895 if (p && !c->pkeys[i].privatekey) {
896 if (!cmd_PrivateKey(cctx, p))
903 SSL_set0_CA_list(cctx->ssl, cctx->canames);
905 SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
907 sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
908 cctx->canames = NULL;
913 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
917 for (i = 0; i < SSL_PKEY_NUM; i++)
918 OPENSSL_free(cctx->cert_filename[i]);
919 OPENSSL_free(cctx->prefix);
920 sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
925 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
927 cctx->flags |= flags;
931 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
933 cctx->flags &= ~flags;
937 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
941 tmp = OPENSSL_strdup(pre);
945 OPENSSL_free(cctx->prefix);
948 cctx->prefixlen = strlen(tmp);
954 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
959 cctx->poptions = &ssl->options;
960 cctx->min_version = &ssl->min_proto_version;
961 cctx->max_version = &ssl->max_proto_version;
962 cctx->pcert_flags = &ssl->cert->cert_flags;
963 cctx->pvfy_flags = &ssl->verify_mode;
965 cctx->poptions = NULL;
966 cctx->min_version = NULL;
967 cctx->max_version = NULL;
968 cctx->pcert_flags = NULL;
969 cctx->pvfy_flags = NULL;
973 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
978 cctx->poptions = &ctx->options;
979 cctx->min_version = &ctx->min_proto_version;
980 cctx->max_version = &ctx->max_proto_version;
981 cctx->pcert_flags = &ctx->cert->cert_flags;
982 cctx->pvfy_flags = &ctx->verify_mode;
984 cctx->poptions = NULL;
985 cctx->min_version = NULL;
986 cctx->max_version = NULL;
987 cctx->pcert_flags = NULL;
988 cctx->pvfy_flags = NULL;