Add some tests for the new custom extensions API
[openssl.git] / ssl / ssl_conf.c
1 /*
2  * Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "ssl_locl.h"
12 #include <openssl/conf.h>
13 #include <openssl/objects.h>
14 #include <openssl/dh.h>
15
16 /*
17  * structure holding name tables. This is used for permitted elements in lists
18  * such as TLSv1.
19  */
20
21 typedef struct {
22     const char *name;
23     int namelen;
24     unsigned int name_flags;
25     unsigned long option_value;
26 } ssl_flag_tbl;
27
28 /* Switch table: use for single command line switches like no_tls2 */
29 typedef struct {
30     unsigned long option_value;
31     unsigned int name_flags;
32 } ssl_switch_tbl;
33
34 /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
35 #define SSL_TFLAG_INV   0x1
36 /* Mask for type of flag referred to */
37 #define SSL_TFLAG_TYPE_MASK 0xf00
38 /* Flag is for options */
39 #define SSL_TFLAG_OPTION    0x000
40 /* Flag is for cert_flags */
41 #define SSL_TFLAG_CERT      0x100
42 /* Flag is for verify mode */
43 #define SSL_TFLAG_VFY       0x200
44 /* Option can only be used for clients */
45 #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
46 /* Option can only be used for servers */
47 #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
48 #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
49
50 #define SSL_FLAG_TBL(str, flag) \
51         {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
52 #define SSL_FLAG_TBL_SRV(str, flag) \
53         {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
54 #define SSL_FLAG_TBL_CLI(str, flag) \
55         {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
56 #define SSL_FLAG_TBL_INV(str, flag) \
57         {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
58 #define SSL_FLAG_TBL_SRV_INV(str, flag) \
59         {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
60 #define SSL_FLAG_TBL_CERT(str, flag) \
61         {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
62
63 #define SSL_FLAG_VFY_CLI(str, flag) \
64         {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
65 #define SSL_FLAG_VFY_SRV(str, flag) \
66         {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
67
68 /*
69  * Opaque structure containing SSL configuration context.
70  */
71
72 struct ssl_conf_ctx_st {
73     /*
74      * Various flags indicating (among other things) which options we will
75      * recognise.
76      */
77     unsigned int flags;
78     /* Prefix and length of commands */
79     char *prefix;
80     size_t prefixlen;
81     /* SSL_CTX or SSL structure to perform operations on */
82     SSL_CTX *ctx;
83     SSL *ssl;
84     /* Pointer to SSL or SSL_CTX options field or NULL if none */
85     uint32_t *poptions;
86     /* Certificate filenames for each type */
87     char *cert_filename[SSL_PKEY_NUM];
88     /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
89     uint32_t *pcert_flags;
90     /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
91     uint32_t *pvfy_flags;
92     /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
93     int *min_version;
94     /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
95     int *max_version;
96     /* Current flag table being worked on */
97     const ssl_flag_tbl *tbl;
98     /* Size of table */
99     size_t ntbl;
100     /* Client CA names */
101     STACK_OF(X509_NAME) *canames;
102 };
103
104 static void ssl_set_option(SSL_CONF_CTX *cctx, unsigned int name_flags,
105                            unsigned long option_value, int onoff)
106 {
107     uint32_t *pflags;
108     if (cctx->poptions == NULL)
109         return;
110     if (name_flags & SSL_TFLAG_INV)
111         onoff ^= 1;
112     switch (name_flags & SSL_TFLAG_TYPE_MASK) {
113
114     case SSL_TFLAG_CERT:
115         pflags = cctx->pcert_flags;
116         break;
117
118     case SSL_TFLAG_VFY:
119         pflags = cctx->pvfy_flags;
120         break;
121
122     case SSL_TFLAG_OPTION:
123         pflags = cctx->poptions;
124         break;
125
126     default:
127         return;
128
129     }
130     if (onoff)
131         *pflags |= option_value;
132     else
133         *pflags &= ~option_value;
134 }
135
136 static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
137                             const char *name, int namelen, int onoff)
138 {
139     /* If name not relevant for context skip */
140     if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
141         return 0;
142     if (namelen == -1) {
143         if (strcmp(tbl->name, name))
144             return 0;
145     } else if (tbl->namelen != namelen || strncasecmp(tbl->name, name, namelen))
146         return 0;
147     ssl_set_option(cctx, tbl->name_flags, tbl->option_value, onoff);
148     return 1;
149 }
150
151 static int ssl_set_option_list(const char *elem, int len, void *usr)
152 {
153     SSL_CONF_CTX *cctx = usr;
154     size_t i;
155     const ssl_flag_tbl *tbl;
156     int onoff = 1;
157     /*
158      * len == -1 indicates not being called in list context, just for single
159      * command line switches, so don't allow +, -.
160      */
161     if (elem == NULL)
162         return 0;
163     if (len != -1) {
164         if (*elem == '+') {
165             elem++;
166             len--;
167             onoff = 1;
168         } else if (*elem == '-') {
169             elem++;
170             len--;
171             onoff = 0;
172         }
173     }
174     for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++) {
175         if (ssl_match_option(cctx, tbl, elem, len, onoff))
176             return 1;
177     }
178     return 0;
179 }
180
181 /* Set supported signature algorithms */
182 static int cmd_SignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
183 {
184     int rv;
185     if (cctx->ssl)
186         rv = SSL_set1_sigalgs_list(cctx->ssl, value);
187     /* NB: ctx == NULL performs syntax checking only */
188     else
189         rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
190     return rv > 0;
191 }
192
193 /* Set supported client signature algorithms */
194 static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
195 {
196     int rv;
197     if (cctx->ssl)
198         rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
199     /* NB: ctx == NULL performs syntax checking only */
200     else
201         rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
202     return rv > 0;
203 }
204
205 static int cmd_Groups(SSL_CONF_CTX *cctx, const char *value)
206 {
207     int rv;
208     if (cctx->ssl)
209         rv = SSL_set1_groups_list(cctx->ssl, value);
210     /* NB: ctx == NULL performs syntax checking only */
211     else
212         rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
213     return rv > 0;
214 }
215
216 /* This is the old name for cmd_Groups - retained for backwards compatibility */
217 static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value)
218 {
219     return cmd_Groups(cctx, value);
220 }
221
222 #ifndef OPENSSL_NO_EC
223 /* ECDH temporary parameters */
224 static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value)
225 {
226     int rv = 1;
227     EC_KEY *ecdh;
228     int nid;
229
230     nid = EC_curve_nist2nid(value);
231     if (nid == NID_undef)
232         nid = OBJ_sn2nid(value);
233     if (nid == 0)
234         return 0;
235     ecdh = EC_KEY_new_by_curve_name(nid);
236     if (!ecdh)
237         return 0;
238     if (cctx->ctx)
239         rv = SSL_CTX_set_tmp_ecdh(cctx->ctx, ecdh);
240     else if (cctx->ssl)
241         rv = SSL_set_tmp_ecdh(cctx->ssl, ecdh);
242     EC_KEY_free(ecdh);
243
244     return rv > 0;
245 }
246 #endif
247 static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
248 {
249     int rv = 1;
250     if (cctx->ctx)
251         rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
252     if (cctx->ssl)
253         rv = SSL_set_cipher_list(cctx->ssl, value);
254     return rv > 0;
255 }
256
257 static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
258 {
259     static const ssl_flag_tbl ssl_protocol_list[] = {
260         SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
261         SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
262         SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
263         SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
264         SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
265         SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
266         SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
267         SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
268         SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
269     };
270     cctx->tbl = ssl_protocol_list;
271     cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
272     return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
273 }
274
275 /*
276  * protocol_from_string - converts a protocol version string to a number
277  *
278  * Returns -1 on failure or the version on success
279  */
280 static int protocol_from_string(const char *value)
281 {
282     struct protocol_versions {
283         const char *name;
284         int version;
285     };
286     static const struct protocol_versions versions[] = {
287         {"None", 0},
288         {"SSLv3", SSL3_VERSION},
289         {"TLSv1", TLS1_VERSION},
290         {"TLSv1.1", TLS1_1_VERSION},
291         {"TLSv1.2", TLS1_2_VERSION},
292         {"TLSv1.3", TLS1_3_VERSION},
293         {"DTLSv1", DTLS1_VERSION},
294         {"DTLSv1.2", DTLS1_2_VERSION}
295     };
296     size_t i;
297     size_t n = OSSL_NELEM(versions);
298
299     for (i = 0; i < n; i++)
300         if (strcmp(versions[i].name, value) == 0)
301             return versions[i].version;
302     return -1;
303 }
304
305 static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
306 {
307     int method_version;
308     int new_version;
309
310     if (cctx->ctx != NULL)
311         method_version = cctx->ctx->method->version;
312     else if (cctx->ssl != NULL)
313         method_version = cctx->ssl->ctx->method->version;
314     else
315         return 0;
316     if ((new_version = protocol_from_string(value)) < 0)
317         return 0;
318     return ssl_set_version_bound(method_version, new_version, bound);
319 }
320
321 /*
322  * cmd_MinProtocol - Set min protocol version
323  * @cctx: config structure to save settings in
324  * @value: The min protocol version in string form
325  *
326  * Returns 1 on success and 0 on failure.
327  */
328 static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
329 {
330     return min_max_proto(cctx, value, cctx->min_version);
331 }
332
333 /*
334  * cmd_MaxProtocol - Set max protocol version
335  * @cctx: config structure to save settings in
336  * @value: The max protocol version in string form
337  *
338  * Returns 1 on success and 0 on failure.
339  */
340 static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
341 {
342     return min_max_proto(cctx, value, cctx->max_version);
343 }
344
345 static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
346 {
347     static const ssl_flag_tbl ssl_option_list[] = {
348         SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
349         SSL_FLAG_TBL_INV("EmptyFragments",
350                          SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
351         SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
352         SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
353         SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
354         SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
355                          SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
356         SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
357         SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
358         SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
359                      SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
360         SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
361     };
362     if (value == NULL)
363         return -3;
364     cctx->tbl = ssl_option_list;
365     cctx->ntbl = OSSL_NELEM(ssl_option_list);
366     return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
367 }
368
369 static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
370 {
371     static const ssl_flag_tbl ssl_vfy_list[] = {
372         SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
373         SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
374         SSL_FLAG_VFY_SRV("Require",
375                          SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
376         SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE)
377     };
378     if (value == NULL)
379         return -3;
380     cctx->tbl = ssl_vfy_list;
381     cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
382     return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
383 }
384
385 static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
386 {
387     int rv = 1;
388     CERT *c = NULL;
389     if (cctx->ctx) {
390         rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
391         c = cctx->ctx->cert;
392     }
393     if (cctx->ssl) {
394         rv = SSL_use_certificate_chain_file(cctx->ssl, value);
395         c = cctx->ssl->cert;
396     }
397     if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
398         char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
399         OPENSSL_free(*pfilename);
400         *pfilename = OPENSSL_strdup(value);
401         if (!*pfilename)
402             rv = 0;
403     }
404
405     return rv > 0;
406 }
407
408 static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
409 {
410     int rv = 1;
411     if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
412         return -2;
413     if (cctx->ctx)
414         rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
415     if (cctx->ssl)
416         rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
417     return rv > 0;
418 }
419
420 static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
421 {
422     int rv = 1;
423     if (cctx->ctx)
424         rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
425     return rv > 0;
426 }
427
428 static int do_store(SSL_CONF_CTX *cctx,
429                     const char *CAfile, const char *CApath, int verify_store)
430 {
431     CERT *cert;
432     X509_STORE **st;
433     if (cctx->ctx)
434         cert = cctx->ctx->cert;
435     else if (cctx->ssl)
436         cert = cctx->ssl->cert;
437     else
438         return 1;
439     st = verify_store ? &cert->verify_store : &cert->chain_store;
440     if (*st == NULL) {
441         *st = X509_STORE_new();
442         if (*st == NULL)
443             return 0;
444     }
445     return X509_STORE_load_locations(*st, CAfile, CApath) > 0;
446 }
447
448 static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
449 {
450     return do_store(cctx, NULL, value, 0);
451 }
452
453 static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
454 {
455     return do_store(cctx, value, NULL, 0);
456 }
457
458 static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
459 {
460     return do_store(cctx, NULL, value, 1);
461 }
462
463 static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
464 {
465     return do_store(cctx, value, NULL, 1);
466 }
467
468 static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
469 {
470     if (cctx->canames == NULL)
471         cctx->canames = sk_X509_NAME_new_null();
472     if (cctx->canames == NULL)
473         return 0;
474     return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
475 }
476
477 static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
478 {
479     return cmd_RequestCAFile(cctx, value);
480 }
481
482 static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
483 {
484     if (cctx->canames == NULL)
485         cctx->canames = sk_X509_NAME_new_null();
486     if (cctx->canames == NULL)
487         return 0;
488     return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
489 }
490
491 static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
492 {
493     return cmd_RequestCAPath(cctx, value);
494 }
495
496 #ifndef OPENSSL_NO_DH
497 static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
498 {
499     int rv = 0;
500     DH *dh = NULL;
501     BIO *in = NULL;
502     if (cctx->ctx || cctx->ssl) {
503         in = BIO_new(BIO_s_file());
504         if (in == NULL)
505             goto end;
506         if (BIO_read_filename(in, value) <= 0)
507             goto end;
508         dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
509         if (dh == NULL)
510             goto end;
511     } else
512         return 1;
513     if (cctx->ctx)
514         rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh);
515     if (cctx->ssl)
516         rv = SSL_set_tmp_dh(cctx->ssl, dh);
517  end:
518     DH_free(dh);
519     BIO_free(in);
520     return rv > 0;
521 }
522 #endif
523 typedef struct {
524     int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
525     const char *str_file;
526     const char *str_cmdline;
527     unsigned short flags;
528     unsigned short value_type;
529 } ssl_conf_cmd_tbl;
530
531 /* Table of supported parameters */
532
533 #define SSL_CONF_CMD(name, cmdopt, flags, type) \
534         {cmd_##name, #name, cmdopt, flags, type}
535
536 #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
537         SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
538
539 #define SSL_CONF_CMD_SWITCH(name, flags) \
540         {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
541
542 /* See apps/apps.h if you change this table. */
543 static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
544     SSL_CONF_CMD_SWITCH("no_ssl3", 0),
545     SSL_CONF_CMD_SWITCH("no_tls1", 0),
546     SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
547     SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
548     SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
549     SSL_CONF_CMD_SWITCH("bugs", 0),
550     SSL_CONF_CMD_SWITCH("no_comp", 0),
551     SSL_CONF_CMD_SWITCH("comp", 0),
552     SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
553     SSL_CONF_CMD_SWITCH("no_ticket", 0),
554     SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
555     SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
556     SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER),
557     SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
558     SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER),
559     SSL_CONF_CMD_SWITCH("strict", 0),
560     SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
561     SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
562     SSL_CONF_CMD_STRING(Curves, "curves", 0),
563     SSL_CONF_CMD_STRING(Groups, "groups", 0),
564 #ifndef OPENSSL_NO_EC
565     SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
566 #endif
567     SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
568     SSL_CONF_CMD_STRING(Protocol, NULL, 0),
569     SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
570     SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
571     SSL_CONF_CMD_STRING(Options, NULL, 0),
572     SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
573     SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
574                  SSL_CONF_TYPE_FILE),
575     SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
576                  SSL_CONF_TYPE_FILE),
577     SSL_CONF_CMD(ServerInfoFile, NULL,
578                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
579                  SSL_CONF_TYPE_FILE),
580     SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
581                  SSL_CONF_TYPE_DIR),
582     SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
583                  SSL_CONF_TYPE_FILE),
584     SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
585                  SSL_CONF_TYPE_DIR),
586     SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
587                  SSL_CONF_TYPE_FILE),
588     SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
589                  SSL_CONF_TYPE_FILE),
590     SSL_CONF_CMD(ClientCAFile, NULL,
591                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
592                  SSL_CONF_TYPE_FILE),
593     SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
594                  SSL_CONF_TYPE_DIR),
595     SSL_CONF_CMD(ClientCAPath, NULL,
596                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
597                  SSL_CONF_TYPE_DIR),
598 #ifndef OPENSSL_NO_DH
599     SSL_CONF_CMD(DHParameters, "dhparam",
600                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
601                  SSL_CONF_TYPE_FILE)
602 #endif
603 };
604
605 /* Supported switches: must match order of switches in ssl_conf_cmds */
606 static const ssl_switch_tbl ssl_cmd_switches[] = {
607     {SSL_OP_NO_SSLv3, 0},       /* no_ssl3 */
608     {SSL_OP_NO_TLSv1, 0},       /* no_tls1 */
609     {SSL_OP_NO_TLSv1_1, 0},     /* no_tls1_1 */
610     {SSL_OP_NO_TLSv1_2, 0},     /* no_tls1_2 */
611     {SSL_OP_NO_TLSv1_3, 0},     /* no_tls1_3 */
612     {SSL_OP_ALL, 0},            /* bugs */
613     {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
614     {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
615     {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
616     {SSL_OP_NO_TICKET, 0},      /* no_ticket */
617     {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
618     /* legacy_renegotiation */
619     {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
620     /* legacy_server_connect */
621     {SSL_OP_LEGACY_SERVER_CONNECT, 0},
622     /* no_resumption_on_reneg */
623     {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
624     /* no_legacy_server_connect */
625     {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
626     {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
627 };
628
629 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
630 {
631     if (!pcmd || !*pcmd)
632         return 0;
633     /* If a prefix is set, check and skip */
634     if (cctx->prefix) {
635         if (strlen(*pcmd) <= cctx->prefixlen)
636             return 0;
637         if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
638             strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
639             return 0;
640         if (cctx->flags & SSL_CONF_FLAG_FILE &&
641             strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
642             return 0;
643         *pcmd += cctx->prefixlen;
644     } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
645         if (**pcmd != '-' || !(*pcmd)[1])
646             return 0;
647         *pcmd += 1;
648     }
649     return 1;
650 }
651
652 /* Determine if a command is allowed according to cctx flags */
653 static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t)
654 {
655     unsigned int tfl = t->flags;
656     unsigned int cfl = cctx->flags;
657     if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
658         return 0;
659     if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
660         return 0;
661     if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
662         && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
663         return 0;
664     return 1;
665 }
666
667 static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
668                                                    const char *cmd)
669 {
670     const ssl_conf_cmd_tbl *t;
671     size_t i;
672     if (cmd == NULL)
673         return NULL;
674
675     /* Look for matching parameter name in table */
676     for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
677         if (ssl_conf_cmd_allowed(cctx, t)) {
678             if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
679                 if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
680                     return t;
681             }
682             if (cctx->flags & SSL_CONF_FLAG_FILE) {
683                 if (t->str_file && strcasecmp(t->str_file, cmd) == 0)
684                     return t;
685             }
686         }
687     }
688     return NULL;
689 }
690
691 static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd)
692 {
693     /* Find index of command in table */
694     size_t idx = cmd - ssl_conf_cmds;
695     const ssl_switch_tbl *scmd;
696     /* Sanity check index */
697     if (idx >= OSSL_NELEM(ssl_cmd_switches))
698         return 0;
699     /* Obtain switches entry with same index */
700     scmd = ssl_cmd_switches + idx;
701     ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
702     return 1;
703 }
704
705 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
706 {
707     const ssl_conf_cmd_tbl *runcmd;
708     if (cmd == NULL) {
709         SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
710         return 0;
711     }
712
713     if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
714         return -2;
715
716     runcmd = ssl_conf_cmd_lookup(cctx, cmd);
717
718     if (runcmd) {
719         int rv;
720         if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
721             return ctrl_switch_option(cctx, runcmd);
722         }
723         if (value == NULL)
724             return -3;
725         rv = runcmd->cmd(cctx, value);
726         if (rv > 0)
727             return 2;
728         if (rv == -2)
729             return -2;
730         if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
731             SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
732             ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
733         }
734         return 0;
735     }
736
737     if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
738         SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
739         ERR_add_error_data(2, "cmd=", cmd);
740     }
741
742     return -2;
743 }
744
745 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
746 {
747     int rv;
748     const char *arg = NULL, *argn;
749     if (pargc && *pargc == 0)
750         return 0;
751     if (!pargc || *pargc > 0)
752         arg = **pargv;
753     if (arg == NULL)
754         return 0;
755     if (!pargc || *pargc > 1)
756         argn = (*pargv)[1];
757     else
758         argn = NULL;
759     cctx->flags &= ~SSL_CONF_FLAG_FILE;
760     cctx->flags |= SSL_CONF_FLAG_CMDLINE;
761     rv = SSL_CONF_cmd(cctx, arg, argn);
762     if (rv > 0) {
763         /* Success: update pargc, pargv */
764         (*pargv) += rv;
765         if (pargc)
766             (*pargc) -= rv;
767         return rv;
768     }
769     /* Unknown switch: indicate no arguments processed */
770     if (rv == -2)
771         return 0;
772     /* Some error occurred processing command, return fatal error */
773     if (rv == 0)
774         return -1;
775     return rv;
776 }
777
778 int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
779 {
780     if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
781         const ssl_conf_cmd_tbl *runcmd;
782         runcmd = ssl_conf_cmd_lookup(cctx, cmd);
783         if (runcmd)
784             return runcmd->value_type;
785     }
786     return SSL_CONF_TYPE_UNKNOWN;
787 }
788
789 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
790 {
791     SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
792
793     return ret;
794 }
795
796 int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
797 {
798     /* See if any certificates are missing private keys */
799     size_t i;
800     CERT *c = NULL;
801     if (cctx->ctx)
802         c = cctx->ctx->cert;
803     else if (cctx->ssl)
804         c = cctx->ssl->cert;
805     if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
806         for (i = 0; i < SSL_PKEY_NUM; i++) {
807             const char *p = cctx->cert_filename[i];
808             /*
809              * If missing private key try to load one from certificate file
810              */
811             if (p && !c->pkeys[i].privatekey) {
812                 if (!cmd_PrivateKey(cctx, p))
813                     return 0;
814             }
815         }
816     }
817     if (cctx->canames) {
818         if (cctx->ssl)
819             SSL_set0_CA_list(cctx->ssl, cctx->canames);
820         else if (cctx->ctx)
821             SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
822         else
823             sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
824         cctx->canames = NULL;
825     }
826     return 1;
827 }
828
829 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
830 {
831     if (cctx) {
832         size_t i;
833         for (i = 0; i < SSL_PKEY_NUM; i++)
834             OPENSSL_free(cctx->cert_filename[i]);
835         OPENSSL_free(cctx->prefix);
836         sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
837         OPENSSL_free(cctx);
838     }
839 }
840
841 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
842 {
843     cctx->flags |= flags;
844     return cctx->flags;
845 }
846
847 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
848 {
849     cctx->flags &= ~flags;
850     return cctx->flags;
851 }
852
853 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
854 {
855     char *tmp = NULL;
856     if (pre) {
857         tmp = OPENSSL_strdup(pre);
858         if (tmp == NULL)
859             return 0;
860     }
861     OPENSSL_free(cctx->prefix);
862     cctx->prefix = tmp;
863     if (tmp)
864         cctx->prefixlen = strlen(tmp);
865     else
866         cctx->prefixlen = 0;
867     return 1;
868 }
869
870 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
871 {
872     cctx->ssl = ssl;
873     cctx->ctx = NULL;
874     if (ssl) {
875         cctx->poptions = &ssl->options;
876         cctx->min_version = &ssl->min_proto_version;
877         cctx->max_version = &ssl->max_proto_version;
878         cctx->pcert_flags = &ssl->cert->cert_flags;
879         cctx->pvfy_flags = &ssl->verify_mode;
880     } else {
881         cctx->poptions = NULL;
882         cctx->min_version = NULL;
883         cctx->max_version = NULL;
884         cctx->pcert_flags = NULL;
885         cctx->pvfy_flags = NULL;
886     }
887 }
888
889 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
890 {
891     cctx->ctx = ctx;
892     cctx->ssl = NULL;
893     if (ctx) {
894         cctx->poptions = &ctx->options;
895         cctx->min_version = &ctx->min_proto_version;
896         cctx->max_version = &ctx->max_proto_version;
897         cctx->pcert_flags = &ctx->cert->cert_flags;
898         cctx->pvfy_flags = &ctx->verify_mode;
899     } else {
900         cctx->poptions = NULL;
901         cctx->min_version = NULL;
902         cctx->max_version = NULL;
903         cctx->pcert_flags = NULL;
904         cctx->pvfy_flags = NULL;
905     }
906 }