5a19a75dbf9d683e31e30b4b40f3104d58efb3d5
[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         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;
512          i < sizeof(ssl_conf_cmds) / sizeof(ssl_conf_cmd_tbl); i++, t++) {
513         if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
514             if (t->str_cmdline && !strcmp(t->str_cmdline, cmd))
515                 return t;
516         }
517         if (cctx->flags & SSL_CONF_FLAG_FILE) {
518             if (t->str_file && !strcasecmp(t->str_file, cmd))
519                 return t;
520         }
521     }
522     return NULL;
523 }
524
525 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
526 {
527     const ssl_conf_cmd_tbl *runcmd;
528     if (cmd == NULL) {
529         SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
530         return 0;
531     }
532
533     if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
534         return -2;
535
536     runcmd = ssl_conf_cmd_lookup(cctx, cmd);
537
538     if (runcmd) {
539         int rv;
540         if (value == NULL)
541             return -3;
542         rv = runcmd->cmd(cctx, value);
543         if (rv > 0)
544             return 2;
545         if (rv == -2)
546             return -2;
547         if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
548             SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
549             ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
550         }
551         return 0;
552     }
553
554     if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
555         if (ctrl_str_option(cctx, cmd))
556             return 1;
557     }
558
559     if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
560         SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
561         ERR_add_error_data(2, "cmd=", cmd);
562     }
563
564     return -2;
565 }
566
567 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
568 {
569     int rv;
570     const char *arg = NULL, *argn;
571     if (pargc && *pargc == 0)
572         return 0;
573     if (!pargc || *pargc > 0)
574         arg = **pargv;
575     if (arg == NULL)
576         return 0;
577     if (!pargc || *pargc > 1)
578         argn = (*pargv)[1];
579     else
580         argn = NULL;
581     cctx->flags &= ~SSL_CONF_FLAG_FILE;
582     cctx->flags |= SSL_CONF_FLAG_CMDLINE;
583     rv = SSL_CONF_cmd(cctx, arg, argn);
584     if (rv > 0) {
585         /* Success: update pargc, pargv */
586         (*pargv) += rv;
587         if (pargc)
588             (*pargc) -= rv;
589         return rv;
590     }
591     /* Unknown switch: indicate no arguments processed */
592     if (rv == -2)
593         return 0;
594     /* Some error occurred processing command, return fatal error */
595     if (rv == 0)
596         return -1;
597     return rv;
598 }
599
600 int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
601 {
602     if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
603         const ssl_conf_cmd_tbl *runcmd;
604         runcmd = ssl_conf_cmd_lookup(cctx, cmd);
605         if (runcmd)
606             return runcmd->value_type;
607     }
608     return SSL_CONF_TYPE_UNKNOWN;
609 }
610
611 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
612 {
613     SSL_CONF_CTX *ret;
614     size_t i;
615     ret = OPENSSL_malloc(sizeof(SSL_CONF_CTX));
616     if (ret) {
617         ret->flags = 0;
618         ret->prefix = NULL;
619         ret->prefixlen = 0;
620         ret->ssl = NULL;
621         ret->ctx = NULL;
622         ret->poptions = NULL;
623         ret->pcert_flags = NULL;
624         ret->tbl = NULL;
625         ret->ntbl = 0;
626         for (i = 0; i < SSL_PKEY_NUM; i++)
627             ret->cert_filename[i] = NULL;
628     }
629     return ret;
630 }
631
632 int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
633 {
634     /* See if any certificates are missing private keys */
635     size_t i;
636     CERT *c = NULL;
637     if (cctx->ctx)
638         c = cctx->ctx->cert;
639     else if (cctx->ssl)
640         c = cctx->ssl->cert;
641     if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
642         for (i = 0; i < SSL_PKEY_NUM; i++) {
643             const char *p = cctx->cert_filename[i];
644             /*
645              * If missing private key try to load one from certificate file
646              */
647             if (p && !c->pkeys[i].privatekey) {
648                 if (!cmd_PrivateKey(cctx, p))
649                     return 0;
650             }
651         }
652     }
653     return 1;
654 }
655
656 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
657 {
658     if (cctx) {
659         size_t i;
660         for (i = 0; i < SSL_PKEY_NUM; i++) {
661             OPENSSL_free(cctx->cert_filename[i]);
662         }
663         OPENSSL_free(cctx->prefix);
664         OPENSSL_free(cctx);
665     }
666 }
667
668 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
669 {
670     cctx->flags |= flags;
671     return cctx->flags;
672 }
673
674 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
675 {
676     cctx->flags &= ~flags;
677     return cctx->flags;
678 }
679
680 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
681 {
682     char *tmp = NULL;
683     if (pre) {
684         tmp = BUF_strdup(pre);
685         if (tmp == NULL)
686             return 0;
687     }
688     OPENSSL_free(cctx->prefix);
689     cctx->prefix = tmp;
690     if (tmp)
691         cctx->prefixlen = strlen(tmp);
692     else
693         cctx->prefixlen = 0;
694     return 1;
695 }
696
697 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
698 {
699     cctx->ssl = ssl;
700     cctx->ctx = NULL;
701     if (ssl) {
702         cctx->poptions = &ssl->options;
703         cctx->pcert_flags = &ssl->cert->cert_flags;
704     } else {
705         cctx->poptions = NULL;
706         cctx->pcert_flags = NULL;
707     }
708 }
709
710 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
711 {
712     cctx->ctx = ctx;
713     cctx->ssl = NULL;
714     if (ctx) {
715         cctx->poptions = &ctx->options;
716         cctx->pcert_flags = &ctx->cert->cert_flags;
717     } else {
718         cctx->poptions = NULL;
719         cctx->pcert_flags = NULL;
720     }
721 }