]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/ssl_conf.c
fix typo and warning
[thirdparty/openssl.git] / ssl / ssl_conf.c
1 /*! \file ssl/ssl_conf.c
2 * \brief SSL configuration functions
3 */
4 /* ====================================================================
5 * Copyright (c) 2012 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
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/)"
23 *
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.
28 *
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.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
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 * ====================================================================
51 *
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).
55 *
56 */
57
58 #ifdef REF_CHECK
59 # include <assert.h>
60 #endif
61 #include <stdio.h>
62 #include "ssl_locl.h"
63 #include <openssl/conf.h>
64 #include <openssl/objects.h>
65
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
68 */
69
70 typedef struct
71 {
72 const char *name;
73 int namelen;
74 unsigned int name_flags;
75 unsigned long option_value;
76 } ssl_flag_tbl;
77
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)
87
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}
100
101 /* Opaque structure containing SSL configuration context.
102 */
103
104 struct ssl_conf_ctx_st
105 {
106 /* Various flags indicating (among other things) which options we
107 * will recognise.
108 */
109 unsigned int flags;
110 /* Prefix and length of commands */
111 char *prefix;
112 size_t prefixlen;
113 /* SSL_CTX or SSL structure to perform operations on */
114 SSL_CTX *ctx;
115 SSL *ssl;
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;
122 /* Size of table */
123 size_t ntbl;
124 };
125
126 static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
127 const char *name, int namelen, int onoff)
128 {
129 /* If name not relevant for context skip */
130 if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
131 return 0;
132 if (namelen == -1)
133 {
134 if (strcmp(tbl->name, name))
135 return 0;
136 }
137 else if (tbl->namelen != namelen || strncasecmp(tbl->name, name, namelen))
138 return 0;
139 if (cctx->poptions)
140 {
141 if (tbl->name_flags & SSL_TFLAG_INV)
142 onoff ^= 1;
143 if (tbl->name_flags & SSL_TFLAG_CERT)
144 {
145 if (onoff)
146 *cctx->pcert_flags |= tbl->option_value;
147 else
148 *cctx->pcert_flags &= ~tbl->option_value;
149 }
150 else
151 {
152 if (onoff)
153 *cctx->poptions |= tbl->option_value;
154 else
155 *cctx->poptions &= ~tbl->option_value;
156 }
157 }
158 return 1;
159 }
160
161 static int ssl_set_option_list(const char *elem, int len, void *usr)
162 {
163 SSL_CONF_CTX *cctx = usr;
164 size_t i;
165 const ssl_flag_tbl *tbl;
166 int onoff = 1;
167 /* len == -1 indicates not being called in list context, just for
168 * single command line switches, so don't allow +, -.
169 */
170 if (len != -1)
171 {
172 if (*elem == '+')
173 {
174 elem++;
175 len--;
176 onoff = 1;
177 }
178 else if (*elem == '-')
179 {
180 elem++;
181 len--;
182 onoff = 0;
183 }
184 }
185 for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++)
186 {
187 if (ssl_match_option(cctx, tbl, elem, len, onoff))
188 return 1;
189 }
190 return 0;
191 }
192
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)
195 {
196 static const ssl_flag_tbl ssl_option_single[] =
197 {
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 #ifndef OPENSSL_NO_TLSEXT
207 SSL_FLAG_TBL("no_ticket", SSL_OP_NO_TICKET),
208 #endif
209 SSL_FLAG_TBL_SRV("serverpref", SSL_OP_CIPHER_SERVER_PREFERENCE),
210 SSL_FLAG_TBL("legacy_renegotiation", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
211 SSL_FLAG_TBL_SRV("legacy_server_connect", SSL_OP_LEGACY_SERVER_CONNECT),
212 SSL_FLAG_TBL_SRV_INV("no_legacy_server_connect", SSL_OP_LEGACY_SERVER_CONNECT),
213 SSL_FLAG_TBL_CERT("strict", SSL_CERT_FLAG_TLS_STRICT),
214 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
215 SSL_FLAG_TBL_CERT("debug_broken_protocol", SSL_CERT_FLAG_BROKEN_PROTOCOL),
216 #endif
217 };
218 cctx->tbl = ssl_option_single;
219 cctx->ntbl = sizeof(ssl_option_single)/sizeof(ssl_flag_tbl);
220 return ssl_set_option_list(cmd, -1, cctx);
221 }
222
223 /* Set supported signature algorithms */
224 static int cmd_sigalgs(SSL_CONF_CTX *cctx, const char *value)
225 {
226 int rv;
227 if (cctx->ssl)
228 rv = SSL_set1_sigalgs_list(cctx->ssl, value);
229 /* NB: ctx == NULL performs syntax checking only */
230 else
231 rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
232 return rv > 0;
233 }
234 /* Set supported client signature algorithms */
235 static int cmd_client_sigalgs(SSL_CONF_CTX *cctx, const char *value)
236 {
237 int rv;
238 if (cctx->ssl)
239 rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
240 /* NB: ctx == NULL performs syntax checking only */
241 else
242 rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
243 return rv > 0;
244 }
245
246 static int cmd_curves(SSL_CONF_CTX *cctx, const char *value)
247 {
248 int rv;
249 if (!(cctx->flags & SSL_CONF_FLAG_CLIENT))
250 return -2;
251 if (cctx->ssl)
252 rv = SSL_set1_curves_list(cctx->ssl, value);
253 /* NB: ctx == NULL performs syntax checking only */
254 else
255 rv = SSL_CTX_set1_curves_list(cctx->ctx, value);
256 return rv > 0;
257 }
258
259 /* ECDH temporary parameters */
260 static int cmd_ecdhparam(SSL_CONF_CTX *cctx, const char *value)
261 {
262 int onoff = -1, rv = 1;
263 if (!(cctx->flags & SSL_CONF_FLAG_SERVER))
264 return -2;
265 if (cctx->flags & SSL_CONF_FLAG_FILE)
266 {
267 if (*value == '+')
268 {
269 onoff = 1;
270 value++;
271 }
272 if (*value == '-')
273 {
274 onoff = 0;
275 value++;
276 }
277 if (strcasecmp(value, "automatic"))
278 return 0;
279 }
280 else if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
281 {
282 if (!strcmp(value, "auto"))
283 onoff = 1;
284 }
285
286 if (onoff != -1)
287 {
288 if (cctx->ctx)
289 rv = SSL_CTX_set_ecdh_auto(cctx->ctx, onoff);
290 else if (cctx->ssl)
291 rv = SSL_set_ecdh_auto(cctx->ssl, onoff);
292 }
293 else
294 {
295 EC_KEY *ecdh;
296 int nid;
297 nid = EC_curve_nist2nid(value);
298 if (nid == NID_undef)
299 nid = OBJ_sn2nid(value);
300 if (nid == 0)
301 return 0;
302 ecdh = EC_KEY_new_by_curve_name(nid);
303 if (!ecdh)
304 return 0;
305 if (cctx->ctx)
306 rv = SSL_CTX_set_tmp_ecdh(cctx->ctx, ecdh);
307 else if (cctx->ssl)
308 rv = SSL_set_tmp_ecdh(cctx->ssl, ecdh);
309 EC_KEY_free(ecdh);
310 }
311
312 return rv > 0;
313 }
314
315 static int cmd_cipher_list(SSL_CONF_CTX *cctx, const char *value)
316 {
317 int rv = 1;
318 if (cctx->ctx)
319 rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
320 if (cctx->ssl)
321 rv = SSL_set_cipher_list(cctx->ssl, value);
322 return rv;
323 }
324
325 static int cmd_protocol(SSL_CONF_CTX *cctx, const char *value)
326 {
327 static const ssl_flag_tbl ssl_protocol_list[] =
328 {
329 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
330 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
331 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
332 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
333 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
334 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2)
335 };
336 if (!(cctx->flags & SSL_CONF_FLAG_FILE))
337 return -2;
338 cctx->tbl = ssl_protocol_list;
339 cctx->ntbl = sizeof(ssl_protocol_list)/sizeof(ssl_flag_tbl);
340 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
341 }
342
343 static int cmd_options(SSL_CONF_CTX *cctx, const char *value)
344 {
345 static const ssl_flag_tbl ssl_option_list[] =
346 {
347 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
348 SSL_FLAG_TBL_INV("EmptyFragments", SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
349 SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
350 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
351 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
352 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
353 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
354 SSL_FLAG_TBL("UnsafeLegacyRenegotiation", SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
355 };
356 if (!(cctx->flags & SSL_CONF_FLAG_FILE))
357 return -2;
358 if (value == NULL)
359 return -3;
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);
363 }
364
365 typedef struct
366 {
367 int (*cmd)(SSL_CONF_CTX *cctx, const char *value);
368 const char *str_file;
369 const char *str_cmdline;
370 } ssl_conf_cmd_tbl;
371
372 /* Table of supported patameters */
373
374 static ssl_conf_cmd_tbl ssl_conf_cmds[] = {
375 {cmd_sigalgs, "SignatureAlgorithms", "sigalgs"},
376 {cmd_client_sigalgs, "ClientSignatureAlgorithms", "client_sigalgs"},
377 {cmd_curves, "Curves", "curves"},
378 {cmd_ecdhparam, "ECDHParameters", "named_curve"},
379 {cmd_cipher_list, "CipherString", "cipher"},
380 {cmd_protocol, "Protocol", NULL},
381 {cmd_options, "Options", NULL},
382 };
383
384 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
385 {
386 ssl_conf_cmd_tbl *t, *runcmd = NULL;
387 size_t i;
388 if (cmd == NULL)
389 {
390 SSLerr(SSL_F_SSL_CONF_CTX_CMD, SSL_R_INVALID_NULL_CMD_NAME);
391 return 0;
392 }
393 /* If a prefix is set, check and skip */
394 if (cctx->prefix)
395 {
396 if (strlen(cmd) <= cctx->prefixlen)
397 return -2;
398 if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
399 strncmp(cmd, cctx->prefix, cctx->prefixlen))
400 return -2;
401 if (cctx->flags & SSL_CONF_FLAG_FILE &&
402 strncasecmp(cmd, cctx->prefix, cctx->prefixlen))
403 return -2;
404 cmd += cctx->prefixlen;
405 }
406 else if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
407 {
408 if (*cmd != '-' || !cmd[1])
409 return -2;
410 cmd++;
411 }
412
413 /* Look for matching parameter name in table */
414 for (i = 0, t = ssl_conf_cmds;
415 i < sizeof(ssl_conf_cmds)/sizeof(ssl_conf_cmd_tbl); i++, t++)
416 {
417 if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
418 {
419 if (t->str_cmdline && !strcmp(t->str_cmdline, cmd))
420 {
421 runcmd = t;
422 break;
423 }
424 }
425 if (cctx->flags & SSL_CONF_FLAG_FILE)
426 {
427 if (t->str_file && !strcasecmp(t->str_file, cmd))
428 {
429 runcmd = t;
430 break;
431 }
432 }
433 }
434
435 if (runcmd)
436 {
437 if (value == NULL)
438 return -3;
439 if (t->cmd(cctx, value))
440 return 2;
441 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
442 {
443 SSLerr(SSL_F_SSL_CONF_CTX_CMD, SSL_R_BAD_VALUE);
444 ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
445 }
446 return -1;
447 }
448
449 if (cctx->flags & SSL_CONF_FLAG_CMDLINE)
450 {
451 if (ctrl_str_option(cctx, cmd))
452 return 1;
453 }
454
455 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
456 {
457 SSLerr(SSL_F_SSL_CONF_CTX_CMD, SSL_R_UNKNOWN_CMD_NAME);
458 ERR_add_error_data(2, "cmd=", cmd);
459 }
460
461 return -2;
462 }
463
464 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
465 {
466 int rv;
467 const char *arg = NULL, *argn;
468 if (pargc && *pargc == 0)
469 return 0;
470 if (!pargc || *pargc > 0)
471 arg = **pargv;
472 if (arg == NULL)
473 return 0;
474 if (!pargc || *pargc > 1)
475 argn = (*pargv)[1];
476 else
477 argn = NULL;
478 cctx->flags &= ~SSL_CONF_FLAG_FILE;
479 cctx->flags |= SSL_CONF_FLAG_CMDLINE;
480 rv = SSL_CONF_cmd(cctx, arg, argn);
481 if (rv > 0)
482 {
483 /* Success: update pargc, pargv */
484 (*pargv) += rv;
485 if (pargc)
486 (*pargc) -= rv;
487 return rv;
488 }
489 /* Unknown swicth: indicate no arguments processed */
490 if (rv == -2)
491 return 0;
492 /* Some error occurred processing command, return fatal error */
493 if (rv == 0)
494 return -1;
495 return rv;
496 }
497
498 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
499 {
500 SSL_CONF_CTX *ret;
501 ret = OPENSSL_malloc(sizeof(SSL_CONF_CTX));
502 if (ret)
503 {
504 ret->flags = 0;
505 ret->prefix = NULL;
506 ret->prefixlen = 0;
507 ret->ssl = NULL;
508 ret->ctx = NULL;
509 ret->poptions = NULL;
510 ret->pcert_flags = NULL;
511 ret->tbl = NULL;
512 ret->ntbl = 0;
513 }
514 return ret;
515 }
516
517 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
518 {
519 if (cctx)
520 {
521 if (cctx->prefix)
522 OPENSSL_free(cctx->prefix);
523 OPENSSL_free(cctx);
524 }
525 }
526
527 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
528 {
529 cctx->flags |= flags;
530 return cctx->flags;
531 }
532
533 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
534 {
535 cctx->flags &= ~flags;
536 return cctx->flags;
537 }
538
539 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
540 {
541 char *tmp = NULL;
542 if (pre)
543 {
544 tmp = BUF_strdup(pre);
545 if (tmp == NULL)
546 return 0;
547 }
548 if (cctx->prefix)
549 OPENSSL_free(cctx->prefix);
550 cctx->prefix = tmp;
551 if (tmp)
552 cctx->prefixlen = strlen(tmp);
553 else
554 cctx->prefixlen = 0;
555 return 1;
556 }
557
558 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
559 {
560 cctx->ssl = ssl;
561 cctx->ctx = NULL;
562 if (ssl)
563 {
564 cctx->poptions = &ssl->options;
565 cctx->pcert_flags = &ssl->cert->cert_flags;
566 }
567 else
568 {
569 cctx->poptions = NULL;
570 cctx->pcert_flags = NULL;
571 }
572 }
573
574 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
575 {
576 cctx->ctx = ctx;
577 cctx->ssl = NULL;
578 if (ctx)
579 {
580 cctx->poptions = &ctx->options;
581 cctx->pcert_flags = &ctx->cert->cert_flags;
582 }
583 else
584 {
585 cctx->poptions = NULL;
586 cctx->pcert_flags = NULL;
587 }
588 }