Skip to content

Commit 72dd37e

Browse files
update stream example for user CTX
1 parent 7f71706 commit 72dd37e

4 files changed

Lines changed: 84 additions & 47 deletions

File tree

pkcs7/envelopedData-ktri-stream.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,44 @@
3030

3131
#define encodedFileKTRI "envelopedDataKTRI-stream.der"
3232

33-
FILE *fileOut, *fileIn;
33+
typedef struct ExampleIO {
34+
FILE *fileOut;
35+
FILE *fileIn;
36+
} ExampleIO;
37+
static ExampleIO testIO;
38+
3439
#define TEST_SIZE 256
3540
static byte* contentRead = NULL;
3641

37-
static int GetContentCB(PKCS7* pkcs7, byte** content)
42+
static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
3843
{
3944
int ret;
45+
ExampleIO* io = (ExampleIO*)ctx;
46+
47+
if (io == NULL) {
48+
printf("Issue getting user ctx in content CB\n");
49+
return -1;
50+
}
4051

41-
ret = fread(contentRead, 1, TEST_SIZE, fileIn);
52+
ret = fread(contentRead, 1, TEST_SIZE, io->fileIn);
4253
*content = contentRead;
4354

4455
return ret;
4556
}
4657

4758

48-
static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz)
59+
static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
60+
void* ctx)
4961
{
62+
ExampleIO* io = (ExampleIO*)ctx;
63+
64+
if (io == NULL) {
65+
printf("Issue getting user ctx in stream output CB\n");
66+
return -1;
67+
}
68+
5069
if (outputSz > 0) {
51-
if (fwrite(output, 1, outputSz, fileOut) != outputSz) {
70+
if (fwrite(output, 1, outputSz, io->fileOut) != outputSz) {
5271
return -1;
5372
}
5473
}
@@ -97,7 +116,8 @@ static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
97116
pkcs7->encryptOID = AES256CBCb;
98117

99118
if (useStreamMode) {
100-
wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB, StreamOutputCB);
119+
wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB, StreamOutputCB,
120+
(void*)&testIO);
101121
}
102122

103123
/* add recipient using RSA certificate (KTRI type) */
@@ -187,7 +207,7 @@ int main(int argc, char** argv)
187207
#endif
188208

189209
if (argc != 2) {
190-
printf("USAGE: ./%s <content file name>\n", argv[0]);
210+
printf("USAGE: %s <content file name>\n", argv[0]);
191211
return -1;
192212
}
193213

@@ -196,16 +216,16 @@ int main(int argc, char** argv)
196216
return -1;
197217
}
198218

199-
fileIn = fopen(argv[1], "rb");
200-
if (fileIn == NULL) {
219+
testIO.fileIn = fopen(argv[1], "rb");
220+
if (testIO.fileIn == NULL) {
201221
printf("Issue opening file %s\n", argv[1]);
202222
return -1;
203223
}
204224

205-
fileOut = fopen(encodedFileKTRI, "wb");
206-
if (fileOut == NULL) {
225+
testIO.fileOut = fopen(encodedFileKTRI, "wb");
226+
if (testIO.fileOut == NULL) {
207227
printf("Issue opening file %s\n", encodedFileKTRI);
208-
fclose(fileIn);
228+
fclose(testIO.fileIn);
209229
return -1;
210230
}
211231

@@ -216,14 +236,14 @@ int main(int argc, char** argv)
216236
}
217237

218238
if (ret == 0) {
219-
fseek(fileIn, 0, SEEK_END);
220-
contentSz = ftell(fileIn);
221-
fseek(fileIn, 0, SEEK_SET);
239+
fseek(testIO.fileIn, 0, SEEK_END);
240+
contentSz = ftell(testIO.fileIn);
241+
fseek(testIO.fileIn, 0, SEEK_SET);
222242
printf("contentSz = %d\n", contentSz);
223243

224244
certSz = sizeof(cert);
225-
keySz = sizeof(key);
226-
ret = load_certs(cert, &certSz, key, &keySz);
245+
keySz = sizeof(key);
246+
ret = load_certs(cert, &certSz, key, &keySz);
227247
}
228248

229249
if (ret == 0) {
@@ -234,8 +254,8 @@ int main(int argc, char** argv)
234254
printf("Issue %d with encrypt\n", ret);
235255
}
236256
}
237-
fclose(fileIn);
238-
fclose(fileOut);
257+
fclose(testIO.fileIn);
258+
fclose(testIO.fileOut);
239259

240260

241261
#if 1
@@ -256,6 +276,7 @@ int main(int argc, char** argv)
256276
printf("error reading file %s\n", encodedFileKTRI);
257277
ret = -1;
258278
}
279+
printf("Read %d bytes for encrypted file found\n", encryptedSz);
259280
}
260281

261282
if (ret == 0) {

pkcs7/envelopedData-ktri.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
9595
pkcs7->encryptOID = AES256CBCb;
9696

9797
if (useStreamMode) {
98-
wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL);
98+
wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL);
9999
}
100100

101101
/* add recipient using RSA certificate (KTRI type) */

pkcs7/signedData-stream.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,42 @@
3131
#define encodedFile "signedData_stream.der"
3232

3333

34-
FILE *fileOut, *fileIn;
34+
typedef struct ExampleIO {
35+
FILE *fileOut;
36+
FILE *fileIn;
37+
} ExampleIO;
38+
static ExampleIO testIO;
39+
3540
#define TEST_SIZE 256
3641
static byte* contentRead = NULL;
3742

38-
static int GetContentCB(PKCS7* pkcs7, byte** content)
43+
static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
3944
{
4045
int ret;
46+
ExampleIO* io = (ExampleIO*)ctx;
4147

42-
ret = fread(contentRead, 1, TEST_SIZE, fileIn);
48+
if (io == NULL) {
49+
printf("Issue getting user ctx in content CB\n");
50+
return -1;
51+
}
52+
53+
ret = fread(contentRead, 1, TEST_SIZE, io->fileIn);
4354
*content = contentRead;
4455

4556
return ret;
4657
}
4758

4859

49-
static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz)
60+
static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
61+
void* ctx)
5062
{
63+
ExampleIO* io = (ExampleIO*)ctx;
64+
65+
if (io == NULL) {
66+
printf("Issue getting user ctx in stream output CB\n");
67+
return -1;
68+
}
69+
5170
if (outputSz > 0) {
5271
#if 0
5372
word32 z;
@@ -56,7 +75,7 @@ static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz)
5675
printf("\n");
5776
#endif
5877

59-
if (fwrite(output, 1, outputSz, fileOut) != outputSz) {
78+
if (fwrite(output, 1, outputSz, io->fileOut) != outputSz) {
6079
return -1;
6180
}
6281
}
@@ -141,7 +160,8 @@ static int signedData(byte* cert, word32 certSz, byte* key, word32 keySz,
141160
pkcs7->signedAttribsSz = sizeof(attribs)/sizeof(PKCS7Attrib);
142161

143162
/* use streaming mode with IO callbacks */
144-
wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB, StreamOutputCB);
163+
wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB, StreamOutputCB,
164+
(void*)&testIO);
145165

146166
/* encode signedData, returns size */
147167
ret = wc_PKCS7_EncodeSignedData_ex(pkcs7, contentHash, WC_SHA256_DIGEST_SIZE, NULL, &outputSz, NULL, NULL);
@@ -154,11 +174,6 @@ static int signedData(byte* cert, word32 certSz, byte* key, word32 keySz,
154174
} else {
155175
printf("Successfully encoded SignedData bundle (%s)\n",
156176
encodedFile);
157-
158-
#ifdef DEBUG_WOLFSSL
159-
printf("Encoded DER (%d bytes):\n", ret);
160-
//WOLFSSL_BUFFER(out, ret);
161-
#endif
162177
}
163178

164179
wc_PKCS7_Free(pkcs7);
@@ -192,11 +207,6 @@ static int signedData_verify(byte* in, word32 inSz, byte* cert,
192207
}
193208
} else {
194209
printf("Successfully verified SignedData bundle.\n");
195-
196-
#ifdef DEBUG_WOLFSSL
197-
printf("Decoded content (%d bytes):\n", pkcs7->contentSz);
198-
WOLFSSL_BUFFER(pkcs7->content, pkcs7->contentSz);
199-
#endif
200210
}
201211

202212
wc_PKCS7_Free(pkcs7);
@@ -218,6 +228,12 @@ int main(int argc, char** argv)
218228
byte *encrypted = NULL;
219229
byte *decrypted = NULL;
220230

231+
if (argc != 2) {
232+
printf("Expecting content file as input\n");
233+
printf("%s <content file name>\n", argv[0]);
234+
return -1;
235+
}
236+
221237
#ifdef DEBUG_WOLFSSL
222238
wolfSSL_Debugging_ON();
223239
#endif
@@ -228,19 +244,19 @@ int main(int argc, char** argv)
228244
}
229245

230246
if (ret == 0) {
231-
fileIn = fopen(argv[1], "rb");
232-
if (fileIn == NULL) {
247+
testIO.fileIn = fopen(argv[1], "rb");
248+
if (testIO.fileIn == NULL) {
233249
printf("Issue opening file %s\n", argv[1]);
234250
XFREE(contentRead, NULL, DYNAMIC_TYPE_TMP_BUFFER);
235251
return -1;
236252
}
237253
}
238254

239255
if (ret == 0) {
240-
fileOut = fopen(encodedFile, "wb");
241-
if (fileOut == NULL) {
256+
testIO.fileOut = fopen(encodedFile, "wb");
257+
if (testIO.fileOut == NULL) {
242258
printf("Issue opening file %s\n", encodedFile);
243-
fclose(fileIn);
259+
fclose(testIO.fileIn);
244260
XFREE(contentRead, NULL, DYNAMIC_TYPE_TMP_BUFFER);
245261
return -1;
246262
}
@@ -258,7 +274,7 @@ int main(int argc, char** argv)
258274

259275
if (ret == 0) {
260276
do {
261-
readSz = fread(contentRead, 1, TEST_SIZE, fileIn);
277+
readSz = fread(contentRead, 1, TEST_SIZE, testIO.fileIn);
262278
if (readSz > 0) {
263279
ret = wc_Sha256Update(&sha256, contentRead, readSz);
264280
if (ret != 0) {
@@ -278,8 +294,8 @@ int main(int argc, char** argv)
278294
wc_Sha256Free(&sha256);
279295
}
280296

281-
contentSz = ftell(fileIn);
282-
fseek(fileIn, 0, SEEK_SET);
297+
contentSz = ftell(testIO.fileIn);
298+
fseek(testIO.fileIn, 0, SEEK_SET);
283299
printf("contentSz = %d\n", contentSz);
284300

285301
if (ret == 0) {
@@ -294,8 +310,8 @@ int main(int argc, char** argv)
294310
contentHash);
295311
}
296312

297-
fclose(fileIn);
298-
fclose(fileOut);
313+
fclose(testIO.fileIn);
314+
fclose(testIO.fileOut);
299315
if (encryptedSz < 0) {
300316
ret = encryptedSz;
301317
printf("Error %d with signing data\n", ret);

pkcs7/signedData.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static int signedData_sign_noattrs(byte* cert, word32 certSz, byte* key,
120120
pkcs7->signedAttribsSz = 0;
121121

122122
if (streamMode) {
123-
wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL);
123+
wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL);
124124
}
125125

126126
if (noCerts) {

0 commit comments

Comments
 (0)