]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/ssl_conf.c
Use safer sizeof variant in malloc
[thirdparty/openssl.git] / ssl / ssl_conf.c
1 /*
2 * ! \file ssl/ssl_conf.c \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 #ifndef OPENSSL_NO_DH
66 # include <openssl/dh.h>
67 #endif
68
69 /*
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
72 */
73
74 typedef struct {
75 const char *name;
76 int namelen;
77 unsigned int name_flags;
78 unsigned long option_value;
79 } ssl_flag_tbl;
80
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)
90
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}
103
104 /*
105 * Opaque structure containing SSL configuration context.
106 */
107
108 struct ssl_conf_ctx_st {
109 /*
110 * Various flags indicating (among other things) which options we will
111 * recognise.
112 */
113 unsigned int flags;
114 /* Prefix and length of commands */
115 char *prefix;
116 size_t prefixlen;
117 /* SSL_CTX or SSL structure to perform operations on */
118 SSL_CTX *ctx;
119 SSL *ssl;
120 /* Pointer to SSL or SSL_CTX options field or NULL if none */
121 unsigned long *poptions;
122 /* Certificate filenames for each type */
123 char *cert_filename[SSL_PKEY_NUM];
124 /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
125 unsigned int *pcert_flags;
126 /* Current flag table being worked on */
127 const ssl_flag_tbl *tbl;
128 /* Size of table */
129 size_t ntbl;
130 };
131
132 static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
133 const char *name, int namelen, int onoff)
134 {
135 /* If name not relevant for context skip */
136 if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
137 return 0;
138 if (namelen == -1) {
139 if (strcmp(tbl->name, name))
140 return 0;
141 } else if (tbl->namelen != namelen
142 || strncasecmp(tbl->name, name, namelen))
143 return 0;
144 if (cctx->poptions) {
145 if (tbl->name_flags & SSL_TFLAG_INV)
146 onoff ^= 1;
147 if (tbl->name_flags & SSL_TFLAG_CERT) {
148 if (onoff)
149 *cctx->pcert_flags |= tbl->option_value;
150 else
151 *cctx->pcert_flags &= ~tbl->option_value;
152 } else {
153 if (onoff)
154 *cctx->poptions |= tbl->option_value;
155 else
156 *cctx->poptions &= ~tbl->option_value;
157 }
158 }
159 return 1;
160 }
161
162 static int ssl_set_option_list(const char *elem, int len, void *usr)
163 {
164 SSL_CONF_CTX *cctx = usr;
165 size_t i;
166 const ssl_flag_tbl *tbl;
167 int onoff = 1;
168 /*
169 * len == -1 indicates not being called in list context, just for single
170 * command line switches, so don't allow +, -.
171 */
172 if (elem == NULL)
173 return 0;
174 if (len != -1) {
175 if (*elem == '+') {
176 elem++;
177 len--;
178 onoff = 1;
179 } else if (*elem == '-') {
180 elem++;
181 len--;
182 onoff = 0;
183 }
184 }
185 for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++) {
186 if (ssl_match_option(cctx, tbl, elem, len, onoff))
187 return 1;
188 }
189 return 0;
190 }
191
192 /* Single command line switches with no argument e.g. -no_ssl3 */
193 static int ctrl_str_option(SSL_CONF_CTX *cctx, const char *cmd)
194 {
195 /* See apps/apps.h if you change this table. */
196 static const ssl_flag_tbl ssl_option_single[] = {
197 SSL_FLAG_TBL("no_ssl3", SSL_OP_NO_SSLv3),
198 SSL_FLAG_TBL("no_tls1", SSL_OP_NO_TLSv1),
199 SSL_FLAG_TBL("no_tls1_1", SSL_OP_NO_TLSv1_1),
200 SSL_FLAG_TBL("no_tls1_2", SSL_OP_NO_TLSv1_2),
201 SSL_FLAG_TBL("bugs", SSL_OP_ALL),
202 SSL_FLAG_TBL("no_comp", SSL_OP_NO_COMPRESSION),
203 SSL_FLAG_TBL_SRV("ecdh_single", SSL_OP_SINGLE_ECDH_USE),
204 #ifndef OPENSSL_NO_TLSEXT
205 SSL_FLAG_TBL("no_ticket", SSL_OP_NO_TICKET),
206 #endif
207 SSL_FLAG_TBL_SRV("serverpref", SSL_OP_CIPHER_SERVER_PREFERENCE),
208 SSL_FLAG_TBL("legacy_renegotiation",
209 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
210 SSL_FLAG_TBL_SRV("legacy_server_connect",
211 SSL_OP_LEGACY_SERVER_CONNECT),
212 SSL_FLAG_TBL_SRV("no_resumption_on_reneg",
213 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
214 SSL_FLAG_TBL_SRV_INV("no_legacy_server_connect",
215 SSL_OP_LEGACY_SERVER_CONNECT),
216 SSL_FLAG_TBL_CERT("strict", SSL_CERT_FLAG_TLS_STRICT),
217 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
218 SSL_FLAG_TBL_CERT("debug_broken_protocol",
219 SSL_CERT_FLAG_BROKEN_PROTOCOL),
220 #endif
221 };
222 cctx->tbl = ssl_option_single;
223 cctx->ntbl = OSSL_NELEM(ssl_option_single);
224 return ssl_set_option_list(cmd, -1, cctx);
225 }
226
227 /* Set supported signature algorithms */
228 static int cmd_SignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
229 {
230 int rv;
231 if (cctx->ssl)
232 rv = SSL_set1_sigalgs_list(cctx->ssl, value);
233 /* NB: ctx == NULL performs syntax checking only */
234 else
235 rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
236 return rv > 0;
237 }
238
239 /* Set supported client signature algorithms */
240 static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx,
241 const char *value)
242 {
243 int rv;
244 if (cctx->ssl)
245 rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
246 /* NB: ctx == NULL performs syntax checking only */
247 else
248 rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
249 return rv > 0;
250 }
251
252 static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value)
253 {
254 int rv;
255 if (cctx->ssl)
256 rv = SSL_set1_curves_list(cctx->ssl, value);
257 /* NB: ctx == NULL performs syntax checking only */
258 else
259 rv = SSL_CTX_set1_curves_list(cctx->ctx, value);
260 return rv > 0;
261 }
262
263 #ifndef OPENSSL_NO_EC
264 /* ECDH temporary parameters */
265 static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value)
266 {
267 int onoff = -1, rv = 1;
268 if (!(cctx->flags & SSL_CONF_FLAG_SERVER))
269 return -2;
270 if (cctx->flags & SSL_CONF_FLAG_FILE) {
271 if (*value == '+') {
272 onoff = 1;
273 value++;
274 }
275 if (*value == '-') {
276 onoff = 0;
277 value++;
278 }
279 if (!strcasecmp(value, "automatic")) {
280 if (onoff == -1)
281 onoff = 1;
282 } else if (onoff != -1)
283 return 0;
284 } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
285 if (!strcmp(value, "auto"))
286 onoff = 1;
287 }
288
289 if (onoff != -1) {
290 if (cctx->ctx)
291 rv = SSL_CTX_set_ecdh_auto(cctx->ctx, onoff);
292 else if (cctx->ssl)
293 rv = SSL_set_ecdh_auto(cctx->ssl, onoff);
294 } else {
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 #endif
315 static int cmd_CipherString(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 > 0;
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 SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
329 SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
330 SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
331 SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
332 SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
333 SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2)
334 };
335 if (!(cctx->flags & SSL_CONF_FLAG_FILE))
336 return -2;
337 cctx->tbl = ssl_protocol_list;
338 cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
339 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
340 }
341
342 static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
343 {
344 static const ssl_flag_tbl ssl_option_list[] = {
345 SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
346 SSL_FLAG_TBL_INV("EmptyFragments",
347 SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
348 SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
349 SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
350 SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
351 SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
352 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
353 SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
354 SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
355 SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
356 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
357 };
358 if (!(cctx->flags & SSL_CONF_FLAG_FILE))
359 return -2;
360 if (value == NULL)
361 return -3;
362 cctx->tbl = ssl_option_list;
363 cctx->ntbl = OSSL_NELEM(ssl_option_list);
364 return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
365 }
366
367 static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
368 {
369 int rv = 1;
370 CERT *c = NULL;
371 if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
372 return -2;
373 if (cctx->ctx) {
374 rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
375 c = cctx->ctx->cert;
376 }
377 if (cctx->ssl) {
378 rv = SSL_use_certificate_file(cctx->ssl, value, SSL_FILETYPE_PEM);
379 c = cctx->ssl->cert;
380 }
381 if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
382 char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
383 OPENSSL_free(*pfilename);
384 *pfilename = BUF_strdup(value);
385 if (!*pfilename)
386 rv = 0;
387 }
388
389 return rv > 0;
390 }
391
392 static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
393 {
394 int rv = 1;
395 if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
396 return -2;
397 if (cctx->ctx)
398 rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
399 if (cctx->ssl)
400 rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
401 return rv > 0;
402 }
403
404 static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
405 {
406 int rv = 1;
407 if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
408 return -2;
409 if (!(cctx->flags & SSL_CONF_FLAG_SERVER))
410 return -2;
411 if (cctx->ctx)
412 rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
413 return rv > 0;
414 }
415
416 #ifndef OPENSSL_NO_DH
417 static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
418 {
419 int rv = 0;
420 DH *dh = NULL;
421 BIO *in = NULL;
422 if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
423 return -2;
424 if (cctx->ctx || cctx->ssl) {
425 in = BIO_new(BIO_s_file_internal());
426 if (!in)
427 goto end;
428 if (BIO_read_filename(in, value) <= 0)
429 goto end;
430 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
431 if (!dh)
432 goto end;
433 } else
434 return 1;
435 if (cctx->ctx)
436 rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh);
437 if (cctx->ssl)
438 rv = SSL_set_tmp_dh(cctx->ssl, dh);
439 end:
440 DH_free(dh);
441 BIO_free(in);
442 return rv > 0;
443 }
444 #endif
445 typedef struct {
446 int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
447 const char *str_file;
448 const char *str_cmdline;
449 unsigned int value_type;
450 } ssl_conf_cmd_tbl;
451
452 /* Table of supported parameters */
453
454 #define SSL_CONF_CMD(name, cmdopt, type) \
455 {cmd_##name, #name, cmdopt, type}
456
457 #define SSL_CONF_CMD_STRING(name, cmdopt) \
458 SSL_CONF_CMD(name, cmdopt, SSL_CONF_TYPE_STRING)
459
460 /* See apps/apps.h if you change this table. */
461 static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
462 SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs"),
463 SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs"),
464 SSL_CONF_CMD_STRING(Curves, "curves"),
465 #ifndef OPENSSL_NO_EC
466 SSL_CONF_CMD_STRING(ECDHParameters, "named_curve"),
467 #endif
468 SSL_CONF_CMD_STRING(CipherString, "cipher"),
469 SSL_CONF_CMD_STRING(Protocol, NULL),
470 SSL_CONF_CMD_STRING(Options, NULL),
471 SSL_CONF_CMD(Certificate, "cert", SSL_CONF_TYPE_FILE),
472 SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_TYPE_FILE),
473 SSL_CONF_CMD(ServerInfoFile, NULL, SSL_CONF_TYPE_FILE),
474 #ifndef OPENSSL_NO_DH
475 SSL_CONF_CMD(DHParameters, "dhparam", SSL_CONF_TYPE_FILE)
476 #endif
477 };
478
479 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
480 {
481 if (!pcmd || !*pcmd)
482 return 0;
483 /* If a prefix is set, check and skip */
484 if (cctx->prefix) {
485 if (strlen(*pcmd) <= cctx->prefixlen)
486 return 0;
487 if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
488 strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
489 return 0;
490 if (cctx->flags & SSL_CONF_FLAG_FILE &&
491 strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
492 return 0;
493 *pcmd += cctx->prefixlen;
494 } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
495 if (**pcmd != '-' || !(*pcmd)[1])
496 return 0;
497 *pcmd += 1;
498 }
499 return 1;
500 }
501
502 static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
503 const char *cmd)
504 {
505 const ssl_conf_cmd_tbl *t;
506 size_t i;
507 if (cmd == NULL)
508 return NULL;
509
510 /* Look for matching parameter name in table */
511 for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
512 if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
513 if (t->str_cmdline && !strcmp(t->str_cmdline, cmd))
514 return t;
515 }
516 if (cctx->flags & SSL_CONF_FLAG_FILE) {
517 if (t->str_file && !strcasecmp(t->str_file, cmd))
518 return t;
519 }
520 }
521 return NULL;
522 }
523
524 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
525 {
526 const ssl_conf_cmd_tbl *runcmd;
527 if (cmd == NULL) {
528 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
529 return 0;
530 }
531
532 if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
533 return -2;
534
535 runcmd = ssl_conf_cmd_lookup(cctx, cmd);
536
537 if (runcmd) {
538 int rv;
539 if (value == NULL)
540 return -3;
541 rv = runcmd->cmd(cctx, value);
542 if (rv > 0)
543 return 2;
544 if (rv == -2)
545 return -2;
546 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
547 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
548 ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
549 }
550 return 0;
551 }
552
553 if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
554 if (ctrl_str_option(cctx, cmd))
555 return 1;
556 }
557
558 if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
559 SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
560 ERR_add_error_data(2, "cmd=", cmd);
561 }
562
563 return -2;
564 }
565
566 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
567 {
568 int rv;
569 const char *arg = NULL, *argn;
570 if (pargc && *pargc == 0)
571 return 0;
572 if (!pargc || *pargc > 0)
573 arg = **pargv;
574 if (arg == NULL)
575 return 0;
576 if (!pargc || *pargc > 1)
577 argn = (*pargv)[1];
578 else
579 argn = NULL;
580 cctx->flags &= ~SSL_CONF_FLAG_FILE;
581 cctx->flags |= SSL_CONF_FLAG_CMDLINE;
582 rv = SSL_CONF_cmd(cctx, arg, argn);
583 if (rv > 0) {
584 /* Success: update pargc, pargv */
585 (*pargv) += rv;
586 if (pargc)
587 (*pargc) -= rv;
588 return rv;
589 }
590 /* Unknown switch: indicate no arguments processed */
591 if (rv == -2)
592 return 0;
593 /* Some error occurred processing command, return fatal error */
594 if (rv == 0)
595 return -1;
596 return rv;
597 }
598
599 int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
600 {
601 if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
602 const ssl_conf_cmd_tbl *runcmd;
603 runcmd = ssl_conf_cmd_lookup(cctx, cmd);
604 if (runcmd)
605 return runcmd->value_type;
606 }
607 return SSL_CONF_TYPE_UNKNOWN;
608 }
609
610 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
611 {
612 SSL_CONF_CTX *ret = OPENSSL_malloc(sizeof(*ret));
613 size_t i;
614
615 if (ret) {
616 ret->flags = 0;
617 ret->prefix = NULL;
618 ret->prefixlen = 0;
619 ret->ssl = NULL;
620 ret->ctx = NULL;
621 ret->poptions = NULL;
622 ret->pcert_flags = NULL;
623 ret->tbl = NULL;
624 ret->ntbl = 0;
625 for (i = 0; i < SSL_PKEY_NUM; i++)
626 ret->cert_filename[i] = NULL;
627 }
628 return ret;
629 }
630
631 int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
632 {
633 /* See if any certificates are missing private keys */
634 size_t i;
635 CERT *c = NULL;
636 if (cctx->ctx)
637 c = cctx->ctx->cert;
638 else if (cctx->ssl)
639 c = cctx->ssl->cert;
640 if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
641 for (i = 0; i < SSL_PKEY_NUM; i++) {
642 const char *p = cctx->cert_filename[i];
643 /*
644 * If missing private key try to load one from certificate file
645 */
646 if (p && !c->pkeys[i].privatekey) {
647 if (!cmd_PrivateKey(cctx, p))
648 return 0;
649 }
650 }
651 }
652 return 1;
653 }
654
655 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
656 {
657 if (cctx) {
658 size_t i;
659 for (i = 0; i < SSL_PKEY_NUM; i++) {
660 OPENSSL_free(cctx->cert_filename[i]);
661 }
662 OPENSSL_free(cctx->prefix);
663 OPENSSL_free(cctx);
664 }
665 }
666
667 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
668 {
669 cctx->flags |= flags;
670 return cctx->flags;
671 }
672
673 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
674 {
675 cctx->flags &= ~flags;
676 return cctx->flags;
677 }
678
679 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
680 {
681 char *tmp = NULL;
682 if (pre) {
683 tmp = BUF_strdup(pre);
684 if (tmp == NULL)
685 return 0;
686 }
687 OPENSSL_free(cctx->prefix);
688 cctx->prefix = tmp;
689 if (tmp)
690 cctx->prefixlen = strlen(tmp);
691 else
692 cctx->prefixlen = 0;
693 return 1;
694 }
695
696 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
697 {
698 cctx->ssl = ssl;
699 cctx->ctx = NULL;
700 if (ssl) {
701 cctx->poptions = &ssl->options;
702 cctx->pcert_flags = &ssl->cert->cert_flags;
703 } else {
704 cctx->poptions = NULL;
705 cctx->pcert_flags = NULL;
706 }
707 }
708
709 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
710 {
711 cctx->ctx = ctx;
712 cctx->ssl = NULL;
713 if (ctx) {
714 cctx->poptions = &ctx->options;
715 cctx->pcert_flags = &ctx->cert->cert_flags;
716 } else {
717 cctx->poptions = NULL;
718 cctx->pcert_flags = NULL;
719 }
720 }