2 * Copyright 1995-2021 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 <openssl/rand.h>
12 #include <openssl/objects.h>
13 #include <openssl/x509.h>
14 #include <openssl/x509v3.h>
15 #include <openssl/err.h>
16 #include "internal/cryptlib.h"
17 #include "internal/sizes.h"
18 #include "pk7_local.h"
20 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
22 static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid);
24 int PKCS7_type_is_other(PKCS7 *p7)
28 int nid = OBJ_obj2nid(p7->type);
32 case NID_pkcs7_signed:
33 case NID_pkcs7_enveloped:
34 case NID_pkcs7_signedAndEnveloped:
35 case NID_pkcs7_digest:
36 case NID_pkcs7_encrypted:
47 ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
49 if (PKCS7_type_is_data(p7))
51 if (PKCS7_type_is_other(p7) && p7->d.other
52 && (p7->d.other->type == V_ASN1_OCTET_STRING))
53 return p7->d.other->value.octet_string;
57 static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
61 char name[OSSL_MAX_NAME_SIZE];
62 EVP_MD *fetched = NULL;
65 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
66 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
70 OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
73 fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
74 ossl_pkcs7_ctx_get0_propq(ctx));
78 md = EVP_get_digestbyname(name);
81 (void)ERR_clear_last_mark();
82 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
85 (void)ERR_pop_to_mark();
91 else if (!BIO_push(*pbio, btmp)) {
92 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
104 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
105 unsigned char *key, int keylen)
107 EVP_PKEY_CTX *pctx = NULL;
108 EVP_PKEY *pkey = NULL;
109 unsigned char *ek = NULL;
112 const PKCS7_CTX *ctx = ri->ctx;
114 pkey = X509_get0_pubkey(ri->cert);
118 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
119 ossl_pkcs7_ctx_get0_propq(ctx));
123 if (EVP_PKEY_encrypt_init(pctx) <= 0)
126 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
129 ek = OPENSSL_malloc(eklen);
133 if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
136 ASN1_STRING_set0(ri->enc_key, ek, eklen);
142 EVP_PKEY_CTX_free(pctx);
148 static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
149 PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
152 EVP_PKEY_CTX *pctx = NULL;
153 unsigned char *ek = NULL;
156 const PKCS7_CTX *ctx = ri->ctx;
158 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
159 ossl_pkcs7_ctx_get0_propq(ctx));
163 if (EVP_PKEY_decrypt_init(pctx) <= 0)
166 if (EVP_PKEY_is_a(pkey, "RSA"))
167 /* upper layer pkcs7 code incorrectly assumes that a successful RSA
168 * decryption means that the key matches ciphertext (which never
169 * was the case, implicit rejection or not), so to make it work
170 * disable implicit rejection for RSA keys */
171 EVP_PKEY_CTX_ctrl_str(pctx, "rsa_pkcs1_implicit_rejection", "0");
173 if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
174 ri->enc_key->data, ri->enc_key->length) <= 0)
177 ek = OPENSSL_malloc(eklen);
181 if (EVP_PKEY_decrypt(pctx, ek, &eklen,
182 ri->enc_key->data, ri->enc_key->length) <= 0
184 || (fixlen != 0 && eklen != fixlen)) {
186 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
192 OPENSSL_clear_free(*pek, *peklen);
197 EVP_PKEY_CTX_free(pctx);
204 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
207 BIO *out = NULL, *btmp = NULL;
208 X509_ALGOR *xa = NULL;
209 EVP_CIPHER *fetched_cipher = NULL;
210 const EVP_CIPHER *cipher;
211 const EVP_CIPHER *evp_cipher = NULL;
212 STACK_OF(X509_ALGOR) *md_sk = NULL;
213 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
214 X509_ALGOR *xalg = NULL;
215 PKCS7_RECIP_INFO *ri = NULL;
216 ASN1_OCTET_STRING *os = NULL;
217 const PKCS7_CTX *p7_ctx;
218 OSSL_LIB_CTX *libctx;
222 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
225 p7_ctx = ossl_pkcs7_get0_ctx(p7);
226 libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
227 propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
230 * The content field in the PKCS7 ContentInfo is optional, but that really
231 * only applies to inner content (precisely, detached signatures).
233 * When reading content, missing outer content is therefore treated as an
236 * When creating content, PKCS7_content_new() must be called before
237 * calling this method, so a NULL p7->d is always an error.
239 if (p7->d.ptr == NULL) {
240 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
244 i = OBJ_obj2nid(p7->type);
245 p7->state = PKCS7_S_HEADER;
248 case NID_pkcs7_signed:
249 md_sk = p7->d.sign->md_algs;
250 os = PKCS7_get_octet_string(p7->d.sign->contents);
252 case NID_pkcs7_signedAndEnveloped:
253 rsk = p7->d.signed_and_enveloped->recipientinfo;
254 md_sk = p7->d.signed_and_enveloped->md_algs;
255 xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
256 evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
257 if (evp_cipher == NULL) {
258 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
262 case NID_pkcs7_enveloped:
263 rsk = p7->d.enveloped->recipientinfo;
264 xalg = p7->d.enveloped->enc_data->algorithm;
265 evp_cipher = p7->d.enveloped->enc_data->cipher;
266 if (evp_cipher == NULL) {
267 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
271 case NID_pkcs7_digest:
272 xa = p7->d.digest->md;
273 os = PKCS7_get_octet_string(p7->d.digest->contents);
278 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
282 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
283 if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
286 if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
289 if (evp_cipher != NULL) {
290 unsigned char key[EVP_MAX_KEY_LENGTH];
291 unsigned char iv[EVP_MAX_IV_LENGTH];
295 if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
296 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
299 BIO_get_cipher_ctx(btmp, &ctx);
300 keylen = EVP_CIPHER_get_key_length(evp_cipher);
301 ivlen = EVP_CIPHER_get_iv_length(evp_cipher);
302 xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher));
304 if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
307 (void)ERR_set_mark();
308 fetched_cipher = EVP_CIPHER_fetch(libctx,
309 EVP_CIPHER_get0_name(evp_cipher),
311 (void)ERR_pop_to_mark();
312 if (fetched_cipher != NULL)
313 cipher = fetched_cipher;
317 if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
320 EVP_CIPHER_free(fetched_cipher);
321 fetched_cipher = NULL;
323 if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
325 if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
329 if (xalg->parameter == NULL) {
330 xalg->parameter = ASN1_TYPE_new();
331 if (xalg->parameter == NULL)
334 if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0)
338 /* Lets do the pub key stuff :-) */
339 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
340 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
341 if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
344 OPENSSL_cleanse(key, keylen);
354 if (PKCS7_is_detached(p7)) {
355 bio = BIO_new(BIO_s_null());
356 } else if (os && os->length > 0) {
357 bio = BIO_new_mem_buf(os->data, os->length);
359 bio = BIO_new(BIO_s_mem());
362 BIO_set_mem_eof_return(bio, 0);
374 EVP_CIPHER_free(fetched_cipher);
380 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
383 ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
384 X509_get_issuer_name(pcert));
387 return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
388 ri->issuer_and_serial->serial);
392 BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
395 BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
397 ASN1_OCTET_STRING *data_body = NULL;
398 EVP_MD *evp_md = NULL;
400 EVP_CIPHER *evp_cipher = NULL;
401 const EVP_CIPHER *cipher = NULL;
402 EVP_CIPHER_CTX *evp_ctx = NULL;
403 X509_ALGOR *enc_alg = NULL;
404 STACK_OF(X509_ALGOR) *md_sk = NULL;
405 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
406 PKCS7_RECIP_INFO *ri = NULL;
407 unsigned char *ek = NULL, *tkey = NULL;
408 int eklen = 0, tkeylen = 0;
409 char name[OSSL_MAX_NAME_SIZE];
410 const PKCS7_CTX *p7_ctx;
411 OSSL_LIB_CTX *libctx;
415 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
419 p7_ctx = ossl_pkcs7_get0_ctx(p7);
420 libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
421 propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
423 if (p7->d.ptr == NULL) {
424 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
428 i = OBJ_obj2nid(p7->type);
429 p7->state = PKCS7_S_HEADER;
432 case NID_pkcs7_signed:
434 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
435 * field and optional content.
436 * data_body is NULL if that structure has no (=detached) content
437 * or if the contentType is wrong (i.e., not "data").
439 data_body = PKCS7_get_octet_string(p7->d.sign->contents);
440 if (!PKCS7_is_detached(p7) && data_body == NULL) {
441 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE);
444 md_sk = p7->d.sign->md_algs;
446 case NID_pkcs7_signedAndEnveloped:
447 rsk = p7->d.signed_and_enveloped->recipientinfo;
448 md_sk = p7->d.signed_and_enveloped->md_algs;
449 /* data_body is NULL if the optional EncryptedContent is missing. */
450 data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
451 enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
453 OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
455 (void)ERR_set_mark();
456 evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
457 if (evp_cipher != NULL)
460 cipher = EVP_get_cipherbyname(name);
462 if (cipher == NULL) {
463 (void)ERR_clear_last_mark();
464 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
467 (void)ERR_pop_to_mark();
469 case NID_pkcs7_enveloped:
470 rsk = p7->d.enveloped->recipientinfo;
471 enc_alg = p7->d.enveloped->enc_data->algorithm;
472 /* data_body is NULL if the optional EncryptedContent is missing. */
473 data_body = p7->d.enveloped->enc_data->enc_data;
474 OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
476 (void)ERR_set_mark();
477 evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
478 if (evp_cipher != NULL)
481 cipher = EVP_get_cipherbyname(name);
483 if (cipher == NULL) {
484 (void)ERR_clear_last_mark();
485 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
488 (void)ERR_pop_to_mark();
491 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
495 /* Detached content must be supplied via in_bio instead. */
496 if (data_body == NULL && in_bio == NULL) {
497 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
501 /* We will be checking the signature */
503 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
504 xa = sk_X509_ALGOR_value(md_sk, i);
505 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
506 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
510 OBJ_obj2txt(name, sizeof(name), xa->algorithm, 0);
512 (void)ERR_set_mark();
513 evp_md = EVP_MD_fetch(libctx, name, propq);
517 md = EVP_get_digestbyname(name);
520 (void)ERR_clear_last_mark();
521 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
524 (void)ERR_pop_to_mark();
526 BIO_set_md(btmp, md);
536 if (cipher != NULL) {
537 if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
538 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
543 * It was encrypted, we need to decrypt the secret key with the
548 * Find the recipientInfo which matches the passed certificate (if
553 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
554 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
555 if (!pkcs7_cmp_ri(ri, pcert))
560 ERR_raise(ERR_LIB_PKCS7,
561 PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
566 /* If we haven't got a certificate try each ri in turn */
569 * Always attempt to decrypt all rinfo even after success as a
570 * defence against MMA timing attacks.
572 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
573 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
575 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
576 EVP_CIPHER_get_key_length(cipher)) < 0)
582 /* Only exit on fatal errors, not decrypt failure */
583 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
589 BIO_get_cipher_ctx(etmp, &evp_ctx);
590 if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
592 if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0)
594 /* Generate random key as MMA defence */
595 len = EVP_CIPHER_CTX_get_key_length(evp_ctx);
598 tkeylen = (size_t)len;
599 tkey = OPENSSL_malloc(tkeylen);
602 if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
610 if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) {
612 * Some S/MIME clients don't use the same key and effective key
613 * length. The key length is determined by the size of the
616 if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) {
617 /* Use random key as MMA defence */
618 OPENSSL_clear_free(ek, eklen);
624 /* Clear errors so we don't leak information useful in MMA */
626 if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
629 OPENSSL_clear_free(ek, eklen);
631 OPENSSL_clear_free(tkey, tkeylen);
640 if (in_bio != NULL) {
643 if (data_body->length > 0)
644 bio = BIO_new_mem_buf(data_body->data, data_body->length);
646 bio = BIO_new(BIO_s_mem());
649 BIO_set_mem_eof_return(bio, 0);
656 EVP_CIPHER_free(evp_cipher);
660 EVP_CIPHER_free(evp_cipher);
661 OPENSSL_clear_free(ek, eklen);
662 OPENSSL_clear_free(tkey, tkeylen);
670 static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
673 bio = BIO_find_type(bio, BIO_TYPE_MD);
675 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
678 BIO_get_md_ctx(bio, pmd);
680 ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
683 if (EVP_MD_CTX_get_type(*pmd) == nid)
690 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
692 unsigned char md_data[EVP_MAX_MD_SIZE];
695 /* Add signing time if not already present */
696 if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
697 if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
698 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
704 if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
705 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
708 if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
709 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
713 /* Now sign the attributes */
714 if (!PKCS7_SIGNER_INFO_sign(si))
720 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
725 PKCS7_SIGNER_INFO *si;
726 EVP_MD_CTX *mdc, *ctx_tmp;
727 STACK_OF(X509_ATTRIBUTE) *sk;
728 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
729 ASN1_OCTET_STRING *os = NULL;
730 const PKCS7_CTX *p7_ctx;
733 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
737 p7_ctx = ossl_pkcs7_get0_ctx(p7);
739 if (p7->d.ptr == NULL) {
740 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
744 ctx_tmp = EVP_MD_CTX_new();
745 if (ctx_tmp == NULL) {
746 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
750 i = OBJ_obj2nid(p7->type);
751 p7->state = PKCS7_S_HEADER;
757 case NID_pkcs7_signedAndEnveloped:
758 /* XXXXXXXXXXXXXXXX */
759 si_sk = p7->d.signed_and_enveloped->signer_info;
760 os = p7->d.signed_and_enveloped->enc_data->enc_data;
762 os = ASN1_OCTET_STRING_new();
764 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
767 p7->d.signed_and_enveloped->enc_data->enc_data = os;
770 case NID_pkcs7_enveloped:
771 /* XXXXXXXXXXXXXXXX */
772 os = p7->d.enveloped->enc_data->enc_data;
774 os = ASN1_OCTET_STRING_new();
776 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
779 p7->d.enveloped->enc_data->enc_data = os;
782 case NID_pkcs7_signed:
783 si_sk = p7->d.sign->signer_info;
784 os = PKCS7_get_octet_string(p7->d.sign->contents);
785 /* If detached data then the content is excluded */
786 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
787 ASN1_OCTET_STRING_free(os);
789 p7->d.sign->contents->d.data = NULL;
793 case NID_pkcs7_digest:
794 os = PKCS7_get_octet_string(p7->d.digest->contents);
795 /* If detached data then the content is excluded */
796 if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
797 ASN1_OCTET_STRING_free(os);
799 p7->d.digest->contents->d.data = NULL;
804 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
809 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
810 si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
811 if (si->pkey == NULL)
814 j = OBJ_obj2nid(si->digest_alg->algorithm);
818 btmp = PKCS7_find_digest(&mdc, btmp, j);
824 * We now have the EVP_MD_CTX, lets do the signing.
826 if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
832 * If there are attributes, we add the digest attribute and only
833 * sign the attributes
835 if (sk_X509_ATTRIBUTE_num(sk) > 0) {
836 if (!do_pkcs7_signed_attrib(si, ctx_tmp))
839 unsigned char *abuf = NULL;
840 unsigned int abuflen;
841 abuflen = EVP_PKEY_get_size(si->pkey);
842 abuf = OPENSSL_malloc(abuflen);
846 if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
847 ossl_pkcs7_ctx_get0_libctx(p7_ctx),
848 ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
850 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
853 ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
856 } else if (i == NID_pkcs7_digest) {
857 unsigned char md_data[EVP_MAX_MD_SIZE];
859 if (!PKCS7_find_digest(&mdc, bio,
860 OBJ_obj2nid(p7->d.digest->md->algorithm)))
862 if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
864 if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
868 if (!PKCS7_is_detached(p7)) {
870 * NOTE(emilia): I think we only reach os == NULL here because detached
871 * digested data support is broken.
875 if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
878 btmp = BIO_find_type(bio, BIO_TYPE_MEM);
880 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
883 contlen = BIO_get_mem_data(btmp, &cont);
885 * Mark the BIO read only then we can use its copy of the data
886 * instead of making an extra copy.
888 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
889 BIO_set_mem_eof_return(btmp, 0);
890 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
895 EVP_MD_CTX_free(ctx_tmp);
899 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
902 EVP_PKEY_CTX *pctx = NULL;
903 unsigned char *abuf = NULL;
906 const EVP_MD *md = NULL;
907 const PKCS7_CTX *ctx = si->ctx;
909 md = EVP_get_digestbyobj(si->digest_alg->algorithm);
913 mctx = EVP_MD_CTX_new();
915 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
919 if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
920 ossl_pkcs7_ctx_get0_libctx(ctx),
921 ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
925 alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
926 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
929 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
933 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
935 abuf = OPENSSL_malloc(siglen);
938 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
941 EVP_MD_CTX_free(mctx);
943 ASN1_STRING_set0(si->enc_digest, abuf, siglen);
949 EVP_MD_CTX_free(mctx);
953 /* This partly overlaps with PKCS7_verify(). It does not support flags. */
954 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
955 PKCS7 *p7, PKCS7_SIGNER_INFO *si)
957 PKCS7_ISSUER_AND_SERIAL *ias;
959 STACK_OF(X509) *untrusted;
960 STACK_OF(X509_CRL) *crls;
964 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
968 if (p7->d.ptr == NULL) {
969 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
973 if (PKCS7_type_is_signed(p7)) {
974 untrusted = p7->d.sign->cert;
975 crls = p7->d.sign->crl;
976 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
977 untrusted = p7->d.signed_and_enveloped->cert;
978 crls = p7->d.signed_and_enveloped->crl;
980 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
983 X509_STORE_CTX_set0_crls(ctx, crls);
985 /* XXXXXXXXXXXXXXXXXXXXXXX */
986 ias = si->issuer_and_serial;
988 signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
990 /* Were we able to find the signer certificate in passed to us? */
991 if (signer == NULL) {
992 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
997 if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
998 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1001 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1002 i = X509_verify_cert(ctx);
1004 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1008 return PKCS7_signatureVerify(bio, p7, si, signer);
1013 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1016 ASN1_OCTET_STRING *os;
1017 EVP_MD_CTX *mdc_tmp, *mdc;
1019 EVP_MD *fetched_md = NULL;
1022 STACK_OF(X509_ATTRIBUTE) *sk;
1025 const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1026 OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1027 const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1029 mdc_tmp = EVP_MD_CTX_new();
1030 if (mdc_tmp == NULL) {
1031 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
1035 if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1036 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1040 md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1044 if ((btmp == NULL) ||
1045 ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1046 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1049 BIO_get_md_ctx(btmp, &mdc);
1051 ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1054 if (EVP_MD_CTX_get_type(mdc) == md_type)
1057 * Workaround for some broken clients that put the signature OID
1058 * instead of the digest OID in digest_alg->algorithm
1060 if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1062 btmp = BIO_next(btmp);
1066 * mdc is the digest ctx that we want, unless there are attributes, in
1067 * which case the digest is the signed attributes
1069 if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1073 if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1074 unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1075 unsigned int md_len;
1077 ASN1_OCTET_STRING *message_digest;
1079 if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1081 message_digest = PKCS7_digest_from_attributes(sk);
1082 if (!message_digest) {
1083 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1086 if ((message_digest->length != (int)md_len) ||
1087 (memcmp(message_digest->data, md_dat, md_len))) {
1088 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1093 (void)ERR_set_mark();
1094 fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1096 if (fetched_md != NULL)
1099 md = EVP_get_digestbynid(md_type);
1101 if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1102 (void)ERR_clear_last_mark();
1105 (void)ERR_pop_to_mark();
1107 alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1108 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1110 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1114 if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1120 os = si->enc_digest;
1121 pkey = X509_get0_pubkey(signer);
1127 i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1129 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1135 EVP_MD_CTX_free(mdc_tmp);
1136 EVP_MD_free(fetched_md);
1140 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1142 STACK_OF(PKCS7_RECIP_INFO) *rsk;
1143 PKCS7_RECIP_INFO *ri;
1146 i = OBJ_obj2nid(p7->type);
1147 if (i != NID_pkcs7_signedAndEnveloped)
1149 if (p7->d.signed_and_enveloped == NULL)
1151 rsk = p7->d.signed_and_enveloped->recipientinfo;
1154 if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1156 ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1157 return ri->issuer_and_serial;
1160 ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1162 return get_attribute(si->auth_attr, nid);
1165 ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1167 return get_attribute(si->unauth_attr, nid);
1170 static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1172 int idx = X509at_get_attr_by_NID(sk, nid, -1);
1176 return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1179 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1182 if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1184 return astype->value.octet_string;
1187 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1188 STACK_OF(X509_ATTRIBUTE) *sk)
1192 sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1193 p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1194 if (p7si->auth_attr == NULL)
1196 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1197 if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1198 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1206 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1207 STACK_OF(X509_ATTRIBUTE) *sk)
1211 sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1212 p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1213 if (p7si->unauth_attr == NULL)
1215 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1216 if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1217 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1225 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1228 return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1231 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1234 return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1237 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1240 X509_ATTRIBUTE *attr = NULL;
1243 if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1246 if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1248 if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1249 X509_ATTRIBUTE_free(attr);
1255 for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1256 attr = sk_X509_ATTRIBUTE_value(*sk, i);
1257 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1258 X509_ATTRIBUTE_free(attr);
1259 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1262 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1263 X509_ATTRIBUTE_free(attr);