7248b042fe598535ae5caafe536aa963c05bfc05
[strongswan.git] / src / pluto / pkcs7.c
1 /* Support of PKCS#7 data structures
2 * Copyright (C) 2005 Jan Hutter, Martin Willi
3 * Copyright (C) 2002-2009 Andreas Steffen
4 *
5 * HSR Hochschule fuer Technik Rapperswil, Switzerland
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <freeswan.h>
22
23 #include <library.h>
24 #include <debug.h>
25 #include <asn1/asn1.h>
26 #include <asn1/asn1_parser.h>
27 #include <asn1/oid.h>
28 #include <crypto/rngs/rng.h>
29 #include <crypto/crypters/crypter.h>
30
31 #include "constants.h"
32 #include "defs.h"
33 #include "x509.h"
34 #include "certs.h"
35 #include "pkcs7.h"
36
37 const contentInfo_t empty_contentInfo = {
38 OID_UNKNOWN , /* type */
39 { NULL, 0 } /* content */
40 };
41
42 /**
43 * ASN.1 definition of the PKCS#7 ContentInfo type
44 */
45 static const asn1Object_t contentInfoObjects[] = {
46 { 0, "contentInfo", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
47 { 1, "contentType", ASN1_OID, ASN1_BODY }, /* 1 */
48 { 1, "content", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 2 */
49 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 3 */
50 { 0, "exit", ASN1_EOC, ASN1_EXIT }
51 };
52 #define PKCS7_INFO_TYPE 1
53 #define PKCS7_INFO_CONTENT 2
54
55 /**
56 * ASN.1 definition of the PKCS#7 signedData type
57 */
58 static const asn1Object_t signedDataObjects[] = {
59 { 0, "signedData", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
60 { 1, "version", ASN1_INTEGER, ASN1_BODY }, /* 1 */
61 { 1, "digestAlgorithms", ASN1_SET, ASN1_LOOP }, /* 2 */
62 { 2, "algorithm", ASN1_EOC, ASN1_RAW }, /* 3 */
63 { 1, "end loop", ASN1_EOC, ASN1_END }, /* 4 */
64 { 1, "contentInfo", ASN1_EOC, ASN1_RAW }, /* 5 */
65 { 1, "certificates", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_LOOP }, /* 6 */
66 { 2, "certificate", ASN1_SEQUENCE, ASN1_OBJ }, /* 7 */
67 { 1, "end opt or loop", ASN1_EOC, ASN1_END }, /* 8 */
68 { 1, "crls", ASN1_CONTEXT_C_1, ASN1_OPT|ASN1_LOOP }, /* 9 */
69 { 2, "crl", ASN1_SEQUENCE, ASN1_OBJ }, /* 10 */
70 { 1, "end opt or loop", ASN1_EOC, ASN1_END }, /* 11 */
71 { 1, "signerInfos", ASN1_SET, ASN1_LOOP }, /* 12 */
72 { 2, "signerInfo", ASN1_SEQUENCE, ASN1_NONE }, /* 13 */
73 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 14 */
74 { 3, "issuerAndSerialNumber", ASN1_SEQUENCE, ASN1_BODY }, /* 15 */
75 { 4, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 16 */
76 { 4, "serial", ASN1_INTEGER, ASN1_BODY }, /* 17 */
77 { 3, "digestAlgorithm", ASN1_EOC, ASN1_RAW }, /* 18 */
78 { 3, "authenticatedAttributes", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_OBJ }, /* 19 */
79 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 20 */
80 { 3, "digestEncryptionAlgorithm", ASN1_EOC, ASN1_RAW }, /* 21 */
81 { 3, "encryptedDigest", ASN1_OCTET_STRING, ASN1_BODY }, /* 22 */
82 { 3, "unauthenticatedAttributes", ASN1_CONTEXT_C_1, ASN1_OPT }, /* 23 */
83 { 3, "end opt", ASN1_EOC, ASN1_END }, /* 24 */
84 { 1, "end loop", ASN1_EOC, ASN1_END }, /* 25 */
85 { 0, "exit", ASN1_EOC, ASN1_EXIT }
86 };
87 #define PKCS7_DIGEST_ALG 3
88 #define PKCS7_SIGNED_CONTENT_INFO 5
89 #define PKCS7_SIGNED_CERT 7
90 #define PKCS7_SIGNER_INFO 13
91 #define PKCS7_SIGNED_ISSUER 16
92 #define PKCS7_SIGNED_SERIAL_NUMBER 17
93 #define PKCS7_DIGEST_ALGORITHM 18
94 #define PKCS7_AUTH_ATTRIBUTES 19
95 #define PKCS7_DIGEST_ENC_ALGORITHM 21
96 #define PKCS7_ENCRYPTED_DIGEST 22
97
98 /**
99 * ASN.1 definition of the PKCS#7 envelopedData type
100 */
101 static const asn1Object_t envelopedDataObjects[] = {
102 { 0, "envelopedData", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
103 { 1, "version", ASN1_INTEGER, ASN1_BODY }, /* 1 */
104 { 1, "recipientInfos", ASN1_SET, ASN1_LOOP }, /* 2 */
105 { 2, "recipientInfo", ASN1_SEQUENCE, ASN1_BODY }, /* 3 */
106 { 3, "version", ASN1_INTEGER, ASN1_BODY }, /* 4 */
107 { 3, "issuerAndSerialNumber", ASN1_SEQUENCE, ASN1_BODY }, /* 5 */
108 { 4, "issuer", ASN1_SEQUENCE, ASN1_OBJ }, /* 6 */
109 { 4, "serial", ASN1_INTEGER, ASN1_BODY }, /* 7 */
110 { 3, "encryptionAlgorithm", ASN1_EOC, ASN1_RAW }, /* 8 */
111 { 3, "encryptedKey", ASN1_OCTET_STRING, ASN1_BODY }, /* 9 */
112 { 1, "end loop", ASN1_EOC, ASN1_END }, /* 10 */
113 { 1, "encryptedContentInfo", ASN1_SEQUENCE, ASN1_OBJ }, /* 11 */
114 { 2, "contentType", ASN1_OID, ASN1_BODY }, /* 12 */
115 { 2, "contentEncryptionAlgorithm", ASN1_EOC, ASN1_RAW }, /* 13 */
116 { 2, "encryptedContent", ASN1_CONTEXT_S_0, ASN1_BODY }, /* 14 */
117 { 0, "exit", ASN1_EOC, ASN1_EXIT }
118 };
119 #define PKCS7_ENVELOPED_VERSION 1
120 #define PKCS7_RECIPIENT_INFO_VERSION 4
121 #define PKCS7_ISSUER 6
122 #define PKCS7_SERIAL_NUMBER 7
123 #define PKCS7_ENCRYPTION_ALG 8
124 #define PKCS7_ENCRYPTED_KEY 9
125 #define PKCS7_CONTENT_TYPE 12
126 #define PKCS7_CONTENT_ENC_ALGORITHM 13
127 #define PKCS7_ENCRYPTED_CONTENT 14
128 #define PKCS7_ENVELOPED_ROOF 15
129
130 /**
131 * PKCS7 contentInfo OIDs
132 */
133
134 static u_char ASN1_pkcs7_data_oid_str[] = {
135 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
136 };
137
138 static u_char ASN1_pkcs7_signed_data_oid_str[] = {
139 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02
140 };
141
142 static u_char ASN1_pkcs7_enveloped_data_oid_str[] = {
143 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x03
144 };
145
146 static u_char ASN1_pkcs7_signed_enveloped_data_oid_str[] = {
147 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x04
148 };
149
150 static u_char ASN1_pkcs7_digested_data_oid_str[] = {
151 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x05
152 };
153
154 static char ASN1_pkcs7_encrypted_data_oid_str[] = {
155 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x06
156 };
157
158 static const chunk_t ASN1_pkcs7_data_oid =
159 chunk_from_buf(ASN1_pkcs7_data_oid_str);
160 static const chunk_t ASN1_pkcs7_signed_data_oid =
161 chunk_from_buf(ASN1_pkcs7_signed_data_oid_str);
162 static const chunk_t ASN1_pkcs7_enveloped_data_oid =
163 chunk_from_buf(ASN1_pkcs7_enveloped_data_oid_str);
164 static const chunk_t ASN1_pkcs7_signed_enveloped_data_oid =
165 chunk_from_buf(ASN1_pkcs7_signed_enveloped_data_oid_str);
166 static const chunk_t ASN1_pkcs7_digested_data_oid =
167 chunk_from_buf(ASN1_pkcs7_digested_data_oid_str);
168 static const chunk_t ASN1_pkcs7_encrypted_data_oid =
169 chunk_from_buf(ASN1_pkcs7_encrypted_data_oid_str);
170
171 /**
172 * 3DES and DES encryption OIDs
173 */
174
175 static u_char ASN1_3des_ede_cbc_oid_str[] = {
176 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07
177 };
178
179 static u_char ASN1_des_cbc_oid_str[] = {
180 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x07
181 };
182
183 static const chunk_t ASN1_3des_ede_cbc_oid =
184 chunk_from_buf(ASN1_3des_ede_cbc_oid_str);
185 static const chunk_t ASN1_des_cbc_oid =
186 chunk_from_buf(ASN1_des_cbc_oid_str);
187
188 /**
189 * PKCS#7 attribute type OIDs
190 */
191
192 static u_char ASN1_contentType_oid_str[] = {
193 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x03
194 };
195
196 static u_char ASN1_messageDigest_oid_str[] = {
197 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x04
198 };
199
200 static const chunk_t ASN1_contentType_oid =
201 chunk_from_buf(ASN1_contentType_oid_str);
202 static const chunk_t ASN1_messageDigest_oid =
203 chunk_from_buf(ASN1_messageDigest_oid_str);
204
205 /**
206 * Parse PKCS#7 ContentInfo object
207 */
208 bool pkcs7_parse_contentInfo(chunk_t blob, u_int level0, contentInfo_t *cInfo)
209 {
210 asn1_parser_t *parser;
211 chunk_t object;
212 int objectID;
213 bool success = FALSE;
214
215 parser = asn1_parser_create(contentInfoObjects, blob);
216 parser->set_top_level(parser, level0);
217
218 while (parser->iterate(parser, &objectID, &object))
219 {
220 if (objectID == PKCS7_INFO_TYPE)
221 {
222 cInfo->type = asn1_known_oid(object);
223 if (cInfo->type < OID_PKCS7_DATA
224 || cInfo->type > OID_PKCS7_ENCRYPTED_DATA)
225 {
226 DBG1("unknown pkcs7 content type");
227 goto end;
228 }
229 }
230 else if (objectID == PKCS7_INFO_CONTENT)
231 {
232 cInfo->content = object;
233 }
234 }
235 success = parser->success(parser);
236
237 end:
238 parser->destroy(parser);
239 return success;
240 }
241
242 /**
243 * Parse a PKCS#7 signedData object
244 */
245 bool pkcs7_parse_signedData(chunk_t blob, contentInfo_t *data, x509cert_t **cert,
246 chunk_t *attributes, const x509cert_t *cacert)
247 {
248 u_char buf[BUF_LEN];
249 asn1_parser_t *parser;
250 chunk_t object;
251 int digest_alg = OID_UNKNOWN;
252 int enc_alg = OID_UNKNOWN;
253 int signerInfos = 0;
254 int objectID;
255 bool success = FALSE;
256
257 contentInfo_t cInfo = empty_contentInfo;
258 chunk_t encrypted_digest = chunk_empty;
259
260 if (!pkcs7_parse_contentInfo(blob, 0, &cInfo))
261 {
262 return FALSE;
263 }
264 if (cInfo.type != OID_PKCS7_SIGNED_DATA)
265 {
266 DBG1("pkcs7 content type is not signedData");
267 return FALSE;
268 }
269
270 parser = asn1_parser_create(signedDataObjects, blob);
271 parser->set_top_level(parser, 2);
272
273 while (parser->iterate(parser, &objectID, &object))
274 {
275 u_int level = parser->get_level(parser);
276
277 switch (objectID)
278 {
279 case PKCS7_DIGEST_ALG:
280 digest_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
281 break;
282 case PKCS7_SIGNED_CONTENT_INFO:
283 if (data != NULL)
284 {
285 pkcs7_parse_contentInfo(object, level, data);
286 }
287 break;
288 case PKCS7_SIGNED_CERT:
289 if (cert != NULL)
290 {
291 chunk_t cert_blob = chunk_clone(object);
292 x509cert_t *newcert = malloc_thing(x509cert_t);
293
294 *newcert = empty_x509cert;
295
296 DBG2(" parsing pkcs7-wrapped certificate");
297 if (parse_x509cert(cert_blob, level+1, newcert))
298 {
299 newcert->next = *cert;
300 *cert = newcert;
301 }
302 else
303 {
304 free_x509cert(newcert);
305 }
306 }
307 break;
308 case PKCS7_SIGNER_INFO:
309 signerInfos++;
310 DBG2(" signer #%d", signerInfos);
311 break;
312 case PKCS7_SIGNED_ISSUER:
313 dntoa(buf, BUF_LEN, object);
314 DBG2(" '%s'",buf);
315 break;
316 case PKCS7_AUTH_ATTRIBUTES:
317 if (attributes != NULL)
318 {
319 *attributes = object;
320 *attributes->ptr = ASN1_SET;
321 }
322 break;
323 case PKCS7_DIGEST_ALGORITHM:
324 digest_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
325 break;
326 case PKCS7_DIGEST_ENC_ALGORITHM:
327 enc_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
328 break;
329 case PKCS7_ENCRYPTED_DIGEST:
330 encrypted_digest = object;
331 }
332 }
333 success = parser->success(parser);
334 parser->destroy(parser);
335 if (!success)
336 {
337 return FALSE;
338 }
339
340 /* check the signature only if a cacert is available */
341 if (cacert != NULL)
342 {
343 public_key_t *key = cacert->public_key;
344 signature_scheme_t scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
345
346 if (signerInfos == 0)
347 {
348 DBG1("no signerInfo object found");
349 return FALSE;
350 }
351 else if (signerInfos > 1)
352 {
353 DBG1("more than one signerInfo object found");
354 return FALSE;
355 }
356 if (attributes->ptr == NULL)
357 {
358 DBG1("no authenticatedAttributes object found");
359 return FALSE;
360 }
361 if (enc_alg != OID_RSA_ENCRYPTION)
362 {
363 DBG1("only RSA digest encryption supported");
364 return FALSE;
365 }
366
367 /* determine signature scheme */
368 scheme = signature_scheme_from_oid(digest_alg);
369
370 if (scheme == SIGN_UNKNOWN)
371 {
372 return FALSE;
373 }
374 if (key->verify(key, scheme, *attributes, encrypted_digest))
375 {
376 DBG2("signature is valid");
377 }
378 else
379 {
380 DBG1("invalid signature");
381 return FALSE;
382 }
383 }
384 return TRUE;
385 }
386
387 /**
388 * Parse a PKCS#7 envelopedData object
389 */
390 bool pkcs7_parse_envelopedData(chunk_t blob, chunk_t *data,
391 chunk_t serialNumber,
392 private_key_t *key)
393 {
394 asn1_parser_t *parser;
395 chunk_t object;
396 chunk_t iv = chunk_empty;
397 chunk_t symmetric_key = chunk_empty;
398 chunk_t encrypted_content = chunk_empty;
399
400 crypter_t *crypter = NULL;
401
402 u_char buf[BUF_LEN];
403 int enc_alg = OID_UNKNOWN;
404 int content_enc_alg = OID_UNKNOWN;
405 int objectID;
406 bool success = FALSE;
407
408 contentInfo_t cInfo = empty_contentInfo;
409 *data = chunk_empty;
410
411 if (!pkcs7_parse_contentInfo(blob, 0, &cInfo))
412 {
413 goto failed;
414 }
415 if (cInfo.type != OID_PKCS7_ENVELOPED_DATA)
416 {
417 DBG1("pkcs7 content type is not envelopedData");
418 goto failed;
419 }
420
421 parser = asn1_parser_create(envelopedDataObjects, cInfo.content);
422 parser->set_top_level(parser, 2);
423
424 while (parser->iterate(parser, &objectID, &object))
425 {
426 u_int level = parser->get_level(parser);
427
428 switch (objectID)
429 {
430 case PKCS7_ENVELOPED_VERSION:
431 if (*object.ptr != 0)
432 {
433 DBG1("envelopedData version is not 0");
434 goto end;
435 }
436 break;
437 case PKCS7_RECIPIENT_INFO_VERSION:
438 if (*object.ptr != 0)
439 {
440 DBG1("recipient info version is not 0");
441 goto end;
442 }
443 break;
444 case PKCS7_ISSUER:
445 dntoa(buf, BUF_LEN, object);
446 DBG2(" '%s'", buf);
447 break;
448 case PKCS7_SERIAL_NUMBER:
449 if (!chunk_equals(serialNumber, object))
450 {
451 DBG1("serial numbers do not match");
452 goto end;
453 }
454 break;
455 case PKCS7_ENCRYPTION_ALG:
456 enc_alg = asn1_parse_algorithmIdentifier(object, level, NULL);
457 if (enc_alg != OID_RSA_ENCRYPTION)
458 {
459 DBG1("only rsa encryption supported");
460 goto end;
461 }
462 break;
463 case PKCS7_ENCRYPTED_KEY:
464 if (!key->decrypt(key, object, &symmetric_key))
465 {
466 DBG1("symmetric key could not be decrypted with rsa");
467 goto end;
468 }
469 DBG4("symmetric key %B", &symmetric_key);
470 break;
471 case PKCS7_CONTENT_TYPE:
472 if (asn1_known_oid(object) != OID_PKCS7_DATA)
473 {
474 DBG1("encrypted content not of type pkcs7 data");
475 goto end;
476 }
477 break;
478 case PKCS7_CONTENT_ENC_ALGORITHM:
479 content_enc_alg = asn1_parse_algorithmIdentifier(object, level, &iv);
480
481 if (content_enc_alg == OID_UNKNOWN)
482 {
483 DBG1("unknown content encryption algorithm");
484 goto end;
485 }
486 if (!asn1_parse_simple_object(&iv, ASN1_OCTET_STRING, level+1, "IV"))
487 {
488 DBG1("IV could not be parsed");
489 goto end;
490 }
491 break;
492 case PKCS7_ENCRYPTED_CONTENT:
493 encrypted_content = object;
494 break;
495 }
496 }
497 success = parser->success(parser);
498
499 end:
500 parser->destroy(parser);
501 if (!success)
502 {
503 goto failed;
504 }
505 success = FALSE;
506
507 /* decrypt the content */
508 {
509 encryption_algorithm_t alg;
510 size_t key_size;
511 crypter_t *crypter;
512
513 alg = encryption_algorithm_from_oid(content_enc_alg, &key_size);
514 if (alg == ENCR_UNDEFINED)
515 {
516 DBG1("unsupported content encryption algorithm");
517 goto failed;
518 }
519 crypter = lib->crypto->create_crypter(lib->crypto, alg, key_size);
520 if (crypter == NULL)
521 {
522 DBG1("crypter %N not available", encryption_algorithm_names, alg);
523 goto failed;
524 }
525 if (symmetric_key.len != crypter->get_key_size(crypter))
526 {
527 DBG1("symmetric key length %d is wrong", symmetric_key.len);
528 goto failed;
529 }
530 if (iv.len != crypter->get_block_size(crypter))
531 {
532 DBG1("IV length %d is wrong", iv.len);
533 goto failed;
534 }
535 crypter->set_key(crypter, symmetric_key);
536 crypter->decrypt(crypter, encrypted_content, iv, data);
537 DBG4("decrypted content with padding: %B", data);
538 }
539
540 /* remove the padding */
541 {
542 u_char *pos = data->ptr + data->len - 1;
543 u_char pattern = *pos;
544 size_t padding = pattern;
545
546 if (padding > data->len)
547 {
548 DBG1("padding greater than data length");
549 goto failed;
550 }
551 data->len -= padding;
552
553 while (padding-- > 0)
554 {
555 if (*pos-- != pattern)
556 {
557 DBG1("wrong padding pattern");
558 goto failed;
559 }
560 }
561 }
562 success = TRUE;
563
564 failed:
565 DESTROY_IF(crypter);
566 chunk_clear(&symmetric_key);
567 if (!success)
568 {
569 free(data->ptr);
570 }
571 return success;
572 }
573
574 /**
575 * @brief Builds a contentType attribute
576 *
577 * @return ASN.1 encoded contentType attribute
578 */
579 chunk_t pkcs7_contentType_attribute(void)
580 {
581 return asn1_wrap(ASN1_SEQUENCE, "cm"
582 , ASN1_contentType_oid
583 , asn1_simple_object(ASN1_SET, ASN1_pkcs7_data_oid));
584 }
585
586 /**
587 * @brief Builds a messageDigest attribute
588 *
589 *
590 * @param[in] blob content to create digest of
591 * @param[in] digest_alg digest algorithm to be used
592 * @return ASN.1 encoded messageDigest attribute
593 *
594 */
595 chunk_t pkcs7_messageDigest_attribute(chunk_t content, int digest_alg)
596 {
597 chunk_t digest;
598 hash_algorithm_t hash_alg;
599 hasher_t *hasher;
600
601 hash_alg = hasher_algorithm_from_oid(digest_alg);
602 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
603 hasher->allocate_hash(hasher, content, &digest);
604 hasher->destroy(hasher);
605
606 return asn1_wrap(ASN1_SEQUENCE, "cm",
607 ASN1_messageDigest_oid,
608 asn1_wrap(ASN1_SET, "m",
609 asn1_wrap(ASN1_OCTET_STRING, "m", digest)
610 )
611 );
612 }
613
614 /**
615 * build a DER-encoded contentInfo object
616 */
617 static chunk_t pkcs7_build_contentInfo(contentInfo_t *cInfo)
618 {
619 chunk_t content_type;
620
621 /* select DER-encoded OID for pkcs7 contentInfo type */
622 switch(cInfo->type)
623 {
624 case OID_PKCS7_DATA:
625 content_type = ASN1_pkcs7_data_oid;
626 break;
627 case OID_PKCS7_SIGNED_DATA:
628 content_type = ASN1_pkcs7_signed_data_oid;
629 break;
630 case OID_PKCS7_ENVELOPED_DATA:
631 content_type = ASN1_pkcs7_enveloped_data_oid;
632 break;
633 case OID_PKCS7_SIGNED_ENVELOPED_DATA:
634 content_type = ASN1_pkcs7_signed_enveloped_data_oid;
635 break;
636 case OID_PKCS7_DIGESTED_DATA:
637 content_type = ASN1_pkcs7_digested_data_oid;
638 break;
639 case OID_PKCS7_ENCRYPTED_DATA:
640 content_type = ASN1_pkcs7_encrypted_data_oid;
641 break;
642 case OID_UNKNOWN:
643 default:
644 DBG1("invalid pkcs7 contentInfo type");
645 return chunk_empty;
646 }
647
648 return (cInfo->content.ptr == NULL)
649 ? asn1_simple_object(ASN1_SEQUENCE, content_type)
650 : asn1_wrap(ASN1_SEQUENCE, "cm"
651 , content_type
652 , asn1_simple_object(ASN1_CONTEXT_C_0, cInfo->content)
653 );
654 }
655
656 /**
657 * build issuerAndSerialNumber object
658 */
659 chunk_t pkcs7_build_issuerAndSerialNumber(const x509cert_t *cert)
660 {
661 return asn1_wrap(ASN1_SEQUENCE, "cm"
662 , cert->issuer
663 , asn1_integer("c", cert->serialNumber));
664 }
665
666 /**
667 * create a signed pkcs7 contentInfo object
668 */
669 chunk_t pkcs7_build_signedData(chunk_t data, chunk_t attributes,
670 const x509cert_t *cert, int digest_alg,
671 private_key_t *key)
672 {
673 contentInfo_t pkcs7Data, signedData;
674 chunk_t authenticatedAttributes, encryptedDigest, signerInfo, cInfo;
675
676 chunk_t digestAlgorithm = asn1_algorithmIdentifier(digest_alg);
677
678 if (attributes.ptr != NULL)
679 {
680 encryptedDigest = x509_build_signature(attributes, digest_alg, key,
681 FALSE);
682 authenticatedAttributes = chunk_clone(attributes);
683 *authenticatedAttributes.ptr = ASN1_CONTEXT_C_0;
684 }
685 else
686 {
687 encryptedDigest = (data.ptr == NULL)? chunk_empty
688 : x509_build_signature(data, digest_alg, key, FALSE);
689 authenticatedAttributes = chunk_empty;
690 }
691
692 signerInfo = asn1_wrap(ASN1_SEQUENCE, "cmcmcm"
693 , ASN1_INTEGER_1
694 , pkcs7_build_issuerAndSerialNumber(cert)
695 , digestAlgorithm
696 , authenticatedAttributes
697 , asn1_algorithmIdentifier(OID_RSA_ENCRYPTION)
698 , encryptedDigest);
699
700 pkcs7Data.type = OID_PKCS7_DATA;
701 pkcs7Data.content = (data.ptr == NULL)? chunk_empty
702 : asn1_simple_object(ASN1_OCTET_STRING, data);
703
704 signedData.type = OID_PKCS7_SIGNED_DATA;
705 signedData.content = asn1_wrap(ASN1_SEQUENCE, "cmmmm"
706 , ASN1_INTEGER_1
707 , asn1_simple_object(ASN1_SET, digestAlgorithm)
708 , pkcs7_build_contentInfo(&pkcs7Data)
709 , asn1_simple_object(ASN1_CONTEXT_C_0, cert->certificate)
710 , asn1_wrap(ASN1_SET, "m", signerInfo));
711
712 cInfo = pkcs7_build_contentInfo(&signedData);
713 DBG3("signedData %B", &cInfo);
714
715 free(pkcs7Data.content.ptr);
716 free(signedData.content.ptr);
717 return cInfo;
718 }
719
720 /**
721 * create a symmetrically encrypted pkcs7 contentInfo object
722 */
723 chunk_t pkcs7_build_envelopedData(chunk_t data, const x509cert_t *cert, int enc_alg)
724 {
725 encryption_algorithm_t alg;
726 size_t alg_key_size;
727 chunk_t symmetricKey, protectedKey, iv, in, out;
728 crypter_t *crypter;
729
730 alg = encryption_algorithm_from_oid(enc_alg, &alg_key_size);
731 crypter = lib->crypto->create_crypter(lib->crypto, alg,
732 alg_key_size/BITS_PER_BYTE);
733 if (crypter == NULL)
734 {
735 DBG1("crypter for %N not available", encryption_algorithm_names, alg);
736 return chunk_empty;
737 }
738
739 /* generate a true random symmetric encryption key and a pseudo-random iv */
740 {
741 rng_t *rng;
742
743 rng = lib->crypto->create_rng(lib->crypto, RNG_TRUE);
744 rng->allocate_bytes(rng, crypter->get_key_size(crypter), &symmetricKey);
745 DBG4("symmetric encryption key %B", &symmetricKey);
746 rng->destroy(rng);
747
748 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
749 rng->allocate_bytes(rng, crypter->get_block_size(crypter), &iv);
750 DBG4("initialization vector: %B", &iv);
751 rng->destroy(rng);
752 }
753
754 /* pad the data to a multiple of the block size */
755 {
756 size_t block_size = crypter->get_block_size(crypter);
757 size_t padding = block_size - data.len % block_size;
758
759 in.len = data.len + padding;
760 in.ptr = malloc(in.len);
761
762 DBG2("padding %u bytes of data to multiple block size of %u bytes",
763 data.len, in.len);
764
765 /* copy data */
766 memcpy(in.ptr, data.ptr, data.len);
767 /* append padding */
768 memset(in.ptr + data.len, padding, padding);
769 }
770 DBG3("padded unencrypted data %B", &in);
771
772 /* symmetric encryption of data object */
773 crypter->set_key(crypter, symmetricKey);
774 crypter->encrypt(crypter, in, iv, &out);
775 crypter->destroy(crypter);
776 DBG3("encrypted data %B", &out);
777
778 cert->public_key->encrypt(cert->public_key, symmetricKey, &protectedKey);
779
780 /* build pkcs7 enveloped data object */
781 {
782
783 chunk_t contentEncryptionAlgorithm = asn1_wrap(ASN1_SEQUENCE, "mm"
784 , asn1_build_known_oid(enc_alg)
785 , asn1_simple_object(ASN1_OCTET_STRING, iv));
786
787 chunk_t encryptedContentInfo = asn1_wrap(ASN1_SEQUENCE, "cmm"
788 , ASN1_pkcs7_data_oid
789 , contentEncryptionAlgorithm
790 , asn1_wrap(ASN1_CONTEXT_S_0, "m", out));
791
792 chunk_t encryptedKey = asn1_wrap(ASN1_OCTET_STRING, "m"
793 , protectedKey);
794
795 chunk_t recipientInfo = asn1_wrap(ASN1_SEQUENCE, "cmcm"
796 , ASN1_INTEGER_0
797 , pkcs7_build_issuerAndSerialNumber(cert)
798 , asn1_algorithmIdentifier(OID_RSA_ENCRYPTION)
799 , encryptedKey);
800
801 chunk_t cInfo;
802 contentInfo_t envelopedData;
803
804 envelopedData.type = OID_PKCS7_ENVELOPED_DATA;
805 envelopedData.content = asn1_wrap(ASN1_SEQUENCE, "cmm"
806 , ASN1_INTEGER_0
807 , asn1_wrap(ASN1_SET, "m", recipientInfo)
808 , encryptedContentInfo);
809
810 cInfo = pkcs7_build_contentInfo(&envelopedData);
811 DBG3("envelopedData %B", &cInfo);
812
813 free(envelopedData.content.ptr);
814 free(symmetricKey.ptr);
815 free(in.ptr);
816 free(iv.ptr);
817 return cInfo;
818 }
819 }