Skip to content

Commit 2e8642d

Browse files
committed
PKCS7-verify example for PKCS7 DER and PEM.
1 parent ec73776 commit 2e8642d

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

pkcs7/pkcs7-verify.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@
2626

2727
#ifdef HAVE_PKCS7
2828

29+
static const char* pkcs7SignedDer = "signed.p7b"; /* DER */
30+
static const char* pkcs7SignedPem = "signed.p7s"; /* PEM */
31+
2932
int main(int argc, char** argv)
3033
{
3134
int rc = 0;
3235
PKCS7 pkcs7;
3336
XFILE derFile;
37+
byte* fileBuf = NULL;
38+
word32 fileSz = 0;
3439
byte* derBuf = NULL;
3540
word32 derSz = 0;
3641

@@ -41,35 +46,50 @@ int main(int argc, char** argv)
4146
wolfSSL_Debugging_ON();
4247
#endif
4348

44-
/* load DER PKCS7 */
45-
derFile = fopen("signed.p7s", "rb");
49+
/* load PKCS7 */
50+
derFile = fopen(pkcs7SignedPem, "rb");
4651
if (derFile) {
4752
fseek(derFile, 0, SEEK_END);
48-
derSz = (int)ftell(derFile);
53+
fileSz = (int)ftell(derFile);
4954
rewind(derFile);
5055

51-
derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
52-
if (derBuf == NULL) {
56+
fileBuf = (byte*)XMALLOC(fileSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
57+
derBuf = (byte*)XMALLOC(fileSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
58+
if (fileBuf == NULL || derBuf == NULL) {
5359
rc = MEMORY_E; goto exit;
5460
}
61+
derSz = fileSz;
5562

56-
rc = (int)fread(derBuf, 1, derSz, derFile);
63+
rc = (int)fread(fileBuf, 1, fileSz, derFile);
5764
fclose(derFile);
5865

59-
if (rc != derSz) {
66+
if (rc != fileSz) {
6067
printf("Failed to read der file!\n");
6168
return -1;
6269
}
6370
}
6471

65-
printf("Der %d\n", derSz);
66-
WOLFSSL_BUFFER(derBuf, derSz);
72+
/* PKCS_Init captures/saves this, so make sure
73+
* isDynamic = 0 since it is on the stack */
74+
pkcs7.isDynamic = 0;
6775

6876
/* Test verify */
6977
rc = wc_PKCS7_Init(&pkcs7, NULL, INVALID_DEVID);
7078
if (rc != 0) goto exit;
7179
rc = wc_PKCS7_InitWithCert(&pkcs7, NULL, 0);
7280
if (rc != 0) goto exit;
81+
82+
/* convert PEM to DER */
83+
rc = wc_CertPemToDer(fileBuf, fileSz, derBuf, derSz, PKCS7_TYPE);
84+
if (rc < 0) {
85+
goto exit;
86+
}
87+
derSz = rc;
88+
rc = 0;
89+
90+
printf("Der %d\n", derSz);
91+
WOLFSSL_BUFFER(derBuf, derSz);
92+
7393
rc = wc_PKCS7_VerifySignedData(&pkcs7, derBuf, derSz);
7494
if (rc != 0) goto exit;
7595

@@ -82,6 +102,7 @@ int main(int argc, char** argv)
82102

83103
wc_PKCS7_Free(&pkcs7);
84104
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
105+
XFREE(fileBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
85106

86107
return rc;
87108
}
@@ -94,4 +115,4 @@ int main(int argc, char** argv)
94115
return 0;
95116
}
96117

97-
#endif
118+
#endif

pkcs7/signed.p7s

624 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)