2 * Copyright 1995-2023 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 "crypto/evp.h"
19 #include "pk7_local.h"
21 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
23 static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid);
25 int PKCS7_type_is_other(PKCS7 *p7)
29 int nid = OBJ_obj2nid(p7->type);
33 case NID_pkcs7_signed:
34 case NID_pkcs7_enveloped:
35 case NID_pkcs7_signedAndEnveloped:
36 case NID_pkcs7_digest:
37 case NID_pkcs7_encrypted:
48 ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7)
50 if (PKCS7_type_is_data(p7))
52 if (PKCS7_type_is_other(p7) && p7->d.other
53 && (p7->d.other->type == V_ASN1_OCTET_STRING))
54 return p7->d.other->value.octet_string;
58 static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg,
62 char name[OSSL_MAX_NAME_SIZE];
63 EVP_MD *fetched = NULL;
66 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
67 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
71 OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0);
74 fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name,
75 ossl_pkcs7_ctx_get0_propq(ctx));
79 md = EVP_get_digestbyname(name);
82 (void)ERR_clear_last_mark();
83 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
86 (void)ERR_pop_to_mark();
88 if (BIO_set_md(btmp, md) <= 0) {
89 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
96 else if (!BIO_push(*pbio, btmp)) {
97 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
109 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri,
110 unsigned char *key, int keylen)
112 EVP_PKEY_CTX *pctx = NULL;
113 EVP_PKEY *pkey = NULL;
114 unsigned char *ek = NULL;
117 const PKCS7_CTX *ctx = ri->ctx;
119 pkey = X509_get0_pubkey(ri->cert);
123 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
124 ossl_pkcs7_ctx_get0_propq(ctx));
128 if (EVP_PKEY_encrypt_init(pctx) <= 0)
131 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0)
134 ek = OPENSSL_malloc(eklen);
138 if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0)
141 ASN1_STRING_set0(ri->enc_key, ek, eklen);
147 EVP_PKEY_CTX_free(pctx);
153 static int pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen,
154 PKCS7_RECIP_INFO *ri, EVP_PKEY *pkey,
157 EVP_PKEY_CTX *pctx = NULL;
158 unsigned char *ek = NULL;
161 const PKCS7_CTX *ctx = ri->ctx;
163 pctx = EVP_PKEY_CTX_new_from_pkey(ossl_pkcs7_ctx_get0_libctx(ctx), pkey,
164 ossl_pkcs7_ctx_get0_propq(ctx));
168 if (EVP_PKEY_decrypt_init(pctx) <= 0)
171 if (EVP_PKEY_is_a(pkey, "RSA"))
172 /* upper layer pkcs7 code incorrectly assumes that a successful RSA
173 * decryption means that the key matches ciphertext (which never
174 * was the case, implicit rejection or not), so to make it work
175 * disable implicit rejection for RSA keys */
176 EVP_PKEY_CTX_ctrl_str(pctx, "rsa_pkcs1_implicit_rejection", "0");
178 ret = evp_pkey_decrypt_alloc(pctx, &ek, &eklen, fixlen,
179 ri->enc_key->data, ri->enc_key->length);
185 OPENSSL_clear_free(*pek, *peklen);
190 EVP_PKEY_CTX_free(pctx);
197 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio)
200 BIO *out = NULL, *btmp = NULL;
201 X509_ALGOR *xa = NULL;
202 EVP_CIPHER *fetched_cipher = NULL;
203 const EVP_CIPHER *cipher;
204 const EVP_CIPHER *evp_cipher = NULL;
205 STACK_OF(X509_ALGOR) *md_sk = NULL;
206 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
207 X509_ALGOR *xalg = NULL;
208 PKCS7_RECIP_INFO *ri = NULL;
209 ASN1_OCTET_STRING *os = NULL;
210 const PKCS7_CTX *p7_ctx;
211 OSSL_LIB_CTX *libctx;
215 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
218 p7_ctx = ossl_pkcs7_get0_ctx(p7);
219 libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
220 propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
223 * The content field in the PKCS7 ContentInfo is optional, but that really
224 * only applies to inner content (precisely, detached signatures).
226 * When reading content, missing outer content is therefore treated as an
229 * When creating content, PKCS7_content_new() must be called before
230 * calling this method, so a NULL p7->d is always an error.
232 if (p7->d.ptr == NULL) {
233 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
237 i = OBJ_obj2nid(p7->type);
238 p7->state = PKCS7_S_HEADER;
241 case NID_pkcs7_signed:
242 md_sk = p7->d.sign->md_algs;
243 os = PKCS7_get_octet_string(p7->d.sign->contents);
245 case NID_pkcs7_signedAndEnveloped:
246 rsk = p7->d.signed_and_enveloped->recipientinfo;
247 md_sk = p7->d.signed_and_enveloped->md_algs;
248 xalg = p7->d.signed_and_enveloped->enc_data->algorithm;
249 evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher;
250 if (evp_cipher == NULL) {
251 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
255 case NID_pkcs7_enveloped:
256 rsk = p7->d.enveloped->recipientinfo;
257 xalg = p7->d.enveloped->enc_data->algorithm;
258 evp_cipher = p7->d.enveloped->enc_data->cipher;
259 if (evp_cipher == NULL) {
260 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CIPHER_NOT_INITIALIZED);
264 case NID_pkcs7_digest:
265 xa = p7->d.digest->md;
266 os = PKCS7_get_octet_string(p7->d.digest->contents);
271 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
275 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++)
276 if (!pkcs7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i), p7_ctx))
279 if (xa && !pkcs7_bio_add_digest(&out, xa, p7_ctx))
282 if (evp_cipher != NULL) {
283 unsigned char key[EVP_MAX_KEY_LENGTH];
284 unsigned char iv[EVP_MAX_IV_LENGTH];
288 if ((btmp = BIO_new(BIO_f_cipher())) == NULL) {
289 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
292 BIO_get_cipher_ctx(btmp, &ctx);
293 keylen = EVP_CIPHER_get_key_length(evp_cipher);
294 ivlen = EVP_CIPHER_get_iv_length(evp_cipher);
295 xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_get_type(evp_cipher));
297 if (RAND_bytes_ex(libctx, iv, ivlen, 0) <= 0)
300 (void)ERR_set_mark();
301 fetched_cipher = EVP_CIPHER_fetch(libctx,
302 EVP_CIPHER_get0_name(evp_cipher),
304 (void)ERR_pop_to_mark();
305 if (fetched_cipher != NULL)
306 cipher = fetched_cipher;
310 if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) <= 0)
313 EVP_CIPHER_free(fetched_cipher);
314 fetched_cipher = NULL;
316 if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
318 if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0)
322 if (xalg->parameter == NULL) {
323 xalg->parameter = ASN1_TYPE_new();
324 if (xalg->parameter == NULL)
327 if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) <= 0)
331 /* Lets do the pub key stuff :-) */
332 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
333 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
334 if (pkcs7_encode_rinfo(ri, key, keylen) <= 0)
337 OPENSSL_cleanse(key, keylen);
347 if (PKCS7_is_detached(p7)) {
348 bio = BIO_new(BIO_s_null());
349 } else if (os && os->length > 0) {
350 bio = BIO_new_mem_buf(os->data, os->length);
352 bio = BIO_new(BIO_s_mem());
355 BIO_set_mem_eof_return(bio, 0);
367 EVP_CIPHER_free(fetched_cipher);
373 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO *ri, X509 *pcert)
376 ret = X509_NAME_cmp(ri->issuer_and_serial->issuer,
377 X509_get_issuer_name(pcert));
380 return ASN1_INTEGER_cmp(X509_get0_serialNumber(pcert),
381 ri->issuer_and_serial->serial);
385 BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
388 BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL;
390 ASN1_OCTET_STRING *data_body = NULL;
391 EVP_MD *evp_md = NULL;
393 EVP_CIPHER *evp_cipher = NULL;
394 const EVP_CIPHER *cipher = NULL;
395 EVP_CIPHER_CTX *evp_ctx = NULL;
396 X509_ALGOR *enc_alg = NULL;
397 STACK_OF(X509_ALGOR) *md_sk = NULL;
398 STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL;
399 PKCS7_RECIP_INFO *ri = NULL;
400 unsigned char *ek = NULL, *tkey = NULL;
401 int eklen = 0, tkeylen = 0;
402 char name[OSSL_MAX_NAME_SIZE];
403 const PKCS7_CTX *p7_ctx;
404 OSSL_LIB_CTX *libctx;
408 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
412 p7_ctx = ossl_pkcs7_get0_ctx(p7);
413 libctx = ossl_pkcs7_ctx_get0_libctx(p7_ctx);
414 propq = ossl_pkcs7_ctx_get0_propq(p7_ctx);
416 if (p7->d.ptr == NULL) {
417 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
421 i = OBJ_obj2nid(p7->type);
422 p7->state = PKCS7_S_HEADER;
425 case NID_pkcs7_signed:
427 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
428 * field and optional content.
429 * data_body is NULL if that structure has no (=detached) content
430 * or if the contentType is wrong (i.e., not "data").
432 data_body = PKCS7_get_octet_string(p7->d.sign->contents);
433 if (!PKCS7_is_detached(p7) && data_body == NULL) {
434 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_SIGNED_DATA_TYPE);
437 md_sk = p7->d.sign->md_algs;
439 case NID_pkcs7_signedAndEnveloped:
440 rsk = p7->d.signed_and_enveloped->recipientinfo;
441 md_sk = p7->d.signed_and_enveloped->md_algs;
442 /* data_body is NULL if the optional EncryptedContent is missing. */
443 data_body = p7->d.signed_and_enveloped->enc_data->enc_data;
444 enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm;
446 OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
448 (void)ERR_set_mark();
449 evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
450 if (evp_cipher != NULL)
453 cipher = EVP_get_cipherbyname(name);
455 if (cipher == NULL) {
456 (void)ERR_clear_last_mark();
457 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
460 (void)ERR_pop_to_mark();
462 case NID_pkcs7_enveloped:
463 rsk = p7->d.enveloped->recipientinfo;
464 enc_alg = p7->d.enveloped->enc_data->algorithm;
465 /* data_body is NULL if the optional EncryptedContent is missing. */
466 data_body = p7->d.enveloped->enc_data->enc_data;
467 OBJ_obj2txt(name, sizeof(name), enc_alg->algorithm, 0);
469 (void)ERR_set_mark();
470 evp_cipher = EVP_CIPHER_fetch(libctx, name, propq);
471 if (evp_cipher != NULL)
474 cipher = EVP_get_cipherbyname(name);
476 if (cipher == NULL) {
477 (void)ERR_clear_last_mark();
478 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CIPHER_TYPE);
481 (void)ERR_pop_to_mark();
484 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
488 /* Detached content must be supplied via in_bio instead. */
489 if (data_body == NULL && in_bio == NULL) {
490 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
494 /* We will be checking the signature */
496 for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) {
497 xa = sk_X509_ALGOR_value(md_sk, i);
498 if ((btmp = BIO_new(BIO_f_md())) == NULL) {
499 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
503 OBJ_obj2txt(name, sizeof(name), xa->algorithm, 0);
505 (void)ERR_set_mark();
506 evp_md = EVP_MD_fetch(libctx, name, propq);
510 md = EVP_get_digestbyname(name);
513 (void)ERR_clear_last_mark();
514 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE);
517 (void)ERR_pop_to_mark();
519 if (BIO_set_md(btmp, md) <= 0) {
521 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
533 if (cipher != NULL) {
534 if ((etmp = BIO_new(BIO_f_cipher())) == NULL) {
535 ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
540 * It was encrypted, we need to decrypt the secret key with the
545 * Find the recipientInfo which matches the passed certificate (if
550 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
551 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
552 if (!pkcs7_cmp_ri(ri, pcert))
557 ERR_raise(ERR_LIB_PKCS7,
558 PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
563 /* If we haven't got a certificate try each ri in turn */
566 * Always attempt to decrypt all rinfo even after success as a
567 * defence against MMA timing attacks.
569 for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) {
570 ri = sk_PKCS7_RECIP_INFO_value(rsk, i);
572 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey,
573 EVP_CIPHER_get_key_length(cipher)) < 0)
579 /* Only exit on fatal errors, not decrypt failure */
580 if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey, 0) < 0)
586 BIO_get_cipher_ctx(etmp, &evp_ctx);
587 if (EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0) <= 0)
589 if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) <= 0)
591 /* Generate random key as MMA defence */
592 len = EVP_CIPHER_CTX_get_key_length(evp_ctx);
595 tkeylen = (size_t)len;
596 tkey = OPENSSL_malloc(tkeylen);
599 if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0)
607 if (eklen != EVP_CIPHER_CTX_get_key_length(evp_ctx)) {
609 * Some S/MIME clients don't use the same key and effective key
610 * length. The key length is determined by the size of the
613 if (EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen) <= 0) {
614 /* Use random key as MMA defence */
615 OPENSSL_clear_free(ek, eklen);
621 /* Clear errors so we don't leak information useful in MMA */
623 if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
626 OPENSSL_clear_free(ek, eklen);
628 OPENSSL_clear_free(tkey, tkeylen);
637 if (in_bio != NULL) {
640 if (data_body->length > 0)
641 bio = BIO_new_mem_buf(data_body->data, data_body->length);
643 bio = BIO_new(BIO_s_mem());
646 BIO_set_mem_eof_return(bio, 0);
653 EVP_CIPHER_free(evp_cipher);
657 EVP_CIPHER_free(evp_cipher);
658 OPENSSL_clear_free(ek, eklen);
659 OPENSSL_clear_free(tkey, tkeylen);
667 static BIO *PKCS7_find_digest(EVP_MD_CTX **pmd, BIO *bio, int nid)
670 bio = BIO_find_type(bio, BIO_TYPE_MD);
672 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
675 BIO_get_md_ctx(bio, pmd);
677 ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
680 if (EVP_MD_CTX_get_type(*pmd) == nid)
687 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO *si, EVP_MD_CTX *mctx)
689 unsigned char md_data[EVP_MAX_MD_SIZE];
692 /* Add signing time if not already present */
693 if (!PKCS7_get_signed_attribute(si, NID_pkcs9_signingTime)) {
694 if (!PKCS7_add0_attrib_signing_time(si, NULL)) {
695 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
701 if (!EVP_DigestFinal_ex(mctx, md_data, &md_len)) {
702 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
705 if (!PKCS7_add1_attrib_digest(si, md_data, md_len)) {
706 ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
710 /* Now sign the attributes */
711 if (!PKCS7_SIGNER_INFO_sign(si))
717 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
722 PKCS7_SIGNER_INFO *si;
723 EVP_MD_CTX *mdc, *ctx_tmp;
724 STACK_OF(X509_ATTRIBUTE) *sk;
725 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
726 ASN1_OCTET_STRING *os = NULL;
727 const PKCS7_CTX *p7_ctx;
730 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
734 p7_ctx = ossl_pkcs7_get0_ctx(p7);
736 if (p7->d.ptr == NULL) {
737 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
741 ctx_tmp = EVP_MD_CTX_new();
742 if (ctx_tmp == NULL) {
743 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
747 i = OBJ_obj2nid(p7->type);
748 p7->state = PKCS7_S_HEADER;
754 case NID_pkcs7_signedAndEnveloped:
755 /* XXXXXXXXXXXXXXXX */
756 si_sk = p7->d.signed_and_enveloped->signer_info;
757 os = p7->d.signed_and_enveloped->enc_data->enc_data;
759 os = ASN1_OCTET_STRING_new();
761 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
764 p7->d.signed_and_enveloped->enc_data->enc_data = os;
767 case NID_pkcs7_enveloped:
768 /* XXXXXXXXXXXXXXXX */
769 os = p7->d.enveloped->enc_data->enc_data;
771 os = ASN1_OCTET_STRING_new();
773 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
776 p7->d.enveloped->enc_data->enc_data = os;
779 case NID_pkcs7_signed:
780 si_sk = p7->d.sign->signer_info;
781 os = PKCS7_get_octet_string(p7->d.sign->contents);
782 /* If detached data then the content is excluded */
783 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
784 ASN1_OCTET_STRING_free(os);
786 p7->d.sign->contents->d.data = NULL;
790 case NID_pkcs7_digest:
791 os = PKCS7_get_octet_string(p7->d.digest->contents);
792 /* If detached data then the content is excluded */
793 if (PKCS7_type_is_data(p7->d.digest->contents) && p7->detached) {
794 ASN1_OCTET_STRING_free(os);
796 p7->d.digest->contents->d.data = NULL;
801 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE);
806 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(si_sk); i++) {
807 si = sk_PKCS7_SIGNER_INFO_value(si_sk, i);
808 if (si->pkey == NULL)
811 j = OBJ_obj2nid(si->digest_alg->algorithm);
815 btmp = PKCS7_find_digest(&mdc, btmp, j);
821 * We now have the EVP_MD_CTX, lets do the signing.
823 if (!EVP_MD_CTX_copy_ex(ctx_tmp, mdc))
829 * If there are attributes, we add the digest attribute and only
830 * sign the attributes
832 if (sk_X509_ATTRIBUTE_num(sk) > 0) {
833 if (!do_pkcs7_signed_attrib(si, ctx_tmp))
836 unsigned char *abuf = NULL;
837 unsigned int abuflen = EVP_PKEY_get_size(si->pkey);
839 if (abuflen == 0 || (abuf = OPENSSL_malloc(abuflen)) == NULL)
842 if (!EVP_SignFinal_ex(ctx_tmp, abuf, &abuflen, si->pkey,
843 ossl_pkcs7_ctx_get0_libctx(p7_ctx),
844 ossl_pkcs7_ctx_get0_propq(p7_ctx))) {
846 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
849 ASN1_STRING_set0(si->enc_digest, abuf, abuflen);
852 } else if (i == NID_pkcs7_digest) {
853 unsigned char md_data[EVP_MAX_MD_SIZE];
855 if (!PKCS7_find_digest(&mdc, bio,
856 OBJ_obj2nid(p7->d.digest->md->algorithm)))
858 if (!EVP_DigestFinal_ex(mdc, md_data, &md_len))
860 if (!ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len))
864 if (!PKCS7_is_detached(p7)) {
866 * NOTE(emilia): I think we only reach os == NULL here because detached
867 * digested data support is broken.
871 if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
874 btmp = BIO_find_type(bio, BIO_TYPE_MEM);
876 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
879 contlen = BIO_get_mem_data(btmp, &cont);
881 * Mark the BIO read only then we can use its copy of the data
882 * instead of making an extra copy.
884 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
885 BIO_set_mem_eof_return(btmp, 0);
886 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
891 EVP_MD_CTX_free(ctx_tmp);
895 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si)
898 EVP_PKEY_CTX *pctx = NULL;
899 unsigned char *abuf = NULL;
902 const EVP_MD *md = NULL;
903 const PKCS7_CTX *ctx = si->ctx;
905 md = EVP_get_digestbyobj(si->digest_alg->algorithm);
909 mctx = EVP_MD_CTX_new();
911 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
915 if (EVP_DigestSignInit_ex(mctx, &pctx, EVP_MD_get0_name(md),
916 ossl_pkcs7_ctx_get0_libctx(ctx),
917 ossl_pkcs7_ctx_get0_propq(ctx), si->pkey,
921 alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf,
922 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
925 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
929 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
931 abuf = OPENSSL_malloc(siglen);
934 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
937 EVP_MD_CTX_free(mctx);
939 ASN1_STRING_set0(si->enc_digest, abuf, siglen);
945 EVP_MD_CTX_free(mctx);
949 /* This partly overlaps with PKCS7_verify(). It does not support flags. */
950 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
951 PKCS7 *p7, PKCS7_SIGNER_INFO *si)
953 PKCS7_ISSUER_AND_SERIAL *ias;
955 STACK_OF(X509) *untrusted;
956 STACK_OF(X509_CRL) *crls;
960 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
964 if (p7->d.ptr == NULL) {
965 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
969 if (PKCS7_type_is_signed(p7)) {
970 untrusted = p7->d.sign->cert;
971 crls = p7->d.sign->crl;
972 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
973 untrusted = p7->d.signed_and_enveloped->cert;
974 crls = p7->d.signed_and_enveloped->crl;
976 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
979 X509_STORE_CTX_set0_crls(ctx, crls);
981 /* XXXXXXXXXXXXXXXXXXXXXXX */
982 ias = si->issuer_and_serial;
984 signer = X509_find_by_issuer_and_serial(untrusted, ias->issuer, ias->serial);
986 /* Were we able to find the signer certificate in passed to us? */
987 if (signer == NULL) {
988 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_CERTIFICATE);
993 if (!X509_STORE_CTX_init(ctx, cert_store, signer, untrusted)) {
994 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
997 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_SMIME_SIGN);
998 i = X509_verify_cert(ctx);
1000 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
1004 return PKCS7_signatureVerify(bio, p7, si, signer);
1009 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
1012 ASN1_OCTET_STRING *os;
1013 EVP_MD_CTX *mdc_tmp, *mdc;
1015 EVP_MD *fetched_md = NULL;
1018 STACK_OF(X509_ATTRIBUTE) *sk;
1021 const PKCS7_CTX *ctx = ossl_pkcs7_get0_ctx(p7);
1022 OSSL_LIB_CTX *libctx = ossl_pkcs7_ctx_get0_libctx(ctx);
1023 const char *propq = ossl_pkcs7_ctx_get0_propq(ctx);
1025 mdc_tmp = EVP_MD_CTX_new();
1026 if (mdc_tmp == NULL) {
1027 ERR_raise(ERR_LIB_PKCS7, ERR_R_EVP_LIB);
1031 if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) {
1032 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_PKCS7_TYPE);
1036 md_type = OBJ_obj2nid(si->digest_alg->algorithm);
1040 if ((btmp == NULL) ||
1041 ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) {
1042 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1045 BIO_get_md_ctx(btmp, &mdc);
1047 ERR_raise(ERR_LIB_PKCS7, ERR_R_INTERNAL_ERROR);
1050 if (EVP_MD_CTX_get_type(mdc) == md_type)
1053 * Workaround for some broken clients that put the signature OID
1054 * instead of the digest OID in digest_alg->algorithm
1056 if (EVP_MD_get_pkey_type(EVP_MD_CTX_get0_md(mdc)) == md_type)
1058 btmp = BIO_next(btmp);
1062 * mdc is the digest ctx that we want, unless there are attributes, in
1063 * which case the digest is the signed attributes
1065 if (!EVP_MD_CTX_copy_ex(mdc_tmp, mdc))
1069 if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) {
1070 unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL;
1071 unsigned int md_len;
1073 ASN1_OCTET_STRING *message_digest;
1075 if (!EVP_DigestFinal_ex(mdc_tmp, md_dat, &md_len))
1077 message_digest = PKCS7_digest_from_attributes(sk);
1078 if (!message_digest) {
1079 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST);
1082 if ((message_digest->length != (int)md_len) ||
1083 (memcmp(message_digest->data, md_dat, md_len))) {
1084 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DIGEST_FAILURE);
1089 (void)ERR_set_mark();
1090 fetched_md = EVP_MD_fetch(libctx, OBJ_nid2sn(md_type), propq);
1092 if (fetched_md != NULL)
1095 md = EVP_get_digestbynid(md_type);
1097 if (md == NULL || !EVP_VerifyInit_ex(mdc_tmp, md, NULL)) {
1098 (void)ERR_clear_last_mark();
1101 (void)ERR_pop_to_mark();
1103 alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf,
1104 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY));
1106 ERR_raise(ERR_LIB_PKCS7, ERR_R_ASN1_LIB);
1110 if (!EVP_VerifyUpdate(mdc_tmp, abuf, alen))
1116 os = si->enc_digest;
1117 pkey = X509_get0_pubkey(signer);
1123 i = EVP_VerifyFinal_ex(mdc_tmp, os->data, os->length, pkey, libctx, propq);
1125 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
1131 EVP_MD_CTX_free(mdc_tmp);
1132 EVP_MD_free(fetched_md);
1136 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx)
1138 STACK_OF(PKCS7_RECIP_INFO) *rsk;
1139 PKCS7_RECIP_INFO *ri;
1142 i = OBJ_obj2nid(p7->type);
1143 if (i != NID_pkcs7_signedAndEnveloped)
1145 if (p7->d.signed_and_enveloped == NULL)
1147 rsk = p7->d.signed_and_enveloped->recipientinfo;
1150 if (sk_PKCS7_RECIP_INFO_num(rsk) <= idx)
1152 ri = sk_PKCS7_RECIP_INFO_value(rsk, idx);
1153 return ri->issuer_and_serial;
1156 ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1158 return get_attribute(si->auth_attr, nid);
1161 ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid)
1163 return get_attribute(si->unauth_attr, nid);
1166 static ASN1_TYPE *get_attribute(const STACK_OF(X509_ATTRIBUTE) *sk, int nid)
1168 int idx = X509at_get_attr_by_NID(sk, nid, -1);
1172 return X509_ATTRIBUTE_get0_type(X509at_get_attr(sk, idx), 0);
1175 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk)
1178 if ((astype = get_attribute(sk, NID_pkcs9_messageDigest)) == NULL)
1180 return astype->value.octet_string;
1183 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
1184 STACK_OF(X509_ATTRIBUTE) *sk)
1188 sk_X509_ATTRIBUTE_pop_free(p7si->auth_attr, X509_ATTRIBUTE_free);
1189 p7si->auth_attr = sk_X509_ATTRIBUTE_dup(sk);
1190 if (p7si->auth_attr == NULL)
1192 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1193 if ((sk_X509_ATTRIBUTE_set(p7si->auth_attr, i,
1194 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1202 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
1203 STACK_OF(X509_ATTRIBUTE) *sk)
1207 sk_X509_ATTRIBUTE_pop_free(p7si->unauth_attr, X509_ATTRIBUTE_free);
1208 p7si->unauth_attr = sk_X509_ATTRIBUTE_dup(sk);
1209 if (p7si->unauth_attr == NULL)
1211 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
1212 if ((sk_X509_ATTRIBUTE_set(p7si->unauth_attr, i,
1213 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1221 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1224 return add_attribute(&(p7si->auth_attr), nid, atrtype, value);
1227 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
1230 return add_attribute(&(p7si->unauth_attr), nid, atrtype, value);
1233 static int add_attribute(STACK_OF(X509_ATTRIBUTE) **sk, int nid, int atrtype,
1236 X509_ATTRIBUTE *attr = NULL;
1239 if ((*sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
1242 if ((attr = X509_ATTRIBUTE_create(nid, atrtype, value)) == NULL)
1244 if (!sk_X509_ATTRIBUTE_push(*sk, attr)) {
1245 X509_ATTRIBUTE_free(attr);
1251 for (i = 0; i < sk_X509_ATTRIBUTE_num(*sk); i++) {
1252 attr = sk_X509_ATTRIBUTE_value(*sk, i);
1253 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr)) == nid) {
1254 X509_ATTRIBUTE_free(attr);
1255 attr = X509_ATTRIBUTE_create(nid, atrtype, value);
1258 if (!sk_X509_ATTRIBUTE_set(*sk, i, attr)) {
1259 X509_ATTRIBUTE_free(attr);