First pass at cleanup of the GetLength function handling of 0 length value. Added some asn.c build option comments.

This commit is contained in:
David Garske
2017-02-06 20:05:04 -08:00
parent 895bf8dfbc
commit 0286d157a7
2 changed files with 329 additions and 281 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -792,39 +792,39 @@ static int GetLength(const byte* input, word32* inOutIdx, int* len,
word32 maxIdx) word32 maxIdx)
{ {
int length = 0; int length = 0;
word32 i = *inOutIdx; word32 idx = *inOutIdx;
byte b; byte b;
*len = 0; /* default length */ *len = 0; /* default length */
if ( (i+1) > maxIdx) { /* for first read */ if ( (idx+1) > maxIdx) { /* for first read */
USER_DEBUG(("GetLength bad index on input\n")); USER_DEBUG(("GetLength bad index on input\n"));
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
} }
b = input[i++]; b = input[idx++];
if (b >= 0x80) { if (b >= 0x80) {
word32 bytes = b & 0x7F; word32 bytes = b & 0x7F;
if ( (i+bytes) > maxIdx) { /* for reading bytes */ if ( (idx+bytes) > maxIdx) { /* for reading bytes */
USER_DEBUG(("GetLength bad long length\n")); USER_DEBUG(("GetLength bad long length\n"));
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
} }
while (bytes--) { while (bytes--) {
b = input[i++]; b = input[idx++];
length = (length << 8) | b; length = (length << 8) | b;
} }
} }
else else
length = b; length = b;
if ( (i+length) > maxIdx) { /* for user of length */ if ( (idx+length) > maxIdx) { /* for user of length */
USER_DEBUG(("GetLength value exceeds buffer length\n")); USER_DEBUG(("GetLength value exceeds buffer length\n"));
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
} }
*inOutIdx = i; *inOutIdx = idx;
if (length > 0) if (length > 0)
*len = length; *len = length;
@@ -836,21 +836,28 @@ static int GetInt(IppsBigNumState** mpi, const byte* input, word32* inOutIdx,
word32 maxIdx) word32 maxIdx)
{ {
IppStatus ret; IppStatus ret;
word32 i = *inOutIdx; word32 idx = *inOutIdx;
byte b = input[i++]; byte b;
int length; int length;
int ctxSz; int ctxSz;
if ((idx + 1) > maxIdx)
return USER_CRYPTO_ERROR;
b = input[idx++];
if (b != 0x02) if (b != 0x02)
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
if (GetLength(input, &i, &length, maxIdx) < 0) if (GetLength(input, &idx, &length, maxIdx) < 0)
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
if ( (b = input[i++]) == 0x00) if (length > 0) {
length--; /* remove leading zero */
else if ( (b = input[i++]) == 0x00)
i--; length--;
else
idx--;
}
ret = ippsBigNumGetSize(length, &ctxSz); ret = ippsBigNumGetSize(length, &ctxSz);
if (ret != ippStsNoErr) if (ret != ippStsNoErr)
@@ -864,11 +871,11 @@ static int GetInt(IppsBigNumState** mpi, const byte* input, word32* inOutIdx,
if (ret != ippStsNoErr) if (ret != ippStsNoErr)
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
ret = ippsSetOctString_BN((Ipp8u*)input + i, length, *mpi); ret = ippsSetOctString_BN((Ipp8u*)input + idx, length, *mpi);
if (ret != ippStsNoErr) if (ret != ippStsNoErr)
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
*inOutIdx = i + length; *inOutIdx = idx + length;
return 0; return 0;
} }
@@ -879,6 +886,9 @@ static int GetSequence(const byte* input, word32* inOutIdx, int* len,
int length = -1; int length = -1;
word32 idx = *inOutIdx; word32 idx = *inOutIdx;
if ((idx + 1) > maxIdx)
return USER_CRYPTO_ERROR;
if (input[idx++] != (0x10 | 0x20) || if (input[idx++] != (0x10 | 0x20) ||
GetLength(input, &idx, &length, maxIdx) < 0) GetLength(input, &idx, &length, maxIdx) < 0)
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;
@@ -895,6 +905,9 @@ static int GetMyVersion(const byte* input, word32* inOutIdx,
{ {
word32 idx = *inOutIdx; word32 idx = *inOutIdx;
if (idx + MIN_VERSION_SZ > maxIdx)
return USER_CRYPTO_ERROR;
if (input[idx++] != 0x02) if (input[idx++] != 0x02)
return USER_CRYPTO_ERROR; return USER_CRYPTO_ERROR;