Implement a replacement for SSL_set_tmp_dh()
[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     int nid;
230
231     /* Ignore values supported by 1.0.2 for the automatic selection */
232     if ((cctx->flags & SSL_CONF_FLAG_FILE)
233             && (strcasecmp(value, "+automatic") == 0
234                 || strcasecmp(value, "automatic") == 0))
235         return 1;
236     if ((cctx->flags & SSL_CONF_FLAG_CMDLINE) &&
237         strcmp(value, "auto") == 0)
238         return 1;
239
240     nid = EC_curve_nist2nid(value);
241     if (nid == NID_undef)
242         nid = OBJ_sn2nid(value);
243     if (nid == 0)
244         return 0;
245
246     if (cctx->ctx)
247         rv = SSL_CTX_set1_groups(cctx->ctx, &nid, 1);
248     else if (cctx->ssl)
249         rv = SSL_set1_groups(cctx->ssl, &nid, 1);
250
251     return rv > 0;
252 }
253 #endif
254 static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
255 {
256     int rv = 1;
257
258     if (cctx->ctx)
259         rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
260     if (cctx->ssl)
261         rv = SSL_set_cipher_list(cctx->ssl, value);
262     return rv > 0;
263 }
264
265 static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
266 {
267     int rv = 1;
268
269     if (cctx->ctx)
270         rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
271     if (cctx->ssl)
272         rv = SSL_set_ciphersuites(cctx->ssl, value);
273     return rv > 0;
274 }
275
276 static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
277 {
278     static const ssl_flag_tbl ssl_protocol_list[] = {
279         SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
280         SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
281         SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
282         SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
283         SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
284         SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
285         SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
286         SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
287         SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
288     };
289     cctx->tbl = ssl_protocol_list;
290     cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
291     return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
292 }
293
294 /*
295  * protocol_from_string - converts a protocol version string to a number
296  *
297  * Returns -1 on failure or the version on success
298  */
299 static int protocol_from_string(const char *value)
300 {
301     struct protocol_versions {
302         const char *name;
303         int version;
304     };
305     /*
306      * Note: To avoid breaking previously valid configurations, we must retain
307      * legacy entries in this table even if the underlying protocol is no
308      * longer supported.  This also means that the constants SSL3_VERSION, ...
309      * need to be retained indefinitely.  This table can only grow, never
310      * shrink.
311      */
312     static const struct protocol_versions versions[] = {
313         {"None", 0},
314         {"SSLv3", SSL3_VERSION},
315         {"TLSv1", TLS1_VERSION},
316         {"TLSv1.1", TLS1_1_VERSION},
317         {"TLSv1.2", TLS1_2_VERSION},
318         {"TLSv1.3", TLS1_3_VERSION},
319         {"DTLSv1", DTLS1_VERSION},
320         {"DTLSv1.2", DTLS1_2_VERSION}
321     };
322     size_t i;
323     size_t n = OSSL_NELEM(versions);
324
325     for (i = 0; i < n; i++)
326         if (strcmp(versions[i].name, value) == 0)
327             return versions[i].version;
328     return -1;
329 }
330
331 static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
332 {
333     int method_version;
334     int new_version;
335
336     if (cctx->ctx != NULL)
337         method_version = cctx->ctx->method->version;
338     else if (cctx->ssl != NULL)
339         method_version = cctx->ssl->ctx->method->version;
340     else
341         return 0;
342     if ((new_version = protocol_from_string(value)) < 0)
343         return 0;
344     return ssl_set_version_bound(method_version, new_version, bound);
345 }
346
347 /*
348  * cmd_MinProtocol - Set min protocol version
349  * @cctx: config structure to save settings in
350  * @value: The min protocol version in string form
351  *
352  * Returns 1 on success and 0 on failure.
353  */
354 static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
355 {
356     return min_max_proto(cctx, value, cctx->min_version);
357 }
358
359 /*
360  * cmd_MaxProtocol - Set max protocol version
361  * @cctx: config structure to save settings in
362  * @value: The max protocol version in string form
363  *
364  * Returns 1 on success and 0 on failure.
365  */
366 static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
367 {
368     return min_max_proto(cctx, value, cctx->max_version);
369 }
370
371 static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
372 {
373     static const ssl_flag_tbl ssl_option_list[] = {
374         SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
375         SSL_FLAG_TBL_INV("EmptyFragments",
376                          SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
377         SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
378         SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
379         SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
380         SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
381                          SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
382         SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
383         SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
384         SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
385                      SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
386         SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
387         SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
388         SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
389         SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
390         SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
391         SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
392         SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
393         SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES)
394     };
395     if (value == NULL)
396         return -3;
397     cctx->tbl = ssl_option_list;
398     cctx->ntbl = OSSL_NELEM(ssl_option_list);
399     return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
400 }
401
402 static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
403 {
404     static const ssl_flag_tbl ssl_vfy_list[] = {
405         SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
406         SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
407         SSL_FLAG_VFY_SRV("Require",
408                          SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
409         SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
410         SSL_FLAG_VFY_SRV("RequestPostHandshake",
411                          SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
412         SSL_FLAG_VFY_SRV("RequirePostHandshake",
413                          SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
414                          SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
415     };
416     if (value == NULL)
417         return -3;
418     cctx->tbl = ssl_vfy_list;
419     cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
420     return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
421 }
422
423 static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
424 {
425     int rv = 1;
426     CERT *c = NULL;
427     if (cctx->ctx) {
428         rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
429         c = cctx->ctx->cert;
430     }
431     if (cctx->ssl) {
432         rv = SSL_use_certificate_chain_file(cctx->ssl, value);
433         c = cctx->ssl->cert;
434     }
435     if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
436         char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
437         OPENSSL_free(*pfilename);
438         *pfilename = OPENSSL_strdup(value);
439         if (*pfilename == NULL)
440             rv = 0;
441     }
442
443     return rv > 0;
444 }
445
446 static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
447 {
448     int rv = 1;
449     if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
450         return -2;
451     if (cctx->ctx)
452         rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
453     if (cctx->ssl)
454         rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
455     return rv > 0;
456 }
457
458 static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
459 {
460     int rv = 1;
461     if (cctx->ctx)
462         rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
463     return rv > 0;
464 }
465
466 static int do_store(SSL_CONF_CTX *cctx,
467                     const char *CAfile, const char *CApath, const char *CAstore,
468                     int verify_store)
469 {
470     CERT *cert;
471     X509_STORE **st;
472     SSL_CTX *ctx;
473     OSSL_LIB_CTX *libctx = NULL;
474     const char *propq = NULL;
475
476     if (cctx->ctx != NULL) {
477         cert = cctx->ctx->cert;
478         ctx = cctx->ctx;
479     } else if (cctx->ssl != NULL) {
480         cert = cctx->ssl->cert;
481         ctx = cctx->ssl->ctx;
482     } else {
483         return 1;
484     }
485     if (ctx != NULL) {
486         libctx = ctx->libctx;
487         propq = ctx->propq;
488     }
489     st = verify_store ? &cert->verify_store : &cert->chain_store;
490     if (*st == NULL) {
491         *st = X509_STORE_new();
492         if (*st == NULL)
493             return 0;
494     }
495
496     if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
497         return 0;
498     if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
499         return 0;
500     if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx,
501                                                      propq))
502         return 0;
503     return 1;
504 }
505
506 static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
507 {
508     return do_store(cctx, NULL, value, NULL, 0);
509 }
510
511 static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
512 {
513     return do_store(cctx, value, NULL, NULL, 0);
514 }
515
516 static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value)
517 {
518     return do_store(cctx, NULL, NULL, value, 0);
519 }
520
521 static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
522 {
523     return do_store(cctx, NULL, value, NULL, 1);
524 }
525
526 static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
527 {
528     return do_store(cctx, value, NULL, NULL, 1);
529 }
530
531 static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value)
532 {
533     return do_store(cctx, NULL, NULL, value, 1);
534 }
535
536 static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
537 {
538     if (cctx->canames == NULL)
539         cctx->canames = sk_X509_NAME_new_null();
540     if (cctx->canames == NULL)
541         return 0;
542     return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
543 }
544
545 static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
546 {
547     return cmd_RequestCAFile(cctx, value);
548 }
549
550 static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
551 {
552     if (cctx->canames == NULL)
553         cctx->canames = sk_X509_NAME_new_null();
554     if (cctx->canames == NULL)
555         return 0;
556     return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
557 }
558
559 static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
560 {
561     return cmd_RequestCAPath(cctx, value);
562 }
563
564 static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value)
565 {
566     if (cctx->canames == NULL)
567         cctx->canames = sk_X509_NAME_new_null();
568     if (cctx->canames == NULL)
569         return 0;
570     return SSL_add_store_cert_subjects_to_stack(cctx->canames, value);
571 }
572
573 static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value)
574 {
575     return cmd_RequestCAStore(cctx, value);
576 }
577
578 static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
579 {
580     int rv = 0;
581     EVP_PKEY *dhpkey = NULL;
582     BIO *in = NULL;
583     SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx;
584     OSSL_DECODER_CTX *decoderctx = NULL;
585
586     if (cctx->ctx != NULL || cctx->ssl != NULL) {
587         in = BIO_new(BIO_s_file());
588         if (in == NULL)
589             goto end;
590         if (BIO_read_filename(in, value) <= 0)
591             goto end;
592
593         decoderctx
594             = OSSL_DECODER_CTX_new_by_EVP_PKEY(&dhpkey, "PEM", NULL, "DH",
595                                                OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
596                                                sslctx->libctx, sslctx->propq);
597         if (decoderctx == NULL
598                 || !OSSL_DECODER_from_bio(decoderctx, in)) {
599             OSSL_DECODER_CTX_free(decoderctx);
600             goto end;
601         }
602         OSSL_DECODER_CTX_free(decoderctx);
603
604         if (dhpkey == NULL)
605             goto end;
606     } else {
607         return 1;
608     }
609
610     if (cctx->ctx != NULL) {
611         if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
612             dhpkey = NULL;
613     }
614     if (cctx->ssl != NULL) {
615         if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
616             dhpkey = NULL;
617     }
618  end:
619     EVP_PKEY_free(dhpkey);
620     BIO_free(in);
621     return rv > 0;
622 }
623
624 static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
625 {
626     int rv = 0;
627     int block_size = atoi(value);
628
629     /*
630      * All we care about is a non-negative value,
631      * the setters check the range
632      */
633     if (block_size >= 0) {
634         if (cctx->ctx)
635             rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
636         if (cctx->ssl)
637             rv = SSL_set_block_padding(cctx->ssl, block_size);
638     }
639     return rv;
640 }
641
642
643 static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
644 {
645     int rv = 0;
646     int num_tickets = atoi(value);
647
648     if (num_tickets >= 0) {
649         if (cctx->ctx)
650             rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
651         if (cctx->ssl)
652             rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
653     }
654     return rv;
655 }
656
657 typedef struct {
658     int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
659     const char *str_file;
660     const char *str_cmdline;
661     unsigned short flags;
662     unsigned short value_type;
663 } ssl_conf_cmd_tbl;
664
665 /* Table of supported parameters */
666
667 #define SSL_CONF_CMD(name, cmdopt, flags, type) \
668         {cmd_##name, #name, cmdopt, flags, type}
669
670 #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
671         SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
672
673 #define SSL_CONF_CMD_SWITCH(name, flags) \
674         {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
675
676 /* See apps/apps.h if you change this table. */
677 static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
678     SSL_CONF_CMD_SWITCH("no_ssl3", 0),
679     SSL_CONF_CMD_SWITCH("no_tls1", 0),
680     SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
681     SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
682     SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
683     SSL_CONF_CMD_SWITCH("bugs", 0),
684     SSL_CONF_CMD_SWITCH("no_comp", 0),
685     SSL_CONF_CMD_SWITCH("comp", 0),
686     SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
687     SSL_CONF_CMD_SWITCH("no_ticket", 0),
688     SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
689     SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
690     SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER),
691     SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
692     SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
693     SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER),
694     SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
695     SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
696     SSL_CONF_CMD_SWITCH("strict", 0),
697     SSL_CONF_CMD_SWITCH("no_middlebox", 0),
698     SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
699     SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
700     SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
701     SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
702     SSL_CONF_CMD_STRING(Curves, "curves", 0),
703     SSL_CONF_CMD_STRING(Groups, "groups", 0),
704 #ifndef OPENSSL_NO_EC
705     SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
706 #endif
707     SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
708     SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
709     SSL_CONF_CMD_STRING(Protocol, NULL, 0),
710     SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
711     SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
712     SSL_CONF_CMD_STRING(Options, NULL, 0),
713     SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
714     SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
715                  SSL_CONF_TYPE_FILE),
716     SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
717                  SSL_CONF_TYPE_FILE),
718     SSL_CONF_CMD(ServerInfoFile, NULL,
719                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
720                  SSL_CONF_TYPE_FILE),
721     SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
722                  SSL_CONF_TYPE_DIR),
723     SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
724                  SSL_CONF_TYPE_FILE),
725     SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
726                  SSL_CONF_TYPE_STORE),
727     SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
728                  SSL_CONF_TYPE_DIR),
729     SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
730                  SSL_CONF_TYPE_FILE),
731     SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
732                  SSL_CONF_TYPE_STORE),
733     SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
734                  SSL_CONF_TYPE_FILE),
735     SSL_CONF_CMD(ClientCAFile, NULL,
736                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
737                  SSL_CONF_TYPE_FILE),
738     SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
739                  SSL_CONF_TYPE_DIR),
740     SSL_CONF_CMD(ClientCAPath, NULL,
741                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
742                  SSL_CONF_TYPE_DIR),
743     SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
744                  SSL_CONF_TYPE_STORE),
745     SSL_CONF_CMD(ClientCAStore, NULL,
746                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
747                  SSL_CONF_TYPE_STORE),
748     SSL_CONF_CMD(DHParameters, "dhparam",
749                  SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
750                  SSL_CONF_TYPE_FILE),
751     SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
752     SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
753 };
754
755 /* Supported switches: must match order of switches in ssl_conf_cmds */
756 static const ssl_switch_tbl ssl_cmd_switches[] = {
757     {SSL_OP_NO_SSLv3, 0},       /* no_ssl3 */
758     {SSL_OP_NO_TLSv1, 0},       /* no_tls1 */
759     {SSL_OP_NO_TLSv1_1, 0},     /* no_tls1_1 */
760     {SSL_OP_NO_TLSv1_2, 0},     /* no_tls1_2 */
761     {SSL_OP_NO_TLSv1_3, 0},     /* no_tls1_3 */
762     {SSL_OP_ALL, 0},            /* bugs */
763     {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
764     {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
765     {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
766     {SSL_OP_NO_TICKET, 0},      /* no_ticket */
767     {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
768     /* legacy_renegotiation */
769     {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
770     /* legacy_server_connect */
771     {SSL_OP_LEGACY_SERVER_CONNECT, 0},
772     /* no_renegotiation */
773     {SSL_OP_NO_RENEGOTIATION, 0},
774     /* no_resumption_on_reneg */
775     {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
776     /* no_legacy_server_connect */
777     {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
778     /* allow_no_dhe_kex */
779     {SSL_OP_ALLOW_NO_DHE_KEX, 0},
780     /* chacha reprioritization */
781     {SSL_OP_PRIORITIZE_CHACHA, 0},
782     {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
783     /* no_middlebox */
784     {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
785     /* anti_replay */
786     {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
787     /* no_anti_replay */
788     {SSL_OP_NO_ANTI_REPLAY, 0},
789 };
790
791 static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
792 {
793     if (pcmd == NULL || *pcmd == NULL)
794         return 0;
795     /* If a prefix is set, check and skip */
796     if (cctx->prefix) {
797         if (strlen(*pcmd) <= cctx->prefixlen)
798             return 0;
799         if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
800             strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
801             return 0;
802         if (cctx->flags & SSL_CONF_FLAG_FILE &&
803             strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
804             return 0;
805         *pcmd += cctx->prefixlen;
806     } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
807         if (**pcmd != '-' || !(*pcmd)[1])
808             return 0;
809         *pcmd += 1;
810     }
811     return 1;
812 }
813
814 /* Determine if a command is allowed according to cctx flags */
815 static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t)
816 {
817     unsigned int tfl = t->flags;
818     unsigned int cfl = cctx->flags;
819     if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
820         return 0;
821     if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
822         return 0;
823     if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
824         && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
825         return 0;
826     return 1;
827 }
828
829 static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
830                                                    const char *cmd)
831 {
832     const ssl_conf_cmd_tbl *t;
833     size_t i;
834     if (cmd == NULL)
835         return NULL;
836
837     /* Look for matching parameter name in table */
838     for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
839         if (ssl_conf_cmd_allowed(cctx, t)) {
840             if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
841                 if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
842                     return t;
843             }
844             if (cctx->flags & SSL_CONF_FLAG_FILE) {
845                 if (t->str_file && strcasecmp(t->str_file, cmd) == 0)
846                     return t;
847             }
848         }
849     }
850     return NULL;
851 }
852
853 static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd)
854 {
855     /* Find index of command in table */
856     size_t idx = cmd - ssl_conf_cmds;
857     const ssl_switch_tbl *scmd;
858     /* Sanity check index */
859     if (idx >= OSSL_NELEM(ssl_cmd_switches))
860         return 0;
861     /* Obtain switches entry with same index */
862     scmd = ssl_cmd_switches + idx;
863     ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
864     return 1;
865 }
866
867 int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
868 {
869     const ssl_conf_cmd_tbl *runcmd;
870     if (cmd == NULL) {
871         ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
872         return 0;
873     }
874
875     if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
876         return -2;
877
878     runcmd = ssl_conf_cmd_lookup(cctx, cmd);
879
880     if (runcmd) {
881         int rv;
882         if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
883             return ctrl_switch_option(cctx, runcmd);
884         }
885         if (value == NULL)
886             return -3;
887         rv = runcmd->cmd(cctx, value);
888         if (rv > 0)
889             return 2;
890         if (rv == -2)
891             return -2;
892         if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
893             ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
894                            "cmd=%s, value=%s", cmd, value);
895         return 0;
896     }
897
898     if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
899         ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
900
901     return -2;
902 }
903
904 int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
905 {
906     int rv;
907     const char *arg = NULL, *argn;
908
909     if (pargc != NULL && *pargc == 0)
910         return 0;
911     if (pargc == NULL || *pargc > 0)
912         arg = **pargv;
913     if (arg == NULL)
914         return 0;
915     if (pargc == NULL || *pargc > 1)
916         argn = (*pargv)[1];
917     else
918         argn = NULL;
919     cctx->flags &= ~SSL_CONF_FLAG_FILE;
920     cctx->flags |= SSL_CONF_FLAG_CMDLINE;
921     rv = SSL_CONF_cmd(cctx, arg, argn);
922     if (rv > 0) {
923         /* Success: update pargc, pargv */
924         (*pargv) += rv;
925         if (pargc)
926             (*pargc) -= rv;
927         return rv;
928     }
929     /* Unknown switch: indicate no arguments processed */
930     if (rv == -2)
931         return 0;
932     /* Some error occurred processing command, return fatal error */
933     if (rv == 0)
934         return -1;
935     return rv;
936 }
937
938 int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
939 {
940     if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
941         const ssl_conf_cmd_tbl *runcmd;
942         runcmd = ssl_conf_cmd_lookup(cctx, cmd);
943         if (runcmd)
944             return runcmd->value_type;
945     }
946     return SSL_CONF_TYPE_UNKNOWN;
947 }
948
949 SSL_CONF_CTX *SSL_CONF_CTX_new(void)
950 {
951     SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
952
953     return ret;
954 }
955
956 int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
957 {
958     /* See if any certificates are missing private keys */
959     size_t i;
960     CERT *c = NULL;
961     if (cctx->ctx)
962         c = cctx->ctx->cert;
963     else if (cctx->ssl)
964         c = cctx->ssl->cert;
965     if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
966         for (i = 0; i < SSL_PKEY_NUM; i++) {
967             const char *p = cctx->cert_filename[i];
968             /*
969              * If missing private key try to load one from certificate file
970              */
971             if (p && !c->pkeys[i].privatekey) {
972                 if (!cmd_PrivateKey(cctx, p))
973                     return 0;
974             }
975         }
976     }
977     if (cctx->canames) {
978         if (cctx->ssl)
979             SSL_set0_CA_list(cctx->ssl, cctx->canames);
980         else if (cctx->ctx)
981             SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
982         else
983             sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
984         cctx->canames = NULL;
985     }
986     return 1;
987 }
988
989 void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
990 {
991     if (cctx) {
992         size_t i;
993         for (i = 0; i < SSL_PKEY_NUM; i++)
994             OPENSSL_free(cctx->cert_filename[i]);
995         OPENSSL_free(cctx->prefix);
996         sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
997         OPENSSL_free(cctx);
998     }
999 }
1000
1001 unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1002 {
1003     cctx->flags |= flags;
1004     return cctx->flags;
1005 }
1006
1007 unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
1008 {
1009     cctx->flags &= ~flags;
1010     return cctx->flags;
1011 }
1012
1013 int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
1014 {
1015     char *tmp = NULL;
1016     if (pre) {
1017         tmp = OPENSSL_strdup(pre);
1018         if (tmp == NULL)
1019             return 0;
1020     }
1021     OPENSSL_free(cctx->prefix);
1022     cctx->prefix = tmp;
1023     if (tmp)
1024         cctx->prefixlen = strlen(tmp);
1025     else
1026         cctx->prefixlen = 0;
1027     return 1;
1028 }
1029
1030 void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
1031 {
1032     cctx->ssl = ssl;
1033     cctx->ctx = NULL;
1034     if (ssl) {
1035         cctx->poptions = &ssl->options;
1036         cctx->min_version = &ssl->min_proto_version;
1037         cctx->max_version = &ssl->max_proto_version;
1038         cctx->pcert_flags = &ssl->cert->cert_flags;
1039         cctx->pvfy_flags = &ssl->verify_mode;
1040     } else {
1041         cctx->poptions = NULL;
1042         cctx->min_version = NULL;
1043         cctx->max_version = NULL;
1044         cctx->pcert_flags = NULL;
1045         cctx->pvfy_flags = NULL;
1046     }
1047 }
1048
1049 void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
1050 {
1051     cctx->ctx = ctx;
1052     cctx->ssl = NULL;
1053     if (ctx) {
1054         cctx->poptions = &ctx->options;
1055         cctx->min_version = &ctx->min_proto_version;
1056         cctx->max_version = &ctx->max_proto_version;
1057         cctx->pcert_flags = &ctx->cert->cert_flags;
1058         cctx->pvfy_flags = &ctx->verify_mode;
1059     } else {
1060         cctx->poptions = NULL;
1061         cctx->min_version = NULL;
1062         cctx->max_version = NULL;
1063         cctx->pcert_flags = NULL;
1064         cctx->pvfy_flags = NULL;
1065     }
1066 }