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