From 1ca62ee0a1835a409d67741a05091115dd22cb04 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Tue, 15 May 2018 22:51:11 -0600 Subject: [PATCH 1/6] add error return for unsuported version --- wolfcrypt/src/asn.c | 1 + 1 file changed, 1 insertion(+) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index fafaf3f21..a9f689adf 100755 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -2600,6 +2600,7 @@ int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz, if (version == PKCS5v2) { WOLFSSL_MSG("PKCS5v2 Not supported yet\n"); + return ASN_VERSION_E; } if (salt == NULL || saltSz <= 0) { From 566bb4cefe0edcb26d9ef5973fb5782e992840f3 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 16 May 2018 08:38:50 -0600 Subject: [PATCH 2/6] version for PBE SHA1 DES oid --- wolfcrypt/src/asn.c | 4 ---- wolfcrypt/src/pkcs12.c | 7 ++++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index a9f689adf..28b7a5666 100755 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -2394,10 +2394,6 @@ static int CheckAlgo(int first, int second, int* id, int* version) return 0; #endif #ifndef NO_DES3 - case PBE_SHA1_DES: - *id = PBE_SHA1_DES; - *version = PKCS12v1; - return 0; case PBE_SHA1_DES3: *id = PBE_SHA1_DES3; *version = PKCS12v1; diff --git a/wolfcrypt/src/pkcs12.c b/wolfcrypt/src/pkcs12.c index 80f80d5c5..aebf2ed58 100644 --- a/wolfcrypt/src/pkcs12.c +++ b/wolfcrypt/src/pkcs12.c @@ -1150,7 +1150,7 @@ static int wc_PKCS12_shroud_key(WC_PKCS12* pkcs12, WC_RNG* rng, { void* heap; word32 tmpIdx = 0; - int vPKCS = 1; /* PKCS#12 is always set to 1 */ + int vPKCS = 1; /* PKCS#12 default set to 1 */ word32 sz; word32 totalSz = 0; int ret; @@ -1190,6 +1190,11 @@ static int wc_PKCS12_shroud_key(WC_PKCS12* pkcs12, WC_RNG* rng, else { WOLFSSL_MSG("creating PKCS12 Shrouded Key Bag"); + if (vAlgo == PBE_SHA1_DES) { + vPKCS = PKCS5; + vAlgo = 10; + } + ret = UnTraditionalEnc(key, keySz, out + tmpIdx, &sz, pass, passSz, vPKCS, vAlgo, NULL, 0, itt, rng, heap); } From d373844a18a4cdab781678d7afc75aa56ac15d96 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 16 May 2018 10:16:15 -0600 Subject: [PATCH 3/6] fix sequence with pkcs12 shrouded keybag creation --- wolfcrypt/src/asn.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 28b7a5666..707f2cbde 100755 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -2591,9 +2591,6 @@ int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz, MAX_LENGTH_SZ + MAX_SHORT_SZ + 1) return BUFFER_E; - sz = SetAlgoID(id, out + inOutIdx, oidPBEType, 0); - totalSz += sz; inOutIdx += sz; - if (version == PKCS5v2) { WOLFSSL_MSG("PKCS5v2 Not supported yet\n"); return ASN_VERSION_E; @@ -2621,6 +2618,7 @@ int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz, /* leave room for a sequence (contains salt and iterations int) */ inOutIdx += MAX_SEQ_SZ; sz = 0; + inOutIdx += MAX_ALGO_SZ; /* place salt in buffer */ out[inOutIdx++] = ASN_OCTET_STRING; sz++; @@ -2642,7 +2640,13 @@ int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz, inOutIdx -= (sz + MAX_SEQ_SZ); tmpSz = SetSequence(sz, out + inOutIdx); XMEMMOVE(out + inOutIdx + tmpSz, out + inOutIdx + MAX_SEQ_SZ, sz); - inOutIdx += tmpSz + sz; totalSz += tmpSz + sz; + totalSz += tmpSz + sz; sz += tmpSz; + + /* add in algo ID */ + inOutIdx -= MAX_ALGO_SZ; + tmpSz = SetAlgoID(id, out + inOutIdx, oidPBEType, sz); + XMEMMOVE(out + inOutIdx + tmpSz, out + inOutIdx + MAX_ALGO_SZ, sz); + totalSz += tmpSz; inOutIdx += tmpSz + sz; /* octet string containing encrypted key */ out[inOutIdx++] = ASN_OCTET_STRING; totalSz++; From e1745428aca7de4646e334944374766bf76e2740 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 16 May 2018 20:16:40 -0600 Subject: [PATCH 4/6] add set short int helper function --- wolfcrypt/src/asn.c | 70 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 13 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 707f2cbde..f6387c105 100755 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -646,6 +646,51 @@ int GetShortInt(const byte* input, word32* inOutIdx, int* number, word32 maxIdx) return *number; } + + +/* Set small integer, 32 bits or less. DER encoding with no leading 0s + * returns total amount written including ASN tag and length byte on success */ +static int SetShortInt(byte* input, word32* inOutIdx, word32 number, + word32 maxIdx) +{ + word32 idx = *inOutIdx; + word32 len = 0; + int i; + byte ar[MAX_LENGTH_SZ]; + + /* check for room for type and length bytes */ + if ((idx + 2) > maxIdx) + return BUFFER_E; + + input[idx++] = ASN_INTEGER; + idx++; /* place holder for length byte */ + if (MAX_LENGTH_SZ + idx > maxIdx) + return ASN_PARSE_E; + + /* find first non zero byte */ + XMEMSET(ar, 0, MAX_LENGTH_SZ); + c32toa(number, ar); + for (i = 0; i < MAX_LENGTH_SZ; i++) { + if (ar[i] != 0) { + break; + } + } + + /* handle case of 0 */ + if (i == MAX_LENGTH_SZ) { + input[idx++] = 0; len++; + } + + for (; i < MAX_LENGTH_SZ && idx < maxIdx; i++) { + input[idx++] = ar[i]; len++; + } + + /* set number of bytes for integer and update index value */ + input[*inOutIdx + 1] = len; + *inOutIdx = idx; + + return len + 2; /* size of integer bytes plus ASN TAG and length byte */ +} #endif /* !NO_PWDBASED */ /* May not have one, not an error */ @@ -2628,13 +2673,11 @@ int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz, inOutIdx += saltSz; sz += saltSz; /* place iteration count in buffer */ - out[inOutIdx++] = ASN_INTEGER; sz++; - out[inOutIdx++] = sizeof(word32); sz++; - out[inOutIdx++] = (itt >> 24) & 0xFF; - out[inOutIdx++] = (itt >> 16) & 0xFF; - out[inOutIdx++] = (itt >> 8 ) & 0xFF; - out[inOutIdx++] = itt & 0xFF; - sz += 4; + ret = SetShortInt(out, &inOutIdx, itt, *outSz); + if (ret < 0) { + return ret; + } + sz += (word32)ret; /* wind back index and set sequence then clean up buffer */ inOutIdx -= (sz + MAX_SEQ_SZ); @@ -3037,12 +3080,13 @@ int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz, tmpIdx += saltSz; /* place itteration setting in buffer */ - out[tmpIdx++] = ASN_INTEGER; - out[tmpIdx++] = sizeof(word32); - out[tmpIdx++] = (itt >> 24) & 0xFF; - out[tmpIdx++] = (itt >> 16) & 0xFF; - out[tmpIdx++] = (itt >> 8) & 0xFF; - out[tmpIdx++] = itt & 0xFF; + ret = SetShortInt(out, &tmpIdx, itt, *outSz); + if (ret < 0) { + #ifdef WOLFSSL_SMALL_STACK + XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif + return ret; + } /* rewind and place sequence */ sz = tmpIdx - inOutIdx - MAX_SEQ_SZ; From b6a92a97ce56e2bdf08f7346e2b95f76b251ec0a Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Thu, 17 May 2018 08:55:07 -0600 Subject: [PATCH 5/6] convert to byte to fix warning --- wolfcrypt/src/asn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index f6387c105..2e5cb96e9 100755 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -686,7 +686,7 @@ static int SetShortInt(byte* input, word32* inOutIdx, word32 number, } /* set number of bytes for integer and update index value */ - input[*inOutIdx + 1] = len; + input[*inOutIdx + 1] = (byte)len; *inOutIdx = idx; return len + 2; /* size of integer bytes plus ASN TAG and length byte */ From 902109189641891faa1dcdf84842138b957c5962 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 23 May 2018 14:04:41 -0600 Subject: [PATCH 6/6] update comments --- wolfcrypt/src/asn.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 2e5cb96e9..e3ab7e545 100755 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -685,7 +685,8 @@ static int SetShortInt(byte* input, word32* inOutIdx, word32 number, input[idx++] = ar[i]; len++; } - /* set number of bytes for integer and update index value */ + /* jump back to beginning of input buffer using unaltered inOutIdx value + * and set number of bytes for integer, then update the index value */ input[*inOutIdx + 1] = (byte)len; *inOutIdx = idx;