2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/rand.h>
13 #include <openssl/objects.h>
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/err.h>
17 #include "pk7_local.h"
19 DEFINE_STACK_OF(X509_ALGOR
)
20 DEFINE_STACK_OF(PKCS7_RECIP_INFO
)
21 DEFINE_STACK_OF(PKCS7_SIGNER_INFO
)
23 static int add_attribute(STACK_OF(X509_ATTRIBUTE
) **sk
, int nid
, int atrtype
,
25 static ASN1_TYPE
*get_attribute(STACK_OF(X509_ATTRIBUTE
) *sk
, int nid
);
27 static int PKCS7_type_is_other(PKCS7
*p7
)
31 int nid
= OBJ_obj2nid(p7
->type
);
35 case NID_pkcs7_signed
:
36 case NID_pkcs7_enveloped
:
37 case NID_pkcs7_signedAndEnveloped
:
38 case NID_pkcs7_digest
:
39 case NID_pkcs7_encrypted
:
50 static ASN1_OCTET_STRING
*PKCS7_get_octet_string(PKCS7
*p7
)
52 if (PKCS7_type_is_data(p7
))
54 if (PKCS7_type_is_other(p7
) && p7
->d
.other
55 && (p7
->d
.other
->type
== V_ASN1_OCTET_STRING
))
56 return p7
->d
.other
->value
.octet_string
;
60 static int pkcs7_bio_add_digest(BIO
**pbio
, X509_ALGOR
*alg
,
65 EVP_MD
*fetched
= NULL
;
68 if ((btmp
= BIO_new(BIO_f_md())) == NULL
) {
69 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST
, ERR_R_BIO_LIB
);
73 name
= OBJ_nid2sn(OBJ_obj2nid(alg
->algorithm
));
76 fetched
= EVP_MD_fetch(ctx
->libctx
, name
, ctx
->propq
);
80 md
= EVP_get_digestbyname(name
);
83 (void)ERR_clear_last_mark();
84 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST
, PKCS7_R_UNKNOWN_DIGEST_TYPE
);
87 (void)ERR_pop_to_mark();
93 else if (!BIO_push(*pbio
, btmp
)) {
94 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST
, ERR_R_BIO_LIB
);
106 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO
*ri
,
107 unsigned char *key
, int keylen
)
109 EVP_PKEY_CTX
*pctx
= NULL
;
110 EVP_PKEY
*pkey
= NULL
;
111 unsigned char *ek
= NULL
;
114 const PKCS7_CTX
*ctx
= ri
->ctx
;
116 pkey
= X509_get0_pubkey(ri
->cert
);
120 pctx
= EVP_PKEY_CTX_new_from_pkey(ctx
->libctx
, pkey
, ctx
->propq
);
124 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
127 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_ENCRYPT
,
128 EVP_PKEY_CTRL_PKCS7_ENCRYPT
, 0, ri
) <= 0) {
129 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO
, PKCS7_R_CTRL_ERROR
);
133 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, key
, keylen
) <= 0)
136 ek
= OPENSSL_malloc(eklen
);
139 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO
, ERR_R_MALLOC_FAILURE
);
143 if (EVP_PKEY_encrypt(pctx
, ek
, &eklen
, key
, keylen
) <= 0)
146 ASN1_STRING_set0(ri
->enc_key
, ek
, eklen
);
152 EVP_PKEY_CTX_free(pctx
);
158 static int pkcs7_decrypt_rinfo(unsigned char **pek
, int *peklen
,
159 PKCS7_RECIP_INFO
*ri
, EVP_PKEY
*pkey
,
162 EVP_PKEY_CTX
*pctx
= NULL
;
163 unsigned char *ek
= NULL
;
166 const PKCS7_CTX
*ctx
= ri
->ctx
;
168 pctx
= EVP_PKEY_CTX_new_from_pkey(ctx
->libctx
, pkey
, ctx
->propq
);
172 if (EVP_PKEY_decrypt_init(pctx
) <= 0)
175 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_DECRYPT
,
176 EVP_PKEY_CTRL_PKCS7_DECRYPT
, 0, ri
) <= 0) {
177 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO
, PKCS7_R_CTRL_ERROR
);
181 if (EVP_PKEY_decrypt(pctx
, NULL
, &eklen
,
182 ri
->enc_key
->data
, ri
->enc_key
->length
) <= 0)
185 ek
= OPENSSL_malloc(eklen
);
188 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO
, ERR_R_MALLOC_FAILURE
);
192 if (EVP_PKEY_decrypt(pctx
, ek
, &eklen
,
193 ri
->enc_key
->data
, ri
->enc_key
->length
) <= 0
195 || (fixlen
!= 0 && eklen
!= fixlen
)) {
197 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO
, ERR_R_EVP_LIB
);
203 OPENSSL_clear_free(*pek
, *peklen
);
208 EVP_PKEY_CTX_free(pctx
);
215 BIO
*PKCS7_dataInit(PKCS7
*p7
, BIO
*bio
)
218 BIO
*out
= NULL
, *btmp
= NULL
;
219 X509_ALGOR
*xa
= NULL
;
220 EVP_CIPHER
*fetched_cipher
= NULL
;
221 const EVP_CIPHER
*cipher
;
222 const EVP_CIPHER
*evp_cipher
= NULL
;
223 STACK_OF(X509_ALGOR
) *md_sk
= NULL
;
224 STACK_OF(PKCS7_RECIP_INFO
) *rsk
= NULL
;
225 X509_ALGOR
*xalg
= NULL
;
226 PKCS7_RECIP_INFO
*ri
= NULL
;
227 ASN1_OCTET_STRING
*os
= NULL
;
228 const PKCS7_CTX
*p7_ctx
;
231 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_INVALID_NULL_POINTER
);
234 p7_ctx
= pkcs7_get0_ctx(p7
);
237 * The content field in the PKCS7 ContentInfo is optional, but that really
238 * only applies to inner content (precisely, detached signatures).
240 * When reading content, missing outer content is therefore treated as an
243 * When creating content, PKCS7_content_new() must be called before
244 * calling this method, so a NULL p7->d is always an error.
246 if (p7
->d
.ptr
== NULL
) {
247 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_NO_CONTENT
);
251 i
= OBJ_obj2nid(p7
->type
);
252 p7
->state
= PKCS7_S_HEADER
;
255 case NID_pkcs7_signed
:
256 md_sk
= p7
->d
.sign
->md_algs
;
257 os
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
259 case NID_pkcs7_signedAndEnveloped
:
260 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
261 md_sk
= p7
->d
.signed_and_enveloped
->md_algs
;
262 xalg
= p7
->d
.signed_and_enveloped
->enc_data
->algorithm
;
263 evp_cipher
= p7
->d
.signed_and_enveloped
->enc_data
->cipher
;
264 if (evp_cipher
== NULL
) {
265 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_CIPHER_NOT_INITIALIZED
);
269 case NID_pkcs7_enveloped
:
270 rsk
= p7
->d
.enveloped
->recipientinfo
;
271 xalg
= p7
->d
.enveloped
->enc_data
->algorithm
;
272 evp_cipher
= p7
->d
.enveloped
->enc_data
->cipher
;
273 if (evp_cipher
== NULL
) {
274 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_CIPHER_NOT_INITIALIZED
);
278 case NID_pkcs7_digest
:
279 xa
= p7
->d
.digest
->md
;
280 os
= PKCS7_get_octet_string(p7
->d
.digest
->contents
);
285 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
289 for (i
= 0; i
< sk_X509_ALGOR_num(md_sk
); i
++)
290 if (!pkcs7_bio_add_digest(&out
, sk_X509_ALGOR_value(md_sk
, i
), p7_ctx
))
293 if (xa
&& !pkcs7_bio_add_digest(&out
, xa
, p7_ctx
))
296 if (evp_cipher
!= NULL
) {
297 unsigned char key
[EVP_MAX_KEY_LENGTH
];
298 unsigned char iv
[EVP_MAX_IV_LENGTH
];
302 if ((btmp
= BIO_new(BIO_f_cipher())) == NULL
) {
303 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, ERR_R_BIO_LIB
);
306 BIO_get_cipher_ctx(btmp
, &ctx
);
307 keylen
= EVP_CIPHER_key_length(evp_cipher
);
308 ivlen
= EVP_CIPHER_iv_length(evp_cipher
);
309 xalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_type(evp_cipher
));
311 if (RAND_bytes_ex(p7_ctx
->libctx
, iv
, ivlen
) <= 0)
314 (void)ERR_set_mark();
315 fetched_cipher
= EVP_CIPHER_fetch(p7_ctx
->libctx
,
316 EVP_CIPHER_name(evp_cipher
),
318 (void)ERR_pop_to_mark();
319 if (fetched_cipher
!= NULL
)
320 cipher
= fetched_cipher
;
324 if (EVP_CipherInit_ex(ctx
, cipher
, NULL
, NULL
, NULL
, 1) <= 0)
327 EVP_CIPHER_free(fetched_cipher
);
328 fetched_cipher
= NULL
;
330 if (EVP_CIPHER_CTX_rand_key(ctx
, key
) <= 0)
332 if (EVP_CipherInit_ex(ctx
, NULL
, NULL
, key
, iv
, 1) <= 0)
336 if (xalg
->parameter
== NULL
) {
337 xalg
->parameter
= ASN1_TYPE_new();
338 if (xalg
->parameter
== NULL
)
341 if (EVP_CIPHER_param_to_asn1(ctx
, xalg
->parameter
) < 0)
345 /* Lets do the pub key stuff :-) */
346 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
347 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
348 if (pkcs7_encode_rinfo(ri
, key
, keylen
) <= 0)
351 OPENSSL_cleanse(key
, keylen
);
361 if (PKCS7_is_detached(p7
)) {
362 bio
= BIO_new(BIO_s_null());
363 } else if (os
&& os
->length
> 0) {
364 bio
= BIO_new_mem_buf(os
->data
, os
->length
);
366 bio
= BIO_new(BIO_s_mem());
369 BIO_set_mem_eof_return(bio
, 0);
381 EVP_CIPHER_free(fetched_cipher
);
387 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO
*ri
, X509
*pcert
)
390 ret
= X509_NAME_cmp(ri
->issuer_and_serial
->issuer
,
391 X509_get_issuer_name(pcert
));
394 return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert
),
395 ri
->issuer_and_serial
->serial
);
399 BIO
*PKCS7_dataDecode(PKCS7
*p7
, EVP_PKEY
*pkey
, BIO
*in_bio
, X509
*pcert
)
402 BIO
*out
= NULL
, *btmp
= NULL
, *etmp
= NULL
, *bio
= NULL
;
404 ASN1_OCTET_STRING
*data_body
= NULL
;
405 EVP_MD
*evp_md
= NULL
;
407 EVP_CIPHER
*evp_cipher
= NULL
;
408 const EVP_CIPHER
*cipher
= NULL
;
409 EVP_CIPHER_CTX
*evp_ctx
= NULL
;
410 X509_ALGOR
*enc_alg
= NULL
;
411 STACK_OF(X509_ALGOR
) *md_sk
= NULL
;
412 STACK_OF(PKCS7_RECIP_INFO
) *rsk
= NULL
;
413 PKCS7_RECIP_INFO
*ri
= NULL
;
414 unsigned char *ek
= NULL
, *tkey
= NULL
;
415 int eklen
= 0, tkeylen
= 0;
417 const PKCS7_CTX
*p7_ctx
;
420 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_INVALID_NULL_POINTER
);
424 p7_ctx
= pkcs7_get0_ctx(p7
);
426 if (p7
->d
.ptr
== NULL
) {
427 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_NO_CONTENT
);
431 i
= OBJ_obj2nid(p7
->type
);
432 p7
->state
= PKCS7_S_HEADER
;
435 case NID_pkcs7_signed
:
437 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
438 * field and optional content.
439 * data_body is NULL if that structure has no (=detached) content
440 * or if the contentType is wrong (i.e., not "data").
442 data_body
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
443 if (!PKCS7_is_detached(p7
) && data_body
== NULL
) {
444 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
445 PKCS7_R_INVALID_SIGNED_DATA_TYPE
);
448 md_sk
= p7
->d
.sign
->md_algs
;
450 case NID_pkcs7_signedAndEnveloped
:
451 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
452 md_sk
= p7
->d
.signed_and_enveloped
->md_algs
;
453 /* data_body is NULL if the optional EncryptedContent is missing. */
454 data_body
= p7
->d
.signed_and_enveloped
->enc_data
->enc_data
;
455 enc_alg
= p7
->d
.signed_and_enveloped
->enc_data
->algorithm
;
457 name
= OBJ_nid2sn(OBJ_obj2nid(enc_alg
->algorithm
));
459 (void)ERR_set_mark();
460 evp_cipher
= EVP_CIPHER_fetch(p7_ctx
->libctx
, name
, p7_ctx
->propq
);
461 if (evp_cipher
!= NULL
)
464 cipher
= EVP_get_cipherbyname(name
);
466 if (cipher
== NULL
) {
467 (void)ERR_clear_last_mark();
468 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
469 PKCS7_R_UNSUPPORTED_CIPHER_TYPE
);
472 (void)ERR_pop_to_mark();
474 case NID_pkcs7_enveloped
:
475 rsk
= p7
->d
.enveloped
->recipientinfo
;
476 enc_alg
= p7
->d
.enveloped
->enc_data
->algorithm
;
477 /* data_body is NULL if the optional EncryptedContent is missing. */
478 data_body
= p7
->d
.enveloped
->enc_data
->enc_data
;
479 name
= OBJ_nid2sn(OBJ_obj2nid(enc_alg
->algorithm
));
481 (void)ERR_set_mark();
482 evp_cipher
= EVP_CIPHER_fetch(p7_ctx
->libctx
, name
, p7_ctx
->propq
);
483 if (evp_cipher
!= NULL
)
486 cipher
= EVP_get_cipherbyname(name
);
488 if (cipher
== NULL
) {
489 (void)ERR_clear_last_mark();
490 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
491 PKCS7_R_UNSUPPORTED_CIPHER_TYPE
);
494 (void)ERR_pop_to_mark();
497 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
501 /* Detached content must be supplied via in_bio instead. */
502 if (data_body
== NULL
&& in_bio
== NULL
) {
503 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_NO_CONTENT
);
507 /* We will be checking the signature */
509 for (i
= 0; i
< sk_X509_ALGOR_num(md_sk
); i
++) {
510 xa
= sk_X509_ALGOR_value(md_sk
, i
);
511 if ((btmp
= BIO_new(BIO_f_md())) == NULL
) {
512 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, ERR_R_BIO_LIB
);
516 name
= OBJ_nid2sn(OBJ_obj2nid(xa
->algorithm
));
518 (void)ERR_set_mark();
519 evp_md
= EVP_MD_fetch(p7_ctx
->libctx
, name
, p7_ctx
->propq
);
523 md
= EVP_get_digestbyname(name
);
526 (void)ERR_clear_last_mark();
527 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
528 PKCS7_R_UNKNOWN_DIGEST_TYPE
);
531 (void)ERR_pop_to_mark();
533 BIO_set_md(btmp
, md
);
543 if (cipher
!= NULL
) {
544 if ((etmp
= BIO_new(BIO_f_cipher())) == NULL
) {
545 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, ERR_R_BIO_LIB
);
550 * It was encrypted, we need to decrypt the secret key with the
555 * Find the recipientInfo which matches the passed certificate (if
560 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
561 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
562 if (!pkcs7_cmp_ri(ri
, pcert
))
567 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
568 PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE
);
573 /* If we haven't got a certificate try each ri in turn */
576 * Always attempt to decrypt all rinfo even after success as a
577 * defence against MMA timing attacks.
579 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
580 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
582 if (pkcs7_decrypt_rinfo(&ek
, &eklen
, ri
, pkey
,
583 EVP_CIPHER_key_length(cipher
)) < 0)
589 /* Only exit on fatal errors, not decrypt failure */
590 if (pkcs7_decrypt_rinfo(&ek
, &eklen
, ri
, pkey
, 0) < 0)
596 BIO_get_cipher_ctx(etmp
, &evp_ctx
);
597 if (EVP_CipherInit_ex(evp_ctx
, cipher
, NULL
, NULL
, NULL
, 0) <= 0)
599 if (EVP_CIPHER_asn1_to_param(evp_ctx
, enc_alg
->parameter
) < 0)
601 /* Generate random key as MMA defence */
602 len
= EVP_CIPHER_CTX_key_length(evp_ctx
);
605 tkeylen
= (size_t)len
;
606 tkey
= OPENSSL_malloc(tkeylen
);
609 if (EVP_CIPHER_CTX_rand_key(evp_ctx
, tkey
) <= 0)
617 if (eklen
!= EVP_CIPHER_CTX_key_length(evp_ctx
)) {
619 * Some S/MIME clients don't use the same key and effective key
620 * length. The key length is determined by the size of the
623 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx
, eklen
)) {
624 /* Use random key as MMA defence */
625 OPENSSL_clear_free(ek
, eklen
);
631 /* Clear errors so we don't leak information useful in MMA */
633 if (EVP_CipherInit_ex(evp_ctx
, NULL
, NULL
, ek
, NULL
, 0) <= 0)
636 OPENSSL_clear_free(ek
, eklen
);
638 OPENSSL_clear_free(tkey
, tkeylen
);
647 if (in_bio
!= NULL
) {
650 if (data_body
->length
> 0)
651 bio
= BIO_new_mem_buf(data_body
->data
, data_body
->length
);
653 bio
= BIO_new(BIO_s_mem());
656 BIO_set_mem_eof_return(bio
, 0);
663 EVP_CIPHER_free(evp_cipher
);
667 EVP_CIPHER_free(evp_cipher
);
668 OPENSSL_clear_free(ek
, eklen
);
669 OPENSSL_clear_free(tkey
, tkeylen
);
677 static BIO
*PKCS7_find_digest(EVP_MD_CTX
**pmd
, BIO
*bio
, int nid
)
680 bio
= BIO_find_type(bio
, BIO_TYPE_MD
);
682 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST
,
683 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
686 BIO_get_md_ctx(bio
, pmd
);
688 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST
, ERR_R_INTERNAL_ERROR
);
691 if (EVP_MD_CTX_type(*pmd
) == nid
)
698 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO
*si
, EVP_MD_CTX
*mctx
)
700 unsigned char md_data
[EVP_MAX_MD_SIZE
];
703 /* Add signing time if not already present */
704 if (!PKCS7_get_signed_attribute(si
, NID_pkcs9_signingTime
)) {
705 if (!PKCS7_add0_attrib_signing_time(si
, NULL
)) {
706 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
, ERR_R_MALLOC_FAILURE
);
712 if (!EVP_DigestFinal_ex(mctx
, md_data
, &md_len
)) {
713 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
, ERR_R_EVP_LIB
);
716 if (!PKCS7_add1_attrib_digest(si
, md_data
, md_len
)) {
717 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
, ERR_R_MALLOC_FAILURE
);
721 /* Now sign the attributes */
722 if (!PKCS7_SIGNER_INFO_sign(si
))
728 int PKCS7_dataFinal(PKCS7
*p7
, BIO
*bio
)
733 PKCS7_SIGNER_INFO
*si
;
734 EVP_MD_CTX
*mdc
, *ctx_tmp
;
735 STACK_OF(X509_ATTRIBUTE
) *sk
;
736 STACK_OF(PKCS7_SIGNER_INFO
) *si_sk
= NULL
;
737 ASN1_OCTET_STRING
*os
= NULL
;
738 const PKCS7_CTX
*p7_ctx
;
741 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_INVALID_NULL_POINTER
);
745 p7_ctx
= pkcs7_get0_ctx(p7
);
747 if (p7
->d
.ptr
== NULL
) {
748 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_NO_CONTENT
);
752 ctx_tmp
= EVP_MD_CTX_new();
753 if (ctx_tmp
== NULL
) {
754 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_MALLOC_FAILURE
);
758 i
= OBJ_obj2nid(p7
->type
);
759 p7
->state
= PKCS7_S_HEADER
;
765 case NID_pkcs7_signedAndEnveloped
:
766 /* XXXXXXXXXXXXXXXX */
767 si_sk
= p7
->d
.signed_and_enveloped
->signer_info
;
768 os
= p7
->d
.signed_and_enveloped
->enc_data
->enc_data
;
770 os
= ASN1_OCTET_STRING_new();
772 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_MALLOC_FAILURE
);
775 p7
->d
.signed_and_enveloped
->enc_data
->enc_data
= os
;
778 case NID_pkcs7_enveloped
:
779 /* XXXXXXXXXXXXXXXX */
780 os
= p7
->d
.enveloped
->enc_data
->enc_data
;
782 os
= ASN1_OCTET_STRING_new();
784 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_MALLOC_FAILURE
);
787 p7
->d
.enveloped
->enc_data
->enc_data
= os
;
790 case NID_pkcs7_signed
:
791 si_sk
= p7
->d
.sign
->signer_info
;
792 os
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
793 /* If detached data then the content is excluded */
794 if (PKCS7_type_is_data(p7
->d
.sign
->contents
) && p7
->detached
) {
795 ASN1_OCTET_STRING_free(os
);
797 p7
->d
.sign
->contents
->d
.data
= NULL
;
801 case NID_pkcs7_digest
:
802 os
= PKCS7_get_octet_string(p7
->d
.digest
->contents
);
803 /* If detached data then the content is excluded */
804 if (PKCS7_type_is_data(p7
->d
.digest
->contents
) && p7
->detached
) {
805 ASN1_OCTET_STRING_free(os
);
807 p7
->d
.digest
->contents
->d
.data
= NULL
;
812 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
817 for (i
= 0; i
< sk_PKCS7_SIGNER_INFO_num(si_sk
); i
++) {
818 si
= sk_PKCS7_SIGNER_INFO_value(si_sk
, i
);
819 if (si
->pkey
== NULL
)
822 j
= OBJ_obj2nid(si
->digest_alg
->algorithm
);
826 btmp
= PKCS7_find_digest(&mdc
, btmp
, j
);
832 * We now have the EVP_MD_CTX, lets do the signing.
834 if (!EVP_MD_CTX_copy_ex(ctx_tmp
, mdc
))
840 * If there are attributes, we add the digest attribute and only
841 * sign the attributes
843 if (sk_X509_ATTRIBUTE_num(sk
) > 0) {
844 if (!do_pkcs7_signed_attrib(si
, ctx_tmp
))
847 unsigned char *abuf
= NULL
;
848 unsigned int abuflen
;
849 abuflen
= EVP_PKEY_size(si
->pkey
);
850 abuf
= OPENSSL_malloc(abuflen
);
854 if (!EVP_SignFinal_with_libctx(ctx_tmp
, abuf
, &abuflen
, si
->pkey
,
855 p7_ctx
->libctx
, p7_ctx
->propq
)) {
857 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_EVP_LIB
);
860 ASN1_STRING_set0(si
->enc_digest
, abuf
, abuflen
);
863 } else if (i
== NID_pkcs7_digest
) {
864 unsigned char md_data
[EVP_MAX_MD_SIZE
];
866 if (!PKCS7_find_digest(&mdc
, bio
,
867 OBJ_obj2nid(p7
->d
.digest
->md
->algorithm
)))
869 if (!EVP_DigestFinal_ex(mdc
, md_data
, &md_len
))
871 if (!ASN1_OCTET_STRING_set(p7
->d
.digest
->digest
, md_data
, md_len
))
875 if (!PKCS7_is_detached(p7
)) {
877 * NOTE(emilia): I think we only reach os == NULL here because detached
878 * digested data support is broken.
882 if (!(os
->flags
& ASN1_STRING_FLAG_NDEF
)) {
885 btmp
= BIO_find_type(bio
, BIO_TYPE_MEM
);
887 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_UNABLE_TO_FIND_MEM_BIO
);
890 contlen
= BIO_get_mem_data(btmp
, &cont
);
892 * Mark the BIO read only then we can use its copy of the data
893 * instead of making an extra copy.
895 BIO_set_flags(btmp
, BIO_FLAGS_MEM_RDONLY
);
896 BIO_set_mem_eof_return(btmp
, 0);
897 ASN1_STRING_set0(os
, (unsigned char *)cont
, contlen
);
902 EVP_MD_CTX_free(ctx_tmp
);
906 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO
*si
)
909 EVP_PKEY_CTX
*pctx
= NULL
;
910 unsigned char *abuf
= NULL
;
913 const EVP_MD
*md
= NULL
;
914 const PKCS7_CTX
*ctx
= si
->ctx
;
916 md
= EVP_get_digestbyobj(si
->digest_alg
->algorithm
);
920 mctx
= EVP_MD_CTX_new();
922 PKCS7err(0, ERR_R_MALLOC_FAILURE
);
926 if (EVP_DigestSignInit_with_libctx(mctx
, &pctx
,
927 EVP_MD_name(md
), ctx
->libctx
, ctx
->propq
,
932 * TODO(3.0): This causes problems when providers are in use, so disabled
933 * for now. Can we get rid of this completely? AFAICT this ctrl has never
934 * been used since it was first put in. All internal implementations just
935 * return 1 and ignore this ctrl and have always done so by the looks of
936 * things. To fix this we could convert this ctrl into a param, which would
937 * require us to send all the signer info data as a set of params...but that
938 * is non-trivial and since this isn't used by anything it may be better
939 * just to remove it. The original commit that added it had this
940 * justification in CHANGES:
942 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
943 * EVP_PKEY_METHOD before and after signing via the
944 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
945 * before and/or after signing if necessary."
948 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
949 EVP_PKEY_CTRL_PKCS7_SIGN
, 0, si
) <= 0) {
950 PKCS7err(0, PKCS7_R_CTRL_ERROR
);
955 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->auth_attr
, &abuf
,
956 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN
));
959 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
963 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
965 abuf
= OPENSSL_malloc(siglen
);
968 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
972 * TODO(3.0): This causes problems when providers are in use, so disabled
973 * for now. Can we get rid of this completely? AFAICT this ctrl has never
974 * been used since it was first put in. All internal implementations just
975 * return 1 and ignore this ctrl and have always done so by the looks of
976 * things. To fix this we could convert this ctrl into a param, which would
977 * require us to send all the signer info data as a set of params...but that
978 * is non-trivial and since this isn't used by anything it may be better
979 * just to remove it. The original commit that added it had this
980 * justification in CHANGES:
982 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
983 * EVP_PKEY_METHOD before and after signing via the
984 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
985 * before and/or after signing if necessary."
988 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
989 EVP_PKEY_CTRL_PKCS7_SIGN
, 1, si
) <= 0) {
990 PKCS7err(0, PKCS7_R_CTRL_ERROR
);
995 EVP_MD_CTX_free(mctx
);
997 ASN1_STRING_set0(si
->enc_digest
, abuf
, siglen
);
1003 EVP_MD_CTX_free(mctx
);
1007 int PKCS7_dataVerify(X509_STORE
*cert_store
, X509_STORE_CTX
*ctx
, BIO
*bio
,
1008 PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
)
1010 PKCS7_ISSUER_AND_SERIAL
*ias
;
1012 STACK_OF(X509
) *cert
;
1016 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, PKCS7_R_INVALID_NULL_POINTER
);
1020 if (p7
->d
.ptr
== NULL
) {
1021 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, PKCS7_R_NO_CONTENT
);
1025 if (PKCS7_type_is_signed(p7
)) {
1026 cert
= p7
->d
.sign
->cert
;
1027 } else if (PKCS7_type_is_signedAndEnveloped(p7
)) {
1028 cert
= p7
->d
.signed_and_enveloped
->cert
;
1030 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, PKCS7_R_WRONG_PKCS7_TYPE
);
1033 /* XXXXXXXXXXXXXXXXXXXXXXX */
1034 ias
= si
->issuer_and_serial
;
1036 x509
= X509_find_by_issuer_and_serial(cert
, ias
->issuer
, ias
->serial
);
1038 /* were we able to find the cert in passed to us */
1040 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
,
1041 PKCS7_R_UNABLE_TO_FIND_CERTIFICATE
);
1046 if (!X509_STORE_CTX_init(ctx
, cert_store
, x509
, cert
)) {
1047 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, ERR_R_X509_LIB
);
1050 X509_STORE_CTX_set_purpose(ctx
, X509_PURPOSE_SMIME_SIGN
);
1051 i
= X509_verify_cert(ctx
);
1053 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, ERR_R_X509_LIB
);
1054 X509_STORE_CTX_cleanup(ctx
);
1057 X509_STORE_CTX_cleanup(ctx
);
1059 return PKCS7_signatureVerify(bio
, p7
, si
, x509
);
1064 int PKCS7_signatureVerify(BIO
*bio
, PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
,
1067 ASN1_OCTET_STRING
*os
;
1068 EVP_MD_CTX
*mdc_tmp
, *mdc
;
1070 EVP_MD
*fetched_md
= NULL
;
1073 STACK_OF(X509_ATTRIBUTE
) *sk
;
1076 const PKCS7_CTX
*ctx
= pkcs7_get0_ctx(p7
);
1078 mdc_tmp
= EVP_MD_CTX_new();
1079 if (mdc_tmp
== NULL
) {
1080 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, ERR_R_MALLOC_FAILURE
);
1084 if (!PKCS7_type_is_signed(p7
) && !PKCS7_type_is_signedAndEnveloped(p7
)) {
1085 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, PKCS7_R_WRONG_PKCS7_TYPE
);
1089 md_type
= OBJ_obj2nid(si
->digest_alg
->algorithm
);
1093 if ((btmp
== NULL
) ||
1094 ((btmp
= BIO_find_type(btmp
, BIO_TYPE_MD
)) == NULL
)) {
1095 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
,
1096 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
1099 BIO_get_md_ctx(btmp
, &mdc
);
1101 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, ERR_R_INTERNAL_ERROR
);
1104 if (EVP_MD_CTX_type(mdc
) == md_type
)
1107 * Workaround for some broken clients that put the signature OID
1108 * instead of the digest OID in digest_alg->algorithm
1110 if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc
)) == md_type
)
1112 btmp
= BIO_next(btmp
);
1116 * mdc is the digest ctx that we want, unless there are attributes, in
1117 * which case the digest is the signed attributes
1119 if (!EVP_MD_CTX_copy_ex(mdc_tmp
, mdc
))
1123 if ((sk
!= NULL
) && (sk_X509_ATTRIBUTE_num(sk
) != 0)) {
1124 unsigned char md_dat
[EVP_MAX_MD_SIZE
], *abuf
= NULL
;
1125 unsigned int md_len
;
1127 ASN1_OCTET_STRING
*message_digest
;
1129 if (!EVP_DigestFinal_ex(mdc_tmp
, md_dat
, &md_len
))
1131 message_digest
= PKCS7_digest_from_attributes(sk
);
1132 if (!message_digest
) {
1133 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
,
1134 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
1137 if ((message_digest
->length
!= (int)md_len
) ||
1138 (memcmp(message_digest
->data
, md_dat
, md_len
))) {
1139 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, PKCS7_R_DIGEST_FAILURE
);
1144 (void)ERR_set_mark();
1145 fetched_md
= EVP_MD_fetch(ctx
->libctx
, OBJ_nid2sn(md_type
), ctx
->propq
);
1147 if (fetched_md
!= NULL
)
1150 md
= EVP_get_digestbynid(md_type
);
1152 if (md
== NULL
|| !EVP_VerifyInit_ex(mdc_tmp
, md
, NULL
)) {
1153 (void)ERR_clear_last_mark();
1156 (void)ERR_pop_to_mark();
1158 alen
= ASN1_item_i2d((ASN1_VALUE
*)sk
, &abuf
,
1159 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY
));
1161 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, ERR_R_ASN1_LIB
);
1165 if (!EVP_VerifyUpdate(mdc_tmp
, abuf
, alen
))
1171 os
= si
->enc_digest
;
1172 pkey
= X509_get0_pubkey(x509
);
1178 i
= EVP_VerifyFinal_with_libctx(mdc_tmp
, os
->data
, os
->length
, pkey
,
1179 ctx
->libctx
, ctx
->propq
);
1181 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, PKCS7_R_SIGNATURE_FAILURE
);
1187 EVP_MD_CTX_free(mdc_tmp
);
1188 EVP_MD_free(fetched_md
);
1192 PKCS7_ISSUER_AND_SERIAL
*PKCS7_get_issuer_and_serial(PKCS7
*p7
, int idx
)
1194 STACK_OF(PKCS7_RECIP_INFO
) *rsk
;
1195 PKCS7_RECIP_INFO
*ri
;
1198 i
= OBJ_obj2nid(p7
->type
);
1199 if (i
!= NID_pkcs7_signedAndEnveloped
)
1201 if (p7
->d
.signed_and_enveloped
== NULL
)
1203 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
1206 if (sk_PKCS7_RECIP_INFO_num(rsk
) <= idx
)
1208 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, idx
);
1209 return ri
->issuer_and_serial
;
1212 ASN1_TYPE
*PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO
*si
, int nid
)
1214 return get_attribute(si
->auth_attr
, nid
);
1217 ASN1_TYPE
*PKCS7_get_attribute(PKCS7_SIGNER_INFO
*si
, int nid
)
1219 return get_attribute(si
->unauth_attr
, nid
);
1222 static ASN1_TYPE
*get_attribute(STACK_OF(X509_ATTRIBUTE
) *sk
, int nid
)
1226 idx
= X509at_get_attr_by_NID(sk
, nid
, -1);
1227 xa
= X509at_get_attr(sk
, idx
);
1228 return X509_ATTRIBUTE_get0_type(xa
, 0);
1231 ASN1_OCTET_STRING
*PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE
) *sk
)
1234 if ((astype
= get_attribute(sk
, NID_pkcs9_messageDigest
)) == NULL
)
1236 return astype
->value
.octet_string
;
1239 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO
*p7si
,
1240 STACK_OF(X509_ATTRIBUTE
) *sk
)
1244 sk_X509_ATTRIBUTE_pop_free(p7si
->auth_attr
, X509_ATTRIBUTE_free
);
1245 p7si
->auth_attr
= sk_X509_ATTRIBUTE_dup(sk
);
1246 if (p7si
->auth_attr
== NULL
)
1248 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1249 if ((sk_X509_ATTRIBUTE_set(p7si
->auth_attr
, i
,
1250 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1258 int PKCS7_set_attributes(PKCS7_SIGNER_INFO
*p7si
,
1259 STACK_OF(X509_ATTRIBUTE
) *sk
)
1263 sk_X509_ATTRIBUTE_pop_free(p7si
->unauth_attr
, X509_ATTRIBUTE_free
);
1264 p7si
->unauth_attr
= sk_X509_ATTRIBUTE_dup(sk
);
1265 if (p7si
->unauth_attr
== NULL
)
1267 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1268 if ((sk_X509_ATTRIBUTE_set(p7si
->unauth_attr
, i
,
1269 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1277 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
,
1280 return add_attribute(&(p7si
->auth_attr
), nid
, atrtype
, value
);
1283 int PKCS7_add_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
,
1286 return add_attribute(&(p7si
->unauth_attr
), nid
, atrtype
, value
);
1289 static int add_attribute(STACK_OF(X509_ATTRIBUTE
) **sk
, int nid
, int atrtype
,
1292 X509_ATTRIBUTE
*attr
= NULL
;
1295 if ((*sk
= sk_X509_ATTRIBUTE_new_null()) == NULL
)
1298 if ((attr
= X509_ATTRIBUTE_create(nid
, atrtype
, value
)) == NULL
)
1300 if (!sk_X509_ATTRIBUTE_push(*sk
, attr
)) {
1301 X509_ATTRIBUTE_free(attr
);
1307 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(*sk
); i
++) {
1308 attr
= sk_X509_ATTRIBUTE_value(*sk
, i
);
1309 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr
)) == nid
) {
1310 X509_ATTRIBUTE_free(attr
);
1311 attr
= X509_ATTRIBUTE_create(nid
, atrtype
, value
);
1314 if (!sk_X509_ATTRIBUTE_set(*sk
, i
, attr
)) {
1315 X509_ATTRIBUTE_free(attr
);