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