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