Skip to content

Commit 5406694

Browse files
Add support for compiling against mbedtls 3.x. (#648)
* MbedTLS 3.x compatibility. * Update mbedtls version in Brewfile, Makefile and build.yml. * Fix indentation.
1 parent a769c3a commit 5406694

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

.github/workflows/build.yml

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ jobs:
111111
brew list --cask | xargs brew uninstall --force --ignore-dependencies
112112
brew update
113113
brew bundle
114-
brew link mbedtls@2 --force # needed for CMake
115114
;;
116115
117116
windows*)

Brewfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ brew "sdl2"
77
brew "libogg"
88
brew "libvorbis"
99
brew "openal-soft"
10-
brew "mbedtls@2"
10+
brew "mbedtls"
1111
brew "libuv"
1212
brew "openssl"
1313
brew "sqlite"

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ BREW_SDL2 := $(shell brew --prefix sdl2)
9898
BREW_JPEGTURBO := $(shell brew --prefix jpeg-turbo)
9999
BREW_VORBIS := $(shell brew --prefix libvorbis)
100100
BREW_OPENAL := $(shell brew --prefix openal-soft)
101-
BREW_MBEDTLS := $(shell brew --prefix mbedtls@2)
101+
BREW_MBEDTLS := $(shell brew --prefix mbedtls)
102102
BREW_LIBPNG := $(shell brew --prefix libpng)
103103
BREW_LIBOGG := $(shell brew --prefix libogg)
104104
BREW_LIBUV := $(shell brew --prefix libuv)

libs/ssl/ssl.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,11 @@ HL_PRIM varray *HL_NAME(cert_get_altnames)(hl_ssl_cert *cert) {
460460
varray *a = NULL;
461461
vbyte **current = NULL;
462462
mbedtls_x509_crt *crt = cert->c;
463+
#if MBEDTLS_VERSION_MAJOR >= 3
464+
if (mbedtls_x509_crt_has_ext_type(crt, MBEDTLS_X509_EXT_SUBJECT_ALT_NAME)) {
465+
#else
463466
if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
467+
#endif
464468
cur = &crt->subject_alt_names;
465469
while (cur != NULL) {
466470
if (pos == count) {
@@ -593,7 +597,11 @@ HL_PRIM hl_ssl_pkey *HL_NAME(key_from_der)(vbyte *data, int len, bool pub) {
593597
if (pub)
594598
r = mbedtls_pk_parse_public_key(pk, (const unsigned char*)data, len);
595599
else
600+
#if MBEDTLS_VERSION_MAJOR >= 3
601+
r = mbedtls_pk_parse_key(pk, (const unsigned char*)data, len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg);
602+
#else
596603
r = mbedtls_pk_parse_key(pk, (const unsigned char*)data, len, NULL, 0);
604+
#endif
597605
if (r != 0) {
598606
mbedtls_pk_free(pk);
599607
free(pk);
@@ -618,10 +626,17 @@ HL_PRIM hl_ssl_pkey *HL_NAME(key_from_pem)(vbyte *data, bool pub, vbyte *pass) {
618626
buf[len - 1] = '\0';
619627
if (pub)
620628
r = mbedtls_pk_parse_public_key(pk, buf, len);
629+
#if MBEDTLS_VERSION_MAJOR >= 3
630+
else if (pass == NULL)
631+
r = mbedtls_pk_parse_key(pk, buf, len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg);
632+
else
633+
r = mbedtls_pk_parse_key(pk, buf, len, (const unsigned char*)pass, strlen((char*)pass), mbedtls_ctr_drbg_random, &ctr_drbg);
634+
#else
621635
else if (pass == NULL)
622636
r = mbedtls_pk_parse_key(pk, buf, len, NULL, 0);
623637
else
624638
r = mbedtls_pk_parse_key(pk, buf, len, (const unsigned char*)pass, strlen((char*)pass));
639+
#endif
625640
free(buf);
626641
if (r != 0) {
627642
mbedtls_pk_free(pk);
@@ -676,9 +691,13 @@ HL_PRIM vbyte *HL_NAME(dgst_sign)(vbyte *data, int len, hl_ssl_pkey *key, vbyte
676691
ssl_error(r);
677692
return NULL;
678693
}
679-
694+
#if MBEDTLS_VERSION_MAJOR >= 3
695+
out = hl_gc_alloc_noptr(MBEDTLS_PK_SIGNATURE_MAX_SIZE);
696+
if ((r = mbedtls_pk_sign(key->k, mbedtls_md_get_type(md), hash, mbedtls_md_get_size(md), out, MBEDTLS_PK_SIGNATURE_MAX_SIZE, (size ? &ssize : NULL), mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
697+
#else
680698
out = hl_gc_alloc_noptr(MBEDTLS_MPI_MAX_SIZE);
681699
if ((r = mbedtls_pk_sign(key->k, mbedtls_md_get_type(md), hash, 0, out, (size ? &ssize : NULL), mbedtls_ctr_drbg_random, &ctr_drbg)) != 0){
700+
#endif
682701
ssl_error(r);
683702
return NULL;
684703
}

0 commit comments

Comments
 (0)