2 * ! \file ssl/ssl_conf.c \brief SSL configuration functions
4 /* ====================================================================
5 * Copyright (c) 2012 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/conf.h>
64 #include <openssl/objects.h>
66 # include <openssl/dh.h>
70 * structure holding name tables. This is used for pemitted elements in lists
71 * such as TLSv1 and single command line switches such as no_tls1
77 unsigned int name_flags
;
78 unsigned long option_value
;
81 /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
82 #define SSL_TFLAG_INV 0x1
83 /* Flags refers to cert_flags not options */
84 #define SSL_TFLAG_CERT 0x2
85 /* Option can only be used for clients */
86 #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
87 /* Option can only be used for servers */
88 #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
89 #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
91 #define SSL_FLAG_TBL(str, flag) \
92 {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
93 #define SSL_FLAG_TBL_SRV(str, flag) \
94 {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
95 #define SSL_FLAG_TBL_CLI(str, flag) \
96 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
97 #define SSL_FLAG_TBL_INV(str, flag) \
98 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
99 #define SSL_FLAG_TBL_SRV_INV(str, flag) \
100 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
101 #define SSL_FLAG_TBL_CERT(str, flag) \
102 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
105 * Opaque structure containing SSL configuration context.
108 struct ssl_conf_ctx_st
{
110 * Various flags indicating (among other things) which options we will
114 /* Prefix and length of commands */
117 /* SSL_CTX or SSL structure to perform operations on */
120 /* Pointer to SSL or SSL_CTX options field or NULL if none */
121 unsigned long *poptions
;
122 /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
123 unsigned int *pcert_flags
;
124 /* Current flag table being worked on */
125 const ssl_flag_tbl
*tbl
;
130 static int ssl_match_option(SSL_CONF_CTX
*cctx
, const ssl_flag_tbl
*tbl
,
131 const char *name
, int namelen
, int onoff
)
133 /* If name not relevant for context skip */
134 if (!(cctx
->flags
& tbl
->name_flags
& SSL_TFLAG_BOTH
))
137 if (strcmp(tbl
->name
, name
))
139 } else if (tbl
->namelen
!= namelen
140 || strncasecmp(tbl
->name
, name
, namelen
))
142 if (cctx
->poptions
) {
143 if (tbl
->name_flags
& SSL_TFLAG_INV
)
145 if (tbl
->name_flags
& SSL_TFLAG_CERT
) {
147 *cctx
->pcert_flags
|= tbl
->option_value
;
149 *cctx
->pcert_flags
&= ~tbl
->option_value
;
152 *cctx
->poptions
|= tbl
->option_value
;
154 *cctx
->poptions
&= ~tbl
->option_value
;
160 static int ssl_set_option_list(const char *elem
, int len
, void *usr
)
162 SSL_CONF_CTX
*cctx
= usr
;
164 const ssl_flag_tbl
*tbl
;
167 * len == -1 indicates not being called in list context, just for single
168 * command line switches, so don't allow +, -.
177 } else if (*elem
== '-') {
183 for (i
= 0, tbl
= cctx
->tbl
; i
< cctx
->ntbl
; i
++, tbl
++) {
184 if (ssl_match_option(cctx
, tbl
, elem
, len
, onoff
))
190 /* Single command line switches with no argument e.g. -no_ssl3 */
191 static int ctrl_str_option(SSL_CONF_CTX
*cctx
, const char *cmd
)
193 static const ssl_flag_tbl ssl_option_single
[] = {
194 SSL_FLAG_TBL("no_ssl2", SSL_OP_NO_SSLv2
),
195 SSL_FLAG_TBL("no_ssl3", SSL_OP_NO_SSLv3
),
196 SSL_FLAG_TBL("no_tls1", SSL_OP_NO_TLSv1
),
197 SSL_FLAG_TBL("no_tls1_1", SSL_OP_NO_TLSv1_1
),
198 SSL_FLAG_TBL("no_tls1_2", SSL_OP_NO_TLSv1_2
),
199 SSL_FLAG_TBL("bugs", SSL_OP_ALL
),
200 SSL_FLAG_TBL("no_comp", SSL_OP_NO_COMPRESSION
),
201 SSL_FLAG_TBL_SRV("ecdh_single", SSL_OP_SINGLE_ECDH_USE
),
202 #ifndef OPENSSL_NO_TLSEXT
203 SSL_FLAG_TBL("no_ticket", SSL_OP_NO_TICKET
),
205 SSL_FLAG_TBL_SRV("serverpref", SSL_OP_CIPHER_SERVER_PREFERENCE
),
206 SSL_FLAG_TBL("legacy_renegotiation",
207 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
),
208 SSL_FLAG_TBL_SRV("legacy_server_connect",
209 SSL_OP_LEGACY_SERVER_CONNECT
),
210 SSL_FLAG_TBL_SRV("no_resumption_on_reneg",
211 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
),
212 SSL_FLAG_TBL_SRV_INV("no_legacy_server_connect",
213 SSL_OP_LEGACY_SERVER_CONNECT
),
214 SSL_FLAG_TBL_CERT("strict", SSL_CERT_FLAG_TLS_STRICT
),
215 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
216 SSL_FLAG_TBL_CERT("debug_broken_protocol",
217 SSL_CERT_FLAG_BROKEN_PROTOCOL
),
220 cctx
->tbl
= ssl_option_single
;
221 cctx
->ntbl
= sizeof(ssl_option_single
) / sizeof(ssl_flag_tbl
);
222 return ssl_set_option_list(cmd
, -1, cctx
);
225 /* Set supported signature algorithms */
226 static int cmd_SignatureAlgorithms(SSL_CONF_CTX
*cctx
, const char *value
)
230 rv
= SSL_set1_sigalgs_list(cctx
->ssl
, value
);
231 /* NB: ctx == NULL performs syntax checking only */
233 rv
= SSL_CTX_set1_sigalgs_list(cctx
->ctx
, value
);
237 /* Set supported client signature algorithms */
238 static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX
*cctx
,
243 rv
= SSL_set1_client_sigalgs_list(cctx
->ssl
, value
);
244 /* NB: ctx == NULL performs syntax checking only */
246 rv
= SSL_CTX_set1_client_sigalgs_list(cctx
->ctx
, value
);
250 static int cmd_Curves(SSL_CONF_CTX
*cctx
, const char *value
)
254 rv
= SSL_set1_curves_list(cctx
->ssl
, value
);
255 /* NB: ctx == NULL performs syntax checking only */
257 rv
= SSL_CTX_set1_curves_list(cctx
->ctx
, value
);
261 #ifndef OPENSSL_NO_ECDH
262 /* ECDH temporary parameters */
263 static int cmd_ECDHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
265 int onoff
= -1, rv
= 1;
266 if (!(cctx
->flags
& SSL_CONF_FLAG_SERVER
))
268 if (cctx
->flags
& SSL_CONF_FLAG_FILE
) {
277 if (!strcasecmp(value
, "automatic")) {
280 } else if (onoff
!= -1)
282 } else if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
283 if (!strcmp(value
, "auto"))
289 rv
= SSL_CTX_set_ecdh_auto(cctx
->ctx
, onoff
);
291 rv
= SSL_set_ecdh_auto(cctx
->ssl
, onoff
);
295 nid
= EC_curve_nist2nid(value
);
296 if (nid
== NID_undef
)
297 nid
= OBJ_sn2nid(value
);
300 ecdh
= EC_KEY_new_by_curve_name(nid
);
304 rv
= SSL_CTX_set_tmp_ecdh(cctx
->ctx
, ecdh
);
306 rv
= SSL_set_tmp_ecdh(cctx
->ssl
, ecdh
);
313 static int cmd_CipherString(SSL_CONF_CTX
*cctx
, const char *value
)
317 rv
= SSL_CTX_set_cipher_list(cctx
->ctx
, value
);
319 rv
= SSL_set_cipher_list(cctx
->ssl
, value
);
323 static int cmd_Protocol(SSL_CONF_CTX
*cctx
, const char *value
)
325 static const ssl_flag_tbl ssl_protocol_list
[] = {
326 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK
),
327 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2
),
328 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3
),
329 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1
),
330 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1
),
331 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2
)
333 if (!(cctx
->flags
& SSL_CONF_FLAG_FILE
))
335 cctx
->tbl
= ssl_protocol_list
;
336 cctx
->ntbl
= sizeof(ssl_protocol_list
) / sizeof(ssl_flag_tbl
);
337 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
340 static int cmd_Options(SSL_CONF_CTX
*cctx
, const char *value
)
342 static const ssl_flag_tbl ssl_option_list
[] = {
343 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET
),
344 SSL_FLAG_TBL_INV("EmptyFragments",
345 SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
),
346 SSL_FLAG_TBL("Bugs", SSL_OP_ALL
),
347 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION
),
348 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE
),
349 SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
350 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
),
351 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE
),
352 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE
),
353 SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
354 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
),
356 if (!(cctx
->flags
& SSL_CONF_FLAG_FILE
))
360 cctx
->tbl
= ssl_option_list
;
361 cctx
->ntbl
= sizeof(ssl_option_list
) / sizeof(ssl_flag_tbl
);
362 return CONF_parse_list(value
, ',', 1, ssl_set_option_list
, cctx
);
365 static int cmd_Certificate(SSL_CONF_CTX
*cctx
, const char *value
)
368 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
371 rv
= SSL_CTX_use_certificate_chain_file(cctx
->ctx
, value
);
373 rv
= SSL_use_certificate_file(cctx
->ssl
, value
, SSL_FILETYPE_PEM
);
377 static int cmd_PrivateKey(SSL_CONF_CTX
*cctx
, const char *value
)
380 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
383 rv
= SSL_CTX_use_PrivateKey_file(cctx
->ctx
, value
, SSL_FILETYPE_PEM
);
385 rv
= SSL_use_PrivateKey_file(cctx
->ssl
, value
, SSL_FILETYPE_PEM
);
389 static int cmd_ServerInfoFile(SSL_CONF_CTX
*cctx
, const char *value
)
392 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
394 if (!(cctx
->flags
& SSL_CONF_FLAG_SERVER
))
397 rv
= SSL_CTX_use_serverinfo_file(cctx
->ctx
, value
);
401 #ifndef OPENSSL_NO_DH
402 static int cmd_DHParameters(SSL_CONF_CTX
*cctx
, const char *value
)
407 if (!(cctx
->flags
& SSL_CONF_FLAG_CERTIFICATE
))
409 if (cctx
->ctx
|| cctx
->ssl
) {
410 in
= BIO_new(BIO_s_file_internal());
413 if (BIO_read_filename(in
, value
) <= 0)
415 dh
= PEM_read_bio_DHparams(in
, NULL
, NULL
, NULL
);
421 rv
= SSL_CTX_set_tmp_dh(cctx
->ctx
, dh
);
423 rv
= SSL_set_tmp_dh(cctx
->ssl
, dh
);
433 int (*cmd
) (SSL_CONF_CTX
*cctx
, const char *value
);
434 const char *str_file
;
435 const char *str_cmdline
;
436 unsigned int value_type
;
439 /* Table of supported parameters */
441 #define SSL_CONF_CMD(name, cmdopt, type) \
442 {cmd_##name, #name, cmdopt, type}
444 #define SSL_CONF_CMD_STRING(name, cmdopt) \
445 SSL_CONF_CMD(name, cmdopt, SSL_CONF_TYPE_STRING)
447 static const ssl_conf_cmd_tbl ssl_conf_cmds
[] = {
448 SSL_CONF_CMD_STRING(SignatureAlgorithms
, "sigalgs"),
449 SSL_CONF_CMD_STRING(ClientSignatureAlgorithms
, "client_sigalgs"),
450 SSL_CONF_CMD_STRING(Curves
, "curves"),
451 #ifndef OPENSSL_NO_ECDH
452 SSL_CONF_CMD_STRING(ECDHParameters
, "named_curve"),
454 SSL_CONF_CMD_STRING(CipherString
, "cipher"),
455 SSL_CONF_CMD_STRING(Protocol
, NULL
),
456 SSL_CONF_CMD_STRING(Options
, NULL
),
457 SSL_CONF_CMD(Certificate
, "cert", SSL_CONF_TYPE_FILE
),
458 SSL_CONF_CMD(PrivateKey
, "key", SSL_CONF_TYPE_FILE
),
459 SSL_CONF_CMD(ServerInfoFile
, NULL
, SSL_CONF_TYPE_FILE
),
460 #ifndef OPENSSL_NO_DH
461 SSL_CONF_CMD(DHParameters
, "dhparam", SSL_CONF_TYPE_FILE
)
465 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX
*cctx
, const char **pcmd
)
469 /* If a prefix is set, check and skip */
471 if (strlen(*pcmd
) <= cctx
->prefixlen
)
473 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
&&
474 strncmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
476 if (cctx
->flags
& SSL_CONF_FLAG_FILE
&&
477 strncasecmp(*pcmd
, cctx
->prefix
, cctx
->prefixlen
))
479 *pcmd
+= cctx
->prefixlen
;
480 } else if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
481 if (**pcmd
!= '-' || !(*pcmd
)[1])
488 static const ssl_conf_cmd_tbl
*ssl_conf_cmd_lookup(SSL_CONF_CTX
*cctx
,
491 const ssl_conf_cmd_tbl
*t
;
496 /* Look for matching parameter name in table */
497 for (i
= 0, t
= ssl_conf_cmds
;
498 i
< sizeof(ssl_conf_cmds
) / sizeof(ssl_conf_cmd_tbl
); i
++, t
++) {
499 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
500 if (t
->str_cmdline
&& !strcmp(t
->str_cmdline
, cmd
))
503 if (cctx
->flags
& SSL_CONF_FLAG_FILE
) {
504 if (t
->str_file
&& !strcasecmp(t
->str_file
, cmd
))
511 int SSL_CONF_cmd(SSL_CONF_CTX
*cctx
, const char *cmd
, const char *value
)
513 const ssl_conf_cmd_tbl
*runcmd
;
515 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_INVALID_NULL_CMD_NAME
);
519 if (!ssl_conf_cmd_skip_prefix(cctx
, &cmd
))
522 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
528 rv
= runcmd
->cmd(cctx
, value
);
533 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
534 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_BAD_VALUE
);
535 ERR_add_error_data(4, "cmd=", cmd
, ", value=", value
);
540 if (cctx
->flags
& SSL_CONF_FLAG_CMDLINE
) {
541 if (ctrl_str_option(cctx
, cmd
))
545 if (cctx
->flags
& SSL_CONF_FLAG_SHOW_ERRORS
) {
546 SSLerr(SSL_F_SSL_CONF_CMD
, SSL_R_UNKNOWN_CMD_NAME
);
547 ERR_add_error_data(2, "cmd=", cmd
);
553 int SSL_CONF_cmd_argv(SSL_CONF_CTX
*cctx
, int *pargc
, char ***pargv
)
556 const char *arg
= NULL
, *argn
;
557 if (pargc
&& *pargc
== 0)
559 if (!pargc
|| *pargc
> 0)
563 if (!pargc
|| *pargc
> 1)
567 cctx
->flags
&= ~SSL_CONF_FLAG_FILE
;
568 cctx
->flags
|= SSL_CONF_FLAG_CMDLINE
;
569 rv
= SSL_CONF_cmd(cctx
, arg
, argn
);
571 /* Success: update pargc, pargv */
577 /* Unknown switch: indicate no arguments processed */
580 /* Some error occurred processing command, return fatal error */
586 int SSL_CONF_cmd_value_type(SSL_CONF_CTX
*cctx
, const char *cmd
)
588 if (ssl_conf_cmd_skip_prefix(cctx
, &cmd
)) {
589 const ssl_conf_cmd_tbl
*runcmd
;
590 runcmd
= ssl_conf_cmd_lookup(cctx
, cmd
);
592 return runcmd
->value_type
;
594 return SSL_CONF_TYPE_UNKNOWN
;
597 SSL_CONF_CTX
*SSL_CONF_CTX_new(void)
600 ret
= OPENSSL_malloc(sizeof(SSL_CONF_CTX
));
607 ret
->poptions
= NULL
;
608 ret
->pcert_flags
= NULL
;
615 int SSL_CONF_CTX_finish(SSL_CONF_CTX
*cctx
)
620 void SSL_CONF_CTX_free(SSL_CONF_CTX
*cctx
)
624 OPENSSL_free(cctx
->prefix
);
629 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
631 cctx
->flags
|= flags
;
635 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX
*cctx
, unsigned int flags
)
637 cctx
->flags
&= ~flags
;
641 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX
*cctx
, const char *pre
)
645 tmp
= BUF_strdup(pre
);
650 OPENSSL_free(cctx
->prefix
);
653 cctx
->prefixlen
= strlen(tmp
);
659 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX
*cctx
, SSL
*ssl
)
664 cctx
->poptions
= &ssl
->options
;
665 cctx
->pcert_flags
= &ssl
->cert
->cert_flags
;
667 cctx
->poptions
= NULL
;
668 cctx
->pcert_flags
= NULL
;
672 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX
*cctx
, SSL_CTX
*ctx
)
677 cctx
->poptions
= &ctx
->options
;
678 cctx
->pcert_flags
= &ctx
->cert
->cert_flags
;
680 cctx
->poptions
= NULL
;
681 cctx
->pcert_flags
= NULL
;