]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
a28d06f3 | 2 | * Copyright 2012-2021 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 |
23 | typedef 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 */ |
31 | typedef 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 |
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 */ | |
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 | 106 | static 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 | 142 | static 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; | |
a230b26e | 151 | } else if (tbl->namelen != namelen || strncasecmp(tbl->name, name, namelen)) |
0f113f3e | 152 | return 0; |
656b2605 | 153 | ssl_set_option(cctx, tbl->name_flags, tbl->option_value, onoff); |
0f113f3e MC |
154 | return 1; |
155 | } | |
3db935a9 DSH |
156 | |
157 | static int ssl_set_option_list(const char *elem, int len, void *usr) | |
0f113f3e MC |
158 | { |
159 | SSL_CONF_CTX *cctx = usr; | |
160 | size_t i; | |
161 | const ssl_flag_tbl *tbl; | |
162 | int onoff = 1; | |
163 | /* | |
164 | * len == -1 indicates not being called in list context, just for single | |
165 | * command line switches, so don't allow +, -. | |
166 | */ | |
2747d73c KR |
167 | if (elem == NULL) |
168 | return 0; | |
0f113f3e MC |
169 | if (len != -1) { |
170 | if (*elem == '+') { | |
171 | elem++; | |
172 | len--; | |
173 | onoff = 1; | |
174 | } else if (*elem == '-') { | |
175 | elem++; | |
176 | len--; | |
177 | onoff = 0; | |
178 | } | |
179 | } | |
180 | for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++) { | |
181 | if (ssl_match_option(cctx, tbl, elem, len, onoff)) | |
182 | return 1; | |
183 | } | |
184 | return 0; | |
185 | } | |
3db935a9 | 186 | |
3db935a9 | 187 | /* Set supported signature algorithms */ |
ec2f7e56 | 188 | static int cmd_SignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
189 | { |
190 | int rv; | |
191 | if (cctx->ssl) | |
192 | rv = SSL_set1_sigalgs_list(cctx->ssl, value); | |
193 | /* NB: ctx == NULL performs syntax checking only */ | |
194 | else | |
195 | rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value); | |
196 | return rv > 0; | |
197 | } | |
198 | ||
3db935a9 | 199 | /* Set supported client signature algorithms */ |
a230b26e | 200 | static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
201 | { |
202 | int rv; | |
203 | if (cctx->ssl) | |
204 | rv = SSL_set1_client_sigalgs_list(cctx->ssl, value); | |
205 | /* NB: ctx == NULL performs syntax checking only */ | |
206 | else | |
207 | rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value); | |
208 | return rv > 0; | |
209 | } | |
3db935a9 | 210 | |
de4d764e | 211 | static int cmd_Groups(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
212 | { |
213 | int rv; | |
214 | if (cctx->ssl) | |
de4d764e | 215 | rv = SSL_set1_groups_list(cctx->ssl, value); |
0f113f3e MC |
216 | /* NB: ctx == NULL performs syntax checking only */ |
217 | else | |
de4d764e | 218 | rv = SSL_CTX_set1_groups_list(cctx->ctx, value); |
0f113f3e MC |
219 | return rv > 0; |
220 | } | |
221 | ||
de4d764e MC |
222 | /* This is the old name for cmd_Groups - retained for backwards compatibility */ |
223 | static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value) | |
224 | { | |
225 | return cmd_Groups(cctx, value); | |
226 | } | |
227 | ||
3db935a9 | 228 | /* ECDH temporary parameters */ |
ec2f7e56 | 229 | static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e | 230 | { |
fe6ef247 | 231 | int rv = 1; |
0f113f3e | 232 | |
1c7aa0db | 233 | /* Ignore values supported by 1.0.2 for the automatic selection */ |
ededc88d MC |
234 | if ((cctx->flags & SSL_CONF_FLAG_FILE) |
235 | && (strcasecmp(value, "+automatic") == 0 | |
236 | || strcasecmp(value, "automatic") == 0)) | |
1c7aa0db TM |
237 | return 1; |
238 | if ((cctx->flags & SSL_CONF_FLAG_CMDLINE) && | |
239 | strcmp(value, "auto") == 0) | |
240 | return 1; | |
241 | ||
462f4f4b MC |
242 | /* ECDHParameters accepts a single group name */ |
243 | if (strstr(value, ":") != NULL) | |
fe6ef247 | 244 | return 0; |
9b1c0e00 | 245 | |
fe6ef247 | 246 | if (cctx->ctx) |
462f4f4b | 247 | rv = SSL_CTX_set1_groups_list(cctx->ctx, value); |
fe6ef247 | 248 | else if (cctx->ssl) |
462f4f4b | 249 | rv = SSL_set1_groups_list(cctx->ssl, value); |
0f113f3e MC |
250 | |
251 | return rv > 0; | |
252 | } | |
462f4f4b | 253 | |
ec2f7e56 | 254 | static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
255 | { |
256 | int rv = 1; | |
f865b081 | 257 | |
0f113f3e MC |
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 | } | |
3db935a9 | 264 | |
f865b081 MC |
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 | ||
ec2f7e56 | 276 | static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
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), | |
7946ab33 | 284 | SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2), |
582a17d6 | 285 | SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3), |
7946ab33 KR |
286 | SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1), |
287 | SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2) | |
0f113f3e | 288 | }; |
0f113f3e | 289 | cctx->tbl = ssl_protocol_list; |
b6eb9827 | 290 | cctx->ntbl = OSSL_NELEM(ssl_protocol_list); |
0f113f3e MC |
291 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); |
292 | } | |
3db935a9 | 293 | |
7946ab33 KR |
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 | }; | |
77174598 VD |
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 | */ | |
7946ab33 | 312 | static const struct protocol_versions versions[] = { |
869e978c | 313 | {"None", 0}, |
7946ab33 KR |
314 | {"SSLv3", SSL3_VERSION}, |
315 | {"TLSv1", TLS1_VERSION}, | |
316 | {"TLSv1.1", TLS1_1_VERSION}, | |
317 | {"TLSv1.2", TLS1_2_VERSION}, | |
582a17d6 | 318 | {"TLSv1.3", TLS1_3_VERSION}, |
7946ab33 | 319 | {"DTLSv1", DTLS1_VERSION}, |
a230b26e EK |
320 | {"DTLSv1.2", DTLS1_2_VERSION} |
321 | }; | |
7946ab33 KR |
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 | ||
4fa52141 VD |
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 | ||
7946ab33 KR |
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 | { | |
4fa52141 | 356 | return min_max_proto(cctx, value, cctx->min_version); |
7946ab33 KR |
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 | { | |
4fa52141 | 368 | return min_max_proto(cctx, value, cctx->max_version); |
7946ab33 KR |
369 | } |
370 | ||
ec2f7e56 | 371 | static int cmd_Options(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
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), | |
55373bfd RS |
386 | SSL_FLAG_TBL("ClientRenegotiation", |
387 | SSL_OP_ALLOW_CLIENT_RENEGOTIATION), | |
b3618f44 | 388 | SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC), |
db0f35dd | 389 | SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION), |
e1c7871d | 390 | SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX), |
a5816a5a | 391 | SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA), |
3bb5e5b0 | 392 | SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT), |
088dfa13 | 393 | SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY), |
90fc2c26 | 394 | SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET), |
a3a54179 MC |
395 | SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES), |
396 | SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS) | |
0f113f3e | 397 | }; |
0f113f3e MC |
398 | if (value == NULL) |
399 | return -3; | |
400 | cctx->tbl = ssl_option_list; | |
b6eb9827 | 401 | cctx->ntbl = OSSL_NELEM(ssl_option_list); |
0f113f3e MC |
402 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); |
403 | } | |
3db935a9 | 404 | |
429261d0 DSH |
405 | static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value) |
406 | { | |
407 | static const ssl_flag_tbl ssl_vfy_list[] = { | |
408 | SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER), | |
409 | SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER), | |
410 | SSL_FLAG_VFY_SRV("Require", | |
411 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), | |
9d75dce3 TS |
412 | SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE), |
413 | SSL_FLAG_VFY_SRV("RequestPostHandshake", | |
414 | SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE), | |
415 | SSL_FLAG_VFY_SRV("RequirePostHandshake", | |
416 | SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE | | |
417 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), | |
429261d0 DSH |
418 | }; |
419 | if (value == NULL) | |
420 | return -3; | |
421 | cctx->tbl = ssl_vfy_list; | |
422 | cctx->ntbl = OSSL_NELEM(ssl_vfy_list); | |
423 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); | |
424 | } | |
425 | ||
ec2f7e56 | 426 | static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
427 | { |
428 | int rv = 1; | |
2011b169 | 429 | CERT *c = NULL; |
2011b169 | 430 | if (cctx->ctx) { |
0f113f3e | 431 | rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value); |
2011b169 DSH |
432 | c = cctx->ctx->cert; |
433 | } | |
434 | if (cctx->ssl) { | |
fae4772c | 435 | rv = SSL_use_certificate_chain_file(cctx->ssl, value); |
2011b169 DSH |
436 | c = cctx->ssl->cert; |
437 | } | |
438 | if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) { | |
439 | char **pfilename = &cctx->cert_filename[c->key - c->pkeys]; | |
b548a1f1 | 440 | OPENSSL_free(*pfilename); |
7644a9ae | 441 | *pfilename = OPENSSL_strdup(value); |
12a765a5 | 442 | if (*pfilename == NULL) |
2011b169 DSH |
443 | rv = 0; |
444 | } | |
445 | ||
0f113f3e MC |
446 | return rv > 0; |
447 | } | |
ec2f7e56 DSH |
448 | |
449 | static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
450 | { |
451 | int rv = 1; | |
452 | if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE)) | |
453 | return -2; | |
454 | if (cctx->ctx) | |
455 | rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM); | |
456 | if (cctx->ssl) | |
457 | rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM); | |
458 | return rv > 0; | |
459 | } | |
5b7f36e8 DSH |
460 | |
461 | static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
462 | { |
463 | int rv = 1; | |
0f113f3e MC |
464 | if (cctx->ctx) |
465 | rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value); | |
466 | return rv > 0; | |
467 | } | |
5b7f36e8 | 468 | |
429261d0 | 469 | static int do_store(SSL_CONF_CTX *cctx, |
6dcb100f RL |
470 | const char *CAfile, const char *CApath, const char *CAstore, |
471 | int verify_store) | |
429261d0 DSH |
472 | { |
473 | CERT *cert; | |
474 | X509_STORE **st; | |
6725682d | 475 | SSL_CTX *ctx; |
b4250010 | 476 | OSSL_LIB_CTX *libctx = NULL; |
6725682d | 477 | const char *propq = NULL; |
6dcb100f | 478 | |
6725682d | 479 | if (cctx->ctx != NULL) { |
429261d0 | 480 | cert = cctx->ctx->cert; |
6725682d SL |
481 | ctx = cctx->ctx; |
482 | } else if (cctx->ssl != NULL) { | |
429261d0 | 483 | cert = cctx->ssl->cert; |
6725682d SL |
484 | ctx = cctx->ssl->ctx; |
485 | } else { | |
429261d0 | 486 | return 1; |
6725682d SL |
487 | } |
488 | if (ctx != NULL) { | |
489 | libctx = ctx->libctx; | |
490 | propq = ctx->propq; | |
491 | } | |
429261d0 DSH |
492 | st = verify_store ? &cert->verify_store : &cert->chain_store; |
493 | if (*st == NULL) { | |
494 | *st = X509_STORE_new(); | |
495 | if (*st == NULL) | |
496 | return 0; | |
497 | } | |
6dcb100f | 498 | |
d8652be0 | 499 | if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq)) |
6dcb100f RL |
500 | return 0; |
501 | if (CApath != NULL && !X509_STORE_load_path(*st, CApath)) | |
502 | return 0; | |
d8652be0 MC |
503 | if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx, |
504 | propq)) | |
6dcb100f RL |
505 | return 0; |
506 | return 1; | |
429261d0 DSH |
507 | } |
508 | ||
509 | static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value) | |
510 | { | |
6dcb100f | 511 | return do_store(cctx, NULL, value, NULL, 0); |
429261d0 DSH |
512 | } |
513 | ||
514 | static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value) | |
515 | { | |
6dcb100f RL |
516 | return do_store(cctx, value, NULL, NULL, 0); |
517 | } | |
518 | ||
519 | static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value) | |
520 | { | |
521 | return do_store(cctx, NULL, NULL, value, 0); | |
429261d0 DSH |
522 | } |
523 | ||
524 | static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value) | |
525 | { | |
6dcb100f | 526 | return do_store(cctx, NULL, value, NULL, 1); |
429261d0 DSH |
527 | } |
528 | ||
529 | static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value) | |
530 | { | |
6dcb100f RL |
531 | return do_store(cctx, value, NULL, NULL, 1); |
532 | } | |
533 | ||
534 | static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value) | |
535 | { | |
536 | return do_store(cctx, NULL, NULL, value, 1); | |
429261d0 DSH |
537 | } |
538 | ||
be885d50 | 539 | static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value) |
429261d0 DSH |
540 | { |
541 | if (cctx->canames == NULL) | |
542 | cctx->canames = sk_X509_NAME_new_null(); | |
543 | if (cctx->canames == NULL) | |
544 | return 0; | |
545 | return SSL_add_file_cert_subjects_to_stack(cctx->canames, value); | |
546 | } | |
547 | ||
be885d50 DSH |
548 | static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value) |
549 | { | |
550 | return cmd_RequestCAFile(cctx, value); | |
551 | } | |
552 | ||
553 | static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value) | |
429261d0 DSH |
554 | { |
555 | if (cctx->canames == NULL) | |
556 | cctx->canames = sk_X509_NAME_new_null(); | |
557 | if (cctx->canames == NULL) | |
558 | return 0; | |
559 | return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value); | |
560 | } | |
561 | ||
be885d50 DSH |
562 | static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value) |
563 | { | |
564 | return cmd_RequestCAPath(cctx, value); | |
565 | } | |
566 | ||
6dcb100f RL |
567 | static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value) |
568 | { | |
569 | if (cctx->canames == NULL) | |
570 | cctx->canames = sk_X509_NAME_new_null(); | |
571 | if (cctx->canames == NULL) | |
572 | return 0; | |
573 | return SSL_add_store_cert_subjects_to_stack(cctx->canames, value); | |
574 | } | |
575 | ||
576 | static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value) | |
577 | { | |
578 | return cmd_RequestCAStore(cctx, value); | |
579 | } | |
580 | ||
c557f921 | 581 | static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
582 | { |
583 | int rv = 0; | |
163f6dc1 | 584 | EVP_PKEY *dhpkey = NULL; |
0f113f3e | 585 | BIO *in = NULL; |
163f6dc1 MC |
586 | SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx; |
587 | OSSL_DECODER_CTX *decoderctx = NULL; | |
588 | ||
589 | if (cctx->ctx != NULL || cctx->ssl != NULL) { | |
9982cbbb | 590 | in = BIO_new(BIO_s_file()); |
a71edf3b | 591 | if (in == NULL) |
0f113f3e MC |
592 | goto end; |
593 | if (BIO_read_filename(in, value) <= 0) | |
594 | goto end; | |
163f6dc1 MC |
595 | |
596 | decoderctx | |
fe75766c TM |
597 | = OSSL_DECODER_CTX_new_for_pkey(&dhpkey, "PEM", NULL, "DH", |
598 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, | |
599 | sslctx->libctx, sslctx->propq); | |
163f6dc1 MC |
600 | if (decoderctx == NULL |
601 | || !OSSL_DECODER_from_bio(decoderctx, in)) { | |
602 | OSSL_DECODER_CTX_free(decoderctx); | |
603 | goto end; | |
604 | } | |
605 | OSSL_DECODER_CTX_free(decoderctx); | |
606 | ||
607 | if (dhpkey == NULL) | |
0f113f3e | 608 | goto end; |
163f6dc1 | 609 | } else { |
0f113f3e | 610 | return 1; |
163f6dc1 MC |
611 | } |
612 | ||
613 | if (cctx->ctx != NULL) { | |
614 | if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0) | |
615 | dhpkey = NULL; | |
616 | } | |
617 | if (cctx->ssl != NULL) { | |
618 | if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0) | |
619 | dhpkey = NULL; | |
620 | } | |
0f113f3e | 621 | end: |
163f6dc1 | 622 | EVP_PKEY_free(dhpkey); |
ca3a82c3 | 623 | BIO_free(in); |
0f113f3e MC |
624 | return rv > 0; |
625 | } | |
c649d10d TS |
626 | |
627 | static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value) | |
628 | { | |
629 | int rv = 0; | |
630 | int block_size = atoi(value); | |
631 | ||
632 | /* | |
633 | * All we care about is a non-negative value, | |
634 | * the setters check the range | |
635 | */ | |
636 | if (block_size >= 0) { | |
637 | if (cctx->ctx) | |
638 | rv = SSL_CTX_set_block_padding(cctx->ctx, block_size); | |
639 | if (cctx->ssl) | |
640 | rv = SSL_set_block_padding(cctx->ssl, block_size); | |
641 | } | |
642 | return rv; | |
643 | } | |
644 | ||
394159da MC |
645 | |
646 | static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value) | |
647 | { | |
648 | int rv = 0; | |
649 | int num_tickets = atoi(value); | |
650 | ||
651 | if (num_tickets >= 0) { | |
652 | if (cctx->ctx) | |
653 | rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets); | |
654 | if (cctx->ssl) | |
655 | rv = SSL_set_num_tickets(cctx->ssl, num_tickets); | |
656 | } | |
657 | return rv; | |
658 | } | |
659 | ||
0f113f3e MC |
660 | typedef struct { |
661 | int (*cmd) (SSL_CONF_CTX *cctx, const char *value); | |
662 | const char *str_file; | |
663 | const char *str_cmdline; | |
656b2605 DSH |
664 | unsigned short flags; |
665 | unsigned short value_type; | |
0f113f3e | 666 | } ssl_conf_cmd_tbl; |
3db935a9 | 667 | |
ec2f7e56 DSH |
668 | /* Table of supported parameters */ |
669 | ||
656b2605 DSH |
670 | #define SSL_CONF_CMD(name, cmdopt, flags, type) \ |
671 | {cmd_##name, #name, cmdopt, flags, type} | |
672 | ||
673 | #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \ | |
674 | SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING) | |
ec2f7e56 | 675 | |
656b2605 DSH |
676 | #define SSL_CONF_CMD_SWITCH(name, flags) \ |
677 | {0, NULL, name, flags, SSL_CONF_TYPE_NONE} | |
3db935a9 | 678 | |
7e1b7485 | 679 | /* See apps/apps.h if you change this table. */ |
27f3b65f | 680 | static const ssl_conf_cmd_tbl ssl_conf_cmds[] = { |
656b2605 DSH |
681 | SSL_CONF_CMD_SWITCH("no_ssl3", 0), |
682 | SSL_CONF_CMD_SWITCH("no_tls1", 0), | |
683 | SSL_CONF_CMD_SWITCH("no_tls1_1", 0), | |
684 | SSL_CONF_CMD_SWITCH("no_tls1_2", 0), | |
582a17d6 | 685 | SSL_CONF_CMD_SWITCH("no_tls1_3", 0), |
656b2605 | 686 | SSL_CONF_CMD_SWITCH("bugs", 0), |
cc5a9ba4 | 687 | SSL_CONF_CMD_SWITCH("no_comp", 0), |
dc5744cb | 688 | SSL_CONF_CMD_SWITCH("comp", 0), |
656b2605 | 689 | SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER), |
656b2605 | 690 | SSL_CONF_CMD_SWITCH("no_ticket", 0), |
656b2605 DSH |
691 | SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER), |
692 | SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0), | |
55373bfd | 693 | SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER), |
cbbbc8fc | 694 | SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT), |
db0f35dd | 695 | SSL_CONF_CMD_SWITCH("no_renegotiation", 0), |
656b2605 DSH |
696 | SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER), |
697 | SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER), | |
e3c0d76b | 698 | SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0), |
e1c7871d | 699 | SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER), |
656b2605 | 700 | SSL_CONF_CMD_SWITCH("strict", 0), |
db37d32c | 701 | SSL_CONF_CMD_SWITCH("no_middlebox", 0), |
3bb5e5b0 MC |
702 | SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER), |
703 | SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER), | |
656b2605 DSH |
704 | SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0), |
705 | SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0), | |
706 | SSL_CONF_CMD_STRING(Curves, "curves", 0), | |
de4d764e | 707 | SSL_CONF_CMD_STRING(Groups, "groups", 0), |
656b2605 | 708 | SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER), |
656b2605 | 709 | SSL_CONF_CMD_STRING(CipherString, "cipher", 0), |
f865b081 | 710 | SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0), |
656b2605 | 711 | SSL_CONF_CMD_STRING(Protocol, NULL, 0), |
453dfd8d EK |
712 | SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0), |
713 | SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0), | |
656b2605 | 714 | SSL_CONF_CMD_STRING(Options, NULL, 0), |
429261d0 | 715 | SSL_CONF_CMD_STRING(VerifyMode, NULL, 0), |
656b2605 DSH |
716 | SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE, |
717 | SSL_CONF_TYPE_FILE), | |
718 | SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE, | |
719 | SSL_CONF_TYPE_FILE), | |
720 | SSL_CONF_CMD(ServerInfoFile, NULL, | |
721 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
722 | SSL_CONF_TYPE_FILE), | |
429261d0 DSH |
723 | SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE, |
724 | SSL_CONF_TYPE_DIR), | |
725 | SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE, | |
726 | SSL_CONF_TYPE_FILE), | |
6dcb100f RL |
727 | SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE, |
728 | SSL_CONF_TYPE_STORE), | |
429261d0 DSH |
729 | SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE, |
730 | SSL_CONF_TYPE_DIR), | |
731 | SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE, | |
732 | SSL_CONF_TYPE_FILE), | |
6dcb100f RL |
733 | SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE, |
734 | SSL_CONF_TYPE_STORE), | |
be885d50 DSH |
735 | SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE, |
736 | SSL_CONF_TYPE_FILE), | |
429261d0 DSH |
737 | SSL_CONF_CMD(ClientCAFile, NULL, |
738 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
739 | SSL_CONF_TYPE_FILE), | |
be885d50 DSH |
740 | SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE, |
741 | SSL_CONF_TYPE_DIR), | |
429261d0 DSH |
742 | SSL_CONF_CMD(ClientCAPath, NULL, |
743 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
744 | SSL_CONF_TYPE_DIR), | |
6dcb100f RL |
745 | SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE, |
746 | SSL_CONF_TYPE_STORE), | |
747 | SSL_CONF_CMD(ClientCAStore, NULL, | |
748 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
749 | SSL_CONF_TYPE_STORE), | |
656b2605 DSH |
750 | SSL_CONF_CMD(DHParameters, "dhparam", |
751 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
c649d10d | 752 | SSL_CONF_TYPE_FILE), |
394159da | 753 | SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0), |
3bb5e5b0 | 754 | SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER), |
656b2605 DSH |
755 | }; |
756 | ||
757 | /* Supported switches: must match order of switches in ssl_conf_cmds */ | |
758 | static const ssl_switch_tbl ssl_cmd_switches[] = { | |
759 | {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */ | |
760 | {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */ | |
761 | {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */ | |
762 | {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */ | |
582a17d6 | 763 | {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */ |
656b2605 | 764 | {SSL_OP_ALL, 0}, /* bugs */ |
cc5a9ba4 VD |
765 | {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */ |
766 | {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */ | |
656b2605 | 767 | {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */ |
656b2605 | 768 | {SSL_OP_NO_TICKET, 0}, /* no_ticket */ |
656b2605 DSH |
769 | {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */ |
770 | /* legacy_renegotiation */ | |
771 | {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0}, | |
55373bfd RS |
772 | /* Allow client renegotiation */ |
773 | {SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0}, | |
656b2605 DSH |
774 | /* legacy_server_connect */ |
775 | {SSL_OP_LEGACY_SERVER_CONNECT, 0}, | |
db0f35dd TS |
776 | /* no_renegotiation */ |
777 | {SSL_OP_NO_RENEGOTIATION, 0}, | |
656b2605 DSH |
778 | /* no_resumption_on_reneg */ |
779 | {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0}, | |
780 | /* no_legacy_server_connect */ | |
781 | {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV}, | |
e3c0d76b MC |
782 | /* allow_no_dhe_kex */ |
783 | {SSL_OP_ALLOW_NO_DHE_KEX, 0}, | |
e1c7871d TS |
784 | /* chacha reprioritization */ |
785 | {SSL_OP_PRIORITIZE_CHACHA, 0}, | |
656b2605 | 786 | {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */ |
a5816a5a MC |
787 | /* no_middlebox */ |
788 | {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV}, | |
3bb5e5b0 MC |
789 | /* anti_replay */ |
790 | {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV}, | |
791 | /* no_anti_replay */ | |
792 | {SSL_OP_NO_ANTI_REPLAY, 0}, | |
3db935a9 DSH |
793 | }; |
794 | ||
ec2f7e56 | 795 | static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd) |
0f113f3e | 796 | { |
12a765a5 | 797 | if (pcmd == NULL || *pcmd == NULL) |
0f113f3e MC |
798 | return 0; |
799 | /* If a prefix is set, check and skip */ | |
800 | if (cctx->prefix) { | |
801 | if (strlen(*pcmd) <= cctx->prefixlen) | |
802 | return 0; | |
803 | if (cctx->flags & SSL_CONF_FLAG_CMDLINE && | |
804 | strncmp(*pcmd, cctx->prefix, cctx->prefixlen)) | |
805 | return 0; | |
806 | if (cctx->flags & SSL_CONF_FLAG_FILE && | |
807 | strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen)) | |
808 | return 0; | |
809 | *pcmd += cctx->prefixlen; | |
810 | } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { | |
811 | if (**pcmd != '-' || !(*pcmd)[1]) | |
812 | return 0; | |
813 | *pcmd += 1; | |
814 | } | |
815 | return 1; | |
816 | } | |
817 | ||
656b2605 | 818 | /* Determine if a command is allowed according to cctx flags */ |
a230b26e | 819 | static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t) |
656b2605 DSH |
820 | { |
821 | unsigned int tfl = t->flags; | |
822 | unsigned int cfl = cctx->flags; | |
823 | if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER)) | |
824 | return 0; | |
825 | if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT)) | |
826 | return 0; | |
827 | if ((tfl & SSL_CONF_FLAG_CERTIFICATE) | |
828 | && !(cfl & SSL_CONF_FLAG_CERTIFICATE)) | |
829 | return 0; | |
830 | return 1; | |
831 | } | |
832 | ||
0f113f3e MC |
833 | static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx, |
834 | const char *cmd) | |
835 | { | |
836 | const ssl_conf_cmd_tbl *t; | |
837 | size_t i; | |
838 | if (cmd == NULL) | |
839 | return NULL; | |
840 | ||
841 | /* Look for matching parameter name in table */ | |
b6eb9827 | 842 | for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) { |
656b2605 DSH |
843 | if (ssl_conf_cmd_allowed(cctx, t)) { |
844 | if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { | |
86885c28 | 845 | if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0) |
656b2605 DSH |
846 | return t; |
847 | } | |
848 | if (cctx->flags & SSL_CONF_FLAG_FILE) { | |
86885c28 | 849 | if (t->str_file && strcasecmp(t->str_file, cmd) == 0) |
656b2605 DSH |
850 | return t; |
851 | } | |
0f113f3e MC |
852 | } |
853 | } | |
854 | return NULL; | |
855 | } | |
ec2f7e56 | 856 | |
a230b26e | 857 | static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd) |
656b2605 DSH |
858 | { |
859 | /* Find index of command in table */ | |
860 | size_t idx = cmd - ssl_conf_cmds; | |
861 | const ssl_switch_tbl *scmd; | |
862 | /* Sanity check index */ | |
863 | if (idx >= OSSL_NELEM(ssl_cmd_switches)) | |
864 | return 0; | |
865 | /* Obtain switches entry with same index */ | |
866 | scmd = ssl_cmd_switches + idx; | |
867 | ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1); | |
868 | return 1; | |
869 | } | |
870 | ||
ec2f7e56 | 871 | int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value) |
0f113f3e MC |
872 | { |
873 | const ssl_conf_cmd_tbl *runcmd; | |
874 | if (cmd == NULL) { | |
6849b73c | 875 | ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME); |
0f113f3e MC |
876 | return 0; |
877 | } | |
878 | ||
879 | if (!ssl_conf_cmd_skip_prefix(cctx, &cmd)) | |
880 | return -2; | |
881 | ||
882 | runcmd = ssl_conf_cmd_lookup(cctx, cmd); | |
883 | ||
884 | if (runcmd) { | |
885 | int rv; | |
656b2605 DSH |
886 | if (runcmd->value_type == SSL_CONF_TYPE_NONE) { |
887 | return ctrl_switch_option(cctx, runcmd); | |
888 | } | |
0f113f3e MC |
889 | if (value == NULL) |
890 | return -3; | |
891 | rv = runcmd->cmd(cctx, value); | |
892 | if (rv > 0) | |
893 | return 2; | |
894 | if (rv == -2) | |
895 | return -2; | |
c48ffbcc RL |
896 | if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) |
897 | ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE, | |
898 | "cmd=%s, value=%s", cmd, value); | |
0f113f3e MC |
899 | return 0; |
900 | } | |
901 | ||
c48ffbcc RL |
902 | if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) |
903 | ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd); | |
0f113f3e MC |
904 | |
905 | return -2; | |
906 | } | |
3db935a9 DSH |
907 | |
908 | int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv) | |
0f113f3e MC |
909 | { |
910 | int rv; | |
911 | const char *arg = NULL, *argn; | |
12a765a5 RS |
912 | |
913 | if (pargc != NULL && *pargc == 0) | |
0f113f3e | 914 | return 0; |
12a765a5 | 915 | if (pargc == NULL || *pargc > 0) |
0f113f3e MC |
916 | arg = **pargv; |
917 | if (arg == NULL) | |
918 | return 0; | |
12a765a5 | 919 | if (pargc == NULL || *pargc > 1) |
0f113f3e MC |
920 | argn = (*pargv)[1]; |
921 | else | |
922 | argn = NULL; | |
923 | cctx->flags &= ~SSL_CONF_FLAG_FILE; | |
924 | cctx->flags |= SSL_CONF_FLAG_CMDLINE; | |
925 | rv = SSL_CONF_cmd(cctx, arg, argn); | |
926 | if (rv > 0) { | |
927 | /* Success: update pargc, pargv */ | |
928 | (*pargv) += rv; | |
929 | if (pargc) | |
930 | (*pargc) -= rv; | |
931 | return rv; | |
932 | } | |
933 | /* Unknown switch: indicate no arguments processed */ | |
934 | if (rv == -2) | |
935 | return 0; | |
936 | /* Some error occurred processing command, return fatal error */ | |
937 | if (rv == 0) | |
938 | return -1; | |
939 | return rv; | |
940 | } | |
3db935a9 | 941 | |
ec2f7e56 | 942 | int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd) |
0f113f3e MC |
943 | { |
944 | if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) { | |
945 | const ssl_conf_cmd_tbl *runcmd; | |
946 | runcmd = ssl_conf_cmd_lookup(cctx, cmd); | |
947 | if (runcmd) | |
948 | return runcmd->value_type; | |
949 | } | |
950 | return SSL_CONF_TYPE_UNKNOWN; | |
951 | } | |
ec2f7e56 | 952 | |
3db935a9 | 953 | SSL_CONF_CTX *SSL_CONF_CTX_new(void) |
0f113f3e | 954 | { |
64b25758 | 955 | SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret)); |
b4faea50 | 956 | |
0f113f3e MC |
957 | return ret; |
958 | } | |
3db935a9 | 959 | |
ec2f7e56 | 960 | int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx) |
0f113f3e | 961 | { |
2011b169 DSH |
962 | /* See if any certificates are missing private keys */ |
963 | size_t i; | |
964 | CERT *c = NULL; | |
965 | if (cctx->ctx) | |
966 | c = cctx->ctx->cert; | |
967 | else if (cctx->ssl) | |
968 | c = cctx->ssl->cert; | |
969 | if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) { | |
970 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
971 | const char *p = cctx->cert_filename[i]; | |
972 | /* | |
973 | * If missing private key try to load one from certificate file | |
974 | */ | |
975 | if (p && !c->pkeys[i].privatekey) { | |
976 | if (!cmd_PrivateKey(cctx, p)) | |
977 | return 0; | |
978 | } | |
979 | } | |
980 | } | |
429261d0 DSH |
981 | if (cctx->canames) { |
982 | if (cctx->ssl) | |
be885d50 | 983 | SSL_set0_CA_list(cctx->ssl, cctx->canames); |
429261d0 | 984 | else if (cctx->ctx) |
be885d50 | 985 | SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames); |
429261d0 DSH |
986 | else |
987 | sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free); | |
988 | cctx->canames = NULL; | |
989 | } | |
0f113f3e MC |
990 | return 1; |
991 | } | |
ec2f7e56 | 992 | |
3db935a9 | 993 | void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx) |
0f113f3e MC |
994 | { |
995 | if (cctx) { | |
2011b169 | 996 | size_t i; |
656b2605 | 997 | for (i = 0; i < SSL_PKEY_NUM; i++) |
b548a1f1 | 998 | OPENSSL_free(cctx->cert_filename[i]); |
b548a1f1 | 999 | OPENSSL_free(cctx->prefix); |
429261d0 | 1000 | sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free); |
4445704f | 1001 | OPENSSL_free(cctx); |
0f113f3e MC |
1002 | } |
1003 | } | |
3db935a9 DSH |
1004 | |
1005 | unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags) | |
0f113f3e MC |
1006 | { |
1007 | cctx->flags |= flags; | |
1008 | return cctx->flags; | |
1009 | } | |
3db935a9 DSH |
1010 | |
1011 | unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags) | |
0f113f3e MC |
1012 | { |
1013 | cctx->flags &= ~flags; | |
1014 | return cctx->flags; | |
1015 | } | |
3db935a9 DSH |
1016 | |
1017 | int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre) | |
0f113f3e MC |
1018 | { |
1019 | char *tmp = NULL; | |
1020 | if (pre) { | |
7644a9ae | 1021 | tmp = OPENSSL_strdup(pre); |
0f113f3e MC |
1022 | if (tmp == NULL) |
1023 | return 0; | |
1024 | } | |
b548a1f1 | 1025 | OPENSSL_free(cctx->prefix); |
0f113f3e MC |
1026 | cctx->prefix = tmp; |
1027 | if (tmp) | |
1028 | cctx->prefixlen = strlen(tmp); | |
1029 | else | |
1030 | cctx->prefixlen = 0; | |
1031 | return 1; | |
1032 | } | |
3db935a9 DSH |
1033 | |
1034 | void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl) | |
0f113f3e MC |
1035 | { |
1036 | cctx->ssl = ssl; | |
1037 | cctx->ctx = NULL; | |
1038 | if (ssl) { | |
1039 | cctx->poptions = &ssl->options; | |
7946ab33 KR |
1040 | cctx->min_version = &ssl->min_proto_version; |
1041 | cctx->max_version = &ssl->max_proto_version; | |
0f113f3e | 1042 | cctx->pcert_flags = &ssl->cert->cert_flags; |
429261d0 | 1043 | cctx->pvfy_flags = &ssl->verify_mode; |
0f113f3e MC |
1044 | } else { |
1045 | cctx->poptions = NULL; | |
7946ab33 KR |
1046 | cctx->min_version = NULL; |
1047 | cctx->max_version = NULL; | |
0f113f3e | 1048 | cctx->pcert_flags = NULL; |
429261d0 | 1049 | cctx->pvfy_flags = NULL; |
0f113f3e MC |
1050 | } |
1051 | } | |
3db935a9 DSH |
1052 | |
1053 | void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx) | |
0f113f3e MC |
1054 | { |
1055 | cctx->ctx = ctx; | |
1056 | cctx->ssl = NULL; | |
1057 | if (ctx) { | |
1058 | cctx->poptions = &ctx->options; | |
7946ab33 KR |
1059 | cctx->min_version = &ctx->min_proto_version; |
1060 | cctx->max_version = &ctx->max_proto_version; | |
0f113f3e | 1061 | cctx->pcert_flags = &ctx->cert->cert_flags; |
429261d0 | 1062 | cctx->pvfy_flags = &ctx->verify_mode; |
0f113f3e MC |
1063 | } else { |
1064 | cctx->poptions = NULL; | |
7946ab33 KR |
1065 | cctx->min_version = NULL; |
1066 | cctx->max_version = NULL; | |
0f113f3e | 1067 | cctx->pcert_flags = NULL; |
429261d0 | 1068 | cctx->pvfy_flags = NULL; |
0f113f3e MC |
1069 | } |
1070 | } |