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