Skip to content

Commit 9030316

Browse files
committed
Add size check in wh_Client_Curve25519SharedSecret
1 parent 80ee2d5 commit 9030316

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/wh_client_crypto.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,14 +2826,33 @@ int wh_Client_Curve25519SharedSecret(whClientContext* ctx,
28262826
/* wolfCrypt allows positive error codes on success in some
28272827
* scenarios */
28282828
if (ret >= 0) {
2829-
if (out_size != NULL) {
2829+
uint8_t* res_out = (uint8_t*)(res + 1);
2830+
const size_t hdr_sz =
2831+
sizeof(whMessageCrypto_GenericResponseHeader) +
2832+
sizeof(*res);
2833+
/* Defensive bound: res->sz must fit within the actual
2834+
* received frame */
2835+
if (res_len < hdr_sz ||
2836+
res->sz > (res_len - hdr_sz)) {
2837+
ret = WH_ERROR_ABORTED;
2838+
}
2839+
else if ((out != NULL) && (out_size != NULL) &&
2840+
(res->sz > *out_size)) {
2841+
/* Output buffer too small. Report required size and
2842+
* fail rather than silently truncating X25519 key
2843+
* material. */
28302844
*out_size = res->sz;
2845+
ret = WH_ERROR_BUFFER_SIZE;
28312846
}
2832-
if (out != NULL) {
2833-
uint8_t* res_out = (uint8_t*)(res + 1);
2834-
memcpy(out, res_out, res->sz);
2835-
WH_DEBUG_VERBOSE_HEXDUMP("[client] X25519:", res_out,
2836-
res->sz);
2847+
else {
2848+
if (out_size != NULL) {
2849+
*out_size = res->sz;
2850+
}
2851+
if ((out != NULL) && (res->sz > 0)) {
2852+
memcpy(out, res_out, res->sz);
2853+
WH_DEBUG_VERBOSE_HEXDUMP("[client] X25519:",
2854+
res_out, res->sz);
2855+
}
28372856
}
28382857
}
28392858
}

test/a.out

128 KB
Binary file not shown.

0 commit comments

Comments
 (0)