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