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