Skip to content

Commit 21cc2df

Browse files
add example verify of smime
1 parent 54286ea commit 21cc2df

3 files changed

Lines changed: 204 additions & 1 deletion

File tree

pkcs7/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ clean:
4949
envelopedDataPWRI.der envelopedDataORI.der envelopedDataKEKRI.der \
5050
authEnvelopedDataKARI.der authEnvelopedDataKTRI.der \
5151
authEnvelopedDataORI.der authEnvelopedDataPWRI.der encryptedData.der \
52-
authEnvelopedDataKEKRI.der compressedData.der
52+
authEnvelopedDataKEKRI.der compressedData.der \
53+
smime-created.p7s

pkcs7/smime-verify.c

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/* smime-verify.c
2+
*
3+
* Copyright (C) 2006-2020 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+
#include <wolfssl/options.h>
23+
#include <wolfssl/wolfcrypt/settings.h>
24+
#include <wolfssl/ssl.h>
25+
#include <wolfssl/wolfcrypt/pkcs7.h>
26+
#include <wolfssl/wolfcrypt/error-crypt.h>
27+
#include <wolfssl/wolfcrypt/logging.h>
28+
29+
30+
#ifdef HAVE_SMIME
31+
32+
static int Verify(byte* smime, int smimeSz, byte* ca, int caSz, int detached)
33+
{
34+
WOLFSSL_PKCS7* pkcs7Compat = NULL;
35+
WOLFSSL_BIO *in, *content = NULL;
36+
WOLFSSL_X509* x509 = NULL;
37+
WOLFSSL_X509_STORE* store = NULL;
38+
int ret = 0;
39+
40+
in = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
41+
if (in == NULL) {
42+
ret = MEMORY_E;
43+
}
44+
45+
if (ret == 0) {
46+
WOLFSSL_BIO *multi = NULL;
47+
48+
wolfSSL_BIO_write(in, smime, smimeSz);
49+
pkcs7Compat = (WOLFSSL_PKCS7*)wolfSSL_SMIME_read_PKCS7(in, &multi);
50+
if (pkcs7Compat == NULL) {
51+
printf("Error parsing SMIME\n");
52+
ret = -1;
53+
}
54+
55+
if (multi != NULL) {
56+
byte* pt;
57+
int ptSz, i;
58+
59+
printf("Multi part message, signed data is : ");
60+
ptSz = wolfSSL_BIO_get_mem_data(multi, &pt);
61+
for (i = 0; i < ptSz; i ++)
62+
printf("%02X", pt[i]);
63+
printf("\n");
64+
wolfSSL_BIO_free(multi);
65+
}
66+
}
67+
68+
if (ret == 0) {
69+
const unsigned char* pt;
70+
71+
/* set devID */
72+
pkcs7Compat->pkcs7.devId = INVALID_DEVID;
73+
74+
pt = ca;
75+
x509 = wolfSSL_d2i_X509(NULL, &pt, caSz);
76+
if (x509 == NULL) {
77+
printf("Error decoding signer\n");
78+
ret = -1;
79+
}
80+
}
81+
82+
if (ret == 0) {
83+
store = wolfSSL_X509_STORE_new();
84+
if (store == NULL) {
85+
printf("Error creating cert store\n");
86+
ret = MEMORY_E;
87+
}
88+
else {
89+
wolfSSL_X509_STORE_add_cert(store, x509);
90+
}
91+
}
92+
93+
if (ret == 0) {
94+
content = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
95+
ret = wolfSSL_PKCS7_verify((PKCS7*)pkcs7Compat, NULL, store, NULL,
96+
content, detached);
97+
if (ret == WOLFSSL_SUCCESS) {
98+
ret = 0;
99+
}
100+
}
101+
102+
103+
if (ret == 0 && content != NULL) {
104+
byte* pt;
105+
int ptSz, i;
106+
107+
printf("Content found on verify : ");
108+
ptSz = wolfSSL_BIO_get_mem_data(content, &pt);
109+
for (i = 0; i < ptSz; i ++)
110+
printf("%c", pt[i]);
111+
printf("\n");
112+
}
113+
114+
wolfSSL_BIO_free(in);
115+
wolfSSL_BIO_free(content);
116+
wolfSSL_PKCS7_free((PKCS7*)pkcs7Compat);
117+
wolfSSL_X509_free(x509);
118+
wolfSSL_X509_STORE_free(store);
119+
return ret;
120+
}
121+
122+
123+
/* read private smime and signer certificate in DER format */
124+
static int ReadSmimeAndCert(char* smimeFile, char* certFile, byte* smime,
125+
int* smimeSz, byte* cert, int* certSz)
126+
{
127+
int ret;
128+
XFILE f;
129+
130+
f = XFOPEN(smimeFile, "rb");
131+
if (f == NULL) {
132+
printf("Error opening file %s\n", smimeFile);
133+
return -1;
134+
}
135+
else {
136+
ret = XFREAD(smime, 1, *smimeSz, f);
137+
if (ret >= 0) {
138+
*smimeSz = ret;
139+
ret = 0;
140+
XFCLOSE(f);
141+
}
142+
}
143+
144+
f = XFOPEN(certFile, "rb");
145+
if (f == NULL) {
146+
printf("Error opening file %s\n", certFile);
147+
return -1;
148+
}
149+
else {
150+
ret = XFREAD(cert, 1, *certSz, f);
151+
if (ret >= 0) {
152+
*certSz = ret;
153+
ret = 0;
154+
XFCLOSE(f);
155+
}
156+
}
157+
158+
return ret;
159+
}
160+
161+
int main(int argc, char** argv)
162+
{
163+
byte cert[2048];
164+
int certSz = 2048;
165+
166+
byte smime[3072];
167+
int smimeSz = 3072;
168+
169+
int ret;
170+
171+
if (argc != 3) {
172+
printf("Use ./smime-verify <smime file> <der cert file>\n");
173+
return -1;
174+
}
175+
176+
ret = ReadSmimeAndCert(argv[1], argv[2], smime, &smimeSz, cert, &certSz);
177+
if (ret == 0) {
178+
ret = Verify(smime, smimeSz, cert, certSz, 0);
179+
if (ret == 0) {
180+
printf("Verify Success\n");
181+
}
182+
else {
183+
printf("Verify Failed\n");
184+
}
185+
}
186+
187+
return ret;
188+
}
189+
#else
190+
int main()
191+
{
192+
printf("wolfSSL was compiled with out HAVE_SMIME support\n");
193+
return 0;
194+
}
195+
#endif

pkcs7/smime.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,17 @@ int main(int argc, char** argv)
178178
ret = Create(smime, &smimeSz, key, keySz, cert, certSz,
179179
content, contentSz, 0);
180180
if (ret == 0) {
181+
FILE* f;
181182
printf("Generated SMIME : ");
182183
for (i = 0; i < smimeSz; i++)
183184
printf("%02X", smime[i]);
184185
printf("\n");
186+
printf("output to file ./smime-created.p7s\n");
187+
f = fopen("./smime-created.p7s", "wb");
188+
if (f != NULL) {
189+
fwrite(smime, 1, smimeSz, f);
190+
fclose(f);
191+
}
185192
}
186193

187194
return ret;

0 commit comments

Comments
 (0)