Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/ssl/ssl_conf.c

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