Big apps cleanup (option-parsing, etc)
[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 = sizeof(ssl_option_single) / sizeof(ssl_flag_tbl);
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 = sizeof(ssl_protocol_list) / sizeof(ssl_flag_tbl);
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 = sizeof(ssl_option_list) / sizeof(ssl_flag_tbl);
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         if (*pfilename)
384             OPENSSL_free(*pfilename);
385         *pfilename = BUF_strdup(value);
386         if (!*pfilename)
387             rv = 0;
388     }
389
390     return rv > 0;
391 }
392
393 static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
394 {
395     int rv = 1;
396     if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
397         return -2;
398     if (cctx->ctx)
399         rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
400     if (cctx->ssl)
401         rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
402     return rv > 0;
403 }
404
405 static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
406 {
407     int rv = 1;
408     if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
409         return -2;
410     if (!(cctx->flags & SSL_CONF_FLAG_SERVER))
411         return -2;
412     if (cctx->ctx)
413         rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
414     return rv > 0;
415 }
416
417 #ifndef OPENSSL_NO_DH
418 static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
419 {
420     int rv = 0;
421     DH *dh = NULL;
422     BIO *in = NULL;
423     if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
424         return -2;
425     if (cctx->ctx || cctx->ssl) {
426         in = BIO_new(BIO_s_file_internal());
427         if (!in)
428             goto end;
429         if (BIO_read_filename(in, value) <= 0)
430             goto end;
431         dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
432         if (!dh)
433             goto end;
434     } else
435         return 1;
436     if (cctx->ctx)
437         rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh);
438     if (cctx->ssl)
439         rv = SSL_set_tmp_dh(cctx->ssl, dh);
440  end:
441     DH_free(dh);
442     BIO_free(in);
443     return rv > 0;
444 }
445 #endif
446 typedef struct {
447     int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
448     const char *str_file;
449     const char *str_cmdline;
450     unsigned int value_type;
451 } ssl_conf_cmd_tbl;
452
453 /* Table of supported parameters */
454
455 #define SSL_CONF_CMD(name, cmdopt, type) \
456         {cmd_##name, #name, cmdopt, type}
457
458 #define SSL_CONF_CMD_STRING(name, cmdopt) \
459         SSL_CONF_CMD(name, cmdopt, SSL_CONF_TYPE_STRING)
460
461 /* See apps/apps.h if you change this table. */
462 static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
463     SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs"),
464     SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs"),
465     SSL_CONF_CMD_STRING(Curves, "curves"),
466 #ifndef OPENSSL_NO_EC
467     SSL_CONF_CMD_STRING(ECDHParameters, "named_curve"),
468 #endif
469     SSL_CONF_CMD_STRING(CipherString, "cipher"),
470     SSL_CONF_CMD_STRING(Protocol, NULL),
471     SSL_CONF_CMD_STRING(Options, NULL),
472     SSL_CONF_CMD(Certificate, "cert", SSL_CONF_TYPE_FILE),
473     SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_TYPE_FILE),
474     SSL_CONF_CMD(ServerInfoFile, NULL, SSL_CONF_TYPE_FILE),
475 #ifndef OPENSSL_NO_DH
476     SSL_CONF_CMD(DHParameters, "dhparam", SSL_CONF_TYPE_FILE)
477 #endif
478 };
479
480 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
481 {
482     if (!pcmd || !*pcmd)
483         return 0;
484     /* If a prefix is set, check and skip */
485     if (cctx->prefix) {
486         if (strlen(*pcmd) <= cctx->prefixlen)
487             return 0;
488         if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
489             strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
490             return 0;
491         if (cctx->flags & SSL_CONF_FLAG_FILE &&
492             strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
493             return 0;
494         *pcmd += cctx->prefixlen;
495     } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
496         if (**pcmd != '-' || !(*pcmd)[1])
497             return 0;
498         *pcmd += 1;
499     }
500     return 1;
501 }
502
503 static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
504                                                    const char *cmd)
505 {
506     const ssl_conf_cmd_tbl *t;
507     size_t i;
508     if (cmd == NULL)
509         return NULL;
510
511     /* Look for matching parameter name in table */
512     for (i = 0, t = ssl_conf_cmds;
513          i < sizeof(ssl_conf_cmds) / sizeof(ssl_conf_cmd_tbl); i++, t++) {
514         if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
515             if (t->str_cmdline && !strcmp(t->str_cmdline, cmd))
516                 return t;
517         }
518         if (cctx->flags & SSL_CONF_FLAG_FILE) {
519             if (t->str_file && !strcasecmp(t->str_file, cmd))
520                 return t;
521         }
522     }
523     return NULL;
524 }
525
526 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
527 {
528     const ssl_conf_cmd_tbl *runcmd;
529     if (cmd == NULL) {
530         SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
531         return 0;
532     }
533
534     if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
535         return -2;
536
537     runcmd = ssl_conf_cmd_lookup(cctx, cmd);
538
539     if (runcmd) {
540         int rv;
541         if (value == NULL)
542             return -3;
543         rv = runcmd->cmd(cctx, value);
544         if (rv > 0)
545             return 2;
546         if (rv == -2)
547             return -2;
548         if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
549             SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
550             ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
551         }
552         return 0;
553     }
554
555     if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
556         if (ctrl_str_option(cctx, cmd))
557             return 1;
558     }
559
560     if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
561         SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
562         ERR_add_error_data(2, "cmd=", cmd);
563     }
564
565     return -2;
566 }
567
568 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
569 {
570     int rv;
571     const char *arg = NULL, *argn;
572     if (pargc && *pargc == 0)
573         return 0;
574     if (!pargc || *pargc > 0)
575         arg = **pargv;
576     if (arg == NULL)
577         return 0;
578     if (!pargc || *pargc > 1)
579         argn = (*pargv)[1];
580     else
581         argn = NULL;
582     cctx->flags &= ~SSL_CONF_FLAG_FILE;
583     cctx->flags |= SSL_CONF_FLAG_CMDLINE;
584     rv = SSL_CONF_cmd(cctx, arg, argn);
585     if (rv > 0) {
586         /* Success: update pargc, pargv */
587         (*pargv) += rv;
588         if (pargc)
589             (*pargc) -= rv;
590         return rv;
591     }
592     /* Unknown switch: indicate no arguments processed */
593     if (rv == -2)
594         return 0;
595     /* Some error occurred processing command, return fatal error */
596     if (rv == 0)
597         return -1;
598     return rv;
599 }
600
601 int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
602 {
603     if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
604         const ssl_conf_cmd_tbl *runcmd;
605         runcmd = ssl_conf_cmd_lookup(cctx, cmd);
606         if (runcmd)
607             return runcmd->value_type;
608     }
609     return SSL_CONF_TYPE_UNKNOWN;
610 }
611
612 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
613 {
614     SSL_CONF_CTX *ret;
615     size_t i;
616     ret = OPENSSL_malloc(sizeof(SSL_CONF_CTX));
617     if (ret) {
618         ret->flags = 0;
619         ret->prefix = NULL;
620         ret->prefixlen = 0;
621         ret->ssl = NULL;
622         ret->ctx = NULL;
623         ret->poptions = NULL;
624         ret->pcert_flags = NULL;
625         ret->tbl = NULL;
626         ret->ntbl = 0;
627         for (i = 0; i < SSL_PKEY_NUM; i++)
628             ret->cert_filename[i] = NULL;
629     }
630     return ret;
631 }
632
633 int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
634 {
635     /* See if any certificates are missing private keys */
636     size_t i;
637     CERT *c = NULL;
638     if (cctx->ctx)
639         c = cctx->ctx->cert;
640     else if (cctx->ssl)
641         c = cctx->ssl->cert;
642     if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
643         for (i = 0; i < SSL_PKEY_NUM; i++) {
644             const char *p = cctx->cert_filename[i];
645             /*
646              * If missing private key try to load one from certificate file
647              */
648             if (p && !c->pkeys[i].privatekey) {
649                 if (!cmd_PrivateKey(cctx, p))
650                     return 0;
651             }
652         }
653     }
654     return 1;
655 }
656
657 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
658 {
659     if (cctx) {
660         size_t i;
661         for (i = 0; i < SSL_PKEY_NUM; i++) {
662             if (cctx->cert_filename[i])
663                 OPENSSL_free(cctx->cert_filename[i]);
664         }
665         if (cctx->prefix)
666             OPENSSL_free(cctx->prefix);
667         OPENSSL_free(cctx);
668     }
669 }
670
671 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
672 {
673     cctx->flags |= flags;
674     return cctx->flags;
675 }
676
677 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
678 {
679     cctx->flags &= ~flags;
680     return cctx->flags;
681 }
682
683 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
684 {
685     char *tmp = NULL;
686     if (pre) {
687         tmp = BUF_strdup(pre);
688         if (tmp == NULL)
689             return 0;
690     }
691     if (cctx->prefix)
692         OPENSSL_free(cctx->prefix);
693     cctx->prefix = tmp;
694     if (tmp)
695         cctx->prefixlen = strlen(tmp);
696     else
697         cctx->prefixlen = 0;
698     return 1;
699 }
700
701 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
702 {
703     cctx->ssl = ssl;
704     cctx->ctx = NULL;
705     if (ssl) {
706         cctx->poptions = &ssl->options;
707         cctx->pcert_flags = &ssl->cert->cert_flags;
708     } else {
709         cctx->poptions = NULL;
710         cctx->pcert_flags = NULL;
711     }
712 }
713
714 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
715 {
716     cctx->ctx = ctx;
717     cctx->ssl = NULL;
718     if (ctx) {
719         cctx->poptions = &ctx->options;
720         cctx->pcert_flags = &ctx->cert->cert_flags;
721     } else {
722         cctx->poptions = NULL;
723         cctx->pcert_flags = NULL;
724     }
725 }