forked from wolfSSL/wolfssl-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthEnvelopedData-ori.c
301 lines (242 loc) · 8.2 KB
/
authEnvelopedData-ori.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* authEnvelopedData-ori.c
*
* Copyright (C) 2006-2020 wolfSSL Inc.
*
* This file is part of wolfSSL. (formerly known as CyaSSL)
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/pkcs7.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/logging.h>
#define certFile "../certs/client-ecc-cert.der"
#define keyFile "../certs/ecc-client-key.der"
#define encodedFileORI "authEnvelopedDataORI.der"
static const byte data[] = { /* Hello World */
0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
0x72,0x6c,0x64
};
static const byte asnDataOid[] = {
0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
};
static int load_certs(byte* cert, word32* certSz, byte* key, word32* keySz)
{
FILE* file;
/* certificate file */
file = fopen(certFile, "rb");
if (!file) {
printf("ERROR: failed to open file: %s\n", certFile);
return -1;
}
*certSz = (word32)fread(cert, 1, *certSz, file);
fclose(file);
/* key file */
file = fopen(keyFile, "rb");
if (!file) {
printf("ERROR: failed to open file: %s\n", keyFile);
return -1;
}
*keySz = (word32)fread(key, 1, *keySz, file);
fclose(file);
return 0;
}
static int write_file_buffer(const char* fileName, byte* in, word32 inSz)
{
int ret;
FILE* file;
file = fopen(fileName, "wb");
if (file == NULL) {
printf("ERROR: opening file for writing: %s\n", fileName);
return -1;
}
ret = (int)fwrite(in, 1, inSz, file);
if (ret == 0) {
printf("ERROR: writing buffer to output file\n");
return -1;
}
fclose(file);
return 0;
}
/* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
* and giving wolfCrypt the value for oriOID and oriValue to place in
* OtherRecipientInfo.
*
* Returns 0 on success, negative upon error. */
static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
void* ctx)
{
int i;
/* make sure buffers are large enough */
if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
return -1;
/* our simple encryption algorithm will be take the bitwise complement */
oriValue[0] = 0x04; /*ASN OCTET STRING */
oriValue[1] = (byte)cekSz; /* length */
for (i = 0; i < (int)cekSz; i++) {
oriValue[2 + i] = ~cek[i];
}
*oriValueSz = 2 + cekSz;
/* set oriType to ASN.1 encoded data OID */
XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
*oriTypeSz = sizeof(asnDataOid);
(void)pkcs7;
(void)ctx;
return 0;
}
/* ORI decrypt callback, responsible for providing a decrypted content
* encryption key (CEK) placed into decryptedKey and size placed into
* decryptedKeySz. oriOID and oriValue are given to the callback to help
* in decrypting the encrypted CEK.
*
* Returns 0 on success, negative upon error. */
static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
byte* oriValue, word32 oriValueSz, byte* decryptedKey,
word32* decryptedKeySz, void* ctx)
{
int i;
/* make sure oriType matches what we expect */
if (oriTypeSz != sizeof(asnDataOid))
return -1;
if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
return -1;
/* make sure decrypted buffer is large enough */
if (*decryptedKeySz < oriValueSz)
return -1;
/* decrypt encrypted CEK using simple bitwise complement,
only for example */
for (i = 0; i < (int)oriValueSz - 2; i++) {
decryptedKey[i] = ~oriValue[2 + i];
}
*decryptedKeySz = oriValueSz - 2;
(void)pkcs7;
(void)ctx;
return 0;
}
static int authEnvelopedData_encrypt(byte* cert, word32 certSz, byte* key,
word32 keySz, byte* out, word32 outSz)
{
int ret;
PKCS7* pkcs7;
pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
if (pkcs7 == NULL)
return -1;
pkcs7->content = (byte*)data;
pkcs7->contentSz = sizeof(data);
pkcs7->contentOID = DATA;
pkcs7->encryptOID = AES256GCMb;
/* add recipient using otherRecipientInfo (ORI) with custom encrypt
* callback to handle encryption. ORI is loosely defined, allowing
* advanced users or future protocols to extend the CMS RecipientInfo
* model. */
ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb, 0);
if (ret < 0) {
printf("wc_PKCS7_AddRecipient_ORI() failed, ret = %d\n", ret);
wc_PKCS7_Free(pkcs7);
return -1;
}
/* encode authEnvelopedData, returns size */
ret = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, out, outSz);
if (ret <= 0) {
printf("wc_PKCS7_EncodeAuthEnvelopedData() failed, ret = %d\n", ret);
wc_PKCS7_Free(pkcs7);
return -1;
} else {
printf("Successfully encoded AuthEnvelopedData bundle (%s)\n",
encodedFileORI);
if (write_file_buffer(encodedFileORI, out, ret) != 0) {
printf("ERROR: error writing encoded to output file\n");
return -1;
}
}
wc_PKCS7_Free(pkcs7);
return ret;
}
static int authEnvelopedData_decrypt(byte* in, word32 inSz, byte* cert,
word32 certSz, byte* key, word32 keySz,
byte* out, word32 outSz)
{
int ret;
PKCS7* pkcs7;
pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
if (pkcs7 == NULL)
return -1;
/* set decrypt callback for decryption */
ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
if (ret != 0) {
printf("ERROR: wc_PKCS7_SetOriDecryptCb(), ret = %d\n", ret);
wc_PKCS7_Free(pkcs7);
return -1;
}
/* decode authEnvelopedData, returns size */
ret = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, in, inSz, out, outSz);
if (ret <= 0 || (ret != sizeof(data)) || (XMEMCMP(out, data, ret) != 0)) {
printf("ERROR: wc_PKCS7_DecodeAuthEnvelopedData(), ret = %d\n", ret);
wc_PKCS7_Free(pkcs7);
return -1;
} else {
printf("Successfully decoded AuthEnvelopedData bundle (%s)\n",
encodedFileORI);
}
wc_PKCS7_Free(pkcs7);
return ret;
}
#ifdef HAVE_PKCS7
int main(int argc, char** argv)
{
int ret;
int encryptedSz, decryptedSz;
word32 certSz, keySz;
byte cert[2048];
byte key[2048];
byte encrypted[1024];
byte decrypted[1024];
#ifdef DEBUG_WOLFSSL
wolfSSL_Debugging_ON();
#endif
certSz = sizeof(cert);
keySz = sizeof(key);
ret = load_certs(cert, &certSz, key, &keySz);
if (ret != 0)
return -1;
encryptedSz = authEnvelopedData_encrypt(cert, certSz, key, keySz,
encrypted, sizeof(encrypted));
if (encryptedSz < 0)
return -1;
#ifdef DEBUG_WOLFSSL
printf("AuthEnvelopedData DER (%d byte):\n", encryptedSz);
WOLFSSL_BUFFER(encrypted, encryptedSz);
#endif
decryptedSz = authEnvelopedData_decrypt(encrypted, encryptedSz,
cert, certSz, key, keySz,
decrypted, sizeof(decrypted));
if (decryptedSz < 0)
return -1;
#ifdef DEBUG_WOLFSSL
printf("Decrypted content (%d byte):\n", decryptedSz);
WOLFSSL_BUFFER(decrypted, decryptedSz);
#endif
(void)argc;
(void)argv;
return 0;
}
#else
int main(int argc, char** argv)
{
printf("Must build wolfSSL using ./configure --enable-pkcs7\n");
return 0;
}
#endif