]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 2012-2018 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 DSH |
10 | #include <stdio.h> |
11 | #include "ssl_locl.h" | |
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 | }; | |
304 | static const struct protocol_versions versions[] = { | |
869e978c | 305 | {"None", 0}, |
7946ab33 KR |
306 | {"SSLv3", SSL3_VERSION}, |
307 | {"TLSv1", TLS1_VERSION}, | |
308 | {"TLSv1.1", TLS1_1_VERSION}, | |
309 | {"TLSv1.2", TLS1_2_VERSION}, | |
582a17d6 | 310 | {"TLSv1.3", TLS1_3_VERSION}, |
7946ab33 | 311 | {"DTLSv1", DTLS1_VERSION}, |
a230b26e EK |
312 | {"DTLSv1.2", DTLS1_2_VERSION} |
313 | }; | |
7946ab33 KR |
314 | size_t i; |
315 | size_t n = OSSL_NELEM(versions); | |
316 | ||
317 | for (i = 0; i < n; i++) | |
318 | if (strcmp(versions[i].name, value) == 0) | |
319 | return versions[i].version; | |
320 | return -1; | |
321 | } | |
322 | ||
4fa52141 VD |
323 | static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound) |
324 | { | |
325 | int method_version; | |
326 | int new_version; | |
327 | ||
328 | if (cctx->ctx != NULL) | |
329 | method_version = cctx->ctx->method->version; | |
330 | else if (cctx->ssl != NULL) | |
331 | method_version = cctx->ssl->ctx->method->version; | |
332 | else | |
333 | return 0; | |
334 | if ((new_version = protocol_from_string(value)) < 0) | |
335 | return 0; | |
336 | return ssl_set_version_bound(method_version, new_version, bound); | |
337 | } | |
338 | ||
7946ab33 KR |
339 | /* |
340 | * cmd_MinProtocol - Set min protocol version | |
341 | * @cctx: config structure to save settings in | |
342 | * @value: The min protocol version in string form | |
343 | * | |
344 | * Returns 1 on success and 0 on failure. | |
345 | */ | |
346 | static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value) | |
347 | { | |
4fa52141 | 348 | return min_max_proto(cctx, value, cctx->min_version); |
7946ab33 KR |
349 | } |
350 | ||
351 | /* | |
352 | * cmd_MaxProtocol - Set max protocol version | |
353 | * @cctx: config structure to save settings in | |
354 | * @value: The max protocol version in string form | |
355 | * | |
356 | * Returns 1 on success and 0 on failure. | |
357 | */ | |
358 | static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value) | |
359 | { | |
4fa52141 | 360 | return min_max_proto(cctx, value, cctx->max_version); |
7946ab33 KR |
361 | } |
362 | ||
ec2f7e56 | 363 | static int cmd_Options(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
364 | { |
365 | static const ssl_flag_tbl ssl_option_list[] = { | |
366 | SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET), | |
367 | SSL_FLAG_TBL_INV("EmptyFragments", | |
368 | SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS), | |
369 | SSL_FLAG_TBL("Bugs", SSL_OP_ALL), | |
370 | SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION), | |
371 | SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE), | |
372 | SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation", | |
373 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION), | |
374 | SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE), | |
375 | SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE), | |
376 | SSL_FLAG_TBL("UnsafeLegacyRenegotiation", | |
377 | SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION), | |
b3618f44 | 378 | SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC), |
db0f35dd | 379 | SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION), |
e1c7871d | 380 | SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX), |
a5816a5a | 381 | SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA), |
3bb5e5b0 MC |
382 | SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT), |
383 | SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY) | |
0f113f3e | 384 | }; |
0f113f3e MC |
385 | if (value == NULL) |
386 | return -3; | |
387 | cctx->tbl = ssl_option_list; | |
b6eb9827 | 388 | cctx->ntbl = OSSL_NELEM(ssl_option_list); |
0f113f3e MC |
389 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); |
390 | } | |
3db935a9 | 391 | |
429261d0 DSH |
392 | static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value) |
393 | { | |
394 | static const ssl_flag_tbl ssl_vfy_list[] = { | |
395 | SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER), | |
396 | SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER), | |
397 | SSL_FLAG_VFY_SRV("Require", | |
398 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), | |
9d75dce3 TS |
399 | SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE), |
400 | SSL_FLAG_VFY_SRV("RequestPostHandshake", | |
401 | SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE), | |
402 | SSL_FLAG_VFY_SRV("RequirePostHandshake", | |
403 | SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE | | |
404 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), | |
429261d0 DSH |
405 | }; |
406 | if (value == NULL) | |
407 | return -3; | |
408 | cctx->tbl = ssl_vfy_list; | |
409 | cctx->ntbl = OSSL_NELEM(ssl_vfy_list); | |
410 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); | |
411 | } | |
412 | ||
ec2f7e56 | 413 | static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
414 | { |
415 | int rv = 1; | |
2011b169 | 416 | CERT *c = NULL; |
2011b169 | 417 | if (cctx->ctx) { |
0f113f3e | 418 | rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value); |
2011b169 DSH |
419 | c = cctx->ctx->cert; |
420 | } | |
421 | if (cctx->ssl) { | |
fae4772c | 422 | rv = SSL_use_certificate_chain_file(cctx->ssl, value); |
2011b169 DSH |
423 | c = cctx->ssl->cert; |
424 | } | |
425 | if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) { | |
426 | char **pfilename = &cctx->cert_filename[c->key - c->pkeys]; | |
b548a1f1 | 427 | OPENSSL_free(*pfilename); |
7644a9ae | 428 | *pfilename = OPENSSL_strdup(value); |
2011b169 DSH |
429 | if (!*pfilename) |
430 | rv = 0; | |
431 | } | |
432 | ||
0f113f3e MC |
433 | return rv > 0; |
434 | } | |
ec2f7e56 DSH |
435 | |
436 | static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
437 | { |
438 | int rv = 1; | |
439 | if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE)) | |
440 | return -2; | |
441 | if (cctx->ctx) | |
442 | rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM); | |
443 | if (cctx->ssl) | |
444 | rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM); | |
445 | return rv > 0; | |
446 | } | |
5b7f36e8 DSH |
447 | |
448 | static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
449 | { |
450 | int rv = 1; | |
0f113f3e MC |
451 | if (cctx->ctx) |
452 | rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value); | |
453 | return rv > 0; | |
454 | } | |
5b7f36e8 | 455 | |
429261d0 DSH |
456 | static int do_store(SSL_CONF_CTX *cctx, |
457 | const char *CAfile, const char *CApath, int verify_store) | |
458 | { | |
459 | CERT *cert; | |
460 | X509_STORE **st; | |
461 | if (cctx->ctx) | |
462 | cert = cctx->ctx->cert; | |
463 | else if (cctx->ssl) | |
464 | cert = cctx->ssl->cert; | |
465 | else | |
466 | return 1; | |
467 | st = verify_store ? &cert->verify_store : &cert->chain_store; | |
468 | if (*st == NULL) { | |
469 | *st = X509_STORE_new(); | |
470 | if (*st == NULL) | |
471 | return 0; | |
472 | } | |
473 | return X509_STORE_load_locations(*st, CAfile, CApath) > 0; | |
474 | } | |
475 | ||
476 | static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value) | |
477 | { | |
478 | return do_store(cctx, NULL, value, 0); | |
479 | } | |
480 | ||
481 | static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value) | |
482 | { | |
483 | return do_store(cctx, value, NULL, 0); | |
484 | } | |
485 | ||
486 | static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value) | |
487 | { | |
488 | return do_store(cctx, NULL, value, 1); | |
489 | } | |
490 | ||
491 | static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value) | |
492 | { | |
493 | return do_store(cctx, value, NULL, 1); | |
494 | } | |
495 | ||
be885d50 | 496 | static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value) |
429261d0 DSH |
497 | { |
498 | if (cctx->canames == NULL) | |
499 | cctx->canames = sk_X509_NAME_new_null(); | |
500 | if (cctx->canames == NULL) | |
501 | return 0; | |
502 | return SSL_add_file_cert_subjects_to_stack(cctx->canames, value); | |
503 | } | |
504 | ||
be885d50 DSH |
505 | static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value) |
506 | { | |
507 | return cmd_RequestCAFile(cctx, value); | |
508 | } | |
509 | ||
510 | static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value) | |
429261d0 DSH |
511 | { |
512 | if (cctx->canames == NULL) | |
513 | cctx->canames = sk_X509_NAME_new_null(); | |
514 | if (cctx->canames == NULL) | |
515 | return 0; | |
516 | return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value); | |
517 | } | |
518 | ||
be885d50 DSH |
519 | static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value) |
520 | { | |
521 | return cmd_RequestCAPath(cctx, value); | |
522 | } | |
523 | ||
c557f921 DSH |
524 | #ifndef OPENSSL_NO_DH |
525 | static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
526 | { |
527 | int rv = 0; | |
528 | DH *dh = NULL; | |
529 | BIO *in = NULL; | |
0f113f3e | 530 | if (cctx->ctx || cctx->ssl) { |
9982cbbb | 531 | in = BIO_new(BIO_s_file()); |
a71edf3b | 532 | if (in == NULL) |
0f113f3e MC |
533 | goto end; |
534 | if (BIO_read_filename(in, value) <= 0) | |
535 | goto end; | |
536 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); | |
a71edf3b | 537 | if (dh == NULL) |
0f113f3e MC |
538 | goto end; |
539 | } else | |
540 | return 1; | |
541 | if (cctx->ctx) | |
542 | rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh); | |
543 | if (cctx->ssl) | |
544 | rv = SSL_set_tmp_dh(cctx->ssl, dh); | |
545 | end: | |
d6407083 | 546 | DH_free(dh); |
ca3a82c3 | 547 | BIO_free(in); |
0f113f3e MC |
548 | return rv > 0; |
549 | } | |
c557f921 | 550 | #endif |
c649d10d TS |
551 | |
552 | static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value) | |
553 | { | |
554 | int rv = 0; | |
555 | int block_size = atoi(value); | |
556 | ||
557 | /* | |
558 | * All we care about is a non-negative value, | |
559 | * the setters check the range | |
560 | */ | |
561 | if (block_size >= 0) { | |
562 | if (cctx->ctx) | |
563 | rv = SSL_CTX_set_block_padding(cctx->ctx, block_size); | |
564 | if (cctx->ssl) | |
565 | rv = SSL_set_block_padding(cctx->ssl, block_size); | |
566 | } | |
567 | return rv; | |
568 | } | |
569 | ||
394159da MC |
570 | |
571 | static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value) | |
572 | { | |
573 | int rv = 0; | |
574 | int num_tickets = atoi(value); | |
575 | ||
576 | if (num_tickets >= 0) { | |
577 | if (cctx->ctx) | |
578 | rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets); | |
579 | if (cctx->ssl) | |
580 | rv = SSL_set_num_tickets(cctx->ssl, num_tickets); | |
581 | } | |
582 | return rv; | |
583 | } | |
584 | ||
0f113f3e MC |
585 | typedef struct { |
586 | int (*cmd) (SSL_CONF_CTX *cctx, const char *value); | |
587 | const char *str_file; | |
588 | const char *str_cmdline; | |
656b2605 DSH |
589 | unsigned short flags; |
590 | unsigned short value_type; | |
0f113f3e | 591 | } ssl_conf_cmd_tbl; |
3db935a9 | 592 | |
ec2f7e56 DSH |
593 | /* Table of supported parameters */ |
594 | ||
656b2605 DSH |
595 | #define SSL_CONF_CMD(name, cmdopt, flags, type) \ |
596 | {cmd_##name, #name, cmdopt, flags, type} | |
597 | ||
598 | #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \ | |
599 | SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING) | |
ec2f7e56 | 600 | |
656b2605 DSH |
601 | #define SSL_CONF_CMD_SWITCH(name, flags) \ |
602 | {0, NULL, name, flags, SSL_CONF_TYPE_NONE} | |
3db935a9 | 603 | |
7e1b7485 | 604 | /* See apps/apps.h if you change this table. */ |
27f3b65f | 605 | static const ssl_conf_cmd_tbl ssl_conf_cmds[] = { |
656b2605 DSH |
606 | SSL_CONF_CMD_SWITCH("no_ssl3", 0), |
607 | SSL_CONF_CMD_SWITCH("no_tls1", 0), | |
608 | SSL_CONF_CMD_SWITCH("no_tls1_1", 0), | |
609 | SSL_CONF_CMD_SWITCH("no_tls1_2", 0), | |
582a17d6 | 610 | SSL_CONF_CMD_SWITCH("no_tls1_3", 0), |
656b2605 | 611 | SSL_CONF_CMD_SWITCH("bugs", 0), |
cc5a9ba4 | 612 | SSL_CONF_CMD_SWITCH("no_comp", 0), |
dc5744cb | 613 | SSL_CONF_CMD_SWITCH("comp", 0), |
656b2605 | 614 | SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER), |
656b2605 | 615 | SSL_CONF_CMD_SWITCH("no_ticket", 0), |
656b2605 DSH |
616 | SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER), |
617 | SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0), | |
618 | SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER), | |
db0f35dd | 619 | SSL_CONF_CMD_SWITCH("no_renegotiation", 0), |
656b2605 DSH |
620 | SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER), |
621 | SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER), | |
e3c0d76b | 622 | SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0), |
e1c7871d | 623 | SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER), |
656b2605 | 624 | SSL_CONF_CMD_SWITCH("strict", 0), |
db37d32c | 625 | SSL_CONF_CMD_SWITCH("no_middlebox", 0), |
3bb5e5b0 MC |
626 | SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER), |
627 | SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER), | |
656b2605 DSH |
628 | SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0), |
629 | SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0), | |
630 | SSL_CONF_CMD_STRING(Curves, "curves", 0), | |
de4d764e | 631 | SSL_CONF_CMD_STRING(Groups, "groups", 0), |
10bf4fc2 | 632 | #ifndef OPENSSL_NO_EC |
656b2605 | 633 | SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER), |
14536c8c | 634 | #endif |
656b2605 | 635 | SSL_CONF_CMD_STRING(CipherString, "cipher", 0), |
f865b081 | 636 | SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0), |
656b2605 | 637 | SSL_CONF_CMD_STRING(Protocol, NULL, 0), |
453dfd8d EK |
638 | SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0), |
639 | SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0), | |
656b2605 | 640 | SSL_CONF_CMD_STRING(Options, NULL, 0), |
429261d0 | 641 | SSL_CONF_CMD_STRING(VerifyMode, NULL, 0), |
656b2605 DSH |
642 | SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE, |
643 | SSL_CONF_TYPE_FILE), | |
644 | SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE, | |
645 | SSL_CONF_TYPE_FILE), | |
646 | SSL_CONF_CMD(ServerInfoFile, NULL, | |
647 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
648 | SSL_CONF_TYPE_FILE), | |
429261d0 DSH |
649 | SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE, |
650 | SSL_CONF_TYPE_DIR), | |
651 | SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE, | |
652 | SSL_CONF_TYPE_FILE), | |
653 | SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE, | |
654 | SSL_CONF_TYPE_DIR), | |
655 | SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE, | |
656 | SSL_CONF_TYPE_FILE), | |
be885d50 DSH |
657 | SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE, |
658 | SSL_CONF_TYPE_FILE), | |
429261d0 DSH |
659 | SSL_CONF_CMD(ClientCAFile, NULL, |
660 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
661 | SSL_CONF_TYPE_FILE), | |
be885d50 DSH |
662 | SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE, |
663 | SSL_CONF_TYPE_DIR), | |
429261d0 DSH |
664 | SSL_CONF_CMD(ClientCAPath, NULL, |
665 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
666 | SSL_CONF_TYPE_DIR), | |
c557f921 | 667 | #ifndef OPENSSL_NO_DH |
656b2605 DSH |
668 | SSL_CONF_CMD(DHParameters, "dhparam", |
669 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
c649d10d | 670 | SSL_CONF_TYPE_FILE), |
656b2605 | 671 | #endif |
394159da | 672 | SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0), |
3bb5e5b0 | 673 | SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER), |
656b2605 DSH |
674 | }; |
675 | ||
676 | /* Supported switches: must match order of switches in ssl_conf_cmds */ | |
677 | static const ssl_switch_tbl ssl_cmd_switches[] = { | |
678 | {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */ | |
679 | {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */ | |
680 | {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */ | |
681 | {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */ | |
582a17d6 | 682 | {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */ |
656b2605 | 683 | {SSL_OP_ALL, 0}, /* bugs */ |
cc5a9ba4 VD |
684 | {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */ |
685 | {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */ | |
656b2605 | 686 | {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */ |
656b2605 | 687 | {SSL_OP_NO_TICKET, 0}, /* no_ticket */ |
656b2605 DSH |
688 | {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */ |
689 | /* legacy_renegotiation */ | |
690 | {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0}, | |
691 | /* legacy_server_connect */ | |
692 | {SSL_OP_LEGACY_SERVER_CONNECT, 0}, | |
db0f35dd TS |
693 | /* no_renegotiation */ |
694 | {SSL_OP_NO_RENEGOTIATION, 0}, | |
656b2605 DSH |
695 | /* no_resumption_on_reneg */ |
696 | {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0}, | |
697 | /* no_legacy_server_connect */ | |
698 | {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV}, | |
e3c0d76b MC |
699 | /* allow_no_dhe_kex */ |
700 | {SSL_OP_ALLOW_NO_DHE_KEX, 0}, | |
e1c7871d TS |
701 | /* chacha reprioritization */ |
702 | {SSL_OP_PRIORITIZE_CHACHA, 0}, | |
656b2605 | 703 | {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */ |
a5816a5a MC |
704 | /* no_middlebox */ |
705 | {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV}, | |
3bb5e5b0 MC |
706 | /* anti_replay */ |
707 | {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV}, | |
708 | /* no_anti_replay */ | |
709 | {SSL_OP_NO_ANTI_REPLAY, 0}, | |
3db935a9 DSH |
710 | }; |
711 | ||
ec2f7e56 | 712 | static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd) |
0f113f3e MC |
713 | { |
714 | if (!pcmd || !*pcmd) | |
715 | return 0; | |
716 | /* If a prefix is set, check and skip */ | |
717 | if (cctx->prefix) { | |
718 | if (strlen(*pcmd) <= cctx->prefixlen) | |
719 | return 0; | |
720 | if (cctx->flags & SSL_CONF_FLAG_CMDLINE && | |
721 | strncmp(*pcmd, cctx->prefix, cctx->prefixlen)) | |
722 | return 0; | |
723 | if (cctx->flags & SSL_CONF_FLAG_FILE && | |
724 | strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen)) | |
725 | return 0; | |
726 | *pcmd += cctx->prefixlen; | |
727 | } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { | |
728 | if (**pcmd != '-' || !(*pcmd)[1]) | |
729 | return 0; | |
730 | *pcmd += 1; | |
731 | } | |
732 | return 1; | |
733 | } | |
734 | ||
656b2605 | 735 | /* Determine if a command is allowed according to cctx flags */ |
a230b26e | 736 | static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t) |
656b2605 DSH |
737 | { |
738 | unsigned int tfl = t->flags; | |
739 | unsigned int cfl = cctx->flags; | |
740 | if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER)) | |
741 | return 0; | |
742 | if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT)) | |
743 | return 0; | |
744 | if ((tfl & SSL_CONF_FLAG_CERTIFICATE) | |
745 | && !(cfl & SSL_CONF_FLAG_CERTIFICATE)) | |
746 | return 0; | |
747 | return 1; | |
748 | } | |
749 | ||
0f113f3e MC |
750 | static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx, |
751 | const char *cmd) | |
752 | { | |
753 | const ssl_conf_cmd_tbl *t; | |
754 | size_t i; | |
755 | if (cmd == NULL) | |
756 | return NULL; | |
757 | ||
758 | /* Look for matching parameter name in table */ | |
b6eb9827 | 759 | for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) { |
656b2605 DSH |
760 | if (ssl_conf_cmd_allowed(cctx, t)) { |
761 | if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { | |
86885c28 | 762 | if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0) |
656b2605 DSH |
763 | return t; |
764 | } | |
765 | if (cctx->flags & SSL_CONF_FLAG_FILE) { | |
86885c28 | 766 | if (t->str_file && strcasecmp(t->str_file, cmd) == 0) |
656b2605 DSH |
767 | return t; |
768 | } | |
0f113f3e MC |
769 | } |
770 | } | |
771 | return NULL; | |
772 | } | |
ec2f7e56 | 773 | |
a230b26e | 774 | static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd) |
656b2605 DSH |
775 | { |
776 | /* Find index of command in table */ | |
777 | size_t idx = cmd - ssl_conf_cmds; | |
778 | const ssl_switch_tbl *scmd; | |
779 | /* Sanity check index */ | |
780 | if (idx >= OSSL_NELEM(ssl_cmd_switches)) | |
781 | return 0; | |
782 | /* Obtain switches entry with same index */ | |
783 | scmd = ssl_cmd_switches + idx; | |
784 | ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1); | |
785 | return 1; | |
786 | } | |
787 | ||
ec2f7e56 | 788 | int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value) |
0f113f3e MC |
789 | { |
790 | const ssl_conf_cmd_tbl *runcmd; | |
791 | if (cmd == NULL) { | |
792 | SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME); | |
793 | return 0; | |
794 | } | |
795 | ||
796 | if (!ssl_conf_cmd_skip_prefix(cctx, &cmd)) | |
797 | return -2; | |
798 | ||
799 | runcmd = ssl_conf_cmd_lookup(cctx, cmd); | |
800 | ||
801 | if (runcmd) { | |
802 | int rv; | |
656b2605 DSH |
803 | if (runcmd->value_type == SSL_CONF_TYPE_NONE) { |
804 | return ctrl_switch_option(cctx, runcmd); | |
805 | } | |
0f113f3e MC |
806 | if (value == NULL) |
807 | return -3; | |
808 | rv = runcmd->cmd(cctx, value); | |
809 | if (rv > 0) | |
810 | return 2; | |
811 | if (rv == -2) | |
812 | return -2; | |
813 | if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) { | |
814 | SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE); | |
815 | ERR_add_error_data(4, "cmd=", cmd, ", value=", value); | |
816 | } | |
817 | return 0; | |
818 | } | |
819 | ||
0f113f3e MC |
820 | if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) { |
821 | SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME); | |
822 | ERR_add_error_data(2, "cmd=", cmd); | |
823 | } | |
824 | ||
825 | return -2; | |
826 | } | |
3db935a9 DSH |
827 | |
828 | int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv) | |
0f113f3e MC |
829 | { |
830 | int rv; | |
831 | const char *arg = NULL, *argn; | |
832 | if (pargc && *pargc == 0) | |
833 | return 0; | |
834 | if (!pargc || *pargc > 0) | |
835 | arg = **pargv; | |
836 | if (arg == NULL) | |
837 | return 0; | |
838 | if (!pargc || *pargc > 1) | |
839 | argn = (*pargv)[1]; | |
840 | else | |
841 | argn = NULL; | |
842 | cctx->flags &= ~SSL_CONF_FLAG_FILE; | |
843 | cctx->flags |= SSL_CONF_FLAG_CMDLINE; | |
844 | rv = SSL_CONF_cmd(cctx, arg, argn); | |
845 | if (rv > 0) { | |
846 | /* Success: update pargc, pargv */ | |
847 | (*pargv) += rv; | |
848 | if (pargc) | |
849 | (*pargc) -= rv; | |
850 | return rv; | |
851 | } | |
852 | /* Unknown switch: indicate no arguments processed */ | |
853 | if (rv == -2) | |
854 | return 0; | |
855 | /* Some error occurred processing command, return fatal error */ | |
856 | if (rv == 0) | |
857 | return -1; | |
858 | return rv; | |
859 | } | |
3db935a9 | 860 | |
ec2f7e56 | 861 | int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd) |
0f113f3e MC |
862 | { |
863 | if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) { | |
864 | const ssl_conf_cmd_tbl *runcmd; | |
865 | runcmd = ssl_conf_cmd_lookup(cctx, cmd); | |
866 | if (runcmd) | |
867 | return runcmd->value_type; | |
868 | } | |
869 | return SSL_CONF_TYPE_UNKNOWN; | |
870 | } | |
ec2f7e56 | 871 | |
3db935a9 | 872 | SSL_CONF_CTX *SSL_CONF_CTX_new(void) |
0f113f3e | 873 | { |
64b25758 | 874 | SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret)); |
b4faea50 | 875 | |
0f113f3e MC |
876 | return ret; |
877 | } | |
3db935a9 | 878 | |
ec2f7e56 | 879 | int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx) |
0f113f3e | 880 | { |
2011b169 DSH |
881 | /* See if any certificates are missing private keys */ |
882 | size_t i; | |
883 | CERT *c = NULL; | |
884 | if (cctx->ctx) | |
885 | c = cctx->ctx->cert; | |
886 | else if (cctx->ssl) | |
887 | c = cctx->ssl->cert; | |
888 | if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) { | |
889 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
890 | const char *p = cctx->cert_filename[i]; | |
891 | /* | |
892 | * If missing private key try to load one from certificate file | |
893 | */ | |
894 | if (p && !c->pkeys[i].privatekey) { | |
895 | if (!cmd_PrivateKey(cctx, p)) | |
896 | return 0; | |
897 | } | |
898 | } | |
899 | } | |
429261d0 DSH |
900 | if (cctx->canames) { |
901 | if (cctx->ssl) | |
be885d50 | 902 | SSL_set0_CA_list(cctx->ssl, cctx->canames); |
429261d0 | 903 | else if (cctx->ctx) |
be885d50 | 904 | SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames); |
429261d0 DSH |
905 | else |
906 | sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free); | |
907 | cctx->canames = NULL; | |
908 | } | |
0f113f3e MC |
909 | return 1; |
910 | } | |
ec2f7e56 | 911 | |
3db935a9 | 912 | void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx) |
0f113f3e MC |
913 | { |
914 | if (cctx) { | |
2011b169 | 915 | size_t i; |
656b2605 | 916 | for (i = 0; i < SSL_PKEY_NUM; i++) |
b548a1f1 | 917 | OPENSSL_free(cctx->cert_filename[i]); |
b548a1f1 | 918 | OPENSSL_free(cctx->prefix); |
429261d0 | 919 | sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free); |
4445704f | 920 | OPENSSL_free(cctx); |
0f113f3e MC |
921 | } |
922 | } | |
3db935a9 DSH |
923 | |
924 | unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags) | |
0f113f3e MC |
925 | { |
926 | cctx->flags |= flags; | |
927 | return cctx->flags; | |
928 | } | |
3db935a9 DSH |
929 | |
930 | unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags) | |
0f113f3e MC |
931 | { |
932 | cctx->flags &= ~flags; | |
933 | return cctx->flags; | |
934 | } | |
3db935a9 DSH |
935 | |
936 | int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre) | |
0f113f3e MC |
937 | { |
938 | char *tmp = NULL; | |
939 | if (pre) { | |
7644a9ae | 940 | tmp = OPENSSL_strdup(pre); |
0f113f3e MC |
941 | if (tmp == NULL) |
942 | return 0; | |
943 | } | |
b548a1f1 | 944 | OPENSSL_free(cctx->prefix); |
0f113f3e MC |
945 | cctx->prefix = tmp; |
946 | if (tmp) | |
947 | cctx->prefixlen = strlen(tmp); | |
948 | else | |
949 | cctx->prefixlen = 0; | |
950 | return 1; | |
951 | } | |
3db935a9 DSH |
952 | |
953 | void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl) | |
0f113f3e MC |
954 | { |
955 | cctx->ssl = ssl; | |
956 | cctx->ctx = NULL; | |
957 | if (ssl) { | |
958 | cctx->poptions = &ssl->options; | |
7946ab33 KR |
959 | cctx->min_version = &ssl->min_proto_version; |
960 | cctx->max_version = &ssl->max_proto_version; | |
0f113f3e | 961 | cctx->pcert_flags = &ssl->cert->cert_flags; |
429261d0 | 962 | cctx->pvfy_flags = &ssl->verify_mode; |
0f113f3e MC |
963 | } else { |
964 | cctx->poptions = NULL; | |
7946ab33 KR |
965 | cctx->min_version = NULL; |
966 | cctx->max_version = NULL; | |
0f113f3e | 967 | cctx->pcert_flags = NULL; |
429261d0 | 968 | cctx->pvfy_flags = NULL; |
0f113f3e MC |
969 | } |
970 | } | |
3db935a9 DSH |
971 | |
972 | void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx) | |
0f113f3e MC |
973 | { |
974 | cctx->ctx = ctx; | |
975 | cctx->ssl = NULL; | |
976 | if (ctx) { | |
977 | cctx->poptions = &ctx->options; | |
7946ab33 KR |
978 | cctx->min_version = &ctx->min_proto_version; |
979 | cctx->max_version = &ctx->max_proto_version; | |
0f113f3e | 980 | cctx->pcert_flags = &ctx->cert->cert_flags; |
429261d0 | 981 | cctx->pvfy_flags = &ctx->verify_mode; |
0f113f3e MC |
982 | } else { |
983 | cctx->poptions = NULL; | |
7946ab33 KR |
984 | cctx->min_version = NULL; |
985 | cctx->max_version = NULL; | |
0f113f3e | 986 | cctx->pcert_flags = NULL; |
429261d0 | 987 | cctx->pvfy_flags = NULL; |
0f113f3e MC |
988 | } |
989 | } |