Skip to content

Commit 81827df

Browse files
add example of no certs bundle and stream mode
1 parent 5a7c327 commit 81827df

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

pkcs7/envelopedData-ktri.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ static int write_file_buffer(const char* fileName, byte* in, word32 inSz)
7979
}
8080

8181
static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
82-
word32 keySz, byte* out, word32 outSz)
82+
word32 keySz, byte* out, word32 outSz,
83+
byte useStreamMode)
8384
{
8485
int ret;
8586
PKCS7* pkcs7;
@@ -93,6 +94,10 @@ static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
9394
pkcs7->contentOID = DATA;
9495
pkcs7->encryptOID = AES256CBCb;
9596

97+
if (useStreamMode) {
98+
wc_PKCS7_SetStreamMode(pkcs7, 1);
99+
}
100+
96101
/* add recipient using RSA certificate (KTRI type) */
97102
ret = wc_PKCS7_AddRecipient_KTRI(pkcs7, cert, certSz, 0);
98103
if (ret < 0) {
@@ -109,8 +114,8 @@ static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
109114
return -1;
110115

111116
} else {
112-
printf("Successfully encoded EnvelopedData bundle (%s)\n",
113-
encodedFileKTRI);
117+
printf("Successfully encoded EnvelopedData bundle (%s), stream mode"
118+
" %d\n", encodedFileKTRI, useStreamMode);
114119

115120
if (write_file_buffer(encodedFileKTRI, out, ret) != 0) {
116121
printf("ERROR: error writing encoded to output file\n");
@@ -177,7 +182,7 @@ int main(int argc, char** argv)
177182
byte key[2048];
178183
byte encrypted[1024];
179184
byte decrypted[1024];
180-
185+
181186
#ifdef DEBUG_WOLFSSL
182187
wolfSSL_Debugging_ON();
183188
#endif
@@ -189,10 +194,18 @@ int main(int argc, char** argv)
189194
return -1;
190195

191196
encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz,
192-
encrypted, sizeof(encrypted));
197+
encrypted, sizeof(encrypted), 0);
193198
if (encryptedSz < 0)
194199
return -1;
195200

201+
#ifdef ASN_BER_TO_DER
202+
/* recreate the bundle with BER encoding */
203+
encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz,
204+
encrypted, sizeof(encrypted), 1);
205+
if (encryptedSz < 0)
206+
return -1;
207+
#endif
208+
196209
#ifdef DEBUG_WOLFSSL
197210
printf("EnvelopedData DER (%d byte):\n", encryptedSz);
198211
WOLFSSL_BUFFER(encrypted, encryptedSz);

pkcs7/signedData.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ static int write_file_buffer(const char* fileName, byte* in, word32 inSz)
7979
}
8080

8181
static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
82-
word32 keySz, byte* out, word32 outSz)
82+
word32 keySz, byte* out, word32 outSz,
83+
byte streamMode, byte noCerts)
8384
{
8485
int ret;
8586
PKCS7* pkcs7;
@@ -118,6 +119,14 @@ static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
118119
pkcs7->signedAttribs = NULL;
119120
pkcs7->signedAttribsSz = 0;
120121

122+
if (streamMode) {
123+
wc_PKCS7_SetStreamMode(pkcs7, 1);
124+
}
125+
126+
if (noCerts) {
127+
wc_PKCS7_SetNoCerts(pkcs7, 1);
128+
}
129+
121130
/* encode signedData, returns size */
122131
ret = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
123132
if (ret <= 0) {
@@ -127,8 +136,8 @@ static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
127136
return -1;
128137

129138
} else {
130-
printf("Successfully encoded SignedData bundle (%s)\n",
131-
encodedFileNoAttrs);
139+
printf("Successfully encoded SignedData bundle (%s) %s\n",
140+
encodedFileNoAttrs, (noCerts)? "No Certs Added":"");
132141

133142
#ifdef DEBUG_WOLFSSL
134143
printf("Encoded DER (%d bytes):\n", ret);
@@ -244,10 +253,14 @@ static int signedData_verify(byte* in, word32 inSz, byte* cert,
244253

245254
if (ret < 0 || (pkcs7->contentSz != sizeof(data)) ||
246255
(XMEMCMP(pkcs7->content, data, pkcs7->contentSz) != 0)) {
247-
printf("ERROR: Failed to verify SignedData bundle, ret = %d\n", ret);
248-
wc_PKCS7_Free(pkcs7);
249-
return -1;
250-
256+
if (ret == PKCS7_SIGNEEDS_CHECK) {
257+
printf("WARNING: Parsed through bundle but no certificates found to"
258+
" verify signature with\n");
259+
}
260+
else {
261+
printf("ERROR: Failed to verify SignedData bundle, ret = %d\n",
262+
ret);
263+
}
251264
} else {
252265
printf("Successfully verified SignedData bundle.\n");
253266

@@ -287,7 +300,7 @@ int main(int argc, char** argv)
287300

288301
/* no attributes */
289302
encryptedSz = signedData_sign_noattrs(cert, certSz, key, keySz,
290-
encrypted, sizeof(encrypted));
303+
encrypted, sizeof(encrypted), 0, 0);
291304
if (encryptedSz < 0)
292305
return -1;
293306

@@ -297,6 +310,19 @@ int main(int argc, char** argv)
297310
if (decryptedSz < 0)
298311
return -1;
299312

313+
/* no attributes, stream mode, and no certs */
314+
encryptedSz = signedData_sign_noattrs(cert, certSz, key, keySz,
315+
encrypted, sizeof(encrypted), 1, 1);
316+
if (encryptedSz < 0)
317+
return -1;
318+
319+
decryptedSz = signedData_verify(encrypted, encryptedSz,
320+
cert, certSz, key, keySz,
321+
decrypted, sizeof(decrypted));
322+
/* should be error to warn that the signature needs checked */
323+
if (decryptedSz != PKCS7_SIGNEEDS_CHECK)
324+
return -1;
325+
300326
/* default attributes + messageType attribute */
301327
encryptedSz = signedData_sign_attrs(cert, certSz, key, keySz,
302328
encrypted, sizeof(encrypted));

0 commit comments

Comments
 (0)