Skip to content

Commit a5a2316

Browse files
committed
async: fix overwrite of keylen params between calls
The `kse->pubKeyLen` parameter is used as an input parameter to `DhGenKeyPair` to provide the size of the `pubKey` buffer (the same size as the prime p). After that, `kse->pubKeyLen` is used to check that the public key generated is of the same length as the prime p. If this is not the case, the public key is padded. If the key generation is asynchronous, then `TLSX_KeyShare_GenDhKey` may be invoked twice. The second time, the `kse->pubKeyLen` value, updated asynchronously by the async code, is overwritten with the prime size at the beginning of the function. When this happens, a wrong public key value is used, and the shared secret computed is incorrect. Similar reasoning can be applied to `kse->keyLen`
1 parent 3e4b95e commit a5a2316

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/tls.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6724,31 +6724,31 @@ static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
67246724
#ifdef HAVE_FFDHE_2048
67256725
case WOLFSSL_FFDHE_2048:
67266726
params = wc_Dh_ffdhe2048_Get();
6727-
kse->keyLen = 29;
6727+
pvtSz = 29;
67286728
break;
67296729
#endif
67306730
#ifdef HAVE_FFDHE_3072
67316731
case WOLFSSL_FFDHE_3072:
67326732
params = wc_Dh_ffdhe3072_Get();
6733-
kse->keyLen = 34;
6733+
pvtSz = 34;
67346734
break;
67356735
#endif
67366736
#ifdef HAVE_FFDHE_4096
67376737
case WOLFSSL_FFDHE_4096:
67386738
params = wc_Dh_ffdhe4096_Get();
6739-
kse->keyLen = 39;
6739+
pvtSz = 39;
67406740
break;
67416741
#endif
67426742
#ifdef HAVE_FFDHE_6144
67436743
case WOLFSSL_FFDHE_6144:
67446744
params = wc_Dh_ffdhe6144_Get();
6745-
kse->keyLen = 46;
6745+
pvtSz = 46;
67466746
break;
67476747
#endif
67486748
#ifdef HAVE_FFDHE_8192
67496749
case WOLFSSL_FFDHE_8192:
67506750
params = wc_Dh_ffdhe8192_Get();
6751-
kse->keyLen = 52;
6751+
pvtSz = 52;
67526752
break;
67536753
#endif
67546754
default:
@@ -6757,19 +6757,16 @@ static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
67576757
if (params == NULL)
67586758
return BAD_FUNC_ARG;
67596759
pSz = params->p_len;
6760-
pvtSz = kse->keyLen;
67616760
#else
6762-
kse->keyLen = wc_DhGetNamedKeyMinSize(kse->group);
6763-
if (kse->keyLen == 0) {
6761+
pvtSz = wc_DhGetNamedKeyMinSize(kse->group);
6762+
if (pvtSz == 0) {
67646763
return BAD_FUNC_ARG;
67656764
}
67666765
ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
67676766
if (ret != 0) {
67686767
return BAD_FUNC_ARG;
67696768
}
6770-
pvtSz = kse->keyLen;
67716769
#endif
6772-
kse->pubKeyLen = pSz;
67736770

67746771
/* Trigger Key Generation */
67756772
if (kse->pubKey == NULL || kse->privKey == NULL) {
@@ -6794,14 +6791,14 @@ static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
67946791

67956792
/* Allocate space for the private and public key */
67966793
if (ret == 0 && kse->pubKey == NULL) {
6797-
kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
6794+
kse->pubKey = (byte*)XMALLOC(pSz, ssl->heap,
67986795
DYNAMIC_TYPE_PUBLIC_KEY);
67996796
if (kse->pubKey == NULL)
68006797
ret = MEMORY_E;
68016798
}
68026799

68036800
if (ret == 0 && kse->privKey == NULL) {
6804-
kse->privKey = (byte*)XMALLOC(kse->keyLen, ssl->heap,
6801+
kse->privKey = (byte*)XMALLOC(pvtSz, ssl->heap,
68056802
DYNAMIC_TYPE_PRIVATE_KEY);
68066803
if (kse->privKey == NULL)
68076804
ret = MEMORY_E;
@@ -6810,6 +6807,8 @@ static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
68106807
if (ret == 0) {
68116808
#if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
68126809
ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
6810+
kse->pubKeyLen = pSz;
6811+
kse->keyLen = pvtSz;
68136812
if (ret == 0) {
68146813
ret = wc_DhExportKeyPair(dhKey,
68156814
(byte*)kse->privKey, &kse->keyLen, /* private */
@@ -6823,6 +6822,8 @@ static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
68236822
/* For async this is called once and when event is done, the
68246823
* provided buffers will be populated.
68256824
* Final processing is zero pad below. */
6825+
kse->pubKeyLen = pSz;
6826+
kse->keyLen = pvtSz;
68266827
ret = DhGenKeyPair(ssl, dhKey,
68276828
(byte*)kse->privKey, &kse->keyLen, /* private */
68286829
kse->pubKey, &kse->pubKeyLen /* public */

0 commit comments

Comments
 (0)