2 * Copyright (C) 2005 Jan Hutter, Martin Willi
3 * Copyright (C) 2002-2008 Andreas Steffen
5 * Hochschule fuer Technik Rapperswil, Switzerland
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>.
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
25 #include <asn1/asn1.h>
26 #include <asn1/asn1_parser.h>
27 #include <credentials/certificates/x509.h>
28 #include <credentials/keys/public_key.h>
29 #include <crypto/pkcs9.h>
30 #include <crypto/hashers/hasher.h>
31 #include <crypto/crypters/crypter.h>
33 #include <utils/linked_list.h>
37 typedef struct private_pkcs7_t private_pkcs7_t
;
40 * Private data of a pkcs7_t object.
42 struct private_pkcs7_t
{
44 * Public interface for this certificate.
54 * ASN.1 encoded content
59 * Has the content already been parsed?
64 * ASN.1 parsing start level
74 * ASN.1 encoded attributes
79 * Linked list of X.509 certificates
85 * PKCS7 contentInfo OIDs
87 static u_char ASN1_pkcs7_data_oid_str
[] = {
89 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
92 static u_char ASN1_pkcs7_signed_data_oid_str
[] = {
94 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02
97 static u_char ASN1_pkcs7_enveloped_data_oid_str
[] = {
99 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x03
102 static u_char ASN1_pkcs7_signed_enveloped_data_oid_str
[] = {
104 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x04
107 static u_char ASN1_pkcs7_digested_data_oid_str
[] = {
109 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x05
112 static char ASN1_pkcs7_encrypted_data_oid_str
[] = {
114 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x06
117 static const chunk_t ASN1_pkcs7_data_oid
=
118 chunk_from_buf(ASN1_pkcs7_data_oid_str
);
119 static const chunk_t ASN1_pkcs7_signed_data_oid
=
120 chunk_from_buf(ASN1_pkcs7_signed_data_oid_str
);
121 static const chunk_t ASN1_pkcs7_enveloped_data_oid
=
122 chunk_from_buf(ASN1_pkcs7_enveloped_data_oid_str
);
123 static const chunk_t ASN1_pkcs7_signed_enveloped_data_oid
=
124 chunk_from_buf(ASN1_pkcs7_signed_enveloped_data_oid_str
);
125 static const chunk_t ASN1_pkcs7_digested_data_oid
=
126 chunk_from_buf(ASN1_pkcs7_digested_data_oid_str
);
127 static const chunk_t ASN1_pkcs7_encrypted_data_oid
=
128 chunk_from_buf(ASN1_pkcs7_encrypted_data_oid_str
);
131 * 3DES and DES encryption OIDs
133 static u_char ASN1_3des_ede_cbc_oid_str
[] = {
135 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07
138 static u_char ASN1_des_cbc_oid_str
[] = {
140 0x2B, 0x0E, 0x03, 0x02, 0x07
143 static const chunk_t ASN1_3des_ede_cbc_oid
=
144 chunk_from_buf(ASN1_3des_ede_cbc_oid_str
);
145 static const chunk_t ASN1_des_cbc_oid
=
146 chunk_from_buf(ASN1_des_cbc_oid_str
);
149 * Implements pkcs7_t.is_data.
151 static bool is_data(private_pkcs7_t
*this)
153 return this->type
== OID_PKCS7_DATA
;
157 * Implements pkcs7_t.is_signedData.
159 static bool is_signedData(private_pkcs7_t
*this)
161 return this->type
== OID_PKCS7_SIGNED_DATA
;
165 * Implements pkcs7_t.is_envelopedData.
167 static bool is_envelopedData(private_pkcs7_t
*this)
169 return this->type
== OID_PKCS7_ENVELOPED_DATA
;
173 * Check whether to abort the requested parsing
175 static bool abort_parsing(private_pkcs7_t
*this, int type
)
177 if (this->type
!= type
)
179 DBG1("pkcs7 content to be parsed is not of type '%s'",
185 DBG1("pkcs7 content has already been parsed");
193 * Implements pkcs7_t.parse_data.
195 static bool parse_data(private_pkcs7_t
*this)
197 chunk_t data
= this->content
;
199 if (abort_parsing(this, OID_PKCS7_DATA
))
205 this->data
= chunk_empty
;
208 if (asn1_parse_simple_object(&data
, ASN1_OCTET_STRING
, this->level
, "data"))
210 this->data
= chunk_clone(data
);
220 * ASN.1 definition of the PKCS#7 signedData type
222 static const asn1Object_t signedDataObjects
[] = {
223 { 0, "signedData", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
224 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
225 { 1, "digestAlgorithms", ASN1_SET
, ASN1_LOOP
}, /* 2 */
226 { 2, "algorithm", ASN1_EOC
, ASN1_RAW
}, /* 3 */
227 { 1, "end loop", ASN1_EOC
, ASN1_END
}, /* 4 */
228 { 1, "contentInfo", ASN1_EOC
, ASN1_RAW
}, /* 5 */
229 { 1, "certificates", ASN1_CONTEXT_C_0
, ASN1_OPT
|
231 { 2, "certificate", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 7 */
232 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 8 */
233 { 1, "crls", ASN1_CONTEXT_C_1
, ASN1_OPT
|
235 { 2, "crl", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 10 */
236 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 11 */
237 { 1, "signerInfos", ASN1_SET
, ASN1_LOOP
}, /* 12 */
238 { 2, "signerInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 13 */
239 { 3, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
240 { 3, "issuerAndSerialNumber", ASN1_SEQUENCE
, ASN1_BODY
}, /* 15 */
241 { 4, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 16 */
242 { 4, "serial", ASN1_INTEGER
, ASN1_BODY
}, /* 17 */
243 { 3, "digestAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 18 */
244 { 3, "authenticatedAttributes", ASN1_CONTEXT_C_0
, ASN1_OPT
|
246 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 20 */
247 { 3, "digestEncryptionAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 21 */
248 { 3, "encryptedDigest", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 22 */
249 { 3, "unauthenticatedAttributes", ASN1_CONTEXT_C_1
, ASN1_OPT
}, /* 23 */
250 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 24 */
251 { 1, "end loop", ASN1_EOC
, ASN1_END
}, /* 25 */
252 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
254 #define PKCS7_DIGEST_ALG 3
255 #define PKCS7_SIGNED_CONTENT_INFO 5
256 #define PKCS7_SIGNED_CERT 7
257 #define PKCS7_SIGNER_INFO 13
258 #define PKCS7_SIGNED_ISSUER 16
259 #define PKCS7_SIGNED_SERIAL_NUMBER 17
260 #define PKCS7_DIGEST_ALGORITHM 18
261 #define PKCS7_AUTH_ATTRIBUTES 19
262 #define PKCS7_DIGEST_ENC_ALGORITHM 21
263 #define PKCS7_ENCRYPTED_DIGEST 22
266 * Implements pkcs7_t.parse_signedData.
268 static bool parse_signedData(private_pkcs7_t
*this, x509_t
*cacert
)
270 asn1_parser_t
*parser
;
273 int digest_alg
= OID_UNKNOWN
;
274 int enc_alg
= OID_UNKNOWN
;
276 bool success
= FALSE
;
278 chunk_t encrypted_digest
= chunk_empty
;
280 if (abort_parsing(this, OID_PKCS7_SIGNED_DATA
))
285 parser
= asn1_parser_create(signedDataObjects
, this->content
);
286 parser
->set_top_level(parser
, this->level
);
288 while (parser
->iterate(parser
, &objectID
, &object
))
290 u_int level
= parser
->get_level(parser
);
294 case PKCS7_DIGEST_ALG
:
295 digest_alg
= asn1_parse_algorithmIdentifier(object
, level
, NULL
);
297 case PKCS7_SIGNED_CONTENT_INFO
:
300 pkcs7_t
*data
= pkcs7_create_from_chunk(object
, level
+1);
306 if (!data
->parse_data(data
))
311 pureData
= data
->get_data(data
);
312 this->data
= (pureData
.len
)?
chunk_clone(pureData
) : chunk_empty
;
316 case PKCS7_SIGNED_CERT
:
318 x509_t
*cert
= x509_create_from_chunk(chunk_clone(object
), level
+1);
322 this->certs
->insert_last(this->certs
, (void*)cert
);
326 case PKCS7_SIGNER_INFO
:
328 DBG2(" signer #%d", signerInfos
);
330 case PKCS7_SIGNED_ISSUER
:
332 identification_t
*issuer
;
334 issuer
= identification_create_from_encoding(ID_DER_ASN1_DN
, object
);
335 DBG2(" '%Y'", issuer
);
336 issuer
->destroy(issuer
);
339 case PKCS7_AUTH_ATTRIBUTES
:
340 *object
.ptr
= ASN1_SET
;
341 this->attributes
= pkcs9_create_from_chunk(object
, level
+1);
342 *object
.ptr
= ASN1_CONTEXT_C_0
;
344 case PKCS7_DIGEST_ALGORITHM
:
345 digest_alg
= parse_algorithmIdentifier(object
, level
, NULL
);
347 case PKCS7_DIGEST_ENC_ALGORITHM
:
348 enc_alg
= parse_algorithmIdentifier(object
, level
, NULL
);
350 case PKCS7_ENCRYPTED_DIGEST
:
351 encrypted_digest
= object
;
354 success
= parser
->success(parser
);
357 parser
->destroy(parser
);
363 /* check the signature only if a cacert is available */
366 hash_algorithm_t algorithm
= hasher_algorithm_from_oid(digest_alg
);
367 rsa_public_key_t
*signer
= cacert
->get_public_key(cacert
);
369 if (signerInfos
== 0)
371 DBG1("no signerInfo object found");
374 else if (signerInfos
> 1)
376 DBG1("more than one signerInfo object found");
379 if (this->attributes
== NULL
)
381 DBG1("no authenticatedAttributes object found");
384 if (enc_alg
!= OID_RSA_ENCRYPTION
)
386 DBG1("only RSA digest encryption supported");
389 if (signer
->verify_emsa_pkcs1_signature(signer
, algorithm
,
390 this->attributes
->get_encoding(this->attributes
), encrypted_digest
) != SUCCESS
)
392 DBG1("invalid digest signature");
397 DBG2("digest signature is valid");
399 if (this->data
.ptr
!= NULL
)
401 chunk_t messageDigest
= this->attributes
->get_messageDigest(this->attributes
);
403 if (messageDigest
.ptr
== NULL
)
405 DBG1("messageDigest attribute not found");
414 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, algorithm
)
417 DBG1("hash algorithm %N not supported",
418 hash_algorithm_names
, algorithm
);
419 free(messageDigest
.ptr
);
422 hasher
->allocate_hash(hasher
, this->data
, &hash
);
423 hasher
->destroy(hasher
);
424 DBG3("hash: %B", &hash
);
426 valid
= chunk_equals(messageDigest
, hash
);
427 free(messageDigest
.ptr
);
431 DBG2("messageDigest is valid");
435 DBG1("invalid messageDigest");
445 * ASN.1 definition of the PKCS#7 envelopedData type
447 static const asn1Object_t envelopedDataObjects
[] = {
448 { 0, "envelopedData", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
449 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
450 { 1, "recipientInfos", ASN1_SET
, ASN1_LOOP
}, /* 2 */
451 { 2, "recipientInfo", ASN1_SEQUENCE
, ASN1_BODY
}, /* 3 */
452 { 3, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
453 { 3, "issuerAndSerialNumber", ASN1_SEQUENCE
, ASN1_BODY
}, /* 5 */
454 { 4, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 6 */
455 { 4, "serial", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
456 { 3, "encryptionAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 8 */
457 { 3, "encryptedKey", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 9 */
458 { 1, "end loop", ASN1_EOC
, ASN1_END
}, /* 10 */
459 { 1, "encryptedContentInfo", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 11 */
460 { 2, "contentType", ASN1_OID
, ASN1_BODY
}, /* 12 */
461 { 2, "contentEncryptionAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 13 */
462 { 2, "encryptedContent", ASN1_CONTEXT_S_0
, ASN1_BODY
}, /* 14 */
463 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
465 #define PKCS7_ENVELOPED_VERSION 1
466 #define PKCS7_RECIPIENT_INFO_VERSION 4
467 #define PKCS7_ISSUER 6
468 #define PKCS7_SERIAL_NUMBER 7
469 #define PKCS7_ENCRYPTION_ALG 8
470 #define PKCS7_ENCRYPTED_KEY 9
471 #define PKCS7_CONTENT_TYPE 12
472 #define PKCS7_CONTENT_ENC_ALGORITHM 13
473 #define PKCS7_ENCRYPTED_CONTENT 14
476 * Parse PKCS#7 envelopedData content
478 static bool parse_envelopedData(private_pkcs7_t
*this, chunk_t serialNumber
,
479 rsa_private_key_t
*key
)
481 asn1_parser_t
*parser
;
484 bool success
= FALSE
;
486 chunk_t iv
= chunk_empty
;
487 chunk_t symmetric_key
= chunk_empty
;
488 chunk_t encrypted_content
= chunk_empty
;
490 crypter_t
*crypter
= NULL
;
492 if (abort_parsing(this, OID_PKCS7_ENVELOPED_DATA
))
497 parser
= asn1_parser_create(envelopedDataObjects
, this->content
);
498 parser
->set_top_level(parser
, this->level
);
500 while (parser
->iterate(parser
, &objectID
, &object
))
504 case PKCS7_ENVELOPED_VERSION
:
505 if (*object
.ptr
!= 0)
507 DBG1("envelopedData version is not 0");
511 case PKCS7_RECIPIENT_INFO_VERSION
:
512 if (*object
.ptr
!= 0)
514 DBG1("recipient info version is not 0");
520 identification_t
*issuer
;
522 issuer
= identification_create_from_encoding(ID_DER_ASN1_DN
, object
);
523 DBG2(" '%Y'", issuer
);
524 issuer
->destroy(issuer
);
527 case PKCS7_SERIAL_NUMBER
:
528 if (!chunk_equals(serialNumber
, object
))
530 DBG1("serial numbers do not match");
534 case PKCS7_ENCRYPTION_ALG
:
536 int alg
= parse_algorithmIdentifier(object
, level
, NULL
);
538 if (alg
!= OID_RSA_ENCRYPTION
)
540 DBG1("only rsa encryption supported");
545 case PKCS7_ENCRYPTED_KEY
:
546 if (key
->pkcs1_decrypt(key
, object
, &symmetric_key
) != SUCCESS
)
548 DBG1("symmetric key could not be decrypted with rsa");
551 DBG4("symmetric key : %B", &symmetric_key
);
553 case PKCS7_CONTENT_TYPE
:
554 if (known_oid(object
) != OID_PKCS7_DATA
)
556 DBG1("encrypted content not of type pkcs7 data");
560 case PKCS7_CONTENT_ENC_ALGORITHM
:
562 int alg
= parse_algorithmIdentifier(object
, level
, &iv
);
567 crypter
= crypter_create(ENCR_DES
, 0);
569 case OID_3DES_EDE_CBC
:
570 crypter
= crypter_create(ENCR_3DES
, 0);
573 DBG1("Only DES and 3DES supported for symmetric encryption");
576 if (symmetric_key
.len
!= crypter
->get_key_size(crypter
))
578 DBG1("symmetric key has wrong length");
581 if (!parse_asn1_simple_object(&iv
, ASN1_OCTET_STRING
, level
+1, "IV"))
583 DBG1("IV could not be parsed");
586 if (iv
.len
!= crypter
->get_block_size(crypter
))
588 DBG1("IV has wrong length");
593 case PKCS7_ENCRYPTED_CONTENT
:
594 encrypted_content
= object
;
598 success
= parser
->success(parser
);
601 parser
->destroy(parser
);
607 /* decrypt the content */
608 crypter
->set_key(crypter
, symmetric_key
);
609 crypter
->decrypt(crypter
, encrypted_content
, iv
, &this->data
);
610 DBG3("decrypted content with padding: %B", &this->data
);
612 /* remove the padding */
614 u_char
*pos
= this->data
.ptr
+ this->data
.len
- 1;
615 u_char pattern
= *pos
;
616 size_t padding
= pattern
;
618 if (padding
> this->data
.len
)
620 DBG1("padding greater than data length");
623 this->data
.len
-= padding
;
625 while (padding
-- > 0)
627 if (*pos
-- != pattern
)
629 DBG1("wrong padding pattern");
634 crypter
->destroy(crypter
);
635 free(symmetric_key
.ptr
);
640 free(symmetric_key
.ptr
);
641 chunk_free(&this->data
);
646 * Implements pkcs7_t.get_data.
648 static chunk_t
get_data(private_pkcs7_t
*this)
654 * Implements pkcs7_t.get_contentInfo.
656 static chunk_t
get_contentInfo(private_pkcs7_t
*this)
658 chunk_t content_type
;
660 /* select DER-encoded OID for pkcs7_contentInfo type */
664 content_type
= ASN1_pkcs7_data_oid
;
666 case OID_PKCS7_SIGNED_DATA
:
667 content_type
= ASN1_pkcs7_signed_data_oid
;
669 case OID_PKCS7_ENVELOPED_DATA
:
670 content_type
= ASN1_pkcs7_enveloped_data_oid
;
672 case OID_PKCS7_SIGNED_ENVELOPED_DATA
:
673 content_type
= ASN1_pkcs7_signed_enveloped_data_oid
;
675 case OID_PKCS7_DIGESTED_DATA
:
676 content_type
= ASN1_pkcs7_digested_data_oid
;
678 case OID_PKCS7_ENCRYPTED_DATA
:
679 content_type
= ASN1_pkcs7_encrypted_data_oid
;
683 DBG1("invalid pkcs7 contentInfo type");
687 return (this->content
.ptr
== NULL
)
688 ?
asn1_simple_object(ASN1_SEQUENCE
, content_type
)
689 : asn1_wrap(ASN1_SEQUENCE
, "cm",
691 asn1_simple_object(ASN1_CONTEXT_C_0
, this->content
)
696 * Implements pkcs7_t.create_certificate_iterator
698 static iterator_t
*create_certificate_iterator(const private_pkcs7_t
*this)
700 return this->certs
->create_iterator(this->certs
, TRUE
);
704 * Implements pkcs7_t.set_certificate
706 static void set_certificate(private_pkcs7_t
*this, x509_t
*cert
)
710 /* TODO the certificate is currently not cloned */
711 this->certs
->insert_last(this->certs
, cert
);
716 * Implements pkcs7_t.set_attributes
718 static void set_attributes(private_pkcs7_t
*this, pkcs9_t
*attributes
)
720 this->attributes
= attributes
;
724 * build a DER-encoded issuerAndSerialNumber object
726 chunk_t
pkcs7_build_issuerAndSerialNumber(x509_t
*cert
)
728 identification_t
*issuer
= cert
->get_issuer(cert
);
730 return asn1_wrap(ASN1_SEQUENCE
, "cm",
731 issuer
->get_encoding(issuer
),
732 asn1_simple_object(ASN1_INTEGER
, cert
->get_serialNumber(cert
)));
736 * Implements pkcs7_t.build_envelopedData.
738 bool build_envelopedData(private_pkcs7_t
*this, x509_t
*cert
,
739 encryption_algorithm_t alg
)
741 chunk_t iv
, symmetricKey
, in
, out
, alg_oid
;
744 /* select OID of symmetric encryption algorithm */
748 alg_oid
= ASN1_des_cbc_oid
;
751 alg_oid
= ASN1_3des_ede_cbc_oid
;
754 DBG1(" encryption algorithm %N not supported",
755 encryption_algorithm_names
, alg
);
759 crypter
= crypter_create(alg
, 0);
762 DBG1(" could not create crypter for algorithm %N",
763 encryption_algorithm_names
, alg
);
767 /* generate a true random symmetric encryption key
768 * and a pseudo-random iv
773 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_TRUE
);
774 rng
->allocate_bytes(rng
, crypter
->get_key_size(crypter
), &symmetricKey
);
775 DBG4(" symmetric encryption key: %B", &symmetricKey
);
778 rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_WEAK
);
779 rng
->allocate_bytes(rng
, crypter
->get_block_size(crypter
), &iv
);
780 DBG4(" initialization vector: %B", &iv
);
784 /* pad the data so that the total length becomes
785 * a multiple of the block size
788 size_t block_size
= crypter
->get_block_size(crypter
);
789 size_t padding
= block_size
- this->data
.len
% block_size
;
791 in
.len
= this->data
.len
+ padding
;
792 in
.ptr
= malloc(in
.len
);
794 DBG2(" padding %d bytes of data to multiple block size of %d bytes",
795 (int)this->data
.len
, (int)in
.len
);
798 memcpy(in
.ptr
, this->data
.ptr
, this->data
.len
);
800 memset(in
.ptr
+ this->data
.len
, padding
, padding
);
802 DBG3(" padded unencrypted data: %B", &in
);
804 /* symmetric encryption of data object */
805 crypter
->set_key(crypter
, symmetricKey
);
806 crypter
->encrypt(crypter
, in
, iv
, &out
);
807 crypter
->destroy(crypter
);
809 DBG3(" encrypted data: %B", &out
);
811 /* build pkcs7 enveloped data object */
813 chunk_t contentEncryptionAlgorithm
= asn1_wrap(ASN1_SEQUENCE
, "cm",
815 asn1_wrap(ASN1_OCTET_STRING
, "m", iv
));
817 chunk_t encryptedContentInfo
= asn1_wrap(ASN1_SEQUENCE
, "cmm",
819 contentEncryptionAlgorithm
,
820 asn1_wrap(ASN1_CONTEXT_S_0
, "m", out
));
822 chunk_t wrappedKey
, encryptedKey
, recipientInfo
;
824 rsa_public_key_t
*public_key
= cert
->get_public_key(cert
);
826 public_key
->pkcs1_encrypt(public_key
, symmetricKey
, &wrappedKey
);
827 chunk_clear(&symmetricKey
);
829 encryptedKey
= asn1_wrap(ASN1_OCTET_STRING
, "m", wrappedKey
);
831 recipientInfo
= asn1_wrap(ASN1_SEQUENCE
, "cmcm",
833 pkcs7_build_issuerAndSerialNumber(cert
),
834 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
837 this->content
= asn1_wrap(ASN1_SEQUENCE
, "cmm",
839 asn1_wrap(ASN1_SET
, "m", recipientInfo
),
840 encryptedContentInfo
);
841 this->type
= OID_PKCS7_ENVELOPED_DATA
;
847 * Implements pkcs7_t.build_signedData.
849 bool build_signedData(private_pkcs7_t
*this, rsa_private_key_t
*private_key
,
850 hash_algorithm_t alg
)
852 int signature_oid
= hasher_signature_algorithm_to_oid(alg
);
853 chunk_t authenticatedAttributes
= chunk_empty
;
854 chunk_t encryptedDigest
= chunk_empty
;
858 if (this->certs
->get_first(this->certs
, (void**)&cert
) != SUCCESS
)
860 DBG1(" no pkcs7 signer certificate found");
864 if (this->attributes
!= NULL
)
866 if(this->data
.ptr
!= NULL
)
870 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, alg
);
873 DBG1(" hash algorithm %N not support",
874 hash_algorithm_names
, alg
);
878 /* take the current time as signingTime */
879 time_t now
= time(NULL
);
880 chunk_t signingTime
= asn1_from_time(&now
, ASN1_UTCTIME
);
882 chunk_t messageDigest
, attributes
;
884 hasher
->allocate_hash(hasher
, this->data
, &messageDigest
);
885 hasher
->destroy(hasher
);
886 this->attributes
->set_attribute(this->attributes
,
887 OID_PKCS9_CONTENT_TYPE
, ASN1_pkcs7_data_oid
);
888 this->attributes
->set_messageDigest(this->attributes
,
890 this->attributes
->set_attribute(this->attributes
,
891 OID_PKCS9_SIGNING_TIME
, signingTime
);
892 attributes
= this->attributes
->get_encoding(this->attributes
);
894 free(messageDigest
.ptr
);
895 free(signingTime
.ptr
);
897 private_key
->build_emsa_pkcs1_signature(private_key
, alg
,
898 attributes
, &encryptedDigest
);
899 authenticatedAttributes
= chunk_clone(attributes
);
900 *authenticatedAttributes
.ptr
= ASN1_CONTEXT_C_0
;
903 else if (this->data
.ptr
!= NULL
)
905 private_key
->build_emsa_pkcs1_signature(private_key
, alg
,
906 this->data
, &encryptedDigest
);
908 if (encryptedDigest
.ptr
)
910 encryptedDigest
= asn1_wrap(ASN1_OCTET_STRING
, "m", encryptedDigest
);
913 signerInfo
= asn1_wrap(ASN1_SEQUENCE
, "cmcmcm",
915 pkcs7_build_issuerAndSerialNumber(cert
),
916 asn1_algorithmIdentifier(signature_oid
),
917 authenticatedAttributes
,
918 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
921 if (this->data
.ptr
!= NULL
)
923 this->content
= asn1_simple_object(ASN1_OCTET_STRING
, this->data
);
924 chunk_free(&this->data
);
926 this->type
= OID_PKCS7_DATA
;
927 this->data
= get_contentInfo(this);
928 chunk_free(&this->content
);
930 this->type
= OID_PKCS7_SIGNED_DATA
;
932 this->content
= asn1_wrap(ASN1_SEQUENCE
, "cmcmm",
934 asn1_simple_object(ASN1_SET
, asn1_algorithmIdentifier(signature_oid
)),
936 asn1_simple_object(ASN1_CONTEXT_C_0
, cert
->get_certificate(cert
)),
937 asn1_wrap(ASN1_SET
, "m", signerInfo
));
943 * Implements pkcs7_t.destroy
945 static void destroy(private_pkcs7_t
*this)
947 DESTROY_IF(this->attributes
);
948 this->certs
->destroy_offset(this->certs
, offsetof(x509_t
, destroy
));
949 free(this->content
.ptr
);
950 free(this->data
.ptr
);
955 * ASN.1 definition of the PKCS#7 ContentInfo type
957 static const asn1Object_t contentInfoObjects
[] = {
958 { 0, "contentInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
959 { 1, "contentType", ASN1_OID
, ASN1_BODY
}, /* 1 */
960 { 1, "content", ASN1_CONTEXT_C_0
, ASN1_OPT
|
962 { 1, "end opt", ASN1_EOC
, ASN1_END
}, /* 3 */
963 { 0, "exit", ASN1_EOC
, ASN1_EXIT
}
965 #define PKCS7_INFO_TYPE 1
966 #define PKCS7_INFO_CONTENT 2
969 * Parse PKCS#7 contentInfo object
971 static bool parse_contentInfo(chunk_t blob
, u_int level0
, private_pkcs7_t
*cInfo
)
973 asn1_parser_t
*parser
;
976 bool success
= FALSE
;
978 parser
= asn1_parser_create(contentInfoObjects
, blob
);
979 parser
->set_top_level(parser
, level0
);
981 while (parser
->iterate(parser
, &objectID
, &object
))
983 if (objectID
== PKCS7_INFO_TYPE
)
985 cInfo
->type
= known_oid(object
);
986 if (cInfo
->type
< OID_PKCS7_DATA
987 || cInfo
->type
> OID_PKCS7_ENCRYPTED_DATA
)
989 DBG1("unknown pkcs7 content type");
993 else if (objectID
== PKCS7_INFO_CONTENT
&& object
.len
> 0)
995 cInfo
->content
= chunk_clone(object
);
998 success
= parser
->success(parser
);
1001 parser
->destroy(parser
);
1006 * Generic private constructor
1008 static private_pkcs7_t
*pkcs7_create_empty(void)
1010 private_pkcs7_t
*this = malloc_thing(private_pkcs7_t
);
1013 this->type
= OID_UNKNOWN
;
1014 this->content
= chunk_empty
;
1015 this->parsed
= FALSE
;
1017 this->data
= chunk_empty
;
1018 this->attributes
= NULL
;
1019 this->certs
= linked_list_create();
1021 /*public functions */
1022 this->public.is_data
= (bool (*) (pkcs7_t
*))is_data
;
1023 this->public.is_signedData
= (bool (*) (pkcs7_t
*))is_signedData
;
1024 this->public.is_envelopedData
= (bool (*) (pkcs7_t
*))is_envelopedData
;
1025 this->public.parse_data
= (bool (*) (pkcs7_t
*))parse_data
;
1026 this->public.parse_signedData
= (bool (*) (pkcs7_t
*,x509_t
*))parse_signedData
;
1027 this->public.parse_envelopedData
= (bool (*) (pkcs7_t
*,chunk_t
,rsa_private_key_t
*))parse_envelopedData
;
1028 this->public.get_data
= (chunk_t (*) (pkcs7_t
*))get_data
;
1029 this->public.get_contentInfo
= (chunk_t (*) (pkcs7_t
*))get_contentInfo
;
1030 this->public.create_certificate_iterator
= (iterator_t
* (*) (pkcs7_t
*))create_certificate_iterator
;
1031 this->public.set_certificate
= (void (*) (pkcs7_t
*,x509_t
*))set_certificate
;
1032 this->public.set_attributes
= (void (*) (pkcs7_t
*,pkcs9_t
*))set_attributes
;
1033 this->public.build_envelopedData
= (bool (*) (pkcs7_t
*,x509_t
*,encryption_algorithm_t
))build_envelopedData
;
1034 this->public.build_signedData
= (bool (*) (pkcs7_t
*,rsa_private_key_t
*,hash_algorithm_t
))build_signedData
;
1035 this->public.destroy
= (void (*) (pkcs7_t
*))destroy
;
1041 * Described in header.
1043 pkcs7_t
*pkcs7_create_from_chunk(chunk_t chunk
, u_int level
)
1045 private_pkcs7_t
*this = pkcs7_create_empty();
1047 this->level
= level
+ 2;
1048 if (!parse_contentInfo(chunk
, level
, this))
1053 return &this->public;
1057 * Described in header.
1059 pkcs7_t
*pkcs7_create_from_data(chunk_t data
)
1061 private_pkcs7_t
*this = pkcs7_create_empty();
1063 this->data
= chunk_clone(data
);
1064 this->parsed
= TRUE
;
1066 return &this->public;