Skip to content

Commit d03347d

Browse files
committed
fix GetAsnTimeString() to correctly increment data_ptr, fixes PKCS#7 signedData signingTime attribute
1 parent 6e58ca3 commit d03347d

File tree

2 files changed

+99
-15
lines changed

2 files changed

+99
-15
lines changed

tests/api.c

+97-13
Original file line numberDiff line numberDiff line change
@@ -28630,16 +28630,111 @@ static int test_wc_PKCS7_VerifySignedData(void)
2863028630
word32 hashSz = wc_HashGetDigestSize(hashType);
2863128631

2863228632
#ifndef NO_RSA
28633+
PKCS7DecodedAttrib* decodedAttrib = NULL;
28634+
28635+
/* contentType OID (1.2.840.113549.1.9.3) */
28636+
static const byte contentTypeOid[] =
28637+
{ 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
28638+
28639+
/* PKCS#7 DATA content type (contentType defaults to DATA) */
28640+
static const byte dataType[] =
28641+
{ 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
28642+
28643+
/* messageDigest OID (1.2.840.113549.1.9.4) */
28644+
static const byte messageDigestOid[] =
28645+
{ 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
28646+
28647+
/* signingTime OID () */
28648+
static const byte signingTimeOid[] =
28649+
{ 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
28650+
28651+
#if !defined(NO_ASN) && !defined(NO_ASN_TIME)
28652+
int dateLength = 0;
28653+
byte dateFormat;
28654+
const byte* datePart = NULL;
28655+
struct tm timearg;
28656+
time_t now;
28657+
struct tm* nowTm = NULL;
28658+
struct tm tmpTimeStorage;
28659+
struct tm* tmpTime = &tmpTimeStorage;
28660+
#endif /* !NO_ASN && !NO_ASN_TIME */
28661+
2863328662
/* Success test with RSA certs/key */
2863428663
AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
2863528664
(word32)sizeof(data),
2863628665
0, 0, 0, RSA_TYPE)), 0);
2863728666

28667+
/* calculate hash for content, used later */
28668+
ret = wc_HashInit(&hash, hashType);
28669+
if (ret == 0) {
28670+
ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
28671+
if (ret == 0) {
28672+
ret = wc_HashFinal(&hash, hashType, hashBuf);
28673+
}
28674+
wc_HashFree(&hash, hashType);
28675+
}
28676+
AssertIntEQ(ret, 0);
28677+
2863828678
AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
2863928679
AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
2864028680
AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
2864128681
AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
28642-
#endif
28682+
28683+
/* Check that decoded signed attributes are correct */
28684+
28685+
/* messageDigest should be first */
28686+
decodedAttrib = pkcs7->decodedAttrib;
28687+
AssertNotNull(decodedAttrib);
28688+
AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
28689+
AssertIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
28690+
decodedAttrib->oidSz), 0);
28691+
/* + 2 for OCTET STRING and length bytes */
28692+
AssertIntEQ(decodedAttrib->valueSz, hashSz + 2);
28693+
AssertNotNull(decodedAttrib->value);
28694+
AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
28695+
28696+
/* signingTime should be second */
28697+
decodedAttrib = decodedAttrib->next;
28698+
AssertNotNull(decodedAttrib);
28699+
AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
28700+
AssertIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
28701+
decodedAttrib->oidSz), 0);
28702+
28703+
AssertIntGT(decodedAttrib->valueSz, 0);
28704+
AssertNotNull(decodedAttrib->value);
28705+
28706+
/* Verify signingTime if ASN and time are available */
28707+
#if !defined(NO_ASN) && !defined(NO_ASN_TIME)
28708+
AssertIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
28709+
&datePart, &dateFormat, &dateLength), 0);
28710+
AssertNotNull(datePart);
28711+
AssertIntGT(dateLength, 0);
28712+
XMEMSET(&timearg, 0, sizeof(timearg));
28713+
AssertIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
28714+
&timearg), 0);
28715+
28716+
/* Get current time and compare year/month/day against attribute value */
28717+
AssertIntEQ(wc_GetTime(&now, sizeof(now)), 0);
28718+
nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
28719+
AssertNotNull(nowTm);
28720+
28721+
AssertIntEQ(timearg.tm_year, nowTm->tm_year);
28722+
AssertIntEQ(timearg.tm_mon, nowTm->tm_mon);
28723+
AssertIntEQ(timearg.tm_mday, nowTm->tm_mday);
28724+
#endif /* !NO_ASN && !NO_ASN_TIME */
28725+
28726+
/* contentType should be third */
28727+
decodedAttrib = decodedAttrib->next;
28728+
AssertNotNull(decodedAttrib);
28729+
AssertIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
28730+
AssertIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
28731+
decodedAttrib->oidSz), 0);
28732+
AssertIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
28733+
AssertNotNull(decodedAttrib->value);
28734+
AssertIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType,
28735+
sizeof(dataType)), 0);
28736+
#endif /* !NO_RSA */
28737+
2864328738
#ifdef HAVE_ECC
2864428739
#ifndef NO_RSA
2864528740
wc_PKCS7_Free(pkcs7);
@@ -28656,7 +28751,7 @@ static int test_wc_PKCS7_VerifySignedData(void)
2865628751
AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
2865728752
AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
2865828753
AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
28659-
#endif
28754+
#endif /* HAVE_ECC */
2866028755

2866128756
/* Test bad args. */
2866228757
#if !defined(NO_RSA) || defined(HAVE_ECC)
@@ -28702,17 +28797,6 @@ static int test_wc_PKCS7_VerifySignedData(void)
2870228797

2870328798
/* verify using pre-computed content digest only (no content) */
2870428799
{
28705-
/* calculate hash for content */
28706-
ret = wc_HashInit(&hash, hashType);
28707-
if (ret == 0) {
28708-
ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
28709-
if (ret == 0) {
28710-
ret = wc_HashFinal(&hash, hashType, hashBuf);
28711-
}
28712-
wc_HashFree(&hash, hashType);
28713-
}
28714-
AssertIntEQ(ret, 0);
28715-
2871628800
AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
2871728801
AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
2871828802
AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,

wolfcrypt/src/asn.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -13605,15 +13605,15 @@ int GetAsnTimeString(void* currTime, byte* buf, word32 len)
1360513605
/* -1 below excludes null terminator */
1360613606
*data_ptr = (byte)ASN_UTC_TIME_SIZE - 1; data_ptr++; data_len++;
1360713607
XMEMCPY(data_ptr, (byte *)uf_time, ASN_UTC_TIME_SIZE - 1);
13608-
*data_ptr += ASN_UTC_TIME_SIZE - 1;
13608+
data_ptr += ASN_UTC_TIME_SIZE - 1;
1360913609
}
1361013610
else if (data_len == ASN_GENERALIZED_TIME_SIZE-1) {
1361113611
/* increment data_len for ASN length byte after adding the data_ptr */
1361213612
*data_ptr = (byte)ASN_GENERALIZED_TIME; data_ptr++; data_len++;
1361313613
/* -1 below excludes null terminator */
1361413614
*data_ptr = (byte)ASN_GENERALIZED_TIME_SIZE - 1; data_ptr++; data_len++;
1361513615
XMEMCPY(data_ptr, (byte*)uf_time, ASN_GENERALIZED_TIME_SIZE - 1);
13616-
*data_ptr += ASN_GENERALIZED_TIME_SIZE - 1;
13616+
data_ptr += ASN_GENERALIZED_TIME_SIZE - 1;
1361713617
}
1361813618
else {
1361913619
WOLFSSL_MSG("Invalid time size returned");

0 commit comments

Comments
 (0)