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 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
21 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid);
23 static int PKCS7_type_is_other(PKCS7 *p7)
27 int nid = OBJ_obj2nid(p7->type);
31 case NID_pkcs7_signed:
32 case NID_pkcs7_enveloped:
33 case NID_pkcs7_signedAndEnveloped:
34 case NID_pkcs7_digest:
35 case NID_pkcs7_encrypted:
46 static ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
48 if (PKCS7_type_is_data(p7))
50 if (PKCS7_type_is_other(p7) && p7->d.other
51 && (p7->d.other->type == V_ASN1_OCTET_STRING))
52 return p7->d.other->value.octet_string;
56 static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
61 EVP_MD *fetched = NULL;
64 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
65 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
69 name = OBJ_nid2sn(OBJ_obj2nid(alg->algorithm));
72 fetched = EVP_MD_fetch(ctx->libctx, name, ctx->propq);
76 md = EVP_get_digestbyname(name);
79 (void)ERR_clear_last_mark();
80 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, PKCS7_R_UNKNOWN_DIGEST_TYPE);
83 (void)ERR_pop_to_mark();
89 else if (!BIO_push(*pbio, btmp)) {
90 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST, ERR_R_BIO_LIB);
102 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
103 unsigned char *key, int keylen)
105 EVP_PKEY_CTX *pctx = NULL;
106 EVP_PKEY *pkey = NULL;
107 unsigned char *ek = NULL;
110 const PKCS7_CTX *ctx = ri->ctx;
112 pkey = X509_get0_pubkey(ri->cert);
116 pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
120 if (EVP_PKEY_encrypt_init(pctx) <= 0)
123 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
124 EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) {
125 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR);
129 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
132 ek = OPENSSL_malloc(eklen);
135 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE);
139 if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
142 ASN1_STRING_set0(ri->enc_key, ek, eklen);
148 EVP_PKEY_CTX_free(pctx);
154 static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
155 PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
158 EVP_PKEY_CTX *pctx = NULL;
159 unsigned char *ek = NULL;
162 const PKCS7_CTX *ctx = ri->ctx;
164 pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkey, ctx->propq);
168 if (EVP_PKEY_decrypt_init(pctx) <= 0)
171 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DECRYPT,
172 EVP_PKEY_CTRL_PKCS7_DECRYPT, 0, ri) <= 0) {
173 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, PKCS7_R_CTRL_ERROR);
177 if (EVP_PKEY_decrypt(pctx, NULL, &eklen,
178 ri->enc_key->data, ri->enc_key->length) <= 0)
181 ek = OPENSSL_malloc(eklen);
184 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_MALLOC_FAILURE);
188 if (EVP_PKEY_decrypt(pctx, ek, &eklen,
189 ri->enc_key->data, ri->enc_key->length) <= 0
191 || (fixlen != 0 && eklen != fixlen)) {
193 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO, ERR_R_EVP_LIB);
199 OPENSSL_clear_free(*pek, *peklen);
204 EVP_PKEY_CTX_free(pctx);
211 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
214 BIO *out = NULL, *btmp = NULL;
215 X509_ALGOR *xa = NULL;
216 EVP_CIPHER *fetched_cipher = NULL;
217 const EVP_CIPHER *cipher;
218 const EVP_CIPHER *evp_cipher = NULL;
219 STACK_OF(X509_ALGOR) *md_sk = NULL;
220 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
221 X509_ALGOR *xalg = NULL;
222 PKCS7_RECIP_INFO *ri = NULL;
223 ASN1_OCTET_STRING *os = NULL;
224 const PKCS7_CTX *p7_ctx;
227 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
230 p7_ctx = pkcs7_get0_ctx(p7);
233 * The content field in the PKCS7 ContentInfo is optional, but that really
234 * only applies to inner content (precisely, detached signatures).
236 * When reading content, missing outer content is therefore treated as an
239 * When creating content, PKCS7_content_new() must be called before
240 * calling this method, so a NULL p7->d is always an error.
242 if (p7->d.ptr == NULL) {
243 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
247 i = OBJ_obj2nid(p7->type);
248 p7->state = PKCS7_S_HEADER;
251 case NID_pkcs7_signed:
252 md_sk = p7->d.sign->md_algs;
253 os = PKCS7_get_octet_string(p7->d.sign->contents);
255 case NID_pkcs7_signedAndEnveloped:
256 rsk = p7->d.signed_and_enveloped->recipientinfo;
257 md_sk = p7->d.signed_and_enveloped->md_algs;
258 xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
259 evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
260 if (evp_cipher == NULL) {
261 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
265 case NID_pkcs7_enveloped:
266 rsk = p7->d.enveloped->recipientinfo;
267 xalg = p7->d.enveloped->enc_data->algorithm;
268 evp_cipher = p7->d.enveloped->enc_data->cipher;
269 if (evp_cipher == NULL) {
270 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED);
274 case NID_pkcs7_digest:
275 xa = p7->d.digest->md;
276 os = PKCS7_get_octet_string(p7->d.digest->contents);
281 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
285 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
286 if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
289 if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
292 if (evp_cipher != NULL) {
293 unsigned char key[EVP_MAX_KEY_LENGTH];
294 unsigned char iv[EVP_MAX_IV_LENGTH];
298 if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
299 PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB);
302 BIO_get_cipher_ctx(btmp, &ctx);
303 keylen = EVP_CIPHER_key_length(evp_cipher);
304 ivlen = EVP_CIPHER_iv_length(evp_cipher);
305 xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher));
307 if (RAND_bytes_ex(p7_ctx->libctx, iv, ivlen) <= 0)
310 (void)ERR_set_mark();
311 fetched_cipher = EVP_CIPHER_fetch(p7_ctx->libctx,
312 EVP_CIPHER_name(evp_cipher),
314 (void)ERR_pop_to_mark();
315 if (fetched_cipher != NULL)
316 cipher = fetched_cipher;
320 if (EVP_CipherInit_ex(ctx, 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 const EVP_CIPHER *cipher = NULL;
405 EVP_CIPHER_CTX *evp_ctx = NULL;
406 X509_ALGOR *enc_alg = NULL;
407 STACK_OF(X509_ALGOR) *md_sk = NULL;
408 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
409 PKCS7_RECIP_INFO *ri = NULL;
410 unsigned char *ek = NULL, *tkey = NULL;
411 int eklen = 0, tkeylen = 0;
413 const PKCS7_CTX *p7_ctx;
416 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER);
420 p7_ctx = pkcs7_get0_ctx(p7);
422 if (p7->d.ptr == NULL) {
423 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
427 i = OBJ_obj2nid(p7->type);
428 p7->state = PKCS7_S_HEADER;
431 case NID_pkcs7_signed:
433 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
434 * field and optional content.
435 * data_body is NULL if that structure has no (=detached) content
436 * or if the contentType is wrong (i.e., not "data").
438 data_body = PKCS7_get_octet_string(p7->d.sign->contents);
439 if (!PKCS7_is_detached(p7) && data_body == NULL) {
440 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
441 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 name = OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm));
455 (void)ERR_set_mark();
456 evp_cipher = EVP_CIPHER_fetch(p7_ctx->libctx, name, p7_ctx->propq);
457 if (evp_cipher != NULL)
460 cipher = EVP_get_cipherbyname(name);
462 if (cipher == NULL) {
463 (void)ERR_clear_last_mark();
464 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
465 PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
468 (void)ERR_pop_to_mark();
470 case NID_pkcs7_enveloped:
471 rsk = p7->d.enveloped->recipientinfo;
472 enc_alg = p7->d.enveloped->enc_data->algorithm;
473 /* data_body is NULL if the optional EncryptedContent is missing. */
474 data_body = p7->d.enveloped->enc_data->enc_data;
475 name = OBJ_nid2sn(OBJ_obj2nid(enc_alg->algorithm));
477 (void)ERR_set_mark();
478 evp_cipher = EVP_CIPHER_fetch(p7_ctx->libctx, name, p7_ctx->propq);
479 if (evp_cipher != NULL)
482 cipher = EVP_get_cipherbyname(name);
484 if (cipher == NULL) {
485 (void)ERR_clear_last_mark();
486 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
487 PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
490 (void)ERR_pop_to_mark();
493 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
497 /* Detached content must be supplied via in_bio instead. */
498 if (data_body == NULL && in_bio == NULL) {
499 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
503 /* We will be checking the signature */
505 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
506 xa = sk_X509_ALGOR_value(md_sk, i);
507 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
508 PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
512 name = OBJ_nid2sn(OBJ_obj2nid(xa->algorithm));
514 (void)ERR_set_mark();
515 evp_md = EVP_MD_fetch(p7_ctx->libctx, name, p7_ctx->propq);
519 md = EVP_get_digestbyname(name);
522 (void)ERR_clear_last_mark();
523 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
524 PKCS7_R_UNKNOWN_DIGEST_TYPE);
527 (void)ERR_pop_to_mark();
529 BIO_set_md(btmp, md);
539 if (cipher != NULL) {
540 if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
541 PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB);
546 * It was encrypted, we need to decrypt the secret key with the
551 * Find the recipientInfo which matches the passed certificate (if
556 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
557 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
558 if (!pkcs7_cmp_ri(ri, pcert))
563 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
564 PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
569 /* If we haven't got a certificate try each ri in turn */
572 * Always attempt to decrypt all rinfo even after success as a
573 * defence against MMA timing attacks.
575 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
576 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
578 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
579 EVP_CIPHER_key_length(cipher)) < 0)
585 /* Only exit on fatal errors, not decrypt failure */
586 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
592 BIO_get_cipher_ctx(etmp, &evp_ctx);
593 if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
595 if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0)
597 /* Generate random key as MMA defence */
598 len = EVP_CIPHER_CTX_key_length(evp_ctx);
601 tkeylen = (size_t)len;
602 tkey = OPENSSL_malloc(tkeylen);
605 if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
613 if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) {
615 * Some S/MIME clients don't use the same key and effective key
616 * length. The key length is determined by the size of the
619 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
620 /* Use random key as MMA defence */
621 OPENSSL_clear_free(ek, eklen);
627 /* Clear errors so we don't leak information useful in MMA */
629 if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
632 OPENSSL_clear_free(ek, eklen);
634 OPENSSL_clear_free(tkey, tkeylen);
643 if (in_bio != NULL) {
646 if (data_body->length > 0)
647 bio = BIO_new_mem_buf(data_body->data, data_body->length);
649 bio = BIO_new(BIO_s_mem());
652 BIO_set_mem_eof_return(bio, 0);
659 EVP_CIPHER_free(evp_cipher);
663 EVP_CIPHER_free(evp_cipher);
664 OPENSSL_clear_free(ek, eklen);
665 OPENSSL_clear_free(tkey, tkeylen);
673 static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
676 bio = BIO_find_type(bio, BIO_TYPE_MD);
678 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST,
679 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
682 BIO_get_md_ctx(bio, pmd);
684 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST, ERR_R_INTERNAL_ERROR);
687 if (EVP_MD_CTX_type(*pmd) == nid)
694 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
696 unsigned char md_data[EVP_MAX_MD_SIZE];
699 /* Add signing time if not already present */
700 if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
701 if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
702 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
708 if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
709 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_EVP_LIB);
712 if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
713 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB, ERR_R_MALLOC_FAILURE);
717 /* Now sign the attributes */
718 if (!PKCS7_SIGNER_INFO_sign(si))
724 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
729 PKCS7_SIGNER_INFO *si;
730 EVP_MD_CTX *mdc, *ctx_tmp;
731 STACK_OF(X509_ATTRIBUTE) *sk;
732 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
733 ASN1_OCTET_STRING *os = NULL;
734 const PKCS7_CTX *p7_ctx;
737 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_INVALID_NULL_POINTER);
741 p7_ctx = pkcs7_get0_ctx(p7);
743 if (p7->d.ptr == NULL) {
744 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
748 ctx_tmp = EVP_MD_CTX_new();
749 if (ctx_tmp == NULL) {
750 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
754 i = OBJ_obj2nid(p7->type);
755 p7->state = PKCS7_S_HEADER;
761 case NID_pkcs7_signedAndEnveloped:
762 /* XXXXXXXXXXXXXXXX */
763 si_sk = p7->d.signed_and_enveloped->signer_info;
764 os = p7->d.signed_and_enveloped->enc_data->enc_data;
766 os = ASN1_OCTET_STRING_new();
768 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
771 p7->d.signed_and_enveloped->enc_data->enc_data = os;
774 case NID_pkcs7_enveloped:
775 /* XXXXXXXXXXXXXXXX */
776 os = p7->d.enveloped->enc_data->enc_data;
778 os = ASN1_OCTET_STRING_new();
780 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_MALLOC_FAILURE);
783 p7->d.enveloped->enc_data->enc_data = os;
786 case NID_pkcs7_signed:
787 si_sk = p7->d.sign->signer_info;
788 os = PKCS7_get_octet_string(p7->d.sign->contents);
789 /* If detached data then the content is excluded */
790 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
791 ASN1_OCTET_STRING_free(os);
793 p7->d.sign->contents->d.data = NULL;
797 case NID_pkcs7_digest:
798 os = PKCS7_get_octet_string(p7->d.digest->contents);
799 /* If detached data then the content is excluded */
800 if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
801 ASN1_OCTET_STRING_free(os);
803 p7->d.digest->contents->d.data = NULL;
808 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
813 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
814 si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
815 if (si->pkey == NULL)
818 j = OBJ_obj2nid(si->digest_alg->algorithm);
822 btmp = PKCS7_find_digest(&mdc, btmp, j);
828 * We now have the EVP_MD_CTX, lets do the signing.
830 if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
836 * If there are attributes, we add the digest attribute and only
837 * sign the attributes
839 if (sk_X509_ATTRIBUTE_num(sk) > 0) {
840 if (!do_pkcs7_signed_attrib(si, ctx_tmp))
843 unsigned char *abuf = NULL;
844 unsigned int abuflen;
845 abuflen = EVP_PKEY_size(si->pkey);
846 abuf = OPENSSL_malloc(abuflen);
850 if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
851 p7_ctx->libctx, p7_ctx->propq)) {
853 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, ERR_R_EVP_LIB);
856 ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
859 } else if (i == NID_pkcs7_digest) {
860 unsigned char md_data[EVP_MAX_MD_SIZE];
862 if (!PKCS7_find_digest(&mdc, bio,
863 OBJ_obj2nid(p7->d.digest->md->algorithm)))
865 if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
867 if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
871 if (!PKCS7_is_detached(p7)) {
873 * NOTE(emilia): I think we only reach os == NULL here because detached
874 * digested data support is broken.
878 if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
881 btmp = BIO_find_type(bio, BIO_TYPE_MEM);
883 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
886 contlen = BIO_get_mem_data(btmp, &cont);
888 * Mark the BIO read only then we can use its copy of the data
889 * instead of making an extra copy.
891 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
892 BIO_set_mem_eof_return(btmp, 0);
893 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
898 EVP_MD_CTX_free(ctx_tmp);
902 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
905 EVP_PKEY_CTX *pctx = NULL;
906 unsigned char *abuf = NULL;
909 const EVP_MD *md = NULL;
910 const PKCS7_CTX *ctx = si->ctx;
912 md = EVP_get_digestbyobj(si->digest_alg->algorithm);
916 mctx = EVP_MD_CTX_new();
918 PKCS7err(0, ERR_R_MALLOC_FAILURE);
922 if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_name(md), ctx->libctx,
923 ctx->propq, si->pkey) <= 0)
927 * TODO(3.0): This causes problems when providers are in use, so disabled
928 * for now. Can we get rid of this completely? AFAICT this ctrl has never
929 * been used since it was first put in. All internal implementations just
930 * return 1 and ignore this ctrl and have always done so by the looks of
931 * things. To fix this we could convert this ctrl into a param, which would
932 * require us to send all the signer info data as a set of params...but that
933 * is non-trivial and since this isn't used by anything it may be better
934 * just to remove it. The original commit that added it had this
935 * justification in CHANGES:
937 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
938 * EVP_PKEY_METHOD before and after signing via the
939 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
940 * before and/or after signing if necessary."
943 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
944 EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) {
945 PKCS7err(0, PKCS7_R_CTRL_ERROR);
950 alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
951 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
954 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
958 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
960 abuf = OPENSSL_malloc(siglen);
963 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
967 * TODO(3.0): This causes problems when providers are in use, so disabled
968 * for now. Can we get rid of this completely? AFAICT this ctrl has never
969 * been used since it was first put in. All internal implementations just
970 * return 1 and ignore this ctrl and have always done so by the looks of
971 * things. To fix this we could convert this ctrl into a param, which would
972 * require us to send all the signer info data as a set of params...but that
973 * is non-trivial and since this isn't used by anything it may be better
974 * just to remove it. The original commit that added it had this
975 * justification in CHANGES:
977 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
978 * EVP_PKEY_METHOD before and after signing via the
979 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
980 * before and/or after signing if necessary."
983 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
984 EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) {
985 PKCS7err(0, PKCS7_R_CTRL_ERROR);
990 EVP_MD_CTX_free(mctx);
992 ASN1_STRING_set0(si->enc_digest, abuf, siglen);
998 EVP_MD_CTX_free(mctx);
1002 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
1003 PKCS7 *p7, PKCS7_SIGNER_INFO *si)
1005 PKCS7_ISSUER_AND_SERIAL *ias;
1007 STACK_OF(X509) *cert;
1011 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_INVALID_NULL_POINTER);
1015 if (p7->d.ptr == NULL) {
1016 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
1020 if (PKCS7_type_is_signed(p7)) {
1021 cert = p7->d.sign->cert;
1022 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
1023 cert = p7->d.signed_and_enveloped->cert;
1025 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
1028 /* XXXXXXXXXXXXXXXXXXXXXXX */
1029 ias = si->issuer_and_serial;
1031 x509 = X509_find_by_issuer_and_serial(cert, ias->issuer, ias->serial);
1033 /* were we able to find the cert in passed to us */
1035 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
1036 PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
1041 if (!X509_STORE_CTX_init(ctx, cert_store, x509, cert)) {
1042 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
1045 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
1046 i = X509_verify_cert(ctx);
1048 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, ERR_R_X509_LIB);
1049 X509_STORE_CTX_cleanup(ctx);
1052 X509_STORE_CTX_cleanup(ctx);
1054 return PKCS7_signatureVerify(bio, p7, si, x509);
1059 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1062 ASN1_OCTET_STRING *os;
1063 EVP_MD_CTX *mdc_tmp, *mdc;
1065 EVP_MD *fetched_md = NULL;
1068 STACK_OF(X509_ATTRIBUTE) *sk;
1071 const PKCS7_CTX *ctx = pkcs7_get0_ctx(p7);
1073 mdc_tmp = EVP_MD_CTX_new();
1074 if (mdc_tmp == NULL) {
1075 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_MALLOC_FAILURE);
1079 if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1080 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE);
1084 md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1088 if ((btmp == NULL) ||
1089 ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1090 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1091 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1094 BIO_get_md_ctx(btmp, &mdc);
1096 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR);
1099 if (EVP_MD_CTX_type(mdc) == md_type)
1102 * Workaround for some broken clients that put the signature OID
1103 * instead of the digest OID in digest_alg->algorithm
1105 if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type)
1107 btmp = BIO_next(btmp);
1111 * mdc is the digest ctx that we want, unless there are attributes, in
1112 * which case the digest is the signed attributes
1114 if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1118 if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1119 unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1120 unsigned int md_len;
1122 ASN1_OCTET_STRING *message_digest;
1124 if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1126 message_digest = PKCS7_digest_from_attributes(sk);
1127 if (!message_digest) {
1128 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY,
1129 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1132 if ((message_digest->length != (int)md_len) ||
1133 (memcmp(message_digest->data, md_dat, md_len))) {
1134 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE);
1139 (void)ERR_set_mark();
1140 fetched_md = EVP_MD_fetch(ctx->libctx, OBJ_nid2sn(md_type), ctx->propq);
1142 if (fetched_md != NULL)
1145 md = EVP_get_digestbynid(md_type);
1147 if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1148 (void)ERR_clear_last_mark();
1151 (void)ERR_pop_to_mark();
1153 alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1154 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1156 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB);
1160 if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1166 os = si->enc_digest;
1167 pkey = X509_get0_pubkey(x509);
1173 i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, ctx->libctx,
1176 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE);
1182 EVP_MD_CTX_free(mdc_tmp);
1183 EVP_MD_free(fetched_md);
1187 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1189 STACK_OF(PKCS7_RECIP_INFO) *rsk;
1190 PKCS7_RECIP_INFO *ri;
1193 i = OBJ_obj2nid(p7->type);
1194 if (i != NID_pkcs7_signedAndEnveloped)
1196 if (p7->d.signed_and_enveloped == NULL)
1198 rsk = p7->d.signed_and_enveloped->recipientinfo;
1201 if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1203 ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1204 return ri->issuer_and_serial;
1207 ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid)
1209 return get_attribute(si->auth_attr, nid);
1212 ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid)
1214 return get_attribute(si->unauth_attr, nid);
1217 static ASN1_TYPE *get_attribute(STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1221 idx = X509at_get_attr_by_NID(sk, nid, -1);
1222 xa = X509at_get_attr(sk, idx);
1223 return X509_ATTRIBUTE_get0_type(xa, 0);
1226 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1229 if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1231 return astype->value.octet_string;
1234 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1235 STACK_OF(X509_ATTRIBUTE) *sk)
1239 sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1240 p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1241 if (p7si->auth_attr == NULL)
1243 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1244 if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1245 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1253 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1254 STACK_OF(X509_ATTRIBUTE) *sk)
1258 sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1259 p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1260 if (p7si->unauth_attr == NULL)
1262 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1263 if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1264 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1272 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1275 return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1278 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1281 return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1284 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1287 X509_ATTRIBUTE *attr = NULL;
1290 if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1293 if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1295 if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1296 X509_ATTRIBUTE_free(attr);
1302 for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1303 attr = sk_X509_ATTRIBUTE_value(*sk, i);
1304 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1305 X509_ATTRIBUTE_free(attr);
1306 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1309 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1310 X509_ATTRIBUTE_free(attr);