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 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
770int
771tls_version_max(void)
772{
773#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
774 return TLS_VER_1_3;
775#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
776 return TLS_VER_1_2;
777#else
778#error mbedtls is compiled without support for TLS 1.2 or 1.3
779#endif
780}
781
789mbedtls_ssl_protocol_version
790tls_version_to_ssl_version(int tls_ver)
791{
792 switch (tls_ver)
793 {
794#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
795 case TLS_VER_1_2:
796 return MBEDTLS_SSL_VERSION_TLS1_2;
797#endif
798
799#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
800 case TLS_VER_1_3:
801 return MBEDTLS_SSL_VERSION_TLS1_3;
802#endif
803
804 default:
805 msg(M_FATAL, "%s: invalid or unsupported TLS version %d", __func__, tls_ver);
806 return MBEDTLS_SSL_VERSION_UNKNOWN;
807 }
808}
809
810void
811backend_tls_ctx_reload_crl(struct tls_root_ctx *ctx, const char *crl_file, bool crl_inline)
812{
813 ASSERT(crl_file);
814
815 if (ctx->crl == NULL)
816 {
817 ALLOC_OBJ_CLEAR(ctx->crl, mbedtls_x509_crl);
818 }
819 mbedtls_x509_crl_free(ctx->crl);
820
821 if (crl_inline)
822 {
823 if (!mbed_ok(mbedtls_x509_crl_parse(ctx->crl, (const unsigned char *)crl_file,
824 strlen(crl_file) + 1)))
825 {
826 msg(M_WARN, "CRL: cannot parse inline CRL");
827 goto err;
828 }
829 }
830 else
831 {
832 if (!mbed_ok(mbedtls_x509_crl_parse_file(ctx->crl, crl_file)))
833 {
834 msg(M_WARN, "CRL: cannot read CRL from file %s", crl_file);
835 goto err;
836 }
837 }
838 return;
839
840err:
841 mbedtls_x509_crl_free(ctx->crl);
842}
843
844void
845key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server,
846 struct tls_session *session)
847{
848 ASSERT(NULL != ssl_ctx);
849 ASSERT(ks_ssl);
850 CLEAR(*ks_ssl);
851
852 /* Initialise SSL config */
853 ALLOC_OBJ_CLEAR(ks_ssl->ssl_config, mbedtls_ssl_config);
854 mbedtls_ssl_config_init(ks_ssl->ssl_config);
855 int endpoint = is_server ? MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT;
856 mbedtls_ssl_config_defaults(ks_ssl->ssl_config, endpoint, MBEDTLS_SSL_TRANSPORT_STREAM,
857 MBEDTLS_SSL_PRESET_DEFAULT);
858#ifdef MBEDTLS_DEBUG_C
859 /* We only want to have mbed TLS generate debug level logging when we would
860 * also display it.
861 * In fact mbed TLS 2.25.0 crashes generating debug log if Curve25591 is
862 * selected for DH (https://github.com/ARMmbed/mbedtls/issues/4208) */
863 if (session->opt->ssl_flags & SSLF_TLS_DEBUG_ENABLED)
864 {
865 mbedtls_debug_set_threshold(3);
866 }
867 else
868 {
869 mbedtls_debug_set_threshold(2);
870 }
871#endif
872 mbedtls_ssl_conf_dbg(ks_ssl->ssl_config, my_debug, NULL);
873
874 mbedtls_ssl_conf_cert_profile(ks_ssl->ssl_config, &ssl_ctx->cert_profile);
875
876 if (ssl_ctx->allowed_ciphers)
877 {
878 mbedtls_ssl_conf_ciphersuites(ks_ssl->ssl_config, ssl_ctx->allowed_ciphers);
879 }
880
881 if (ssl_ctx->groups)
882 {
883 mbedtls_ssl_conf_groups(ks_ssl->ssl_config, ssl_ctx->groups);
884 }
885
886 /* Disable TLS renegotiations if the mbedtls library supports that feature.
887 * OpenVPN's renegotiation creates new SSL sessions and does not depend on
888 * this feature and TLS renegotiations have been problematic in the past. */
889#if defined(MBEDTLS_SSL_RENEGOTIATION)
890 mbedtls_ssl_conf_renegotiation(ks_ssl->ssl_config, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
891#endif /* MBEDTLS_SSL_RENEGOTIATION */
892
893 /* Disable record splitting (for now). OpenVPN assumes records are sent
894 * unfragmented, and changing that will require thorough review and
895 * testing. Since OpenVPN is not susceptible to BEAST, we can just
896 * disable record splitting as a quick fix. */
897#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
898 mbedtls_ssl_conf_cbc_record_splitting(ks_ssl->ssl_config,
899 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
900#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
901
902 /* Initialise authentication information */
903 (void)mbed_ok(mbedtls_ssl_conf_own_cert(ks_ssl->ssl_config, ssl_ctx->crt_chain, ssl_ctx->priv_key));
904
905 /* Initialise SSL verification */
906 if (session->opt->ssl_flags & SSLF_CLIENT_CERT_OPTIONAL)
907 {
908 mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL);
909 }
910 else if (!(session->opt->ssl_flags & SSLF_CLIENT_CERT_NOT_REQUIRED))
911 {
912 mbedtls_ssl_conf_authmode(ks_ssl->ssl_config, MBEDTLS_SSL_VERIFY_REQUIRED);
913 }
914 mbedtls_ssl_conf_verify(ks_ssl->ssl_config, verify_callback, session);
915
916 /* TODO: mbed TLS does not currently support sending the CA chain to the client */
917 mbedtls_ssl_conf_ca_chain(ks_ssl->ssl_config, ssl_ctx->ca_chain, ssl_ctx->crl);
918
919 /* Initialize minimum TLS version */
920 {
921 const int configured_tls_version_min =
923
924 /* default to TLS 1.2 */
925 mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_TLS1_2;
926
927 if (configured_tls_version_min > TLS_VER_UNSPEC)
928 {
929 version = tls_version_to_ssl_version(configured_tls_version_min);
930 }
931
932 mbedtls_ssl_conf_min_tls_version(ks_ssl->ssl_config, version);
933 }
934
935 /* Initialize maximum TLS version */
936 {
937 const int configured_tls_version_max =
939
940 mbedtls_ssl_protocol_version version = MBEDTLS_SSL_VERSION_UNKNOWN;
941
942 if (configured_tls_version_max > TLS_VER_UNSPEC)
943 {
944 version = tls_version_to_ssl_version(configured_tls_version_max);
945 }
946 else
947 {
948 /* Default to tls_version_max(). */
949 version = tls_version_to_ssl_version(tls_version_max());
950 }
951
952 mbedtls_ssl_conf_max_tls_version(ks_ssl->ssl_config, version);
953 }
954
955 /* Initialise SSL context */
956 ALLOC_OBJ_CLEAR(ks_ssl->ctx, mbedtls_ssl_context);
957 mbedtls_ssl_init(ks_ssl->ctx);
958 (void)mbed_ok(mbedtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config));
959 /* We do verification in our own callback depending on the
960 * exact configuration. We do not rely on the default hostname
961 * verification. */
962 ASSERT(mbed_ok(mbedtls_ssl_set_hostname(ks_ssl->ctx, NULL)));
963
964 /* Initialise BIOs */
966 mbedtls_ssl_set_bio(ks_ssl->ctx, ks_ssl->bio_ctx, ssl_bio_write, ssl_bio_read, NULL);
967}
968
969
970void
972{
973 mbedtls_ssl_send_alert_message(ks_ssl->ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
974 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY);
975}
976
977void
978key_state_ssl_free(struct key_state_ssl *ks_ssl)
979{
980 if (ks_ssl)
981 {
982 if (ks_ssl->ctx)
983 {
984 mbedtls_ssl_free(ks_ssl->ctx);
985 free(ks_ssl->ctx);
986 }
987 if (ks_ssl->ssl_config)
988 {
989 mbedtls_ssl_config_free(ks_ssl->ssl_config);
990 free(ks_ssl->ssl_config);
991 }
992 if (ks_ssl->bio_ctx)
993 {
994 buf_free_entries(&ks_ssl->bio_ctx->in);
995 buf_free_entries(&ks_ssl->bio_ctx->out);
996 free(ks_ssl->bio_ctx);
997 }
998 CLEAR(*ks_ssl);
999 }
1000}
1001
1002int
1003key_state_write_plaintext(struct key_state_ssl *ks, struct buffer *buf)
1004{
1005 int retval = 0;
1006
1007 ASSERT(buf);
1008
1009 retval = key_state_write_plaintext_const(ks, BPTR(buf), BLEN(buf));
1010
1011 if (1 == retval)
1012 {
1013 memset(BPTR(buf), 0, BLEN(buf)); /* erase data just written */
1014 buf->len = 0;
1015 }
1016
1017 return retval;
1018}
1019
1020int
1021key_state_write_plaintext_const(struct key_state_ssl *ks, const uint8_t *data, int len)
1022{
1023 int retval = 0;
1024
1025 ASSERT(NULL != ks);
1026 ASSERT(len >= 0);
1027
1028 if (0 == len)
1029 {
1030 return 0;
1031 }
1032
1033 ASSERT(data);
1034
1035 retval = mbedtls_ssl_write(ks->ctx, data, len);
1036
1037 if (retval < 0)
1038 {
1039 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1040 {
1041 return 0;
1042 }
1043 mbed_log_err(D_TLS_ERRORS, retval, "TLS ERROR: write tls_write_plaintext_const error");
1044 return -1;
1045 }
1046
1047 if (retval != len)
1048 {
1049 msg(D_TLS_ERRORS, "TLS ERROR: write tls_write_plaintext_const incomplete %d/%d", retval,
1050 len);
1051 return -1;
1052 }
1053
1054 /* successful write */
1055 dmsg(D_HANDSHAKE_VERBOSE, "write tls_write_plaintext_const %d bytes", retval);
1056
1057 return 1;
1058}
1059
1060int
1061key_state_read_ciphertext(struct key_state_ssl *ks, struct buffer *buf)
1062{
1063 int retval = 0;
1064 int len = 0;
1065
1066 ASSERT(NULL != ks);
1067 ASSERT(buf);
1068 ASSERT(buf->len >= 0);
1069
1070 if (buf->len)
1071 {
1072 return 0;
1073 }
1074
1075 len = buf_forward_capacity(buf);
1076
1077 retval = endless_buf_read(&ks->bio_ctx->out, BPTR(buf), len);
1078
1079 /* Error during read, check for retry error */
1080 if (retval < 0)
1081 {
1082 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1083 {
1084 return 0;
1085 }
1086 mbed_log_err(D_TLS_ERRORS, retval, "TLS_ERROR: read tls_read_ciphertext error");
1087 buf->len = 0;
1088 return -1;
1089 }
1090 /* Nothing read, try again */
1091 if (0 == retval)
1092 {
1093 buf->len = 0;
1094 return 0;
1095 }
1096
1097 /* successful read */
1098 dmsg(D_HANDSHAKE_VERBOSE, "read tls_read_ciphertext %d bytes", retval);
1099 buf->len = retval;
1100 return 1;
1101}
1102
1103int
1104key_state_write_ciphertext(struct key_state_ssl *ks, struct buffer *buf)
1105{
1106 int retval = 0;
1107
1108 ASSERT(NULL != ks);
1109 ASSERT(buf);
1110 ASSERT(buf->len >= 0);
1111
1112 if (0 == buf->len)
1113 {
1114 return 0;
1115 }
1116
1117 retval = endless_buf_write(&ks->bio_ctx->in, BPTR(buf), buf->len);
1118
1119 if (retval < 0)
1120 {
1121 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval)
1122 {
1123 return 0;
1124 }
1125 mbed_log_err(D_TLS_ERRORS, retval, "TLS ERROR: write tls_write_ciphertext error");
1126 return -1;
1127 }
1128
1129 if (retval != buf->len)
1130 {
1131 msg(D_TLS_ERRORS, "TLS ERROR: write tls_write_ciphertext incomplete %d/%d", retval,
1132 buf->len);
1133 return -1;
1134 }
1135
1136 /* successful write */
1137 dmsg(D_HANDSHAKE_VERBOSE, "write tls_write_ciphertext %d bytes", retval);
1138
1139 memset(BPTR(buf), 0, BLEN(buf)); /* erase data just written */
1140 buf->len = 0;
1141
1142 return 1;
1143}
1144
1145int
1146key_state_read_plaintext(struct key_state_ssl *ks, struct buffer *buf)
1147{
1148 int retval = 0;
1149 int len = 0;
1150
1151 ASSERT(NULL != ks);
1152 ASSERT(buf);
1153 ASSERT(buf->len >= 0);
1154
1155 if (buf->len)
1156 {
1157 return 0;
1158 }
1159
1160 len = buf_forward_capacity(buf);
1161
1162 retval = mbedtls_ssl_read(ks->ctx, BPTR(buf), len);
1163
1164 /* Error during read, check for retry error */
1165 if (retval < 0)
1166 {
1167 if (MBEDTLS_ERR_SSL_WANT_WRITE == retval || MBEDTLS_ERR_SSL_WANT_READ == retval
1168 || MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET == retval)
1169 {
1170 return 0;
1171 }
1172 mbed_log_err(D_TLS_ERRORS, retval, "TLS_ERROR: read tls_read_plaintext error");
1173 buf->len = 0;
1174 return -1;
1175 }
1176 /* Nothing read, try again */
1177 if (0 == retval)
1178 {
1179 buf->len = 0;
1180 return 0;
1181 }
1182
1183 /* successful read */
1184 dmsg(D_HANDSHAKE_VERBOSE, "read tls_read_plaintext %d bytes", retval);
1185 buf->len = retval;
1186
1187 return 1;
1188}
1189
1190/* **************************************
1191 *
1192 * Information functions
1193 *
1194 * Print information for the end user.
1195 *
1196 ***************************************/
1197void
1198print_details(struct key_state_ssl *ks_ssl, const char *prefix)
1199{
1200 const mbedtls_x509_crt *cert;
1201 char s1[256];
1202 char s2[256];
1203
1204 s1[0] = s2[0] = 0;
1205 snprintf(s1, sizeof(s1), "%s %s, cipher %s", prefix, mbedtls_ssl_get_version(ks_ssl->ctx),
1206 mbedtls_ssl_get_ciphersuite(ks_ssl->ctx));
1207
1208 cert = mbedtls_ssl_get_peer_cert(ks_ssl->ctx);
1209 if (cert != NULL)
1210 {
1211 snprintf(s2, sizeof(s2), ", %u bit key", (unsigned int)mbedtls_pk_get_bitlen(&cert->pk));
1212 }
1213
1214 msg(D_HANDSHAKE, "%s%s", s1, s2);
1215}
1216
1217void
1218show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
1219{
1220 if (tls13)
1221 {
1222 /* mbed TLS has no TLS 1.3 support currently */
1223 return;
1224 }
1225 struct tls_root_ctx tls_ctx;
1226 const int *ciphers = mbedtls_ssl_list_ciphersuites();
1227
1228 tls_ctx_new(&tls_ctx);
1229 tls_ctx_set_cert_profile(&tls_ctx, tls_cert_profile);
1230 tls_ctx_restrict_ciphers(&tls_ctx, cipher_list);
1231
1232 if (tls_ctx.allowed_ciphers)
1233 {
1234 ciphers = tls_ctx.allowed_ciphers;
1235 }
1236
1237 while (*ciphers != 0)
1238 {
1239 printf("%s\n", mbedtls_ssl_get_ciphersuite_name(*ciphers));
1240 ciphers++;
1241 }
1242 tls_ctx_free(&tls_ctx);
1243}
1244
1245void
1247{
1248 printf("Available elliptic curves:\n\n");
1249 for (size_t i = 0; i < ecp_curve_info_table_items; i++)
1250 {
1251 printf("%s\n", ecp_curve_info_table[i].name);
1252 }
1253}
1254
1255const char *
1257{
1258 static char mbedtls_version[30];
1259 unsigned int pv = mbedtls_version_get_number();
1260 snprintf(mbedtls_version, sizeof(mbedtls_version), "mbed TLS %u.%u.%u", (pv >> 24) & 0xff,
1261 (pv >> 16) & 0xff, (pv >> 8) & 0xff);
1262 return mbedtls_version;
1263}
1264
1265void
1267{
1268 return; /* no external key provider in mbedTLS build */
1269}
1270
1271#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:1246
#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