2 * Copyright 2012-2016 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>
17 * structure holding name tables. This is used for permitted elements in lists
24 unsigned int name_flags
;
25 unsigned long option_value
;
28 /* Switch table: use for single command line switches like no_tls2 */
30 unsigned long option_value
;
31 unsigned int name_flags
;
34 /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
35 #define SSL_TFLAG_INV 0x1
36 /* Mask for type of flag referred to */
37 #define SSL_TFLAG_TYPE_MASK 0xf00
38 /* Flag is for options */
39 #define SSL_TFLAG_OPTION 0x000
40 /* Flag is for cert_flags */
41 #define SSL_TFLAG_CERT 0x100
42 /* Flag is for verify mode */
43 #define SSL_TFLAG_VFY 0x200
44 /* Option can only be used for clients */
45 #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
46 /* Option can only be used for servers */
47 #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
48 #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
50 #define SSL_FLAG_TBL(str, flag) \
51 {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
52 #define SSL_FLAG_TBL_SRV(str, flag) \
53 {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
54 #define SSL_FLAG_TBL_CLI(str, flag) \
55 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
56 #define SSL_FLAG_TBL_INV(str, flag) \
57 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
58 #define SSL_FLAG_TBL_SRV_INV(str, flag) \
59 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
60 #define SSL_FLAG_TBL_CERT(str, flag) \
61 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
63 #define SSL_FLAG_VFY_CLI(str, flag) \
64 {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
65 #define SSL_FLAG_VFY_SRV(str, flag) \
66 {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
69 * Opaque structure containing SSL configuration context.
72 struct ssl_conf_ctx_st
{
74 * Various flags indicating (among other things) which options we will
78 /* Prefix and length of commands */
81 /* SSL_CTX or SSL structure to perform operations on */
84 /* Pointer to SSL or SSL_CTX options field or NULL if none */
86 /* Certificate filenames for each type */
87 char *cert_filename
[SSL_PKEY_NUM
];
88 /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
89 uint32_t *pcert_flags
;
90 /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
92 /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
94 /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
96 /* Current flag table being worked on */
97 const ssl_flag_tbl
*tbl
;
100 /* Client CA names */
101 STACK_OF(X509_NAME
) *canames
;
104 static void ssl_set_option(SSL_CONF_CTX
*cctx
, unsigned int name_flags
,
105 unsigned long option_value
, int onoff
)
108 if (cctx
->poptions
== NULL
)
110 if (name_flags
& SSL_TFLAG_INV
)
112 switch (name_flags
& SSL_TFLAG_TYPE_MASK
) {
115 pflags
= cctx
->pcert_flags
;
119 pflags
= cctx
->pvfy_flags
;
122 case SSL_TFLAG_OPTION
:
123 pflags
= cctx
->poptions
;
131 *pflags
|= option_value
;
133 *pflags
&= ~option_value
;
136 static int ssl_match_option(SSL_CONF_CTX
*cctx
, const ssl_flag_tbl
*tbl
,
137 const char *name
, int namelen
, int onoff
)
139 /* If name not relevant for context skip */
140 if (!(cctx
->flags
& tbl
->name_flags
& SSL_TFLAG_BOTH
))
143 if (strcmp(tbl
->name
, name
))
145 } else if (tbl
->namelen
!= namelen
|| strncasecmp(tbl
->name
, name
, namelen
))
147 ssl_set_option(cctx
, tbl
->name_flags
, tbl
->option_value
, onoff
);
151 static int ssl_set_option_list(const char *elem
, int len
, void *usr
)
153 SSL_CONF_CTX
*cctx
= usr
;
155 const ssl_flag_tbl
*tbl
;
158 * len == -1 indicates not being called in list context, just for single
159 * command line switches, so don't allow +, -.
168 } else if (*elem
== '-') {
174 for (i
= 0, tbl
= cctx
->tbl
; i
< cctx
->ntbl
; i
++, tbl
++) {
175 if (ssl_match_option(cctx
, tbl
, elem
, len
, onoff
))
181 /* Set supported signature algorithms */
182 static int cmd_SignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
186 rv
= SSL_set1_sigalgs_list(cctx
->ssl
, value
);
187 /* NB: ctx == NULL performs syntax checking only */
189 rv
= SSL_CTX_set1_sigalgs_list(cctx
->ctx
, value
);
193 /* Set supported client signature algorithms */
194 static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
198 rv
= SSL_set1_client_sigalgs_list(cctx
->ssl
, value
);
199 /* NB: ctx == NULL performs syntax checking only */
201 rv
= SSL_CTX_set1_client_sigalgs_list(cctx
->ctx
, value
);
205 static int cmd_Groups(SSL_CONF_CTX
*cctx
, const char *value
)
209 rv
= SSL_set1_groups_list(cctx
->ssl
, value
);
210 /* NB: ctx == NULL performs syntax checking only */
212 rv
= SSL_CTX_set1_groups_list(cctx
->ctx
, value
);
216 /* This is the old name for cmd_Groups - retained for backwards compatibility */
217 static int cmd_Curves(SSL_CONF_CTX
*cctx
, const char *value
)
219 return cmd_Groups(cctx
, value
);
222 #ifndef OPENSSL_NO_EC
223 /* ECDH temporary parameters */
224 static int cmd_ECDHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
230 nid
= EC_curve_nist2nid(value
);
231 if (nid
== NID_undef
)
232 nid
= OBJ_sn2nid(value
);
235 ecdh
= EC_KEY_new_by_curve_name(nid
);
239 rv
= SSL_CTX_set_tmp_ecdh(cctx
->ctx
, ecdh
);
241 rv
= SSL_set_tmp_ecdh(cctx
->ssl
, ecdh
);
247 static int cmd_CipherString(SSL_CONF_CTX
*cctx
, const char *value
)
251 rv
= SSL_CTX_set_cipher_list(cctx
->ctx
, value
);
253 rv
= SSL_set_cipher_list(cctx
->ssl
, value
);
257 static int cmd_Protocol(SSL_CONF_CTX
*cctx
, const char *value
)
259 static const ssl_flag_tbl ssl_protocol_list
[] = {
260 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK
),
261 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2
),
262 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3
),
263 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1
),
264 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1
),
265 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2
),
266 SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3
),
267 SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1
),
268 SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2
)
270 cctx
->tbl
= ssl_protocol_list
;
271 cctx
->ntbl
= OSSL_NELEM(ssl_protocol_list
);
272 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
276 * protocol_from_string - converts a protocol version string to a number
278 * Returns -1 on failure or the version on success
280 static int protocol_from_string(const char *value
)
282 struct protocol_versions
{
286 static const struct protocol_versions versions
[] = {
288 {"SSLv3", SSL3_VERSION
},
289 {"TLSv1", TLS1_VERSION
},
290 {"TLSv1.1", TLS1_1_VERSION
},
291 {"TLSv1.2", TLS1_2_VERSION
},
292 {"TLSv1.3", TLS1_3_VERSION
},
293 {"DTLSv1", DTLS1_VERSION
},
294 {"DTLSv1.2", DTLS1_2_VERSION
}
297 size_t n
= OSSL_NELEM(versions
);
299 for (i
= 0; i
< n
; i
++)
300 if (strcmp(versions
[i
].name
, value
) == 0)
301 return versions
[i
].version
;
305 static int min_max_proto(SSL_CONF_CTX
*cctx
, const char *value
, int *bound
)
310 if (cctx
->ctx
!= NULL
)
311 method_version
= cctx
->ctx
->method
->version
;
312 else if (cctx
->ssl
!= NULL
)
313 method_version
= cctx
->ssl
->ctx
->method
->version
;
316 if ((new_version
= protocol_from_string(value
)) < 0)
318 return ssl_set_version_bound(method_version
, new_version
, bound
);
322 * cmd_MinProtocol - Set min protocol version
323 * @cctx: config structure to save settings in
324 * @value: The min protocol version in string form
326 * Returns 1 on success and 0 on failure.
328 static int cmd_MinProtocol(SSL_CONF_CTX
*cctx
, const char *value
)
330 return min_max_proto(cctx
, value
, cctx
->min_version
);
334 * cmd_MaxProtocol - Set max protocol version
335 * @cctx: config structure to save settings in
336 * @value: The max protocol version in string form
338 * Returns 1 on success and 0 on failure.
340 static int cmd_MaxProtocol(SSL_CONF_CTX
*cctx
, const char *value
)
342 return min_max_proto(cctx
, value
, cctx
->max_version
);
345 static int cmd_Options(SSL_CONF_CTX
*cctx
, const char *value
)
347 static const ssl_flag_tbl ssl_option_list
[] = {
348 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET
),
349 SSL_FLAG_TBL_INV("EmptyFragments",
350 SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
),
351 SSL_FLAG_TBL("Bugs", SSL_OP_ALL
),
352 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION
),
353 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE
),
354 SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
355 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
),
356 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE
),
357 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE
),
358 SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
359 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
),
360 SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC
),
364 cctx
->tbl
= ssl_option_list
;
365 cctx
->ntbl
= OSSL_NELEM(ssl_option_list
);
366 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
369 static int cmd_VerifyMode(SSL_CONF_CTX
*cctx
, const char *value
)
371 static const ssl_flag_tbl ssl_vfy_list
[] = {
372 SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER
),
373 SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER
),
374 SSL_FLAG_VFY_SRV("Require",
375 SSL_VERIFY_PEER
| SSL_VERIFY_FAIL_IF_NO_PEER_CERT
),
376 SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER
| SSL_VERIFY_CLIENT_ONCE
)
380 cctx
->tbl
= ssl_vfy_list
;
381 cctx
->ntbl
= OSSL_NELEM(ssl_vfy_list
);
382 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
385 static int cmd_Certificate(SSL_CONF_CTX
*cctx
, const char *value
)
390 rv
= SSL_CTX_use_certificate_chain_file(cctx
->ctx
, value
);
394 rv
= SSL_use_certificate_chain_file(cctx
->ssl
, value
);
397 if (rv
> 0 && c
&& cctx
->flags
& SSL_CONF_FLAG_REQUIRE_PRIVATE
) {
398 char **pfilename
= &cctx
->cert_filename
[c
->key
- c
->pkeys
];
399 OPENSSL_free(*pfilename
);
400 *pfilename
= OPENSSL_strdup(value
);
408 static int cmd_PrivateKey(SSL_CONF_CTX
*cctx
, const char *value
)
411 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
414 rv
= SSL_CTX_use_PrivateKey_file(cctx
->ctx
, value
, SSL_FILETYPE_PEM
);
416 rv
= SSL_use_PrivateKey_file(cctx
->ssl
, value
, SSL_FILETYPE_PEM
);
420 static int cmd_ServerInfoFile(SSL_CONF_CTX
*cctx
, const char *value
)
424 rv
= SSL_CTX_use_serverinfo_file(cctx
->ctx
, value
);
428 static int do_store(SSL_CONF_CTX
*cctx
,
429 const char *CAfile
, const char *CApath
, int verify_store
)
434 cert
= cctx
->ctx
->cert
;
436 cert
= cctx
->ssl
->cert
;
439 st
= verify_store
? &cert
->verify_store
: &cert
->chain_store
;
441 *st
= X509_STORE_new();
445 return X509_STORE_load_locations(*st
, CAfile
, CApath
) > 0;
448 static int cmd_ChainCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
450 return do_store(cctx
, NULL
, value
, 0);
453 static int cmd_ChainCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
455 return do_store(cctx
, value
, NULL
, 0);
458 static int cmd_VerifyCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
460 return do_store(cctx
, NULL
, value
, 1);
463 static int cmd_VerifyCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
465 return do_store(cctx
, value
, NULL
, 1);
468 static int cmd_ClientCAFile(SSL_CONF_CTX
*cctx
, const char *value
)
470 if (cctx
->canames
== NULL
)
471 cctx
->canames
= sk_X509_NAME_new_null();
472 if (cctx
->canames
== NULL
)
474 return SSL_add_file_cert_subjects_to_stack(cctx
->canames
, value
);
477 static int cmd_ClientCAPath(SSL_CONF_CTX
*cctx
, const char *value
)
479 if (cctx
->canames
== NULL
)
480 cctx
->canames
= sk_X509_NAME_new_null();
481 if (cctx
->canames
== NULL
)
483 return SSL_add_dir_cert_subjects_to_stack(cctx
->canames
, value
);
486 #ifndef OPENSSL_NO_DH
487 static int cmd_DHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
492 if (cctx
->ctx
|| cctx
->ssl
) {
493 in
= BIO_new(BIO_s_file());
496 if (BIO_read_filename(in
, value
) <= 0)
498 dh
= PEM_read_bio_DHparams(in
, NULL
, NULL
, NULL
);
504 rv
= SSL_CTX_set_tmp_dh(cctx
->ctx
, dh
);
506 rv
= SSL_set_tmp_dh(cctx
->ssl
, dh
);
514 int (*cmd
) (SSL_CONF_CTX
*cctx
, const char *value
);
515 const char *str_file
;
516 const char *str_cmdline
;
517 unsigned short flags
;
518 unsigned short value_type
;
521 /* Table of supported parameters */
523 #define SSL_CONF_CMD(name, cmdopt, flags, type) \
524 {cmd_##name, #name, cmdopt, flags, type}
526 #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
527 SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
529 #define SSL_CONF_CMD_SWITCH(name, flags) \
530 {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
532 /* See apps/apps.h if you change this table. */
533 static const ssl_conf_cmd_tbl ssl_conf_cmds
[] = {
534 SSL_CONF_CMD_SWITCH("no_ssl3", 0),
535 SSL_CONF_CMD_SWITCH("no_tls1", 0),
536 SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
537 SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
538 SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
539 SSL_CONF_CMD_SWITCH("bugs", 0),
540 SSL_CONF_CMD_SWITCH("no_comp", 0),
541 SSL_CONF_CMD_SWITCH("comp", 0),
542 SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER
),
543 SSL_CONF_CMD_SWITCH("no_ticket", 0),
544 SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER
),
545 SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
546 SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER
),
547 SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER
),
548 SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER
),
549 SSL_CONF_CMD_SWITCH("strict", 0),
550 SSL_CONF_CMD_STRING(SignatureAlgorithms
, "sigalgs", 0),
551 SSL_CONF_CMD_STRING(ClientSignatureAlgorithms
, "client_sigalgs", 0),
552 SSL_CONF_CMD_STRING(Curves
, "curves", 0),
553 SSL_CONF_CMD_STRING(Groups
, "groups", 0),
554 #ifndef OPENSSL_NO_EC
555 SSL_CONF_CMD_STRING(ECDHParameters
, "named_curve", SSL_CONF_FLAG_SERVER
),
557 SSL_CONF_CMD_STRING(CipherString
, "cipher", 0),
558 SSL_CONF_CMD_STRING(Protocol
, NULL
, 0),
559 SSL_CONF_CMD_STRING(MinProtocol
, "min_protocol", 0),
560 SSL_CONF_CMD_STRING(MaxProtocol
, "max_protocol", 0),
561 SSL_CONF_CMD_STRING(Options
, NULL
, 0),
562 SSL_CONF_CMD_STRING(VerifyMode
, NULL
, 0),
563 SSL_CONF_CMD(Certificate
, "cert", SSL_CONF_FLAG_CERTIFICATE
,
565 SSL_CONF_CMD(PrivateKey
, "key", SSL_CONF_FLAG_CERTIFICATE
,
567 SSL_CONF_CMD(ServerInfoFile
, NULL
,
568 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
570 SSL_CONF_CMD(ChainCAPath
, "chainCApath", SSL_CONF_FLAG_CERTIFICATE
,
572 SSL_CONF_CMD(ChainCAFile
, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE
,
574 SSL_CONF_CMD(VerifyCAPath
, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE
,
576 SSL_CONF_CMD(VerifyCAFile
, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE
,
578 SSL_CONF_CMD(ClientCAFile
, NULL
,
579 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
581 SSL_CONF_CMD(ClientCAPath
, NULL
,
582 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
584 #ifndef OPENSSL_NO_DH
585 SSL_CONF_CMD(DHParameters
, "dhparam",
586 SSL_CONF_FLAG_SERVER
| SSL_CONF_FLAG_CERTIFICATE
,
591 /* Supported switches: must match order of switches in ssl_conf_cmds */
592 static const ssl_switch_tbl ssl_cmd_switches
[] = {
593 {SSL_OP_NO_SSLv3
, 0}, /* no_ssl3 */
594 {SSL_OP_NO_TLSv1
, 0}, /* no_tls1 */
595 {SSL_OP_NO_TLSv1_1
, 0}, /* no_tls1_1 */
596 {SSL_OP_NO_TLSv1_2
, 0}, /* no_tls1_2 */
597 {SSL_OP_NO_TLSv1_3
, 0}, /* no_tls1_3 */
598 {SSL_OP_ALL
, 0}, /* bugs */
599 {SSL_OP_NO_COMPRESSION
, 0}, /* no_comp */
600 {SSL_OP_NO_COMPRESSION
, SSL_TFLAG_INV
}, /* comp */
601 {SSL_OP_SINGLE_ECDH_USE
, 0}, /* ecdh_single */
602 {SSL_OP_NO_TICKET
, 0}, /* no_ticket */
603 {SSL_OP_CIPHER_SERVER_PREFERENCE
, 0}, /* serverpref */
604 /* legacy_renegotiation */
605 {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
, 0},
606 /* legacy_server_connect */
607 {SSL_OP_LEGACY_SERVER_CONNECT
, 0},
608 /* no_resumption_on_reneg */
609 {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
, 0},
610 /* no_legacy_server_connect */
611 {SSL_OP_LEGACY_SERVER_CONNECT
, SSL_TFLAG_INV
},
612 {SSL_CERT_FLAG_TLS_STRICT
, SSL_TFLAG_CERT
}, /* strict */
615 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX
*cctx
, const char **pcmd
)
619 /* If a prefix is set, check and skip */
621 if (strlen(*pcmd
) <= cctx
->prefixlen
)
623 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
&&
624 strncmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
626 if (cctx
->flags
& SSL_CONF_FLAG_FILE
&&
627 strncasecmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
629 *pcmd
+= cctx
->prefixlen
;
630 } else if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
631 if (**pcmd
!= '-' || !(*pcmd
)[1])
638 /* Determine if a command is allowed according to cctx flags */
639 static int ssl_conf_cmd_allowed(SSL_CONF_CTX
*cctx
, const ssl_conf_cmd_tbl
* t
)
641 unsigned int tfl
= t
->flags
;
642 unsigned int cfl
= cctx
->flags
;
643 if ((tfl
& SSL_CONF_FLAG_SERVER
) && !(cfl
& SSL_CONF_FLAG_SERVER
))
645 if ((tfl
& SSL_CONF_FLAG_CLIENT
) && !(cfl
& SSL_CONF_FLAG_CLIENT
))
647 if ((tfl
& SSL_CONF_FLAG_CERTIFICATE
)
648 && !(cfl
& SSL_CONF_FLAG_CERTIFICATE
))
653 static const ssl_conf_cmd_tbl
*ssl_conf_cmd_lookup(SSL_CONF_CTX
*cctx
,
656 const ssl_conf_cmd_tbl
*t
;
661 /* Look for matching parameter name in table */
662 for (i
= 0, t
= ssl_conf_cmds
; i
< OSSL_NELEM(ssl_conf_cmds
); i
++, t
++) {
663 if (ssl_conf_cmd_allowed(cctx
, t
)) {
664 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
665 if (t
->str_cmdline
&& strcmp(t
->str_cmdline
, cmd
) == 0)
668 if (cctx
->flags
& SSL_CONF_FLAG_FILE
) {
669 if (t
->str_file
&& strcasecmp(t
->str_file
, cmd
) == 0)
677 static int ctrl_switch_option(SSL_CONF_CTX
*cctx
, const ssl_conf_cmd_tbl
* cmd
)
679 /* Find index of command in table */
680 size_t idx
= cmd
- ssl_conf_cmds
;
681 const ssl_switch_tbl
*scmd
;
682 /* Sanity check index */
683 if (idx
>= OSSL_NELEM(ssl_cmd_switches
))
685 /* Obtain switches entry with same index */
686 scmd
= ssl_cmd_switches
+ idx
;
687 ssl_set_option(cctx
, scmd
->name_flags
, scmd
->option_value
, 1);
691 int SSL_CONF_cmd(SSL_CONF_CTX
*cctx
, const char *cmd
, const char *value
)
693 const ssl_conf_cmd_tbl
*runcmd
;
695 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_INVALID_NULL_CMD_NAME
);
699 if (!ssl_conf_cmd_skip_prefix(cctx
, &cmd
))
702 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
706 if (runcmd
->value_type
== SSL_CONF_TYPE_NONE
) {
707 return ctrl_switch_option(cctx
, runcmd
);
711 rv
= runcmd
->cmd(cctx
, value
);
716 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
717 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_BAD_VALUE
);
718 ERR_add_error_data(4, "cmd=", cmd
, ", value=", value
);
723 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
724 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_UNKNOWN_CMD_NAME
);
725 ERR_add_error_data(2, "cmd=", cmd
);
731 int SSL_CONF_cmd_argv(SSL_CONF_CTX
*cctx
, int *pargc
, char ***pargv
)
734 const char *arg
= NULL
, *argn
;
735 if (pargc
&& *pargc
== 0)
737 if (!pargc
|| *pargc
> 0)
741 if (!pargc
|| *pargc
> 1)
745 cctx
->flags
&= ~SSL_CONF_FLAG_FILE
;
746 cctx
->flags
|= SSL_CONF_FLAG_CMDLINE
;
747 rv
= SSL_CONF_cmd(cctx
, arg
, argn
);
749 /* Success: update pargc, pargv */
755 /* Unknown switch: indicate no arguments processed */
758 /* Some error occurred processing command, return fatal error */
764 int SSL_CONF_cmd_value_type(SSL_CONF_CTX
*cctx
, const char *cmd
)
766 if (ssl_conf_cmd_skip_prefix(cctx
, &cmd
)) {
767 const ssl_conf_cmd_tbl
*runcmd
;
768 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
770 return runcmd
->value_type
;
772 return SSL_CONF_TYPE_UNKNOWN
;
775 SSL_CONF_CTX
*SSL_CONF_CTX_new(void)
777 SSL_CONF_CTX
*ret
= OPENSSL_zalloc(sizeof(*ret
));
782 int SSL_CONF_CTX_finish(SSL_CONF_CTX
*cctx
)
784 /* See if any certificates are missing private keys */
791 if (c
&& cctx
->flags
& SSL_CONF_FLAG_REQUIRE_PRIVATE
) {
792 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
793 const char *p
= cctx
->cert_filename
[i
];
795 * If missing private key try to load one from certificate file
797 if (p
&& !c
->pkeys
[i
].privatekey
) {
798 if (!cmd_PrivateKey(cctx
, p
))
805 SSL_set_client_CA_list(cctx
->ssl
, cctx
->canames
);
807 SSL_CTX_set_client_CA_list(cctx
->ctx
, cctx
->canames
);
809 sk_X509_NAME_pop_free(cctx
->canames
, X509_NAME_free
);
810 cctx
->canames
= NULL
;
815 void SSL_CONF_CTX_free(SSL_CONF_CTX
*cctx
)
819 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
820 OPENSSL_free(cctx
->cert_filename
[i
]);
821 OPENSSL_free(cctx
->prefix
);
822 sk_X509_NAME_pop_free(cctx
->canames
, X509_NAME_free
);
827 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
829 cctx
->flags
|= flags
;
833 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
835 cctx
->flags
&= ~flags
;
839 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX
*cctx
, const char *pre
)
843 tmp
= OPENSSL_strdup(pre
);
847 OPENSSL_free(cctx
->prefix
);
850 cctx
->prefixlen
= strlen(tmp
);
856 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX
*cctx
, SSL
*ssl
)
861 cctx
->poptions
= &ssl
->options
;
862 cctx
->min_version
= &ssl
->min_proto_version
;
863 cctx
->max_version
= &ssl
->max_proto_version
;
864 cctx
->pcert_flags
= &ssl
->cert
->cert_flags
;
865 cctx
->pvfy_flags
= &ssl
->verify_mode
;
867 cctx
->poptions
= NULL
;
868 cctx
->min_version
= NULL
;
869 cctx
->max_version
= NULL
;
870 cctx
->pcert_flags
= NULL
;
871 cctx
->pvfy_flags
= NULL
;
875 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX
*cctx
, SSL_CTX
*ctx
)
880 cctx
->poptions
= &ctx
->options
;
881 cctx
->min_version
= &ctx
->min_proto_version
;
882 cctx
->max_version
= &ctx
->max_proto_version
;
883 cctx
->pcert_flags
= &ctx
->cert
->cert_flags
;
884 cctx
->pvfy_flags
= &ctx
->verify_mode
;
886 cctx
->poptions
= NULL
;
887 cctx
->min_version
= NULL
;
888 cctx
->max_version
= NULL
;
889 cctx
->pcert_flags
= NULL
;
890 cctx
->pvfy_flags
= NULL
;