Skip to content

Commit fad7bc2

Browse files
add detatched content option for smime-verify
1 parent fa28f24 commit fad7bc2

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

pkcs7/smime-verify.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#ifdef HAVE_SMIME
3131

32-
static int Verify(byte* smime, int smimeSz, byte* ca, int caSz, int detached)
32+
static int Verify(byte* smime, int smimeSz, byte* ca, int caSz, byte* contentIn, int contentInSz, int detached)
3333
{
3434
WOLFSSL_PKCS7* pkcs7Compat = NULL;
3535
WOLFSSL_BIO *in, *content = NULL;
@@ -90,6 +90,12 @@ static int Verify(byte* smime, int smimeSz, byte* ca, int caSz, int detached)
9090
}
9191
}
9292

93+
if (ret == 0 && contentIn != NULL) {
94+
pkcs7Compat->pkcs7.content = contentIn;
95+
pkcs7Compat->pkcs7.contentSz = contentInSz;
96+
wc_PKCS7_SetDetached(&pkcs7Compat->pkcs7, 1);
97+
}
98+
9399
if (ret == 0) {
94100
content = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
95101
ret = wolfSSL_PKCS7_verify((PKCS7*)pkcs7Compat, NULL, store, NULL,
@@ -133,8 +139,9 @@ static int Verify(byte* smime, int smimeSz, byte* ca, int caSz, int detached)
133139

134140

135141
/* read private smime and signer certificate in DER format */
136-
static int ReadSmimeAndCert(char* smimeFile, char* certFile, byte* smime,
137-
int* smimeSz, byte* cert, int* certSz)
142+
static int ReadSmimeAndCert(char* smimeFile, char* certFile, char* contentFile,
143+
byte* smime,
144+
int* smimeSz, byte* cert, int* certSz, byte* content, int* contentSz)
138145
{
139146
int ret;
140147
XFILE f;
@@ -181,21 +188,45 @@ static int ReadSmimeAndCert(char* smimeFile, char* certFile, byte* smime,
181188
}
182189
}
183190

191+
f = XFOPEN(contentFile, "rb");
192+
if (f == NULL) {
193+
printf("Error opening file %s\n", contentFile);
194+
return -1;
195+
}
196+
else {
197+
ret = XFREAD(content, 1, *contentSz, f);
198+
if (ret >= 0) {
199+
if (ret == *contentSz) {
200+
printf("Cert read in was larger than buffer\n");
201+
XFCLOSE(f);
202+
return -1;
203+
}
204+
else {
205+
*contentSz = ret;
206+
ret = 0;
207+
XFCLOSE(f);
208+
}
209+
}
210+
}
211+
184212
return ret;
185213
}
186214

187215
int main(int argc, char** argv)
188216
{
189-
byte cert[2048];
190-
int certSz = 2048;
217+
byte cert[4096];
218+
int certSz = 4096;
219+
220+
byte smime[10000];
221+
int smimeSz = 10000;
191222

192-
byte smime[3072];
193-
int smimeSz = 3072;
223+
byte content[10000];
224+
int contentSz = 10000;
194225

195226
int ret;
196227

197-
if (argc != 3) {
198-
printf("Use ./smime-verify <smime file> <der cert file>\n");
228+
if (argc != 4) {
229+
printf("Use ./smime-verify <smime file> <der cert file> <content file>\n");
199230
return -1;
200231
}
201232

@@ -204,9 +235,10 @@ int main(int argc, char** argv)
204235
return -1;
205236
}
206237

207-
ret = ReadSmimeAndCert(argv[1], argv[2], smime, &smimeSz, cert, &certSz);
238+
ret = ReadSmimeAndCert(argv[1], argv[2], argv[3], smime, &smimeSz, cert,
239+
&certSz, content, &contentSz);
208240
if (ret == 0) {
209-
ret = Verify(smime, smimeSz, cert, certSz, 0);
241+
ret = Verify(smime, smimeSz, cert, certSz, content, contentSz, 0);
210242
if (ret == 0) {
211243
printf("Verify Success\n");
212244
}

0 commit comments

Comments
 (0)