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