From: "cron2 (Code Review)" cron2 upl=
oaded patch set #8 to the change originally created by MaxF. The following approvals got outdated and were removed:
Code-Review+2 by plaisthosAdd support for Mbed TLS 4
<=
br>This commit adds support for Mbed TLS 4. This version comes with some
drastic changes. The crypto library has been completely redesigned, so
=
the contents of crypto_mbedtls.c are moved to crypto_mbedtls_legacy.c
an=
d crypto_mbedtls.c handles the crypto for version 4.
Mbed TLS 4 also=
removed the feature for looking up a crypto algorithm by
name, so we ne=
ed to translate algorithm names to Mbed TLS numbers in
OpenVPN. The tabl=
es are not yet complete. For symmetric algorithms, I
have added AES and =
Chacha-Poly which should be enough for most use
cases.
Change-Id:=
Ib251d546d993b96ed3bd8cb9111bcc627cdb0fae
Signed-off-by: Max Fillinger =
<maximilian.fillinger@...2918...>
Acked-by: Arne Schwabe <arne-=
openvpn@...1227...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/=
+/1441
Message-Id: <20260123164746.7333-1-gert@...1296...>
URL:=
https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg35401.=
html
Signed-off-by: Gert Doering <gert@...1296...>
---
M REA=
DME.mbedtls
M src/openvpn/Makefile.am
M src/openvpn/crypto_backend.h<=
br>M src/openvpn/crypto_mbedtls.c
M src/openvpn/crypto_mbedtls.h
A sr=
c/openvpn/crypto_mbedtls_legacy.c
A src/openvpn/crypto_mbedtls_legacy.h<=
br>M src/openvpn/mbedtls_compat.h
M src/openvpn/ssl_mbedtls.c
M src/o=
penvpn/ssl_mbedtls.h
M src/openvpn/ssl_verify_mbedtls.c
M src/openvpn=
/syshead.h
M tests/Makefile.am
M tests/t_server_null_default.rc
M =
tests/unit_tests/openvpn/Makefile.am
M tests/unit_tests/openvpn/test_com=
mon.h
16 files changed, 2,442 insertions(+), 704 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/chang= es/41/1441/8
diff --git a/README.mbedtls b/README.mbedtls
index fb30db1..b768687 100644
--- a/README.mbedtls= span>
+++ b/README.mbedtls
@@ -7,8 +7,8 @@<= br> make
make install
= -This version requires mbed TLS v= ersion >=3D 3.2.1. Versions >=3D 4.0.0 are not
-yet supported. Support for TLS 1.3 requires= an Mbed TLS version >=3D 3.6.4.
+This version requires mbed TLS version >=3D 3.2.1. Suppo= rt for TLS 1.3 requires
+an Mbed TLS version >=3D 3.6.4.
*= ************************************************************************
diff --git a/src/openvpn/Makefile.am b/src/= openvpn/Makefile.am
index c879585..3c567aa 100644
--- a/src/openvpn/Makefile.am
+++ b/src/openvpn/Make= file.am
@@ -54,6 +54,7 @@
comp-lz4.c comp= -lz4.h \
crypto.c crypto.h crypto_backend.h \
<= span> crypto_openssl.c crypto_openssl.h \
+ crypto_mbedtls_legacy.c crypto_mbedtls_legacy.h \= span>
crypto_mbedtls.c crypto_mbedtls.h \
cryp= to_epoch.c crypto_epoch.h \
dco.c dco.h dco_internal.h \<= /span>
diff --git a/src/openvpn/crypto_backend.h b/src/openvpn/cry= pto_backend.h
index e9fc2f3..5248614 100644
--- a/src/openvpn/crypto_backend.h
+++ b/src/openvpn/crypt= o_backend.h
@@ -32,9 +32,16 @@
#ifdef ENAB= LE_CRYPTO_OPENSSL
#include "crypto_openssl.h"
#endif
+
#ifdef ENABLE_CRYPTO_MBEDTLS
+#include <mbedtls/version.h>
<= span style=3D"color: hsl(120, 100%, 40%);">+#if MBEDTLS_VERSION_NUMBER <= 0x04000000
+#include= "crypto_mbedtls_legacy.h"
+#else
#include "crypto_mbedtls.h&quo= t;
#endif
+#endif
+
#include "basic.h"
#include "bu= ffer.h"
diff --git a/src/openvpn/cry= pto_mbedtls.c b/src/openvpn/crypto_mbedtls.c
index e1a5cad.= .5418df9 100644
--- a/src/openvpn/crypto_mbedtls.c+++ b/src/openvpn/crypto_mbedtls.c
@@ -23,7 +23,8 @= @
/**
* @file- * Data Channel Cryptography m= bed TLS-specific backend interface
+ * Data Channel Cryptography backend interface using the TF-= PSA-Crypto library
+ = * part of Mbed TLS 4.
*/
#ifdef HAVE_CONFIG_H
@@ -33,6 +34,9 @@
= #include "syshead.h"
#if defin= ed(ENABLE_CRYPTO_MBEDTLS)
+#include <mbedtls/version.h>
+
+#if MBEDTLS_VERSION_NUMBER >=3D 0x04000000
#include "errlevel.h"
#include "bas= ic.h"
@@ -40,20 +44,16 @@
#include &q= uot;crypto.h"
#include "integer.h" #include "crypto_backend.h"
+#include "crypto_mbedtls.h"
= #include "otime.h"
#include "misc.h&= quot;
-#include <mbedtls/base64.h>
-#include <mbedtls/des.h>
+#include <psa/crypto.h>
+#include <psa/crypto_config.h>
+#include <mbedtls/co= nstant_time.h>
#include <mbedtls/error.h><= br>-#include <mbedtls/md5.h>= ;
-#include <mbedtls= /cipher.h>
#include <mbedtls/pem.h>
-#include <mbed= tls/entropy.h>
-#inc= lude <mbedtls/ssl.h>
-
-
/*
*
* Hardware engine support. Allow= s loading/unloading of engines.
@@ -82,331 +82,65 @@=
{
}
-/*
- *
- * Functio= ns related to the core crypto library
- *
- */=
+/* The library does= n't support looking up algorithms by string anymore, so here
= + * is a lookup table. */
+static const cipher_info_= t cipher_info_table[] =3D {
+/* TODO: Complete the table. */
-void
-crypto_init_lib(void)
-{
-}=
+/* AES */
+#if PSA_WANT_KEY_TYPE_AES
+#if PSA_WANT_ALG_GCM
+ { "AES-128-GC= M", PSA_KEY_TYPE_AES, PSA_ALG_GCM, 128 / 8, 96 / 8, 128 / 8 },<= br>+ { "AES-192-GCM&quo= t;, PSA_KEY_TYPE_AES, PSA_ALG_GCM, 192 / 8, 96 / 8, 128 / 8 },+ { "AES-256-GCM", P= SA_KEY_TYPE_AES, PSA_ALG_GCM, 256 / 8, 96 / 8, 128 / 8 },
+#endif /* PSA_WANT_ALG_GCM */<= br>+#if PSA_WANT_ALG_CBC_PKCS7<= /span>
+ { "AES-128-= CBC", PSA_KEY_TYPE_AES, PSA_ALG_CBC_PKCS7, 128 / 8, 128 / 8, 128 / 8 }= ,
+ { "AES-19= 2-CBC", PSA_KEY_TYPE_AES, PSA_ALG_CBC_PKCS7, 192 / 8, 128 / 8, 128 / 8= },
+ { "AES-= 256-CBC", PSA_KEY_TYPE_AES, PSA_ALG_CBC_PKCS7, 256 / 8, 128 / 8, 128 /= 8 },
+#endif /* PSA_= WANT_ALG_CBC_PKCS7 */
+#if PSA_WANT_ALG_CTR
+ { "AES-128-CTR", PSA_KEY_TYPE_AES, PSA_ALG_CTR, 128 / 8, 1= 28 / 8, 128 / 8 },
+ = { "AES-192-CTR", PSA_KEY_TYPE_AES, PSA_ALG_CTR, 192 / 8, 128 /= 8, 128 / 8 },
+ {= "AES-256-CTR", PSA_KEY_TYPE_AES, PSA_ALG_CTR, 256 / 8, 128 / 8, = 128 / 8 },
+#endif /*= PSA_WANT_ALG_CTR */
= +#endif /* PSA_WANT_KEY_TYPE_AES */
-void
-crypto_uninit_lib(void)
-{
-}= span>
-
-void
-crypto_clear_error(void)
-{
-}<= /span>
-
-bool
-mbed_log_err(unsigned int flags, int errval, const char *pre= fix)
-{
- if (0 !=3D errval)- {
- char errstr[256];
- mbedtls_strerror(errval, errstr, sizeof(= errstr));
-
<= span style=3D"color: hsl(0, 100%, 40%);">- if (NULL =3D=3D prefix)= span>
- {- prefix =3D "mbed T= LS error";
- = }
- msg(flags,= "%s: %s", prefix, errstr);
- }
-=
- return 0 =3D=3D e= rrval;
-}-
-bool
-mbed_log_func_line(unsigned int flags, int errval, const char *func, int = line)
-{- char prefix[256]; -
- if (snprintf(prefix, sizeof(prefix), "%s:%d"= ;, func, line) >=3D sizeof(prefix))
- {
= - return mbed_log_err(flags, errval, func);
- }
-
- retu= rn mbed_log_err(flags, errval, prefix);
-}
-= span>
-
-#ifdef DMALLOC
-void
-crypto_init_dmalloc(void)
-{
- msg(= M_ERR, "Error: dmalloc support is not available for mbed TLS.");<= /span>
-}
-#endif /* DMALLOC */
-
-const cipher_name_pair cipher_name_translation_table[] =3D {
- { "BF-CBC",= "BLOWFISH-CBC" },
- { "BF-CFB", "BLOWFISH-CFB64" },- { "CAMELLIA-128-CFB"= , "CAMELLIA-128-CFB128" },
- { "CAMELLIA-192-CFB", "CAMELLIA-192-CFB12= 8" },
- { "= ;CAMELLIA-256-CFB", "CAMELLIA-256-CFB128" }
+/* Chacha-Poly */
+#if PSA_WANT_KEY_TYPE_CHACHA20 &&a= mp; PSA_WANT_ALG_CHACHA20_POLY1305
+ { "CHACHA20-POLY1305", PSA_KEY_TYPE_CHACHA20, = PSA_ALG_CHACHA20_POLY1305, 256 / 8, 96 / 8, 1 },
+#endif
};
+static const size_t cipher_info_table_e= ntries =3D sizeof(cipher_info_table) / sizeof(cipher_info_t);+
+static const cipher_info_t *
+cipher_get(const char *ciphername)=
+{
+ for (size_t i =3D 0; i < cipher_info_= table_entries; i++)
+= {
+ if (st= rcmp(ciphername, cipher_info_table[i].name) =3D=3D 0)
+ {
+ return &cipher_info_table[i];
+ }
+ }
+ return NULL;
+}
+
+/* Because Mbe= d TLS 4 doesn't support looking up algorithms by string, there's
+ * nothing to translat= e. */
+const cipher_n= ame_pair cipher_name_translation_table[] =3D {};
const siz= e_t cipher_name_translation_table_count =3D
- sizeof(cipher_name_translation_table) / sizeof(*c= ipher_name_translation_table);
-
-void<= br>-show_available_ciphers(void)<= /span>
-{
- const int *ciphers =3D mbedtls_cipher_= list();
--#ifndef ENABLE_SMALL - printf("The following ciphe= rs and cipher modes are available for use\n"
- "with " PACKAGE_NAME "= ;. Each cipher shown below may be used as a\n"
- "parameter to the --data-ci= phers (or --cipher) option. Using a\n"
- "GCM or CBC mode is recommended. In= static key mode only CBC\n"
- "mode is allowed.\n\n");
-#endif
-
-= while (*ciphers !=3D 0)
- {
- co= nst mbedtls_cipher_info_t *info =3D mbedtls_cipher_info_from_type(*ciphers)= ;
- const char *= name =3D mbedtls_cipher_info_get_name(info);
- if (info && name && !cipher_= kt_insecure(name)
- = && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))=
- {
<= span style=3D"color: hsl(0, 100%, 40%);">- print_cipher(name);= span>
- }- ciphers++;
- }
-
-= printf("\nThe following ciphers have a block size of less than 128= bits, \n"
- = "and are therefore deprecated. Do not use unless you have to.\n\= n");
- ciphers = =3D mbedtls_cipher_list();
- while (*ciphers !=3D 0)
- {
- = const mbedtls_cipher_info_t *info =3D mbedtls_cipher_info_from_type(*cip= hers);
- const c= har *name =3D mbedtls_cipher_info_get_name(info);
- if (info && name && cip= her_kt_insecure(name)
-= && (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(nam= e)))
- {<= br>- print_cipher(name= );
- }
- ciphers++;- }
- printf("\n");
-}
-
-void
-show_available_digests(voi= d)
-{
- const int *digests =3D mbedtls_md_l= ist();
--#ifndef ENABLE_SMALL
- printf("The following messag= e digests are available for use with\n" PACKAGE_NAME
- ". A message digest is= used in conjunction with\n"
- "the HMAC function, to authenticate received pa= ckets.\n"
- = "You can specify a message digest as parameter to\n"- "the --auth op= tion.\n\n");
-#end= if
-
- while (*digests !=3D 0)- {
- const mbedtls_md_info_t *info =3D mbedtls_= md_info_from_type(*digests);
-
- if (in= fo)
- {- printf("%s %d= bit default key\n", mbedtls_md_get_name(info),
- mbedtls_md_get_size(info= ) * 8);
- }
- digests++;<= br>- }
- printf("\n");
-}
-
-voi= d
-show_available_engin= es(void)
-{
<= span style=3D"color: hsl(0, 100%, 40%);">- printf("Sorry, mbed TLS = hardware crypto engine functionality is not "
- "available\n");
-}
-
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wconv= ersion"
-#endif
-
-bool
-crypto_pem_encode(const char *name, struct buffer *dst, cons= t struct buffer *src,
-= struct gc_arena *gc)
-{
- = /* 1000 chars is the PEM line length limit (+1 for tailing NUL) */<= br>- char header[1000 + 1] =3D= { 0 };
- char foote= r[1000 + 1] =3D { 0 };
= -
- if (snprintf(hea= der, sizeof(header), "-----BEGIN %s-----\n", name) >=3D sizeof= (header))
- {=
- return false;=
- }
- if (snprintf(footer, sizeof(footer), &q= uot;-----END %s-----\n", name) >=3D sizeof(footer))
- {
- return false;
- }
-
- size_t out_= len =3D 0;
- if (MBE= DTLS_ERR_BASE64_BUFFER_TOO_SMALL
- !=3D mbedtls_pem_write_buffer(header, footer, BPTR(src),= BLEN(src), NULL, 0, &out_len))
- {
- = return false;
- = }
-
- /* We set the size buf to out_len-1= to NOT include the 0 byte that
- * mbedtls_pem_write_buffer in its length calculation */
- *dst =3D alloc_buf_gc(= out_len, gc);
- if (= !mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src), BPT= R(dst),
- = BCAP(dst), &out_len))
- || !buf_inc_len(dst, out_len - 1))<= /span>
- {
- CLEAR(*dst);
- return false;
- }
-
- = return true;
-}
-
-bool
-crypto_pem_decode(const char *name, struct buffer *dst, const st= ruct buffer *src)
-{
- /* 1000 chars is the= PEM line length limit (+1 for tailing NUL) */
- char header[1000 + 1] =3D { 0 };- char footer[1000 + 1] =3D { 0 }= ;
-
- if (snprintf(header, sizeof(header), = "-----BEGIN %s-----", name) >=3D sizeof(header))- {
- return false;
- }
- if (snprintf(footer, sizeof(footer), "-----END %s-----&quo= t;, name) >=3D sizeof(footer))
- {
- = return false;
- = }
-
- /* mbed TLS requires the src to be nu= ll-terminated */
- /= * allocate a new buffer to avoid modifying the src buffer */- struct gc_arena gc =3D gc_new();=
- struct buffer inp= ut =3D alloc_buf_gc(BLEN(src) + 1, &gc);
- buf_copy(&input, src);
- buf_null_terminate(&input);<= br>-
- size_t use_len =3D 0;
- mbedtls_pem_context ctx =3D { 0 };<= br>- bool ret =3D- mbed_ok(mbedtls_pem_read_b= uffer(&ctx, header, footer, BPTR(&input), NULL, 0, &use_len));<= /span>
- size_t buf_size = =3D 0;
- const unsig= ned char *buf =3D mbedtls_pem_get_buffer(&ctx, &buf_size);- if (ret && !buf_wr= ite(dst, buf, buf_size))
- {
- ret = =3D false;
- msg= (M_WARN, "PEM decode error: destination buffer too small");
- }
-
- mbedtls_pem_free(&ctx);
- gc_free(&gc);
- return ret;
-}
-
-/*
- *
- * Random number functions, used in cases where we want=
- * reasonably strong cryptog= raphic random number generation
- * without depleting our entropy pool. Used for random
- * IV values and a number of ot= her miscellaneous tasks.
- *
- */-
-/*
= - * Initialise the given ctr_drbg context, using a personalisation string a= nd an
- * entropy gathe= ring function.
- */
-mbedtls_ctr_drbg_context = *
-rand_ctx_get(void)= span>
-{
- static mbedtls_entropy_context ec =3D {= 0 };
- static mbedt= ls_ctr_drbg_context cd_ctx =3D { 0 };
- static bool rand_initialised =3D false;
-
- if (!rand_initialised)
- {
- struct gc_arena gc =3D gc_new();
- struct buffer pers_string =3D alloc_buf_gc= (100, &gc);
-
- /*
- * Personalisation string, sho= uld be as unique as possible (see NIST
- * 800-90 section 8.7.1). We have very little info= rmation at this stage.
= - * Include Program Name, memory address of the context and PID.
- */- buf_printf(&pers_strin= g, "OpenVPN %0u %p %s", platform_getpid(), &cd_ctx,
- time_string(= 0, 0, 0, &gc));
-= span>
- /* Initialise m= bed TLS RNG, and built-in entropy sources */
- mbedtls_entropy_init(&ec);-
- mbedtls_ctr_drbg_init(&cd_ctx);- if (!mbed_ok(mbedtls_ctr_d= rbg_seed(&cd_ctx, mbedtls_entropy_func, &ec, BPTR(&pers_string)= ,
- = BLEN(&pers_string))))
- {
- msg(M_FATAL, "Failed to initialize random g= enerator");
- = }
-
- gc_free(&gc);- rand_initialised =3D true;=
- }
-
- return &cd_ctx;
-}
-<= /span>
-#ifdef ENABLE_PREDICTI= ON_RESISTANCE
-void
-rand_ctx_enable_predictio= n_resistance(void)
-{= span>
- mbedtls_ctr_drbg_co= ntext *cd_ctx =3D rand_ctx_get();
-
- mbedt= ls_ctr_drbg_set_prediction_resistance(cd_ctx, 1);
-}
-#endif /* ENABLE_PREDICTION_RESISTANCE */
+ sizeof(cipher_name_translation_table) / s= izeof(cipher_name_pair);
int
<= span> rand_bytes(uint8_t *output, int len)
{- mbedtls_ctr_drbg_context *rng_c= tx =3D rand_ctx_get();
= -
- while (len > = 0)
+ if (len < = 0)
{
- const size_t blen =3D min_int(len, MBEDTLS_CTR_DRBG_MAX_REQUEST= );
- if (0 !=3D = mbedtls_ctr_drbg_random(rng_ctx, output, blen))
- {
- return 0;
- }
-
- output +=3D= blen;
- len -= =3D blen;
+ re= turn 0;
}
-
- return 1= ;
-}
-
-/*
- *
- * Generic cipher key ty= pe functions
- *=
- */
-static const mbedtls_cipher_info_t *
-cipher_get(const char *cipherna= me)
-{
- ASSERT(ciphername);-
- const mbedtls_cipher_info_t *cipher =3D NULL;<= br>-
- ciphername =3D translate_cipher_name_from_open= vpn(ciphername);
- c= ipher =3D mbedtls_cipher_info_from_string(ciphername);
- return cipher;
+ psa_status_t result =3D psa_generate_= random(output, (size_t)len);
+ return result =3D=3D PSA_SUCCESS;
}
bool
@@ -414,23 +148,22 @@=
{
ASSERT(reason);
<= br>- const mbedtls_cipher_info= _t *cipher =3D cipher_get(ciphername);
+ const cipher_info_t *cipher_info =3D cipher_get(ciph= ername);
- if (NULL =3D=3D cipher)
+ if (cipher_info =3D=3D NULL)
{=
msg(D_LOW, "Cipher algorithm '%s' not found= ", ciphername);
*reason =3D "disabled be= cause unknown";
return false;
}
- const size_t key_bytelen =3D mbedtls_cipher_info_get_key_bitlen(cip= her) / 8;
- if (key_= bytelen > MAX_CIPHER_KEY_LENGTH)
+ if (cipher_info->key_bytes > MAX_CIPHER_KEY_LENGT= H)
{
msg(D_LOW,- "Cipher algorithm = '%s' uses a default key size (%zu bytes) "
+ "Cipher algorithm '= ;%s' uses a default key size (%d bytes) "
= "which is larger than " PACKAGE_NAME "'s current ma= ximum key size "
"(%d bytes)",<= /span>
- ciphername= , key_bytelen, MAX_CIPHER_KEY_LENGTH);
+ ciphername, cipher_info->key_bytes, MAX_C= IPHER_KEY_LENGTH);
*reason =3D "disabled due = to key size too large";
return false;<= br> }
@@ -442,49 +175,46 @@
cons= t char *
cipher_kt_name(const char *ciphername)
= {
- const mbe= dtls_cipher_info_t *cipher_kt =3D cipher_get(ciphername);
- if (NULL =3D=3D cipher_kt)+ const cipher_info_t *cip= her_info =3D cipher_get(ciphername);
+ if (cipher_info =3D=3D NULL)
{
return "[null-cipher]";
= }
-
- return translate_cipher_name_to_op= envpn(mbedtls_cipher_info_get_name(cipher_kt));
+ return cipher_info->name;}
int
cipher_kt_key_= size(const char *ciphername)
{
- const mbedtls_cipher_info_t *cipher_kt =3D ci= pher_get(ciphername);
-=
- if (NULL =3D=3D c= ipher_kt)
+ const = cipher_info_t *cipher_info =3D cipher_get(ciphername);
+ if (cipher_info =3D=3D NULL)<= br> {
return 0;
}= span>
-
- return (int)mbedtls_cipher_info_get_key= _bitlen(cipher_kt) / 8;
+ return cipher_info->key_bytes;
}
int
cipher_kt_iv_size(const char *cip= hername)
{
- const mbedtls_cipher_info_t *cipher_kt =3D cipher_get(ciphername)= ;
+ const cipher_i= nfo_t *cipher_info =3D cipher_get(ciphername);
= - if (NULL =3D=3D cipher_kt)= span>
+ if (cipher_info = =3D=3D NULL)
{
return 0;
}
- = return (int)mbedtls_cipher_info_get_iv_size(cipher_kt);
+ return cipher_info->iv_bytes;= span>
}
int
c= ipher_kt_block_size(const char *ciphername)
{- const mbedtls_cipher_info_t *c= ipher_kt =3D cipher_get(ciphername);
- if (NULL =3D=3D cipher_kt)
+ const cipher_info_t *cipher_info =3D cipher_g= et(ciphername);
+ = if (cipher_info =3D=3D NULL)
{
= return 0;
}
- return (int)mbedtls_cipher_info_get_block_size(cipher_kt);=
+ return cipher_i= nfo->block_size;
}
i= nt
@@ -500,498 +230,596 @@
bool
= cipher_kt_insecure(const char *ciphername)
{=
- const mbedtls_cipher_inf= o_t *cipher_kt =3D cipher_get(ciphername);
- if (!cipher_kt)
+ const cipher_info_t *cipher_info =3D cipher_get(ci= phername);
+ if (c= ipher_info =3D=3D NULL)
{
ret= urn true;
}
- return !(cipher_kt_block_size(ciphername) = >=3D 128 / 8
-#ifdef= MBEDTLS_CHACHAPOLY_C
-= || mbedtls_cipher_info_get_type(cipher_kt) =3D=3D MBEDTLS_CIPH= ER_CHACHA20_POLY1305
-#= endif
- );
-}
-
-static mbedtls_cipher_mode_t
-cipher_kt_mode(const mbedtls_cipher_info_t *cipher_kt)
= -{
- ASSERT(NULL !=3D cipher_kt);
- return mbedtls_cipher_info_get_mode(c= ipher_kt);
+ retur= n !(cipher_info->block_size >=3D 128 / 8
+ || cipher_info->psa_alg =3D=3D P= SA_ALG_CHACHA20_POLY1305);
}
<= span> bool
cipher_kt_mode_cbc(const char *ciphername)
{
- con= st mbedtls_cipher_info_t *cipher =3D cipher_get(ciphername);- return cipher && cipher_= kt_mode(cipher) =3D=3D OPENVPN_MODE_CBC;
+ const cipher_info_t *cipher_info =3D cipher_get(ci= phername);
+ if (c= ipher_info =3D=3D NULL)
+ {
+ re= turn false;
+ }
+ return cipher_info= ->psa_alg =3D=3D PSA_ALG_CBC_PKCS7;
}
=
bool
cipher_kt_mode_ofb_cfb(const char *= ciphername)
{
- const mbedtls_cipher_info_t *cipher =3D cipher_get(ciphername)= ;
- return cipher
- && (c= ipher_kt_mode(cipher) =3D=3D OPENVPN_MODE_OFB
- || cipher_kt_mode(cipher) =3D=3D OPE= NVPN_MODE_CFB);
+ = const cipher_info_t *cipher_info =3D cipher_get(ciphername);+ if (cipher_info =3D=3D NULL)= span>
+ {
+ return false;+ }
+ return cipher_info->psa_alg =3D=3D PSA_A= LG_OFB || cipher_info->psa_alg =3D=3D PSA_ALG_CFB;
}
bool
cipher_kt_mode_aead(= const char *ciphername)
{
- const mbedtls_cipher_info_t *cipher =3D cipher_get= (ciphername);
- retu= rn cipher
- &= amp;& (cipher_kt_mode(cipher) =3D=3D OPENVPN_MODE_GCM
-#ifdef MBEDTLS_CHACHAPOLY_C
<= span style=3D"color: hsl(0, 100%, 40%);">- || cipher_kt_mode(= cipher) =3D=3D MBEDTLS_MODE_CHACHAPOLY
-#endif
- );
+ = const cipher_info_t *cipher_info =3D cipher_get(ciphername);+ if (cipher_info =3D=3D NULL)= span>
+ {
+ return false;+ }
+ return cipher_info->psa_alg =3D=3D PSA_A= LG_GCM || cipher_info->psa_alg =3D=3D PSA_ALG_CHACHA20_POLY1305;<= br> }
-
-/*
= - *
- * Generic cipher context functions
- *
- */
= -
-mbedtls_cipher_conte= xt_t *
+cipher_ctx_t = *
cipher_ctx_new(void)
{
- mbedtls_cipher_context_t *ctx;
- ALLOC_OBJ(ctx, mbedtl= s_cipher_context_t);
= + cipher_ctx_t *ctx;
+ /* Initializing the object with zeros ensures that it is always saf= e to call
+ * cip= her_ctx_free. */
+ = ALLOC_OBJ_CLEAR(ctx, cipher_ctx_t);
return ctx;
}
void
-cipher_ctx_free(mbedtls_cipher_context_t *c= tx)
+cipher_ctx_free(= cipher_ctx_t *ctx)
{
- mbedtls_cipher_free(ctx);
+ if (cipher_ctx_mode_aead(ctx))+ {
+ ASSERT(psa_aead_abort(&ctx->opera= tion.aead) =3D=3D PSA_SUCCESS);
+ }
+ = else
+ {<= br>+ ASSERT(psa_cipher_a= bort(&ctx->operation.cipher) =3D=3D PSA_SUCCESS);
+ }
+ ASSERT(psa_destroy_key(ctx->key) =3D=3D PSA_S= UCCESS);
free(ctx);
}
void
-cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t *key, const= char *ciphername,
+c= ipher_ctx_init(cipher_ctx_t *ctx, const uint8_t *key, const char *ciphernam= e,
crypto_operation_t enc)
{
- ASSERT(NULL != =3D ciphername && NULL !=3D ctx);
+ ASSERT(ciphername !=3D NULL && ctx !=3D N= ULL);
CLEAR(*ctx);
- const mbedtls_cipher_info_t *kt =3D= cipher_get(ciphername);
- ASSERT(kt);
- = size_t key_bitlen =3D mbedtls_cipher_info_get_key_bitlen(kt);+ ctx->cipher_info =3D ciph= er_get(ciphername);
+= ASSERT(ctx->cipher_info !=3D NULL);
- if (!mbed_ok(mbedtls_cipher_setu= p(ctx, kt)))
- {
- msg(M_FATAL, &quo= t;mbed TLS cipher context init #1");
- }
+ psa_set_key_type(&ctx->key_attributes, ctx->cipher_info= ->psa_key_type);
+= psa_set_key_algorithm(&ctx->key_attributes, ctx->cipher_info-= >psa_alg);
+ ps= a_set_key_bits(&ctx->key_attributes, (size_t)ctx->cipher_info->= ;key_bytes * 8);
+ = psa_set_key_usage_flags(&ctx->key_attributes,
+ enc =3D=3D OP= ENVPN_OP_ENCRYPT ? PSA_KEY_USAGE_ENCRYPT : PSA_KEY_USAGE_DECRYPT);
- if (!mbe= d_ok(mbedtls_cipher_setkey(ctx, key, (int)key_bitlen, enc)))+ if (psa_import_key(&ctx-&g= t;key_attributes, key, (size_t)ctx->cipher_info->key_bytes, &ctx-= >key) !=3D PSA_SUCCESS)
{
- msg(M_FATAL, "mbed TLS cipher set = key");
- }
-
- if (mbedtls_cipher_info_get_mode(kt) =3D=3D= MBEDTLS_MODE_CBC)
- = {
- if (!mbed_o= k(mbedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7)))
<= span style=3D"color: hsl(0, 100%, 40%);">- {
- msg(M_FATAL, "mbed TLS cip= her set padding mode");
- }
+ = msg(M_FATAL, "psa_import_key failed");
= }
/* make sure we used a big enough = key */
- ASSERT(mbed= tls_cipher_get_key_bitlen(ctx) <=3D key_bitlen);
+ ASSERT(psa_get_key_bits(&ctx->= key_attributes) =3D=3D (size_t)(8 * ctx->cipher_info->key_bytes));
}
int
-cipher_ctx_iv_length(const mbedtls_cipher= _context_t *ctx)
+cip= her_ctx_iv_length(const cipher_ctx_t *ctx)
{- return mbedtls_cipher_get_iv_si= ze(ctx);
+ return = ctx->cipher_info->iv_bytes;
}
int
cipher_ctx_get_tag(cipher_ctx_t *ctx, uin= t8_t *tag, int tag_len)
{
- if (tag_len > SIZE_MAX)
+ if (!ctx->aead_finished || tag_len= < OPENVPN_AEAD_TAG_LENGTH)
{
= return 0;
}
- if (!mbed_ok(mbedtls_cipher_write_tag(= ctx, (unsigned char *)tag, tag_len)))
- {
-= return 0;
- = }
-
+ memcpy(tag, ctx->tag, OPENVPN_AE= AD_TAG_LENGTH);
return 1;
}
int
-cipher_ctx_block_size(const mbedtls_cipher_context_t *ctx)+cipher_ctx_block_size(const = cipher_ctx_t *ctx)
{
- return (int)mbedtls_cipher_get_block_size(ctx);<= br>+ return ctx->cipher_i= nfo->block_size;
}
i= nt
-cipher_ctx_mode(con= st mbedtls_cipher_context_t *ctx)
+cipher_ctx_mode(const cipher_ctx_t *ctx)
{= span>
- ASSERT(NULL !=3D ct= x);
-
- return mbedtls_cipher_get_cipher_mo= de(ctx);
+ ASSERT(= ctx !=3D NULL);
+ = return (int)psa_get_key_algorithm(&ctx->key_attributes);
<= span> }
bool
cipher_ctx= _mode_cbc(const cipher_ctx_t *ctx)
{
- return ctx && cipher_ctx_mode(c= tx) =3D=3D OPENVPN_MODE_CBC;
+ return ctx !=3D NULL && cipher_ctx_mode(ctx) =3D=3D OP= ENVPN_MODE_CBC;
}
-
bool
cip= her_ctx_mode_ofb_cfb(const cipher_ctx_t *ctx)
{
= - return ctx
- && (cipher_ctx_mode(= ctx) =3D=3D OPENVPN_MODE_OFB
- || cipher_ctx_mode(ctx) =3D=3D OPENVPN_MODE_CFB);
+ if (ctx =3D=3D NULL= )
+ {+ return false;
= + }
+ int mode =3D cipher_ctx_mode(ctx);
+ return mode =3D=3D OP= ENVPN_MODE_OFB || mode =3D=3D OPENVPN_MODE_CFB;
}
bool
cipher_ctx_mode_aead(const= cipher_ctx_t *ctx)
{
- return ctx
- && (cipher_ctx_mode(ctx) =3D=3D OPENVPN_MODE_G= CM
-#ifdef MBEDTLS_CHAC= HAPOLY_C
- = || cipher_ctx_mode(ctx) =3D=3D MBEDTLS_MODE_CHACHAPOLY
-#endif
- );
+ if (ctx =3D=3D NULL)
+ {
+ return false;
+ }
+ int= mode =3D cipher_ctx_mode(ctx);
+ return mode =3D=3D (int)PSA_ALG_GCM || mode =3D=3D (int)PSA= _ALG_CHACHA20_POLY1305;
+}
++static int
+cipher_ctx_direction(const cipher_ctx_t *= ctx)
+{+ psa_key_usage_t key_usage =3D= psa_get_key_usage_flags(&ctx->key_attributes);
+ if (key_usage & PSA_KEY_USAGE_EN= CRYPT)
+ {<= br>+ return OPENVPN_OP_E= NCRYPT;
+ }=
+ else if (key_usage &am= p; PSA_KEY_USAGE_DECRYPT)
+ {
+ = return OPENVPN_OP_DECRYPT;
+ }
+ els= e
+ {+ return -1;+ }
}
int
-cipher_ctx_reset(mbedtls_cipher_context_t *ctx, const uint8_t *iv_= buf)
+cipher_ctx_rese= t(cipher_ctx_t *ctx, const uint8_t *iv_buf)
{- if (!mbed_ok(mbedtls_cipher_re= set(ctx)))
- {
- return 0;
- }
+ psa_status_t status =3D 0;
- if (!mbed_ok(m= bedtls_cipher_set_iv(ctx, iv_buf, (size_t)mbedtls_cipher_get_iv_size(ctx)))= )
+ if (cipher_ctx= _mode_aead(ctx))
{
- return 0;
+ if (psa_aead_abort(&ctx->operation.aead) != =3D PSA_SUCCESS)
+ = {
+ r= eturn 0;
+ }= span>
+
+ if (cipher_ctx_direction(ctx) = =3D=3D OPENVPN_OP_ENCRYPT)
+ {
+ = status =3D psa_aead_encrypt_setup(&ctx->operation.aead, ctx= ->key, ctx->cipher_info->psa_alg);
+ }
+ else if (cipher_ctx_direction(ctx) =3D=3D OPENVPN_OP_= DECRYPT)
+ {= span>
+ status = =3D psa_aead_decrypt_setup(&ctx->operation.aead, ctx->key, ctx-&g= t;cipher_info->psa_alg);
+ }
+ = else
+ {=
+ return = 0;
+ }<= br>+
+ if (status !=3D PSA_SUCCESS)
+ {
+ return 0;
+ }
+
+ status =3D psa_aead_set_nonce(&ctx->operation.aead,= iv_buf, ctx->cipher_info->iv_bytes);
+ if (status !=3D PSA_SUCCESS)+ {
+ return 0;
+ }
+ }
+ else
+ = {
+ if (psa= _cipher_abort(&ctx->operation.cipher) !=3D PSA_SUCCESS)+ {
+ return 0;
+ }
+
+ if (cipher_ctx_direction(ctx) =3D=3D OPENVPN_OP_ENCRYPT)
+ {+ status =3D psa_cipher= _encrypt_setup(&ctx->operation.cipher, ctx->key, ctx->cipher_i= nfo->psa_alg);
+ = }
+ else= if (cipher_ctx_direction(ctx) =3D=3D OPENVPN_OP_DECRYPT)
+ {
+ status =3D psa_cipher_decrypt_setup= (&ctx->operation.cipher, ctx->key, ctx->cipher_info->psa_al= g);
+ }=
+ else+ {
+ return 0;
+ }
+
+ if (status !=3D PSA_SUCCESS)
+ {
+ return 0;
+ }
+
+ statu= s =3D psa_cipher_set_iv(&ctx->operation.cipher, iv_buf, ctx->ciph= er_info->iv_bytes);
+ if (status !=3D PSA_SUCCESS)
+ {
+ return 0;
+ }
}
return 1;
}
+/* We rely on the caller to ensure that= the destination buffer has enough room,
+ * but Mbed TLS always wants a size for the destinatio= n buffer. This function
+ * calculates the minimum necessary size for a given cipher and input l= ength.
+ *
= + * This funcion assumes that s= rc_len has been checked to be >=3D 0. */
+static size_t
+needed_dst_size(const cipher_ctx_t *ctx, int src_len)
+{
+ int mode =3D cipher_ctx_mode(ctx);
+ if (mode =3D=3D PS= A_ALG_CTR || mode =3D=3D PSA_ALG_GCM || mode =3D=3D PSA_ALG_CHACHA20_POLY13= 05)
+ {
= + /* These algorithms ar= e based on a keystream, so the input and output
+ * length are always equal. */+ return (size_t)src_l= en;
+ }
= + else
+ {
+ /* These algorithms are block-based. The number= of output blocks that are
+ * produced is at most 1 + src_len / block_size. */+ size_t block_size = =3D (size_t)cipher_ctx_block_size(ctx);
+ size_t max_blocks =3D 1 + (size_t)src_len / blo= ck_size;
+ ret= urn max_blocks * block_size;
+ }
+}
+
int= span>
cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src,= int src_len)
{
- if (src_len > SIZE_MAX)
+ if (src_len < 0 || !cipher_ctx_mode_aead(ctx))=
{
return 0;
= }
- if (!mbed_ok(mbedtls_cipher_update_ad(ctx, src, src_len)))
+ if (psa_aead_update_ad(&a= mp;ctx->operation.aead, src, (size_t)src_len) !=3D PSA_SUCCESS) {
return 0;
}
+ return 1;+}
+
+int
+cipher= _ctx_update(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, in= t src_len)
+{<= br>+ if (src_len < 0)
+ {
+ return 0;
+ }
+
+ size_t dst_size =3D needed_dst_size(ctx, src_len);
+ size_t psa_output_len =3D 0;
+ psa_status_t status = =3D 0;
++ if (cipher_ctx_mode_aead(ctx= ))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ status =3D psa_aead_upd= ate(&ctx->operation.aead, src, (size_t)src_len, dst, dst_size, &= psa_output_len);
+ = }
+ else+ {
+ status =3D psa_cipher_update(&= ;ctx->operation.cipher, src, (size_t)src_len, dst, dst_size, &psa_ou= tput_len);
+ }
+
+ if (status !=3D PSA_SUCCESS)+ {
+ return 0;
+ }
+
= + if (psa_output_len > INT_MAX)
+ {
+ return 0;
+ }
+ *dst_len= =3D (int)psa_output_len;
return 1;<= /span>
}
int
-cipher_ctx_update(mbedtls_cipher_contex= t_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src,
- int src_len)+cipher_ctx_final(cipher_ctx_t *c= tx, uint8_t *dst, int *dst_len)
{
- size_t s_dst_len =3D *dst_len;
<= span style=3D"color: hsl(120, 100%, 40%);">+ size_t dst_size =3D needed_= dst_size(ctx, 0);
+ = size_t psa_output_len =3D 0;
+ psa_status_t status =3D 0;
- if (!mbed_ok(mbedtls_cipher_updat= e(ctx, src, (size_t)src_len, dst, &s_dst_len)))
+ if (cipher_ctx_mode_aead(ctx))=
{
- = return 0;
+ = size_t actual_tag_size =3D 0;
+ status =3D psa_aead_finish(&ctx->operation.aead,<= /span>
+ = dst,
+ = dst_size,
+ &psa_output_len,<= /span>
+ = ctx->tag,
+ (size_t)OPENVPN_AEAD_TAG_LENGTH,
+ = &actual_tag_size);
+ if (status !=3D PSA_SUCCESS || psa_output_len > (si= ze_t)INT_MAX || actual_tag_size !=3D (size_t)OPENVPN_AEAD_TAG_LENGTH)
+ {
+ return 0;+ }
+ ctx->aead_finished =3D true;= span>
+ }
+ else
+ {
+ status =3D psa_cipher_finish(&ctx->operation.= cipher, dst, dst_size, &psa_output_len);
+ if (status !=3D PSA_SUCCESS || psa_output_= len > (size_t)INT_MAX)
+ {
+ = return 0;
+ = }
}
- *dst_len =3D s_dst_len;
+ *dst_len =3D (int)psa_output_len;
return 1;
}
int
-cipher_ctx_final(mbedtls_cipher_context_t *ctx, uint8_t *dst, int *= dst_len)
+cipher_ctx_= final_check_tag(cipher_ctx_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag= , size_t tag_len)
{
- size_t s_dst_len =3D *dst_len;
-
- if (!mbed_ok(mbedtls_cipher_finish(ctx, dst, &s_dst_len)))= span>
+ if (cipher_ctx_di= rection(ctx) !=3D OPENVPN_OP_DECRYPT || !cipher_ctx_mode_aead(ctx))<= br> {
return 0;
}= span>
- *= dst_len =3D s_dst_len;
+ size_t psa_output_len =3D 0;
+ psa_status_t status =3D 0;
+
+ status =3D psa_aead_verify(&ctx->operation.aead, dst, 0,= &psa_output_len, tag, tag_len);
+ if (status !=3D PSA_SUCCESS || psa_output_len > (si= ze_t)INT_MAX)
+ {<= /span>
+ return 0;
+ }
+ *dst_len =3D (int)psa_output_len= ;
return 1;
}
-int
-cipher_ctx_final_check_tag(= mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
- = size_t tag_len)
+s= tatic const md_info_t md_info_table[] =3D {
+ /* TODO: Fill out table. */
+ { "MD5", PSA_ALG_MD5 },= span>
+ { "SHA1"= ;, PSA_ALG_SHA_1 },
+= { "SHA256", PSA_ALG_SHA_256 },
+};
+const size_t md_info_table_entries =3D sizeof(md_info_table) / size= of(md_info_t);
+
+static const md_info_t *=
+md_get(const char *= digest_name)
{
- size_t olen =3D 0;
-
- if (MB= EDTLS_DECRYPT !=3D mbedtls_cipher_get_operation(ctx))
+ for (size_t i =3D 0; i < md_info_t= able_entries; i++)
{
- return 0;
+ if (strcmp(digest_name, md_info_table[i].name) = =3D=3D 0)
+ {<= /span>
+ return &= amp;md_info_table[i];
+ }
}
-
- if (= tag_len > SIZE_MAX)
= - {
- return = 0;
- }-
- if (!mbed_ok(mbedtls_cipher_finish(ctx, dst, &ol= en)))
- {
= - msg(D_CRYPT_ERRORS, &quo= t;%s: cipher_ctx_final() failed", __func__);
- return 0;
- }
-
- if (olen = > INT_MAX)
- {
- return 0;=
- }
- *dst_len =3D olen;
-
- if (!mbed_ok(mbedtls_cipher_check_tag(ctx, (const unsigned = char *)tag, tag_len)))
= - {
- return = 0;
- }-
- return 1;
-}
--#if defined(__GNUC__) || defin= ed(__clang__)
-#pragma = GCC diagnostic pop
-#en= dif
-
-/*
- *
- *= Generic message digest information functions
- *
- */
--
-static const mbedtls_md_info_t *
-md_get(const char *digest)
-{
- const mbedtls_md_info_t *md =3D NULL;
- ASSERT(digest);
-
- md =3D mbedtls_md_info_from_string(digest);- if (!md)
- {
- msg(M_FATAL, "Message hash algorithm '%s= 39; not found", digest);
- }
- if (= mbedtls_md_get_size(md) > MAX_HMAC_KEY_LENGTH)
- {
- msg(M_FATAL,
- "Message hash algorithm '%s' uses a def= ault hash size (%d bytes) which is larger than " PACKAGE_NAME- "'s curre= nt maximum hash size (%d bytes)",
- digest, mbedtls_md_get_size(md), MAX_HMAC_KEY_= LENGTH);
- }<= br>- return md;+ return NULL;
= }
bool
md_valid(const c= har *digest)
{
- const mbedtls_md_info_t *md =3D mbedtls_md_info_from_string(d= igest);
+ const md= _info_t *md =3D md_get(digest);
return md !=3D NULL;= span>
}
const char *
md_kt_name(const char *mdname)
{
- if (!strcmp("none", mdnam= e))
+ if (strcmp(&= quot;none", mdname) =3D=3D 0)
{
= return "[null-digest]";
}
= - const mbedtls_md_info_t *kt = =3D md_get(mdname);
- = return mbedtls_md_get_name(kt);
+ const md_info_t *md =3D md_get(mdname);
+ if (md =3D=3D NULL)+ {
+ return NULL;
+ }
+ return md->name;
}
=
unsigned char
md_kt_size(const char *mdn= ame)
{
- if (!strcmp("none", mdname))
+ if (strcmp("none", mdname) =3D=3D 0)=
{
return 0;
= }
- const mbedt= ls_md_info_t *kt =3D md_get(mdname);
- return mbedtls_md_get_size(kt);
+ const md_info_t *md_info =3D md_get(mdna= me);
+ if (md_info= =3D=3D NULL)
+ {<= /span>
+ return 0;
+ }
+ return (unsigned char)PSA_HASH_L= ENGTH(md_info->psa_alg);
}
= -/*
- *
- * Generic message digest functions
- *
-= */
+md_ctx_t *
+md_ctx_new(void)+{
+ md_ctx_t *ctx;
+ ALLOC_OBJ_CLEAR(ctx, md_ctx_t);
<= span style=3D"color: hsl(120, 100%, 40%);">+ return ctx;
+}
int
md_full(const char *mdname, const uint8_t *src, int = src_len, uint8_t *dst)
{
- const mbedtls_md_info_t *kt =3D md_get(mdname);
- return 0 =3D=3D mbedt= ls_md(kt, src, src_len, dst);
-}
+ const = md_info_t *md =3D md_get(mdname);
+ if (md =3D=3D NULL || src_len < 0)
+ {
+ return 0;
+ }
-mbedtls_md_context_t *
-md_ctx_new(void)
-{
- = mbedtls_md_context_t *ctx;
- ALLOC_OBJ_CLEAR(ctx, mbedtls_md_context_t);
- return ctx;
+ /* We depend on the caller to ensure that = dst has enough room for the hash,
+ * so we just tell PSA that it can hold the appropriate a= mount of bytes. */
+ = size_t dst_size =3D PSA_HASH_LENGTH(md->psa_alg);
+ size_t hash_length =3D 0;
+
+ psa_status_t status =3D psa_hash_compute(md-= >psa_alg, src, (size_t)src_len, dst, dst_size, &hash_length);=
+ if (status !=3D PSA_SU= CCESS || hash_length !=3D dst_size)
+ {
+ return 0;
+= }
+ return 1;<= /span>
}
void
-md_ctx_free(mbedtls_md_context_t *ctx)=
+md_ctx_free(md_ctx_= t *ctx)
{
free(ctx);
= }
void
-md_ctx_init(mbedtls_md_context_t *ctx, const char *mdn= ame)
+md_ctx_init(md_= ctx_t *ctx, const char *mdname)
{
- const mbedtls_md_info_t *kt =3D md_get(mdn= ame);
- ASSERT(NULL = !=3D ctx && NULL !=3D kt);
+ const md_info_t *md_info =3D md_get(mdname);
<= span style=3D"color: hsl(120, 100%, 40%);">+ ASSERT(ctx !=3D NULL &&= amp; md_info !=3D NULL);
- mbedtls_md_init(ctx);
- ASSERT(0 =3D=3D mbedtls_md_setup(ctx, kt, 0));=
- ASSERT(0 =3D=3D m= bedtls_md_starts(ctx));
+ ctx->md_info =3D md_info;
+ ASSERT(psa_hash_setup(&ctx->operation, md_info-= >psa_alg) =3D=3D PSA_SUCCESS);
}
void
-md_c= tx_cleanup(mbedtls_md_context_t *ctx)
+md_ctx_cleanup(md_ctx_t *ctx)
{- mbedtls_md_free(ctx);
+ ASSERT(psa_hash_abor= t(&ctx->operation) =3D=3D PSA_SUCCESS);
}
int
-md_ctx_size(const mbedtls_md_context_t *ctx)
+md_ctx_size(const md_ctx_t *ctx) {
- if (NUL= L =3D=3D ctx)
+ if= (ctx =3D=3D NULL)
{
return 0= ;
}
- return (int)mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx));
+ return (int)PSA_HASH= _LENGTH(ctx->md_info->psa_alg);
}
<= /span>
void
-= md_ctx_update(mbedtls_md_context_t *ctx, const uint8_t *src, size_t src_len= )
+md_ctx_update(md_c= tx_t *ctx, const uint8_t *src, size_t src_len)
{
- ASSERT(0 =3D=3D mbedtls_md_= update(ctx, src, src_len));
+ ASSERT(psa_hash_update(&ctx->operation, src, src_len) = =3D=3D PSA_SUCCESS);
}
= void
-md_ctx_final(mbed= tls_md_context_t *ctx, uint8_t *dst)
+md_ctx_final(md_ctx_t *ctx, uint8_t *dst)
= {
- ASSERT(0 =3D=3D= mbedtls_md_finish(ctx, dst));
- mbedtls_md_free(ctx);
+ /* We depend on the caller to ensure that dst has enough= room for the hash,
+= * so we just tell PSA that it can hold the appropriate amount of bytes= . */
+ size_t dst_= size =3D PSA_HASH_LENGTH(ctx->md_info->psa_alg);
+ size_t hash_length =3D 0;
= +
+ ASSERT(psa_hash_finish(&ctx->operation= , dst, dst_size, &hash_length) =3D=3D PSA_SUCCESS);
+ ASSERT(hash_length =3D=3D dst_size)= ;
}
-
-/*= span>
- *
- * Generic HMAC functions
- *
- */
-=
-
-/*
- * TODO: re-enable dmsg for crypto debug
- */
-
-mbedtl= s_md_context_t *
+hma= c_ctx_t *
hmac_ctx_new(void)
{
= - mbedtls_md_context_t *ctx;= span>
- ALLOC_OBJ(ctx, mbed= tls_md_context_t);
+ = hmac_ctx_t *ctx;
+= ALLOC_OBJ_CLEAR(ctx, hmac_ctx_t);
return ctx;
}
void
-hmac_ctx_free(mbedtls_md_context_t *ctx)= span>
+hmac_ctx_free(hmac_ct= x_t *ctx)
{
free(ctx);}
= -void
-hmac_ctx_init(mb= edtls_md_context_t *ctx, const uint8_t *key, const char *mdname)
= +static void
+hmac_ctx_init_with_arbitrary_key_lengt= h(hmac_ctx_t *ctx, const uint8_t *key, size_t key_len, const md_info_t *md_= info)
{
- const mbedtls_md_info_t *kt =3D md_get(mdname);
- ASSERT(NULL !=3D kt && NULL != =3D ctx);
+ ctx-&g= t;md_info =3D md_info;
+ psa_set_key_type(&ctx->key_attributes, PSA_KEY_TYPE_HMAC);
+ psa_set_key_algori= thm(&ctx->key_attributes, PSA_ALG_HMAC(md_info->psa_alg));=
+ psa_set_key_usage_flag= s(&ctx->key_attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
- mbedtls_md_init= (ctx);
- int key_len= =3D mbedtls_md_get_size(kt);
- ASSERT(0 =3D=3D mbedtls_md_setup(ctx, kt, 1));
- ASSERT(0 =3D=3D mbedtls_md_hmac_st= arts(ctx, key, key_len));
+ if (psa_import_key(&ctx->key_attributes, key, key_len, &am= p;ctx->key) !=3D PSA_SUCCESS)
+ {
+ = msg(M_FATAL, "psa_import_key failed");
+ }
- /* make sure we used a big enough= key */
- ASSERT(mbe= dtls_md_get_size(kt) <=3D key_len);
+ ASSERT(psa_mac_sign_setup(&ctx->operation, ct= x->key, PSA_ALG_HMAC(md_info->psa_alg)) =3D=3D PSA_SUCCESS); }
void
-hmac_ctx_cleanup(mbedtls_md_context_t *ctx)
+hmac_ctx_init(hmac_ctx= _t *ctx, const uint8_t *key, const char *mdname)
{<= br>- mbedtls_md_free(ctx);
+ const md_info_t *md= _info =3D md_get(mdname);
+ ASSERT(ctx !=3D NULL && key !=3D NULL && md_info = !=3D NULL);
+<= br>+ hmac_ctx_init_with_arbi= trary_key_length(ctx, key, PSA_HASH_LENGTH(md_info->psa_alg), md_info);<= /span>
+}
+
+void
+hmac_ctx_cleanup(hmac_ctx_t *ctx)
+{
+ = ASSERT(psa_mac_abort(&ctx->operation) =3D=3D PSA_SUCCESS);=
+ ASSERT(psa_destroy_key= (ctx->key) =3D=3D PSA_SUCCESS);
}
int
-hmac= _ctx_size(mbedtls_md_context_t *ctx)
+hmac_ctx_size(hmac_ctx_t *ctx)
{- if (NULL =3D=3D ctx)
- {
- return 0;
- }
- return mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx));<= /span>
+ return (int)PSA_= HASH_LENGTH(ctx->md_info->psa_alg);
}
void
-hmac_ctx_reset(mbedtls_md_context_t *ctx)
+hmac_ctx_reset(hmac_ctx_t *ctx)
= {
- ASSERT(0 =3D=3D = mbedtls_md_hmac_reset(ctx));
+ ASSERT(psa_mac_abort(&ctx->operation) =3D=3D PSA_SUCCES= S);
+ ASSERT(psa_m= ac_sign_setup(&ctx->operation, ctx->key, PSA_ALG_HMAC(ctx->md_= info->psa_alg)) =3D=3D PSA_SUCCESS);
}
=
void
-hmac_ctx_update(mbedtls_md_context_t *ctx, const uint8_t *src, int src_le= n)
+hmac_ctx_update(h= mac_ctx_t *ctx, const uint8_t *src, int src_len)
{<= br>- ASSERT(0 =3D=3D mbedtls_m= d_hmac_update(ctx, src, src_len));
+ ASSERT(src_len >=3D 0);
+ ASSERT(psa_mac_update(&ctx->operation,= src, (size_t)src_len) =3D=3D PSA_SUCCESS);
}
void
-hmac_ctx_final(mbedtls_md_context_t *ctx, uint8_t *dst)+hmac_ctx_final(hmac_ctx_t *ctx, u= int8_t *dst)
{
- ASSERT(0 =3D=3D mbedtls_md_hmac_finish(ctx, dst));
= + /* We depend on the caller= to ensure that dst has enough room for the hash,
+ * so we just tell PSA that it can hold t= he appropriate amount of bytes. */
+ size_t dst_size =3D PSA_HASH_LENGTH(ctx->md_info->= psa_alg);
+ size_t= hmac_length =3D 0;
+=
+ ASSERT(psa_mac_= sign_finish(&ctx->operation, dst, dst_size, &hmac_length) =3D=3D= PSA_SUCCESS);
+ A= SSERT(hmac_length =3D=3D dst_size);
}
-int
-memcmp_constant_time(const void *a, const = void *b, size_t size)
-= {
- /* mbed TLS has = a no const time memcmp function that it exposes
- * via its APIs like OpenSSL does with CRYPTO= _memcmp
- * Adapt t= he function that mbedtls itself uses in
- * mbedtls_safer_memcmp as it considers that to be sa= fe */
- volatile con= st unsigned char *A =3D (volatile const unsigned char *)a;
- volatile const unsigned char *B = =3D (volatile const unsigned char *)b;
- volatile unsigned char diff =3D 0;
-
- for (size_t i =3D 0; i < size; i++)
- {
- unsigned char x =3D A[i], y =3D B[i];
<= span style=3D"color: hsl(0, 100%, 40%);">- diff |=3D x ^ y;- }
-
- return diff;
-}
-
-#if defined(__GNUC__) || defined(__cla= ng__)
-#pragma GCC diag= nostic push
-#pragma GC= C diagnostic ignored "-Wconversion"
-#endif
-
/*
* Generate the hash require= d by for the \c tls1_PRF function.
*
@@ -= 1004,8 +832,8 @@
* @param olen Length of the out= put buffer
*/
static void-tls1_P_hash(const mbedtls_md_info_t = *md_kt, const uint8_t *sec, size_t sec_len, const uint8_t *seed,
= - size_t seed_len, uin= t8_t *out, size_t olen)
+tls1_P_hash(const md_info_t *md_info, const uint8_t *sec, size_t sec_le= n, const uint8_t *seed,
+ int seed_len, uint8_t *out, size_t olen)
{=
struct gc_arena gc =3D gc_new();
= uint8_t A1[MAX_HMAC_KEY_LENGTH];
@@ -1023,18 +851,13 @@
dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash sec: %s",= format_hex(sec, sec_len, 0, &gc));
dmsg(D_SHOW_KE= Y_SOURCE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &= amp;gc));
- unsigned int chunk =3D mbedtls_md_get_size(md_kt);- unsigned int A1_len =3D mbedtls_= md_get_size(md_kt);
+= unsigned int chunk =3D (unsigned int)PSA_HASH_LENGTH(md_info->psa_al= g);
+ unsigned int= A1_len =3D (unsigned int)PSA_HASH_LENGTH(md_info->psa_alg);
<= span>
/* This is the only place where we init an HMAC= with a key that is not
* equal to its size, therefor= e we init the hmac ctx manually here */
- mbedtls_md_init(ctx);
- ASSERT(0 =3D=3D mbedtls_md_setup(ctx, md_kt, 1));=
- ASSERT(0 =3D=3D m= bedtls_md_hmac_starts(ctx, sec, sec_len));
-
-= mbedtls_md_init(ctx_tmp);
- ASSERT(0 =3D=3D mbedtls_md_setup(ctx_tmp, md_kt, 1));
- ASSERT(0 =3D=3D mbedtls_md_= hmac_starts(ctx_tmp, sec, sec_len));
+ hmac_ctx_init_with_arbitrary_key_length(ctx, sec, sec_= len, md_info);
+ h= mac_ctx_init_with_arbitrary_key_length(ctx_tmp, sec, sec_len, md_info);
hmac_ctx_update(ctx, seed, seed_len);= span>
hmac_ctx_final(ctx, A1);
@@ -1045,7 +86= 8,7 @@
hmac_ctx_reset(ctx_tmp);
= hmac_ctx_update(ctx, A1, A1_len);
hmac_ctx_= update(ctx_tmp, A1, A1_len);
- hmac_ctx_update(ctx, seed, seed_len);
+ hmac_ctx_update(ctx, seed, (int)s= eed_len);
if (olen > chunk)= span>
{
@@ -1094,9 +917,15 @@
<= span> ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *s= ec, size_t slen, uint8_t *out1,
size_t olen)<= /span>
{
+ = const md_info_t *md5 =3D md_get("MD5");
+ const md_info_t *sha1 =3D md_get(&quo= t;SHA1");
+
+ if (label_len > (= size_t)INT_MAX)
+ = {
+ return fal= se;
+ }
= +
struct g= c_arena gc =3D gc_new();
- const md_kt_t *md5 =3D md_get("MD5");
- const md_kt_t *sha1 =3D md_get("S= HA1");
uint8_t *out2 =3D (uint8= _t *)gc_malloc(olen, false, &gc);
@@ = -1105,8 +934,8 @@
const uint8_t *S2 =3D &(sec[len]= );
len +=3D (slen & 1); /* add for odd, make longe= r */
= - tls1_P_hash(md5, S1, len, label, label_len, out1, olen);- tls1_P_hash(sha1, S2, len, labe= l, label_len, out2, olen);
+ tls1_P_hash(md5, S1, len, label, (int)label_len, out1, olen);
+ tls1_P_hash(sha1, = S2, len, label, (int)label_len, out2, olen);
for (size_t i =3D 0; i < olen; i++)
{
@@ -1121,8 +950,204 @@
return true;=
}
-#if defined(__GNUC__) || defined(__clang__)
-#pragma GCC diagnostic pop
+void
+crypto_init_lib(void)
+{
+}
+
+void
+crypto_uninit_lib(void)
+{
+}
+<= /span>
+void
+crypto_clear_error(void)
= +{
+}
+
+bool
+mbed_log_err(unsigned int= flags, int errval, const char *prefix)
+{
+ if (0 !=3D errval)
+ {
+ c= har errstr[256];
+ = mbedtls_strerror(errval, errstr, sizeof(errstr));
+
+ if (NULL =3D=3D prefix)
+ {
+ prefix =3D "mbed TLS error";=
+ }
+ msg(flags, "%s: %s"= ;, prefix, errstr);
+= }
++ return 0 =3D=3D errval;
+}
+
+bool
+= mbed_log_func_line(unsigned int flags, int errval, const char *func, int li= ne)
+{+ char prefix[256]; +
+ if (snprintf(prefix, sizeof(prefix), "%s:%= d", func, line) >=3D sizeof(prefix))
+ {
+ return mbed_log_err(flags, errval, func);+ }
+
+ return mbed_log_err(flags, errval, prefix);
+}
+
+i= nt
+memcmp_constant_t= ime(const void *a, const void *b, size_t size)
+{
+ return mbedtls_ct_memcmp(a, b, size);
+}
+
+void= span>
+show_available_cipher= s(void)
+{
= + /* Mbed TLS 4 does not cur= rently have a mechanism to discover available
+ * ciphers. We instead print out the ciphers = from cipher_info_table. */
+
+#ifndef ENAB= LE_SMALL
+ printf(= "The following ciphers and cipher modes are available for use\n"<= /span>
+ "wit= h " PACKAGE_NAME ". Each cipher shown below may be used as a\n&q= uot;
+ &quo= t;parameter to the --data-ciphers (or --cipher) option. Using a\n"
+ "GCM o= r CBC mode is recommended. In static key mode only CBC\n"
<= span style=3D"color: hsl(120, 100%, 40%);">+ "mode is allowe= d.\n\n");
#endif
-#endif /* ENABLE_CRYPTO_MBEDTLS */
+ for (size_t i =3D 0; = i < cipher_info_table_entries; i++)
+ {
+ const cipher_info_t *info =3D &cipher_info_table[i];
+ const char *name = =3D info->name;
+ = if (!cipher_kt_insecure(name) && (cipher_kt_mode_aead(name) = || cipher_kt_mode_cbc(name)))
+ {
+= print_cipher(name);
+ }
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ printf("\nThe followin= g ciphers have a block size of less than 128 bits, \n"
+ "and are therefore = deprecated. Do not use unless you have to.\n\n");
+ for (size_t i =3D 0; i < cipher_= info_table_entries; i++)
+ {
+ c= onst cipher_info_t *info =3D &cipher_info_table[i];
+ const char *name =3D info->n= ame;
+ if (cip= her_kt_insecure(name) && (cipher_kt_mode_aead(name) || cipher_kt_mo= de_cbc(name)))
+ = {
+ pri= nt_cipher(name);
+ = }
+ }<= br>+ printf("\n");=
+}
+
+void
+show_available_digests(void)
+{
+ /= * Mbed TLS 4 does not currently have a mechanism to discover available
+ * message digests. = We instead print out the digests from md_info_table. */
+
+#ifndef ENABLE_SMALL
+ printf("The following message digests are availa= ble for use with\n" PACKAGE_NAME
+ ". A message digest is used in conjuncti= on with\n"
+ = "the HMAC function, to authenticate received packets.\n"= span>
+ "You = can specify a message digest as parameter to\n"
+ "the --auth option.\n\n&q= uot;);
+#endif=
+
+ for (size_t i =3D 0; i < md_info_table= _entries; i++)
+ {=
+ const md_in= fo_t *info =3D &md_info_table[i];
+ printf("%s %d bit default key\n", info-= >name,
+ = (unsigned char)PSA_HASH_LENGTH(info->psa_alg) * 8);
+ }
+ printf("\n");
+}
+
+voi= d
+show_available_eng= ines(void)
+{<= br>+ printf("Sorry, mbe= d TLS hardware crypto engine functionality is not "
+ "available\n");= span>
+}
+
+bool
+crypto_pem_encode(const char *name, struct buffer *dst, const struct buff= er *src,
+ = struct gc_arena *gc)
+{
+ /* 100= 0 chars is the PEM line length limit (+1 for tailing NUL) */+ char header[1000 + 1] =3D { 0 = };
+ char footer[1= 000 + 1] =3D { 0 };
+=
+ if (snprintf(he= ader, sizeof(header), "-----BEGIN %s-----\n", name) >=3D sizeo= f(header))
+ {
+ return false;= span>
+ }
+ if (snprintf(footer, sizeof(foo= ter), "-----END %s-----\n", name) >=3D sizeof(footer))<= br>+ {
+ return false;
+ }
+
+ size_t out_len =3D 0;
+ if (MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL
+ !=3D mbedtls_pem_write_buffer(hea= der, footer, BPTR(src), BLEN(src), NULL, 0, &out_len))
+ {
+ return false;
+ }
+
+ /*= We set the size buf to out_len-1 to NOT include the 0 byte that
= + * mbedtls_pem_write_buffe= r in its length calculation */
+ *dst =3D alloc_buf_gc(out_len, gc);
+ if (!mbed_ok(mbedtls_pem_write_buffer(he= ader, footer, BPTR(src), BLEN(src), BPTR(dst),
+ BCAP(d= st), &out_len))
+= || !(out_len < INT_MAX && out_len > 1)+ || !buf_inc_len(dst, (int)= out_len - 1))
+ {<= /span>
+ CLEAR(*dst);=
+ return fals= e;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ return true;
+}
+
+bool
= +crypto_pem_decode(const char *= name, struct buffer *dst, const struct buffer *src)
+{
+ /* 1000 chars is the PEM line length limit (+1 for tail= ing NUL) */
+ char= header[1000 + 1] =3D { 0 };
+ char footer[1000 + 1] =3D { 0 };
+
+ if (snprintf(header, sizeof(header), "-----BEGIN %s-----&qu= ot;, name) >=3D sizeof(header))
+ {
= + return false;
+ }
+ if (snp= rintf(footer, sizeof(footer), "-----END %s-----", name) >=3D s= izeof(footer))
+ {=
+ return fals= e;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ /* mbed TLS requires the src to be null-termin= ated */
+ /* alloc= ate a new buffer to avoid modifying the src buffer */
+ struct gc_arena gc =3D gc_new();
+ struct buffer input = =3D alloc_buf_gc(BLEN(src) + 1, &gc);
+ buf_copy(&input, src);
+ buf_null_terminate(&input);
+
+ size_t use_len =3D 0;
+ mbedtls_pem_context ctx =3D { 0 };=
+ bool ret =3D
+ mbed_ok(mbedtl= s_pem_read_buffer(&ctx, header, footer, BPTR(&input), NULL, 0, &= ;use_len));
+ size= _t buf_size =3D 0;
+ = const unsigned char *buf =3D mbedtls_pem_get_buffer(&ctx, &buf_s= ize);
+ if (ret &a= mp;& !buf_write(dst, buf, buf_size))
+ {
+ ret =3D false;
+ msg(M_WARN, "PEM decode error: destination buffer to= o small");
+ = }
+
+ mbedtls_pem_free(&ctx);
+ gc_free(&gc);
+ return ret;<= br>+}
+
+#endif /* MBEDTLS_VERSION_NUMBER >=3D 0x04000000 */
+#endif /* defined(ENABLE_CRYP= TO_MBEDTLS) */
diff --git a/src/openvpn/crypto_mbedtls.h b/= src/openvpn/crypto_mbedtls.h
index af71037..16d41e4 100644<= /span>
--- a/src/openvpn/crypto_mbedtls.h
+++ b/sr= c/openvpn/crypto_mbedtls.h
@@ -1,3 +1,4 @@
+
/*
* OpenVPN -- An application to securely tunnel IP networks* over a single TCP/UDP port, with support for SSL/TLS-ba= sed
@@ -23,85 +24,98 @@
= /**
* @file
- * Data Channel Cryptography mbed TLS-specific backend interface
+ * Data Channel Crypto= graphy backend interface using the TF-PSA-Crypto library
+ * part of Mbed TLS 4.
*/
-#ifndef CRYPTO_MBEDTLS_H_
-#define CRYPTO_MBEDTLS_H_
+#ifndef CRYPTO_MBEDTLS4_H_
+#define CRYPTO_MBEDTLS4_H_
<= span style=3D"color: hsl(0, 100%, 40%);">-#include <stdbool.h>=
-#include <mbedtls/cipher.= h>
-#include <mbe= dtls/md.h>
-#include= <mbedtls/ctr_drbg.h>
+#include <psa/crypto.h>
-/** Generic message digest key type %cont= ext. */
-typedef mbedtl= s_md_info_t md_kt_t;
-<= /span>
-/** Generic cipher %co= ntext. */
-typedef mbed= tls_cipher_context_t cipher_ctx_t;
-
-/** Gene= ric message digest %context. */
-typedef mbedtls_md_context_t md_ctx_t;
-
-/** Generic HMAC %context. */
-typedef mbedtls_md_context_t hmac_ctx_t;
-
-/* Use a dummy type for the provider */
-typedef void provider_t;
+#include "integer.h"<= br>
/** Maximum length of an IV */
-#define OPENVPN_MAX_IV_LENGTH MBEDTLS_= MAX_IV_LENGTH
+#defin= e OPENVPN_MAX_IV_LENGTH 16
/** Cipher is= in CBC mode */
-#defin= e OPENVPN_MODE_CBC MBEDTLS_MODE_CBC
+#define OPENVPN_MODE_CBC PSA_ALG_CBC_PKCS7
=
/** Cipher is in OFB mode */
-#define OPENVPN_MODE_OFB MBEDTLS_MODE_OFB<= br>+#define OPENVPN_MODE_OFB PS= A_ALG_OFB
/** Cipher is in CFB mode */= span>
-#define OPENVPN_MODE_CF= B MBEDTLS_MODE_CFB
+#= define OPENVPN_MODE_CFB PSA_ALG_CFB
/** = Cipher is in GCM mode */
-#define OPENVPN_MODE_GCM MBEDTLS_MODE_GCM
+#define OPENVPN_MODE_GCM PSA_ALG_GCM
-typedef mbedtls_o= peration_t crypto_operation_t;
+typedef int crypto_operation_t;
/** Cipher should encrypt */
-#define OPENVPN_OP_ENCRYPT MBEDTLS_ENCRYPT
+#define OPENVPN_OP_ENCRYPT 0
/** Cipher should decrypt */
-#define OPENVPN_OP_DECRYPT MBEDTLS_DECRYPT<= /span>
+#define OPENVPN_OP_D= ECRYPT 1
#define MD4_DIGEST_LENGTH 16=
#define MD5_DIGEST_LENGTH 16
#define = SHA_DIGEST_LENGTH 20
#define SHA256_DIGEST_LENGTH 32
-/**
- * Returns a singleton i= nstance of the mbed TLS random number generator.
- *
- * For PolarSSL/mbed TLS 1.1+, this is the CTR_DRBG random number g= enerator. If it
- * has= n't been initialised yet, the RNG will be initialised using the default=
- * entropy sources. A= side from the default platform entropy sources, an
- * additional entropy source, the HAVEGE rando= m number generator will also be
- * added. During initialisation, a personalisation string will be= added based
- * on the= time, the PID, and a pointer to the random context.
- */
-mbedtls_ctr_drbg_context *rand_ctx_get(void);
+typedef void provider_t;
=
-#ifdef ENABLE_= PREDICTION_RESISTANCE
-= /**
- * Enable predicti= on resistance on the random number generator.
- */
-void rand_ctx_enable_prediction_resistance(void);
+typedef struct cipher_info+{
+ const char *name;
+ psa_key_type_t psa_key_type;
+ psa_algorithm_t psa_alg;=
+ int key_bytes;<= br>+ int iv_bytes;
+ int block_size;
+} cipher_info_t;
-#endif
+typedef union psa_cipher_or_a= ead_operation
+{
+ psa_cipher_operation= _t cipher;
+ psa_a= ead_operation_t aead;
+} cipher_operation_t;
+
+typedef struct = cipher_ctx
+{<= br>+ mbedtls_svc_key_id_t ke= y;
+ psa_key_attri= butes_t key_attributes;
+ const cipher_info_t *cipher_info;
+ bool aead_finished;
+ cipher_operation_t operation;
+ uint8_t tag[16];+} cipher_ctx_t;
+
+typedef struct md_info
+{
+ const char *name;
+ psa_algorithm_t psa_alg;
+} md_info_t;
+
+typedef stru= ct md_ctx
+{+ const md_info_t *md_info= ;
+ psa_hash_opera= tion_t operation;
+} = md_ctx_t;
+
+typedef struct hmac_ctx
+{
+ mbedtls_svc_key_id_t key;
<= span style=3D"color: hsl(120, 100%, 40%);">+ psa_key_attributes_t key_at= tributes;
+ const = md_info_t *md_info;
+= psa_mac_operation_t operation;
+} hmac_ctx_t;
/**<= br> * Log the supplied mbed TLS error, prefixed by supplied prefix.<= /span>
*
* @param flags Flags to indic= ate error type and priority.
- * @param errval mbed TLS error code to convert to error mess= age.
+ * @param errva= l mbed TLS error code.
* @param prefix Pref= ix to mbed TLS error message.
*
* @retu= rns true if no errors are detected, false otherwise.
@@ -11= 2,7 +126,7 @@
* Log the supplied mbed TLS error, prefixed= by function name and line number.
*
* = @param flags Flags to indicate error type and priority.
<= span style=3D"color: hsl(0, 100%, 40%);">- * @param errval mbed TLS = error code to convert to error message.
+ * @param errval mbed TLS error code.
= * @param func Function name where error was reported.
* @param line Line number where error was reported.<= /span>
*
@@ -142,7 +156,8 @@
*= @param errval mbed TLS error code to convert to error message.
*
* @returns true if no errors are detected= , false otherwise.
+ = * TODO: The log function has been removed, do something about it?
*/
#define mbed_ok(errval) mbed_log_func_line_lit= e(D_CRYPT_ERRORS, errval, __func__, __LINE__)
<= span style=3D"color: hsl(0, 100%, 40%);">-#endif /* CRYPTO_MBEDTLS_H_ */
+#endif /* CRYPTO_MBEDT= LS4_H_ */
diff --git a/src/openvpn/crypto_mbedtls_legacy.c = b/src/openvpn/crypto_mbedtls_legacy.c
new file mode 100644<= /span>
index 0000000..a991349
--- /dev/null=
+++ b/src/openvpn/crypto_mbedtls_legacy.c
@@ -0,0= +1,1133 @@
+/*
+ * OpenVPN -- An applica= tion to securely tunnel IP networks
+ * over a single TCP/UDP port, with support for= SSL/TLS-based
+ * = session authentication and key exchange,
+ * packet encryption, packet aut= hentication, and
+ * = packet compression.
+ *
+ * C= opyright (C) 2002-2026 OpenVPN Inc <sales@...515...>
+ * Copyright (C) 2010-2026 Sentyron= B.V. <openvpn@...2918...>
+ *
+ * = This program is free software; you can redistribute it and/or modify=
+ * it under the terms of = the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the ho= pe that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of= span>
+ * MERCHANTABILITY o= r FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.= span>
+ *
+ * You should have received a copy of= the GNU General Public License along
+ * with this program; if not, see <https://www.gnu.or= g/licenses/>.
+ */=
+
+/**
+ * @file
+ * Data Channel Cryptography mbed TLS-specific backend interface,=
+ * for mbed TLS 3.X= versions.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
<= span style=3D"color: hsl(120, 100%, 40%);">+#endif
+
+#include "syshead.h"
+
+#if defined(ENABLE_CRYPTO_MBEDTLS)
+#include <mbedtls/version.h>
+
+#if MBEDTLS_VERSION_NUMBER < 0x04000000
+
+#include "errlevel.h"
+#include "basic.h"
+#include "buffer.h"
+#include "crypto.h&q= uot;
+#include "= integer.h"
+#inc= lude "crypto_backend.h"
+#include "otime.h"
+#include "misc.h"
+
+#include <mbedtls/base64.h>
+#include <mbedtls/des.h>
+#include <mbedtls/error.h>
+#include <mbedtls/md5.= h>
+#include <m= bedtls/cipher.h>
+= #include <mbedtls/pem.h>
+
+#include= <mbedtls/entropy.h>
+#include <mbedtls/ssl.h>
+
+<= /span>
+/*
+ *
+ * Hardware engine support. Allows loading/unloading o= f engines.
+ *=
+ */
+
+void
+= crypto_init_lib_engine(const char *engine_name)
+{
+ msg(M_WARN, "Note: mbed TLS hardware crypto engine funct= ionality is not "
+ "available");
+}
+
+provider_t *= span>
+crypto_load_provider(= const char *provider)
+{
+ if (provider= )
+ {+ msg(M_WARN, "Note: = mbed TLS provider functionality is not available");
+ }
+ return NULL;
+}
+
+void+crypto_unload_provider(const cha= r *provname, provider_t *provider)
+{
+}= span>
+
+/*
+ *
= + * Functions related to the core crypto library
+ *
+ */
+
+void
+crypto_init_lib(void)
+{
+}
+
+void
<= span style=3D"color: hsl(120, 100%, 40%);">+crypto_uninit_lib(void)<= br>+{
+}
+
+void
+crypto_clear_error(voi= d)
+{
+}
+
+bool
+mbed_log_err= (unsigned int flags, int errval, const char *prefix)
+{
+ if (0 !=3D errval)
+ {
+ char errstr[256];
+ mbedtls_strerror(errval, errstr, sizeof(errstr));=
+
+ if (NULL =3D=3D prefix)+ {
+ prefix =3D "mbed TLS err= or";
+ }<= /span>
+ msg(flags, &= quot;%s: %s", prefix, errstr);
+ }
+
+ return 0 =3D= =3D errval;
+}=
+
+bool
+mbed_log_func_line(unsigned int flags, int errval, const cha= r *func, int line)
+{=
+ char prefix[256= ];
+
+ if (snprintf(prefix, sizeof(pref= ix), "%s:%d", func, line) >=3D sizeof(prefix))
+ {
+ return mbed_log_err(flags, errval, func);=
+ }+
+ return mbed_log_err(flags, errval, prefix);
+}
+
+
+#ifd= ef DMALLOC
+void
+crypto_init_dmalloc(void= )
+{
+ msg(M_ERR, "Error: dmalloc = support is not available for mbed TLS.");
+}
+#endif /* DMALLOC */
+
+const cip= her_name_pair cipher_name_translation_table[] =3D {
+ { "BF-CBC", "BLOWFISH-= CBC" },
+ { &= quot;BF-CFB", "BLOWFISH-CFB64" },
+ { "CAMELLIA-128-CFB", "CAME= LLIA-128-CFB128" },
+ { "CAMELLIA-192-CFB", "CAMELLIA-192-CFB128" },=
+ { "CAMELLI= A-256-CFB", "CAMELLIA-256-CFB128" }
+};
+const size_t cipher_name_translation_table_count =3D+ sizeof(cipher_name_trans= lation_table) / sizeof(*cipher_name_translation_table);
+
+void
+show_available_ciphers(void)
+{
+ con= st int *ciphers =3D mbedtls_cipher_list();
+
+#ifndef ENABLE_SMALL
+ printf("The following ciphers and cipher modes are available = for use\n"
+ = "with " PACKAGE_NAME ". Each cipher shown below may = be used as a\n"
= + "parameter to the --data-ciphers (or --cipher) option. Us= ing a\n"
+ = "GCM or CBC mode is recommended. In static key mode only CBC\n&q= uot;
+ &quo= t;mode is allowed.\n\n");
+#endif
+= span>
+ while (*ciphers != =3D 0)
+ {<= br>+ const mbedtls_ciphe= r_info_t *info =3D mbedtls_cipher_info_from_type(*ciphers);
+ const char *name =3D mbedtl= s_cipher_info_get_name(info);
+ if (info && name && !cipher_kt_insecure(n= ame)
+ &am= p;& (cipher_kt_mode_aead(name) || cipher_kt_mode_cbc(name)))
= + {
+ print_cipher(name);+ }
+ ciphers++;
+ }
+
+ printf("\nThe following ciphers have a block size of less than = 128 bits, \n"
+ = "and are therefore deprecated. Do not use unless you have t= o.\n\n");
+ c= iphers =3D mbedtls_cipher_list();
+ while (*ciphers !=3D 0)
+ {
+ const mbedtls_cipher_info_t *info =3D mbedtls_cipher_info_f= rom_type(*ciphers);
+= const char *name =3D mbedtls_cipher_info_get_name(info);
= + if (info && na= me && cipher_kt_insecure(name)
+ && (cipher_kt_mode_aead(name) || cip= her_kt_mode_cbc(name)))
+ {
+ = print_cipher(name);
+ }
+ = ciphers++;
+ }=
+ printf("\n= ");
+}
+
+void
+show_available_digests(void)
+{
+ const int *digests =3D mbedtls_md_list();
+
+#ifndef ENABLE_SMALL
+ printf("The following message digests are available for= use with\n" PACKAGE_NAME
+ ". A message digest is used in conjunction with= \n"
+ = "the HMAC function, to authenticate received packets.\n"+ "You can spe= cify a message digest as parameter to\n"
+ "the --auth option.\n\n");= span>
+#endif+
+ while (*digests !=3D 0)
+ {
+ const mbedtls_md_info_t *info =3D mbedtls_md_info_f= rom_type(*digests);
+=
+ if (info)= span>
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ printf("%s %d = bit default key\n", mbedtls_md_get_name(info),
+ mbedtls_md_get_size(in= fo) * 8);
+ }<= /span>
+ digests++;= span>
+ }
+ printf("\n");<= br>+}
+
+void
+show= _available_engines(void)
+{
+ printf(&q= uot;Sorry, mbed TLS hardware crypto engine functionality is not "
+ "availab= le\n");
+}
+
+#if defined(__GNUC__) || defined(__clang__)<= /span>
+#pragma GCC diagnost= ic push
+#pragma GCC = diagnostic ignored "-Wconversion"
+#endif
+
+bool
+crypto_pem_encode(const= char *name, struct buffer *dst, const struct buffer *src,
+ struct gc_arena *g= c)
+{
+ /* 1000 chars is the PEM line l= ength limit (+1 for tailing NUL) */
+ char header[1000 + 1] =3D { 0 };
+ char footer[1000 + 1] =3D { 0 };
+
+ if (snprintf(header, sizeof(header), = "-----BEGIN %s-----\n", name) >=3D sizeof(header))
<= span style=3D"color: hsl(120, 100%, 40%);">+ {
+ return false;
+ }
+ if (snprintf(footer, sizeof(footer), "-----END= %s-----\n", name) >=3D sizeof(footer))
+ {
+ return false;
+ }
+
+ size_t out_le= n =3D 0;
+ if (MBE= DTLS_ERR_BASE64_BUFFER_TOO_SMALL
+ !=3D mbedtls_pem_write_buffer(header, footer, BPTR(src= ), BLEN(src), NULL, 0, &out_len))
+ {
+ return false;
+ }
+<= br>+ /* We set the size buf = to out_len-1 to NOT include the 0 byte that
+ * mbedtls_pem_write_buffer in its length calcu= lation */
+ *dst = =3D alloc_buf_gc(out_len, gc);
+ if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(s= rc), BLEN(src), BPTR(dst),
+ BCAP(dst), &out_len))<= /span>
+ || !buf_inc_= len(dst, out_len - 1))
+ {
+ CLE= AR(*dst);
+ re= turn false;
+ }
+
+ return true;
+}
+
+bool= span>
+crypto_pem_decode(con= st char *name, struct buffer *dst, const struct buffer *src)+{
+ /* 1000 chars is the PEM line length limit (+1 f= or tailing NUL) */
+ = char header[1000 + 1] =3D { 0 };
+ char footer[1000 + 1] =3D { 0 };
+
+ if (snprintf(header, sizeof(header), "-----BEGIN %s= -----", name) >=3D sizeof(header))
+ {
+ return false;
+ }
+ = if (snprintf(footer, sizeof(footer), "-----END %s-----", name) &= gt;=3D sizeof(footer))
+ {
+ ret= urn false;
+ }
+
+ /* mbed TLS requires the src to be nu= ll-terminated */
+ = /* allocate a new buffer to avoid modifying the src buffer */+ struct gc_arena gc =3D gc_ne= w();
+ struct buff= er input =3D alloc_buf_gc(BLEN(src) + 1, &gc);
+ buf_copy(&input, src);+ buf_null_terminate(&input= );
+
+ size_t use_len =3D 0;
= + mbedtls_pem_context ctx = =3D { 0 };
+ bool = ret =3D
+ mbed= _ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(&input), NUL= L, 0, &use_len));
+ size_t buf_size =3D 0;
+ const unsigned char *buf =3D mbedtls_pem_get_buffer(&ctx, = &buf_size);
+ = if (ret && !buf_write(dst, buf, buf_size))
+ {
+ ret =3D false;
+ msg(M_WARN, "PEM decode error: destination= buffer too small");
+ }
+<= br>+ mbedtls_pem_free(&c= tx);
+ gc_free(&am= p;gc);
+ return re= t;
+}
+
+/*
+ *
+ * Random num= ber functions, used in cases where we want
+ * reasonably strong cryptographic random number gen= eration
+ * without d= epleting our entropy pool. Used for random
+ * IV values and a number of other miscellaneous ta= sks.
+ *+ */
+
+/*
+ * Initia= lise the given ctr_drbg context, using a personalisation string and an
+ * entropy gathering fun= ction.
+ */
+mbedtls_ctr_drbg_context *
+rand_ctx_get(void)
+{
+ static mbedtls_entropy_context ec =3D= { 0 };
+ static m= bedtls_ctr_drbg_context cd_ctx =3D { 0 };
+ static bool rand_initialised =3D false;
+
+ if (!rand_initialised)
+ {
+ struct gc_arena gc =3D gc_new();+ struct buffer pers_string = =3D alloc_buf_gc(100, &gc);
+
+ = /*
+ * Perso= nalisation string, should be as unique as possible (see NIST+ * 800-90 section 8.7.1). = We have very little information at this stage.
+ * Include Program Name, memory address = of the context and PID.
+ */
+ = buf_printf(&pers_string, "OpenVPN %0u %p %s", platform_ge= tpid(), &cd_ctx,
= + time_string(0, 0, 0, &gc));
+
+ /* Initialise mbed TLS RNG, and built-in entropy sou= rces */
+ mbed= tls_entropy_init(&ec);
+
+ mbed= tls_ctr_drbg_init(&cd_ctx);
+ if (!mbed_ok(mbedtls_ctr_drbg_seed(&cd_ctx, mbedtls= _entropy_func, &ec, BPTR(&pers_string),
+ BLEN= (&pers_string))))
+ {
+ = msg(M_FATAL, "Failed to initialize random generator");=
+ }
+
+ gc_free(&gc);
+ rand_initialised =3D true;+ }
+
+ return &cd_ctx;
+}
+
+#ifdef ENABLE_PREDICTION_= RESISTANCE
+void
+rand_ctx_enable_predicti= on_resistance(void)
+= {
+ mbedtls_ctr_dr= bg_context *cd_ctx =3D rand_ctx_get();
+
+= mbedtls_ctr_drbg_set_prediction_resistance(cd_ctx, 1);
+}
+#endif /* ENABLE_PREDICTION_RESISTANCE */+
+int
+rand_bytes(uint8_t *output, int len)
+{
+ mbedtls_ctr_drbg_context *rng_ctx =3D rand_ctx_get();
= +
+ while (len > 0)
+ {
+ const size_t blen =3D min_int(len, MBEDTLS_CTR_DRBG_= MAX_REQUEST);
+ = if (0 !=3D mbedtls_ctr_drbg_random(rng_ctx, output, blen))+ {
+ return 0;
+ }
+
+ output +=3D blen;
+ len -=3D blen;
+ }
+=
+ return 1;
+}
+
+/*
+ *=
+ * Generic cipher k= ey type functions
+ *=
+ */
+static const mbedtls_cipher_info_t = *
+cipher_get(const c= har *ciphername)
+{= span>
+ ASSERT(ciphername= );
+
+ const mbedtls_cipher_info_t *cip= her =3D NULL;
+
+ ciphername =3D transl= ate_cipher_name_from_openvpn(ciphername);
+ cipher =3D mbedtls_cipher_info_from_string(cipher= name);
+ return ci= pher;
+}+
+bool
+cipher_valid_reason(const char *ciphername, const char **reason)= span>
+{
+ ASSERT(reason);
+
+ const mbedtls_cipher_info_t *cipher =3D cipher_get(cip= hername);
+
+ if (NULL =3D=3D cipher)= span>
+ {
+ msg(D_LOW, "Cipher alg= orithm '%s' not found", ciphername);
+ *reason =3D "disabled because un= known";
+ = return false;
+ }=
+
+ const size_t key_bytelen =3D mbedt= ls_cipher_info_get_key_bitlen(cipher) / 8;
+ if (key_bytelen > MAX_CIPHER_KEY_LENGTH)
+ {
+ msg(D_LOW,
+ "Cipher algorithm '= ;%s' uses a default key size (%zu bytes) "
+ "which is larger than &q= uot; PACKAGE_NAME "'s current maximum key size "+ "(%d bytes)&quo= t;,
+ ciph= ername, key_bytelen, MAX_CIPHER_KEY_LENGTH);
+ *reason =3D "disabled due to key size= too large";
+ = return false;
+= }
++ *reason =3D NULL;
<= span style=3D"color: hsl(120, 100%, 40%);">+ return true;+}
+
+const char *
+cip= her_kt_name(const char *ciphername)
+{
+ = const mbedtls_cipher_info_t *cipher_kt =3D cipher_get(ciphername);=
+ if (NULL =3D=3D cipher= _kt)
+ {
+ return "[null-ci= pher]";
+ }= span>
+
+ return translate_cipher_name_to_open= vpn(mbedtls_cipher_info_get_name(cipher_kt));
+}
+
+int<= br>+cipher_kt_key_size(const ch= ar *ciphername)
+{
+ const mbedtls_ciph= er_info_t *cipher_kt =3D cipher_get(ciphername);
+
+ if (NULL =3D=3D cipher_kt)
+ {
+ return 0;
+ }
++ return (int)mbedtls_ciph= er_info_get_key_bitlen(cipher_kt) / 8;
+}
= +
+int+cipher_kt_iv_size(const char *ciph= ername)
+{
= + const mbedtls_cipher_info_= t *cipher_kt =3D cipher_get(ciphername);
+
+ if (NULL =3D=3D cipher_kt)
+ {
+ = return 0;
+ = }
+ return (int)m= bedtls_cipher_info_get_iv_size(cipher_kt);
+}
+
+int
= +cipher_kt_block_size(const cha= r *ciphername)
+{
+ const mbedtls_ciphe= r_info_t *cipher_kt =3D cipher_get(ciphername);
+ if (NULL =3D=3D cipher_kt)
+ {
+ return 0;
+ }
+ return (int)mbedtls_cipher_info_get_block_size(cipher_kt);
+}
+
+int
+c= ipher_kt_tag_size(const char *ciphername)
+{
+ if (cipher_kt_mode_aead(ciphername))
+ {
+ return OPENVPN_AEAD_TAG_LENGTH;
+ }
+ return 0;
+}
+=
+bool
+cipher_kt_insecure(const char *ciphername= )
+{
+ const mbedtls_cipher_info_t *cip= her_kt =3D cipher_get(ciphername);
+ if (!cipher_kt)
+ {
= + return true;
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ return !(cipher_kt_block_si= ze(ciphername) >=3D 128 / 8
+#ifdef MBEDTLS_CHACHAPOLY_C
+ || mbedtls_cipher_info_get_type(cipher_kt) = =3D=3D MBEDTLS_CIPHER_CHACHA20_POLY1305
+#endif
+ );
+}
+
+static mbedtls_cipher_mode_t+cipher_kt_mode(const mbedtls_cip= her_info_t *cipher_kt)
+{
+ ASSERT(NULL= !=3D cipher_kt);
+ = return mbedtls_cipher_info_get_mode(cipher_kt);
+}
+
+bool
+cipher_kt_mode_cbc(con= st char *ciphername)
= +{
+ const mbedtls= _cipher_info_t *cipher =3D cipher_get(ciphername);
+ return cipher && cipher_kt_mode(= cipher) =3D=3D OPENVPN_MODE_CBC;
+}
+
+bool
+cipher_kt_mode_ofb_cfb(const char *ciph= ername)
+{
= + const mbedtls_cipher_info_= t *cipher =3D cipher_get(ciphername);
+ return cipher
+ && (cipher_kt_mode(cipher) =3D=3D OPENVP= N_MODE_OFB
+ = || cipher_kt_mode(cipher) =3D=3D OPENVPN_MODE_CFB);
+}
+
= +bool
+cipher_kt_mode= _aead(const char *ciphername)
+{
+ cons= t mbedtls_cipher_info_t *cipher =3D cipher_get(ciphername);
+ return cipher
+ && (cipher_kt_mode= (cipher) =3D=3D OPENVPN_MODE_GCM
+#ifdef MBEDTLS_CHACHAPOLY_C
+ || cipher_kt_mode(cipher) =3D=3D MBEDTL= S_MODE_CHACHAPOLY
+#e= ndif
+ );= span>
+}
+
+
+/*=
+ *
+ * Generic cipher context functions<= /span>
+ *
+ */
+
+mbedtls_cipher_context_t *
+cipher_ctx_new(void)
+{
+ mbe= dtls_cipher_context_t *ctx;
+ ALLOC_OBJ(ctx, mbedtls_cipher_context_t);
+ return ctx;
+}
+
+void
+cipher_ctx_free(mbedtl= s_cipher_context_t *ctx)
+{
+ mbedtls_c= ipher_free(ctx);
+ = free(ctx);
+}=
+
+void
+cipher_ctx_init(mbedtls_cipher_context_t *ctx, const uint8_t= *key, const char *ciphername,
+ crypto_operation_t enc)
+{
+ ASSERT(NULL !=3D ciphername && NULL !=3D ctx);
+ CLEAR(*ctx);=
+
+ const mbedtls_cipher_info_t *kt =3D ciphe= r_get(ciphername);
+ = ASSERT(kt);
+ s= ize_t key_bitlen =3D mbedtls_cipher_info_get_key_bitlen(kt);+
+ if (!mbed_ok(mbedtls_cipher_setup(ctx, kt)))
+ {
+ msg(M_FATAL, "mbed TLS ci= pher context init #1");
+ }
+
+ if (!mbed_ok(mbedtls= _cipher_setkey(ctx, key, (int)key_bitlen, enc)))
+ {
+ msg(M_FATAL, "mbed TLS cipher set key");= span>
+ }
+
+ if (mbedtls_cipher_info_get_mode(kt) =3D=3D MBEDTL= S_MODE_CBC)
+ {
+ if (!mbed_ok(m= bedtls_cipher_set_padding_mode(ctx, MBEDTLS_PADDING_PKCS7)))+ {
+ msg(M_FATAL, "mbed TLS ciph= er set padding mode");
+ }
+ = }
+
+ /* make sure we used a big enou= gh key */
+ ASSERT= (mbedtls_cipher_get_key_bitlen(ctx) <=3D key_bitlen);
+}
+
+= int
+cipher_ctx_iv_le= ngth(const mbedtls_cipher_context_t *ctx)
+{
+ return mbedtls_cipher_get_iv_size(ctx);
+}
+
+int
+cipher_ctx_get_tag(cipher= _ctx_t *ctx, uint8_t *tag, int tag_len)
+{
+ if (tag_len > SIZE_MAX)
+ {
+ = return 0;
+ = }
+
+ if (!mbed_ok(mbedtls_cipher_writ= e_tag(ctx, (unsigned char *)tag, tag_len)))
+ {
+ return 0;
+ }
+
+ return 1;
<= span style=3D"color: hsl(120, 100%, 40%);">+}
+
+int
+cipher_ct= x_block_size(const mbedtls_cipher_context_t *ctx)
+{
+ return (int)mbedtls_cipher_get_block_size(ctx);
<= span style=3D"color: hsl(120, 100%, 40%);">+}
+
+int
+cipher_ct= x_mode(const mbedtls_cipher_context_t *ctx)
+{
+ ASSERT(NULL !=3D ctx);
+
+ re= turn mbedtls_cipher_get_cipher_mode(ctx);
+}
+
+bool
= +cipher_ctx_mode_cbc(const ciph= er_ctx_t *ctx)
+{
+ return ctx &&am= p; cipher_ctx_mode(ctx) =3D=3D OPENVPN_MODE_CBC;
+}
+
+<= br>+bool
+cipher_ctx_mode_ofb_cfb(const cipher_ctx_= t *ctx)
+{
= + return ctx
+ && (cipher_ctx_m= ode(ctx) =3D=3D OPENVPN_MODE_OFB
+ || cipher_ctx_mode(ctx) =3D=3D OPENVPN_MODE_CFB= );
+}
+
+bool
+cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
+{
+ return ctx
+ && (cipher_ctx_mode(ctx) =3D=3D OPENVPN_MODE_= GCM
+#ifdef MBEDTLS_C= HACHAPOLY_C
+ = || cipher_ctx_mode(ctx) =3D=3D MBEDTLS_MODE_CHACHAPOLY+#endif
+ );
+}
+
+int
= +cipher_ctx_reset(mbedtls_ciphe= r_context_t *ctx, const uint8_t *iv_buf)
+{
+ if (!mbed_ok(mbedtls_cipher_reset(ctx)))
+ {
+ return 0;
+ }
+= span>
+ if (!mbed_ok(mbed= tls_cipher_set_iv(ctx, iv_buf, (size_t)mbedtls_cipher_get_iv_size(ctx))))= span>
+ {
+ return 0;
+ }
+
+ return 1;
+}=
+
+int
+cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t = *src, int src_len)
+{=
+ if (src_len >= ; SIZE_MAX)
+ {
+ return 0;
+ }
+
+ if (!mbed_ok(mbedtls_cipher_update_ad(ctx, src, src_l= en)))
+ {+ return 0;
= + }
+
+ return 1;
+}
+
<= span style=3D"color: hsl(120, 100%, 40%);">+int
+cipher_ctx_update(mbedtls_cipher_context_t *ctx= , uint8_t *dst, int *dst_len, uint8_t *src,
+ int src_len)
+{
+ size_t s_dst_len =3D *dst_len;
+
+ if (!mbed_ok(mbedtls_cipher_update(ctx, src, (size_t)src= _len, dst, &s_dst_len)))
+ {
+ = return 0;
+ }= span>
+
+ *dst_len =3D s_dst_len;+
+ return 1;
+}
+=
+int
+cipher_ctx_final(mbedtls_cipher_con= text_t *ctx, uint8_t *dst, int *dst_len)
+{
+ size_t s_dst_len =3D *dst_len;
+
+ = if (!mbed_ok(mbedtls_cipher_finish(ctx, dst, &s_dst_len)))
+ {
+ return 0;
+ }
+
= + *dst_len =3D s_dst_len;
+
+ return= 1;
+}+
+int
+cipher_ctx_final_check_tag(mbedtls_cipher_context_t *ctx, uint8_t *ds= t, int *dst_len, uint8_t *tag,
+ size_t tag_len)
+{
+ size_t olen =3D 0;
+
+= if (MBEDTLS_DECRYPT !=3D mbedtls_cipher_get_operation(ctx))
<= span style=3D"color: hsl(120, 100%, 40%);">+ {
+ return 0;
+ }
+
+ i= f (tag_len > SIZE_MAX)
+ {
+ = return 0;
+ }
+
+ if (!mbed_ok(mbedtls_cipher_finish(ct= x, dst, &olen)))
= + {
+ msg(D= _CRYPT_ERRORS, "%s: cipher_ctx_final() failed", __func__);=
+ return 0;+ }
+
+ if (olen > INT_MAX)
+ {
+ return 0;
+ }
+ *dst= _len =3D olen;
+
+ if (!mbed_ok(mbedtls= _cipher_check_tag(ctx, (const unsigned char *)tag, tag_len)))+ {
+ return 0;
+ }
+
+ ret= urn 1;
+}
<= span style=3D"color: hsl(120, 100%, 40%);">+
+#if defined(__GNUC__) || defined(__clang__)=
+#pragma GCC diagnostic pop=
+#endif+
+/*
+ *
+ * Generic= message digest information functions
+ *
= + */
++
+static const mbedtls_md_info_t *
+md_get(const char *digest)+{
+ const mbedtls_md_info_t *md =3D NULL;
+ ASSERT(digest);
+
+ md =3D mbedtls_md_info_from_string(digest);<= /span>
+ if (!md)<= br>+ {
+ msg(M_FATAL, "Message hash a= lgorithm '%s' not found", digest);
+ }
+ if (mbedtls_md_get_size(md) > MAX_HMAC_KEY_LENGTH)
+ {
+ msg(M_FATAL,
+ "Message hash algor= ithm '%s' uses a default hash size (%d bytes) which is larger than = " PACKAGE_NAME
+= "'s current maximum hash size (%d bytes)",=
+ digest, mbedtl= s_md_get_size(md), MAX_HMAC_KEY_LENGTH);
+ }
+ return md;
+}
+
+bool
+md_valid(const char *digest)
+{
+ const mbedtls_md_info_t *md =3D mbedtls_md_info_from_st= ring(digest);
+ re= turn md !=3D NULL;
+}=
+
+const char *
+md_kt_name(const char *mdname)+{
+ if (!strcmp("none", mdname))
+ {
+ return "[null-digest]";=
+ }+ const mbedtls_md_info_t *kt = =3D md_get(mdname);
+= return mbedtls_md_get_name(kt);
+}
+= span>
+unsigned char<= br>+md_kt_size(const char *mdna= me)
+{+ if (!strcmp("none", = mdname))
+ {
+ return 0;<= br>+ }
+ const mbedtls_md_info_t *kt =3D md_ge= t(mdname);
+ retur= n mbedtls_md_get_size(kt);
+}
+
= +/*
+ *
+ * Generic message digest functions
+ *
+ */
+
+int
+md_full(const char *mdname, const uint8_t= *src, int src_len, uint8_t *dst)
+{
+ = const mbedtls_md_info_t *kt =3D md_get(mdname);
+ return 0 =3D=3D mbedtls_md(kt, src, src_len= , dst);
+}
= +
+mbedtls_md_context_t *
+md_ctx_new(void)
+{
+ mbedtls_md_context_t *ctx;
+ ALLOC_OBJ_CLEAR(ctx, mbedtls_md_context_t);+ return ctx;+}
+
+void
+md_ctx_fr= ee(mbedtls_md_context_t *ctx)
+{
+ free= (ctx);
+}
<= span style=3D"color: hsl(120, 100%, 40%);">+
+void
+md_ctx_init(mbedtls_md_context_t *ctx, const char *mdname)=
+{
+ const mbedtls_md_info_t *kt =3D md_get(m= dname);
+ ASSERT(N= ULL !=3D ctx && NULL !=3D kt);
+
+= mbedtls_md_init(ctx);
+ ASSERT(0 =3D=3D mbedtls_md_setup(ctx, kt, 0));
+ ASSERT(0 =3D=3D mbedtls_md_starts(= ctx));
+}
<= span style=3D"color: hsl(120, 100%, 40%);">+
+void
+md_ctx_cleanup(mbedtls_md_context_t *ctx)
+{
+ mbedtls_md_free(ctx);
+}
+
+int
<= span style=3D"color: hsl(120, 100%, 40%);">+md_ctx_size(const mbedtls_md_co= ntext_t *ctx)
+{
+ if (NULL =3D=3D ctx)=
+ {+ return 0;
+ }
+ return (int)mbedtls_md_get_size(mbedtls_md_in= fo_from_ctx(ctx));
+}=
+
+void
+md_ctx_update(mbedtls_md_context_t *ctx, const uint8_= t *src, size_t src_len)
+{
+ ASSERT(0 = =3D=3D mbedtls_md_update(ctx, src, src_len));
+}
+
+void=
+md_ctx_final(mbedtls_md_co= ntext_t *ctx, uint8_t *dst)
+{
+ ASSERT= (0 =3D=3D mbedtls_md_finish(ctx, dst));
+ mbedtls_md_free(ctx);
+}
+
+
= +/*
+ *
+ * Generic HMAC functions
+ *
+= */
+
+
+/*
+ * TODO: re-enable dmsg for crypto debug
+ */
+
+mbedtls_md= _context_t *
+hmac_ct= x_new(void)
+{=
+ mbedtls_md_context_t *= ctx;
+ ALLOC_OBJ(c= tx, mbedtls_md_context_t);
+ return ctx;
+}
++void
+hmac_ctx_free(mbedtls_md_context_t *ctx)+{
+ free(ctx);
+}
+
+void+hmac_ctx_init(mbedtls_md_con= text_t *ctx, const uint8_t *key, const char *mdname)
+{
+ const mbedtls_md_info_t *kt =3D md_get(mdname);<= br>+ ASSERT(NULL !=3D kt &am= p;& NULL !=3D ctx);
+
+ mbedtls_md_= init(ctx);
+ int k= ey_len =3D mbedtls_md_get_size(kt);
+ ASSERT(0 =3D=3D mbedtls_md_setup(ctx, kt, 1));+ ASSERT(0 =3D=3D mbedtls_= md_hmac_starts(ctx, key, key_len));
+
+ = /* make sure we used a big enough key */
+ ASSERT(mbedtls_md_get_size(kt) <=3D key_len);<= /span>
+}
+
+void
+hmac_ctx_cleanup(mbedtls_md_context_t *ctx)
+{
+ mbedtls_md_free(ctx);
+}
+
+int
+hmac_ctx_size(mbedtls_md_context_t *ct= x)
+{
+ if (NULL =3D=3D ctx)
= + {
+ return 0;
+ }
+ return mbedtls_md_get_size(mbedtls_md_info_from_ctx(ctx)= );
+}
+
+void
+hmac_ctx_reset(mbedtls_md_context_t *ctx)
+{
+ ASSERT(0 =3D=3D mbedtls_md_hmac_reset(ctx));
+}
+
+void
+hmac_ctx_upda= te(mbedtls_md_context_t *ctx, const uint8_t *src, int src_len)+{
+ ASSERT(0 =3D=3D mbedtls_md_hmac_update(ctx, sr= c, src_len));
+}
+
+void
+hmac_ctx_final(mbedtls_md_context_t *ctx, uint8_t *dst)<= /span>
+{
+ ASSERT(0 =3D=3D mbedtls_md_hmac_fi= nish(ctx, dst));
+}= span>
+
+int
+memcmp_constant_time(const void *a, const void *b, size_= t size)
+{
= + /* mbed TLS has a no const= time memcmp function that it exposes
+ * via its APIs like OpenSSL does with CRYPTO_memcmp<= /span>
+ * Adapt the fun= ction that mbedtls itself uses in
+ * mbedtls_safer_memcmp as it considers that to be safe *= /
+ volatile const= unsigned char *A =3D (volatile const unsigned char *)a;
+ volatile const unsigned char *B = =3D (volatile const unsigned char *)b;
+ volatile unsigned char diff =3D 0;
+
+ for (size_t i =3D 0; i < size; i++)+ {
+ unsigned char x =3D A[i], y =3D B[i];= span>
+ diff |=3D x ^= y;
+ }
= +
+ return diff;
+}
+
+#if defined(__= GNUC__) || defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wconversion"= span>
+#endif+
+/*
+ * Generate the hash required by for the \c tls1_PRF function.<= br>+ *
+ * @param md_kt Message digest to us= e
+ * @param sec = Secret to base the hash on
+ * @param sec_len Length of the secret
+ * @param seed Seed to hash<= /span>
+ * @param seed_len = Length of the seed
+ * @param out Output buffer
+ * @param olen Length of the output buffer
+ */
+static void
+tls1_P_hash(const mbedtls_md_info_t *md_kt, co= nst uint8_t *sec, size_t sec_len, const uint8_t *seed,
+ size_t seed_len, uint8_t *ou= t, size_t olen)
+{
+ struct gc_arena gc= =3D gc_new();
+ u= int8_t A1[MAX_HMAC_KEY_LENGTH];
+
+#ifdef = ENABLE_DEBUG
+ /* = used by the D_SHOW_KEY_SOURCE, guarded with ENABLE_DEBUG to avoid unused
+ * variables warni= ngs if compiled with --enable-small */
+ const size_t olen_orig =3D olen;
+ const uint8_t *out_orig =3D out;
+#endif
+
+ hmac_ctx_t *ctx =3D hmac_ctx_new();+ hmac_ctx_t *ctx_tmp =3D hmac_c= tx_new();
+
+ dmsg(D_SHOW_KEY_SOURCE, &= quot;tls1_P_hash sec: %s", format_hex(sec, sec_len, 0, &gc));
+ dmsg(D_SHOW_KEY_SOUR= CE, "tls1_P_hash seed: %s", format_hex(seed, seed_len, 0, &gc= ));
+
+ unsigned int chunk =3D mbedtls_= md_get_size(md_kt);
+= unsigned int A1_len =3D mbedtls_md_get_size(md_kt);
+
+ /* This is the only place where we init an HMAC with a= key that is not
+ = * equal to its size, therefore we init the hmac ctx manually here */
+ mbedtls_md_init(ctx)= ;
+ ASSERT(0 =3D= =3D mbedtls_md_setup(ctx, md_kt, 1));
+ ASSERT(0 =3D=3D mbedtls_md_hmac_starts(ctx, sec, sec_= len));
++ mbedtls_md_init(ctx_tmp);
+ ASSERT(0 =3D=3D mb= edtls_md_setup(ctx_tmp, md_kt, 1));
+ ASSERT(0 =3D=3D mbedtls_md_hmac_starts(ctx_tmp, sec, se= c_len));
+
= + hmac_ctx_update(ctx, seed,= seed_len);
+ hmac= _ctx_final(ctx, A1);
= +
+ for (;;)
+ {
+ hmac_ctx_reset(ctx);
= + hmac_ctx_reset(ctx_tmp= );
+ hmac_ctx_= update(ctx, A1, A1_len);
+ hmac_ctx_update(ctx_tmp, A1, A1_len);
+ hmac_ctx_update(ctx, seed, seed_len)= ;
+
+ if (olen > chunk)+ {
+ hmac_ctx_final(ctx, out);<= /span>
+ out +=3D= chunk;
+ = olen -=3D chunk;
+ = hmac_ctx_final(ctx_tmp, A1); /* calc the next A1 value */+ }
+ else = /* last one */
+ = {
+ = hmac_ctx_final(ctx, A1);
+ memcpy(out, A1, olen);
+ break;
+ }
+ }
+ h= mac_ctx_cleanup(ctx);
+ hmac_ctx_free(ctx);
+ hmac_ctx_cleanup(ctx_tmp);
+ hmac_ctx_free(ctx_tmp);
+ secure_memzero(A1, sizeof(A1));+
+ dmsg(D_SHOW_KEY_SOURCE, "tls1_P_hash out: %s= ", format_hex(out_orig, olen_orig, 0, &gc));
+ gc_free(&gc);
+}
+
+/= *
+ * Use the TLS PRF= function for generating data channel keys.
+ * This code is based on the OpenSSL library.
+ *
+ * TLS generates keys as such:
= + *
+ * master_secret[48] =3D PRF(pre_master_secret[= 48], "master secret",
+ * ClientHello.random[32] + ServerHello= .random[32])
+ *
+ * key_block[] =3D PRF(S= ecurityParameters.master_secret[48],
+ * "key expansion",+ * SecurityParam= eters.server_random[32] +
+ * SecurityParameters.client_random[32]);
<= span style=3D"color: hsl(120, 100%, 40%);">+ *
+ * Notes:
+ *
+= * (1) key_block contains a full set of 4 keys.
+ * (2) The pre-master secret is generated by th= e client.
+ */=
+bool
+ssl_tls1_PRF(const uint8_t *label, size_t= label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
+ size_t olen)
+{
+ struct gc_arena gc =3D gc_new();
= + const md_kt_t *md5 =3D md_= get("MD5");
+ const md_kt_t *sha1 =3D md_get("SHA1");
+
+ uint8_t *out2 =3D (uint8_t *)gc_malloc(olen, false, &a= mp;gc);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ size_t len =3D slen / 2;
+ const uint8_t *S1 = =3D sec;
+ const u= int8_t *S2 =3D &(sec[len]);
+ len +=3D (slen & 1); /* add for odd, make longer */
+
+ tls1_P_hash(md5, S1, len, label, labe= l_len, out1, olen);
+= tls1_P_hash(sha1, S2, len, label, label_len, out2, olen);+
+ for (size_t i =3D 0; i < olen; i++)
+ {
+ out1[i] ^=3D out2[i];
<= span style=3D"color: hsl(120, 100%, 40%);">+ }
+
+ secure_memzero(out2, olen);
+
+ dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%zu]: %s", olen, fo= rmat_hex(out1, olen, 0, &gc));
+
+ = gc_free(&gc);
+ = return true;
+}
+
+#if defined(__GNUC__) || defined(__clang_= _)
+#pragma GCC diagn= ostic pop
+#endif
+
+#endif /* MBEDTLS_VERSION_NUMBER < 0x0= 40000 */
+#endif /* E= NABLE_CRYPTO_MBEDTLS */
diff --git a/src/openvpn/crypto_mbe= dtls_legacy.h b/src/openvpn/crypto_mbedtls_legacy.h
new fil= e mode 100644
index 0000000..af71037
--- /d= ev/null
+++ b/src/openvpn/crypto_mbedtls_legacy.h
@@ -0,0 +1,148 @@
+/*
+ * OpenVPN = -- An application to securely tunnel IP networks
+ * over a single TCP/UDP port, wit= h support for SSL/TLS-based
+ * session authentication and key exchange,
<= span style=3D"color: hsl(120, 100%, 40%);">+ * packet encryptio= n, packet authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2002-2026 OpenVPN Inc <sales@...515...>
+ * Copyright (C) 2010-= 2026 Sentyron B.V. <openvpn@...2918...>
+ *
+ * This program is free software; you can redistribute it and/or = modify
+ * it under = the terms of the GNU General Public License version 2
+ * as published by the Free Software Fou= ndation.
+ *+ * This program is distribu= ted in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied = warranty of
+ * MERC= HANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for mo= re details.
+ *
+ * You should have recei= ved a copy of the GNU General Public License along
+ * with this program; if not, see <https= ://www.gnu.org/licenses/>.
+ */
+
+/**
+ * @file
+ * Data Channel Cryptography mbed TLS-specific backe= nd interface
+ */
+
+#ifndef CRYPTO_MBEDTLS_H_
+#define CRYPTO_MBEDTLS_H_
+
+#include <stdbool.h>
+#include <mbedtls/cipher.h><= br>+#include <mbedtls/md.h&g= t;
+#include <mbed= tls/ctr_drbg.h>
+<= /span>
+/** Generic message = digest key type %context. */
+typedef mbedtls_md_info_t md_kt_t;
+
+/** Generic cipher %context. */
+typedef mbedtls_cipher_context_t cipher_ctx_t;
<= span style=3D"color: hsl(120, 100%, 40%);">+
+/** Generic message digest %context. */
= +typedef mbedtls_md_context_t m= d_ctx_t;
+
= +/** Generic HMAC %context. */<= /span>
+typedef mbedtls_md_c= ontext_t hmac_ctx_t;
= +
+/* Use a dummy typ= e for the provider */
+typedef void provider_t;
+
+/** Maximum = length of an IV */
+#= define OPENVPN_MAX_IV_LENGTH MBEDTLS_MAX_IV_LENGTH
+
+/** Cipher is in CBC mode */
+#define OPENVPN_MODE_CBC MBEDTLS_MODE_CBC+
+/** Cipher is in OFB mode */
+#define OPENVPN_MODE_OFB MBEDTLS_MODE_OFB=
+
+/** Cipher is in CFB mode */
+#define OPENVPN_MODE_CFB MBED= TLS_MODE_CFB
+=
+/** Cipher is in GCM mode = */
+#define OPENVPN_M= ODE_GCM MBEDTLS_MODE_GCM
+
+typedef mbedtl= s_operation_t crypto_operation_t;
+
+/** C= ipher should encrypt */
+#define OPENVPN_OP_ENCRYPT MBEDTLS_ENCRYPT
+
+/** Cipher should decrypt */
+#define OPENVPN_OP_DECRYPT MBEDTLS_DECRYPT
+
+#define MD4_DIGEST_LENGTH 16
+#define MD5_DIGEST_LENGTH 16
+#define SHA_DIGEST_LENGTH = 20
+#define SHA256_DI= GEST_LENGTH 32
+
+/**
+ * Returns a singleton instance of the m= bed TLS random number generator.
+ *
+ * F= or PolarSSL/mbed TLS 1.1+, this is the CTR_DRBG random number generator. If= it
+ * hasn't be= en initialised yet, the RNG will be initialised using the default
+ * entropy sources. Aside fro= m the default platform entropy sources, an
+ * additional entropy source, the HAVEGE random numb= er generator will also be
+ * added. During initialisation, a personalisation string will be add= ed based
+ * on the t= ime, the PID, and a pointer to the random context.
+ */
+mbedtls_ctr_drbg_context *rand_ctx_get(void);+
+#ifdef ENABLE_PREDICTION_RESISTANCE
+/**
+ * Enable prediction resistance on the random number = generator.
+ */
+void rand_ctx_enable_pred= iction_resistance(void);
+
+#endif<= br>+
+/**
+ * Log the supplied mbed TLS error, prefixed by supplied prefi= x.
+ *+ * @param flags Flags to i= ndicate error type and priority.
+ * @param errval mbed TLS error code to convert to erro= r message.
+ * @param= prefix Prefix to mbed TLS error message.
+ *
+ * @returns true if no errors are detected, false otherwise.
+ */
+bool mbed_log_err(unsigned int flags, = int errval, const char *prefix);
+
+/**
+ * Log the supplied mb= ed TLS error, prefixed by function name and line number.
+ *
+ * @param flags Flags to indicate error type an= d priority.
+ * @para= m errval mbed TLS error code to convert to error message.
= + * @param func Functi= on name where error was reported.
+ * @param line Line number where error was reported.=
+ *
+ * @returns true if no errors are de= tected, false otherwise.
+ */
+bool mbed_l= og_func_line(unsigned int flags, int errval, const char *func, int line);= span>
+
+/** Wraps mbed_log_func_line() to preven= t function calls for non-errors */
+static inline bool
+mbed_log_func_line_lite(unsigned int flags, int errval, const= char *func, int line)
+{
+ if (errval)=
+ {+ return mbed_log_func_line= (flags, errval, func, line);
+ }
+ r= eturn true;
+}=
+
+/**
+ * Check errval and log on error.
+ *
+ * Convenience wrapper to put around mbed TLS library calls, e.g= .
+ * if (!mbed_ok = (mbedtls_ssl_func())) return 0;
+ * or
+ *= ASSERT (mbed_ok (mbedtls_ssl_func()));
+ *
+ * @param errval mbed TLS error code to convert to error messag= e.
+ *+ * @returns true if no errors are = detected, false otherwise.
+ */
+#define m= bed_ok(errval) mbed_log_func_line_lite(D_CRYPT_ERRORS, errval, __func__, __= LINE__)
+
<= span style=3D"color: hsl(120, 100%, 40%);">+#endif /* CRYPTO_MBEDTLS_H_ */<= /span>
diff --git a/src/openvpn/mbedtls_compat.h b/src/openvpn/mbe= dtls_compat.h
index d457b5e..667529e 100644
--- a/src/openvpn/mbedtls_compat.h
+++ b/src/openvpn/mbedt= ls_compat.h
@@ -34,6 +34,16 @@
= #include "errlevel.h"
+#include <mbedtls/asn1.h>=
+#include <mbedtls/pk.h&= gt;
+
+#if MBEDTLS_VERSION_NUMBER < 0x0= 4000000
+#include <= ;mbedtls/ctr_drbg.h>
+#include "crypto_mbedtls_legacy.h"
+#else
+#include <mbedtls/oid.h>
+#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 = */
+
= #ifdef HAVE_PSA_CRYPTO_H
#include <psa/crypto.h>
#endif
@@ -51,4 +61,176 @@
= #endif
}
+#if MBEDTLS_VERSION_NUMBER >=3D 0x04000000
+typedef struct
= +{
+ const char *name;
+ uint16_t tls_id;
+} mbedtls_ecp_curve_info;
+
+static inline int
+mbedtls_oid_get_attr_short_name(const mbedtls_asn1_buf *oid,= const char **desc)
+= {
+ /* The relevan= t OIDs all have equal length. */
+ if (oid->tag !=3D MBEDTLS_ASN1_OID || oid->len !=3D = strlen(MBEDTLS_OID_AT_CN))
+ {
+ = *desc =3D NULL;
+ = return -1;
+ = }
+
+ if (memcmp(oid->p, MBEDTLS_OID= _AT_CN, oid->len) =3D=3D 0)
+ {
+ = *desc =3D "CN";
+ }
+ = else if (memcmp(oid->p, MBEDTLS_OID_AT_SUR_NAME, oid->len) =3D=3D 0)=
+ {+ *desc =3D "SN";=
+ }+ else if (memcmp(oid->p, MB= EDTLS_OID_AT_SERIAL_NUMBER, oid->len) =3D=3D 0)
+ {
+ *desc =3D "serialNumber";+ }
+ else if (memcmp(oid->p, MBEDTLS_OID_AT_CO= UNTRY, oid->len) =3D=3D 0)
+ {
+ = *desc =3D "C";
+ }
+ e= lse if (memcmp(oid->p, MBEDTLS_OID_AT_LOCALITY, oid->len) =3D=3D 0)= span>
+ {
+ *desc =3D "L";
+ }
+ else if (memcmp(oid->p, MBEDT= LS_OID_AT_STATE, oid->len) =3D=3D 0)
+ {
+ *desc =3D "ST";
+ }
+ else if (memcmp(oid->p, MBEDTLS_OID_AT_ORGANIZATION, oid->l= en) =3D=3D 0)
+ {<= /span>
+ *desc =3D &q= uot;O";
+ }= span>
+ else if (memcmp(o= id->p, MBEDTLS_OID_AT_ORG_UNIT, oid->len) =3D=3D 0)
+ {
+ *desc =3D "OU";
+ }
+ else if (memcmp(oid->p, MBEDTLS_OID_AT_TITLE= , oid->len) =3D=3D 0)
+ {
+ *= desc =3D "title";
+ }
+ el= se if (memcmp(oid->p, MBEDTLS_OID_AT_POSTAL_ADDRESS, oid->len) =3D=3D= 0)
+ {
= + *desc =3D "postal= Address";
+ }=
+ else if (memcmp= (oid->p, MBEDTLS_OID_AT_POSTAL_CODE, oid->len) =3D=3D 0)+ {
+ *desc =3D "postalCode";
+ }
+ else if (memcmp(oid->p, MBEDTLS= _OID_AT_GIVEN_NAME, oid->len) =3D=3D 0)
+ {
+ *desc =3D "GN";
+ }
+ else if (memcmp(oid->p, MBEDTLS_OID_AT_INITIALS, oid->le= n) =3D=3D 0)
+ {= span>
+ *desc =3D &qu= ot;initials";
+ = }
+ else if (me= mcmp(oid->p, MBEDTLS_OID_AT_GENERATION_QUALIFIER, oid->len) =3D=3D 0)=
+ {+ *desc =3D "generatio= nQualifier";
+ = }
+ else if (mem= cmp(oid->p, MBEDTLS_OID_AT_UNIQUE_IDENTIFIER, oid->len) =3D=3D 0)
+ {
+ *desc =3D "uniqueIdentif= ier";
+ }
+ else if (memcmp(oid= ->p, MBEDTLS_OID_AT_DN_QUALIFIER, oid->len) =3D=3D 0)
+ {
+ *desc =3D "dnQualifier";=
+ }
+ else if (memcmp(oid->p, MBEDTLS_O= ID_AT_PSEUDONYM, oid->len) =3D=3D 0)
+ {
+ *desc =3D "pseudonym";
+ }
+ else
+ {
+ *des= c =3D NULL;
+ = return -1;
+ }
+ return 0;
+}
+
+static inline int
+mbedtls_oid_get_extended_key_usage(const mbedtls_asn1_buf *oid, cons= t char **desc)
+{
+ /* The relevant OID= s all have equal length. */
+ if (oid->tag !=3D MBEDTLS_ASN1_OID || oid->len !=3D strle= n(MBEDTLS_OID_SERVER_AUTH))
+ {
+ = *desc =3D NULL;
+ = return -1;
+ = }
+
+ if (memcmp(oid->p, MBEDTLS_OI= D_SERVER_AUTH, oid->len) =3D=3D 0)
+ {
+ *desc =3D "TLS Web Server Authentication";
= + }
+ else if (memcmp(oid->p, MBEDTLS_OID_C= LIENT_AUTH, oid->len) =3D=3D 0)
+ {
= + *desc =3D "TLS Web Client Authentication";+ }
+ else if (memcmp(oid->p, MBEDTLS_OID_CODE= _SIGNING, oid->len) =3D=3D 0)
+ {
+ = *desc =3D "Code Signing";
+ }
+ else if (memcmp(oid->p, MBEDTLS_OID_EMAIL_PROTECTION, oid-&= gt;len) =3D=3D 0)
+ = {
+ *desc = =3D "E-mail Protection";
+ }
= + else if (memcmp(oid->p, MBEDTLS_OID_TIME_STAMPING, oid->len) =3D= =3D 0)
+ {<= br>+ *desc =3D "Tim= e Stamping";
+ = }
+ else if (mem= cmp(oid->p, MBEDTLS_OID_OCSP_SIGNING, oid->len) =3D=3D 0)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {
+ *desc =3D "OCSP Signing";= span>
+ }
+ else
+ {
+ *desc =3D NULL;
+ return -1;
+ }
+
+ return 0;= span>
+}
+#endif /* MBEDTLS_VERSION_NUMBER >= =3D 0x04000000 */
+= span>
+/* Some functions tha= t operate on private keys use randomness to protect against
+ * side channels. In Mbed TLS 4, th= ey automatically use the RNG in the PSA
+ * library, but in Mbed TLS 3, they require them as exp= licit arguments. */
+= static inline int
+mb= edtls_compat_pk_parse_key(mbedtls_pk_context *ctx,
+ const unsigned c= har *key, size_t keylen,
+ const unsigned char *pwd, size_t pwdlen)= span>
+{
+#if MBEDTLS_VERSION_NUMBER >=3D 0x04= 000000
+ return mb= edtls_pk_parse_key(ctx, key, keylen, pwd, pwdlen);
+#else
+ return mbedtls_pk_parse_key(ctx, key, keylen, pwd, pwd= len, mbedtls_ctr_drbg_random, rand_ctx_get());
+#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000= */
+}+
+static inline int
+mbedtls_compat_pk_parse_keyfile(mbedtls_pk_context *ctx= , const char *path, const char *password)
+{
+#if MBEDTLS_VERSION_NUMBER >=3D 0x04000000
+ return mbedtls_pk_parse_keyfile(ctx, path= , password);
+#else= span>
+ return mbedtls_pk= _parse_keyfile(ctx, path, password, mbedtls_ctr_drbg_random, rand_ctx_get()= );
+#endif /* MBEDTLS= _VERSION_NUMBER < 0x04000000 */
+}
+
+static inline int
+mbedtls_compat_pk_check_= pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)+{
+#if MBEDTLS_VERSION_NUMBER >=3D 0x04000000<= /span>
+ return mbedtls_p= k_check_pair(pub, prv);
+#else
+ return= mbedtls_pk_check_pair(pub, prv, mbedtls_ctr_drbg_random, rand_ctx_get());<= /span>
+#endif /* MBEDTLS_VE= RSION_NUMBER < 0x04000000 */
+}
+
#endif /* MBEDTLS_COMPAT_H_ */
diff --git a/src= /openvpn/ssl_mbedtls.c b/src/openvpn/ssl_mbedtls.c
index f9= 02e17..3e1698f 100644
--- a/src/openvpn/ssl_mbedtls.c
+++ b/src/openvpn/ssl_mbedtls.c
@@ -93,7 +93,9 @= @
ASSERT(NULL !=3D ctx);
CLEAR(*c= tx);
+#if MBEDTLS_VERSION_NUMBER < 0x04000000
ALLOC_OB= J_CLEAR(ctx->dhm_ctx, mbedtls_dhm_context);
+#endif
AL= LOC_OBJ_CLEAR(ctx->ca_chain, mbedtls_x509_crt);
=
@@ -107,7 +109,9 @@
ASSERT(NULL !=3D ctx);= span>
CLEAR(*ctx);
+#if MBEDTLS_VERSION_NUMBER < 0x04000000= span>
ALLOC_OBJ_CLEAR(ctx->dhm_ctx, mbedtls_dhm_context);<= /span>
+#endifALLOC_OBJ_CLEAR(ctx->ca_chain, mbedtls_x509_crt);
ctx->endpoint =3D MBEDTLS_SSL_IS_CLIENT;=
@@ -128,8 +132,10 @@
mbedtls_x509_crt_fr= ee(ctx->crt_chain);
free(ctx->crt_chain);
+#if = MBEDTLS_VERSION_NUMBER < 0x04000000
mbedtls_dhm= _free(ctx->dhm_ctx);
free(ctx->dhm_ctx);
+#endif
=
mbedtls_x509_crl_free(ctx->crl);free(ctx->crl);
@@ -348,6 +354,34 @@=
}
}
+#if MBEDTLS_VERSION_NUMBER >=3D 0x0400= 0000
+static const mb= edtls_ecp_curve_info ecp_curve_info_table[] =3D {
+ /* TODO: Fill out the table. */
+ { "secp256r1", = MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1 },
+ { "secp384r1", MBEDTLS_SSL_IANA_TLS_GROUP= _SECP384R1 },
+ { = "X25519", MBEDTLS_SSL_IANA_TLS_GROUP_X25519 },
+ { "ffdhe2048", MBEDTLS_S= SL_IANA_TLS_GROUP_FFDHE2048 },
+ { "ffdhe3072", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE307= 2 },
+ { "ffd= he4096", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096 },
+ { "ffdhe6144", MBEDTLS_SSL_= IANA_TLS_GROUP_FFDHE6144 },
+ { "ffdhe8192", MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192 }= ,
+};
+static const size_t ecp_curve_info_= table_items =3D sizeof(ecp_curve_info_table) / sizeof(mbedtls_ecp_curve_inf= o);
+
+static const mbedtls_ecp_curve_info= *
+mbedtls_ecp_curve= _info_from_name(const char *name)
+{
+ = for (size_t i =3D 0; i < ecp_curve_info_table_items; i++)+ {
+ if (strcmp(name, ecp_curve_info_table[i]= .name) =3D=3D 0)
+ = {
+ r= eturn &ecp_curve_info_table[i];
+ }
+ }
+ retu= rn NULL;
+}
+#endif /* MBEDTLS_VERSION_NUM= BER >=3D 0x04000000 */
+
void
tls_ctx_set_tls_groups(struct= tls_root_ctx *ctx, const char *groups)
{
= @@ -409,6 +443,7 @@
void
tls_ctx_load_dh_= params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_inline)
{
+#if MB= EDTLS_VERSION_NUMBER < 0x04000000
if (dh_inline)
{
if (!mbed_ok(mbedtls_dhm_pars= e_dhm(ctx->dhm_ctx, (const unsigned char *)dh_file,
@@ -= 427,6 +462,12 @@
msg(D_TLS_DEBUG_LOW= , "Diffie-Hellman initialized with " counter_format " bit ke= y",
(counter_type)mbedtls_dhm_get_bitlen(ctx-= >dhm_ctx));
+#else=
+ if (strcmp(dh_f= ile, "none") !=3D 0)
+ {
+ = msg(M_FATAL, "Mbed TLS 4 only supports pre-defined Diffie-Hellman= groups.");
+ = }
+#endif /* MBEDTLS= _VERSION_NUMBER < 0x04000000 */
}
void
@@ -500,29 +541,26 @@
<= /span>
if (priv_key_inline)
{
= - status =3D mbedtls_pk_pa= rse_key(ctx->priv_key, (const unsigned char *)priv_key_file,
<= span style=3D"color: hsl(0, 100%, 40%);">- = strlen(priv_key_file) + 1, NULL, 0,
- mbedtls_ctr_drbg_r= andom, rand_ctx_get());
+ status =3D mbedtls_compat_pk_parse_key(ctx->priv_key, (const= unsigned char *)priv_key_file,
+ strlen(priv_key_fi= le) + 1, NULL, 0);
if (MBEDTLS_E= RR_PK_PASSWORD_REQUIRED =3D=3D status)
{
char passbuf[512] =3D { 0 };
= pem_password_callback(passbuf, 512, 0, NULL);
- status =3D mbedtls_pk_parse_key(
+ status =3D = mbedtls_compat_pk_parse_key(
ctx->priv_= key, (const unsigned char *)priv_key_file, strlen(priv_key_file) + 1,
- (unsigned c= har *)passbuf, strlen(passbuf), mbedtls_ctr_drbg_random, rand_ctx_get());= span>
+ (unsi= gned char *)passbuf, strlen(passbuf));
}
}
else
{- status =3D mbedtls_pk_pars= e_keyfile(ctx->priv_key, priv_key_file, NULL,
- mbedtl= s_ctr_drbg_random, rand_ctx_get());
+ status =3D mbedtls_compat_pk_parse_keyfile(ctx->= priv_key, priv_key_file, NULL);
if (MBEDTLS_ERR_PK= _PASSWORD_REQUIRED =3D=3D status)
{char passbuf[512] =3D { 0 };
pe= m_password_callback(passbuf, 512, 0, NULL);
- status =3D mbedtls_pk_parse_keyfile(ctx-&= gt;priv_key, priv_key_file, passbuf,
- mbedtls_ctr_dr= bg_random, rand_ctx_get());
+ status =3D mbedtls_compat_pk_parse_keyfile(ctx->priv= _key, priv_key_file, passbuf);
}
= }
if (!mbed_ok(status))
@@ -538,8 = +576,7 @@
return 1;
}<= br>
- if (!mb= ed_ok(mbedtls_pk_check_pair(&ctx->crt_chain->pk, ctx->priv_key= ,
- = mbedtls_ctr_drbg_random, rand_ctx_get())))+ if (!mbed_ok(mbedtls_compat_p= k_check_pair(&ctx->crt_chain->pk, ctx->priv_key)))
<= span> {
msg(M_WARN, "Private key does not= match the certificate");
return 1;
@@ -553,6 +590,7 @@
#pragma GCC diagnostic ignored = "-Wconversion"
#endif
+#if MBEDTLS_VERSION_NUMBER &= lt; 0x04000000
/**
* external_pkcs1_sign= implements a mbed TLS rsa_sign_func callback, that uses
= * the management interface to request an RSA signature for the supplied has= h.
@@ -669,11 +707,16 @@
= return ctx->signature_length;
}
+#endif /* MBEDTLS_VERSION_NUMBER < = 0x04000000 */
int
tls_c= tx_use_external_signing_func(struct tls_root_ctx *ctx, external_sign_func s= ign_func,
void *sign_ctx= )
{
= +#if MBEDTLS_VERSION_NUMBER >=3D 0x04000000
+ msg(M_WARN, "tls_ctx_use_external_signi= ng_func is not implemented for Mbed TLS 4.");
+ return 1;
+#else
ASSERT(NULL !=3D ctx);<= /span>
if (ctx->crt_chain =3D=3D NULL)= span>
@@ -701,6 +744,7 @@
}
=
return 0;
+#endif /* MBEDTLS_VERSION_NUMBER >=3D 0x04000000 */ }
#ifdef ENABLE_MANAGEMENT
@@ -938,6 +982,7 @@
void
tls_= ctx_personalise_random(struct tls_root_ctx *ctx)
{<= br>+#if MBEDTLS_VERSION_NUMBER = < 0x04000000
static char old_sha256_hash[32] =3D { = 0 };
unsigned char sha256_hash[32] =3D { 0 }; mbedtls_ctr_drbg_context *cd_ctx =3D rand_ctx_get();
@@ -960,6 +1005,7 @@
memcpy(old_sha256_= hash, sha256_hash, sizeof(old_sha256_hash));
}
}
+= #endif /* MBEDTLS_VERSION_NUMBER < 0x040000 */
}=
#if defined(__GNUC__) || defined(__clang__)
@@ -1069,7 +1115,9 @@
}
= #endif
mbedtls_ssl_conf_dbg(ks_ssl->ssl_config, my= _debug, NULL);
+#if M= BEDTLS_VERSION_NUMBER < 0x04000000
mbedtls_ssl_conf= _rng(ks_ssl->ssl_config, mbedtls_ctr_drbg_random, rand_ctx_get());
+#endif /* MBEDTLS_VERSION= _NUMBER < 0x04000000 */
mbedtls_s= sl_conf_cert_profile(ks_ssl->ssl_config, &ssl_ctx->cert_profile);=
@@ -1100,12 +1148,14 @@
= #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
= /* Initialise authentication information */
+#if MBEDTLS_VERSION_NUMBER < 0x040000= 00
if (is_server)
{mbed_ok(mbedtls_ssl_conf_dh_param_ctx(ks_ssl->ssl_config, ss= l_ctx->dhm_ctx));
}
+#endif
- mbed_ok(mbedtls_ssl_conf_own_cert(ks_ssl->= ;ssl_config, ssl_ctx->crt_chain, ssl_ctx->priv_key));
+ (void)mbed_ok(mbedtls_ssl_conf_= own_cert(ks_ssl->ssl_config, ssl_ctx->crt_chain, ssl_ctx->priv_key= ));
/* Initialise SSL verification *= /
if (session->opt->ssl_flags & SSLF_CLIENT_= CERT_OPTIONAL)
@@ -1160,7 +1210,7 @@
/= * Initialise SSL context */
ALLOC_OBJ_CLEAR(ks_ssl->= ;ctx, mbedtls_ssl_context);
mbedtls_ssl_init(ks_ssl-&g= t;ctx);
- mbed_ok(mb= edtls_ssl_setup(ks_ssl->ctx, ks_ssl->ssl_config));
+ (void)mbed_ok(mbedtls_ssl_setup(ks= _ssl->ctx, ks_ssl->ssl_config));
/* We do verifi= cation in our own callback depending on the
* exact c= onfiguration. We do not rely on the default hostname
= * verification. */
@@ -1376,7 +1426,8 @@
= /* Error during read, check for retry error */
if (= retval < 0)
{
- if (MBEDTLS_ERR_SSL_WANT_WRITE =3D=3D retval || MBE= DTLS_ERR_SSL_WANT_READ =3D=3D retval)
+ if (MBEDTLS_ERR_SSL_WANT_WRITE =3D=3D retval || M= BEDTLS_ERR_SSL_WANT_READ =3D=3D retval
+ || MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICK= ET =3D=3D retval)
{
r= eturn 0;
}
@@ -1456,6 +1507,7 @@= span>
void
show_available_curves(void)
{
+#if MBEDTL= S_VERSION_NUMBER < 0x04000000
const mbedtls_ecp_cur= ve_info *pcurve =3D mbedtls_ecp_curve_list();
<= span> if (NULL =3D=3D pcurve)
@@ -1470,6 +1522,9 @@
printf("%s\n", pcurve->name);
= pcurve++;
}
+#else
+ msg(M_FATAL, "Mbed TLS 4 has no mechanism to list su= pported curves.");
+#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */
}<= /span>
const char *
diff --git = a/src/openvpn/ssl_mbedtls.h b/src/openvpn/ssl_mbedtls.h
ind= ex f744945..6b678b2 100644
--- a/src/openvpn/ssl_mbedtls.h<= /span>
+++ b/src/openvpn/ssl_mbedtls.h
@@ -112,11 = +112,13 @@
*/
struct tls_root_ctx=
{
- bool = initialised; /**< True if the context has been init= ialised */
+ bool = initialised; /**< True if the context has been initialised */
=
- int endpoi= nt; /**< Whether or not this is a server or a c= lient */
+ int end= point; /**< Whether or not this is a server or a client */
+#if MBEDTLS= _VERSION_NUMBER < 0x04000000
mbedtls_dhm_context *d= hm_ctx; /**< Diffie-Helmann-Merkle context */
+#endif
mbedtls_x= 509_crt *crt_chain; /**< Local Certificate chain */
= mbedtls_x509_crt *ca_chain; /**< CA chain for remo= te verification */
mbedtls_pk_context *priv_key; = /**< Local private key */
diff --git a/src/openvpn/s= sl_verify_mbedtls.c b/src/openvpn/ssl_verify_mbedtls.c
inde= x 9d89a1d..32f3ecb 100644
--- a/src/openvpn/ssl_verify_mbed= tls.c
+++ b/src/openvpn/ssl_verify_mbedtls.c@@ -34,13 +34,22 @@
#if defined(ENABLE= _CRYPTO_MBEDTLS)
+#include <mbedtls/version.h>
+
+#if MBEDTLS_VERSION_NUMBER < 0x04000000
+#include "crypto_mbedtls_legacy.h&qu= ot;
+#include <mbe= dtls/bignum.h>
+#i= nclude <mbedtls/sha1.h>
+#else
#include "crypto_mbedtls.h"
+#endif
+
+#include "mbedtls_compat.h"
+
#include "ssl_v= erify.h"
#include <mbedtls/asn1.h>#include <mbedtls/error.h>
-#include <mbedtls/bignum.h>
#include= <mbedtls/oid.h>
= -#include <mbedtls/sha1.h>
#define= MAX_SUBJECT_LENGTH 256
@@ -171,11 +180,1= 39 @@
return SUCCESS;
}
+#if MBEDTLS_VER= SION_NUMBER >=3D 0x04000000
+/* Mbed TLS 4 has no function to print the certificate serial nu= mber and does
+ * not= expose the bignum functions anymore. So in order to write the serial
+ * number as a decimal st= ring, we implement bignum % 10 and bignum / 10. */
+static char
+bignum_mod_10(const uint8_t *bignum, size_t bignum_l= ength)
+{
<= span style=3D"color: hsl(120, 100%, 40%);">+ int result =3D 0;
+ for (size_t i =3D 0; i &l= t; bignum_length; i++)
+ {
+ res= ult =3D (result * 256) % 10;
+ result =3D (result + bignum[i]) % 10;
+ }
+ return (char)result;
+}
+
+/* Divide b= ignum by 10 rounded down, in place. */
+static void
+bignum_div_10(uint8_t *bignum, size_t *bignum_length)
= +{
+ /*
+ * Some intuition for the algorithm below:+ *
+ * We want to calculate
+ *
+ * (bignum[0] * 256^n + bignum[1] * 256^(n-1= ) + ... + bignum[n]) / 10.
+ *
+ *= Let remainder =3D bignum[0] % 10 and carry =3D remainder * 256.
= + * Then we can write the a= bove as
+ *
+ * (bignum[0] / 1= 0) * 256^n
+ * = + ((carry + bignum[1]) * 256^(n-1) + ... + bignum[n]) / 10.
<= span style=3D"color: hsl(120, 100%, 40%);">+ *
+ * So now we have the first byte of our = result. The second byte will be
+ * (carry + bignum[1]) / 10. Note that this fits into one b= yte because
+ * 0= <=3D remainder < 10. We calculate the next remainder and carry as
+ * remainder =3D (= carry + bignum[1]) % 10 and carry =3D remainder * 256 and
+ * move on to the next byte until= we are done.
+ *= /
+ size_t new_len= gth =3D 0;
+ int c= arry =3D 0;
+ for = (size_t i =3D 0; i < *bignum_length; i++)
+ {
+ uint8_t next_byte =3D (uint8_t)((bignum[i] + carry) / 10= );
+ int remai= nder =3D (bignum[i] + carry) % 10;
+ carry =3D remainder * 256;
+
+ /* Write the byte unless it's a leading zero. */
+ if (new_length = !=3D 0 || next_byte !=3D 0)
+ {
+ = bignum[new_length++] =3D next_byte;
+ }
+ }
+ *bignum_length =3D new_length;
+}
+<= /span>
+/* Write the decimal= representation of bignum to out, if enough space is available.
<= span style=3D"color: hsl(120, 100%, 40%);">+ * Returns the number of bytes = needed in out, or 0 on error. To calculate the
+ * necessary buffer size, the function can be ca= lled with out =3D NULL. */
+static size_t
= +write_bignum(char *out, size_t out_size, const uint8_t *bignum, size_t big= num_length)
+{=
+ if (bignum_length =3D= =3D 0)
+ {<= br>+ /* We want out to b= e "0". */
+= if (out !=3D NULL)
+ {
+ = if (out_size >=3D 2)
+ {
+ out[0] =3D '0';
+ out[1] =3D '\0';<= br>+ }+ else if (out_size >= ; 0)
+ {= span>
+ out[0= ] =3D '\0';
+= }
+ = }
+ return 2= ;
+ }+
+ uint8_t *bignum_copy =3D malloc(bignum_length);=
+ if (bignum_copy= =3D=3D NULL)
+ {<= /span>
+ return 0;
+ }
+ memcpy(bignum_copy, bignum, bign= um_length);
+<= br>+ size_t bytes_needed =3D= 0;
+ size_t bytes= _written =3D 0;
+ = while (bignum_length > 0)
+ {
+ = /* We're writing the digits in reverse order. We put them in the rig= ht order later. */
+ = char digit =3D bignum_mod_10(bignum_copy, bignum_length);
= + if (out !=3D NULL &= ;& bytes_written < out_size - 1)
+ {
+ out[bytes_written++] =3D '0' + (char)digit;= span>
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ bytes_needed +=3D 1;
+ bignum_div_10(= bignum_copy, &bignum_length);
+ }
+=
+ if (out !=3D NU= LL)
+ {
= + if (bytes_written =3D= =3D bytes_needed)
+ = {
+ = /* We had space for all digits. Now reverse them. */
+ for (size_t i =3D 0; i < b= ytes_written / 2; i++)
+ {
+ = char tmp =3D out[i];
+ out[i] =3D out[bytes_written - 1 - i];=
+ out[bytes_= written - 1 - i] =3D tmp;
+ }
+= out[bytes_written] =3D '\0';
+ }
+ else if (out_size > 0)
+ {
+ out[0] =3D '\0';+ }
+ }
+ bytes_needed +=3D 1;
+
+ free(bignum_copy);
+ return bytes_needed;
+}
+#endif = /* MBEDTLS_VERSION_NUMBER >=3D 0x04000000 */
+
char *
backe= nd_x509_get_serial(mbedtls_x509_crt *cert, struct gc_arena *gc)
<= span> {
char *buf =3D NULL;
size_= t buflen =3D 0;
+
+#if MBEDTLS_VERSION_NUM= BER < 0x04000000
mbedtls_mpi serial_mpi =3D { 0 };<= /span>
/* Transform asn1 integer serial int= o mbed TLS MPI */
@@ -201,6 +338,21 @@
end= :
mbedtls_mpi_free(&serial_mpi);
= return buf;
+#els= e
+ buflen =3D wri= te_bignum(NULL, 0, cert->serial.p, cert->serial.len);
+ if (buflen =3D=3D 0)
= + {
+ msg(M_WARN, "Failed to write se= rial to string.");
+ return NULL;
+ }
+ buf = =3D gc_malloc(buflen, true, gc);
+ if (write_bignum(buf, buflen, cert->serial.p, cert->= serial.len) !=3D buflen)
+ {
+ m= sg(M_WARN, "Failed to write serial to string.");
+ return NULL;
+ }
+ return buf;
+#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */= span>
}
char *
diff --git a/src/openvpn/syshead.h b/src/openvpn/syshead.h
index 2e22204..582e130 100644
--- a/src/openvpn/syshead.h<= /span>
+++ b/src/openvpn/syshead.h
@@ -475,7 +475,= 10 @@
#endif
#ifdef ENA= BLE_CRYPTO_MBEDTLS
+#= include <mbedtls/version.h>
+#if MBEDTLS_VERSION_NUMBER < 0x04000000
#= define ENABLE_PREDICTION_RESISTANCE
+#endif /* MBEDTLS_VERSION_NUMBER < 0x04000000 */<= br> #endif /* ENABLE_CRYPTO_MBEDTLS */
/*
diff --git a/tests/Makefile.am b/tests/Makefile.am<= /span>
index bf8f960..cb30fe5 100644
--- a/tests/M= akefile.am
+++ b/tests/Makefile.am
@@ -55,6= +55,7 @@
$(top_srcdir)/src/openvpn/crypto_epoch.c \
$(top_srcdir)/src/openvpn/crypto_openssl.c \
= $(top_srcdir)/src/openvpn/crypto_mbedtls.c \
+ $(top_srcdir)/src/openvpn/crypto_mbedtls_legacy= .c \
$(top_srcdir)/src/openvpn/otime.c \
= $(top_srcdir)/src/openvpn/packet_id.c \
$(top_srcdir)/s= rc/openvpn/platform.c
diff --git a/tests/t_server_null_defa= ult.rc b/tests/t_server_null_default.rc
index 798cfd0..bba7= 7aa 100755
--- a/tests/t_server_null_default.rc
<= span>+++ b/tests/t_server_null_default.rc
@@ -57,7 +57,7 @@=
SERVER_SERVER_2=3D"--server 10.29.42.0 255.255.255.0= "
SERVER_MGMT_PORT_2=3D"11195"SERVER_EXEC_2=3D"${SERVER_EXEC}"
-SERVER_CONF_2=3D"${SERVER_CONF_BASE} ${SERVE= R_SERVER_2} --lport 1195 --proto tcp --management 127.0.0.1 ${SERVER_MGMT_P= ORT_2} --dh ${DH}"
+SERVER_CONF_2=3D"${SERVER_CONF_BASE} ${SERVER_SERVER_2} --lport 11= 95 --proto tcp --management 127.0.0.1 ${SERVER_MGMT_PORT_2} --dh none"=
SERVER_NAME_3=3D"t_server_null_ser= ver-1196_udp"
SERVER_SERVER_3=3D"--server 10.29.= 43.0 255.255.255.0"
diff --git a/tests/unit_tests/open= vpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am
inde= x 7aeea47..1128eb4 100644
--- a/tests/unit_tests/openvpn/Ma= kefile.am
+++ b/tests/unit_tests/openvpn/Makefile.am=
@@ -76,6 +76,7 @@
$(top_srcdir)/src/openvpn/buf= fer.c \
$(top_srcdir)/src/openvpn/crypto.c \$(top_srcdir)/src/openvpn/crypto_mbedtls.c \
+ $(top_srcdir)/src/openvpn/crypto_mbedtls_le= gacy.c \
$(top_srcdir)/src/openvpn/crypto_openssl.c \
$(top_srcdir)/src/openvpn/crypto_epoch.c \
= $(top_srcdir)/src/openvpn/otime.c \
@@ -110,6 +111,7 @@
$(top_srcdir)/src/openvpn/cryptoapi.c \
$= (top_srcdir)/src/openvpn/crypto_epoch.c \
$(top_srcdir)/s= rc/openvpn/crypto_mbedtls.c \
+ $(top_srcdir)/src/openvpn/crypto_mbedtls_legacy.c \$(top_srcdir)/src/openvpn/crypto_openssl.c \
$(top_= srcdir)/src/openvpn/env_set.c \
$(top_srcdir)/src/openvpn= /mss.c \
@@ -158,6 +160,7 @@
$(top_srcdir= )/src/openvpn/crypto.c \
$(top_srcdir)/src/openvpn/crypto= _epoch.c \
$(top_srcdir)/src/openvpn/crypto_mbedtls.c \= span>
+ $(top_srcdir)/src/op= envpn/crypto_mbedtls_legacy.c \
$(top_srcdir)/src/openvpn= /crypto_openssl.c \
$(top_srcdir)/src/openvpn/env_set.c \=
$(top_srcdir)/src/openvpn/otime.c \
@@ -= 188,6 +191,7 @@
$(top_srcdir)/src/openvpn/crypto.c \
$(top_srcdir)/src/openvpn/crypto_epoch.c \
= $(top_srcdir)/src/openvpn/crypto_mbedtls.c \
+ $(top_srcdir)/src/openvpn/crypto_mbedtls_legacy.c= \
$(top_srcdir)/src/openvpn/crypto_openssl.c \
$(top_srcdir)/src/openvpn/env_set.c \
$(top_srcd= ir)/src/openvpn/otime.c \
@@ -208,6 +212,7 @@$(top_srcdir)/src/openvpn/crypto.c \
$(top_srcdir)/s= rc/openvpn/crypto_epoch.c \
$(top_srcdir)/src/openvpn/cry= pto_mbedtls.c \
+ $(t= op_srcdir)/src/openvpn/crypto_mbedtls_legacy.c \
$(top_sr= cdir)/src/openvpn/crypto_openssl.c \
$(top_srcdir)/src/op= envpn/fdmisc.c \
$(top_srcdir)/src/openvpn/otime.c \
@@ -294,6 +299,7 @@
$(top_srcdir)/src/openvpn= /crypto.c \
$(top_srcdir)/src/openvpn/crypto_epoch.c \
$(top_srcdir)/src/openvpn/crypto_mbedtls.c \+ $(top_srcdir)/src/openvpn/crypto_= mbedtls_legacy.c \
$(top_srcdir)/src/openvpn/crypto_opens= sl.c \
$(top_srcdir)/src/openvpn/otime.c \$(top_srcdir)/src/openvpn/packet_id.c \
@@ -332,6 +338,= 7 @@
$(top_srcdir)/src/openvpn/crypto.c \
$(top_srcdir)/src/openvpn/crypto_epoch.c \
$(top_srcdi= r)/src/openvpn/crypto_mbedtls.c \
+ $(top_srcdir)/src/openvpn/crypto_mbedtls_legacy.c \ $(top_srcdir)/src/openvpn/crypto_openssl.c \
$(= top_srcdir)/src/openvpn/otime.c \
$(top_srcdir)/src/openv= pn/packet_id.c \
diff --git a/tests/unit_tests/openvpn/test= _common.h b/tests/unit_tests/openvpn/test_common.h
index 15= 6e5a9..fb070aa 100644
--- a/tests/unit_tests/openvpn/test_c= ommon.h
+++ b/tests/unit_tests/openvpn/test_common.h=
@@ -25,6 +25,10 @@
#include <stdlib.h>
#include <cmocka.h>
+#if defined(ENABLE_CRYPTO_MBEDTLS)
+#include "mbedtls= _compat.h"
+#end= if
+
= /* Do we use cmocka < 2.0.0? */
#ifndef HAVE_CMOCKA_VE= RSION_H
#define HAVE_OLD_CMOCKA_API 1
@@ -= 58,6 +62,9 @@
{
assert_int_equal(setv= buf(stdout, NULL, _IONBF, BUFSIZ), 0);
assert_int_equa= l(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
+#if defined(ENABLE_CRYPTO_MBEDTLS)
+ mbedtls_compat_psa_crypto_init(= );
+#endif
= }
/**
=
To view, visit change 1441. To unsubscribe, or for help writing mail f= ilters, visit se= ttings.