OpenVPN
ssl_mbedtls.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 * Copyright (C) 2006-2010, Brainspark B.V.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, see <https://www.gnu.org/licenses/>.
23 */
24
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include "syshead.h"
35
36#if defined(ENABLE_CRYPTO_MBEDTLS)
37
38#include "errlevel.h"
39#include "ssl_backend.h"
40#include "base64.h"
41#include "buffer.h"
42#include "misc.h"
43#include "manage.h"
44#include "pkcs11_backend.h"
45#include "ssl_common.h"
46#include "ssl_util.h"
47
48#include "ssl_verify_mbedtls.h"
49#include <mbedtls/debug.h>
50#include <mbedtls/error.h>
51#include <mbedtls/net_sockets.h>
52#include <mbedtls/oid.h>
53#include <mbedtls/pem.h>
54#include <mbedtls/version.h>
55
56static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy = {
57 /* Hashes from SHA-1 and above */
58 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160)
59 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256)
60 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
61 0xFFFFFFF, /* Any PK alg */
62 0xFFFFFFF, /* Any curve */
63 1024, /* RSA-1024 and larger */
64};
65
66static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_preferred = {
67 /* SHA-2 and above */
68 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256)
69 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
70 0xFFFFFFF, /* Any PK alg */
71 0xFFFFFFF, /* Any curve */
72 2048, /* RSA-2048 and larger */
73};
74
75#define openvpn_x509_crt_profile_suiteb mbedtls_x509_crt_profile_suiteb;
76
77void
78tls_init_lib(void)
79{
80 if (psa_crypto_init() != PSA_SUCCESS)
81 {
82 msg(M_FATAL, "mbedtls: psa_crypto_init() failed");
83 }
84}
85
86void
87tls_free_lib(void)
88{
89}
90
91void
92tls_ctx_new(struct tls_root_ctx *ctx)
93{
94 ASSERT(NULL != ctx);
95 CLEAR(*ctx);
96
97 ALLOC_OBJ_CLEAR(ctx->ca_chain, mbedtls_x509_crt);
98
99 ctx->initialised = true;
100}
101
102
103void
104tls_ctx_free(struct tls_root_ctx *ctx)
105{
106 if (ctx)
107 {
108 mbedtls_pk_free(ctx->priv_key);
109 free(ctx->priv_key);
110
111 mbedtls_x509_crt_free(ctx->ca_chain);
112 free(ctx->ca_chain);
113
114 mbedtls_x509_crt_free(ctx->crt_chain);
115 free(ctx->crt_chain);
116
117 mbedtls_x509_crl_free(ctx->crl);
118 free(ctx->crl);
119
120#if defined(ENABLE_PKCS11)
121 /* ...freeCertificate() can handle NULL ptrs, but if pkcs11 helper
122 * has not been initialized, it will ASSERT() - so, do not pass NULL
123 */
124 if (ctx->pkcs11_cert)
125 {
126 pkcs11h_certificate_freeCertificate(ctx->pkcs11_cert);
127 }
128#endif
129
130 free(ctx->allowed_ciphers);
131
132 free(ctx->groups);
133
134 CLEAR(*ctx);
135
136 ctx->initialised = false;
137 }
138}
139
140bool
141tls_ctx_initialised(const struct tls_root_ctx *ctx)
142{
143 /* either this should be NULL or should be non-null and then have a
144 * valid TLS ctx inside as well */
145 ASSERT(NULL == ctx || ctx->initialised);
146 return ctx != NULL;
147}
148
149bool
150key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size,
151 void *ekm, size_t ekm_size)
152{
153 ASSERT(strlen(label) == label_size);
154
155 mbedtls_ssl_context *ctx = session->key[KS_PRIMARY].ks_ssl.ctx;
156 return mbed_ok(mbedtls_ssl_export_keying_material(ctx, ekm, ekm_size, label, label_size,
157 NULL, 0, 0));
158}
159
160bool
161tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
162{
163 return true;
164}
165
166static const char *
167tls_translate_cipher_name(const char *cipher_name)
168{
169 const tls_cipher_name_pair *pair = tls_get_cipher_name_pair(cipher_name, strlen(cipher_name));
170
171 if (NULL == pair)
172 {
173 /* No translation found, return original */
174 return cipher_name;
175 }
176
177 if (0 != strcmp(cipher_name, pair->iana_name))
178 {
179 /* Deprecated name found, notify user */
180 msg(M_WARN, "Deprecated cipher suite name '%s', please use IANA name '%s'",
181 pair->openssl_name, pair->iana_name);
182 }
183
184 return pair->iana_name;
185}
186
187void
188tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
189{
190 if (ciphers == NULL)
191 {
192 /* Nothing to do, return without warning message */
193 return;
194 }
195
196 msg(M_WARN,
197 "mbed TLS does not support setting tls-ciphersuites. "
198 "Ignoring TLS 1.3 cipher list: %s",
199 ciphers);
200}
201
202void
203tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
204{
205 char *tmp_ciphers, *tmp_ciphers_orig;
206
207 if (NULL == ciphers)
208 {
209 return; /* Nothing to do */
210 }
211
212 ASSERT(NULL != ctx);
213
214 /* Get number of ciphers */
215 int cipher_count = get_num_elements(ciphers, ':');
216
217 /* Allocate an array for them */
218 ALLOC_ARRAY_CLEAR(ctx->allowed_ciphers, int, cipher_count + 1)
219
220 /* Parse allowed ciphers, getting IDs */
221 int i = 0;
222 char *lasts = NULL;
223 tmp_ciphers_orig = tmp_ciphers = string_alloc(ciphers, NULL);
224
225 const char *token = strtok_r(tmp_ciphers, ":", &lasts);
226 while (token)
227 {
228 ctx->allowed_ciphers[i] = mbedtls_ssl_get_ciphersuite_id(tls_translate_cipher_name(token));
229 if (0 != ctx->allowed_ciphers[i])
230 {
231 i++;
232 }
233 token = strtok_r(NULL, ":", &lasts);
234 }
235 free(tmp_ciphers_orig);
236}
237
238void
239tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
240{
241 if (!profile || 0 == strcmp(profile, "legacy") || 0 == strcmp(profile, "insecure"))
242 {
243 ctx->cert_profile = openvpn_x509_crt_profile_legacy;
244 }
245 else if (0 == strcmp(profile, "preferred"))
246 {
247 ctx->cert_profile = openvpn_x509_crt_profile_preferred;
248 }
249 else if (0 == strcmp(profile, "suiteb"))
250 {
251 ctx->cert_profile = openvpn_x509_crt_profile_suiteb;
252 }
253 else
254 {
255 msg(M_FATAL, "ERROR: Invalid cert profile: %s", profile);
256 }
257}
258
259typedef struct
260{
261 const char *name;
262 uint16_t tls_id;
263} mbedtls_ecp_curve_info;
264
265static const mbedtls_ecp_curve_info ecp_curve_info_table[] = {
266/* secp curves. */
267#if defined(PSA_WANT_ECC_SECP_R1_256)
268 { "secp256r1", MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 },
269#endif
270#if defined(PSA_WANT_ECC_SECP_R1_384)
271 { "secp384r1", MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1 },
272#endif
273#if defined(PSA_WANT_ECC_SECP_R1_521)
274 { "secp521r1", MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1 },
275#endif
276
277/* Curve25519. */
278#if defined(PSA_WANT_ECC_MONTGOMERY_255)
279 { "X25519", MBEDTLS_SSL_IANA_TLS_GROUP_X25519 },
280#endif
281
282/* Curve448. */
283#if defined(PSA_WANT_ECC_MONTGOMERY_448)
284 { "X448", MBEDTLS_SSL_IANA_TLS_GROUP_X448 },
285#endif
286
287/* Brainpool curves. */
288#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
289 { "brainpoolP256r1", MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1 },
290#endif
291#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
292 { "brainpoolP384r1", MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1 },
293#endif
294#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
295 { "brainpoolP512r1", MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1 },
296#endif
297
298/* Named Diffie-Hellman groups. */
299#if defined(PSA_WANT_DH_RFC7919_2048)
300 { "ffdhe2048", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048 },
301#endif
302#if defined(PSA_WANT_DH_RFC7919_3072)
303 { "ffdhe3072", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072 },
304#endif
305#if defined(PSA_WANT_DH_RFC7919_4096)
306 { "ffdhe4096", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096 },
307#endif
308#if defined(PSA_WANT_DH_RFC7919_6144)
309 { "ffdhe6144", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144 },
310#endif
311#if defined(PSA_WANT_DH_RFC7919_8192)
312 { "ffdhe8192", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192 },
313#endif
314};
315static const size_t ecp_curve_info_table_items = sizeof(ecp_curve_info_table) / sizeof(mbedtls_ecp_curve_info);
316
317static const mbedtls_ecp_curve_info *
318mbedtls_ecp_curve_info_from_name(const char *name)
319{
320 for (size_t i = 0; i < ecp_curve_info_table_items; i++)
321 {
322 if (strcmp(name, ecp_curve_info_table[i].name) == 0)
323 {
324 return &ecp_curve_info_table[i];
325 }
326 }
327 return NULL;
328}
329
330void
331tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
332{
333 ASSERT(ctx);
334 struct gc_arena gc = gc_new();
335
336 /* Get number of groups and allocate an array in ctx */
337 int groups_count = get_num_elements(groups, ':');
338 ALLOC_ARRAY_CLEAR(ctx->groups, uint16_t, groups_count + 1)
339
340 /* Parse allowed ciphers, getting IDs */
341 int i = 0;
342 char *tmp_groups = string_alloc(groups, &gc);
343
344 const char *token;
345 while ((token = strsep(&tmp_groups, ":")))
346 {
347 const mbedtls_ecp_curve_info *ci = mbedtls_ecp_curve_info_from_name(token);
348 if (!ci)
349 {
350 msg(M_WARN, "Warning unknown curve/group specified: %s", token);
351 }
352 else
353 {
354 ctx->groups[i] = ci->tls_id;
355 i++;
356 }
357 }
358
359 /* Check if any groups were valid. */
360 if (i == 0)
361 {
362 msg(M_FATAL, "Error: All groups in \"%s\" are invalid or unsupported.", groups);
363 }
364
365 /* Recent mbedtls versions state that the list of groups must be terminated
366 * with 0. Older versions state that it must be terminated with MBEDTLS_ECP_DP_NONE
367 * which is also 0, so this works either way. */
368 ctx->groups[i] = 0;
369
370 gc_free(&gc);
371}
372
373
374void
375tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
376{
377 ASSERT(ctx);
378 if (ctx->crt_chain == NULL)
379 {
380 return; /* Nothing to check if there is no certificate */
381 }
382
383 if (mbedtls_x509_time_is_future(&ctx->crt_chain->valid_from))
384 {
385 msg(M_WARN, "WARNING: Your certificate is not yet valid!");
386 }
387
388 if (mbedtls_x509_time_is_past(&ctx->crt_chain->valid_to))
389 {
390 msg(M_WARN, "WARNING: Your certificate has expired!");
391 }
392}
393
394void
395tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_inline)
396{
397 if (strcmp(dh_file, "none") != 0)
398 {
399 msg(M_FATAL, "Mbed TLS 4 only supports pre-defined Diffie-Hellman groups.");
400 }
401}
402
403void
404tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
405{
406 if (NULL != curve_name)
407 {
408 msg(M_WARN, "WARNING: mbed TLS builds do not support specifying an "
409 "ECDH curve with --ecdh-curve, using default curves. Use "
410 "--tls-groups to specify curves.");
411 }
412}
413
414int
415tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline,
416 bool load_ca_file)
417{
418 msg(M_FATAL, "PKCS #12 files not yet supported for mbed TLS.");
419 return 0;
420}
421
422#ifdef ENABLE_CRYPTOAPI
423void
424tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
425{
426 msg(M_FATAL, "Windows CryptoAPI not yet supported for mbed TLS.");
427}
428#endif /* _WIN32 */
429
430void
431tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_inline)
432{
433 ASSERT(NULL != ctx);
434
435 if (!ctx->crt_chain)
436 {
437 ALLOC_OBJ_CLEAR(ctx->crt_chain, mbedtls_x509_crt);
438 }
439
440 if (cert_inline)
441 {
442 if (!cert_file)
443 {
444 msg(M_FATAL, "Cannot load inline certificate: NULL");
445 }
446 if (!mbed_ok(mbedtls_x509_crt_parse(ctx->crt_chain, (const unsigned char *)cert_file,
447 strlen(cert_file) + 1)))
448 {
449 msg(M_FATAL, "Cannot load inline certificate");
450 }
451 }
452 else
453 {
454 if (!mbed_ok(mbedtls_x509_crt_parse_file(ctx->crt_chain, cert_file)))
455 {
456 msg(M_FATAL, "Cannot load certificate file %s", cert_file);
457 }
458 }
459}
460
461int
462tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_inline)
463{
464 int status;
465 ASSERT(NULL != ctx);
466
467 if (!ctx->priv_key)
468 {
469 ALLOC_OBJ_CLEAR(ctx->priv_key, mbedtls_pk_context);
470 }
471
472 if (priv_key_inline)
473 {
474 status = mbedtls_pk_parse_key(ctx->priv_key, (const unsigned char *)priv_key_file,
475 strlen(priv_key_file) + 1, NULL, 0);
476
477 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED == status)
478 {
479 char passbuf[512] = { 0 };
480 pem_password_callback(passbuf, 512, 0, NULL);
481 status = mbedtls_pk_parse_key(
482 ctx->priv_key, (const unsigned char *)priv_key_file, strlen(priv_key_file) + 1,
483 (unsigned char *)passbuf, strlen(passbuf));
484 }
485 }
486 else
487 {
488 status = mbedtls_pk_parse_keyfile(ctx->priv_key, priv_key_file, NULL);
489 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED == status)
490 {
491 char passbuf[512] = { 0 };
492 pem_password_callback(passbuf, 512, 0, NULL);
493 status = mbedtls_pk_parse_keyfile(ctx->priv_key, priv_key_file, passbuf);
494 }
495 }
496 if (!mbed_ok(status))
497 {
498#ifdef ENABLE_MANAGEMENT
499 if (management && (MBEDTLS_ERR_PK_PASSWORD_MISMATCH == status))
500 {
502 }
503#endif
504 msg(M_WARN, "Cannot load private key file %s",
505 print_key_filename(priv_key_file, priv_key_inline));
506 return 1;
507 }
508
509 /* work around bug in mbedtls 4.1.0 by adding missing public key information in prv
510 * cf. https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/807 */
511#if MBEDTLS_VERSION_NUMBER >= 0x04010000
512 if (ctx->priv_key->MBEDTLS_PRIVATE(pub_raw_len) == 0)
513 {
514 ASSERT(mbed_ok(psa_export_public_key(ctx->priv_key->MBEDTLS_PRIVATE(priv_id),
515 ctx->priv_key->MBEDTLS_PRIVATE(pub_raw),
516 sizeof(ctx->priv_key->MBEDTLS_PRIVATE(pub_raw)),
517 &ctx->priv_key->MBEDTLS_PRIVATE(pub_raw_len))));
518 }
519#endif
520 if (!mbed_ok(mbedtls_pk_check_pair(&ctx->crt_chain->pk, ctx->priv_key)))
521 {
522 msg(M_WARN, "Private key does not match the certificate");
523 return 1;
524 }
525
526 return 0;
527}
528
529int
531 void *sign_ctx)
532{
533 msg(M_WARN, "tls_ctx_use_external_signing_func is not implemented for Mbed TLS 4.");
534 return 1;
535}
536
537#ifdef ENABLE_MANAGEMENT
539static bool
540management_sign_func(void *sign_ctx, const void *src, size_t src_len, void *dst, size_t dst_len)
541{
542 bool ret = false;
543 char *src_b64 = NULL;
544 char *dst_b64 = NULL;
545
546 if (!management || (openvpn_base64_encode(src, (int)src_len, &src_b64) <= 0))
547 {
548 goto cleanup;
549 }
550
551 /*
552 * We only support RSA external keys and PKCS1 signatures at the moment
553 * in mbed TLS, so the signature parameter is hardcoded to this encoding
554 */
555 if (!(dst_b64 = management_query_pk_sig(management, src_b64, "RSA_PKCS1_PADDING")))
556 {
557 goto cleanup;
558 }
559
560 if (openvpn_base64_decode(dst_b64, dst, (int)dst_len) != (int)dst_len)
561 {
562 goto cleanup;
563 }
564
565 ret = true;
566cleanup:
567 free(src_b64);
568 free(dst_b64);
569
570 return ret;
571}
572
573int
575{
576 return tls_ctx_use_external_signing_func(ctx, management_sign_func, NULL);
577}
578
579#endif /* ifdef ENABLE_MANAGEMENT */
580
581void
582tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_inline, const char *ca_path,
583 bool tls_server)
584{
585 if (ca_path)
586 {
587 msg(M_FATAL, "ERROR: mbed TLS cannot handle the capath directive");
588 }
589
590 if (ca_file && ca_inline)
591 {
592 if (!mbed_ok(mbedtls_x509_crt_parse(ctx->ca_chain, (const unsigned char *)ca_file,
593 strlen(ca_file) + 1)))
594 {
595 msg(M_FATAL, "Cannot load inline CA certificates");
596 }
597 }
598 else
599 {
600 /* Load CA file for verifying peer supplied certificate */
601 if (!mbed_ok(mbedtls_x509_crt_parse_file(ctx->ca_chain, ca_file)))
602 {
603 msg(M_FATAL, "Cannot load CA certificate file %s", ca_file);
604 }
605 }
606}
607
608void
609tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file,
610 bool extra_certs_inline)
611{
612 ASSERT(NULL != ctx);
613
614 if (!ctx->crt_chain)
615 {
616 ALLOC_OBJ_CLEAR(ctx->crt_chain, mbedtls_x509_crt);
617 }
618
619 if (extra_certs_inline)
620 {
621 if (!mbed_ok(mbedtls_x509_crt_parse(ctx->crt_chain, (const unsigned char *)extra_certs_file,
622 strlen(extra_certs_file) + 1)))
623 {
624 msg(M_FATAL, "Cannot load inline extra-certs file");
625 }
626 }
627 else
628 {
629 if (!mbed_ok(mbedtls_x509_crt_parse_file(ctx->crt_chain, extra_certs_file)))
630 {
631 msg(M_FATAL, "Cannot load extra-certs file: %s", extra_certs_file);
632 }
633 }
634}
635
636/* **************************************
637 *
638 * Key-state specific functions
639 *
640 ***************************************/
641
642/*
643 * "Endless buffer"
644 */
645
646static inline void
647buf_free_entry(buffer_entry *entry)
648{
649 if (NULL != entry)
650 {
651 free(entry->data);
652 free(entry);
653 }
654}
655
656static void
657buf_free_entries(endless_buffer *buf)
658{
659 while (buf->first_block)
660 {
661 buffer_entry *cur_block = buf->first_block;
662 buf->first_block = cur_block->next_block;
663 buf_free_entry(cur_block);
664 }
665 buf->last_block = NULL;
666}
667
668static int
669endless_buf_read(endless_buffer *in, unsigned char *out, size_t out_len)
670{
671 size_t read_len = 0;
672
673 ASSERT(out_len <= INT_MAX);
674
675 if (in->first_block == NULL)
676 {
677 return MBEDTLS_ERR_SSL_WANT_READ;
678 }
679
680 while (in->first_block != NULL && read_len < out_len)
681 {
682 size_t block_len = in->first_block->length - in->data_start;
683 if (block_len <= out_len - read_len)
684 {
685 buffer_entry *cur_entry = in->first_block;
686 memcpy(out + read_len, cur_entry->data + in->data_start, block_len);
687
688 read_len += block_len;
689
690 in->first_block = cur_entry->next_block;
691 in->data_start = 0;
692
693 if (in->first_block == NULL)
694 {
695 in->last_block = NULL;
696 }
697
698 buf_free_entry(cur_entry);
699 }
700 else
701 {
702 memcpy(out + read_len, in->first_block->data + in->data_start, out_len - read_len);
703 in->data_start += out_len - read_len;
704 read_len = out_len;
705 }
706 }
707
708 return (int)read_len;
709}
710
711static int
712endless_buf_write(endless_buffer *out, const unsigned char *in, size_t len)
713{
714 buffer_entry *new_block = malloc(sizeof(buffer_entry));
715 if (NULL == new_block)
716 {
717 return MBEDTLS_ERR_NET_SEND_FAILED;
718 }
719
720 new_block->data = malloc(len);
721 if (NULL == new_block->data)
722 {
723 free(new_block);
724 return MBEDTLS_ERR_NET_SEND_FAILED;
725 }
726
727 ASSERT(len <= INT_MAX);
728
729 new_block->length = len;
730 new_block->next_block = NULL;
731
732 memcpy(new_block->data, in, len);
733
734 if (NULL == out->first_block)
735 {
736 out->first_block = new_block;
737 }
738
739 if (NULL != out->last_block)
740 {
741 out->last_block->next_block = new_block;
742 }
743
744 out->last_block = new_block;
745
746 return (int)len;
747}
748
749static int
750ssl_bio_read(void *ctx, unsigned char *out, size_t out_len)
751{
752 bio_ctx *my_ctx = (bio_ctx *)ctx;
753 return endless_buf_read(&my_ctx->in, out, out_len);
754}
755
756static int
757ssl_bio_write(void *ctx, const unsigned char *in, size_t in_len)
758{
759 bio_ctx *my_ctx = (bio_ctx *)ctx;
760 return endless_buf_write(&my_ctx->out, in, in_len);
761}
762
763static void
764my_debug(void *ctx, int level, const char *file, int line, const char *str)
765{
766 msglvl_t my_loglevel = (level < 3) ? D_TLS_DEBUG_MED : D_TLS_DEBUG;
767 msg(my_loglevel, "mbed TLS msg (%s:%d): %s", file, line, str);
768}
769
770/*
771 * Further personalise the RNG using a hash of the public key
772 */
773void
774tls_ctx_personalise_random(struct tls_root_ctx *ctx)
775{
776}
777
778int
779tls_version_max(void)
780{
781#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
782 return TLS_VER_1_3;
783#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
784 return TLS_VER_1_2;
785#else
786#error mbedtls is compiled without support for TLS 1.2 or 1.3
787#endif
788}
789
797mbedtls_ssl_protocol_version
798tls_version_to_ssl_version(int tls_ver)
799{
800 switch (tls_ver)
801 {
802#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
803 case TLS_VER_1_2:
804 return MBEDTLS_SSL_VERSION_TLS1_2;
805#endif
806
807#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
808 case TLS_VER_1_3:
809 return MBEDTLS_SSL_VERSION_TLS1_3;
810#endif
811
812 default:
813 msg(M_FATAL, "%s: invalid or unsupported TLS version %d", __func__, tls_ver);
814 return MBEDTLS_SSL_VERSION_UNKNOWN;
815 }
816}
817
818void
819backend_tls_ctx_reload_crl(struct tls_root_ctx *ctx, const char *crl_file, bool crl_inline)
820{
821 ASSERT(crl_file);
822
823 if (ctx->crl == NULL)
824 {
825 ALLOC_OBJ_CLEAR(ctx->crl, mbedtls_x509_crl);
826 }
827 mbedtls_x509_crl_free(ctx->crl);
828
829 if (crl_inline)
830 {
831 if (!mbed_ok(mbedtls_x509_crl_parse(ctx->crl, (const unsigned char *)crl_file,
832 strlen(crl_file) + 1)))
833 {
834 msg(M_WARN, "CRL: cannot parse inline CRL");
835 goto err;
836 }
837 }
838 else
839 {
840 if (!mbed_ok(mbedtls_x509_crl_parse_file(ctx->crl, crl_file)))
841 {
842 msg(M_WARN, "CRL: cannot read CRL from file %s", crl_file);
843 goto err;
844 }
845 }
846 return;
847
848err:
849 mbedtls_x509_crl_free(ctx->crl);
850}
851
852void
853key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server,
854 struct tls_session *session)
855{
856 ASSERT(NULL != ssl_ctx);
857 ASSERT(ks_ssl);
858 CLEAR(*ks_ssl);
859
860 /* Initialise SSL config */
861 ALLOC_OBJ_CLEAR(ks_ssl->ssl_config, mbedtls_ssl_config);
862 mbedtls_ssl_config_init(ks_ssl->ssl_config);
863 int endpoint = is_server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
864 mbedtls_ssl_config_defaults(ks_ssl->ssl_config, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM,
865 MBEDTLS_SSL_PRESET_DEFAULT);
866#ifdef MBEDTLS_DEBUG_C
867 /* We only want to have mbed TLS generate debug level logging when we would
868 * also display it.
869 * In fact mbed TLS 2.25.0 crashes generating debug log if Curve25591 is
870 * selected for DH (https://github.com/ARMmbed/mbedtls/issues/4208) */
871 if (session->opt->ssl_flags & SSLF_TLS_DEBUG_ENABLED)
872 {
873 mbedtls_debug_set_threshold(3);
874 }
875 else
876 {
877 mbedtls_debug_set_threshold(2);
878 }
879#endif
880 mbedtls_ssl_conf_dbg(ks_ssl->ssl_config, my_debug, NULL);
881
882 mbedtls_ssl_conf_cert_profile(ks_ssl->ssl_config, &ssl_ctx->cert_profile);
883
884 if (ssl_ctx->allowed_ciphers)
885 {
886 mbedtls_ssl_conf_ciphersuites(ks_ssl->ssl_config, ssl_ctx->allowed_ciphers);
887 }
888
889 if (ssl_ctx->groups)
890 {
891 mbedtls_ssl_conf_groups(ks_ssl->ssl_config, ssl_ctx->groups);
892 }
893
894 /* Disable TLS renegotiations if the mbedtls library supports that feature.
895 * OpenVPN's renegotiation creates new SSL sessions and does not depend on
896 * this feature and TLS renegotiations have been problematic in the past. */
897#if defined(MBEDTLS_SSL_RENEGOTIATION)
898 mbedtls_ssl_conf_renegotiation(ks_ssl->ssl_config, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
899#endif /* MBEDTLS_SSL_RENEGOTIATION */
900
901 /* Disable record splitting (for now). OpenVPN assumes records are sent
902 * unfragmented, and changing that will require thorough review and
903 * testing. Since OpenVPN is not susceptible to BEAST, we can just
904 * disable record splitting as a quick fix. */
905#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
906 mbedtls_ssl_conf_cbc_record_splitting(ks_ssl->ssl_config,
907 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
908#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
909
910 /* Initialise authentication information */
911 (void)mbed_ok(mbedtls_ssl_conf_own_cert(ks_ssl->ssl_config, ssl_ctx->crt_chain, ssl_ctx->priv_key));
912
913 /* Initialise SSL verification */
914 if (session->opt->ssl_flags & SSLF_CLIENT_CERT_OPTIONAL)
915 {
916 mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);
917 }
918 else if (!(session->opt->ssl_flags & SSLF_CLIENT_CERT_NOT_REQUIRED))
919 {
920 mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);
921 }
922 mbedtls_ssl_conf_verify(ks_ssl->ssl_config, verify_callback, session);
923
924 /* TODO: mbed TLS does not currently support sending the CA chain to the client */
925 mbedtls_ssl_conf_ca_chain(ks_ssl->ssl_config, ssl_ctx->ca_chain, ssl_ctx->crl);
926
927 /* Initialize minimum TLS version */
928 {
929 const int configured_tls_version_min =
931
932 /* default to TLS 1.2 */
933 mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_TLS1_2;
934
935 if (configured_tls_version_min > TLS_VER_UNSPEC)
936 {
937 version = tls_version_to_ssl_version(configured_tls_version_min);
938 }
939
940 mbedtls_ssl_conf_min_tls_version(ks_ssl->ssl_config, version);
941 }
942
943 /* Initialize maximum TLS version */
944 {
945 const int configured_tls_version_max =
947
948 mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_UNKNOWN;
949
950 if (configured_tls_version_max > TLS_VER_UNSPEC)
951 {
952 version = tls_version_to_ssl_version(configured_tls_version_max);
953 }
954 else
955 {
956 /* Default to tls_version_max(). */
957 version = tls_version_to_ssl_version(tls_version_max());
958 }
959
960 mbedtls_ssl_conf_max_tls_version(ks_ssl->ssl_config, version);
961 }
962
963 /* Initialise SSL context */
964 ALLOC_OBJ_CLEAR(ks_ssl->ctx, mbedtls_ssl_context);
965 mbedtls_ssl_init(ks_ssl->ctx);
966 (void)mbed_ok(mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config));
967 /* We do verification in our own callback depending on the
968 * exact configuration. We do not rely on the default hostname
969 * verification. */
970 ASSERT(mbed_ok(mbedtls_ssl_set_hostname(ks_ssl->ctx, NULL)));
971
972 /* Initialise BIOs */
974 mbedtls_ssl_set_bio(ks_ssl->ctx, ks_ssl->bio_ctx, ssl_bio_write, ssl_bio_read, NULL);
975}
976
977
978void
980{
981 mbedtls_ssl_send_alert_message(ks_ssl->ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
982 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY);
983}
984
985void
986key_state_ssl_free(struct key_state_ssl *ks_ssl)
987{
988 if (ks_ssl)
989 {
990 if (ks_ssl->ctx)
991 {
992 mbedtls_ssl_free(ks_ssl->ctx);
993 free(ks_ssl->ctx);
994 }
995 if (ks_ssl->ssl_config)
996 {
997 mbedtls_ssl_config_free(ks_ssl->ssl_config);
998 free(ks_ssl->ssl_config);
999 }
1000 if (ks_ssl->bio_ctx)
1001 {
1002 buf_free_entries(&ks_ssl->bio_ctx->in);
1003 buf_free_entries(&ks_ssl->bio_ctx->out);
1004 free(ks_ssl->bio_ctx);
1005 }
1006 CLEAR(*ks_ssl);
1007 }
1008}
1009
1010int
1011key_state_write_plaintext(struct key_state_ssl *ks, struct buffer *buf)
1012{
1013 int retval = 0;
1014
1015 ASSERT(buf);
1016
1017 retval = key_state_write_plaintext_const(ks, BPTR(buf), BLEN(buf));
1018
1019 if (1 == retval)
1020 {
1021 memset(BPTR(buf), 0, BLEN(buf)); /* erase data just written */
1022 buf->len = 0;
1023 }
1024
1025 return retval;
1026}
1027
1028int
1029key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, int len)
1030{
1031 int retval = 0;
1032
1033 ASSERT(NULL != ks);
1034 ASSERT(len >= 0);
1035
1036 if (0 == len)
1037 {
1038 return 0;
1039 }
1040
1041 ASSERT(data);
1042
1043 retval = mbedtls_ssl_write(ks->ctx, data, len);
1044
1045 if (retval < 0)
1046 {
1047 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1048 {
1049 return 0;
1050 }
1051 mbed_log_err(D_TLS_ERRORS, retval, "TLS ERROR: write tls_write_plaintext_const error");
1052 return -1;
1053 }
1054
1055 if (retval != len)
1056 {
1057 msg(D_TLS_ERRORS, "TLS ERROR: write tls_write_plaintext_const incomplete %d/%d", retval,
1058 len);
1059 return -1;
1060 }
1061
1062 /* successful write */
1063 dmsg(D_HANDSHAKE_VERBOSE, "write tls_write_plaintext_const %d bytes", retval);
1064
1065 return 1;
1066}
1067
1068int
1069key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf)
1070{
1071 int retval = 0;
1072 int len = 0;
1073
1074 ASSERT(NULL != ks);
1075 ASSERT(buf);
1076 ASSERT(buf->len >= 0);
1077
1078 if (buf->len)
1079 {
1080 return 0;
1081 }
1082
1083 len = buf_forward_capacity(buf);
1084
1085 retval = endless_buf_read(&ks->bio_ctx->out, BPTR(buf), len);
1086
1087 /* Error during read, check for retry error */
1088 if (retval < 0)
1089 {
1090 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1091 {
1092 return 0;
1093 }
1094 mbed_log_err(D_TLS_ERRORS, retval, "TLS_ERROR: read tls_read_ciphertext error");
1095 buf->len = 0;
1096 return -1;
1097 }
1098 /* Nothing read, try again */
1099 if (0 == retval)
1100 {
1101 buf->len = 0;
1102 return 0;
1103 }
1104
1105 /* successful read */
1106 dmsg(D_HANDSHAKE_VERBOSE, "read tls_read_ciphertext %d bytes", retval);
1107 buf->len = retval;
1108 return 1;
1109}
1110
1111int
1112key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf)
1113{
1114 int retval = 0;
1115
1116 ASSERT(NULL != ks);
1117 ASSERT(buf);
1118 ASSERT(buf->len >= 0);
1119
1120 if (0 == buf->len)
1121 {
1122 return 0;
1123 }
1124
1125 retval = endless_buf_write(&ks->bio_ctx->in, BPTR(buf), buf->len);
1126
1127 if (retval < 0)
1128 {
1129 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1130 {
1131 return 0;
1132 }
1133 mbed_log_err(D_TLS_ERRORS, retval, "TLS ERROR: write tls_write_ciphertext error");
1134 return -1;
1135 }
1136
1137 if (retval != buf->len)
1138 {
1139 msg(D_TLS_ERRORS, "TLS ERROR: write tls_write_ciphertext incomplete %d/%d", retval,
1140 buf->len);
1141 return -1;
1142 }
1143
1144 /* successful write */
1145 dmsg(D_HANDSHAKE_VERBOSE, "write tls_write_ciphertext %d bytes", retval);
1146
1147 memset(BPTR(buf), 0, BLEN(buf)); /* erase data just written */
1148 buf->len = 0;
1149
1150 return 1;
1151}
1152
1153int
1154key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf)
1155{
1156 int retval = 0;
1157 int len = 0;
1158
1159 ASSERT(NULL != ks);
1160 ASSERT(buf);
1161 ASSERT(buf->len >= 0);
1162
1163 if (buf->len)
1164 {
1165 return 0;
1166 }
1167
1168 len = buf_forward_capacity(buf);
1169
1170 retval = mbedtls_ssl_read(ks->ctx, BPTR(buf), len);
1171
1172 /* Error during read, check for retry error */
1173 if (retval < 0)
1174 {
1175 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval
1176 || MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET == retval)
1177 {
1178 return 0;
1179 }
1180 mbed_log_err(D_TLS_ERRORS, retval, "TLS_ERROR: read tls_read_plaintext error");
1181 buf->len = 0;
1182 return -1;
1183 }
1184 /* Nothing read, try again */
1185 if (0 == retval)
1186 {
1187 buf->len = 0;
1188 return 0;
1189 }
1190
1191 /* successful read */
1192 dmsg(D_HANDSHAKE_VERBOSE, "read tls_read_plaintext %d bytes", retval);
1193 buf->len = retval;
1194
1195 return 1;
1196}
1197
1198/* **************************************
1199 *
1200 * Information functions
1201 *
1202 * Print information for the end user.
1203 *
1204 ***************************************/
1205void
1206print_details(struct key_state_ssl *ks_ssl, const char *prefix)
1207{
1208 const mbedtls_x509_crt *cert;
1209 char s1[256];
1210 char s2[256];
1211
1212 s1[0] = s2[0] = 0;
1213 snprintf(s1, sizeof(s1), "%s %s, cipher %s", prefix, mbedtls_ssl_get_version(ks_ssl->ctx),
1214 mbedtls_ssl_get_ciphersuite(ks_ssl->ctx));
1215
1216 cert = mbedtls_ssl_get_peer_cert(ks_ssl->ctx);
1217 if (cert != NULL)
1218 {
1219 snprintf(s2, sizeof(s2), ", %u bit key", (unsigned int)mbedtls_pk_get_bitlen(&cert->pk));
1220 }
1221
1222 msg(D_HANDSHAKE, "%s%s", s1, s2);
1223}
1224
1225void
1226show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
1227{
1228 if (tls13)
1229 {
1230 /* mbed TLS has no TLS 1.3 support currently */
1231 return;
1232 }
1233 struct tls_root_ctx tls_ctx;
1234 const int *ciphers = mbedtls_ssl_list_ciphersuites();
1235
1236 tls_ctx_new(&tls_ctx);
1237 tls_ctx_set_cert_profile(&tls_ctx, tls_cert_profile);
1238 tls_ctx_restrict_ciphers(&tls_ctx, cipher_list);
1239
1240 if (tls_ctx.allowed_ciphers)
1241 {
1242 ciphers = tls_ctx.allowed_ciphers;
1243 }
1244
1245 while (*ciphers != 0)
1246 {
1247 printf("%s\n", mbedtls_ssl_get_ciphersuite_name(*ciphers));
1248 ciphers++;
1249 }
1250 tls_ctx_free(&tls_ctx);
1251}
1252
1253void
1255{
1256 printf("Available elliptic curves:\n\n");
1257 for (size_t i = 0; i < ecp_curve_info_table_items; i++)
1258 {
1259 printf("%s\n", ecp_curve_info_table[i].name);
1260 }
1261}
1262
1263const char *
1265{
1266 static char mbedtls_version[30];
1267 unsigned int pv = mbedtls_version_get_number();
1268 snprintf(mbedtls_version, sizeof(mbedtls_version), "mbed TLS %u.%u.%u", (pv >> 24) & 0xff,
1269 (pv >> 16) & 0xff, (pv >> 8) & 0xff);
1270 return mbedtls_version;
1271}
1272
1273void
1275{
1276 return; /* no external key provider in mbedTLS build */
1277}
1278
1279#endif /* defined(ENABLE_CRYPTO_MBEDTLS) */
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:601
Buffer management functions and garbage collection.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Allocate and zero-initialise an array of n elements of the given type.
Definition buffer.h:2068
static int buf_forward_capacity(const struct buffer *buf)
Return the number of bytes that can still be appended to the buffer.
Definition buffer.h:1038
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1965
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2027
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1949
char * strsep(char **stringp, const char *delim)
char * strtok_r(char *s, const char *delim, char **last)
const char * print_key_filename(const char *str, bool is_inline)
To be used when printing a string that may contain inline data.
Definition crypto.c:1243
#define mbed_ok(errval)
Check errval and log on error.
bool mbed_log_err(unsigned int flags, int errval, const char *prefix)
Log the supplied mbed TLS error, prefixed by supplied prefix.
#define D_TLS_DEBUG_MED
Definition errlevel.h:156
#define D_HANDSHAKE_VERBOSE
Definition errlevel.h:155
#define D_HANDSHAKE
Definition errlevel.h:71
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_TLS_DEBUG
Definition errlevel.h:164
#define KS_PRIMARY
Primary key state index.
Definition ssl_common.h:472
int key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract plaintext data from the TLS module.
int key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a ciphertext buffer into the TLS module.
int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract ciphertext data from the TLS module.
int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *data, int len)
Insert plaintext data into the TLS module.
int key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a plaintext buffer into the TLS module.
int verify_callback(void *session_obj, mbedtls_x509_crt *cert, int cert_depth, uint32_t *flags)
Verify that the remote OpenVPN peer's certificate allows setting up a VPN tunnel.
static SERVICE_STATUS status
Definition interactive.c:52
void management_auth_failure(const struct management *man, const char *type, const char *reason)
Definition manage.c:3274
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition manage.c:3966
#define CLEAR(x)
Definition basic.h:32
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
PKCS #11 SSL library-specific backend.
int openvpn_base64_decode(const char *str, void *data, int size)
Definition base64.c:160
int openvpn_base64_encode(const void *data, int size, char **str)
Definition base64.c:51
static struct user_pass passbuf
Definition ssl.c:260
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user's password.
Definition ssl.c:274
void load_xkey_provider(void)
Load ovpn.xkey provider used for external key signing.
Control Channel SSL library backend module.
void tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
Set the (elliptic curve) group allowed for signatures and key exchange.
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
const char * get_ssl_library_version(void)
return a pointer to a static memory area containing the name and version number of the SSL library in...
bool key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size, void *ekm, size_t ekm_size)
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
void show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
Show the TLS ciphers that are available for us to use in the library depending on the TLS version.
void show_available_curves(void)
Show the available elliptic curves in the crypto library.
void key_state_ssl_free(struct key_state_ssl *ks_ssl)
Free the SSL channel part of the given key state.
#define TLS_VER_1_2
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline)
Load private key file into the given TLS context.
void key_state_ssl_shutdown(struct key_state_ssl *ks_ssl)
Sets a TLS session to be shutdown state, so the TLS library will generate a shutdown alert.
void tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file, bool extra_certs_file_inline)
Load extra certificate authority certificates from the given file or path.
void tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
Check our certificate notBefore and notAfter fields, and warn if the cert is either not yet valid or ...
void tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.3 and higher.
int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline, bool load_ca_file)
Load PKCS #12 file for key, cert and (optionally) CA certs, and add to library-specific TLS context.
void key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server, struct tls_session *session)
Initialise the SSL channel part of the given key state.
void tls_free_lib(void)
Free any global SSL library-specific data structures.
Definition ssl_openssl.c:98
void tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
Load Elliptic Curve Parameters, and load them into the library-specific TLS context.
#define TLS_VER_1_3
#define TLS_VER_UNSPEC
void tls_init_lib(void)
Perform any static initialisation necessary by the library.
Definition ssl_openssl.c:91
void print_details(struct key_state_ssl *ks_ssl, const char *prefix)
Print a one line summary of SSL/TLS session handshake.
int tls_version_max(void)
Return the maximum TLS version (as a TLS_VER_x constant) supported by current SSL implementation.
void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_inline)
Reload the Certificate Revocation List for the SSL channel.
void tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.2 and below.
void tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_file_inline, const char *ca_path, bool tls_server)
Load certificate authority certificates from the given file or path.
void tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
Set the TLS certificate profile.
int tls_ctx_use_management_external_key(struct tls_root_ctx *ctx)
Tell the management interface to load the given certificate and the external private key matching the...
void tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
Use Windows cryptoapi for key and cert, and add to library-specific TLS context.
bool tls_ctx_initialised(const struct tls_root_ctx *ctx)
Checks whether the given TLS context is initialised.
bool tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
Set any library specific options.
void tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_file_inline)
Load Diffie Hellman Parameters, and load them into the library-specific TLS context.
void tls_ctx_new(struct tls_root_ctx *ctx)
Initialise a library-specific TLS context.
void tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
Load certificate file into the given TLS context.
Control Channel Common Data Structures.
#define SSLF_TLS_VERSION_MAX_SHIFT
Definition ssl_common.h:436
#define UP_TYPE_PRIVATE_KEY
Definition ssl_common.h:42
#define SSLF_CLIENT_CERT_OPTIONAL
Definition ssl_common.h:429
#define SSLF_CLIENT_CERT_NOT_REQUIRED
Definition ssl_common.h:428
#define SSLF_TLS_DEBUG_ENABLED
Definition ssl_common.h:438
#define SSLF_TLS_VERSION_MAX_MASK
Definition ssl_common.h:437
#define SSLF_TLS_VERSION_MIN_SHIFT
Definition ssl_common.h:434
#define SSLF_TLS_VERSION_MIN_MASK
Definition ssl_common.h:435
int tls_ctx_use_external_signing_func(struct tls_root_ctx *ctx, external_sign_func sign_func, void *sign_ctx)
Call the supplied signing function to create a TLS signature during the TLS handshake.
bool(* external_sign_func)(void *sign_ctx, const void *src, size_t src_size, void *dst, size_t dst_size)
External signing function prototype.
Definition ssl_mbedtls.h:76
int get_num_elements(const char *string, char delimiter)
Returns the occurrences of 'delimiter' in a string +1 This is typically used to find out the number e...
Definition ssl_util.c:355
const tls_cipher_name_pair * tls_get_cipher_name_pair(const char *cipher_name, size_t len)
Definition ssl_util.c:336
SSL utility functions.
Control Channel Verification Module mbed TLS backend.
size_t length
Definition ssl_mbedtls.h:45
uint8_t * data
Definition ssl_mbedtls.h:46
buffer_entry * next_block
Definition ssl_mbedtls.h:47
endless_buffer out
Definition ssl_mbedtls.h:60
endless_buffer in
Definition ssl_mbedtls.h:59
One node in a buffer_list linked list.
Definition buffer.h:2152
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
size_t data_start
Definition ssl_mbedtls.h:52
buffer_entry * first_block
Definition ssl_mbedtls.h:53
buffer_entry * last_block
Definition ssl_mbedtls.h:54
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
bio_ctx * bio_ctx
mbedtls_ssl_config * ssl_config
mbedTLS global ssl config
mbedtls_ssl_context * ctx
mbedTLS connection context
Get a tls_cipher_name_pair containing OpenSSL and IANA names for supplied TLS cipher name.
Definition ssl_util.h:111
const char * iana_name
Definition ssl_util.h:113
const char * openssl_name
Definition ssl_util.h:112
Structure that wraps the TLS context.
Definition ssl_mbedtls.h:86
mbedtls_x509_crl * crl
Certificate Revocation List.
Definition ssl_mbedtls.h:92
mbedtls_x509_crt * crt_chain
Local Certificate chain.
Definition ssl_mbedtls.h:89
uint16_t * groups
List of allowed groups for this connection.
Definition ssl_mbedtls.h:99
mbedtls_x509_crt * ca_chain
CA chain for remote verification.
Definition ssl_mbedtls.h:90
int * allowed_ciphers
List of allowed ciphers for this connection.
Definition ssl_mbedtls.h:98
mbedtls_x509_crt_profile cert_profile
Allowed certificate types.
bool initialised
True if the context has been initialised.
Definition ssl_mbedtls.h:87
mbedtls_pk_context * priv_key
Local private key.
Definition ssl_mbedtls.h:91
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:497
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:138