2 * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include "cms_local.h"
17 #include "crypto/asn1.h"
19 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
23 rbio = BIO_new(BIO_s_null());
24 else if (flags & CMS_TEXT) {
25 rbio = BIO_new(BIO_s_mem());
26 BIO_set_mem_eof_return(rbio, 0);
32 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
34 unsigned char buf[4096];
38 tmpout = cms_get_text_bio(out, flags);
41 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
45 /* Read all content through chain to process digest, decrypt etc */
47 i = BIO_read(in, buf, sizeof(buf));
49 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
50 if (!BIO_get_cipher_status(in))
58 if (tmpout && (BIO_write(tmpout, buf, i) != i))
62 if (flags & CMS_TEXT) {
63 if (!SMIME_text(tmpout, out)) {
64 CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
78 static int check_content(CMS_ContentInfo *cms)
80 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
82 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
88 static void do_free_upto(BIO *f, BIO *upto)
97 while (f && f != upto);
102 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
106 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
107 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
110 cont = CMS_dataInit(cms, NULL);
113 r = cms_copy_content(out, cont, flags);
118 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
120 CMS_ContentInfo *cms;
121 cms = cms_Data_create();
125 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
128 CMS_ContentInfo_free(cms);
133 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
138 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
139 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
143 if (!dcont && !check_content(cms))
146 cont = CMS_dataInit(cms, dcont);
149 r = cms_copy_content(out, cont, flags);
151 r = cms_DigestedData_do_final(cms, cont, 1);
152 do_free_upto(cont, dcont);
156 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
159 CMS_ContentInfo *cms;
162 cms = cms_DigestedData_create(md);
166 if (!(flags & CMS_DETACHED))
167 CMS_set_detached(cms, 0);
169 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
172 CMS_ContentInfo_free(cms);
176 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
177 const unsigned char *key, size_t keylen,
178 BIO *dcont, BIO *out, unsigned int flags)
182 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
183 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
184 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
188 if (!dcont && !check_content(cms))
191 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
193 cont = CMS_dataInit(cms, dcont);
196 r = cms_copy_content(out, cont, flags);
197 do_free_upto(cont, dcont);
201 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
202 const unsigned char *key,
203 size_t keylen, unsigned int flags)
205 CMS_ContentInfo *cms;
207 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
210 cms = CMS_ContentInfo_new();
213 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
216 if (!(flags & CMS_DETACHED))
217 CMS_set_detached(cms, 0);
219 if ((flags & (CMS_STREAM | CMS_PARTIAL))
220 || CMS_final(cms, in, NULL, flags))
223 CMS_ContentInfo_free(cms);
227 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
229 STACK_OF(X509) *certs,
230 STACK_OF(X509_CRL) *crls)
232 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
237 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
240 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
241 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
242 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
245 X509_STORE_CTX_set_default(ctx, "smime_sign");
247 X509_STORE_CTX_set0_crls(ctx, crls);
249 i = X509_verify_cert(ctx);
251 j = X509_STORE_CTX_get_error(ctx);
252 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
253 CMS_R_CERTIFICATE_VERIFY_ERROR);
254 ERR_add_error_data(2, "Verify error:",
255 X509_verify_cert_error_string(j));
260 X509_STORE_CTX_free(ctx);
265 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
266 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
269 STACK_OF(CMS_SignerInfo) *sinfos;
270 STACK_OF(X509) *cms_certs = NULL;
271 STACK_OF(X509_CRL) *crls = NULL;
273 int i, scount = 0, ret = 0;
274 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
276 if (!dcont && !check_content(cms))
278 if (dcont && !(flags & CMS_BINARY)) {
279 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
280 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
281 flags |= CMS_ASCIICRLF;
284 /* Attempt to find all signer certificates */
286 sinfos = CMS_get0_SignerInfos(cms);
288 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
289 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
293 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
294 si = sk_CMS_SignerInfo_value(sinfos, i);
295 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
300 if (scount != sk_CMS_SignerInfo_num(sinfos))
301 scount += CMS_set1_signers_certs(cms, certs, flags);
303 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
304 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
308 /* Attempt to verify all signers certs */
310 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
311 cms_certs = CMS_get1_certs(cms);
312 if (!(flags & CMS_NOCRL))
313 crls = CMS_get1_crls(cms);
314 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
315 si = sk_CMS_SignerInfo_value(sinfos, i);
316 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls))
321 /* Attempt to verify all SignerInfo signed attribute signatures */
323 if (!(flags & CMS_NO_ATTR_VERIFY)) {
324 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
325 si = sk_CMS_SignerInfo_value(sinfos, i);
326 if (CMS_signed_get_attr_count(si) < 0)
328 if (CMS_SignerInfo_verify(si) <= 0)
334 * Performance optimization: if the content is a memory BIO then store
335 * its contents in a temporary read only memory BIO. This avoids
336 * potentially large numbers of slow copies of data which will occur when
337 * reading from a read write memory BIO when signatures are calculated.
340 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
343 len = BIO_get_mem_data(dcont, &ptr);
344 tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
346 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
352 * If not binary mode and detached generate digests by *writing* through
353 * the BIO. That makes it possible to canonicalise the input.
355 if (!(flags & SMIME_BINARY) && dcont) {
357 * Create output BIO so we can either handle text or to ensure
358 * included content doesn't override detached content.
360 tmpout = cms_get_text_bio(out, flags);
362 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
365 cmsbio = CMS_dataInit(cms, tmpout);
369 * Don't use SMIME_TEXT for verify: it adds headers and we want to
372 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
374 if (flags & CMS_TEXT) {
375 if (!SMIME_text(tmpout, out)) {
376 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
381 cmsbio = CMS_dataInit(cms, tmpin);
385 if (!cms_copy_content(out, cmsbio, flags))
389 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
390 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
391 si = sk_CMS_SignerInfo_value(sinfos, i);
392 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
393 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
402 if (!(flags & SMIME_BINARY) && dcont) {
403 do_free_upto(cmsbio, tmpout);
407 if (dcont && (tmpin == dcont))
408 do_free_upto(cmsbio, dcont);
410 BIO_free_all(cmsbio);
414 BIO_free_all(tmpout);
417 sk_X509_pop_free(cms_certs, X509_free);
418 sk_X509_CRL_pop_free(crls, X509_CRL_free);
423 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
424 STACK_OF(X509) *certs,
425 X509_STORE *store, unsigned int flags)
428 flags &= ~(CMS_DETACHED | CMS_TEXT);
429 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
432 return cms_Receipt_verify(rcms, ocms);
435 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
436 STACK_OF(X509) *certs, BIO *data,
439 CMS_ContentInfo *cms;
442 cms = CMS_ContentInfo_new();
443 if (cms == NULL || !CMS_SignedData_init(cms))
445 if (flags & CMS_ASCIICRLF
446 && !CMS_set1_eContentType(cms,
447 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
450 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
451 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
455 for (i = 0; i < sk_X509_num(certs); i++) {
456 X509 *x = sk_X509_value(certs, i);
457 if (!CMS_add1_cert(cms, x))
461 if (!(flags & CMS_DETACHED))
462 CMS_set_detached(cms, 0);
464 if ((flags & (CMS_STREAM | CMS_PARTIAL))
465 || CMS_final(cms, data, NULL, flags))
471 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
474 CMS_ContentInfo_free(cms);
478 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
479 X509 *signcert, EVP_PKEY *pkey,
480 STACK_OF(X509) *certs, unsigned int flags)
482 CMS_SignerInfo *rct_si;
483 CMS_ContentInfo *cms = NULL;
484 ASN1_OCTET_STRING **pos, *os;
485 BIO *rct_cont = NULL;
488 flags &= ~(CMS_STREAM | CMS_TEXT);
489 /* Not really detached but avoids content being allocated */
490 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
491 if (!pkey || !signcert) {
492 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
496 /* Initialize signed data */
498 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
502 /* Set inner content type to signed receipt */
503 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
506 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
508 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
512 os = cms_encode_Receipt(si);
517 /* Set content to digest */
518 rct_cont = BIO_new_mem_buf(os->data, os->length);
522 /* Add msgSigDigest attribute */
524 if (!cms_msgSigDigest_add1(rct_si, si))
527 /* Finalize structure */
528 if (!CMS_final(cms, rct_cont, NULL, flags))
531 /* Set embedded content */
532 pos = CMS_get0_content(cms);
541 CMS_ContentInfo_free(cms);
546 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
547 const EVP_CIPHER *cipher, unsigned int flags)
549 CMS_ContentInfo *cms;
552 cms = CMS_EnvelopedData_create(cipher);
555 for (i = 0; i < sk_X509_num(certs); i++) {
556 recip = sk_X509_value(certs, i);
557 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
558 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
563 if (!(flags & CMS_DETACHED))
564 CMS_set_detached(cms, 0);
566 if ((flags & (CMS_STREAM | CMS_PARTIAL))
567 || CMS_final(cms, data, NULL, flags))
573 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
575 CMS_ContentInfo_free(cms);
579 static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
580 EVP_PKEY *pk, X509 *cert)
583 STACK_OF(CMS_RecipientEncryptedKey) *reks;
584 CMS_RecipientEncryptedKey *rek;
585 reks = CMS_RecipientInfo_kari_get0_reks(ri);
586 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
588 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
589 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
591 CMS_RecipientInfo_kari_set0_pkey(ri, pk);
592 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
593 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
596 return cert == NULL ? 0 : -1;
601 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
603 STACK_OF(CMS_RecipientInfo) *ris;
604 CMS_RecipientInfo *ri;
606 int debug = 0, match_ri = 0;
607 ris = CMS_get0_RecipientInfos(cms);
609 debug = cms->d.envelopedData->encryptedContentInfo->debug;
610 ri_type = cms_pkey_get_ri_type(pk);
611 if (ri_type == CMS_RECIPINFO_NONE) {
612 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
613 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
617 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
618 ri = sk_CMS_RecipientInfo_value(ris, i);
619 if (CMS_RecipientInfo_type(ri) != ri_type)
622 if (ri_type == CMS_RECIPINFO_AGREE) {
623 r = cms_kari_set1_pkey(cms, ri, pk, cert);
630 * If we have a cert try matching RecipientInfo otherwise try them
633 else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
635 CMS_RecipientInfo_set0_pkey(ri, pk);
636 r = CMS_RecipientInfo_decrypt(cms, ri);
637 CMS_RecipientInfo_set0_pkey(ri, NULL);
640 * If not debugging clear any error and return success to
641 * avoid leaking of information useful to MMA
649 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
653 * If no cert and not debugging don't leave loop after first
654 * successful decrypt. Always attempt to decrypt all recipients
655 * to avoid leaking timing of a successful decrypt.
657 else if (r > 0 && debug)
661 /* If no cert, key transport and not debugging always return success */
662 if (cert == NULL && ri_type == CMS_RECIPINFO_TRANS && match_ri && !debug) {
667 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
672 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
673 unsigned char *key, size_t keylen,
674 const unsigned char *id, size_t idlen)
676 STACK_OF(CMS_RecipientInfo) *ris;
677 CMS_RecipientInfo *ri;
679 ris = CMS_get0_RecipientInfos(cms);
680 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
681 ri = sk_CMS_RecipientInfo_value(ris, i);
682 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
686 * If we have an id try matching RecipientInfo otherwise try them
689 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
690 CMS_RecipientInfo_set0_key(ri, key, keylen);
691 r = CMS_RecipientInfo_decrypt(cms, ri);
692 CMS_RecipientInfo_set0_key(ri, NULL, 0);
696 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
703 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
708 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
709 unsigned char *pass, ossl_ssize_t passlen)
711 STACK_OF(CMS_RecipientInfo) *ris;
712 CMS_RecipientInfo *ri;
714 ris = CMS_get0_RecipientInfos(cms);
715 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
716 ri = sk_CMS_RecipientInfo_value(ris, i);
717 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
719 CMS_RecipientInfo_set0_password(ri, pass, passlen);
720 r = CMS_RecipientInfo_decrypt(cms, ri);
721 CMS_RecipientInfo_set0_password(ri, NULL, 0);
726 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
731 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
732 BIO *dcont, BIO *out, unsigned int flags)
736 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
737 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
740 if (!dcont && !check_content(cms))
742 if (flags & CMS_DEBUG_DECRYPT)
743 cms->d.envelopedData->encryptedContentInfo->debug = 1;
745 cms->d.envelopedData->encryptedContentInfo->debug = 0;
747 cms->d.envelopedData->encryptedContentInfo->havenocert = 1;
749 cms->d.envelopedData->encryptedContentInfo->havenocert = 0;
750 if (!pk && !cert && !dcont && !out)
752 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
754 cont = CMS_dataInit(cms, dcont);
757 r = cms_copy_content(out, cont, flags);
758 do_free_upto(cont, dcont);
762 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
767 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
768 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
772 SMIME_crlf_copy(data, cmsbio, flags);
774 (void)BIO_flush(cmsbio);
776 if (!CMS_dataFinal(cms, cmsbio)) {
777 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
784 do_free_upto(cmsbio, dcont);
792 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
797 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
798 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
802 if (!dcont && !check_content(cms))
805 cont = CMS_dataInit(cms, dcont);
808 r = cms_copy_content(out, cont, flags);
809 do_free_upto(cont, dcont);
813 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
815 CMS_ContentInfo *cms;
817 comp_nid = NID_zlib_compression;
818 cms = cms_CompressedData_create(comp_nid);
822 if (!(flags & CMS_DETACHED))
823 CMS_set_detached(cms, 0);
825 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
828 CMS_ContentInfo_free(cms);
834 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
837 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
841 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
843 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);