ASN: DH private key encoding

Proper fix for sequence length when small keys.
This commit is contained in:
Sean Parkinson
2021-11-17 14:13:02 +10:00
parent 995ef60ff1
commit 370570d19b

View File

@ -8313,7 +8313,7 @@ int wc_DhKeyToDer(DhKey* key, byte* output, word32* outSz, int exportPriv)
{
#ifndef WOLFSSL_ASN_TEMPLATE
int ret, privSz = 0, pubSz = 0, keySz;
word32 idx, total;
word32 idx, len, total;
if (key == NULL || outSz == NULL) {
return BAD_FUNC_ARG;
@ -8342,8 +8342,9 @@ int wc_DhKeyToDer(DhKey* key, byte* output, word32* outSz, int exportPriv)
/* object dhKeyAgreement 1.2.840.113549.1.3.1 */
idx += SetObjectId(sizeof(keyDhOid), NULL);
idx += sizeof(keyDhOid);
len = idx - keySz;
/* sequence - all but pub/priv */
idx += SetSequence(idx - keySz, NULL);
idx += SetSequence(len, NULL);
if (exportPriv) {
/* version: 0 (ASN_INTEGER, 0x01, 0x00) */
idx += 3;
@ -8370,7 +8371,7 @@ int wc_DhKeyToDer(DhKey* key, byte* output, word32* outSz, int exportPriv)
idx += SetMyVersion(0, output + idx, 0);
}
/* sequence - all but pub/priv */
idx += SetSequence(total - keySz - idx, output + idx);
idx += SetSequence(len, output + idx);
/* object dhKeyAgreement 1.2.840.113549.1.3.1 */
idx += SetObjectId(sizeof(keyDhOid), output + idx);
XMEMCPY(output + idx, keyDhOid, sizeof(keyDhOid));