1 /*! \file ssl/ssl_conf.c
2 * \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 /* structure holding name tables. This is used for pemitted elements in
67 * lists such as TLSv1 and single command line switches such as no_tls1
74 unsigned int name_flags;
75 unsigned long option_value;
78 /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
79 #define SSL_TFLAG_INV 0x1
80 /* Flags refers to cert_flags not options */
81 #define SSL_TFLAG_CERT 0x2
82 /* Option can only be used for clients */
83 #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
84 /* Option can only be used for servers */
85 #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
86 #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
88 #define SSL_FLAG_TBL(str, flag) \
89 {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
90 #define SSL_FLAG_TBL_SRV(str, flag) \
91 {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
92 #define SSL_FLAG_TBL_CLI(str, flag) \
93 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
94 #define SSL_FLAG_TBL_INV(str, flag) \
95 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
96 #define SSL_FLAG_TBL_SRV_INV(str, flag) \
97 {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
98 #define SSL_FLAG_TBL_CERT(str, flag) \
99 {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
101 /* Opaque structure containing SSL configuration context.
104 struct ssl_conf_ctx_st
106 /* Various flags indicating (among other things) which options we
110 /* Prefix and length of commands */
113 /* SSL_CTX or SSL structure to perform operations on */
116 /* Pointer to SSL or SSL_CTX options field or NULL if none */
117 unsigned long *poptions;
118 /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
119 unsigned int *pcert_flags;
120 /* Current flag table being worked on */
121 const ssl_flag_tbl *tbl;
126 static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
127 const char *name, int namelen, int onoff)
129 /* If name not relevant for context skip */
130 if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
134 if (strcmp(tbl->name, name))
137 else if (tbl->namelen != namelen || strncasecmp(tbl->name, name, namelen))
141 if (tbl->name_flags & SSL_TFLAG_INV)
143 if (tbl->name_flags & SSL_TFLAG_CERT)
146 *cctx->pcert_flags |= tbl->option_value;
148 *cctx->pcert_flags &= ~tbl->option_value;
153 *cctx->poptions |= tbl->option_value;
155 *cctx->poptions &= ~tbl->option_value;
161 static int ssl_set_option_list(const char *elem, int len, void *usr)
163 SSL_CONF_CTX *cctx = usr;
165 const ssl_flag_tbl *tbl;
167 /* len == -1 indicates not being called in list context, just for
168 * single command line switches, so don't allow +, -.
178 else if (*elem == '-')
185 for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++)
187 if (ssl_match_option(cctx, tbl, elem, len, onoff))
193 /* Single command line switches with no argument e.g. -no_ssl3 */
194 static int ctrl_str_option(SSL_CONF_CTX *cctx, const char *cmd)
196 static const ssl_flag_tbl ssl_option_single[] =
198 SSL_FLAG_TBL("no_ssl2", SSL_OP_NO_SSLv2),
199 SSL_FLAG_TBL("no_ssl3", SSL_OP_NO_SSLv3),
200 SSL_FLAG_TBL("no_tls1", SSL_OP_NO_TLSv1),
201 SSL_FLAG_TBL("no_tls1_1", SSL_OP_NO_TLSv1_1),
202 SSL_FLAG_TBL("no_tls1_2", SSL_OP_NO_TLSv1_2),
203 SSL_FLAG_TBL("no_tls1_2", SSL_OP_NO_TLSv1_2),
204 SSL_FLAG_TBL("bugs", SSL_OP_ALL),
205 SSL_FLAG_TBL("no_comp", SSL_OP_NO_COMPRESSION),
206 SSL_FLAG_TBL_SRV("ecdh_single", SSL_OP_SINGLE_ECDH_USE),
207 #ifndef OPENSSL_NO_TLSEXT
208 SSL_FLAG_TBL("no_ticket", SSL_OP_NO_TICKET),
210 SSL_FLAG_TBL_SRV("serverpref", SSL_OP_CIPHER_SERVER_PREFERENCE),
211 SSL_FLAG_TBL("legacy_renegotiation", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
212 SSL_FLAG_TBL_SRV("legacy_server_connect", SSL_OP_LEGACY_SERVER_CONNECT),
213 SSL_FLAG_TBL_SRV_INV("no_legacy_server_connect", 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", SSL_CERT_FLAG_BROKEN_PROTOCOL),
219 cctx->tbl = ssl_option_single;
220 cctx->ntbl = sizeof(ssl_option_single)/sizeof(ssl_flag_tbl);
221 return ssl_set_option_list(cmd, -1, cctx);
224 /* Set supported signature algorithms */
225 static int cmd_sigalgs(SSL_CONF_CTX *cctx, const char *value)
229 rv = SSL_set1_sigalgs_list(cctx->ssl, value);
230 /* NB: ctx == NULL performs syntax checking only */
232 rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
235 /* Set supported client signature algorithms */
236 static int cmd_client_sigalgs(SSL_CONF_CTX *cctx, const char *value)
240 rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
241 /* NB: ctx == NULL performs syntax checking only */
243 rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
247 static int cmd_curves(SSL_CONF_CTX *cctx, const char *value)
251 rv = SSL_set1_curves_list(cctx->ssl, value);
252 /* NB: ctx == NULL performs syntax checking only */
254 rv = SSL_CTX_set1_curves_list(cctx->ctx, value);
257 #ifndef OPENSSL_NO_ECDH
258 /* ECDH temporary parameters */
259 static int cmd_ecdhparam(SSL_CONF_CTX *cctx, const char *value)
261 int onoff = -1, rv = 1;
262 if (!(cctx->flags & SSL_CONF_FLAG_SERVER))
264 if (cctx->flags & SSL_CONF_FLAG_FILE)
276 if (!strcasecmp(value, "automatic"))
281 else if (onoff != -1)
284 else if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
286 if (!strcmp(value, "auto"))
293 rv = SSL_CTX_set_ecdh_auto(cctx->ctx, onoff);
295 rv = SSL_set_ecdh_auto(cctx->ssl, onoff);
301 nid = EC_curve_nist2nid(value);
302 if (nid == NID_undef)
303 nid = OBJ_sn2nid(value);
306 ecdh = EC_KEY_new_by_curve_name(nid);
310 rv = SSL_CTX_set_tmp_ecdh(cctx->ctx, ecdh);
312 rv = SSL_set_tmp_ecdh(cctx->ssl, ecdh);
319 static int cmd_cipher_list(SSL_CONF_CTX *cctx, const char *value)
323 rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
325 rv = SSL_set_cipher_list(cctx->ssl, value);
329 static int cmd_protocol(SSL_CONF_CTX *cctx, const char *value)
331 static const ssl_flag_tbl ssl_protocol_list[] =
333 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
334 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
335 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
336 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
337 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
338 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2)
340 if (!(cctx->flags & SSL_CONF_FLAG_FILE))
342 cctx->tbl = ssl_protocol_list;
343 cctx->ntbl = sizeof(ssl_protocol_list)/sizeof(ssl_flag_tbl);
344 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
347 static int cmd_options(SSL_CONF_CTX *cctx, const char *value)
349 static const ssl_flag_tbl ssl_option_list[] =
351 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
352 SSL_FLAG_TBL_INV("EmptyFragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
353 SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
354 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
355 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
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", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
360 if (!(cctx->flags & SSL_CONF_FLAG_FILE))
364 cctx->tbl = ssl_option_list;
365 cctx->ntbl = sizeof(ssl_option_list)/sizeof(ssl_flag_tbl);
366 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
371 int (*cmd)(SSL_CONF_CTX *cctx, const char *value);
372 const char *str_file;
373 const char *str_cmdline;
376 /* Table of supported patameters */
378 static ssl_conf_cmd_tbl ssl_conf_cmds[] = {
379 {cmd_sigalgs, "SignatureAlgorithms", "sigalgs"},
380 {cmd_client_sigalgs, "ClientSignatureAlgorithms", "client_sigalgs"},
381 {cmd_curves, "Curves", "curves"},
382 #ifndef OPENSSL_NO_ECDH
383 {cmd_ecdhparam, "ECDHParameters", "named_curve"},
385 {cmd_cipher_list, "CipherString", "cipher"},
386 {cmd_protocol, "Protocol", NULL},
387 {cmd_options, "Options", NULL},
390 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
392 ssl_conf_cmd_tbl *t, *runcmd = NULL;
396 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
399 /* If a prefix is set, check and skip */
402 if (strlen(cmd) <= cctx->prefixlen)
404 if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
405 strncmp(cmd, cctx->prefix, cctx->prefixlen))
407 if (cctx->flags & SSL_CONF_FLAG_FILE &&
408 strncasecmp(cmd, cctx->prefix, cctx->prefixlen))
410 cmd += cctx->prefixlen;
412 else if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
414 if (*cmd != '-' || !cmd[1])
419 /* Look for matching parameter name in table */
420 for (i = 0, t = ssl_conf_cmds;
421 i < sizeof(ssl_conf_cmds)/sizeof(ssl_conf_cmd_tbl); i++, t++)
423 if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
425 if (t->str_cmdline && !strcmp(t->str_cmdline, cmd))
431 if (cctx->flags & SSL_CONF_FLAG_FILE)
433 if (t->str_file && !strcasecmp(t->str_file, cmd))
446 rv = t->cmd(cctx, value);
451 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
453 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
454 ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
459 if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
461 if (ctrl_str_option(cctx, cmd))
465 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
467 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
468 ERR_add_error_data(2, "cmd=", cmd);
474 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
477 const char *arg = NULL, *argn;
478 if (pargc && *pargc == 0)
480 if (!pargc || *pargc > 0)
484 if (!pargc || *pargc > 1)
488 cctx->flags &= ~SSL_CONF_FLAG_FILE;
489 cctx->flags |= SSL_CONF_FLAG_CMDLINE;
490 rv = SSL_CONF_cmd(cctx, arg, argn);
493 /* Success: update pargc, pargv */
499 /* Unknown switch: indicate no arguments processed */
502 /* Some error occurred processing command, return fatal error */
508 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
511 ret = OPENSSL_malloc(sizeof(SSL_CONF_CTX));
519 ret->poptions = NULL;
520 ret->pcert_flags = NULL;
527 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
532 OPENSSL_free(cctx->prefix);
537 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
539 cctx->flags |= flags;
543 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
545 cctx->flags &= ~flags;
549 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
554 tmp = BUF_strdup(pre);
559 OPENSSL_free(cctx->prefix);
562 cctx->prefixlen = strlen(tmp);
568 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
574 cctx->poptions = &ssl->options;
575 cctx->pcert_flags = &ssl->cert->cert_flags;
579 cctx->poptions = NULL;
580 cctx->pcert_flags = NULL;
584 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
590 cctx->poptions = &ctx->options;
591 cctx->pcert_flags = &ctx->cert->cert_flags;
595 cctx->poptions = NULL;
596 cctx->pcert_flags = NULL;