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