@@ -28630,16 +28630,111 @@ static int test_wc_PKCS7_VerifySignedData(void)
28630
28630
word32 hashSz = wc_HashGetDigestSize(hashType);
28631
28631
28632
28632
#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
+
28633
28662
/* Success test with RSA certs/key */
28634
28663
AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
28635
28664
(word32)sizeof(data),
28636
28665
0, 0, 0, RSA_TYPE)), 0);
28637
28666
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
+
28638
28678
AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
28639
28679
AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
28640
28680
AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28641
28681
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
+
28643
28738
#ifdef HAVE_ECC
28644
28739
#ifndef NO_RSA
28645
28740
wc_PKCS7_Free(pkcs7);
@@ -28656,7 +28751,7 @@ static int test_wc_PKCS7_VerifySignedData(void)
28656
28751
AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
28657
28752
AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
28658
28753
AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
28659
- #endif
28754
+ #endif /* HAVE_ECC */
28660
28755
28661
28756
/* Test bad args. */
28662
28757
#if !defined(NO_RSA) || defined(HAVE_ECC)
@@ -28702,17 +28797,6 @@ static int test_wc_PKCS7_VerifySignedData(void)
28702
28797
28703
28798
/* verify using pre-computed content digest only (no content) */
28704
28799
{
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
-
28716
28800
AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
28717
28801
AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
28718
28802
AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
0 commit comments