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
3641static 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 );
0 commit comments