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
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 size_t 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 size_t 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));
485 }
486 }
487 else
488 {
489 status = mbedtls_pk_parse_keyfile(ctx->priv_key, priv_key_file, NULL);
490 if (MBEDTLS_ERR_PK_PASSWORD_REQUIRED == status)
491 {
492 char passbuf[512] = { 0 };
493 pem_password_callback(passbuf, 512, 0, NULL);
494 status = mbedtls_pk_parse_keyfile(ctx->priv_key, priv_key_file, passbuf);
496 }
497 }
498 if (!mbed_ok(status))
499 {
500#ifdef ENABLE_MANAGEMENT
501 if (management && (MBEDTLS_ERR_PK_PASSWORD_MISMATCH == status))
502 {
504 }
505#endif
506 msg(M_WARN, "Cannot load private key file %s",
507 print_key_filename(priv_key_file, priv_key_inline));
508 return 1;
509 }
510
511 /* work around bug in mbedtls 4.1.0 by adding missing public key information in prv
512 * cf. https://github.com/Mbed-TLS/TF-PSA-Crypto/issues/807 */
513#if MBEDTLS_VERSION_NUMBER >= 0x04010000
514 if (ctx->priv_key->MBEDTLS_PRIVATE(pub_raw_len) == 0)
515 {
516 ASSERT(mbed_ok(psa_export_public_key(ctx->priv_key->MBEDTLS_PRIVATE(priv_id),
517 ctx->priv_key->MBEDTLS_PRIVATE(pub_raw),
518 sizeof(ctx->priv_key->MBEDTLS_PRIVATE(pub_raw)),
519 &ctx->priv_key->MBEDTLS_PRIVATE(pub_raw_len))));
520 }
521#endif
522 if (!mbed_ok(mbedtls_pk_check_pair(&ctx->crt_chain->pk, ctx->priv_key)))
523 {
524 msg(M_WARN, "Private key does not match the certificate");
525 return 1;
526 }
527
528 return 0;
529}
530
531int
533 void *sign_ctx)
534{
535 msg(M_WARN, "tls_ctx_use_external_signing_func is not implemented for Mbed TLS 4.");
536 return 1;
537}
538
539#ifdef ENABLE_MANAGEMENT
541static bool
542management_sign_func(void *sign_ctx, const void *src, size_t src_len, void *dst, size_t dst_len)
543{
544 bool ret = false;
545 char *src_b64 = NULL;
546 char *dst_b64 = NULL;
547
548 if (!management || (openvpn_base64_encode(src, (int)src_len, &src_b64) <= 0))
549 {
550 goto cleanup;
551 }
552
553 /*
554 * We only support RSA external keys and PKCS1 signatures at the moment
555 * in mbed TLS, so the signature parameter is hardcoded to this encoding
556 */
557 if (!(dst_b64 = management_query_pk_sig(management, src_b64, "RSA_PKCS1_PADDING")))
558 {
559 goto cleanup;
560 }
561
562 if (openvpn_base64_decode(dst_b64, dst, (int)dst_len) != (int)dst_len)
563 {
564 goto cleanup;
565 }
566
567 ret = true;
568cleanup:
569 free(src_b64);
570 free(dst_b64);
571
572 return ret;
573}
574
575int
577{
578 return tls_ctx_use_external_signing_func(ctx, management_sign_func, NULL);
579}
580
581#endif /* ifdef ENABLE_MANAGEMENT */
582
583void
584tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_inline, const char *ca_path,
585 bool tls_server)
586{
587 if (ca_path)
588 {
589 msg(M_FATAL, "ERROR: mbed TLS cannot handle the capath directive");
590 }
591
592 if (ca_file && ca_inline)
593 {
594 if (!mbed_ok(mbedtls_x509_crt_parse(ctx->ca_chain, (const unsigned char *)ca_file,
595 strlen(ca_file) + 1)))
596 {
597 msg(M_FATAL, "Cannot load inline CA certificates");
598 }
599 }
600 else
601 {
602 /* Load CA file for verifying peer supplied certificate */
603 if (!mbed_ok(mbedtls_x509_crt_parse_file(ctx->ca_chain, ca_file)))
604 {
605 msg(M_FATAL, "Cannot load CA certificate file %s", ca_file);
606 }
607 }
608}
609
610void
611tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file,
612 bool extra_certs_inline)
613{
614 ASSERT(NULL != ctx);
615
616 if (!ctx->crt_chain)
617 {
618 ALLOC_OBJ_CLEAR(ctx->crt_chain, mbedtls_x509_crt);
619 }
620
621 if (extra_certs_inline)
622 {
623 if (!mbed_ok(mbedtls_x509_crt_parse(ctx->crt_chain, (const unsigned char *)extra_certs_file,
624 strlen(extra_certs_file) + 1)))
625 {
626 msg(M_FATAL, "Cannot load inline extra-certs file");
627 }
628 }
629 else
630 {
631 if (!mbed_ok(mbedtls_x509_crt_parse_file(ctx->crt_chain, extra_certs_file)))
632 {
633 msg(M_FATAL, "Cannot load extra-certs file: %s", extra_certs_file);
634 }
635 }
636}
637
638/* **************************************
639 *
640 * Key-state specific functions
641 *
642 ***************************************/
643
644/*
645 * "Endless buffer"
646 */
647
648static inline void
649buf_free_entry(buffer_entry *entry)
650{
651 if (NULL != entry)
652 {
653 free(entry->data);
654 free(entry);
655 }
656}
657
658static void
659buf_free_entries(endless_buffer *buf)
660{
661 while (buf->first_block)
662 {
663 buffer_entry *cur_block = buf->first_block;
664 buf->first_block = cur_block->next_block;
665 buf_free_entry(cur_block);
666 }
667 buf->last_block = NULL;
668}
669
670static int
671endless_buf_read(endless_buffer *in, unsigned char *out, size_t out_len)
672{
673 size_t read_len = 0;
674
675 ASSERT(out_len <= INT_MAX);
676
677 if (in->first_block == NULL)
678 {
679 return MBEDTLS_ERR_SSL_WANT_READ;
680 }
681
682 while (in->first_block != NULL && read_len < out_len)
683 {
684 size_t block_len = in->first_block->length - in->data_start;
685 if (block_len <= out_len - read_len)
686 {
687 buffer_entry *cur_entry = in->first_block;
688 memcpy(out + read_len, cur_entry->data + in->data_start, block_len);
689
690 read_len += block_len;
691
692 in->first_block = cur_entry->next_block;
693 in->data_start = 0;
694
695 if (in->first_block == NULL)
696 {
697 in->last_block = NULL;
698 }
699
700 buf_free_entry(cur_entry);
701 }
702 else
703 {
704 memcpy(out + read_len, in->first_block->data + in->data_start, out_len - read_len);
705 in->data_start += out_len - read_len;
706 read_len = out_len;
707 }
708 }
709
710 return (int)read_len;
711}
712
713static int
714endless_buf_write(endless_buffer *out, const unsigned char *in, size_t len)
715{
716 buffer_entry *new_block = malloc(sizeof(buffer_entry));
717 if (NULL == new_block)
718 {
719 return MBEDTLS_ERR_NET_SEND_FAILED;
720 }
721
722 new_block->data = malloc(len);
723 if (NULL == new_block->data)
724 {
725 free(new_block);
726 return MBEDTLS_ERR_NET_SEND_FAILED;
727 }
728
729 ASSERT(len <= INT_MAX);
730
731 new_block->length = len;
732 new_block->next_block = NULL;
733
734 memcpy(new_block->data, in, len);
735
736 if (NULL == out->first_block)
737 {
738 out->first_block = new_block;
739 }
740
741 if (NULL != out->last_block)
742 {
743 out->last_block->next_block = new_block;
744 }
745
746 out->last_block = new_block;
747
748 return (int)len;
749}
750
751static int
752ssl_bio_read(void *ctx, unsigned char *out, size_t out_len)
753{
754 bio_ctx *my_ctx = (bio_ctx *)ctx;
755 return endless_buf_read(&my_ctx->in, out, out_len);
756}
757
758static int
759ssl_bio_write(void *ctx, const unsigned char *in, size_t in_len)
760{
761 bio_ctx *my_ctx = (bio_ctx *)ctx;
762 return endless_buf_write(&my_ctx->out, in, in_len);
763}
764
765static void
766my_debug(void *ctx, int level, const char *file, int line, const char *str)
767{
768 msglvl_t my_loglevel = (level < 3) ? D_TLS_DEBUG_MED : D_TLS_DEBUG;
769 msg(my_loglevel, "mbed TLS msg (%s:%d): %s", file, line, str);
770}
771
772int
773tls_version_max(void)
774{
775#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
776 return TLS_VER_1_3;
777#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
778 return TLS_VER_1_2;
779#else
780#error mbedtls is compiled without support for TLS 1.2 or 1.3
781#endif
782}
783
791mbedtls_ssl_protocol_version
792tls_version_to_ssl_version(int tls_ver)
793{
794 switch (tls_ver)
795 {
796#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
797 case TLS_VER_1_2:
798 return MBEDTLS_SSL_VERSION_TLS1_2;
799#endif
800
801#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
802 case TLS_VER_1_3:
803 return MBEDTLS_SSL_VERSION_TLS1_3;
804#endif
805
806 default:
807 msg(M_FATAL, "%s: invalid or unsupported TLS version %d", __func__, tls_ver);
808 return MBEDTLS_SSL_VERSION_UNKNOWN;
809 }
810}
811
812void
813backend_tls_ctx_reload_crl(struct tls_root_ctx *ctx, const char *crl_file, bool crl_inline)
814{
815 ASSERT(crl_file);
816
817 if (ctx->crl == NULL)
818 {
819 ALLOC_OBJ_CLEAR(ctx->crl, mbedtls_x509_crl);
820 }
821 mbedtls_x509_crl_free(ctx->crl);
822
823 if (crl_inline)
824 {
825 if (!mbed_ok(mbedtls_x509_crl_parse(ctx->crl, (const unsigned char *)crl_file,
826 strlen(crl_file) + 1)))
827 {
828 msg(M_WARN, "CRL: cannot parse inline CRL");
829 goto err;
830 }
831 }
832 else
833 {
834 if (!mbed_ok(mbedtls_x509_crl_parse_file(ctx->crl, crl_file)))
835 {
836 msg(M_WARN, "CRL: cannot read CRL from file %s", crl_file);
837 goto err;
838 }
839 }
840 return;
841
842err:
843 mbedtls_x509_crl_free(ctx->crl);
844}
845
846void
847key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server,
848 struct tls_session *session)
849{
850 ASSERT(NULL != ssl_ctx);
851 ASSERT(ks_ssl);
852 CLEAR(*ks_ssl);
853
854 /* Initialise SSL config */
855 ALLOC_OBJ_CLEAR(ks_ssl->ssl_config, mbedtls_ssl_config);
856 mbedtls_ssl_config_init(ks_ssl->ssl_config);
857 int endpoint = is_server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
858 mbedtls_ssl_config_defaults(ks_ssl->ssl_config, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM,
859 MBEDTLS_SSL_PRESET_DEFAULT);
860#ifdef MBEDTLS_DEBUG_C
861 /* We only want to have mbed TLS generate debug level logging when we would
862 * also display it.
863 * In fact mbed TLS 2.25.0 crashes generating debug log if Curve25591 is
864 * selected for DH (https://github.com/ARMmbed/mbedtls/issues/4208) */
865 if (session->opt->ssl_flags & SSLF_TLS_DEBUG_ENABLED)
866 {
867 mbedtls_debug_set_threshold(3);
868 }
869 else
870 {
871 mbedtls_debug_set_threshold(2);
872 }
873#endif
874 mbedtls_ssl_conf_dbg(ks_ssl->ssl_config, my_debug, NULL);
875
876 mbedtls_ssl_conf_cert_profile(ks_ssl->ssl_config, &ssl_ctx->cert_profile);
877
878 if (ssl_ctx->allowed_ciphers)
879 {
880 mbedtls_ssl_conf_ciphersuites(ks_ssl->ssl_config, ssl_ctx->allowed_ciphers);
881 }
882
883 if (ssl_ctx->groups)
884 {
885 mbedtls_ssl_conf_groups(ks_ssl->ssl_config, ssl_ctx->groups);
886 }
887
888 /* Disable TLS renegotiations if the mbedtls library supports that feature.
889 * OpenVPN's renegotiation creates new SSL sessions and does not depend on
890 * this feature and TLS renegotiations have been problematic in the past. */
891#if defined(MBEDTLS_SSL_RENEGOTIATION)
892 mbedtls_ssl_conf_renegotiation(ks_ssl->ssl_config, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
893#endif /* MBEDTLS_SSL_RENEGOTIATION */
894
895 /* Disable record splitting (for now). OpenVPN assumes records are sent
896 * unfragmented, and changing that will require thorough review and
897 * testing. Since OpenVPN is not susceptible to BEAST, we can just
898 * disable record splitting as a quick fix. */
899#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
900 mbedtls_ssl_conf_cbc_record_splitting(ks_ssl->ssl_config,
901 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
902#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
903
904 /* Initialise authentication information */
905 (void)mbed_ok(mbedtls_ssl_conf_own_cert(ks_ssl->ssl_config, ssl_ctx->crt_chain, ssl_ctx->priv_key));
906
907 /* Initialise SSL verification */
908 if (session->opt->ssl_flags & SSLF_CLIENT_CERT_OPTIONAL)
909 {
910 mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);
911 }
912 else if (!(session->opt->ssl_flags & SSLF_CLIENT_CERT_NOT_REQUIRED))
913 {
914 mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);
915 }
916 mbedtls_ssl_conf_verify(ks_ssl->ssl_config, verify_callback, session);
917
918 /* TODO: mbed TLS does not currently support sending the CA chain to the client */
919 mbedtls_ssl_conf_ca_chain(ks_ssl->ssl_config, ssl_ctx->ca_chain, ssl_ctx->crl);
920
921 /* Initialize minimum TLS version */
922 {
923 const int configured_tls_version_min =
925
926 /* default to TLS 1.2 */
927 mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_TLS1_2;
928
929 if (configured_tls_version_min > TLS_VER_UNSPEC)
930 {
931 version = tls_version_to_ssl_version(configured_tls_version_min);
932 }
933
934 mbedtls_ssl_conf_min_tls_version(ks_ssl->ssl_config, version);
935 }
936
937 /* Initialize maximum TLS version */
938 {
939 const int configured_tls_version_max =
941
942 mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_UNKNOWN;
943
944 if (configured_tls_version_max > TLS_VER_UNSPEC)
945 {
946 version = tls_version_to_ssl_version(configured_tls_version_max);
947 }
948 else
949 {
950 /* Default to tls_version_max(). */
951 version = tls_version_to_ssl_version(tls_version_max());
952 }
953
954 mbedtls_ssl_conf_max_tls_version(ks_ssl->ssl_config, version);
955 }
956
957 /* Initialise SSL context */
958 ALLOC_OBJ_CLEAR(ks_ssl->ctx, mbedtls_ssl_context);
959 mbedtls_ssl_init(ks_ssl->ctx);
960 (void)mbed_ok(mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config));
961 /* We do verification in our own callback depending on the
962 * exact configuration. We do not rely on the default hostname
963 * verification. */
964 ASSERT(mbed_ok(mbedtls_ssl_set_hostname(ks_ssl->ctx, NULL)));
965
966 /* Initialise BIOs */
968 mbedtls_ssl_set_bio(ks_ssl->ctx, ks_ssl->bio_ctx, ssl_bio_write, ssl_bio_read, NULL);
969}
970
971
972void
974{
975 mbedtls_ssl_send_alert_message(ks_ssl->ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
976 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY);
977}
978
979void
980key_state_ssl_free(struct key_state_ssl *ks_ssl)
981{
982 if (ks_ssl)
983 {
984 if (ks_ssl->ctx)
985 {
986 mbedtls_ssl_free(ks_ssl->ctx);
987 free(ks_ssl->ctx);
988 }
989 if (ks_ssl->ssl_config)
990 {
991 mbedtls_ssl_config_free(ks_ssl->ssl_config);
992 free(ks_ssl->ssl_config);
993 }
994 if (ks_ssl->bio_ctx)
995 {
996 buf_free_entries(&ks_ssl->bio_ctx->in);
997 buf_free_entries(&ks_ssl->bio_ctx->out);
998 free(ks_ssl->bio_ctx);
999 }
1000 CLEAR(*ks_ssl);
1001 }
1002}
1003
1004int
1005key_state_write_plaintext(struct key_state_ssl *ks, struct buffer *buf)
1006{
1007 int retval = 0;
1008
1009 ASSERT(buf);
1010
1011 retval = key_state_write_plaintext_const(ks, BPTR(buf), BLEN(buf));
1012
1013 if (1 == retval)
1014 {
1015 memset(BPTR(buf), 0, BLENZ(buf)); /* erase data just written */
1016 buf->len = 0;
1017 }
1018
1019 return retval;
1020}
1021
1022int
1023key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, int len)
1024{
1025 int retval = 0;
1026
1027 ASSERT(NULL != ks);
1028 ASSERT(len >= 0);
1029
1030 if (0 == len)
1031 {
1032 return 0;
1033 }
1034
1035 ASSERT(data);
1036
1037 retval = mbedtls_ssl_write(ks->ctx, data, (size_t)len);
1038
1039 if (retval < 0)
1040 {
1041 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1042 {
1043 return 0;
1044 }
1045 mbed_log_err(D_TLS_ERRORS, retval, "TLS ERROR: write tls_write_plaintext_const error");
1046 return -1;
1047 }
1048
1049 if (retval != len)
1050 {
1051 msg(D_TLS_ERRORS, "TLS ERROR: write tls_write_plaintext_const incomplete %d/%d", retval,
1052 len);
1053 return -1;
1054 }
1055
1056 /* successful write */
1057 dmsg(D_HANDSHAKE_VERBOSE, "write tls_write_plaintext_const %d bytes", retval);
1058
1059 return 1;
1060}
1061
1062int
1063key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf)
1064{
1065 int retval = 0;
1066
1067 ASSERT(NULL != ks);
1068 ASSERT(buf);
1069 ASSERT(buf->len >= 0);
1070
1071 if (buf->len)
1072 {
1073 return 0;
1074 }
1075
1076 retval = endless_buf_read(&ks->bio_ctx->out, BPTR(buf), BCAP(buf));
1077
1078 /* Error during read, check for retry error */
1079 if (retval < 0)
1080 {
1081 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1082 {
1083 return 0;
1084 }
1085 mbed_log_err(D_TLS_ERRORS, retval, "TLS_ERROR: read tls_read_ciphertext error");
1086 buf->len = 0;
1087 return -1;
1088 }
1089 /* Nothing read, try again */
1090 if (0 == retval)
1091 {
1092 buf->len = 0;
1093 return 0;
1094 }
1095
1096 /* successful read */
1097 dmsg(D_HANDSHAKE_VERBOSE, "read tls_read_ciphertext %d bytes", retval);
1098 buf->len = retval;
1099 return 1;
1100}
1101
1102int
1103key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf)
1104{
1105 int retval = 0;
1106
1107 ASSERT(NULL != ks);
1108 ASSERT(buf);
1109 ASSERT(buf->len >= 0);
1110
1111 if (0 == buf->len)
1112 {
1113 return 0;
1114 }
1115
1116 retval = endless_buf_write(&ks->bio_ctx->in, BPTR(buf), BLENZ(buf));
1117
1118 if (retval < 0)
1119 {
1120 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1121 {
1122 return 0;
1123 }
1124 mbed_log_err(D_TLS_ERRORS, retval, "TLS ERROR: write tls_write_ciphertext error");
1125 return -1;
1126 }
1127
1128 if (retval != buf->len)
1129 {
1130 msg(D_TLS_ERRORS, "TLS ERROR: write tls_write_ciphertext incomplete %d/%d", retval,
1131 buf->len);
1132 return -1;
1133 }
1134
1135 /* successful write */
1136 dmsg(D_HANDSHAKE_VERBOSE, "write tls_write_ciphertext %d bytes", retval);
1137
1138 memset(BPTR(buf), 0, BLENZ(buf)); /* erase data just written */
1139 buf->len = 0;
1140
1141 return 1;
1142}
1143
1144int
1145key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf)
1146{
1147 int retval = 0;
1148
1149 ASSERT(NULL != ks);
1150 ASSERT(buf);
1151 ASSERT(buf->len >= 0);
1152
1153 if (buf->len)
1154 {
1155 return 0;
1156 }
1157
1158 retval = mbedtls_ssl_read(ks->ctx, BPTR(buf), BCAP(buf));
1159
1160 /* Error during read, check for retry error */
1161 if (retval < 0)
1162 {
1163 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval
1164 || MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET == retval)
1165 {
1166 return 0;
1167 }
1168 mbed_log_err(D_TLS_ERRORS, retval, "TLS_ERROR: read tls_read_plaintext error");
1169 buf->len = 0;
1170 return -1;
1171 }
1172 /* Nothing read, try again */
1173 if (0 == retval)
1174 {
1175 buf->len = 0;
1176 return 0;
1177 }
1178
1179 /* successful read */
1180 dmsg(D_HANDSHAKE_VERBOSE, "read tls_read_plaintext %d bytes", retval);
1181 buf->len = retval;
1182
1183 return 1;
1184}
1185
1186/* **************************************
1187 *
1188 * Information functions
1189 *
1190 * Print information for the end user.
1191 *
1192 ***************************************/
1193void
1194print_details(struct key_state_ssl *ks_ssl, const char *prefix)
1195{
1196 const mbedtls_x509_crt *cert;
1197 char s1[256];
1198 char s2[256];
1199
1200 s1[0] = s2[0] = 0;
1201 snprintf(s1, sizeof(s1), "%s %s, cipher %s", prefix, mbedtls_ssl_get_version(ks_ssl->ctx),
1202 mbedtls_ssl_get_ciphersuite(ks_ssl->ctx));
1203
1204 cert = mbedtls_ssl_get_peer_cert(ks_ssl->ctx);
1205 if (cert != NULL)
1206 {
1207 snprintf(s2, sizeof(s2), ", %u bit key", (unsigned int)mbedtls_pk_get_bitlen(&cert->pk));
1208 }
1209
1210 msg(D_HANDSHAKE, "%s%s", s1, s2);
1211}
1212
1213void
1214show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
1215{
1216 if (tls13)
1217 {
1218 /* mbed TLS has no TLS 1.3 support currently */
1219 return;
1220 }
1221 struct tls_root_ctx tls_ctx;
1222 const int *ciphers = mbedtls_ssl_list_ciphersuites();
1223
1224 tls_ctx_new(&tls_ctx);
1225 tls_ctx_set_cert_profile(&tls_ctx, tls_cert_profile);
1226 tls_ctx_restrict_ciphers(&tls_ctx, cipher_list);
1227
1228 if (tls_ctx.allowed_ciphers)
1229 {
1230 ciphers = tls_ctx.allowed_ciphers;
1231 }
1232
1233 while (*ciphers != 0)
1234 {
1235 printf("%s\n", mbedtls_ssl_get_ciphersuite_name(*ciphers));
1236 ciphers++;
1237 }
1238 tls_ctx_free(&tls_ctx);
1239}
1240
1241void
1243{
1244 printf("Available elliptic curves:\n\n");
1245 for (size_t i = 0; i < ecp_curve_info_table_items; i++)
1246 {
1247 printf("%s\n", ecp_curve_info_table[i].name);
1248 }
1249}
1250
1251const char *
1253{
1254 static char mbedtls_version[30];
1255 unsigned int pv = mbedtls_version_get_number();
1256 snprintf(mbedtls_version, sizeof(mbedtls_version), "mbed TLS %u.%u.%u", (pv >> 24) & 0xff,
1257 (pv >> 16) & 0xff, (pv >> 8) & 0xff);
1258 return mbedtls_version;
1259}
1260
1261void
1263{
1264 return; /* no external key provider in mbedTLS build */
1265}
1266
1267#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:618
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:2077
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
Definition buffer.h:161
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2036
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
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:1240
#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:153
#define D_HANDSHAKE_VERBOSE
Definition errlevel.h:152
#define D_HANDSHAKE
Definition errlevel.h:70
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_TLS_DEBUG
Definition errlevel.h:161
#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:3277
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition manage.c:3974
#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:269
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user's password.
Definition ssl.c:283
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
size_t 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:363
const tls_cipher_name_pair * tls_get_cipher_name_pair(const char *cipher_name, size_t len)
Definition ssl_util.c:344
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:2161
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