1 /* $OpenBSD: pk7_doit.c,v 1.41 2017/01/29 17:49:23 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
63 #include <openssl/err.h>
64 #include <openssl/objects.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
68 static int add_attribute(STACK_OF(X509_ATTRIBUTE
) **sk
, int nid
, int atrtype
,
70 static ASN1_TYPE
*get_attribute(STACK_OF(X509_ATTRIBUTE
) *sk
, int nid
);
73 PKCS7_type_is_other(PKCS7
* p7
)
77 int nid
= OBJ_obj2nid(p7
->type
);
81 case NID_pkcs7_signed
:
82 case NID_pkcs7_enveloped
:
83 case NID_pkcs7_signedAndEnveloped
:
84 case NID_pkcs7_digest
:
85 case NID_pkcs7_encrypted
:
96 static ASN1_OCTET_STRING
*
97 PKCS7_get_octet_string(PKCS7
*p7
)
99 if (PKCS7_type_is_data(p7
))
101 if (PKCS7_type_is_other(p7
) && p7
->d
.other
&&
102 (p7
->d
.other
->type
== V_ASN1_OCTET_STRING
))
103 return p7
->d
.other
->value
.octet_string
;
108 PKCS7_bio_add_digest(BIO
**pbio
, X509_ALGOR
*alg
)
112 if ((btmp
= BIO_new(BIO_f_md())) == NULL
) {
113 PKCS7error(ERR_R_BIO_LIB
);
117 md
= EVP_get_digestbyobj(alg
->algorithm
);
119 PKCS7error(PKCS7_R_UNKNOWN_DIGEST_TYPE
);
123 BIO_set_md(btmp
, md
);
126 else if (!BIO_push(*pbio
, btmp
)) {
127 PKCS7error(ERR_R_BIO_LIB
);
141 pkcs7_encode_rinfo(PKCS7_RECIP_INFO
*ri
, unsigned char *key
, int keylen
)
143 EVP_PKEY_CTX
*pctx
= NULL
;
144 EVP_PKEY
*pkey
= NULL
;
145 unsigned char *ek
= NULL
;
149 pkey
= X509_get_pubkey(ri
->cert
);
153 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
157 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
160 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_ENCRYPT
,
161 EVP_PKEY_CTRL_PKCS7_ENCRYPT
, 0, ri
) <= 0) {
162 PKCS7error(PKCS7_R_CTRL_ERROR
);
166 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, key
, keylen
) <= 0)
172 PKCS7error(ERR_R_MALLOC_FAILURE
);
176 if (EVP_PKEY_encrypt(pctx
, ek
, &eklen
, key
, keylen
) <= 0)
179 ASN1_STRING_set0(ri
->enc_key
, ek
, eklen
);
186 EVP_PKEY_CTX_free(pctx
);
193 pkcs7_decrypt_rinfo(unsigned char **pek
, int *peklen
, PKCS7_RECIP_INFO
*ri
,
196 EVP_PKEY_CTX
*pctx
= NULL
;
197 unsigned char *ek
= NULL
;
202 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
206 if (EVP_PKEY_decrypt_init(pctx
) <= 0)
209 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_DECRYPT
,
210 EVP_PKEY_CTRL_PKCS7_DECRYPT
, 0, ri
) <= 0) {
211 PKCS7error(PKCS7_R_CTRL_ERROR
);
215 if (EVP_PKEY_decrypt(pctx
, NULL
, &eklen
,
216 ri
->enc_key
->data
, ri
->enc_key
->length
) <= 0)
221 PKCS7error(ERR_R_MALLOC_FAILURE
);
225 if (EVP_PKEY_decrypt(pctx
, ek
, &eklen
,
226 ri
->enc_key
->data
, ri
->enc_key
->length
) <= 0) {
228 PKCS7error(ERR_R_EVP_LIB
);
235 explicit_bzero(*pek
, *peklen
);
243 EVP_PKEY_CTX_free(pctx
);
251 PKCS7_dataInit(PKCS7
*p7
, BIO
*bio
)
254 BIO
*out
= NULL
, *btmp
= NULL
;
255 X509_ALGOR
*xa
= NULL
;
256 const EVP_CIPHER
*evp_cipher
= NULL
;
257 STACK_OF(X509_ALGOR
) *md_sk
= NULL
;
258 STACK_OF(PKCS7_RECIP_INFO
) *rsk
= NULL
;
259 X509_ALGOR
*xalg
= NULL
;
260 PKCS7_RECIP_INFO
*ri
= NULL
;
261 ASN1_OCTET_STRING
*os
= NULL
;
264 PKCS7error(PKCS7_R_INVALID_NULL_POINTER
);
269 * The content field in the PKCS7 ContentInfo is optional,
270 * but that really only applies to inner content (precisely,
271 * detached signatures).
273 * When reading content, missing outer content is therefore
274 * treated as an error.
276 * When creating content, PKCS7_content_new() must be called
277 * before calling this method, so a NULL p7->d is always
280 if (p7
->d
.ptr
== NULL
) {
281 PKCS7error(PKCS7_R_NO_CONTENT
);
285 i
= OBJ_obj2nid(p7
->type
);
286 p7
->state
= PKCS7_S_HEADER
;
289 case NID_pkcs7_signed
:
290 md_sk
= p7
->d
.sign
->md_algs
;
291 os
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
293 case NID_pkcs7_signedAndEnveloped
:
294 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
295 md_sk
= p7
->d
.signed_and_enveloped
->md_algs
;
296 xalg
= p7
->d
.signed_and_enveloped
->enc_data
->algorithm
;
297 evp_cipher
= p7
->d
.signed_and_enveloped
->enc_data
->cipher
;
298 if (evp_cipher
== NULL
) {
299 PKCS7error(PKCS7_R_CIPHER_NOT_INITIALIZED
);
303 case NID_pkcs7_enveloped
:
304 rsk
= p7
->d
.enveloped
->recipientinfo
;
305 xalg
= p7
->d
.enveloped
->enc_data
->algorithm
;
306 evp_cipher
= p7
->d
.enveloped
->enc_data
->cipher
;
307 if (evp_cipher
== NULL
) {
308 PKCS7error(PKCS7_R_CIPHER_NOT_INITIALIZED
);
312 case NID_pkcs7_digest
:
313 xa
= p7
->d
.digest
->md
;
314 os
= PKCS7_get_octet_string(p7
->d
.digest
->contents
);
319 PKCS7error(PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
323 for (i
= 0; i
< sk_X509_ALGOR_num(md_sk
); i
++)
324 if (!PKCS7_bio_add_digest(&out
, sk_X509_ALGOR_value(md_sk
, i
)))
327 if (xa
&& !PKCS7_bio_add_digest(&out
, xa
))
330 if (evp_cipher
!= NULL
) {
331 unsigned char key
[EVP_MAX_KEY_LENGTH
];
332 unsigned char iv
[EVP_MAX_IV_LENGTH
];
336 if ((btmp
= BIO_new(BIO_f_cipher())) == NULL
) {
337 PKCS7error(ERR_R_BIO_LIB
);
340 BIO_get_cipher_ctx(btmp
, &ctx
);
341 keylen
= EVP_CIPHER_key_length(evp_cipher
);
342 ivlen
= EVP_CIPHER_iv_length(evp_cipher
);
343 xalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_type(evp_cipher
));
345 arc4random_buf(iv
, ivlen
);
346 if (EVP_CipherInit_ex(ctx
, evp_cipher
, NULL
, NULL
,
349 if (EVP_CIPHER_CTX_rand_key(ctx
, key
) <= 0)
351 if (EVP_CipherInit_ex(ctx
, NULL
, NULL
, key
, iv
, 1) <= 0)
355 if (xalg
->parameter
== NULL
) {
356 xalg
->parameter
= ASN1_TYPE_new();
357 if (xalg
->parameter
== NULL
)
360 if (EVP_CIPHER_param_to_asn1(ctx
, xalg
->parameter
) < 0)
364 /* Lets do the pub key stuff :-) */
365 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
366 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
367 if (pkcs7_encode_rinfo(ri
, key
, keylen
) <= 0)
370 explicit_bzero(key
, keylen
);
380 if (PKCS7_is_detached(p7
))
381 bio
= BIO_new(BIO_s_null());
382 else if (os
&& os
->length
> 0)
383 bio
= BIO_new_mem_buf(os
->data
, os
->length
);
385 bio
= BIO_new(BIO_s_mem());
388 BIO_set_mem_eof_return(bio
, 0);
408 pkcs7_cmp_ri(PKCS7_RECIP_INFO
*ri
, X509
*pcert
)
412 ret
= X509_NAME_cmp(ri
->issuer_and_serial
->issuer
,
413 pcert
->cert_info
->issuer
);
416 return ASN1_STRING_cmp(pcert
->cert_info
->serialNumber
,
417 ri
->issuer_and_serial
->serial
);
422 PKCS7_dataDecode(PKCS7
*p7
, EVP_PKEY
*pkey
, BIO
*in_bio
, X509
*pcert
)
425 BIO
*out
= NULL
, *btmp
= NULL
, *etmp
= NULL
, *bio
= NULL
;
427 ASN1_OCTET_STRING
*data_body
= NULL
;
428 const EVP_MD
*evp_md
;
429 const EVP_CIPHER
*evp_cipher
= NULL
;
430 EVP_CIPHER_CTX
*evp_ctx
= NULL
;
431 X509_ALGOR
*enc_alg
= NULL
;
432 STACK_OF(X509_ALGOR
) *md_sk
= NULL
;
433 STACK_OF(PKCS7_RECIP_INFO
) *rsk
= NULL
;
434 PKCS7_RECIP_INFO
*ri
= NULL
;
435 unsigned char *ek
= NULL
, *tkey
= NULL
;
436 int eklen
= 0, tkeylen
= 0;
439 PKCS7error(PKCS7_R_INVALID_NULL_POINTER
);
443 if (p7
->d
.ptr
== NULL
) {
444 PKCS7error(PKCS7_R_NO_CONTENT
);
448 i
= OBJ_obj2nid(p7
->type
);
449 p7
->state
= PKCS7_S_HEADER
;
452 case NID_pkcs7_signed
:
453 data_body
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
454 md_sk
= p7
->d
.sign
->md_algs
;
456 case NID_pkcs7_signedAndEnveloped
:
457 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
458 md_sk
= p7
->d
.signed_and_enveloped
->md_algs
;
459 data_body
= p7
->d
.signed_and_enveloped
->enc_data
->enc_data
;
460 enc_alg
= p7
->d
.signed_and_enveloped
->enc_data
->algorithm
;
461 evp_cipher
= EVP_get_cipherbyobj(enc_alg
->algorithm
);
462 if (evp_cipher
== NULL
) {
463 PKCS7error(PKCS7_R_UNSUPPORTED_CIPHER_TYPE
);
467 case NID_pkcs7_enveloped
:
468 rsk
= p7
->d
.enveloped
->recipientinfo
;
469 enc_alg
= p7
->d
.enveloped
->enc_data
->algorithm
;
470 data_body
= p7
->d
.enveloped
->enc_data
->enc_data
;
471 evp_cipher
= EVP_get_cipherbyobj(enc_alg
->algorithm
);
472 if (evp_cipher
== NULL
) {
473 PKCS7error(PKCS7_R_UNSUPPORTED_CIPHER_TYPE
);
478 PKCS7error(PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
482 /* We will be checking the signature */
484 for (i
= 0; i
< sk_X509_ALGOR_num(md_sk
); i
++) {
485 xa
= sk_X509_ALGOR_value(md_sk
, i
);
486 if ((btmp
= BIO_new(BIO_f_md())) == NULL
) {
487 PKCS7error(ERR_R_BIO_LIB
);
491 j
= OBJ_obj2nid(xa
->algorithm
);
492 evp_md
= EVP_get_digestbynid(j
);
493 if (evp_md
== NULL
) {
494 PKCS7error(PKCS7_R_UNKNOWN_DIGEST_TYPE
);
498 BIO_set_md(btmp
, evp_md
);
507 if (evp_cipher
!= NULL
) {
508 if ((etmp
= BIO_new(BIO_f_cipher())) == NULL
) {
509 PKCS7error(ERR_R_BIO_LIB
);
513 /* It was encrypted, we need to decrypt the secret key
514 * with the private key */
516 /* Find the recipientInfo which matches the passed certificate
520 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
521 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
522 if (!pkcs7_cmp_ri(ri
, pcert
))
527 PKCS7error(PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE
);
532 /* If we haven't got a certificate try each ri in turn */
534 /* Always attempt to decrypt all rinfo even
535 * after sucess as a defence against MMA timing
538 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
539 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
541 if (pkcs7_decrypt_rinfo(&ek
, &eklen
,
547 /* Only exit on fatal errors, not decrypt failure */
548 if (pkcs7_decrypt_rinfo(&ek
, &eklen
, ri
, pkey
) < 0)
554 BIO_get_cipher_ctx(etmp
, &evp_ctx
);
555 if (EVP_CipherInit_ex(evp_ctx
, evp_cipher
, NULL
, NULL
,
558 if (EVP_CIPHER_asn1_to_param(evp_ctx
, enc_alg
->parameter
) < 0)
560 /* Generate random key as MMA defence */
561 tkeylen
= EVP_CIPHER_CTX_key_length(evp_ctx
);
562 tkey
= malloc(tkeylen
);
565 if (EVP_CIPHER_CTX_rand_key(evp_ctx
, tkey
) <= 0)
573 if (eklen
!= EVP_CIPHER_CTX_key_length(evp_ctx
)) {
574 /* Some S/MIME clients don't use the same key
575 * and effective key length. The key length is
576 * determined by the size of the decrypted RSA key.
578 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx
, eklen
)) {
579 /* Use random key as MMA defence */
580 explicit_bzero(ek
, eklen
);
587 /* Clear errors so we don't leak information useful in MMA */
589 if (EVP_CipherInit_ex(evp_ctx
, NULL
, NULL
, ek
, NULL
, 0) <= 0)
593 explicit_bzero(ek
, eklen
);
598 explicit_bzero(tkey
, tkeylen
);
610 if (PKCS7_is_detached(p7
) || (in_bio
!= NULL
)) {
613 if (data_body
!= NULL
&& data_body
->length
> 0)
614 bio
= BIO_new_mem_buf(data_body
->data
, data_body
->length
);
616 bio
= BIO_new(BIO_s_mem());
617 BIO_set_mem_eof_return(bio
, 0);
627 explicit_bzero(ek
, eklen
);
631 explicit_bzero(tkey
, tkeylen
);
646 PKCS7_find_digest(EVP_MD_CTX
**pmd
, BIO
*bio
, int nid
)
649 bio
= BIO_find_type(bio
, BIO_TYPE_MD
);
651 PKCS7error(PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
654 BIO_get_md_ctx(bio
, pmd
);
656 PKCS7error(ERR_R_INTERNAL_ERROR
);
659 if (EVP_MD_CTX_type(*pmd
) == nid
)
667 do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO
*si
, EVP_MD_CTX
*mctx
)
669 unsigned char md_data
[EVP_MAX_MD_SIZE
];
672 /* Add signing time if not already present */
673 if (!PKCS7_get_signed_attribute(si
, NID_pkcs9_signingTime
)) {
674 if (!PKCS7_add0_attrib_signing_time(si
, NULL
)) {
675 PKCS7error(ERR_R_MALLOC_FAILURE
);
681 if (!EVP_DigestFinal_ex(mctx
, md_data
, &md_len
)) {
682 PKCS7error(ERR_R_EVP_LIB
);
685 if (!PKCS7_add1_attrib_digest(si
, md_data
, md_len
)) {
686 PKCS7error(ERR_R_MALLOC_FAILURE
);
690 /* Now sign the attributes */
691 if (!PKCS7_SIGNER_INFO_sign(si
))
699 PKCS7_dataFinal(PKCS7
*p7
, BIO
*bio
)
704 PKCS7_SIGNER_INFO
*si
;
705 EVP_MD_CTX
*mdc
, ctx_tmp
;
706 STACK_OF(X509_ATTRIBUTE
) *sk
;
707 STACK_OF(PKCS7_SIGNER_INFO
) *si_sk
= NULL
;
708 ASN1_OCTET_STRING
*os
= NULL
;
711 PKCS7error(PKCS7_R_INVALID_NULL_POINTER
);
715 if (p7
->d
.ptr
== NULL
) {
716 PKCS7error(PKCS7_R_NO_CONTENT
);
720 EVP_MD_CTX_init(&ctx_tmp
);
721 i
= OBJ_obj2nid(p7
->type
);
722 p7
->state
= PKCS7_S_HEADER
;
728 case NID_pkcs7_signedAndEnveloped
:
730 si_sk
= p7
->d
.signed_and_enveloped
->signer_info
;
731 os
= p7
->d
.signed_and_enveloped
->enc_data
->enc_data
;
733 os
= ASN1_OCTET_STRING_new();
735 PKCS7error(ERR_R_MALLOC_FAILURE
);
738 p7
->d
.signed_and_enveloped
->enc_data
->enc_data
= os
;
741 case NID_pkcs7_enveloped
:
743 os
= p7
->d
.enveloped
->enc_data
->enc_data
;
745 os
= ASN1_OCTET_STRING_new();
747 PKCS7error(ERR_R_MALLOC_FAILURE
);
750 p7
->d
.enveloped
->enc_data
->enc_data
= os
;
753 case NID_pkcs7_signed
:
754 si_sk
= p7
->d
.sign
->signer_info
;
755 os
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
756 if (!PKCS7_is_detached(p7
) && os
== NULL
) {
757 PKCS7error(PKCS7_R_DECODE_ERROR
);
760 /* If detached data then the content is excluded */
761 if (PKCS7_type_is_data(p7
->d
.sign
->contents
) && p7
->detached
) {
762 ASN1_OCTET_STRING_free(os
);
764 p7
->d
.sign
->contents
->d
.data
= NULL
;
768 case NID_pkcs7_digest
:
769 os
= PKCS7_get_octet_string(p7
->d
.digest
->contents
);
771 PKCS7error(PKCS7_R_DECODE_ERROR
);
774 /* If detached data then the content is excluded */
775 if (PKCS7_type_is_data(p7
->d
.digest
->contents
) &&
777 ASN1_OCTET_STRING_free(os
);
779 p7
->d
.digest
->contents
->d
.data
= NULL
;
784 PKCS7error(PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
789 for (i
= 0; i
< sk_PKCS7_SIGNER_INFO_num(si_sk
); i
++) {
790 si
= sk_PKCS7_SIGNER_INFO_value(si_sk
, i
);
791 if (si
->pkey
== NULL
)
794 j
= OBJ_obj2nid(si
->digest_alg
->algorithm
);
796 if ((btmp
= PKCS7_find_digest(&mdc
, bio
, j
)) == NULL
)
799 /* We now have the EVP_MD_CTX, lets do the
801 if (!EVP_MD_CTX_copy_ex(&ctx_tmp
, mdc
))
806 /* If there are attributes, we add the digest
807 * attribute and only sign the attributes */
808 if (sk_X509_ATTRIBUTE_num(sk
) > 0) {
809 if (!do_pkcs7_signed_attrib(si
, &ctx_tmp
))
812 unsigned char *abuf
= NULL
;
813 unsigned int abuflen
;
814 abuflen
= EVP_PKEY_size(si
->pkey
);
815 abuf
= malloc(abuflen
);
819 if (!EVP_SignFinal(&ctx_tmp
, abuf
, &abuflen
,
821 PKCS7error(ERR_R_EVP_LIB
);
824 ASN1_STRING_set0(si
->enc_digest
, abuf
, abuflen
);
827 } else if (i
== NID_pkcs7_digest
) {
828 unsigned char md_data
[EVP_MAX_MD_SIZE
];
831 if (!PKCS7_find_digest(&mdc
, bio
,
832 OBJ_obj2nid(p7
->d
.digest
->md
->algorithm
)))
834 if (!EVP_DigestFinal_ex(mdc
, md_data
, &md_len
))
836 if (ASN1_STRING_set(p7
->d
.digest
->digest
, md_data
,
841 if (!PKCS7_is_detached(p7
)) {
843 * NOTE: only reach os == NULL here because detached
844 * digested data support is broken?
848 if (!(os
->flags
& ASN1_STRING_FLAG_NDEF
)) {
852 btmp
= BIO_find_type(bio
, BIO_TYPE_MEM
);
854 PKCS7error(PKCS7_R_UNABLE_TO_FIND_MEM_BIO
);
857 contlen
= BIO_get_mem_data(btmp
, &cont
);
859 * Mark the BIO read only then we can use its copy
860 * of the data instead of making an extra copy.
862 BIO_set_flags(btmp
, BIO_FLAGS_MEM_RDONLY
);
863 BIO_set_mem_eof_return(btmp
, 0);
864 ASN1_STRING_set0(os
, (unsigned char *)cont
, contlen
);
869 EVP_MD_CTX_cleanup(&ctx_tmp
);
874 PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO
*si
)
878 unsigned char *abuf
= NULL
;
881 const EVP_MD
*md
= NULL
;
883 md
= EVP_get_digestbyobj(si
->digest_alg
->algorithm
);
887 EVP_MD_CTX_init(&mctx
);
888 if (EVP_DigestSignInit(&mctx
, &pctx
, md
, NULL
, si
->pkey
) <= 0)
891 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
892 EVP_PKEY_CTRL_PKCS7_SIGN
, 0, si
) <= 0) {
893 PKCS7error(PKCS7_R_CTRL_ERROR
);
897 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->auth_attr
, &abuf
,
898 &PKCS7_ATTR_SIGN_it
);
901 if (EVP_DigestSignUpdate(&mctx
, abuf
, alen
) <= 0)
905 if (EVP_DigestSignFinal(&mctx
, NULL
, &siglen
) <= 0)
907 abuf
= malloc(siglen
);
910 if (EVP_DigestSignFinal(&mctx
, abuf
, &siglen
) <= 0)
913 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
914 EVP_PKEY_CTRL_PKCS7_SIGN
, 1, si
) <= 0) {
915 PKCS7error(PKCS7_R_CTRL_ERROR
);
919 EVP_MD_CTX_cleanup(&mctx
);
921 ASN1_STRING_set0(si
->enc_digest
, abuf
, siglen
);
927 EVP_MD_CTX_cleanup(&mctx
);
932 PKCS7_dataVerify(X509_STORE
*cert_store
, X509_STORE_CTX
*ctx
, BIO
*bio
,
933 PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
)
935 PKCS7_ISSUER_AND_SERIAL
*ias
;
937 STACK_OF(X509
) *cert
;
941 PKCS7error(PKCS7_R_INVALID_NULL_POINTER
);
945 if (p7
->d
.ptr
== NULL
) {
946 PKCS7error(PKCS7_R_NO_CONTENT
);
950 if (PKCS7_type_is_signed(p7
)) {
951 cert
= p7
->d
.sign
->cert
;
952 } else if (PKCS7_type_is_signedAndEnveloped(p7
)) {
953 cert
= p7
->d
.signed_and_enveloped
->cert
;
955 PKCS7error(PKCS7_R_WRONG_PKCS7_TYPE
);
959 ias
= si
->issuer_and_serial
;
961 x509
= X509_find_by_issuer_and_serial(cert
, ias
->issuer
, ias
->serial
);
963 /* were we able to find the cert in passed to us */
965 PKCS7error(PKCS7_R_UNABLE_TO_FIND_CERTIFICATE
);
970 if (!X509_STORE_CTX_init(ctx
, cert_store
, x509
, cert
)) {
971 PKCS7error(ERR_R_X509_LIB
);
974 if (X509_STORE_CTX_set_purpose(ctx
, X509_PURPOSE_SMIME_SIGN
) == 0) {
975 X509_STORE_CTX_cleanup(ctx
);
978 i
= X509_verify_cert(ctx
);
980 PKCS7error(ERR_R_X509_LIB
);
981 X509_STORE_CTX_cleanup(ctx
);
984 X509_STORE_CTX_cleanup(ctx
);
986 return PKCS7_signatureVerify(bio
, p7
, si
, x509
);
993 PKCS7_signatureVerify(BIO
*bio
, PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
, X509
*x509
)
995 ASN1_OCTET_STRING
*os
;
996 EVP_MD_CTX mdc_tmp
, *mdc
;
999 STACK_OF(X509_ATTRIBUTE
) *sk
;
1003 EVP_MD_CTX_init(&mdc_tmp
);
1005 if (!PKCS7_type_is_signed(p7
) &&
1006 !PKCS7_type_is_signedAndEnveloped(p7
)) {
1007 PKCS7error(PKCS7_R_WRONG_PKCS7_TYPE
);
1011 md_type
= OBJ_obj2nid(si
->digest_alg
->algorithm
);
1015 if ((btmp
== NULL
) ||
1016 ((btmp
= BIO_find_type(btmp
, BIO_TYPE_MD
)) == NULL
)) {
1017 PKCS7error(PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
1020 BIO_get_md_ctx(btmp
, &mdc
);
1022 PKCS7error(ERR_R_INTERNAL_ERROR
);
1025 if (EVP_MD_CTX_type(mdc
) == md_type
)
1027 /* Workaround for some broken clients that put the signature
1028 * OID instead of the digest OID in digest_alg->algorithm
1030 if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc
)) == md_type
)
1032 btmp
= BIO_next(btmp
);
1035 /* mdc is the digest ctx that we want, unless there are attributes,
1036 * in which case the digest is the signed attributes */
1037 if (!EVP_MD_CTX_copy_ex(&mdc_tmp
, mdc
))
1041 if ((sk
!= NULL
) && (sk_X509_ATTRIBUTE_num(sk
) != 0)) {
1042 unsigned char md_dat
[EVP_MAX_MD_SIZE
], *abuf
= NULL
;
1043 unsigned int md_len
;
1045 ASN1_OCTET_STRING
*message_digest
;
1047 if (!EVP_DigestFinal_ex(&mdc_tmp
, md_dat
, &md_len
))
1049 message_digest
= PKCS7_digest_from_attributes(sk
);
1050 if (!message_digest
) {
1051 PKCS7error(PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
1054 if ((message_digest
->length
!= (int)md_len
) ||
1055 (memcmp(message_digest
->data
, md_dat
, md_len
))) {
1056 PKCS7error(PKCS7_R_DIGEST_FAILURE
);
1061 if (!EVP_VerifyInit_ex(&mdc_tmp
, EVP_get_digestbynid(md_type
),
1065 alen
= ASN1_item_i2d((ASN1_VALUE
*)sk
, &abuf
,
1066 &PKCS7_ATTR_VERIFY_it
);
1068 PKCS7error(ERR_R_ASN1_LIB
);
1072 if (!EVP_VerifyUpdate(&mdc_tmp
, abuf
, alen
))
1078 os
= si
->enc_digest
;
1079 pkey
= X509_get_pubkey(x509
);
1085 i
= EVP_VerifyFinal(&mdc_tmp
, os
->data
, os
->length
, pkey
);
1086 EVP_PKEY_free(pkey
);
1088 PKCS7error(PKCS7_R_SIGNATURE_FAILURE
);
1094 EVP_MD_CTX_cleanup(&mdc_tmp
);
1098 PKCS7_ISSUER_AND_SERIAL
*
1099 PKCS7_get_issuer_and_serial(PKCS7
*p7
, int idx
)
1101 STACK_OF(PKCS7_RECIP_INFO
) *rsk
;
1102 PKCS7_RECIP_INFO
*ri
;
1105 i
= OBJ_obj2nid(p7
->type
);
1106 if (i
!= NID_pkcs7_signedAndEnveloped
)
1108 if (p7
->d
.signed_and_enveloped
== NULL
)
1110 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
1113 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, 0);
1114 if (sk_PKCS7_RECIP_INFO_num(rsk
) <= idx
)
1116 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, idx
);
1117 return (ri
->issuer_and_serial
);
1121 PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO
*si
, int nid
)
1123 return (get_attribute(si
->auth_attr
, nid
));
1127 PKCS7_get_attribute(PKCS7_SIGNER_INFO
*si
, int nid
)
1129 return (get_attribute(si
->unauth_attr
, nid
));
1133 get_attribute(STACK_OF(X509_ATTRIBUTE
) *sk
, int nid
)
1139 o
= OBJ_nid2obj(nid
);
1142 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1143 xa
= sk_X509_ATTRIBUTE_value(sk
, i
);
1144 if (OBJ_cmp(xa
->object
, o
) == 0) {
1145 if (!xa
->single
&& sk_ASN1_TYPE_num(xa
->value
.set
))
1146 return (sk_ASN1_TYPE_value(xa
->value
.set
, 0));
1155 PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE
) *sk
)
1159 if (!(astype
= get_attribute(sk
, NID_pkcs9_messageDigest
)))
1161 if (astype
->type
!= V_ASN1_OCTET_STRING
)
1163 return astype
->value
.octet_string
;
1167 PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO
*p7si
,
1168 STACK_OF(X509_ATTRIBUTE
) *sk
)
1172 if (p7si
->auth_attr
!= NULL
)
1173 sk_X509_ATTRIBUTE_pop_free(p7si
->auth_attr
,
1174 X509_ATTRIBUTE_free
);
1175 p7si
->auth_attr
= sk_X509_ATTRIBUTE_dup(sk
);
1176 if (p7si
->auth_attr
== NULL
)
1178 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1179 if ((sk_X509_ATTRIBUTE_set(p7si
->auth_attr
, i
,
1180 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk
, i
))))
1188 PKCS7_set_attributes(PKCS7_SIGNER_INFO
*p7si
, STACK_OF(X509_ATTRIBUTE
) *sk
)
1192 if (p7si
->unauth_attr
!= NULL
)
1193 sk_X509_ATTRIBUTE_pop_free(p7si
->unauth_attr
,
1194 X509_ATTRIBUTE_free
);
1195 p7si
->unauth_attr
= sk_X509_ATTRIBUTE_dup(sk
);
1196 if (p7si
->unauth_attr
== NULL
)
1198 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1199 if ((sk_X509_ATTRIBUTE_set(p7si
->unauth_attr
, i
,
1200 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value(sk
, i
))))
1208 PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
,
1211 return (add_attribute(&(p7si
->auth_attr
), nid
, atrtype
, value
));
1215 PKCS7_add_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
, void *value
)
1217 return (add_attribute(&(p7si
->unauth_attr
), nid
, atrtype
, value
));
1221 add_attribute(STACK_OF(X509_ATTRIBUTE
) **sk
, int nid
, int atrtype
, void *value
)
1223 X509_ATTRIBUTE
*attr
= NULL
;
1226 *sk
= sk_X509_ATTRIBUTE_new_null();
1230 if (!(attr
= X509_ATTRIBUTE_create(nid
, atrtype
, value
)))
1232 if (!sk_X509_ATTRIBUTE_push(*sk
, attr
)) {
1233 X509_ATTRIBUTE_free(attr
);
1239 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(*sk
); i
++) {
1240 attr
= sk_X509_ATTRIBUTE_value(*sk
, i
);
1241 if (OBJ_obj2nid(attr
->object
) == nid
) {
1242 X509_ATTRIBUTE_free(attr
);
1243 attr
= X509_ATTRIBUTE_create(nid
, atrtype
,
1247 if (!sk_X509_ATTRIBUTE_set(*sk
, i
, attr
)) {
1248 X509_ATTRIBUTE_free(attr
);