1 /* $OpenBSD: cms_smime.c,v 1.24 2019/10/04 18:03:56 tb Exp $ */
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
56 #include <openssl/asn1t.h>
57 #include <openssl/x509.h>
58 #include <openssl/x509v3.h>
59 #include <openssl/err.h>
60 #include <openssl/cms.h>
62 #include "asn1/asn1_locl.h"
65 cms_get_text_bio(BIO *out, unsigned int flags)
70 rbio = BIO_new(BIO_s_null());
71 else if (flags & CMS_TEXT) {
72 rbio = BIO_new(BIO_s_mem());
73 BIO_set_mem_eof_return(rbio, 0);
81 cms_copy_content(BIO *out, BIO *in, unsigned int flags)
83 unsigned char buf[4096];
87 tmpout = cms_get_text_bio(out, flags);
90 CMSerror(ERR_R_MALLOC_FAILURE);
94 /* Read all content through chain to process digest, decrypt etc */
96 i = BIO_read(in, buf, sizeof(buf));
98 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
99 if (!BIO_get_cipher_status(in))
107 if (tmpout && (BIO_write(tmpout, buf, i) != i))
111 if (flags & CMS_TEXT) {
112 if (!SMIME_text(tmpout, out)) {
113 CMSerror(CMS_R_SMIME_TEXT_ERROR);
128 check_content(CMS_ContentInfo *cms)
130 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
133 CMSerror(CMS_R_NO_CONTENT);
141 do_free_upto(BIO *f, BIO *upto)
150 while (f && f != upto);
156 CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
161 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
162 CMSerror(CMS_R_TYPE_NOT_DATA);
165 cont = CMS_dataInit(cms, NULL);
168 r = cms_copy_content(out, cont, flags);
175 CMS_data_create(BIO *in, unsigned int flags)
177 CMS_ContentInfo *cms;
179 cms = cms_Data_create();
183 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
186 CMS_ContentInfo_free(cms);
192 CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)
197 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
198 CMSerror(CMS_R_TYPE_NOT_DIGESTED_DATA);
202 if (!dcont && !check_content(cms))
205 cont = CMS_dataInit(cms, dcont);
208 r = cms_copy_content(out, cont, flags);
210 r = cms_DigestedData_do_final(cms, cont, 1);
211 do_free_upto(cont, dcont);
217 CMS_digest_create(BIO *in, const EVP_MD *md, unsigned int flags)
219 CMS_ContentInfo *cms;
223 cms = cms_DigestedData_create(md);
227 if (!(flags & CMS_DETACHED))
228 CMS_set_detached(cms, 0);
230 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
233 CMS_ContentInfo_free(cms);
239 CMS_EncryptedData_decrypt(CMS_ContentInfo *cms, const unsigned char *key,
240 size_t keylen, BIO *dcont, BIO *out, unsigned int flags)
245 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
246 CMSerror(CMS_R_TYPE_NOT_ENCRYPTED_DATA);
250 if (!dcont && !check_content(cms))
253 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
255 cont = CMS_dataInit(cms, dcont);
258 r = cms_copy_content(out, cont, flags);
259 do_free_upto(cont, dcont);
265 CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
266 const unsigned char *key, size_t keylen, unsigned int flags)
268 CMS_ContentInfo *cms;
271 CMSerror(CMS_R_NO_CIPHER);
274 cms = CMS_ContentInfo_new();
277 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
280 if (!(flags & CMS_DETACHED))
281 CMS_set_detached(cms, 0);
283 if ((flags & (CMS_STREAM | CMS_PARTIAL)) ||
284 CMS_final(cms, in, NULL, flags))
287 CMS_ContentInfo_free(cms);
293 cms_signerinfo_verify_cert(CMS_SignerInfo *si, X509_STORE *store,
294 STACK_OF(X509) *certs, STACK_OF(X509_CRL) *crls)
296 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
301 CMSerror(ERR_R_MALLOC_FAILURE);
304 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
305 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
306 CMSerror(CMS_R_STORE_INIT_ERROR);
309 X509_STORE_CTX_set_default(ctx, "smime_sign");
311 X509_STORE_CTX_set0_crls(ctx, crls);
313 i = X509_verify_cert(ctx);
315 j = X509_STORE_CTX_get_error(ctx);
316 CMSerror(CMS_R_CERTIFICATE_VERIFY_ERROR);
317 ERR_asprintf_error_data("Verify error: %s",
318 X509_verify_cert_error_string(j));
324 X509_STORE_CTX_free(ctx);
330 CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs, X509_STORE *store,
331 BIO *dcont, BIO *out, unsigned int flags)
334 STACK_OF(CMS_SignerInfo) *sinfos;
335 STACK_OF(X509) *cms_certs = NULL;
336 STACK_OF(X509_CRL) *crls = NULL;
338 int i, scount = 0, ret = 0;
339 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
341 if (!dcont && !check_content(cms))
343 if (dcont && !(flags & CMS_BINARY)) {
344 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
345 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
346 flags |= CMS_ASCIICRLF;
349 /* Attempt to find all signer certificates */
351 sinfos = CMS_get0_SignerInfos(cms);
352 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
353 CMSerror(CMS_R_NO_SIGNERS);
357 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
358 si = sk_CMS_SignerInfo_value(sinfos, i);
359 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
364 if (scount != sk_CMS_SignerInfo_num(sinfos))
365 scount += CMS_set1_signers_certs(cms, certs, flags);
367 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
368 CMSerror(CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
372 /* Attempt to verify all signers certs */
374 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
375 cms_certs = CMS_get1_certs(cms);
376 if (!(flags & CMS_NOCRL))
377 crls = CMS_get1_crls(cms);
378 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
379 si = sk_CMS_SignerInfo_value(sinfos, i);
380 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls))
385 /* Attempt to verify all SignerInfo signed attribute signatures */
387 if (!(flags & CMS_NO_ATTR_VERIFY)) {
388 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
389 si = sk_CMS_SignerInfo_value(sinfos, i);
390 if (CMS_signed_get_attr_count(si) < 0)
392 if (CMS_SignerInfo_verify(si) <= 0)
398 * Performance optimization: if the content is a memory BIO then store
399 * its contents in a temporary read only memory BIO. This avoids
400 * potentially large numbers of slow copies of data which will occur when
401 * reading from a read write memory BIO when signatures are calculated.
404 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
408 len = BIO_get_mem_data(dcont, &ptr);
409 tmpin = BIO_new_mem_buf(ptr, len);
411 CMSerror(ERR_R_MALLOC_FAILURE);
418 * If not binary mode and detached generate digests by *writing* through
419 * the BIO. That makes it possible to canonicalise the input.
421 if (!(flags & SMIME_BINARY) && dcont) {
423 * Create output BIO so we can either handle text or to ensure
424 * included content doesn't override detached content.
426 tmpout = cms_get_text_bio(out, flags);
428 CMSerror(ERR_R_MALLOC_FAILURE);
431 cmsbio = CMS_dataInit(cms, tmpout);
435 * Don't use SMIME_TEXT for verify: it adds headers and we want to
438 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
440 if (flags & CMS_TEXT) {
441 if (!SMIME_text(tmpout, out)) {
442 CMSerror(CMS_R_SMIME_TEXT_ERROR);
447 cmsbio = CMS_dataInit(cms, tmpin);
451 if (!cms_copy_content(out, cmsbio, flags))
455 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
456 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
457 si = sk_CMS_SignerInfo_value(sinfos, i);
458 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
459 CMSerror(CMS_R_CONTENT_VERIFY_ERROR);
468 if (!(flags & SMIME_BINARY) && dcont) {
469 do_free_upto(cmsbio, tmpout);
473 if (dcont && (tmpin == dcont))
474 do_free_upto(cmsbio, dcont);
476 BIO_free_all(cmsbio);
480 BIO_free_all(tmpout);
483 sk_X509_pop_free(cms_certs, X509_free);
484 sk_X509_CRL_pop_free(crls, X509_CRL_free);
490 CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
491 STACK_OF(X509) *certs, X509_STORE *store, unsigned int flags)
495 flags &= ~(CMS_DETACHED | CMS_TEXT);
496 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
500 return cms_Receipt_verify(rcms, ocms);
504 CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, BIO *data,
507 CMS_ContentInfo *cms;
510 cms = CMS_ContentInfo_new();
511 if (cms == NULL || !CMS_SignedData_init(cms))
513 if (flags & CMS_ASCIICRLF &&
514 !CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
517 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
518 CMSerror(CMS_R_ADD_SIGNER_ERROR);
522 for (i = 0; i < sk_X509_num(certs); i++) {
523 X509 *x = sk_X509_value(certs, i);
524 if (!CMS_add1_cert(cms, x))
528 if (!(flags & CMS_DETACHED))
529 CMS_set_detached(cms, 0);
531 if ((flags & (CMS_STREAM | CMS_PARTIAL)) ||
532 CMS_final(cms, data, NULL, flags))
538 CMSerror(ERR_R_MALLOC_FAILURE);
541 CMS_ContentInfo_free(cms);
547 CMS_sign_receipt(CMS_SignerInfo *si, X509 *signcert, EVP_PKEY *pkey,
548 STACK_OF(X509) *certs, unsigned int flags)
550 CMS_SignerInfo *rct_si;
551 CMS_ContentInfo *cms = NULL;
552 ASN1_OCTET_STRING **pos, *os;
553 BIO *rct_cont = NULL;
556 flags &= ~(CMS_STREAM | CMS_TEXT);
557 /* Not really detached but avoids content being allocated */
558 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
559 if (!pkey || !signcert) {
560 CMSerror(CMS_R_NO_KEY_OR_CERT);
564 /* Initialize signed data */
566 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
570 /* Set inner content type to signed receipt */
571 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
574 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
576 CMSerror(CMS_R_ADD_SIGNER_ERROR);
580 os = cms_encode_Receipt(si);
584 /* Set content to digest */
585 rct_cont = BIO_new_mem_buf(os->data, os->length);
589 /* Add msgSigDigest attribute */
591 if (!cms_msgSigDigest_add1(rct_si, si))
594 /* Finalize structure */
595 if (!CMS_final(cms, rct_cont, NULL, flags))
598 /* Set embedded content */
599 pos = CMS_get0_content(cms);
608 CMS_ContentInfo_free(cms);
614 CMS_encrypt(STACK_OF(X509) *certs, BIO *data, const EVP_CIPHER *cipher,
617 CMS_ContentInfo *cms;
621 cms = CMS_EnvelopedData_create(cipher);
624 for (i = 0; i < sk_X509_num(certs); i++) {
625 recip = sk_X509_value(certs, i);
626 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
627 CMSerror(CMS_R_RECIPIENT_ERROR);
632 if (!(flags & CMS_DETACHED))
633 CMS_set_detached(cms, 0);
635 if ((flags & (CMS_STREAM | CMS_PARTIAL)) ||
636 CMS_final(cms, data, NULL, flags))
642 CMSerror(ERR_R_MALLOC_FAILURE);
644 CMS_ContentInfo_free(cms);
650 cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri, EVP_PKEY *pk,
654 STACK_OF(CMS_RecipientEncryptedKey) *reks;
655 CMS_RecipientEncryptedKey *rek;
657 reks = CMS_RecipientInfo_kari_get0_reks(ri);
658 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
661 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
662 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
664 CMS_RecipientInfo_kari_set0_pkey(ri, pk);
665 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
666 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
669 return cert == NULL ? 0 : -1;
676 CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
678 STACK_OF(CMS_RecipientInfo) *ris;
679 CMS_RecipientInfo *ri;
681 int debug = 0, match_ri = 0;
683 ris = CMS_get0_RecipientInfos(cms);
685 debug = cms->d.envelopedData->encryptedContentInfo->debug;
686 ri_type = cms_pkey_get_ri_type(pk);
687 if (ri_type == CMS_RECIPINFO_NONE) {
688 CMSerror(CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
692 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
693 ri = sk_CMS_RecipientInfo_value(ris, i);
694 if (CMS_RecipientInfo_type(ri) != ri_type)
697 if (ri_type == CMS_RECIPINFO_AGREE) {
698 r = cms_kari_set1_pkey(cms, ri, pk, cert);
705 * If we have a cert try matching RecipientInfo otherwise try them
708 else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
710 CMS_RecipientInfo_set0_pkey(ri, pk);
711 r = CMS_RecipientInfo_decrypt(cms, ri);
712 CMS_RecipientInfo_set0_pkey(ri, NULL);
715 * If not debugging clear any error and return success to
716 * avoid leaking of information useful to MMA
724 CMSerror(CMS_R_DECRYPT_ERROR);
728 * If no cert and not debugging don't leave loop after first
729 * successful decrypt. Always attempt to decrypt all recipients
730 * to avoid leaking timing of a successful decrypt.
732 else if (r > 0 && debug)
736 /* If no cert, key transport and not debugging always return success */
737 if (cert == NULL && ri_type == CMS_RECIPINFO_TRANS && match_ri && !debug) {
742 CMSerror(CMS_R_NO_MATCHING_RECIPIENT);
748 CMS_decrypt_set1_key(CMS_ContentInfo *cms, unsigned char *key, size_t keylen,
749 const unsigned char *id, size_t idlen)
751 STACK_OF(CMS_RecipientInfo) *ris;
752 CMS_RecipientInfo *ri;
755 ris = CMS_get0_RecipientInfos(cms);
756 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
757 ri = sk_CMS_RecipientInfo_value(ris, i);
758 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
762 * If we have an id try matching RecipientInfo otherwise try them
765 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
766 CMS_RecipientInfo_set0_key(ri, key, keylen);
767 r = CMS_RecipientInfo_decrypt(cms, ri);
768 CMS_RecipientInfo_set0_key(ri, NULL, 0);
772 CMSerror(CMS_R_DECRYPT_ERROR);
779 CMSerror(CMS_R_NO_MATCHING_RECIPIENT);
785 CMS_decrypt_set1_password(CMS_ContentInfo *cms, unsigned char *pass,
788 STACK_OF(CMS_RecipientInfo) *ris;
789 CMS_RecipientInfo *ri;
792 ris = CMS_get0_RecipientInfos(cms);
793 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
794 ri = sk_CMS_RecipientInfo_value(ris, i);
795 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
797 CMS_RecipientInfo_set0_password(ri, pass, passlen);
798 r = CMS_RecipientInfo_decrypt(cms, ri);
799 CMS_RecipientInfo_set0_password(ri, NULL, 0);
804 CMSerror(CMS_R_NO_MATCHING_RECIPIENT);
810 CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, BIO *dcont,
811 BIO *out, unsigned int flags)
816 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
817 CMSerror(CMS_R_TYPE_NOT_ENVELOPED_DATA);
820 if (!dcont && !check_content(cms))
822 if (flags & CMS_DEBUG_DECRYPT)
823 cms->d.envelopedData->encryptedContentInfo->debug = 1;
825 cms->d.envelopedData->encryptedContentInfo->debug = 0;
827 cms->d.envelopedData->encryptedContentInfo->havenocert = 1;
829 cms->d.envelopedData->encryptedContentInfo->havenocert = 0;
830 if (!pk && !cert && !dcont && !out)
832 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
834 cont = CMS_dataInit(cms, dcont);
837 r = cms_copy_content(out, cont, flags);
838 do_free_upto(cont, dcont);
844 CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
849 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
850 CMSerror(CMS_R_CMS_LIB);
854 SMIME_crlf_copy(data, cmsbio, flags);
856 (void)BIO_flush(cmsbio);
858 if (!CMS_dataFinal(cms, cmsbio)) {
859 CMSerror(CMS_R_CMS_DATAFINAL_ERROR);
866 do_free_upto(cmsbio, dcont);
874 CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)
879 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
880 CMSerror(CMS_R_TYPE_NOT_COMPRESSED_DATA);
884 if (!dcont && !check_content(cms))
887 cont = CMS_dataInit(cms, dcont);
890 r = cms_copy_content(out, cont, flags);
891 do_free_upto(cont, dcont);
897 CMS_compress(BIO *in, int comp_nid, unsigned int flags)
899 CMS_ContentInfo *cms;
902 comp_nid = NID_zlib_compression;
903 cms = cms_CompressedData_create(comp_nid);
907 if (!(flags & CMS_DETACHED))
908 CMS_set_detached(cms, 0);
910 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
913 CMS_ContentInfo_free(cms);
921 CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)
923 CMSerror(CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
928 CMS_compress(BIO *in, int comp_nid, unsigned int flags)
930 CMSerror(CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);