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(X509_ATTRIBUTE)
21 DEFINE_STACK_OF(PKCS7_RECIP_INFO)
22 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
24 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
26 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid);
28 static int PKCS7_type_is_other(PKCS7 *p7)
32 int nid = OBJ_obj2nid(p7->type);
36 case NID_pkcs7_signed:
37 case NID_pkcs7_enveloped:
38 case NID_pkcs7_signedAndEnveloped:
39 case NID_pkcs7_digest:
40 case NID_pkcs7_encrypted:
51 static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
53 if (PKCS7_type_is_data(p7))
55 if (PKCS7_type_is_other(p7) && p7->d.other
56 && (p7->d.other->type == V_ASN1_OCTET_STRING))
57 return p7->d.other->value.octet_string;
61 static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
66 EVP_MD *fetched = NULL;
69 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
70 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
74 name = OBJ_nid2sn(OBJ_obj2nid(alg->algorithm));
77 fetched = EVP_MD_fetch(ctx->libctx, name, ctx->propq);
81 md = EVP_get_digestbyname(name);
84 (void)ERR_clear_last_mark();
85 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, PKCS7_R_UNKNOWN_DIGEST_TYPE);
88 (void)ERR_pop_to_mark();
94 else if (!BIO_push(*pbio, btmp)) {
95 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
107 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
108 unsigned char *key, int keylen)
110 EVP_PKEY_CTX *pctx = NULL;
111 EVP_PKEY *pkey = NULL;
112 unsigned char *ek = NULL;
115 const PKCS7_CTX *ctx = ri->ctx;
117 pkey = X509_get0_pubkey(ri->cert);
121 pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
125 if (EVP_PKEY_encrypt_init(pctx) <= 0)
128 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
129 EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
130 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR);
134 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
137 ek = OPENSSL_malloc(eklen);
140 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE);
144 if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
147 ASN1_STRING_set0(ri->enc_key, ek, eklen);
153 EVP_PKEY_CTX_free(pctx);
159 static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
160 PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
163 EVP_PKEY_CTX *pctx = NULL;
164 unsigned char *ek = NULL;
167 const PKCS7_CTX *ctx = ri->ctx;
169 pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
173 if (EVP_PKEY_decrypt_init(pctx) <= 0)
176 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
177 EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
178 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
182 if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
183 ri->enc_key->data, ri->enc_key->length) <= 0)
186 ek = OPENSSL_malloc(eklen);
189 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
193 if (EVP_PKEY_decrypt(pctx, ek, &eklen,
194 ri->enc_key->data, ri->enc_key->length) <= 0
196 || (fixlen != 0 && eklen != fixlen)) {
198 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
204 OPENSSL_clear_free(*pek, *peklen);
209 EVP_PKEY_CTX_free(pctx);
216 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
219 BIO *out = NULL, *btmp = NULL;
220 X509_ALGOR *xa = NULL;
221 EVP_CIPHER *fetched_cipher = NULL;
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 fetched_cipher = EVP_CIPHER_fetch(p7_ctx->libctx,
315 EVP_CIPHER_name(evp_cipher),
317 if (fetched_cipher == NULL)
320 if (EVP_CipherInit_ex(ctx, fetched_cipher, NULL, NULL, NULL, 1) <= 0)
323 EVP_CIPHER_free(fetched_cipher);
324 fetched_cipher = NULL;
326 if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
328 if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
332 if (xalg->parameter == NULL) {
333 xalg->parameter = ASN1_TYPE_new();
334 if (xalg->parameter == NULL)
337 if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0)
341 /* Lets do the pub key stuff :-) */
342 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
343 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
344 if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
347 OPENSSL_cleanse(key, keylen);
357 if (PKCS7_is_detached(p7)) {
358 bio = BIO_new(BIO_s_null());
359 } else if (os && os->length > 0) {
360 bio = BIO_new_mem_buf(os->data, os->length);
362 bio = BIO_new(BIO_s_mem());
365 BIO_set_mem_eof_return(bio, 0);
377 EVP_CIPHER_free(fetched_cipher);
383 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
386 ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
387 X509_get_issuer_name(pcert));
390 return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
391 ri->issuer_and_serial->serial);
395 BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
398 BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
400 ASN1_OCTET_STRING *data_body = NULL;
401 EVP_MD *evp_md = NULL;
403 EVP_CIPHER *evp_cipher = NULL;
404 EVP_CIPHER_CTX *evp_ctx = NULL;
405 X509_ALGOR *enc_alg = NULL;
406 STACK_OF(X509_ALGOR) *md_sk = NULL;
407 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
408 PKCS7_RECIP_INFO *ri = NULL;
409 unsigned char *ek = NULL, *tkey = NULL;
410 int eklen = 0, tkeylen = 0;
412 const PKCS7_CTX *p7_ctx;
415 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER);
419 p7_ctx = pkcs7_get0_ctx(p7);
421 if (p7->d.ptr == NULL) {
422 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
426 i = OBJ_obj2nid(p7->type);
427 p7->state = PKCS7_S_HEADER;
430 case NID_pkcs7_signed:
432 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
433 * field and optional content.
434 * data_body is NULL if that structure has no (=detached) content
435 * or if the contentType is wrong (i.e., not "data").
437 data_body = PKCS7_get_octet_string(p7->d.sign->contents);
438 if (!PKCS7_is_detached(p7) && data_body == NULL) {
439 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
440 PKCS7_R_INVALID_SIGNED_DATA_TYPE);
443 md_sk = p7->d.sign->md_algs;
445 case NID_pkcs7_signedAndEnveloped:
446 rsk = p7->d.signed_and_enveloped->recipientinfo;
447 md_sk = p7->d.signed_and_enveloped->md_algs;
448 /* data_body is NULL if the optional EncryptedContent is missing. */
449 data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
450 enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
452 name = OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm));
453 evp_cipher = EVP_CIPHER_fetch(p7_ctx->libctx, name, p7_ctx->propq);
454 if (evp_cipher == NULL) {
455 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
456 PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
460 case NID_pkcs7_enveloped:
461 rsk = p7->d.enveloped->recipientinfo;
462 enc_alg = p7->d.enveloped->enc_data->algorithm;
463 /* data_body is NULL if the optional EncryptedContent is missing. */
464 data_body = p7->d.enveloped->enc_data->enc_data;
465 name = OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm));
466 evp_cipher = EVP_CIPHER_fetch(p7_ctx->libctx, name, p7_ctx->propq);
467 if (evp_cipher == NULL) {
468 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
469 PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
474 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
478 /* Detached content must be supplied via in_bio instead. */
479 if (data_body == NULL && in_bio == NULL) {
480 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
484 /* We will be checking the signature */
486 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
487 xa = sk_X509_ALGOR_value(md_sk, i);
488 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
489 PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
493 name = OBJ_nid2sn(OBJ_obj2nid(xa->algorithm));
495 (void)ERR_set_mark();
496 evp_md = EVP_MD_fetch(p7_ctx->libctx, name, p7_ctx->propq);
500 md = EVP_get_digestbyname(name);
503 (void)ERR_clear_last_mark();
504 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
505 PKCS7_R_UNKNOWN_DIGEST_TYPE);
508 (void)ERR_pop_to_mark();
510 BIO_set_md(btmp, md);
520 if (evp_cipher != NULL) {
521 if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
522 PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
527 * It was encrypted, we need to decrypt the secret key with the
532 * Find the recipientInfo which matches the passed certificate (if
537 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
538 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
539 if (!pkcs7_cmp_ri(ri, pcert))
544 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
545 PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
550 /* If we haven't got a certificate try each ri in turn */
553 * Always attempt to decrypt all rinfo even after success as a
554 * defence against MMA timing attacks.
556 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
557 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
559 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
560 EVP_CIPHER_key_length(evp_cipher)) < 0)
566 /* Only exit on fatal errors, not decrypt failure */
567 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
573 BIO_get_cipher_ctx(etmp, &evp_ctx);
574 if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, NULL, 0) <= 0)
576 if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0)
578 /* Generate random key as MMA defence */
579 len = EVP_CIPHER_CTX_key_length(evp_ctx);
582 tkeylen = (size_t)len;
583 tkey = OPENSSL_malloc(tkeylen);
586 if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
594 if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
596 * Some S/MIME clients don't use the same key and effective key
597 * length. The key length is determined by the size of the
600 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
601 /* Use random key as MMA defence */
602 OPENSSL_clear_free(ek, eklen);
608 /* Clear errors so we don't leak information useful in MMA */
610 if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
613 OPENSSL_clear_free(ek, eklen);
615 OPENSSL_clear_free(tkey, tkeylen);
624 if (in_bio != NULL) {
627 if (data_body->length > 0)
628 bio = BIO_new_mem_buf(data_body->data, data_body->length);
630 bio = BIO_new(BIO_s_mem());
633 BIO_set_mem_eof_return(bio, 0);
640 EVP_CIPHER_free(evp_cipher);
644 EVP_CIPHER_free(evp_cipher);
645 OPENSSL_clear_free(ek, eklen);
646 OPENSSL_clear_free(tkey, tkeylen);
654 static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
657 bio = BIO_find_type(bio, BIO_TYPE_MD);
659 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
660 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
663 BIO_get_md_ctx(bio, pmd);
665 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
668 if (EVP_MD_CTX_type(*pmd) == nid)
675 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
677 unsigned char md_data[EVP_MAX_MD_SIZE];
680 /* Add signing time if not already present */
681 if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
682 if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
683 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
689 if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
690 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
693 if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
694 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
698 /* Now sign the attributes */
699 if (!PKCS7_SIGNER_INFO_sign(si))
705 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
710 PKCS7_SIGNER_INFO *si;
711 EVP_MD_CTX *mdc, *ctx_tmp;
712 STACK_OF(X509_ATTRIBUTE) *sk;
713 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
714 ASN1_OCTET_STRING *os = NULL;
715 const PKCS7_CTX *p7_ctx;
718 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
722 p7_ctx = pkcs7_get0_ctx(p7);
724 if (p7->d.ptr == NULL) {
725 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
729 ctx_tmp = EVP_MD_CTX_new();
730 if (ctx_tmp == NULL) {
731 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
735 i = OBJ_obj2nid(p7->type);
736 p7->state = PKCS7_S_HEADER;
742 case NID_pkcs7_signedAndEnveloped:
743 /* XXXXXXXXXXXXXXXX */
744 si_sk = p7->d.signed_and_enveloped->signer_info;
745 os = p7->d.signed_and_enveloped->enc_data->enc_data;
747 os = ASN1_OCTET_STRING_new();
749 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
752 p7->d.signed_and_enveloped->enc_data->enc_data = os;
755 case NID_pkcs7_enveloped:
756 /* XXXXXXXXXXXXXXXX */
757 os = p7->d.enveloped->enc_data->enc_data;
759 os = ASN1_OCTET_STRING_new();
761 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
764 p7->d.enveloped->enc_data->enc_data = os;
767 case NID_pkcs7_signed:
768 si_sk = p7->d.sign->signer_info;
769 os = PKCS7_get_octet_string(p7->d.sign->contents);
770 /* If detached data then the content is excluded */
771 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
772 ASN1_OCTET_STRING_free(os);
774 p7->d.sign->contents->d.data = NULL;
778 case NID_pkcs7_digest:
779 os = PKCS7_get_octet_string(p7->d.digest->contents);
780 /* If detached data then the content is excluded */
781 if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
782 ASN1_OCTET_STRING_free(os);
784 p7->d.digest->contents->d.data = NULL;
789 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
794 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
795 si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
796 if (si->pkey == NULL)
799 j = OBJ_obj2nid(si->digest_alg->algorithm);
803 btmp = PKCS7_find_digest(&mdc, btmp, j);
809 * We now have the EVP_MD_CTX, lets do the signing.
811 if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
817 * If there are attributes, we add the digest attribute and only
818 * sign the attributes
820 if (sk_X509_ATTRIBUTE_num(sk) > 0) {
821 if (!do_pkcs7_signed_attrib(si, ctx_tmp))
824 unsigned char *abuf = NULL;
825 unsigned int abuflen;
826 abuflen = EVP_PKEY_size(si->pkey);
827 abuf = OPENSSL_malloc(abuflen);
831 if (!EVP_SignFinal_with_libctx(ctx_tmp, abuf, &abuflen, si->pkey,
832 p7_ctx->libctx, p7_ctx->propq)) {
834 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
837 ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
840 } else if (i == NID_pkcs7_digest) {
841 unsigned char md_data[EVP_MAX_MD_SIZE];
843 if (!PKCS7_find_digest(&mdc, bio,
844 OBJ_obj2nid(p7->d.digest->md->algorithm)))
846 if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
848 if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
852 if (!PKCS7_is_detached(p7)) {
854 * NOTE(emilia): I think we only reach os == NULL here because detached
855 * digested data support is broken.
859 if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
862 btmp = BIO_find_type(bio, BIO_TYPE_MEM);
864 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
867 contlen = BIO_get_mem_data(btmp, &cont);
869 * Mark the BIO read only then we can use its copy of the data
870 * instead of making an extra copy.
872 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
873 BIO_set_mem_eof_return(btmp, 0);
874 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
879 EVP_MD_CTX_free(ctx_tmp);
883 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
886 EVP_PKEY_CTX *pctx = NULL;
887 unsigned char *abuf = NULL;
890 const EVP_MD *md = NULL;
891 const PKCS7_CTX *ctx = si->ctx;
893 md = EVP_get_digestbyobj(si->digest_alg->algorithm);
897 mctx = EVP_MD_CTX_new();
899 PKCS7err(0, ERR_R_MALLOC_FAILURE);
903 if (EVP_DigestSignInit_with_libctx(mctx, &pctx,
904 EVP_MD_name(md), ctx->libctx, ctx->propq,
909 * TODO(3.0): This causes problems when providers are in use, so disabled
910 * for now. Can we get rid of this completely? AFAICT this ctrl has never
911 * been used since it was first put in. All internal implementations just
912 * return 1 and ignore this ctrl and have always done so by the looks of
913 * things. To fix this we could convert this ctrl into a param, which would
914 * require us to send all the signer info data as a set of params...but that
915 * is non-trivial and since this isn't used by anything it may be better
916 * just to remove it. The original commit that added it had this
917 * justification in CHANGES:
919 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
920 * EVP_PKEY_METHOD before and after signing via the
921 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
922 * before and/or after signing if necessary."
925 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
926 EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
927 PKCS7err(0, PKCS7_R_CTRL_ERROR);
932 alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
933 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
936 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
940 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
942 abuf = OPENSSL_malloc(siglen);
945 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
949 * TODO(3.0): This causes problems when providers are in use, so disabled
950 * for now. Can we get rid of this completely? AFAICT this ctrl has never
951 * been used since it was first put in. All internal implementations just
952 * return 1 and ignore this ctrl and have always done so by the looks of
953 * things. To fix this we could convert this ctrl into a param, which would
954 * require us to send all the signer info data as a set of params...but that
955 * is non-trivial and since this isn't used by anything it may be better
956 * just to remove it. The original commit that added it had this
957 * justification in CHANGES:
959 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
960 * EVP_PKEY_METHOD before and after signing via the
961 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
962 * before and/or after signing if necessary."
965 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
966 EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
967 PKCS7err(0, PKCS7_R_CTRL_ERROR);
972 EVP_MD_CTX_free(mctx);
974 ASN1_STRING_set0(si->enc_digest, abuf, siglen);
980 EVP_MD_CTX_free(mctx);
984 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
985 PKCS7 *p7, PKCS7_SIGNER_INFO *si)
987 PKCS7_ISSUER_AND_SERIAL *ias;
989 STACK_OF(X509) *cert;
993 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
997 if (p7->d.ptr == NULL) {
998 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
1002 if (PKCS7_type_is_signed(p7)) {
1003 cert = p7->d.sign->cert;
1004 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
1005 cert = p7->d.signed_and_enveloped->cert;
1007 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
1010 /* XXXXXXXXXXXXXXXXXXXXXXX */
1011 ias = si->issuer_and_serial;
1013 x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
1015 /* were we able to find the cert in passed to us */
1017 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
1018 PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
1023 if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
1024 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
1027 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1028 i = X509_verify_cert(ctx);
1030 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
1031 X509_STORE_CTX_cleanup(ctx);
1034 X509_STORE_CTX_cleanup(ctx);
1036 return PKCS7_signatureVerify(bio, p7, si, x509);
1041 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1044 ASN1_OCTET_STRING *os;
1045 EVP_MD_CTX *mdc_tmp, *mdc;
1047 EVP_MD *fetched_md = NULL;
1050 STACK_OF(X509_ATTRIBUTE) *sk;
1053 const PKCS7_CTX *ctx = pkcs7_get0_ctx(p7);
1055 mdc_tmp = EVP_MD_CTX_new();
1056 if (mdc_tmp == NULL) {
1057 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_MALLOC_FAILURE);
1061 if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1062 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
1066 md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1070 if ((btmp == NULL) ||
1071 ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1072 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1073 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1076 BIO_get_md_ctx(btmp, &mdc);
1078 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
1081 if (EVP_MD_CTX_type(mdc) == md_type)
1084 * Workaround for some broken clients that put the signature OID
1085 * instead of the digest OID in digest_alg->algorithm
1087 if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
1089 btmp = BIO_next(btmp);
1093 * mdc is the digest ctx that we want, unless there are attributes, in
1094 * which case the digest is the signed attributes
1096 if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1100 if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1101 unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1102 unsigned int md_len;
1104 ASN1_OCTET_STRING *message_digest;
1106 if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1108 message_digest = PKCS7_digest_from_attributes(sk);
1109 if (!message_digest) {
1110 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1111 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1114 if ((message_digest->length != (int)md_len) ||
1115 (memcmp(message_digest->data, md_dat, md_len))) {
1116 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
1121 (void)ERR_set_mark();
1122 fetched_md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(md_type), ctx->propq);
1124 if (fetched_md != NULL)
1127 md = EVP_get_digestbynid(md_type);
1129 if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1130 (void)ERR_clear_last_mark();
1133 (void)ERR_pop_to_mark();
1135 alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1136 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1138 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
1142 if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1148 os = si->enc_digest;
1149 pkey = X509_get0_pubkey(x509);
1155 i = EVP_VerifyFinal_with_libctx(mdc_tmp, os->data, os->length, pkey,
1156 ctx->libctx, ctx->propq);
1158 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
1164 EVP_MD_CTX_free(mdc_tmp);
1165 EVP_MD_free(fetched_md);
1169 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1171 STACK_OF(PKCS7_RECIP_INFO) *rsk;
1172 PKCS7_RECIP_INFO *ri;
1175 i = OBJ_obj2nid(p7->type);
1176 if (i != NID_pkcs7_signedAndEnveloped)
1178 if (p7->d.signed_and_enveloped == NULL)
1180 rsk = p7->d.signed_and_enveloped->recipientinfo;
1183 if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1185 ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1186 return ri->issuer_and_serial;
1189 ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
1191 return get_attribute(si->auth_attr, nid);
1194 ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
1196 return get_attribute(si->unauth_attr, nid);
1199 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1203 idx = X509at_get_attr_by_NID(sk, nid, -1);
1204 xa = X509at_get_attr(sk, idx);
1205 return X509_ATTRIBUTE_get0_type(xa, 0);
1208 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1211 if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1213 return astype->value.octet_string;
1216 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1217 STACK_OF(X509_ATTRIBUTE) *sk)
1221 sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1222 p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1223 if (p7si->auth_attr == NULL)
1225 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1226 if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1227 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1235 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1236 STACK_OF(X509_ATTRIBUTE) *sk)
1240 sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1241 p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1242 if (p7si->unauth_attr == NULL)
1244 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1245 if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1246 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1254 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1257 return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1260 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1263 return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1266 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1269 X509_ATTRIBUTE *attr = NULL;
1272 if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1275 if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1277 if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1278 X509_ATTRIBUTE_free(attr);
1284 for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1285 attr = sk_X509_ATTRIBUTE_value(*sk, i);
1286 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1287 X509_ATTRIBUTE_free(attr);
1288 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1291 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1292 X509_ATTRIBUTE_free(attr);