VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/ssl/ssl_conf.c@ 94330

Last change on this file since 94330 was 94082, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette