1 /* crypto/cms/cms_smime.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
55 #include <openssl/asn1t.h>
56 #include <openssl/x509.h>
57 #include <openssl/x509v3.h>
58 #include <openssl/err.h>
59 #include <openssl/cms.h>
62 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
64 unsigned char buf[4096];
69 tmpout = BIO_new(BIO_s_null());
70 else if (flags & CMS_TEXT)
71 tmpout = BIO_new(BIO_s_mem());
77 CMSerr(CMS_F_CMS_COPY_CONTENT,ERR_R_MALLOC_FAILURE);
81 /* Read all content through chain to process digest, decrypt etc */
84 i=BIO_read(in,buf,sizeof(buf));
87 if (BIO_method_type(in) == BIO_TYPE_CIPHER)
89 if (!BIO_get_cipher_status(in))
97 if (tmpout && (BIO_write(tmpout, buf, i) != i))
103 if(!SMIME_text(tmpout, out))
105 CMSerr(CMS_F_CMS_COPY_CONTENT,CMS_R_SMIME_TEXT_ERROR);
113 if (tmpout && (tmpout != out))
119 static int check_content(CMS_ContentInfo *cms)
121 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
124 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
130 static void do_free_upto(BIO *f, BIO *upto)
147 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
151 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data)
153 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
156 cont = CMS_dataInit(cms, NULL);
159 r = cms_copy_content(out, cont, flags);
164 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
166 CMS_ContentInfo *cms;
167 cms = cms_Data_create();
171 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
174 CMS_ContentInfo_free(cms);
179 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
184 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest)
186 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
190 if (!dcont && !check_content(cms))
193 cont = CMS_dataInit(cms, dcont);
196 r = cms_copy_content(out, cont, flags);
198 r = cms_DigestedData_do_final(cms, cont, 1);
199 do_free_upto(cont, dcont);
203 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
206 CMS_ContentInfo *cms;
209 cms = cms_DigestedData_create(md);
213 if(!(flags & CMS_DETACHED))
214 CMS_set_detached(cms, 0);
216 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
219 CMS_ContentInfo_free(cms);
223 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
224 const unsigned char *key, size_t keylen,
225 BIO *dcont, BIO *out, unsigned int flags)
229 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted)
231 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
232 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
236 if (!dcont && !check_content(cms))
239 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
241 cont = CMS_dataInit(cms, dcont);
244 r = cms_copy_content(out, cont, flags);
245 do_free_upto(cont, dcont);
249 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
250 const unsigned char *key, size_t keylen,
253 CMS_ContentInfo *cms;
256 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
259 cms = CMS_ContentInfo_new();
262 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
265 if(!(flags & CMS_DETACHED))
266 CMS_set_detached(cms, 0);
268 if ((flags & (CMS_STREAM|CMS_PARTIAL))
269 || CMS_final(cms, in, NULL, flags))
272 CMS_ContentInfo_free(cms);
276 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
278 STACK_OF(X509) *certs,
279 STACK_OF(X509_CRL) *crls,
285 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
286 if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
288 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
289 CMS_R_STORE_INIT_ERROR);
292 X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
294 X509_STORE_CTX_set0_crls(&ctx, crls);
296 i = X509_verify_cert(&ctx);
299 j = X509_STORE_CTX_get_error(&ctx);
300 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
301 CMS_R_CERTIFICATE_VERIFY_ERROR);
302 ERR_add_error_data(2, "Verify error:",
303 X509_verify_cert_error_string(j));
308 X509_STORE_CTX_cleanup(&ctx);
313 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
314 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
317 STACK_OF(CMS_SignerInfo) *sinfos;
318 STACK_OF(X509) *cms_certs = NULL;
319 STACK_OF(X509_CRL) *crls = NULL;
321 int i, scount = 0, ret = 0;
322 BIO *cmsbio = NULL, *tmpin = NULL;
324 if (!dcont && !check_content(cms))
327 /* Attempt to find all signer certificates */
329 sinfos = CMS_get0_SignerInfos(cms);
331 if (sk_CMS_SignerInfo_num(sinfos) <= 0)
333 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
337 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
339 si = sk_CMS_SignerInfo_value(sinfos, i);
340 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
345 if (scount != sk_CMS_SignerInfo_num(sinfos))
346 scount += CMS_set1_signers_certs(cms, certs, flags);
348 if (scount != sk_CMS_SignerInfo_num(sinfos))
350 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
354 /* Attempt to verify all signers certs */
356 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
358 cms_certs = CMS_get1_certs(cms);
359 if (!(flags & CMS_NOCRL))
360 crls = CMS_get1_crls(cms);
361 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
363 si = sk_CMS_SignerInfo_value(sinfos, i);
364 if (!cms_signerinfo_verify_cert(si, store,
365 cms_certs, crls, flags))
370 /* Attempt to verify all SignerInfo signed attribute signatures */
372 if (!(flags & CMS_NO_ATTR_VERIFY))
374 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
376 si = sk_CMS_SignerInfo_value(sinfos, i);
377 if (CMS_signed_get_attr_count(si) < 0)
379 if (CMS_SignerInfo_verify(si) <= 0)
384 /* Performance optimization: if the content is a memory BIO then
385 * store its contents in a temporary read only memory BIO. This
386 * avoids potentially large numbers of slow copies of data which will
387 * occur when reading from a read write memory BIO when signatures
391 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
395 len = BIO_get_mem_data(dcont, &ptr);
396 tmpin = BIO_new_mem_buf(ptr, len);
399 CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
407 cmsbio=CMS_dataInit(cms, tmpin);
411 if (!cms_copy_content(out, cmsbio, flags))
414 if (!(flags & CMS_NO_CONTENT_VERIFY))
416 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
418 si = sk_CMS_SignerInfo_value(sinfos, i);
419 if (!CMS_SignerInfo_verify_content(si, cmsbio))
421 CMSerr(CMS_F_CMS_VERIFY,
422 CMS_R_CONTENT_VERIFY_ERROR);
432 if (dcont && (tmpin == dcont))
433 do_free_upto(cmsbio, dcont);
435 BIO_free_all(cmsbio);
438 sk_X509_pop_free(cms_certs, X509_free);
440 sk_X509_CRL_pop_free(crls, X509_CRL_free);
445 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
446 STACK_OF(X509) *certs,
447 X509_STORE *store, unsigned int flags)
450 flags &= ~(CMS_DETACHED|CMS_TEXT);
451 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
454 return cms_Receipt_verify(rcms, ocms);
457 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
458 BIO *data, unsigned int flags)
460 CMS_ContentInfo *cms;
463 cms = CMS_ContentInfo_new();
464 if (!cms || !CMS_SignedData_init(cms))
467 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
469 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
473 for (i = 0; i < sk_X509_num(certs); i++)
475 X509 *x = sk_X509_value(certs, i);
476 if (!CMS_add1_cert(cms, x))
480 if(!(flags & CMS_DETACHED))
481 CMS_set_detached(cms, 0);
483 if ((flags & (CMS_STREAM|CMS_PARTIAL))
484 || CMS_final(cms, data, NULL, flags))
490 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
494 CMS_ContentInfo_free(cms);
498 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
499 X509 *signcert, EVP_PKEY *pkey,
500 STACK_OF(X509) *certs,
503 CMS_SignerInfo *rct_si;
504 CMS_ContentInfo *cms = NULL;
505 ASN1_OCTET_STRING **pos, *os;
506 BIO *rct_cont = NULL;
509 flags &= ~(CMS_STREAM|CMS_TEXT);
510 /* Not really detached but avoids content being allocated */
511 flags |= CMS_PARTIAL|CMS_BINARY|CMS_DETACHED;
512 if (!pkey || !signcert)
514 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
518 /* Initialize signed data */
520 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
524 /* Set inner content type to signed receipt */
525 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
528 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
531 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
535 os = cms_encode_Receipt(si);
540 /* Set content to digest */
541 rct_cont = BIO_new_mem_buf(os->data, os->length);
545 /* Add msgSigDigest attribute */
547 if (!cms_msgSigDigest_add1(rct_si, si))
550 /* Finalize structure */
551 if (!CMS_final(cms, rct_cont, NULL, flags))
554 /* Set embedded content */
555 pos = CMS_get0_content(cms);
565 CMS_ContentInfo_free(cms);
570 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
571 const EVP_CIPHER *cipher, unsigned int flags)
573 CMS_ContentInfo *cms;
576 cms = CMS_EnvelopedData_create(cipher);
579 for (i = 0; i < sk_X509_num(certs); i++)
581 recip = sk_X509_value(certs, i);
582 if (!CMS_add1_recipient_cert(cms, recip, flags))
584 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
589 if(!(flags & CMS_DETACHED))
590 CMS_set_detached(cms, 0);
592 if ((flags & (CMS_STREAM|CMS_PARTIAL))
593 || CMS_final(cms, data, NULL, flags))
599 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
602 CMS_ContentInfo_free(cms);
606 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
608 STACK_OF(CMS_RecipientInfo) *ris;
609 CMS_RecipientInfo *ri;
611 ris = CMS_get0_RecipientInfos(cms);
612 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
614 ri = sk_CMS_RecipientInfo_value(ris, i);
615 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_TRANS)
617 /* If we have a cert try matching RecipientInfo
618 * otherwise try them all.
620 if (!cert || (CMS_RecipientInfo_ktri_cert_cmp(ri, cert) == 0))
622 CMS_RecipientInfo_set0_pkey(ri, pk);
623 r = CMS_RecipientInfo_decrypt(cms, ri);
624 CMS_RecipientInfo_set0_pkey(ri, NULL);
629 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
630 CMS_R_DECRYPT_ERROR);
637 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
642 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
643 unsigned char *key, size_t keylen,
644 unsigned char *id, size_t idlen)
646 STACK_OF(CMS_RecipientInfo) *ris;
647 CMS_RecipientInfo *ri;
649 ris = CMS_get0_RecipientInfos(cms);
650 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++)
652 ri = sk_CMS_RecipientInfo_value(ris, i);
653 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
656 /* If we have an id try matching RecipientInfo
657 * otherwise try them all.
659 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0))
661 CMS_RecipientInfo_set0_key(ri, key, keylen);
662 r = CMS_RecipientInfo_decrypt(cms, ri);
663 CMS_RecipientInfo_set0_key(ri, NULL, 0);
668 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY,
669 CMS_R_DECRYPT_ERROR);
676 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
681 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
682 BIO *dcont, BIO *out,
687 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped)
689 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
692 if (!dcont && !check_content(cms))
694 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
697 cont = CMS_dataInit(cms, dcont);
700 r = cms_copy_content(out, cont, flags);
701 do_free_upto(cont, dcont);
705 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
709 if (!(cmsbio = CMS_dataInit(cms, dcont)))
711 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
715 SMIME_crlf_copy(data, cmsbio, flags);
717 (void)BIO_flush(cmsbio);
720 if (!CMS_dataFinal(cms, cmsbio))
722 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
729 do_free_upto(cmsbio, dcont);
737 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
742 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
744 CMSerr(CMS_F_CMS_UNCOMPRESS,
745 CMS_R_TYPE_NOT_COMPRESSED_DATA);
749 if (!dcont && !check_content(cms))
752 cont = CMS_dataInit(cms, dcont);
755 r = cms_copy_content(out, cont, flags);
756 do_free_upto(cont, dcont);
760 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
762 CMS_ContentInfo *cms;
764 comp_nid = NID_zlib_compression;
765 cms = cms_CompressedData_create(comp_nid);
769 if(!(flags & CMS_DETACHED))
770 CMS_set_detached(cms, 0);
772 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
775 CMS_ContentInfo_free(cms);
781 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
784 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
788 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
790 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);