Skip to content

Commit 17f1b3d

Browse files
authored
Merge pull request #425 from JacobBarthelmeh/pkcs7_stream
add content/stream callback example
2 parents 7afa73d + 5238018 commit 17f1b3d

8 files changed

Lines changed: 712 additions & 11 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ pkcs7/compressedData
177177
pkcs7/*.der
178178
pkcs7/envelopedData-kari
179179
pkcs7/envelopedData-ktri
180+
pkcs7/envelopedData-ktri-stream
180181
pkcs7/envelopedData-kekri
181182
pkcs7/envelopedData-pwri
182183
pkcs7/envelopedData-ori
183184
pkcs7/signedData
185+
pkcs7/signedData-stream
184186
pkcs7/signedData-cryptodev
185187
pkcs7/signedData-FirmwarePkgData
186188
pkcs7/signedData-DetachedSignature

pkcs7/envelopedData-ktri-stream.c

Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
/* envelopedData-ktri-stream.c
2+
*
3+
* Copyright (C) 2006-2024 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL. (formerly known as CyaSSL)
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
#ifndef WOLFSSL_USER_SETTINGS
23+
#include <wolfssl/options.h>
24+
#endif
25+
#include <wolfssl/wolfcrypt/settings.h>
26+
#include <wolfssl/wolfcrypt/pkcs7.h>
27+
#include <wolfssl/wolfcrypt/error-crypt.h>
28+
#include <wolfssl/wolfcrypt/logging.h>
29+
30+
#define certFile "../certs/client-cert.der"
31+
#define keyFile "../certs/client-key.der"
32+
33+
#define encodedFileKTRI "envelopedDataKTRI-stream.der"
34+
35+
typedef struct ExampleIO {
36+
FILE *fileOut;
37+
FILE *fileIn;
38+
} ExampleIO;
39+
static ExampleIO testIO;
40+
41+
#define TEST_SIZE 256
42+
static byte* contentRead = NULL;
43+
44+
static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
45+
{
46+
int ret;
47+
ExampleIO* io = (ExampleIO*)ctx;
48+
49+
if (io == NULL) {
50+
printf("Issue getting user ctx in content CB\n");
51+
return -1;
52+
}
53+
54+
ret = fread(contentRead, 1, TEST_SIZE, io->fileIn);
55+
*content = contentRead;
56+
57+
return ret;
58+
}
59+
60+
61+
static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
62+
void* ctx)
63+
{
64+
ExampleIO* io = (ExampleIO*)ctx;
65+
66+
if (io == NULL) {
67+
printf("Issue getting user ctx in stream output CB\n");
68+
return -1;
69+
}
70+
71+
if (outputSz > 0) {
72+
if (fwrite(output, 1, outputSz, io->fileOut) != outputSz) {
73+
return -1;
74+
}
75+
}
76+
return 0;
77+
}
78+
79+
80+
static int load_certs(byte* cert, word32* certSz, byte* key, word32* keySz)
81+
{
82+
FILE* file;
83+
84+
/* certificate file */
85+
file = fopen(certFile, "rb");
86+
if (!file)
87+
return -1;
88+
89+
*certSz = (word32)fread(cert, 1, *certSz, file);
90+
fclose(file);
91+
92+
/* key file */
93+
file = fopen(keyFile, "rb");
94+
if (!file)
95+
return -1;
96+
97+
*keySz = (word32)fread(key, 1, *keySz, file);
98+
fclose(file);
99+
100+
return 0;
101+
}
102+
103+
104+
static int envelopedData_encrypt(byte* cert, word32 certSz, byte* key,
105+
word32 keySz, byte* out, word32 outSz,
106+
word32 contentSz, byte useStreamMode)
107+
{
108+
int ret;
109+
PKCS7* pkcs7;
110+
111+
pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
112+
if (pkcs7 == NULL)
113+
return -1;
114+
115+
pkcs7->content = NULL;
116+
pkcs7->contentSz = contentSz;
117+
pkcs7->contentOID = DATA;
118+
pkcs7->encryptOID = AES256CBCb;
119+
120+
if (useStreamMode) {
121+
wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB, StreamOutputCB,
122+
(void*)&testIO);
123+
}
124+
125+
/* add recipient using RSA certificate (KTRI type) */
126+
ret = wc_PKCS7_AddRecipient_KTRI(pkcs7, cert, certSz, 0);
127+
if (ret < 0) {
128+
printf("wc_PKCS7_AddRecipient_KTRI() failed, ret = %d\n", ret);
129+
wc_PKCS7_Free(pkcs7);
130+
return -1;
131+
}
132+
133+
/* encode envelopedData, returns size */
134+
ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
135+
if (ret <= 0) {
136+
printf("ERROR: wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
137+
wc_PKCS7_Free(pkcs7);
138+
return -1;
139+
140+
} else {
141+
printf("Successfully encoded EnvelopedData bundle (%s), stream mode"
142+
" %d\n", encodedFileKTRI, useStreamMode);
143+
}
144+
145+
wc_PKCS7_Free(pkcs7);
146+
147+
return ret;
148+
}
149+
150+
static int envelopedData_decrypt(byte* in, word32 inSz, byte* cert,
151+
word32 certSz, byte* key, word32 keySz,
152+
byte* out, word32 outSz)
153+
{
154+
int ret;
155+
PKCS7* pkcs7;
156+
157+
pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
158+
if (pkcs7 == NULL)
159+
return -1;
160+
161+
/* init with recipient cert */
162+
ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
163+
if (ret != 0) {
164+
wc_PKCS7_Free(pkcs7);
165+
return -1;
166+
}
167+
168+
/* set recipient private key */
169+
ret = wc_PKCS7_SetKey(pkcs7, key, keySz);
170+
if (ret != 0) {
171+
wc_PKCS7_Free(pkcs7);
172+
return -1;
173+
}
174+
175+
/* decode envelopedData, returns size */
176+
ret = wc_PKCS7_DecodeEnvelopedData(pkcs7, in, inSz, out, outSz);
177+
if (ret <= 0) {
178+
printf("Failed to decode EnvelopedData bundle (%s), error %d\n",
179+
encodedFileKTRI, ret);
180+
wc_PKCS7_Free(pkcs7);
181+
return -1;
182+
} else {
183+
printf("Successfully decoded EnvelopedData bundle (%s)\n",
184+
encodedFileKTRI);
185+
}
186+
187+
wc_PKCS7_Free(pkcs7);
188+
189+
return ret;
190+
}
191+
192+
#if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
193+
194+
#define EXTRA_ASN1_SIZE 1024
195+
#define DEFAULT_EXAMPLE_BUFFER_SIZE 2048
196+
197+
int main(int argc, char** argv)
198+
{
199+
int ret = 0;
200+
int encryptedSz = 0, decryptedSz;
201+
word32 certSz, keySz, contentSz = 0;
202+
203+
byte cert[DEFAULT_EXAMPLE_BUFFER_SIZE];
204+
byte key[DEFAULT_EXAMPLE_BUFFER_SIZE];
205+
byte* encrypted = NULL;
206+
byte* decrypted = NULL;
207+
208+
#ifdef DEBUG_WOLFSSL
209+
wolfSSL_Debugging_ON();
210+
#endif
211+
212+
if (argc != 2) {
213+
printf("USAGE: %s <content file name>\n", argv[0]);
214+
return -1;
215+
}
216+
217+
if (wolfCrypt_Init() != 0) {
218+
printf("Issue with wolfcrypt init\n");
219+
return -1;
220+
}
221+
222+
testIO.fileIn = fopen(argv[1], "rb");
223+
if (testIO.fileIn == NULL) {
224+
printf("Issue opening file %s\n", argv[1]);
225+
return -1;
226+
}
227+
228+
testIO.fileOut = fopen(encodedFileKTRI, "wb");
229+
if (testIO.fileOut == NULL) {
230+
printf("Issue opening file %s\n", encodedFileKTRI);
231+
fclose(testIO.fileIn);
232+
return -1;
233+
}
234+
235+
contentRead = (byte*)XMALLOC(TEST_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
236+
if (contentRead == NULL) {
237+
printf("Unable to malloc content read buffer\n");
238+
ret = MEMORY_E;
239+
}
240+
241+
if (ret == 0) {
242+
fseek(testIO.fileIn, 0, SEEK_END);
243+
contentSz = ftell(testIO.fileIn);
244+
fseek(testIO.fileIn, 0, SEEK_SET);
245+
printf("contentSz = %d\n", contentSz);
246+
247+
certSz = sizeof(cert);
248+
keySz = sizeof(key);
249+
ret = load_certs(cert, &certSz, key, &keySz);
250+
}
251+
252+
if (ret == 0) {
253+
encryptedSz = envelopedData_encrypt(cert, certSz, key, keySz,
254+
encrypted, encryptedSz, contentSz, 1);
255+
if (encryptedSz < 0) {
256+
ret = encryptedSz;
257+
printf("Issue %d with encrypt\n", ret);
258+
}
259+
}
260+
fclose(testIO.fileIn);
261+
fclose(testIO.fileOut);
262+
263+
264+
#if 1
265+
decryptedSz = encryptedSz = contentSz + EXTRA_ASN1_SIZE;
266+
if (ret == 0) {
267+
encrypted = (byte*)XMALLOC(encryptedSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
268+
decrypted = (byte*)XMALLOC(decryptedSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
269+
if (encrypted == NULL || decrypted == NULL) {
270+
ret = MEMORY_E;
271+
}
272+
}
273+
274+
if (ret == 0) {
275+
FILE* f = fopen(encodedFileKTRI, "rb");
276+
encryptedSz = fread(encrypted, 1, encryptedSz, f);
277+
fclose(f);
278+
if (encryptedSz <= 0) {
279+
printf("error reading file %s\n", encodedFileKTRI);
280+
ret = -1;
281+
}
282+
printf("Read %d bytes for encrypted file found\n", encryptedSz);
283+
}
284+
285+
if (ret == 0) {
286+
decryptedSz = envelopedData_decrypt(encrypted, encryptedSz,
287+
cert, certSz, key, keySz,
288+
decrypted, decryptedSz);
289+
if (decryptedSz < 0) {
290+
ret = decryptedSz;
291+
printf("Issue %d with decrypt\n", ret);
292+
}
293+
}
294+
295+
#endif
296+
297+
if (contentRead != NULL)
298+
XFREE(contentRead, NULL, DYNAMIC_TYPE_TMP_BUFFER);
299+
if (encrypted != NULL)
300+
XFREE(encrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER);
301+
if (decrypted != NULL)
302+
XFREE(decrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER);
303+
304+
return ret;
305+
}
306+
307+
#else
308+
309+
int main(int argc, char** argv)
310+
{
311+
printf("Must build wolfSSL using ./configure --enable-pkcs7 --enable-indef\n");
312+
return 0;
313+
}
314+
315+
#endif
316+

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);
98+
wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL);
9999
}
100100

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

pkcs7/signedData-cryptocb.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
* along with this program; if not, write to the Free Software
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2020
*/
21-
#include <wolfssl/options.h>
21+
22+
#ifndef WOLFSSL_USER_SETTINGS
23+
#include <wolfssl/options.h>
24+
#endif
2225
#include <wolfssl/wolfcrypt/settings.h>
2326
#include <wolfssl/wolfcrypt/pkcs7.h>
2427
#include <wolfssl/wolfcrypt/error-crypt.h>

pkcs7/signedData-p7b.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* signedData-p7b.c
22
*
3-
* Copyright (C) 2006-2020 wolfSSL Inc.
3+
* Copyright (C) 2006-2024 wolfSSL Inc.
44
*
55
* This file is part of wolfSSL. (formerly known as CyaSSL)
66
*
@@ -28,7 +28,10 @@
2828
* This is only provided as an example and may need modification if integrated
2929
* into a production application.
3030
*/
31-
#include <wolfssl/options.h>
31+
32+
#ifndef WOLFSSL_USER_SETTINGS
33+
#include <wolfssl/options.h>
34+
#endif
3235
#include <wolfssl/wolfcrypt/settings.h>
3336
#include <wolfssl/wolfcrypt/logging.h>
3437
#include <wolfssl/wolfcrypt/pkcs7.h>
@@ -114,8 +117,8 @@ int main(int argc, char** argv)
114117
singleCertPemSz = wc_DerToPem(singleCertDer, singleCertDerSz,
115118
singleCertPem, singleCertPemSz,
116119
CERT_TYPE);
117-
if (singleCertPem < 0) {
118-
printf("Error converting DER to PEM, ret = %d\n", ret);
120+
if (singleCertPemSz < 0) {
121+
printf("Error converting DER to PEM, ret = %d\n", singleCertPemSz);
119122
XFREE(singleCertPem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
120123
break;
121124
}

0 commit comments

Comments
 (0)