Skip to content

Commit 541ded7

Browse files
committed
TLS: enable TLS 1.3 for Mbed TLS.
This requires using a supporting version of Mbed TLS. We have to use PSA crypto for TLS 1.3.
1 parent 6e5cf29 commit 541ded7

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/supplemental/tls/mbedtls/tls.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "mbedtls/version.h" // Must be first in order to pick up version
1818

1919
#include "mbedtls/error.h"
20+
#ifdef MBEDTLS_PSA_CRYPTO_C
21+
#include "psa/crypto.h"
22+
#endif
2023

2124
#include "nng/nng.h"
2225
#include "nng/supplemental/tls/tls.h"
@@ -28,6 +31,7 @@
2831
#include "mbedtls/net.h"
2932
#endif
3033

34+
#include "mbedtls/debug.h"
3135
#include "mbedtls/ssl.h"
3236

3337
#include "core/nng_impl.h"
@@ -465,7 +469,11 @@ config_init(nng_tls_engine_config *cfg, enum nng_tls_mode mode)
465469
// SSL v3.3. As of this writing, Mbed TLS still does not support
466470
// version 1.3, and we would want to test it before enabling it here.
467471
cfg->min_ver = MBEDTLS_SSL_MINOR_VERSION_3;
472+
#ifdef MBEDTLS_SSL_PROTO_TLS1_3
473+
cfg->max_ver = MBEDTLS_SSL_MINOR_VERSION_4;
474+
#else
468475
cfg->max_ver = MBEDTLS_SSL_MINOR_VERSION_3;
476+
#endif
469477

470478
mbedtls_ssl_conf_min_version(
471479
&cfg->cfg_ctx, MBEDTLS_SSL_MAJOR_VERSION_3, cfg->min_ver);
@@ -689,9 +697,16 @@ config_version(nng_tls_engine_config *cfg, nng_tls_version min_ver,
689697
v1 = MBEDTLS_SSL_MINOR_VERSION_2;
690698
break;
691699
#endif
700+
#ifdef MBEDTLS_SSL_MINOR_VERSION_3
692701
case NNG_TLS_1_2:
693702
v1 = MBEDTLS_SSL_MINOR_VERSION_3;
694703
break;
704+
#endif
705+
#ifdef MBEDTLS_SSL_PROTO_TLS1_3
706+
case NNG_TLS_1_3:
707+
v1 = MBEDTLS_SSL_MINOR_VERSION_4;
708+
break;
709+
#endif
695710
default:
696711
nng_log_err(
697712
"TLS-CFG-VER", "TLS minimum version not supported");
@@ -709,9 +724,17 @@ config_version(nng_tls_engine_config *cfg, nng_tls_version min_ver,
709724
v2 = MBEDTLS_SSL_MINOR_VERSION_2;
710725
break;
711726
#endif
727+
#ifdef MBEDTLS_SSL_MINOR_VERSION_3
712728
case NNG_TLS_1_2:
729+
v2 = MBEDTLS_SSL_MINOR_VERSION_3;
730+
break;
731+
#endif
713732
case NNG_TLS_1_3: // We lack support for 1.3, so treat as 1.2.
733+
#ifdef MBEDTLS_SSL_PROTO_TLS1_3
734+
v2 = MBEDTLS_SSL_MINOR_VERSION_4;
735+
#else
714736
v2 = MBEDTLS_SSL_MINOR_VERSION_3;
737+
#endif
715738
break;
716739
default:
717740
// Note that this means that if we ever TLS 1.4 or 2.0,
@@ -778,10 +801,18 @@ nng_tls_engine_init_mbed(void)
778801
nni_mtx_fini(&rng_lock);
779802
return (rv);
780803
}
804+
#endif
805+
#ifdef MBEDTLS_PSA_CRYPTO_C
806+
rv = psa_crypto_init();
807+
if (rv != 0) {
808+
tls_log_err(
809+
"NNG-TLS-INIT", "Failed initializing PSA crypto", rv);
810+
return (rv);
811+
}
781812
#endif
782813
// Uncomment the following to have noisy debug from mbedTLS.
783814
// This may be useful when trying to debug failures.
784-
// mbedtls_debug_set_threshold(3);
815+
// mbedtls_debug_set_threshold(9);
785816

786817
rv = nng_tls_engine_register(&tls_engine_mbed);
787818

@@ -801,4 +832,7 @@ nng_tls_engine_fini_mbed(void)
801832
mbedtls_ctr_drbg_free(&rng_ctx);
802833
nni_mtx_fini(&rng_lock);
803834
#endif
835+
#ifdef MBEDTLS_PSA_CRYPTO_C
836+
mbedtls_psa_crypto_free();
837+
#endif
804838
}

src/supplemental/tls/tls_test.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ test_tls_psk(void)
207207
nng_aio_set_timeout(aio1, 5000);
208208
nng_aio_set_timeout(aio2, 5000);
209209

210+
// all PSK implementations also can do TLS 1.3
211+
210212
// Allocate the listener first. We use a wild-card port.
211213
NUTS_PASS(nng_stream_listener_alloc(&l, "tls+tcp://127.0.0.1:0"));
212214
NUTS_PASS(nng_tls_config_alloc(&c1, NNG_TLS_MODE_SERVER));
@@ -417,8 +419,9 @@ test_tls_psk_bad_identity(void)
417419
t1 = nuts_stream_send_start(s1, buf1, size);
418420
t2 = nuts_stream_recv_start(s2, buf2, size);
419421

420-
NUTS_FAIL(nuts_stream_wait(t1), NNG_ECRYPTO);
421-
NUTS_FAIL(nuts_stream_wait(t2), NNG_ECRYPTO);
422+
// These can fail due to ECRYPTO, EPEERAUTH, or ECONNSHUT, for example
423+
NUTS_ASSERT(nuts_stream_wait(t1) != 0);
424+
NUTS_ASSERT(nuts_stream_wait(t2) != 0);
422425

423426
nng_free(buf1, size);
424427
nng_free(buf2, size);

0 commit comments

Comments
 (0)