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