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
)
72 tmpout
= BIO_new(BIO_s_mem());
73 BIO_set_mem_eof_return(tmpout
, 0);
80 CMSerr(CMS_F_CMS_COPY_CONTENT
,ERR_R_MALLOC_FAILURE
);
84 /* Read all content through chain to process digest, decrypt etc */
87 i
=BIO_read(in
,buf
,sizeof(buf
));
90 if (BIO_method_type(in
) == BIO_TYPE_CIPHER
)
92 if (!BIO_get_cipher_status(in
))
100 if (tmpout
&& (BIO_write(tmpout
, buf
, i
) != i
))
106 if(!SMIME_text(tmpout
, out
))
108 CMSerr(CMS_F_CMS_COPY_CONTENT
,CMS_R_SMIME_TEXT_ERROR
);
116 if (tmpout
&& (tmpout
!= out
))
122 static int check_content(CMS_ContentInfo
*cms
)
124 ASN1_OCTET_STRING
**pos
= CMS_get0_content(cms
);
127 CMSerr(CMS_F_CHECK_CONTENT
, CMS_R_NO_CONTENT
);
133 static void do_free_upto(BIO
*f
, BIO
*upto
)
150 int CMS_data(CMS_ContentInfo
*cms
, BIO
*out
, unsigned int flags
)
154 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_data
)
156 CMSerr(CMS_F_CMS_DATA
, CMS_R_TYPE_NOT_DATA
);
159 cont
= CMS_dataInit(cms
, NULL
);
162 r
= cms_copy_content(out
, cont
, flags
);
167 CMS_ContentInfo
*CMS_data_create(BIO
*in
, unsigned int flags
)
169 CMS_ContentInfo
*cms
;
170 cms
= cms_Data_create();
174 if (CMS_final(cms
, in
, NULL
, flags
))
177 CMS_ContentInfo_free(cms
);
182 int CMS_digest_verify(CMS_ContentInfo
*cms
, BIO
*dcont
, BIO
*out
,
187 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_digest
)
189 CMSerr(CMS_F_CMS_DIGEST_VERIFY
, CMS_R_TYPE_NOT_DIGESTED_DATA
);
193 if (!dcont
&& !check_content(cms
))
196 cont
= CMS_dataInit(cms
, dcont
);
199 r
= cms_copy_content(out
, cont
, flags
);
201 r
= cms_DigestedData_do_final(cms
, cont
, 1);
202 do_free_upto(cont
, dcont
);
206 CMS_ContentInfo
*CMS_digest_create(BIO
*in
, const EVP_MD
*md
,
209 CMS_ContentInfo
*cms
;
212 cms
= cms_DigestedData_create(md
);
216 if(!(flags
& CMS_DETACHED
))
218 flags
&= ~CMS_STREAM
;
219 CMS_set_detached(cms
, 0);
222 if ((flags
& CMS_STREAM
) || CMS_final(cms
, in
, NULL
, flags
))
225 CMS_ContentInfo_free(cms
);
229 int CMS_EncryptedData_decrypt(CMS_ContentInfo
*cms
,
230 const unsigned char *key
, size_t keylen
,
231 BIO
*dcont
, BIO
*out
, unsigned int flags
)
235 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_encrypted
)
237 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT
,
238 CMS_R_TYPE_NOT_ENCRYPTED_DATA
);
242 if (!dcont
&& !check_content(cms
))
245 if (CMS_EncryptedData_set1_key(cms
, NULL
, key
, keylen
) <= 0)
247 cont
= CMS_dataInit(cms
, dcont
);
250 r
= cms_copy_content(out
, cont
, flags
);
251 do_free_upto(cont
, dcont
);
255 CMS_ContentInfo
*CMS_EncryptedData_encrypt(BIO
*in
, const EVP_CIPHER
*cipher
,
256 const unsigned char *key
, size_t keylen
,
259 CMS_ContentInfo
*cms
;
262 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT
, CMS_R_NO_CIPHER
);
265 cms
= CMS_ContentInfo_new();
268 if (!CMS_EncryptedData_set1_key(cms
, cipher
, key
, keylen
))
271 if(!(flags
& CMS_DETACHED
))
273 flags
&= ~CMS_STREAM
;
274 CMS_set_detached(cms
, 0);
277 if ((flags
& (CMS_STREAM
|CMS_PARTIAL
))
278 || CMS_final(cms
, in
, NULL
, flags
))
281 CMS_ContentInfo_free(cms
);
285 static int cms_signerinfo_verify_cert(CMS_SignerInfo
*si
,
287 STACK_OF(X509
) *certs
,
288 STACK_OF(X509_CRL
) *crls
,
294 CMS_SignerInfo_get0_algs(si
, NULL
, &signer
, NULL
, NULL
);
295 if (!X509_STORE_CTX_init(&ctx
, store
, signer
, certs
))
297 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT
,
298 CMS_R_STORE_INIT_ERROR
);
301 X509_STORE_CTX_set_default(&ctx
, "smime_sign");
303 X509_STORE_CTX_set0_crls(&ctx
, crls
);
305 i
= X509_verify_cert(&ctx
);
308 j
= X509_STORE_CTX_get_error(&ctx
);
309 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT
,
310 CMS_R_CERTIFICATE_VERIFY_ERROR
);
311 ERR_add_error_data(2, "Verify error:",
312 X509_verify_cert_error_string(j
));
317 X509_STORE_CTX_cleanup(&ctx
);
322 int CMS_verify(CMS_ContentInfo
*cms
, STACK_OF(X509
) *certs
,
323 X509_STORE
*store
, BIO
*dcont
, BIO
*out
, unsigned int flags
)
326 STACK_OF(CMS_SignerInfo
) *sinfos
;
327 STACK_OF(X509
) *cms_certs
= NULL
;
328 STACK_OF(X509_CRL
) *crls
= NULL
;
330 int i
, scount
= 0, ret
= 0;
331 BIO
*cmsbio
= NULL
, *tmpin
= NULL
;
333 if (!dcont
&& !check_content(cms
))
336 /* Attempt to find all signer certificates */
338 sinfos
= CMS_get0_SignerInfos(cms
);
340 if (sk_CMS_SignerInfo_num(sinfos
) <= 0)
342 CMSerr(CMS_F_CMS_VERIFY
, CMS_R_NO_SIGNERS
);
346 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++)
348 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
349 CMS_SignerInfo_get0_algs(si
, NULL
, &signer
, NULL
, NULL
);
354 if (scount
!= sk_CMS_SignerInfo_num(sinfos
))
355 scount
+= CMS_set1_signers_certs(cms
, certs
, flags
);
357 if (scount
!= sk_CMS_SignerInfo_num(sinfos
))
359 CMSerr(CMS_F_CMS_VERIFY
, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND
);
363 /* Attempt to verify all signers certs */
365 if (!(flags
& CMS_NO_SIGNER_CERT_VERIFY
))
367 cms_certs
= CMS_get1_certs(cms
);
368 if (!(flags
& CMS_NOCRL
))
369 crls
= CMS_get1_crls(cms
);
370 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++)
372 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
373 if (!cms_signerinfo_verify_cert(si
, store
,
374 cms_certs
, crls
, flags
))
379 /* Attempt to verify all SignerInfo signed attribute signatures */
381 if (!(flags
& CMS_NO_ATTR_VERIFY
))
383 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++)
385 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
386 if (CMS_signed_get_attr_count(si
) < 0)
388 if (CMS_SignerInfo_verify(si
) <= 0)
393 /* Performance optimization: if the content is a memory BIO then
394 * store its contents in a temporary read only memory BIO. This
395 * avoids potentially large numbers of slow copies of data which will
396 * occur when reading from a read write memory BIO when signatures
400 if (dcont
&& (BIO_method_type(dcont
) == BIO_TYPE_MEM
))
404 len
= BIO_get_mem_data(dcont
, &ptr
);
405 tmpin
= BIO_new_mem_buf(ptr
, len
);
408 CMSerr(CMS_F_CMS_VERIFY
,ERR_R_MALLOC_FAILURE
);
416 cmsbio
=CMS_dataInit(cms
, tmpin
);
420 if (!cms_copy_content(out
, cmsbio
, flags
))
423 if (!(flags
& CMS_NO_CONTENT_VERIFY
))
425 for (i
= 0; i
< sk_CMS_SignerInfo_num(sinfos
); i
++)
427 si
= sk_CMS_SignerInfo_value(sinfos
, i
);
428 if (CMS_SignerInfo_verify_content(si
, cmsbio
) <= 0)
430 CMSerr(CMS_F_CMS_VERIFY
,
431 CMS_R_CONTENT_VERIFY_ERROR
);
441 if (dcont
&& (tmpin
== dcont
))
442 do_free_upto(cmsbio
, dcont
);
444 BIO_free_all(cmsbio
);
447 sk_X509_pop_free(cms_certs
, X509_free
);
449 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
454 int CMS_verify_receipt(CMS_ContentInfo
*rcms
, CMS_ContentInfo
*ocms
,
455 STACK_OF(X509
) *certs
,
456 X509_STORE
*store
, unsigned int flags
)
459 r
= CMS_verify(rcms
, certs
, store
, NULL
, NULL
, flags
);
462 return cms_Receipt_verify(rcms
, ocms
);
465 CMS_ContentInfo
*CMS_sign(X509
*signcert
, EVP_PKEY
*pkey
, STACK_OF(X509
) *certs
,
466 BIO
*data
, unsigned int flags
)
468 CMS_ContentInfo
*cms
;
471 cms
= CMS_ContentInfo_new();
472 if (!cms
|| !CMS_SignedData_init(cms
))
475 if (pkey
&& !CMS_add1_signer(cms
, signcert
, pkey
, NULL
, flags
))
477 CMSerr(CMS_F_CMS_SIGN
, CMS_R_ADD_SIGNER_ERROR
);
481 for (i
= 0; i
< sk_X509_num(certs
); i
++)
483 X509
*x
= sk_X509_value(certs
, i
);
484 if (!CMS_add1_cert(cms
, x
))
488 if(!(flags
& CMS_DETACHED
))
490 flags
&= ~CMS_STREAM
;
491 CMS_set_detached(cms
, 0);
494 if ((flags
& (CMS_STREAM
|CMS_PARTIAL
))
495 || CMS_final(cms
, data
, NULL
, flags
))
501 CMSerr(CMS_F_CMS_SIGN
, ERR_R_MALLOC_FAILURE
);
505 CMS_ContentInfo_free(cms
);
509 CMS_ContentInfo
*CMS_sign_receipt(CMS_SignerInfo
*si
,
510 X509
*signcert
, EVP_PKEY
*pkey
,
511 STACK_OF(X509
) *certs
,
514 CMS_SignerInfo
*rct_si
;
515 CMS_ContentInfo
*cms
= NULL
;
516 ASN1_OCTET_STRING
**pos
, *os
;
517 BIO
*rct_cont
= NULL
;
520 flags
&= ~CMS_STREAM
;
521 /* Not really detached but avoids content being allocated */
522 flags
|= CMS_PARTIAL
|CMS_BINARY
|CMS_DETACHED
;
523 if (!pkey
|| !signcert
)
525 CMSerr(CMS_F_CMS_SIGN_RECEIPT
, CMS_R_NO_KEY_OR_CERT
);
529 /* Initialize signed data */
531 cms
= CMS_sign(NULL
, NULL
, certs
, NULL
, flags
);
535 /* Set inner content type to signed receipt */
536 if (!CMS_set1_eContentType(cms
, OBJ_nid2obj(NID_id_smime_ct_receipt
)))
539 rct_si
= CMS_add1_signer(cms
, signcert
, pkey
, NULL
, flags
);
542 CMSerr(CMS_F_CMS_SIGN_RECEIPT
, CMS_R_ADD_SIGNER_ERROR
);
546 os
= cms_encode_Receipt(si
);
551 /* Set content to digest */
552 rct_cont
= BIO_new_mem_buf(os
->data
, os
->length
);
556 /* Add msgSigDigest attribute */
558 if (!cms_msgSigDigest_add1(rct_si
, si
))
561 /* Finalize structure */
562 if (!CMS_final(cms
, rct_cont
, NULL
, flags
))
565 /* Set embedded content */
566 pos
= CMS_get0_content(cms
);
576 CMS_ContentInfo_free(cms
);
581 CMS_ContentInfo
*CMS_encrypt(STACK_OF(X509
) *certs
, BIO
*data
,
582 const EVP_CIPHER
*cipher
, unsigned int flags
)
584 CMS_ContentInfo
*cms
;
587 cms
= CMS_EnvelopedData_create(cipher
);
590 for (i
= 0; i
< sk_X509_num(certs
); i
++)
592 recip
= sk_X509_value(certs
, i
);
593 if (!CMS_add1_recipient_cert(cms
, recip
, flags
))
595 CMSerr(CMS_F_CMS_ENCRYPT
, CMS_R_RECIPIENT_ERROR
);
600 if(!(flags
& CMS_DETACHED
))
602 flags
&= ~CMS_STREAM
;
603 CMS_set_detached(cms
, 0);
606 if ((flags
& (CMS_STREAM
|CMS_PARTIAL
))
607 || CMS_final(cms
, data
, NULL
, flags
))
613 CMSerr(CMS_F_CMS_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
616 CMS_ContentInfo_free(cms
);
620 int CMS_decrypt_set1_pkey(CMS_ContentInfo
*cms
, EVP_PKEY
*pk
, X509
*cert
)
622 STACK_OF(CMS_RecipientInfo
) *ris
;
623 CMS_RecipientInfo
*ri
;
625 ris
= CMS_get0_RecipientInfos(cms
);
626 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++)
628 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
629 if (CMS_RecipientInfo_type(ri
) != CMS_RECIPINFO_TRANS
)
631 /* If we have a cert try matching RecipientInfo
632 * otherwise try them all.
634 if (!cert
|| (CMS_RecipientInfo_ktri_cert_cmp(ri
, cert
) == 0))
636 CMS_RecipientInfo_set0_pkey(ri
, pk
);
637 r
= CMS_RecipientInfo_decrypt(cms
, ri
);
638 CMS_RecipientInfo_set0_pkey(ri
, NULL
);
643 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY
,
644 CMS_R_DECRYPT_ERROR
);
651 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY
, CMS_R_NO_MATCHING_RECIPIENT
);
656 int CMS_decrypt_set1_key(CMS_ContentInfo
*cms
,
657 unsigned char *key
, size_t keylen
,
658 unsigned char *id
, size_t idlen
)
660 STACK_OF(CMS_RecipientInfo
) *ris
;
661 CMS_RecipientInfo
*ri
;
663 ris
= CMS_get0_RecipientInfos(cms
);
664 for (i
= 0; i
< sk_CMS_RecipientInfo_num(ris
); i
++)
666 ri
= sk_CMS_RecipientInfo_value(ris
, i
);
667 if (CMS_RecipientInfo_type(ri
) != CMS_RECIPINFO_KEK
)
670 /* If we have an id try matching RecipientInfo
671 * otherwise try them all.
673 if (!id
|| (CMS_RecipientInfo_kekri_id_cmp(ri
, id
, idlen
) == 0))
675 CMS_RecipientInfo_set0_key(ri
, key
, keylen
);
676 r
= CMS_RecipientInfo_decrypt(cms
, ri
);
677 CMS_RecipientInfo_set0_key(ri
, NULL
, 0);
682 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY
,
683 CMS_R_DECRYPT_ERROR
);
690 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY
, CMS_R_NO_MATCHING_RECIPIENT
);
695 int CMS_decrypt(CMS_ContentInfo
*cms
, EVP_PKEY
*pk
, X509
*cert
,
696 BIO
*dcont
, BIO
*out
,
701 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_pkcs7_enveloped
)
703 CMSerr(CMS_F_CMS_DECRYPT
, CMS_R_TYPE_NOT_ENVELOPED_DATA
);
706 if (!dcont
&& !check_content(cms
))
708 if (pk
&& !CMS_decrypt_set1_pkey(cms
, pk
, cert
))
711 cont
= CMS_dataInit(cms
, dcont
);
714 r
= cms_copy_content(out
, cont
, flags
);
715 do_free_upto(cont
, dcont
);
719 int CMS_final(CMS_ContentInfo
*cms
, BIO
*data
, BIO
*dcont
, unsigned int flags
)
723 if (!(cmsbio
= CMS_dataInit(cms
, dcont
)))
725 CMSerr(CMS_F_CMS_FINAL
,ERR_R_MALLOC_FAILURE
);
729 SMIME_crlf_copy(data
, cmsbio
, flags
);
731 (void)BIO_flush(cmsbio
);
734 if (!CMS_dataFinal(cms
, cmsbio
))
736 CMSerr(CMS_F_CMS_FINAL
,CMS_R_CMS_DATAFINAL_ERROR
);
743 do_free_upto(cmsbio
, dcont
);
751 int CMS_uncompress(CMS_ContentInfo
*cms
, BIO
*dcont
, BIO
*out
,
756 if (OBJ_obj2nid(CMS_get0_type(cms
)) != NID_id_smime_ct_compressedData
)
758 CMSerr(CMS_F_CMS_UNCOMPRESS
,
759 CMS_R_TYPE_NOT_COMPRESSED_DATA
);
763 if (!dcont
&& !check_content(cms
))
766 cont
= CMS_dataInit(cms
, dcont
);
769 r
= cms_copy_content(out
, cont
, flags
);
770 do_free_upto(cont
, dcont
);
774 CMS_ContentInfo
*CMS_compress(BIO
*in
, int comp_nid
, unsigned int flags
)
776 CMS_ContentInfo
*cms
;
778 comp_nid
= NID_zlib_compression
;
779 cms
= cms_CompressedData_create(comp_nid
);
783 if(!(flags
& CMS_DETACHED
))
785 flags
&= ~CMS_STREAM
;
786 CMS_set_detached(cms
, 0);
789 if (CMS_final(cms
, in
, NULL
, flags
))
792 CMS_ContentInfo_free(cms
);
798 int CMS_uncompress(CMS_ContentInfo
*cms
, BIO
*dcont
, BIO
*out
,
801 CMSerr(CMS_F_CMS_UNCOMPRESS
, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
805 CMS_ContentInfo
*CMS_compress(BIO
*in
, int comp_nid
, unsigned int flags
)
807 CMSerr(CMS_F_CMS_COMPRESS
, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);