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