]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
846e33c7 | 2 | * Copyright 2012-2016 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> |
3db935a9 | 15 | |
0f113f3e | 16 | /* |
f430ba31 | 17 | * structure holding name tables. This is used for permitted elements in lists |
656b2605 | 18 | * such as TLSv1. |
3db935a9 DSH |
19 | */ |
20 | ||
0f113f3e MC |
21 | typedef struct { |
22 | const char *name; | |
23 | int namelen; | |
24 | unsigned int name_flags; | |
25 | unsigned long option_value; | |
26 | } ssl_flag_tbl; | |
3db935a9 | 27 | |
656b2605 DSH |
28 | /* Switch table: use for single command line switches like no_tls2 */ |
29 | typedef struct { | |
30 | unsigned long option_value; | |
31 | unsigned int name_flags; | |
32 | } ssl_switch_tbl; | |
33 | ||
3db935a9 | 34 | /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */ |
0f113f3e | 35 | #define SSL_TFLAG_INV 0x1 |
429261d0 DSH |
36 | /* Mask for type of flag referred to */ |
37 | #define SSL_TFLAG_TYPE_MASK 0xf00 | |
38 | /* Flag is for options */ | |
39 | #define SSL_TFLAG_OPTION 0x000 | |
40 | /* Flag is for cert_flags */ | |
41 | #define SSL_TFLAG_CERT 0x100 | |
42 | /* Flag is for verify mode */ | |
43 | #define SSL_TFLAG_VFY 0x200 | |
3db935a9 DSH |
44 | /* Option can only be used for clients */ |
45 | #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT | |
46 | /* Option can only be used for servers */ | |
47 | #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER | |
48 | #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER) | |
49 | ||
50 | #define SSL_FLAG_TBL(str, flag) \ | |
0f113f3e | 51 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag} |
3db935a9 | 52 | #define SSL_FLAG_TBL_SRV(str, flag) \ |
0f113f3e | 53 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag} |
3db935a9 | 54 | #define SSL_FLAG_TBL_CLI(str, flag) \ |
0f113f3e | 55 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag} |
3db935a9 | 56 | #define SSL_FLAG_TBL_INV(str, flag) \ |
0f113f3e | 57 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag} |
3db935a9 | 58 | #define SSL_FLAG_TBL_SRV_INV(str, flag) \ |
0f113f3e | 59 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag} |
3db935a9 | 60 | #define SSL_FLAG_TBL_CERT(str, flag) \ |
0f113f3e | 61 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag} |
3db935a9 | 62 | |
429261d0 DSH |
63 | #define SSL_FLAG_VFY_CLI(str, flag) \ |
64 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag} | |
65 | #define SSL_FLAG_VFY_SRV(str, flag) \ | |
66 | {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag} | |
67 | ||
0f113f3e MC |
68 | /* |
69 | * Opaque structure containing SSL configuration context. | |
3db935a9 DSH |
70 | */ |
71 | ||
0f113f3e MC |
72 | struct ssl_conf_ctx_st { |
73 | /* | |
74 | * Various flags indicating (among other things) which options we will | |
75 | * recognise. | |
76 | */ | |
77 | unsigned int flags; | |
78 | /* Prefix and length of commands */ | |
79 | char *prefix; | |
80 | size_t prefixlen; | |
81 | /* SSL_CTX or SSL structure to perform operations on */ | |
82 | SSL_CTX *ctx; | |
83 | SSL *ssl; | |
84 | /* Pointer to SSL or SSL_CTX options field or NULL if none */ | |
f7d53487 | 85 | uint32_t *poptions; |
2011b169 DSH |
86 | /* Certificate filenames for each type */ |
87 | char *cert_filename[SSL_PKEY_NUM]; | |
0f113f3e | 88 | /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */ |
f7d53487 | 89 | uint32_t *pcert_flags; |
429261d0 DSH |
90 | /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */ |
91 | uint32_t *pvfy_flags; | |
7946ab33 KR |
92 | /* Pointer to SSL or SSL_CTX min_version field or NULL if none */ |
93 | int *min_version; | |
94 | /* Pointer to SSL or SSL_CTX max_version field or NULL if none */ | |
95 | int *max_version; | |
0f113f3e MC |
96 | /* Current flag table being worked on */ |
97 | const ssl_flag_tbl *tbl; | |
98 | /* Size of table */ | |
99 | size_t ntbl; | |
429261d0 DSH |
100 | /* Client CA names */ |
101 | STACK_OF(X509_NAME) *canames; | |
0f113f3e | 102 | }; |
3db935a9 | 103 | |
656b2605 DSH |
104 | static void ssl_set_option(SSL_CONF_CTX *cctx, unsigned int name_flags, |
105 | unsigned long option_value, int onoff) | |
106 | { | |
4fdf17a0 | 107 | uint32_t *pflags; |
656b2605 DSH |
108 | if (cctx->poptions == NULL) |
109 | return; | |
110 | if (name_flags & SSL_TFLAG_INV) | |
111 | onoff ^= 1; | |
429261d0 DSH |
112 | switch (name_flags & SSL_TFLAG_TYPE_MASK) { |
113 | ||
114 | case SSL_TFLAG_CERT: | |
115 | pflags = cctx->pcert_flags; | |
116 | break; | |
117 | ||
118 | case SSL_TFLAG_VFY: | |
119 | pflags = cctx->pvfy_flags; | |
120 | break; | |
121 | ||
122 | case SSL_TFLAG_OPTION: | |
123 | pflags = cctx->poptions; | |
124 | break; | |
125 | ||
126 | default: | |
127 | return; | |
128 | ||
656b2605 | 129 | } |
429261d0 DSH |
130 | if (onoff) |
131 | *pflags |= option_value; | |
132 | else | |
133 | *pflags &= ~option_value; | |
656b2605 DSH |
134 | } |
135 | ||
3db935a9 | 136 | static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl, |
0f113f3e MC |
137 | const char *name, int namelen, int onoff) |
138 | { | |
139 | /* If name not relevant for context skip */ | |
140 | if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH)) | |
141 | return 0; | |
142 | if (namelen == -1) { | |
143 | if (strcmp(tbl->name, name)) | |
144 | return 0; | |
145 | } else if (tbl->namelen != namelen | |
146 | || strncasecmp(tbl->name, name, namelen)) | |
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 */ |
0f113f3e MC |
195 | static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx, |
196 | const char *value) | |
197 | { | |
198 | int rv; | |
199 | if (cctx->ssl) | |
200 | rv = SSL_set1_client_sigalgs_list(cctx->ssl, value); | |
201 | /* NB: ctx == NULL performs syntax checking only */ | |
202 | else | |
203 | rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value); | |
204 | return rv > 0; | |
205 | } | |
3db935a9 | 206 | |
ec2f7e56 | 207 | static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
208 | { |
209 | int rv; | |
210 | if (cctx->ssl) | |
211 | rv = SSL_set1_curves_list(cctx->ssl, value); | |
212 | /* NB: ctx == NULL performs syntax checking only */ | |
213 | else | |
214 | rv = SSL_CTX_set1_curves_list(cctx->ctx, value); | |
215 | return rv > 0; | |
216 | } | |
217 | ||
10bf4fc2 | 218 | #ifndef OPENSSL_NO_EC |
3db935a9 | 219 | /* ECDH temporary parameters */ |
ec2f7e56 | 220 | static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e | 221 | { |
fe6ef247 KR |
222 | int rv = 1; |
223 | EC_KEY *ecdh; | |
224 | int nid; | |
0f113f3e | 225 | |
fe6ef247 KR |
226 | nid = EC_curve_nist2nid(value); |
227 | if (nid == NID_undef) | |
228 | nid = OBJ_sn2nid(value); | |
229 | if (nid == 0) | |
230 | return 0; | |
231 | ecdh = EC_KEY_new_by_curve_name(nid); | |
232 | if (!ecdh) | |
233 | return 0; | |
234 | if (cctx->ctx) | |
235 | rv = SSL_CTX_set_tmp_ecdh(cctx->ctx, ecdh); | |
236 | else if (cctx->ssl) | |
237 | rv = SSL_set_tmp_ecdh(cctx->ssl, ecdh); | |
238 | EC_KEY_free(ecdh); | |
0f113f3e MC |
239 | |
240 | return rv > 0; | |
241 | } | |
14536c8c | 242 | #endif |
ec2f7e56 | 243 | static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
244 | { |
245 | int rv = 1; | |
246 | if (cctx->ctx) | |
247 | rv = SSL_CTX_set_cipher_list(cctx->ctx, value); | |
248 | if (cctx->ssl) | |
249 | rv = SSL_set_cipher_list(cctx->ssl, value); | |
250 | return rv > 0; | |
251 | } | |
3db935a9 | 252 | |
ec2f7e56 | 253 | static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
254 | { |
255 | static const ssl_flag_tbl ssl_protocol_list[] = { | |
256 | SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK), | |
257 | SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2), | |
258 | SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3), | |
259 | SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1), | |
260 | SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1), | |
7946ab33 KR |
261 | SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2), |
262 | SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1), | |
263 | SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2) | |
0f113f3e | 264 | }; |
0f113f3e | 265 | cctx->tbl = ssl_protocol_list; |
b6eb9827 | 266 | cctx->ntbl = OSSL_NELEM(ssl_protocol_list); |
0f113f3e MC |
267 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); |
268 | } | |
3db935a9 | 269 | |
7946ab33 KR |
270 | /* |
271 | * protocol_from_string - converts a protocol version string to a number | |
272 | * | |
273 | * Returns -1 on failure or the version on success | |
274 | */ | |
275 | static int protocol_from_string(const char *value) | |
276 | { | |
277 | struct protocol_versions { | |
278 | const char *name; | |
279 | int version; | |
280 | }; | |
281 | static const struct protocol_versions versions[] = { | |
869e978c | 282 | {"None", 0}, |
7946ab33 KR |
283 | {"SSLv3", SSL3_VERSION}, |
284 | {"TLSv1", TLS1_VERSION}, | |
285 | {"TLSv1.1", TLS1_1_VERSION}, | |
286 | {"TLSv1.2", TLS1_2_VERSION}, | |
287 | {"DTLSv1", DTLS1_VERSION}, | |
288 | {"DTLSv1.2", DTLS1_2_VERSION}}; | |
289 | size_t i; | |
290 | size_t n = OSSL_NELEM(versions); | |
291 | ||
292 | for (i = 0; i < n; i++) | |
293 | if (strcmp(versions[i].name, value) == 0) | |
294 | return versions[i].version; | |
295 | return -1; | |
296 | } | |
297 | ||
4fa52141 VD |
298 | static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound) |
299 | { | |
300 | int method_version; | |
301 | int new_version; | |
302 | ||
303 | if (cctx->ctx != NULL) | |
304 | method_version = cctx->ctx->method->version; | |
305 | else if (cctx->ssl != NULL) | |
306 | method_version = cctx->ssl->ctx->method->version; | |
307 | else | |
308 | return 0; | |
309 | if ((new_version = protocol_from_string(value)) < 0) | |
310 | return 0; | |
311 | return ssl_set_version_bound(method_version, new_version, bound); | |
312 | } | |
313 | ||
7946ab33 KR |
314 | /* |
315 | * cmd_MinProtocol - Set min protocol version | |
316 | * @cctx: config structure to save settings in | |
317 | * @value: The min protocol version in string form | |
318 | * | |
319 | * Returns 1 on success and 0 on failure. | |
320 | */ | |
321 | static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value) | |
322 | { | |
4fa52141 | 323 | return min_max_proto(cctx, value, cctx->min_version); |
7946ab33 KR |
324 | } |
325 | ||
326 | /* | |
327 | * cmd_MaxProtocol - Set max protocol version | |
328 | * @cctx: config structure to save settings in | |
329 | * @value: The max protocol version in string form | |
330 | * | |
331 | * Returns 1 on success and 0 on failure. | |
332 | */ | |
333 | static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value) | |
334 | { | |
4fa52141 | 335 | return min_max_proto(cctx, value, cctx->max_version); |
7946ab33 KR |
336 | } |
337 | ||
ec2f7e56 | 338 | static int cmd_Options(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
339 | { |
340 | static const ssl_flag_tbl ssl_option_list[] = { | |
341 | SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET), | |
342 | SSL_FLAG_TBL_INV("EmptyFragments", | |
343 | SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS), | |
344 | SSL_FLAG_TBL("Bugs", SSL_OP_ALL), | |
345 | SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION), | |
346 | SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE), | |
347 | SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation", | |
348 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION), | |
349 | SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE), | |
350 | SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE), | |
351 | SSL_FLAG_TBL("UnsafeLegacyRenegotiation", | |
352 | SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION), | |
353 | }; | |
0f113f3e MC |
354 | if (value == NULL) |
355 | return -3; | |
356 | cctx->tbl = ssl_option_list; | |
b6eb9827 | 357 | cctx->ntbl = OSSL_NELEM(ssl_option_list); |
0f113f3e MC |
358 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); |
359 | } | |
3db935a9 | 360 | |
429261d0 DSH |
361 | static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value) |
362 | { | |
363 | static const ssl_flag_tbl ssl_vfy_list[] = { | |
364 | SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER), | |
365 | SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER), | |
366 | SSL_FLAG_VFY_SRV("Require", | |
367 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT), | |
368 | SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE) | |
369 | }; | |
370 | if (value == NULL) | |
371 | return -3; | |
372 | cctx->tbl = ssl_vfy_list; | |
373 | cctx->ntbl = OSSL_NELEM(ssl_vfy_list); | |
374 | return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx); | |
375 | } | |
376 | ||
ec2f7e56 | 377 | static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value) |
0f113f3e MC |
378 | { |
379 | int rv = 1; | |
2011b169 | 380 | CERT *c = NULL; |
2011b169 | 381 | if (cctx->ctx) { |
0f113f3e | 382 | rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value); |
2011b169 DSH |
383 | c = cctx->ctx->cert; |
384 | } | |
385 | if (cctx->ssl) { | |
fae4772c | 386 | rv = SSL_use_certificate_chain_file(cctx->ssl, value); |
2011b169 DSH |
387 | c = cctx->ssl->cert; |
388 | } | |
389 | if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) { | |
390 | char **pfilename = &cctx->cert_filename[c->key - c->pkeys]; | |
b548a1f1 | 391 | OPENSSL_free(*pfilename); |
7644a9ae | 392 | *pfilename = OPENSSL_strdup(value); |
2011b169 DSH |
393 | if (!*pfilename) |
394 | rv = 0; | |
395 | } | |
396 | ||
0f113f3e MC |
397 | return rv > 0; |
398 | } | |
ec2f7e56 DSH |
399 | |
400 | static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
401 | { |
402 | int rv = 1; | |
403 | if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE)) | |
404 | return -2; | |
405 | if (cctx->ctx) | |
406 | rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM); | |
407 | if (cctx->ssl) | |
408 | rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM); | |
409 | return rv > 0; | |
410 | } | |
5b7f36e8 DSH |
411 | |
412 | static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
413 | { |
414 | int rv = 1; | |
0f113f3e MC |
415 | if (cctx->ctx) |
416 | rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value); | |
417 | return rv > 0; | |
418 | } | |
5b7f36e8 | 419 | |
429261d0 DSH |
420 | static int do_store(SSL_CONF_CTX *cctx, |
421 | const char *CAfile, const char *CApath, int verify_store) | |
422 | { | |
423 | CERT *cert; | |
424 | X509_STORE **st; | |
425 | if (cctx->ctx) | |
426 | cert = cctx->ctx->cert; | |
427 | else if (cctx->ssl) | |
428 | cert = cctx->ssl->cert; | |
429 | else | |
430 | return 1; | |
431 | st = verify_store ? &cert->verify_store : &cert->chain_store; | |
432 | if (*st == NULL) { | |
433 | *st = X509_STORE_new(); | |
434 | if (*st == NULL) | |
435 | return 0; | |
436 | } | |
437 | return X509_STORE_load_locations(*st, CAfile, CApath) > 0; | |
438 | } | |
439 | ||
440 | static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value) | |
441 | { | |
442 | return do_store(cctx, NULL, value, 0); | |
443 | } | |
444 | ||
445 | static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value) | |
446 | { | |
447 | return do_store(cctx, value, NULL, 0); | |
448 | } | |
449 | ||
450 | static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value) | |
451 | { | |
452 | return do_store(cctx, NULL, value, 1); | |
453 | } | |
454 | ||
455 | static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value) | |
456 | { | |
457 | return do_store(cctx, value, NULL, 1); | |
458 | } | |
459 | ||
460 | static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value) | |
461 | { | |
462 | if (cctx->canames == NULL) | |
463 | cctx->canames = sk_X509_NAME_new_null(); | |
464 | if (cctx->canames == NULL) | |
465 | return 0; | |
466 | return SSL_add_file_cert_subjects_to_stack(cctx->canames, value); | |
467 | } | |
468 | ||
469 | static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value) | |
470 | { | |
471 | if (cctx->canames == NULL) | |
472 | cctx->canames = sk_X509_NAME_new_null(); | |
473 | if (cctx->canames == NULL) | |
474 | return 0; | |
475 | return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value); | |
476 | } | |
477 | ||
c557f921 DSH |
478 | #ifndef OPENSSL_NO_DH |
479 | static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value) | |
0f113f3e MC |
480 | { |
481 | int rv = 0; | |
482 | DH *dh = NULL; | |
483 | BIO *in = NULL; | |
0f113f3e | 484 | if (cctx->ctx || cctx->ssl) { |
9982cbbb | 485 | in = BIO_new(BIO_s_file()); |
a71edf3b | 486 | if (in == NULL) |
0f113f3e MC |
487 | goto end; |
488 | if (BIO_read_filename(in, value) <= 0) | |
489 | goto end; | |
490 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); | |
a71edf3b | 491 | if (dh == NULL) |
0f113f3e MC |
492 | goto end; |
493 | } else | |
494 | return 1; | |
495 | if (cctx->ctx) | |
496 | rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh); | |
497 | if (cctx->ssl) | |
498 | rv = SSL_set_tmp_dh(cctx->ssl, dh); | |
499 | end: | |
d6407083 | 500 | DH_free(dh); |
ca3a82c3 | 501 | BIO_free(in); |
0f113f3e MC |
502 | return rv > 0; |
503 | } | |
c557f921 | 504 | #endif |
0f113f3e MC |
505 | typedef struct { |
506 | int (*cmd) (SSL_CONF_CTX *cctx, const char *value); | |
507 | const char *str_file; | |
508 | const char *str_cmdline; | |
656b2605 DSH |
509 | unsigned short flags; |
510 | unsigned short value_type; | |
0f113f3e | 511 | } ssl_conf_cmd_tbl; |
3db935a9 | 512 | |
ec2f7e56 DSH |
513 | /* Table of supported parameters */ |
514 | ||
656b2605 DSH |
515 | #define SSL_CONF_CMD(name, cmdopt, flags, type) \ |
516 | {cmd_##name, #name, cmdopt, flags, type} | |
517 | ||
518 | #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \ | |
519 | SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING) | |
ec2f7e56 | 520 | |
656b2605 DSH |
521 | #define SSL_CONF_CMD_SWITCH(name, flags) \ |
522 | {0, NULL, name, flags, SSL_CONF_TYPE_NONE} | |
3db935a9 | 523 | |
7e1b7485 | 524 | /* See apps/apps.h if you change this table. */ |
27f3b65f | 525 | static const ssl_conf_cmd_tbl ssl_conf_cmds[] = { |
656b2605 DSH |
526 | SSL_CONF_CMD_SWITCH("no_ssl3", 0), |
527 | SSL_CONF_CMD_SWITCH("no_tls1", 0), | |
528 | SSL_CONF_CMD_SWITCH("no_tls1_1", 0), | |
529 | SSL_CONF_CMD_SWITCH("no_tls1_2", 0), | |
530 | SSL_CONF_CMD_SWITCH("bugs", 0), | |
cc5a9ba4 | 531 | SSL_CONF_CMD_SWITCH("no_comp", 0), |
dc5744cb | 532 | SSL_CONF_CMD_SWITCH("comp", 0), |
656b2605 | 533 | SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER), |
656b2605 | 534 | SSL_CONF_CMD_SWITCH("no_ticket", 0), |
656b2605 DSH |
535 | SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER), |
536 | SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0), | |
537 | SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER), | |
538 | SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER), | |
539 | SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER), | |
540 | SSL_CONF_CMD_SWITCH("strict", 0), | |
656b2605 DSH |
541 | SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0), |
542 | SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0), | |
543 | SSL_CONF_CMD_STRING(Curves, "curves", 0), | |
10bf4fc2 | 544 | #ifndef OPENSSL_NO_EC |
656b2605 | 545 | SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER), |
14536c8c | 546 | #endif |
656b2605 DSH |
547 | SSL_CONF_CMD_STRING(CipherString, "cipher", 0), |
548 | SSL_CONF_CMD_STRING(Protocol, NULL, 0), | |
453dfd8d EK |
549 | SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0), |
550 | SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0), | |
656b2605 | 551 | SSL_CONF_CMD_STRING(Options, NULL, 0), |
429261d0 | 552 | SSL_CONF_CMD_STRING(VerifyMode, NULL, 0), |
656b2605 DSH |
553 | SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE, |
554 | SSL_CONF_TYPE_FILE), | |
555 | SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE, | |
556 | SSL_CONF_TYPE_FILE), | |
557 | SSL_CONF_CMD(ServerInfoFile, NULL, | |
558 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
559 | SSL_CONF_TYPE_FILE), | |
429261d0 DSH |
560 | SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE, |
561 | SSL_CONF_TYPE_DIR), | |
562 | SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE, | |
563 | SSL_CONF_TYPE_FILE), | |
564 | SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE, | |
565 | SSL_CONF_TYPE_DIR), | |
566 | SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE, | |
567 | SSL_CONF_TYPE_FILE), | |
568 | SSL_CONF_CMD(ClientCAFile, NULL, | |
569 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
570 | SSL_CONF_TYPE_FILE), | |
571 | SSL_CONF_CMD(ClientCAPath, NULL, | |
572 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
573 | SSL_CONF_TYPE_DIR), | |
c557f921 | 574 | #ifndef OPENSSL_NO_DH |
656b2605 DSH |
575 | SSL_CONF_CMD(DHParameters, "dhparam", |
576 | SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE, | |
577 | SSL_CONF_TYPE_FILE) | |
578 | #endif | |
579 | }; | |
580 | ||
581 | /* Supported switches: must match order of switches in ssl_conf_cmds */ | |
582 | static const ssl_switch_tbl ssl_cmd_switches[] = { | |
583 | {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */ | |
584 | {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */ | |
585 | {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */ | |
586 | {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */ | |
587 | {SSL_OP_ALL, 0}, /* bugs */ | |
cc5a9ba4 VD |
588 | {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */ |
589 | {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */ | |
656b2605 | 590 | {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */ |
656b2605 | 591 | {SSL_OP_NO_TICKET, 0}, /* no_ticket */ |
656b2605 DSH |
592 | {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */ |
593 | /* legacy_renegotiation */ | |
594 | {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0}, | |
595 | /* legacy_server_connect */ | |
596 | {SSL_OP_LEGACY_SERVER_CONNECT, 0}, | |
597 | /* no_resumption_on_reneg */ | |
598 | {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0}, | |
599 | /* no_legacy_server_connect */ | |
600 | {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV}, | |
601 | {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */ | |
3db935a9 DSH |
602 | }; |
603 | ||
ec2f7e56 | 604 | static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd) |
0f113f3e MC |
605 | { |
606 | if (!pcmd || !*pcmd) | |
607 | return 0; | |
608 | /* If a prefix is set, check and skip */ | |
609 | if (cctx->prefix) { | |
610 | if (strlen(*pcmd) <= cctx->prefixlen) | |
611 | return 0; | |
612 | if (cctx->flags & SSL_CONF_FLAG_CMDLINE && | |
613 | strncmp(*pcmd, cctx->prefix, cctx->prefixlen)) | |
614 | return 0; | |
615 | if (cctx->flags & SSL_CONF_FLAG_FILE && | |
616 | strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen)) | |
617 | return 0; | |
618 | *pcmd += cctx->prefixlen; | |
619 | } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { | |
620 | if (**pcmd != '-' || !(*pcmd)[1]) | |
621 | return 0; | |
622 | *pcmd += 1; | |
623 | } | |
624 | return 1; | |
625 | } | |
626 | ||
656b2605 DSH |
627 | /* Determine if a command is allowed according to cctx flags */ |
628 | static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, | |
629 | const ssl_conf_cmd_tbl * t) | |
630 | { | |
631 | unsigned int tfl = t->flags; | |
632 | unsigned int cfl = cctx->flags; | |
633 | if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER)) | |
634 | return 0; | |
635 | if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT)) | |
636 | return 0; | |
637 | if ((tfl & SSL_CONF_FLAG_CERTIFICATE) | |
638 | && !(cfl & SSL_CONF_FLAG_CERTIFICATE)) | |
639 | return 0; | |
640 | return 1; | |
641 | } | |
642 | ||
0f113f3e MC |
643 | static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx, |
644 | const char *cmd) | |
645 | { | |
646 | const ssl_conf_cmd_tbl *t; | |
647 | size_t i; | |
648 | if (cmd == NULL) | |
649 | return NULL; | |
650 | ||
651 | /* Look for matching parameter name in table */ | |
b6eb9827 | 652 | for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) { |
656b2605 DSH |
653 | if (ssl_conf_cmd_allowed(cctx, t)) { |
654 | if (cctx->flags & SSL_CONF_FLAG_CMDLINE) { | |
86885c28 | 655 | if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0) |
656b2605 DSH |
656 | return t; |
657 | } | |
658 | if (cctx->flags & SSL_CONF_FLAG_FILE) { | |
86885c28 | 659 | if (t->str_file && strcasecmp(t->str_file, cmd) == 0) |
656b2605 DSH |
660 | return t; |
661 | } | |
0f113f3e MC |
662 | } |
663 | } | |
664 | return NULL; | |
665 | } | |
ec2f7e56 | 666 | |
656b2605 DSH |
667 | static int ctrl_switch_option(SSL_CONF_CTX *cctx, |
668 | const ssl_conf_cmd_tbl * cmd) | |
669 | { | |
670 | /* Find index of command in table */ | |
671 | size_t idx = cmd - ssl_conf_cmds; | |
672 | const ssl_switch_tbl *scmd; | |
673 | /* Sanity check index */ | |
674 | if (idx >= OSSL_NELEM(ssl_cmd_switches)) | |
675 | return 0; | |
676 | /* Obtain switches entry with same index */ | |
677 | scmd = ssl_cmd_switches + idx; | |
678 | ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1); | |
679 | return 1; | |
680 | } | |
681 | ||
ec2f7e56 | 682 | int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value) |
0f113f3e MC |
683 | { |
684 | const ssl_conf_cmd_tbl *runcmd; | |
685 | if (cmd == NULL) { | |
686 | SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME); | |
687 | return 0; | |
688 | } | |
689 | ||
690 | if (!ssl_conf_cmd_skip_prefix(cctx, &cmd)) | |
691 | return -2; | |
692 | ||
693 | runcmd = ssl_conf_cmd_lookup(cctx, cmd); | |
694 | ||
695 | if (runcmd) { | |
696 | int rv; | |
656b2605 DSH |
697 | if (runcmd->value_type == SSL_CONF_TYPE_NONE) { |
698 | return ctrl_switch_option(cctx, runcmd); | |
699 | } | |
0f113f3e MC |
700 | if (value == NULL) |
701 | return -3; | |
702 | rv = runcmd->cmd(cctx, value); | |
703 | if (rv > 0) | |
704 | return 2; | |
705 | if (rv == -2) | |
706 | return -2; | |
707 | if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) { | |
708 | SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE); | |
709 | ERR_add_error_data(4, "cmd=", cmd, ", value=", value); | |
710 | } | |
711 | return 0; | |
712 | } | |
713 | ||
0f113f3e MC |
714 | if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) { |
715 | SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME); | |
716 | ERR_add_error_data(2, "cmd=", cmd); | |
717 | } | |
718 | ||
719 | return -2; | |
720 | } | |
3db935a9 DSH |
721 | |
722 | int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv) | |
0f113f3e MC |
723 | { |
724 | int rv; | |
725 | const char *arg = NULL, *argn; | |
726 | if (pargc && *pargc == 0) | |
727 | return 0; | |
728 | if (!pargc || *pargc > 0) | |
729 | arg = **pargv; | |
730 | if (arg == NULL) | |
731 | return 0; | |
732 | if (!pargc || *pargc > 1) | |
733 | argn = (*pargv)[1]; | |
734 | else | |
735 | argn = NULL; | |
736 | cctx->flags &= ~SSL_CONF_FLAG_FILE; | |
737 | cctx->flags |= SSL_CONF_FLAG_CMDLINE; | |
738 | rv = SSL_CONF_cmd(cctx, arg, argn); | |
739 | if (rv > 0) { | |
740 | /* Success: update pargc, pargv */ | |
741 | (*pargv) += rv; | |
742 | if (pargc) | |
743 | (*pargc) -= rv; | |
744 | return rv; | |
745 | } | |
746 | /* Unknown switch: indicate no arguments processed */ | |
747 | if (rv == -2) | |
748 | return 0; | |
749 | /* Some error occurred processing command, return fatal error */ | |
750 | if (rv == 0) | |
751 | return -1; | |
752 | return rv; | |
753 | } | |
3db935a9 | 754 | |
ec2f7e56 | 755 | int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd) |
0f113f3e MC |
756 | { |
757 | if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) { | |
758 | const ssl_conf_cmd_tbl *runcmd; | |
759 | runcmd = ssl_conf_cmd_lookup(cctx, cmd); | |
760 | if (runcmd) | |
761 | return runcmd->value_type; | |
762 | } | |
763 | return SSL_CONF_TYPE_UNKNOWN; | |
764 | } | |
ec2f7e56 | 765 | |
3db935a9 | 766 | SSL_CONF_CTX *SSL_CONF_CTX_new(void) |
0f113f3e | 767 | { |
64b25758 | 768 | SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret)); |
b4faea50 | 769 | |
0f113f3e MC |
770 | return ret; |
771 | } | |
3db935a9 | 772 | |
ec2f7e56 | 773 | int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx) |
0f113f3e | 774 | { |
2011b169 DSH |
775 | /* See if any certificates are missing private keys */ |
776 | size_t i; | |
777 | CERT *c = NULL; | |
778 | if (cctx->ctx) | |
779 | c = cctx->ctx->cert; | |
780 | else if (cctx->ssl) | |
781 | c = cctx->ssl->cert; | |
782 | if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) { | |
783 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
784 | const char *p = cctx->cert_filename[i]; | |
785 | /* | |
786 | * If missing private key try to load one from certificate file | |
787 | */ | |
788 | if (p && !c->pkeys[i].privatekey) { | |
789 | if (!cmd_PrivateKey(cctx, p)) | |
790 | return 0; | |
791 | } | |
792 | } | |
793 | } | |
429261d0 DSH |
794 | if (cctx->canames) { |
795 | if (cctx->ssl) | |
796 | SSL_set_client_CA_list(cctx->ssl, cctx->canames); | |
797 | else if (cctx->ctx) | |
798 | SSL_CTX_set_client_CA_list(cctx->ctx, cctx->canames); | |
799 | else | |
800 | sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free); | |
801 | cctx->canames = NULL; | |
802 | } | |
0f113f3e MC |
803 | return 1; |
804 | } | |
ec2f7e56 | 805 | |
3db935a9 | 806 | void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx) |
0f113f3e MC |
807 | { |
808 | if (cctx) { | |
2011b169 | 809 | size_t i; |
656b2605 | 810 | for (i = 0; i < SSL_PKEY_NUM; i++) |
b548a1f1 | 811 | OPENSSL_free(cctx->cert_filename[i]); |
b548a1f1 | 812 | OPENSSL_free(cctx->prefix); |
429261d0 | 813 | sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free); |
4445704f | 814 | OPENSSL_free(cctx); |
0f113f3e MC |
815 | } |
816 | } | |
3db935a9 DSH |
817 | |
818 | unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags) | |
0f113f3e MC |
819 | { |
820 | cctx->flags |= flags; | |
821 | return cctx->flags; | |
822 | } | |
3db935a9 DSH |
823 | |
824 | unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags) | |
0f113f3e MC |
825 | { |
826 | cctx->flags &= ~flags; | |
827 | return cctx->flags; | |
828 | } | |
3db935a9 DSH |
829 | |
830 | int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre) | |
0f113f3e MC |
831 | { |
832 | char *tmp = NULL; | |
833 | if (pre) { | |
7644a9ae | 834 | tmp = OPENSSL_strdup(pre); |
0f113f3e MC |
835 | if (tmp == NULL) |
836 | return 0; | |
837 | } | |
b548a1f1 | 838 | OPENSSL_free(cctx->prefix); |
0f113f3e MC |
839 | cctx->prefix = tmp; |
840 | if (tmp) | |
841 | cctx->prefixlen = strlen(tmp); | |
842 | else | |
843 | cctx->prefixlen = 0; | |
844 | return 1; | |
845 | } | |
3db935a9 DSH |
846 | |
847 | void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl) | |
0f113f3e MC |
848 | { |
849 | cctx->ssl = ssl; | |
850 | cctx->ctx = NULL; | |
851 | if (ssl) { | |
852 | cctx->poptions = &ssl->options; | |
7946ab33 KR |
853 | cctx->min_version = &ssl->min_proto_version; |
854 | cctx->max_version = &ssl->max_proto_version; | |
0f113f3e | 855 | cctx->pcert_flags = &ssl->cert->cert_flags; |
429261d0 | 856 | cctx->pvfy_flags = &ssl->verify_mode; |
0f113f3e MC |
857 | } else { |
858 | cctx->poptions = NULL; | |
7946ab33 KR |
859 | cctx->min_version = NULL; |
860 | cctx->max_version = NULL; | |
0f113f3e | 861 | cctx->pcert_flags = NULL; |
429261d0 | 862 | cctx->pvfy_flags = NULL; |
0f113f3e MC |
863 | } |
864 | } | |
3db935a9 DSH |
865 | |
866 | void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx) | |
0f113f3e MC |
867 | { |
868 | cctx->ctx = ctx; | |
869 | cctx->ssl = NULL; | |
870 | if (ctx) { | |
871 | cctx->poptions = &ctx->options; | |
7946ab33 KR |
872 | cctx->min_version = &ctx->min_proto_version; |
873 | cctx->max_version = &ctx->max_proto_version; | |
0f113f3e | 874 | cctx->pcert_flags = &ctx->cert->cert_flags; |
429261d0 | 875 | cctx->pvfy_flags = &ctx->verify_mode; |
0f113f3e MC |
876 | } else { |
877 | cctx->poptions = NULL; | |
7946ab33 KR |
878 | cctx->min_version = NULL; |
879 | cctx->max_version = NULL; | |
0f113f3e | 880 | cctx->pcert_flags = NULL; |
429261d0 | 881 | cctx->pvfy_flags = NULL; |
0f113f3e MC |
882 | } |
883 | } |