Skip to content

Commit 697fd2c

Browse files
rado17jukkar
authored andcommitted
[noup] zephyr: Fix WPA3 connection issue
The static initialization of hostap_rng_ctx results in passing a NULL context pointer which results in a crash. Initialize rng context before passing it to MPI calls. Signed-off-by: Ravi Dondaputi <ravi.dondaputi@nordicsemi.no>
1 parent 6855231 commit 697fd2c

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/crypto/crypto_mbedtls_alt.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,13 @@
154154
/* Setting ctr_drbg_init_state to 1 to allow unload_crypto to run */
155155
static int ctr_drbg_init_state = 1;
156156
int (*hostap_rng_fn)(void*, unsigned char*, size_t) = mbedtls_psa_get_random;
157-
void *hostap_rng_ctx = MBEDTLS_PSA_RANDOM_STATE;
158157
#else
159158
#include <mbedtls/entropy.h>
160159
#include <mbedtls/ctr_drbg.h>
161160
static int ctr_drbg_init_state;
162161
static mbedtls_ctr_drbg_context ctr_drbg;
163162
static mbedtls_entropy_context entropy;
164163
int(*hostap_rng_fn)(void*, unsigned char*, size_t) = mbedtls_ctr_drbg_random;
165-
void *hostap_rng_ctx = &ctr_drbg;
166164
#endif
167165

168166
#ifdef CRYPTO_MBEDTLS_CRYPTO_BIGNUM
@@ -228,6 +226,15 @@ inline mbedtls_ctr_drbg_context *crypto_mbedtls_ctr_drbg(void)
228226
}
229227
#endif
230228

229+
void *hostap_rng_ctx(void)
230+
{
231+
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
232+
return MBEDTLS_PSA_RANDOM_STATE;
233+
#else
234+
return (mbedtls_ctr_drbg_context *) crypto_mbedtls_ctr_drbg();
235+
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
236+
}
237+
231238
/* tradeoff: slightly smaller code size here at cost of slight increase
232239
* in instructions and function calls at runtime versus the expanded
233240
* per-message-digest code that follows in #else (~0.5 kib .text larger) */
@@ -1213,7 +1220,7 @@ int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
12131220

12141221
/*assert(r != m);*/ /* r must not be same as m for mbedtls_mpi_random()*/
12151222
#if MBEDTLS_VERSION_NUMBER >= 0x021B0000 /* mbedtls 2.27.0 */
1216-
return mbedtls_mpi_random((mbedtls_mpi *)r, 0, (mbedtls_mpi *)m, hostap_rng_fn, hostap_rng_ctx) ?
1223+
return mbedtls_mpi_random((mbedtls_mpi *)r, 0, (mbedtls_mpi *)m, hostap_rng_fn, hostap_rng_ctx()) ?
12171224
-1 :
12181225
0;
12191226
#else
@@ -1441,7 +1448,7 @@ __attribute_noinline__ static int crypto_mbedtls_dh_init_public(
14411448
mbedtls_dhm_context *ctx, u8 generator, const u8 *prime, size_t prime_len, u8 *privkey, u8 *pubkey)
14421449
{
14431450
if (crypto_mbedtls_dh_set_bin_pg(ctx, generator, prime, prime_len) ||
1444-
mbedtls_dhm_make_public(ctx, (int)prime_len, pubkey, prime_len, hostap_rng_fn, hostap_rng_ctx))
1451+
mbedtls_dhm_make_public(ctx, (int)prime_len, pubkey, prime_len, hostap_rng_fn, hostap_rng_ctx()))
14451452
return -1;
14461453

14471454
return mbedtls_mpi_write_binary(&ctx->MBEDTLS_PRIVATE(X), privkey, prime_len) ? -1 : 0;
@@ -1506,7 +1513,7 @@ int crypto_dh_derive_secret(u8 generator,
15061513
int ret =
15071514
mbedtls_dhm_read_params(&ctx, &p, p + 2 + prime_len + 5 + pubkey_len) ||
15081515
mbedtls_mpi_read_binary(&ctx.MBEDTLS_PRIVATE(X), privkey, privkey_len) ||
1509-
mbedtls_dhm_calc_secret(&ctx, secret, *len, len, hostap_rng_fn, hostap_rng_ctx) ?
1516+
mbedtls_dhm_calc_secret(&ctx, secret, *len, len, hostap_rng_fn, hostap_rng_ctx()) ?
15101517
-1 :
15111518
0;
15121519
mbedtls_dhm_free(&ctx);
@@ -1598,7 +1605,7 @@ struct wpabuf *dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, co
15981605
if (buf == NULL)
15991606
return NULL;
16001607
if (mbedtls_dhm_read_public((mbedtls_dhm_context *)ctx, wpabuf_head(peer_public), wpabuf_len(peer_public)) == 0 &&
1601-
mbedtls_dhm_calc_secret(ctx, wpabuf_mhead(buf), olen, &olen, hostap_rng_fn, hostap_rng_ctx) == 0)
1608+
mbedtls_dhm_calc_secret(ctx, wpabuf_mhead(buf), olen, &olen, hostap_rng_fn, hostap_rng_ctx()) == 0)
16021609
{
16031610
wpabuf_put(buf, olen);
16041611
return buf;
@@ -1748,7 +1755,7 @@ static int crypto_mbedtls_keypair_gen(int group, mbedtls_pk_context *pk)
17481755
if (pk_info == NULL)
17491756
return -1;
17501757
return mbedtls_pk_setup(pk, pk_info) ||
1751-
mbedtls_ecp_gen_key(grp_id, mbedtls_pk_ec(*pk), hostap_rng_fn, hostap_rng_ctx) ?
1758+
mbedtls_ecp_gen_key(grp_id, mbedtls_pk_ec(*pk), hostap_rng_fn, hostap_rng_ctx()) ?
17521759
-1 :
17531760
0;
17541761
}
@@ -1956,7 +1963,7 @@ struct wpabuf *crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, cons
19561963
if (buf == NULL)
19571964
return NULL;
19581965

1959-
if (mbedtls_ecdh_calc_secret(&ecdh->ctx, &len, wpabuf_mhead(buf), len, hostap_rng_fn, hostap_rng_ctx) == 0)
1966+
if (mbedtls_ecdh_calc_secret(&ecdh->ctx, &len, wpabuf_mhead(buf), len, hostap_rng_fn, hostap_rng_ctx()) == 0)
19601967
{
19611968
wpabuf_put(buf, len);
19621969
return buf;
@@ -2255,7 +2262,7 @@ int crypto_ec_point_mul(struct crypto_ec *e,
22552262
return -1;
22562263

22572264
return mbedtls_ecp_mul((mbedtls_ecp_group *)e, (mbedtls_ecp_point *)res, (const mbedtls_mpi *)b,
2258-
(const mbedtls_ecp_point *)p, hostap_rng_fn, hostap_rng_ctx) ?
2265+
(const mbedtls_ecp_point *)p, hostap_rng_fn, hostap_rng_ctx()) ?
22592266
-1 :
22602267
0;
22612268
}
@@ -2376,7 +2383,7 @@ struct crypto_ec_key *crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
23762383
#if MBEDTLS_VERSION_NUMBER < 0x03000000 /* mbedtls 3.0.0 */
23772384
if (mbedtls_pk_parse_key(ctx, der, der_len, NULL, 0) == 0)
23782385
#else
2379-
if (mbedtls_pk_parse_key(ctx, der, der_len, NULL, 0, hostap_rng_fn, hostap_rng_ctx) == 0)
2386+
if (mbedtls_pk_parse_key(ctx, der, der_len, NULL, 0, hostap_rng_fn, hostap_rng_ctx()) == 0)
23802387
#endif
23812388
return (struct crypto_ec_key *)ctx;
23822389

@@ -2536,7 +2543,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
25362543
{
25372544
/* (Is private key generation necessary for callers?)
25382545
* alt: gen key then overwrite Q
2539-
* mbedtls_ecp_gen_key(grp_id, ecp_kp, hostap_rng_fn, hostap_rng_ctx) == 0
2546+
* mbedtls_ecp_gen_key(grp_id, ecp_kp, hostap_rng_fn, hostap_rng_ctx()) == 0
25402547
*/
25412548
mbedtls_ecp_keypair *ecp_kp = mbedtls_pk_ec(*ctx);
25422549
mbedtls_ecp_group *ecp_kp_grp = &ecp_kp->MBEDTLS_PRIVATE(grp);
@@ -2545,7 +2552,7 @@ static struct crypto_ec_key *crypto_ec_key_set_pub_point_for_group(mbedtls_ecp_g
25452552
if (mbedtls_ecp_group_load(ecp_kp_grp, grp_id) == 0 &&
25462553
(pub ? mbedtls_ecp_copy(ecp_kp_Q, pub) == 0 :
25472554
mbedtls_ecp_point_read_binary(ecp_kp_grp, ecp_kp_Q, buf, len) == 0) &&
2548-
mbedtls_ecp_gen_privkey(ecp_kp_grp, ecp_kp_d, hostap_rng_fn, hostap_rng_ctx) == 0)
2555+
mbedtls_ecp_gen_privkey(ecp_kp_grp, ecp_kp_d, hostap_rng_fn, hostap_rng_ctx()) == 0)
25492556
{
25502557
return (struct crypto_ec_key *)ctx;
25512558
}
@@ -2815,7 +2822,7 @@ struct wpabuf *crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data, siz
28152822
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
28162823
sig_len,
28172824
#endif
2818-
&sig_len, hostap_rng_fn, hostap_rng_ctx) == 0)
2825+
&sig_len, hostap_rng_fn, hostap_rng_ctx()) == 0)
28192826
{
28202827
wpabuf_put(buf, sig_len);
28212828
return buf;
@@ -2838,7 +2845,7 @@ struct wpabuf *crypto_ec_key_sign_r_s(struct crypto_ec_key *key, const u8 *data,
28382845
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
28392846
sig_len,
28402847
#endif
2841-
&sig_len, hostap_rng_fn, hostap_rng_ctx))
2848+
&sig_len, hostap_rng_fn, hostap_rng_ctx()))
28422849
{
28432850
return NULL;
28442851
}
@@ -3204,7 +3211,7 @@ struct wpabuf *crypto_csr_sign(struct crypto_csr *csr, struct crypto_ec_key *key
32043211
mbedtls_x509write_csr_set_md_alg((mbedtls_x509write_csr *)csr, sig_md);
32053212

32063213
unsigned char buf[4096]; /* XXX: large enough? too large? */
3207-
int len = mbedtls_x509write_csr_der((mbedtls_x509write_csr *)csr, buf, sizeof(buf), hostap_rng_fn, hostap_rng_ctx);
3214+
int len = mbedtls_x509write_csr_der((mbedtls_x509write_csr *)csr, buf, sizeof(buf), hostap_rng_fn, hostap_rng_ctx());
32083215
if (len < 0)
32093216
return NULL;
32103217
/* Note: data is written at the end of the buffer! Use the

src/crypto/tls_mbedtls_alt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#include <mbedtls/x509_crt.h>
6161

6262
extern int (*hostap_rng_fn)(void*, unsigned char*, size_t);
63-
extern void* hostap_rng_ctx;
63+
extern void* hostap_rng_ctx(void);
6464

6565
#ifdef MBEDTLS_DEBUG_C
6666
#define DEBUG_THRESHOLD 4
@@ -348,7 +348,7 @@ struct tls_conf *tls_conf_init(void *tls_ctx)
348348
tls_conf->refcnt = 1;
349349

350350
mbedtls_ssl_config_init(&tls_conf->conf);
351-
mbedtls_ssl_conf_rng(&tls_conf->conf, hostap_rng_fn, hostap_rng_ctx);
351+
mbedtls_ssl_conf_rng(&tls_conf->conf, hostap_rng_fn, hostap_rng_ctx());
352352
mbedtls_x509_crt_init(&tls_conf->ca_cert);
353353
mbedtls_x509_crt_init(&tls_conf->client_cert);
354354
mbedtls_pk_init(&tls_conf->private_key);
@@ -402,7 +402,7 @@ __attribute_cold__ void *tls_init(const struct tls_config *conf)
402402
#endif
403403
#ifdef MBEDTLS_SSL_SESSION_TICKETS
404404
mbedtls_ssl_ticket_init(&tls_ctx_global.ticket_ctx);
405-
mbedtls_ssl_ticket_setup(&tls_ctx_global.ticket_ctx, hostap_rng_fn, hostap_rng_ctx,
405+
mbedtls_ssl_ticket_setup(&tls_ctx_global.ticket_ctx, hostap_rng_fn, hostap_rng_ctx(),
406406
MBEDTLS_CIPHER_AES_256_GCM, 43200); /* ticket timeout: 12 hours */
407407
#endif
408408
/* copy struct for future use */
@@ -1615,7 +1615,7 @@ static int tls_mbedtls_set_certs(struct tls_conf *tls_conf, const struct tls_con
16151615
const char *pwd = params->private_key_passwd;
16161616
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 /* mbedtls 3.0.0 */
16171617
ret = mbedtls_pk_parse_key(&tls_conf->private_key, data, len, (const unsigned char *)pwd,
1618-
pwd ? os_strlen(pwd) : 0, hostap_rng_fn, hostap_rng_ctx);
1618+
pwd ? os_strlen(pwd) : 0, hostap_rng_fn, hostap_rng_ctx());
16191619
#else
16201620
ret = mbedtls_pk_parse_key(&tls_conf->private_key, data, len, (const unsigned char *)pwd,
16211621
pwd ? os_strlen(pwd) : 0);

0 commit comments

Comments
 (0)