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
26 #include <asn1/asn1.h>
28 #include <crypto/x509.h>
29 #include <crypto/pkcs9.h>
30 #include <crypto/hashers/hasher.h>
31 #include <crypto/crypters/crypter.h>
32 #include <crypto/rsa/rsa_public_key.h>
33 #include <utils/randomizer.h>
34 #include <utils/linked_list.h>
38 typedef struct private_pkcs7_t private_pkcs7_t
;
41 * Private data of a pkcs7_t object.
43 struct private_pkcs7_t
{
45 * Public interface for this certificate.
55 * ASN.1 encoded content
60 * Has the content already been parsed?
65 * ASN.1 parsing start level
75 * ASN.1 encoded attributes
80 * Linked list of X.509 certificates
86 * ASN.1 definition of the PKCS#7 ContentInfo type
88 static const asn1Object_t contentInfoObjects
[] = {
89 { 0, "contentInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
90 { 1, "contentType", ASN1_OID
, ASN1_BODY
}, /* 1 */
91 { 1, "content", ASN1_CONTEXT_C_0
, ASN1_OPT
|
93 { 1, "end opt", ASN1_EOC
, ASN1_END
} /* 3 */
96 #define PKCS7_INFO_TYPE 1
97 #define PKCS7_INFO_CONTENT 2
98 #define PKCS7_INFO_ROOF 4
101 * ASN.1 definition of the PKCS#7 signedData type
103 static const asn1Object_t signedDataObjects
[] = {
104 { 0, "signedData", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
105 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
106 { 1, "digestAlgorithms", ASN1_SET
, ASN1_LOOP
}, /* 2 */
107 { 2, "algorithm", ASN1_EOC
, ASN1_RAW
}, /* 3 */
108 { 1, "end loop", ASN1_EOC
, ASN1_END
}, /* 4 */
109 { 1, "contentInfo", ASN1_EOC
, ASN1_RAW
}, /* 5 */
110 { 1, "certificates", ASN1_CONTEXT_C_0
, ASN1_OPT
|
112 { 2, "certificate", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 7 */
113 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 8 */
114 { 1, "crls", ASN1_CONTEXT_C_1
, ASN1_OPT
|
116 { 2, "crl", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 10 */
117 { 1, "end opt or loop", ASN1_EOC
, ASN1_END
}, /* 11 */
118 { 1, "signerInfos", ASN1_SET
, ASN1_LOOP
}, /* 12 */
119 { 2, "signerInfo", ASN1_SEQUENCE
, ASN1_NONE
}, /* 13 */
120 { 3, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 14 */
121 { 3, "issuerAndSerialNumber", ASN1_SEQUENCE
, ASN1_BODY
}, /* 15 */
122 { 4, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 16 */
123 { 4, "serial", ASN1_INTEGER
, ASN1_BODY
}, /* 17 */
124 { 3, "digestAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 18 */
125 { 3, "authenticatedAttributes", ASN1_CONTEXT_C_0
, ASN1_OPT
|
127 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 20 */
128 { 3, "digestEncryptionAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 21 */
129 { 3, "encryptedDigest", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 22 */
130 { 3, "unauthenticatedAttributes", ASN1_CONTEXT_C_1
, ASN1_OPT
}, /* 23 */
131 { 3, "end opt", ASN1_EOC
, ASN1_END
}, /* 24 */
132 { 1, "end loop", ASN1_EOC
, ASN1_END
} /* 25 */
135 #define PKCS7_DIGEST_ALG 3
136 #define PKCS7_SIGNED_CONTENT_INFO 5
137 #define PKCS7_SIGNED_CERT 7
138 #define PKCS7_SIGNER_INFO 13
139 #define PKCS7_SIGNED_ISSUER 16
140 #define PKCS7_SIGNED_SERIAL_NUMBER 17
141 #define PKCS7_DIGEST_ALGORITHM 18
142 #define PKCS7_AUTH_ATTRIBUTES 19
143 #define PKCS7_DIGEST_ENC_ALGORITHM 21
144 #define PKCS7_ENCRYPTED_DIGEST 22
145 #define PKCS7_SIGNED_ROOF 26
148 * ASN.1 definition of the PKCS#7 envelopedData type
150 static const asn1Object_t envelopedDataObjects
[] = {
151 { 0, "envelopedData", ASN1_SEQUENCE
, ASN1_NONE
}, /* 0 */
152 { 1, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 1 */
153 { 1, "recipientInfos", ASN1_SET
, ASN1_LOOP
}, /* 2 */
154 { 2, "recipientInfo", ASN1_SEQUENCE
, ASN1_BODY
}, /* 3 */
155 { 3, "version", ASN1_INTEGER
, ASN1_BODY
}, /* 4 */
156 { 3, "issuerAndSerialNumber", ASN1_SEQUENCE
, ASN1_BODY
}, /* 5 */
157 { 4, "issuer", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 6 */
158 { 4, "serial", ASN1_INTEGER
, ASN1_BODY
}, /* 7 */
159 { 3, "encryptionAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 8 */
160 { 3, "encryptedKey", ASN1_OCTET_STRING
, ASN1_BODY
}, /* 9 */
161 { 1, "end loop", ASN1_EOC
, ASN1_END
}, /* 10 */
162 { 1, "encryptedContentInfo", ASN1_SEQUENCE
, ASN1_OBJ
}, /* 11 */
163 { 2, "contentType", ASN1_OID
, ASN1_BODY
}, /* 12 */
164 { 2, "contentEncryptionAlgorithm", ASN1_EOC
, ASN1_RAW
}, /* 13 */
165 { 2, "encryptedContent", ASN1_CONTEXT_S_0
, ASN1_BODY
} /* 14 */
168 #define PKCS7_ENVELOPED_VERSION 1
169 #define PKCS7_RECIPIENT_INFO_VERSION 4
170 #define PKCS7_ISSUER 6
171 #define PKCS7_SERIAL_NUMBER 7
172 #define PKCS7_ENCRYPTION_ALG 8
173 #define PKCS7_ENCRYPTED_KEY 9
174 #define PKCS7_CONTENT_TYPE 12
175 #define PKCS7_CONTENT_ENC_ALGORITHM 13
176 #define PKCS7_ENCRYPTED_CONTENT 14
177 #define PKCS7_ENVELOPED_ROOF 15
180 * PKCS7 contentInfo OIDs
182 static u_char ASN1_pkcs7_data_oid_str
[] = {
184 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
187 static u_char ASN1_pkcs7_signed_data_oid_str
[] = {
189 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02
192 static u_char ASN1_pkcs7_enveloped_data_oid_str
[] = {
194 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x03
197 static u_char ASN1_pkcs7_signed_enveloped_data_oid_str
[] = {
199 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x04
202 static u_char ASN1_pkcs7_digested_data_oid_str
[] = {
204 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x05
207 static char ASN1_pkcs7_encrypted_data_oid_str
[] = {
209 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x06
212 static const chunk_t ASN1_pkcs7_data_oid
=
213 chunk_from_buf(ASN1_pkcs7_data_oid_str
);
214 static const chunk_t ASN1_pkcs7_signed_data_oid
=
215 chunk_from_buf(ASN1_pkcs7_signed_data_oid_str
);
216 static const chunk_t ASN1_pkcs7_enveloped_data_oid
=
217 chunk_from_buf(ASN1_pkcs7_enveloped_data_oid_str
);
218 static const chunk_t ASN1_pkcs7_signed_enveloped_data_oid
=
219 chunk_from_buf(ASN1_pkcs7_signed_enveloped_data_oid_str
);
220 static const chunk_t ASN1_pkcs7_digested_data_oid
=
221 chunk_from_buf(ASN1_pkcs7_digested_data_oid_str
);
222 static const chunk_t ASN1_pkcs7_encrypted_data_oid
=
223 chunk_from_buf(ASN1_pkcs7_encrypted_data_oid_str
);
226 * 3DES and DES encryption OIDs
228 static u_char ASN1_3des_ede_cbc_oid_str
[] = {
230 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07
233 static u_char ASN1_des_cbc_oid_str
[] = {
235 0x2B, 0x0E, 0x03, 0x02, 0x07
238 static const chunk_t ASN1_3des_ede_cbc_oid
=
239 chunk_from_buf(ASN1_3des_ede_cbc_oid_str
);
240 static const chunk_t ASN1_des_cbc_oid
=
241 chunk_from_buf(ASN1_des_cbc_oid_str
);
244 * Implements pkcs7_t.is_data.
246 static bool is_data(private_pkcs7_t
*this)
248 return this->type
== OID_PKCS7_DATA
;
252 * Implements pkcs7_t.is_signedData.
254 static bool is_signedData(private_pkcs7_t
*this)
256 return this->type
== OID_PKCS7_SIGNED_DATA
;
260 * Implements pkcs7_t.is_envelopedData.
262 static bool is_envelopedData(private_pkcs7_t
*this)
264 return this->type
== OID_PKCS7_ENVELOPED_DATA
;
268 * Check whether to abort the requested parsing
270 static bool abort_parsing(private_pkcs7_t
*this, int type
)
272 if (this->type
!= type
)
274 DBG1("pkcs7 content to be parsed is not of type '%s'",
280 DBG1("pkcs7 content has already been parsed");
288 * Implements pkcs7_t.parse_data.
290 static bool parse_data(private_pkcs7_t
*this)
292 chunk_t data
= this->content
;
294 if (abort_parsing(this, OID_PKCS7_DATA
))
300 this->data
= chunk_empty
;
303 if (parse_asn1_simple_object(&data
, ASN1_OCTET_STRING
, this->level
, "data"))
305 this->data
= chunk_clone(data
);
315 * Implements pkcs7_t.parse_signedData.
317 static bool parse_signedData(private_pkcs7_t
*this, x509_t
*cacert
)
324 int digest_alg
= OID_UNKNOWN
;
325 int enc_alg
= OID_UNKNOWN
;
328 chunk_t encrypted_digest
= chunk_empty
;
330 if (abort_parsing(this, OID_PKCS7_SIGNED_DATA
))
335 asn1_init(&ctx
, this->content
, this->level
, FALSE
, FALSE
);
337 while (objectID
< PKCS7_SIGNED_ROOF
)
339 if (!extract_object(signedDataObjects
, &objectID
, &object
, &level
, &ctx
))
346 case PKCS7_DIGEST_ALG
:
347 digest_alg
= parse_algorithmIdentifier(object
, level
, NULL
);
349 case PKCS7_SIGNED_CONTENT_INFO
:
352 pkcs7_t
*data
= pkcs7_create_from_chunk(object
, level
+1);
358 if (!data
->parse_data(data
))
363 pureData
= data
->get_data(data
);
364 this->data
= (pureData
.len
)?
chunk_clone(pureData
) : chunk_empty
;
368 case PKCS7_SIGNED_CERT
:
370 x509_t
*cert
= x509_create_from_chunk(chunk_clone(object
), level
+1);
374 this->certs
->insert_last(this->certs
, (void*)cert
);
378 case PKCS7_SIGNER_INFO
:
380 DBG2(" signer #%d", signerInfos
);
382 case PKCS7_SIGNED_ISSUER
:
384 identification_t
*issuer
;
386 issuer
= identification_create_from_encoding(ID_DER_ASN1_DN
, object
);
387 DBG2(" '%D'", issuer
);
388 issuer
->destroy(issuer
);
391 case PKCS7_AUTH_ATTRIBUTES
:
392 *object
.ptr
= ASN1_SET
;
393 this->attributes
= pkcs9_create_from_chunk(object
, level
+1);
394 *object
.ptr
= ASN1_CONTEXT_C_0
;
396 case PKCS7_DIGEST_ALGORITHM
:
397 digest_alg
= parse_algorithmIdentifier(object
, level
, NULL
);
399 case PKCS7_DIGEST_ENC_ALGORITHM
:
400 enc_alg
= parse_algorithmIdentifier(object
, level
, NULL
);
402 case PKCS7_ENCRYPTED_DIGEST
:
403 encrypted_digest
= object
;
408 /* check the signature only if a cacert is available */
411 hash_algorithm_t algorithm
= hasher_algorithm_from_oid(digest_alg
);
412 rsa_public_key_t
*signer
= cacert
->get_public_key(cacert
);
414 if (signerInfos
== 0)
416 DBG1("no signerInfo object found");
419 else if (signerInfos
> 1)
421 DBG1("more than one signerInfo object found");
424 if (this->attributes
== NULL
)
426 DBG1("no authenticatedAttributes object found");
429 if (enc_alg
!= OID_RSA_ENCRYPTION
)
431 DBG1("only RSA digest encryption supported");
434 if (signer
->verify_emsa_pkcs1_signature(signer
, algorithm
,
435 this->attributes
->get_encoding(this->attributes
), encrypted_digest
) != SUCCESS
)
437 DBG1("invalid digest signature");
442 DBG2("digest signature is valid");
444 if (this->data
.ptr
!= NULL
)
446 chunk_t messageDigest
= this->attributes
->get_messageDigest(this->attributes
);
448 if (messageDigest
.ptr
== NULL
)
450 DBG1("messageDigest attribute not found");
459 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, algorithm
)
462 DBG1("hash algorithm %N not supported",
463 hash_algorithm_names
, algorithm
);
464 free(messageDigest
.ptr
);
467 hasher
->allocate_hash(hasher
, this->data
, &hash
);
468 hasher
->destroy(hasher
);
469 DBG3("hash: %B", &hash
);
471 valid
= chunk_equals(messageDigest
, hash
);
472 free(messageDigest
.ptr
);
476 DBG2("messageDigest is valid");
480 DBG1("invalid messageDigest");
490 * Parse PKCS#7 envelopedData content
492 static bool parse_envelopedData(private_pkcs7_t
*this, chunk_t serialNumber
,
493 rsa_private_key_t
*key
)
500 chunk_t iv
= chunk_empty
;
501 chunk_t symmetric_key
= chunk_empty
;
502 chunk_t encrypted_content
= chunk_empty
;
504 crypter_t
*crypter
= NULL
;
506 if (abort_parsing(this, OID_PKCS7_ENVELOPED_DATA
))
511 asn1_init(&ctx
, this->content
, this->level
, FALSE
, FALSE
);
513 while (objectID
< PKCS7_ENVELOPED_ROOF
)
515 if (!extract_object(envelopedDataObjects
, &objectID
, &object
, &level
, &ctx
))
522 case PKCS7_ENVELOPED_VERSION
:
523 if (*object
.ptr
!= 0)
525 DBG1("envelopedData version is not 0");
529 case PKCS7_RECIPIENT_INFO_VERSION
:
530 if (*object
.ptr
!= 0)
532 DBG1("recipient info version is not 0");
538 identification_t
*issuer
;
540 issuer
= identification_create_from_encoding(ID_DER_ASN1_DN
, object
);
541 DBG2(" '%D'", issuer
);
542 issuer
->destroy(issuer
);
545 case PKCS7_SERIAL_NUMBER
:
546 if (!chunk_equals(serialNumber
, object
))
548 DBG1("serial numbers do not match");
552 case PKCS7_ENCRYPTION_ALG
:
554 int alg
= parse_algorithmIdentifier(object
, level
, NULL
);
556 if (alg
!= OID_RSA_ENCRYPTION
)
558 DBG1("only rsa encryption supported");
563 case PKCS7_ENCRYPTED_KEY
:
564 if (key
->pkcs1_decrypt(key
, object
, &symmetric_key
) != SUCCESS
)
566 DBG1("symmetric key could not be decrypted with rsa");
569 DBG4("symmetric key : %B", &symmetric_key
);
571 case PKCS7_CONTENT_TYPE
:
572 if (known_oid(object
) != OID_PKCS7_DATA
)
574 DBG1("encrypted content not of type pkcs7 data");
578 case PKCS7_CONTENT_ENC_ALGORITHM
:
580 int alg
= parse_algorithmIdentifier(object
, level
, &iv
);
585 crypter
= crypter_create(ENCR_DES
, 0);
587 case OID_3DES_EDE_CBC
:
588 crypter
= crypter_create(ENCR_3DES
, 0);
591 DBG1("Only DES and 3DES supported for symmetric encryption");
594 if (symmetric_key
.len
!= crypter
->get_key_size(crypter
))
596 DBG1("symmetric key has wrong length");
599 if (!parse_asn1_simple_object(&iv
, ASN1_OCTET_STRING
, level
+1, "IV"))
601 DBG1("IV could not be parsed");
604 if (iv
.len
!= crypter
->get_block_size(crypter
))
606 DBG1("IV has wrong length");
611 case PKCS7_ENCRYPTED_CONTENT
:
612 encrypted_content
= object
;
618 /* decrypt the content */
619 crypter
->set_key(crypter
, symmetric_key
);
620 crypter
->decrypt(crypter
, encrypted_content
, iv
, &this->data
);
621 DBG3("decrypted content with padding: %B", &this->data
);
623 /* remove the padding */
625 u_char
*pos
= this->data
.ptr
+ this->data
.len
- 1;
626 u_char pattern
= *pos
;
627 size_t padding
= pattern
;
629 if (padding
> this->data
.len
)
631 DBG1("padding greater than data length");
634 this->data
.len
-= padding
;
636 while (padding
-- > 0)
638 if (*pos
-- != pattern
)
640 DBG1("wrong padding pattern");
645 crypter
->destroy(crypter
);
646 free(symmetric_key
.ptr
);
651 free(symmetric_key
.ptr
);
652 chunk_free(&this->data
);
657 * Implements pkcs7_t.get_data.
659 static chunk_t
get_data(private_pkcs7_t
*this)
665 * Implements pkcs7_t.get_contentInfo.
667 static chunk_t
get_contentInfo(private_pkcs7_t
*this)
669 chunk_t content_type
;
671 /* select DER-encoded OID for pkcs7_contentInfo type */
675 content_type
= ASN1_pkcs7_data_oid
;
677 case OID_PKCS7_SIGNED_DATA
:
678 content_type
= ASN1_pkcs7_signed_data_oid
;
680 case OID_PKCS7_ENVELOPED_DATA
:
681 content_type
= ASN1_pkcs7_enveloped_data_oid
;
683 case OID_PKCS7_SIGNED_ENVELOPED_DATA
:
684 content_type
= ASN1_pkcs7_signed_enveloped_data_oid
;
686 case OID_PKCS7_DIGESTED_DATA
:
687 content_type
= ASN1_pkcs7_digested_data_oid
;
689 case OID_PKCS7_ENCRYPTED_DATA
:
690 content_type
= ASN1_pkcs7_encrypted_data_oid
;
694 DBG1("invalid pkcs7 contentInfo type");
698 return (this->content
.ptr
== NULL
)
699 ?
asn1_simple_object(ASN1_SEQUENCE
, content_type
)
700 : asn1_wrap(ASN1_SEQUENCE
, "cm",
702 asn1_simple_object(ASN1_CONTEXT_C_0
, this->content
)
707 * Implements pkcs7_t.create_certificate_iterator
709 static iterator_t
*create_certificate_iterator(const private_pkcs7_t
*this)
711 return this->certs
->create_iterator(this->certs
, TRUE
);
715 * Implements pkcs7_t.set_certificate
717 static void set_certificate(private_pkcs7_t
*this, x509_t
*cert
)
721 /* TODO the certificate is currently not cloned */
722 this->certs
->insert_last(this->certs
, cert
);
727 * Implements pkcs7_t.set_attributes
729 static void set_attributes(private_pkcs7_t
*this, pkcs9_t
*attributes
)
731 this->attributes
= attributes
;
735 * build a DER-encoded issuerAndSerialNumber object
737 chunk_t
pkcs7_build_issuerAndSerialNumber(x509_t
*cert
)
739 identification_t
*issuer
= cert
->get_issuer(cert
);
741 return asn1_wrap(ASN1_SEQUENCE
, "cm",
742 issuer
->get_encoding(issuer
),
743 asn1_simple_object(ASN1_INTEGER
, cert
->get_serialNumber(cert
)));
747 * Implements pkcs7_t.build_envelopedData.
749 bool build_envelopedData(private_pkcs7_t
*this, x509_t
*cert
,
750 encryption_algorithm_t alg
)
752 chunk_t iv
, symmetricKey
, in
, out
, alg_oid
;
755 /* select OID of symmetric encryption algorithm */
759 alg_oid
= ASN1_des_cbc_oid
;
762 alg_oid
= ASN1_3des_ede_cbc_oid
;
765 DBG1(" encryption algorithm %N not supported",
766 encryption_algorithm_names
, alg
);
770 crypter
= crypter_create(alg
, 0);
773 DBG1(" could not create crypter for algorithm %N",
774 encryption_algorithm_names
, alg
);
778 /* generate a true random symmetric encryption key
779 * and a pseudo-random iv
782 randomizer_t
*randomizer
= randomizer_create();
784 randomizer
->allocate_random_bytes(randomizer
,
785 crypter
->get_key_size(crypter
), &symmetricKey
);
786 DBG4(" symmetric encryption key: %B", &symmetricKey
);
788 randomizer
->allocate_pseudo_random_bytes(randomizer
,
789 crypter
->get_block_size(crypter
), &iv
);
790 DBG4(" initialization vector: %B", &iv
);
792 randomizer
->destroy(randomizer
);
795 /* pad the data so that the total length becomes
796 * a multiple of the block size
799 size_t block_size
= crypter
->get_block_size(crypter
);
800 size_t padding
= block_size
- this->data
.len
% block_size
;
802 in
.len
= this->data
.len
+ padding
;
803 in
.ptr
= malloc(in
.len
);
805 DBG2(" padding %d bytes of data to multiple block size of %d bytes",
806 (int)this->data
.len
, (int)in
.len
);
809 memcpy(in
.ptr
, this->data
.ptr
, this->data
.len
);
811 memset(in
.ptr
+ this->data
.len
, padding
, padding
);
813 DBG3(" padded unencrypted data: %B", &in
);
815 /* symmetric encryption of data object */
816 crypter
->set_key(crypter
, symmetricKey
);
817 crypter
->encrypt(crypter
, in
, iv
, &out
);
818 crypter
->destroy(crypter
);
819 chunk_free_randomized(&in
);
820 DBG3(" encrypted data: %B", &out
);
822 /* build pkcs7 enveloped data object */
824 chunk_t contentEncryptionAlgorithm
= asn1_wrap(ASN1_SEQUENCE
, "cm",
826 asn1_wrap(ASN1_OCTET_STRING
, "m", iv
));
828 chunk_t encryptedContentInfo
= asn1_wrap(ASN1_SEQUENCE
, "cmm",
830 contentEncryptionAlgorithm
,
831 asn1_wrap(ASN1_CONTEXT_S_0
, "m", out
));
833 chunk_t wrappedKey
, encryptedKey
, recipientInfo
;
835 rsa_public_key_t
*public_key
= cert
->get_public_key(cert
);
837 public_key
->pkcs1_encrypt(public_key
, symmetricKey
, &wrappedKey
);
838 chunk_free_randomized(&symmetricKey
);
840 encryptedKey
= asn1_wrap(ASN1_OCTET_STRING
, "m", wrappedKey
);
842 recipientInfo
= asn1_wrap(ASN1_SEQUENCE
, "cmcm",
844 pkcs7_build_issuerAndSerialNumber(cert
),
845 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
848 this->content
= asn1_wrap(ASN1_SEQUENCE
, "cmm",
850 asn1_wrap(ASN1_SET
, "m", recipientInfo
),
851 encryptedContentInfo
);
852 this->type
= OID_PKCS7_ENVELOPED_DATA
;
858 * Implements pkcs7_t.build_signedData.
860 bool build_signedData(private_pkcs7_t
*this, rsa_private_key_t
*private_key
,
861 hash_algorithm_t alg
)
863 int signature_oid
= hasher_signature_algorithm_to_oid(alg
);
864 chunk_t authenticatedAttributes
= chunk_empty
;
865 chunk_t encryptedDigest
= chunk_empty
;
869 if (this->certs
->get_first(this->certs
, (void**)&cert
) != SUCCESS
)
871 DBG1(" no pkcs7 signer certificate found");
875 if (this->attributes
!= NULL
)
877 if(this->data
.ptr
!= NULL
)
881 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, alg
);
884 DBG1(" hash algorithm %N not support",
885 hash_algorithm_names
, alg
);
889 /* take the current time as signingTime */
890 time_t now
= time(NULL
);
891 chunk_t signingTime
= timetoasn1(&now
, ASN1_UTCTIME
);
893 chunk_t messageDigest
, attributes
;
895 hasher
->allocate_hash(hasher
, this->data
, &messageDigest
);
896 hasher
->destroy(hasher
);
897 this->attributes
->set_attribute(this->attributes
,
898 OID_PKCS9_CONTENT_TYPE
, ASN1_pkcs7_data_oid
);
899 this->attributes
->set_messageDigest(this->attributes
,
901 this->attributes
->set_attribute(this->attributes
,
902 OID_PKCS9_SIGNING_TIME
, signingTime
);
903 attributes
= this->attributes
->get_encoding(this->attributes
);
905 free(messageDigest
.ptr
);
906 free(signingTime
.ptr
);
908 private_key
->build_emsa_pkcs1_signature(private_key
, alg
,
909 attributes
, &encryptedDigest
);
910 authenticatedAttributes
= chunk_clone(attributes
);
911 *authenticatedAttributes
.ptr
= ASN1_CONTEXT_C_0
;
914 else if (this->data
.ptr
!= NULL
)
916 private_key
->build_emsa_pkcs1_signature(private_key
, alg
,
917 this->data
, &encryptedDigest
);
919 if (encryptedDigest
.ptr
)
921 encryptedDigest
= asn1_wrap(ASN1_OCTET_STRING
, "m", encryptedDigest
);
924 signerInfo
= asn1_wrap(ASN1_SEQUENCE
, "cmcmcm",
926 pkcs7_build_issuerAndSerialNumber(cert
),
927 asn1_algorithmIdentifier(signature_oid
),
928 authenticatedAttributes
,
929 asn1_algorithmIdentifier(OID_RSA_ENCRYPTION
),
932 if (this->data
.ptr
!= NULL
)
934 this->content
= asn1_simple_object(ASN1_OCTET_STRING
, this->data
);
935 chunk_free(&this->data
);
937 this->type
= OID_PKCS7_DATA
;
938 this->data
= get_contentInfo(this);
939 chunk_free(&this->content
);
941 this->type
= OID_PKCS7_SIGNED_DATA
;
943 this->content
= asn1_wrap(ASN1_SEQUENCE
, "cmcmm",
945 asn1_simple_object(ASN1_SET
, asn1_algorithmIdentifier(signature_oid
)),
947 asn1_simple_object(ASN1_CONTEXT_C_0
, cert
->get_certificate(cert
)),
948 asn1_wrap(ASN1_SET
, "m", signerInfo
));
954 * Implements pkcs7_t.destroy
956 static void destroy(private_pkcs7_t
*this)
958 DESTROY_IF(this->attributes
);
959 this->certs
->destroy_offset(this->certs
, offsetof(x509_t
, destroy
));
960 free(this->content
.ptr
);
961 free(this->data
.ptr
);
966 * Parse PKCS#7 contentInfo object
968 static bool parse_contentInfo(chunk_t blob
, u_int level0
, private_pkcs7_t
*cInfo
)
975 asn1_init(&ctx
, blob
, level0
, FALSE
, FALSE
);
977 while (objectID
< PKCS7_INFO_ROOF
)
979 if (!extract_object(contentInfoObjects
, &objectID
, &object
, &level
, &ctx
))
984 if (objectID
== PKCS7_INFO_TYPE
)
986 cInfo
->type
= known_oid(object
);
987 if (cInfo
->type
< OID_PKCS7_DATA
988 || cInfo
->type
> OID_PKCS7_ENCRYPTED_DATA
)
990 DBG1("unknown pkcs7 content type");
994 else if (objectID
== PKCS7_INFO_CONTENT
&& object
.len
> 0)
996 cInfo
->content
= chunk_clone(object
);
1004 * Generic private constructor
1006 static private_pkcs7_t
*pkcs7_create_empty(void)
1008 private_pkcs7_t
*this = malloc_thing(private_pkcs7_t
);
1011 this->type
= OID_UNKNOWN
;
1012 this->content
= chunk_empty
;
1013 this->parsed
= FALSE
;
1015 this->data
= chunk_empty
;
1016 this->attributes
= NULL
;
1017 this->certs
= linked_list_create();
1019 /*public functions */
1020 this->public.is_data
= (bool (*) (pkcs7_t
*))is_data
;
1021 this->public.is_signedData
= (bool (*) (pkcs7_t
*))is_signedData
;
1022 this->public.is_envelopedData
= (bool (*) (pkcs7_t
*))is_envelopedData
;
1023 this->public.parse_data
= (bool (*) (pkcs7_t
*))parse_data
;
1024 this->public.parse_signedData
= (bool (*) (pkcs7_t
*,x509_t
*))parse_signedData
;
1025 this->public.parse_envelopedData
= (bool (*) (pkcs7_t
*,chunk_t
,rsa_private_key_t
*))parse_envelopedData
;
1026 this->public.get_data
= (chunk_t (*) (pkcs7_t
*))get_data
;
1027 this->public.get_contentInfo
= (chunk_t (*) (pkcs7_t
*))get_contentInfo
;
1028 this->public.create_certificate_iterator
= (iterator_t
* (*) (pkcs7_t
*))create_certificate_iterator
;
1029 this->public.set_certificate
= (void (*) (pkcs7_t
*,x509_t
*))set_certificate
;
1030 this->public.set_attributes
= (void (*) (pkcs7_t
*,pkcs9_t
*))set_attributes
;
1031 this->public.build_envelopedData
= (bool (*) (pkcs7_t
*,x509_t
*,encryption_algorithm_t
))build_envelopedData
;
1032 this->public.build_signedData
= (bool (*) (pkcs7_t
*,rsa_private_key_t
*,hash_algorithm_t
))build_signedData
;
1033 this->public.destroy
= (void (*) (pkcs7_t
*))destroy
;
1039 * Described in header.
1041 pkcs7_t
*pkcs7_create_from_chunk(chunk_t chunk
, u_int level
)
1043 private_pkcs7_t
*this = pkcs7_create_empty();
1045 this->level
= level
+ 2;
1046 if (!parse_contentInfo(chunk
, level
, this))
1051 return &this->public;
1055 * Described in header.
1057 pkcs7_t
*pkcs7_create_from_data(chunk_t data
)
1059 private_pkcs7_t
*this = pkcs7_create_empty();
1061 this->data
= chunk_clone(data
);
1062 this->parsed
= TRUE
;
1064 return &this->public;
1068 * Described in header.
1070 pkcs7_t
*pkcs7_create_from_file(const char *filename
, const char *label
)
1073 chunk_t chunk
= chunk_empty
;
1074 char cert_label
[BUF_LEN
];
1077 snprintf(cert_label
, BUF_LEN
, "%s pkcs7", label
);
1079 if (!pem_asn1_load_file(filename
, NULL
, cert_label
, &chunk
, &pgp
))
1084 pkcs7
= pkcs7_create_from_chunk(chunk
, 0);