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];
70 tmpout = BIO_new(BIO_s_mem());
73 CMSerr(CMS_F_CMS_COPY_CONTENT,ERR_R_MALLOC_FAILURE);
80 /* Read all content through chain to determine content digests */
83 i=BIO_read(in,buf,sizeof(buf));
87 BIO_write(tmpout, buf, i);
92 if(!SMIME_text(tmpout, out))
94 CMSerr(CMS_F_CMS_COPY_CONTENT,CMS_R_SMIME_TEXT_ERROR);
102 if (tmpout && (tmpout != out))
108 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
112 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data)
114 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
117 cont = CMS_dataInit(cms, NULL);
120 r = cms_copy_content(out, cont, flags);
125 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
127 CMS_ContentInfo *cms;
128 cms = cms_Data_create();
132 if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
135 CMS_ContentInfo_free(cms);
140 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
145 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest)
147 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
153 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
156 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_NO_CONTENT);
161 cont = CMS_dataInit(cms, dcont);
164 r = cms_copy_content(out, cont, flags);
166 r = cms_DigestedData_do_final(cms, cont, 1);
171 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
174 CMS_ContentInfo *cms;
177 cms = cms_DigestedData_create(md);
181 if(!(flags & CMS_DETACHED))
182 CMS_set_detached(cms, 0);
184 if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
187 CMS_ContentInfo_free(cms);
191 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
192 const unsigned char *key, size_t keylen,
193 BIO *dcont, BIO *out, unsigned int flags)
197 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted)
199 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
200 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
206 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
209 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
215 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
217 cont = CMS_dataInit(cms, dcont);
220 r = cms_copy_content(out, cont, flags);
225 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
226 const unsigned char *key, size_t keylen,
229 CMS_ContentInfo *cms;
232 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
235 cms = CMS_ContentInfo_new();
238 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
241 if(!(flags & CMS_DETACHED))
242 CMS_set_detached(cms, 0);
244 if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, in, flags))
247 CMS_ContentInfo_free(cms);
251 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
253 STACK_OF(X509) *certs,
254 STACK_OF(X509_CRL) *crls,
260 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
261 if (!X509_STORE_CTX_init(&ctx, store, signer, certs))
263 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
264 CMS_R_STORE_INIT_ERROR);
267 X509_STORE_CTX_set_purpose(&ctx, X509_PURPOSE_SMIME_SIGN);
269 X509_STORE_CTX_set0_crls(&ctx, crls);
271 i = X509_verify_cert(&ctx);
274 j = X509_STORE_CTX_get_error(&ctx);
275 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
276 CMS_R_CERTIFICATE_VERIFY_ERROR);
277 ERR_add_error_data(2, "Verify error:",
278 X509_verify_cert_error_string(j));
283 X509_STORE_CTX_cleanup(&ctx);
288 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
289 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
292 STACK_OF(CMS_SignerInfo) *sinfos;
293 STACK_OF(X509) *cms_certs = NULL;
294 STACK_OF(X509_CRL) *crls = NULL;
296 int i, scount = 0, ret = 0;
297 BIO *cmsbio = NULL, *tmpin = NULL;
301 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
304 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_CONTENT);
309 /* Attempt to find all signer certificates */
311 sinfos = CMS_get0_SignerInfos(cms);
313 if (sk_CMS_SignerInfo_num(sinfos) <= 0)
315 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
319 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
321 si = sk_CMS_SignerInfo_value(sinfos, i);
322 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
327 if (scount != sk_CMS_SignerInfo_num(sinfos))
328 scount += CMS_set1_signers_certs(cms, certs, flags);
330 if (scount != sk_CMS_SignerInfo_num(sinfos))
332 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
336 /* Attempt to verify all signers certs */
338 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY))
340 cms_certs = CMS_get1_certs(cms);
341 crls = CMS_get1_crls(cms);
342 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
344 si = sk_CMS_SignerInfo_value(sinfos, i);
345 if (!cms_signerinfo_verify_cert(si, store,
346 cms_certs, crls, flags))
351 /* Attempt to verify all SignerInfo signed attribute signatures */
353 if (!(flags & CMS_NO_ATTR_VERIFY))
355 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
357 si = sk_CMS_SignerInfo_value(sinfos, i);
358 if (CMS_signed_get_attr_count(si) < 0)
360 if (CMS_SignerInfo_verify(si) <= 0)
365 /* Performance optimization: if the content is a memory BIO then
366 * store its contents in a temporary read only memory BIO. This
367 * avoids potentially large numbers of slow copies of data which will
368 * occur when reading from a read write memory BIO when signatures
372 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM))
376 len = BIO_get_mem_data(dcont, &ptr);
377 tmpin = BIO_new_mem_buf(ptr, len);
380 CMSerr(CMS_F_CMS_VERIFY,ERR_R_MALLOC_FAILURE);
388 cmsbio=CMS_dataInit(cms, tmpin);
392 if (!cms_copy_content(out, cmsbio, flags))
395 if (!(flags & CMS_NO_CONTENT_VERIFY))
397 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++)
399 si = sk_CMS_SignerInfo_value(sinfos, i);
400 if (!CMS_SignerInfo_verify_content(si, cmsbio))
402 CMSerr(CMS_F_CMS_VERIFY,
403 CMS_R_CONTENT_VERIFY_ERROR);
413 if (dcont && (tmpin == dcont))
415 BIO_free_all(cmsbio);
418 sk_X509_pop_free(cms_certs, X509_free);
420 sk_X509_CRL_pop_free(crls, X509_CRL_free);
425 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
426 BIO *data, unsigned int flags)
428 CMS_ContentInfo *cms;
430 cms = CMS_ContentInfo_new();
433 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags))
435 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
438 for (i = 0; i < sk_X509_num(certs); i++)
440 X509 *x = sk_X509_value(certs, i);
441 if (!CMS_add1_cert(cms, x))
444 /* If no signer or certs initialize signedData */
445 if (!pkey && !i && !CMS_SignedData_init(cms))
448 if(!(flags & CMS_DETACHED))
449 CMS_set_detached(cms, 0);
451 if ((flags & (CMS_STREAM|CMS_PARTIAL)) || CMS_final(cms, data, flags))
457 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
461 CMS_ContentInfo_free(cms);
465 /* Placeholders for now... */
467 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *in,
468 const EVP_CIPHER *cipher, unsigned int flags)
473 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert, BIO *data,
479 int CMS_final(CMS_ContentInfo *cms, BIO *data, int flags)
483 if (!(cmsbio = CMS_dataInit(cms, NULL)))
485 CMSerr(CMS_F_CMS_FINAL,ERR_R_MALLOC_FAILURE);
489 SMIME_crlf_copy(data, cmsbio, flags);
491 (void)BIO_flush(cmsbio);
494 if (!CMS_dataFinal(cms, cmsbio))
496 CMSerr(CMS_F_CMS_FINAL,CMS_R_CMS_DATAFINAL_ERROR);
503 BIO_free_all(cmsbio);
511 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
516 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData)
518 CMSerr(CMS_F_CMS_UNCOMPRESS,
519 CMS_R_TYPE_NOT_COMPRESSED_DATA);
525 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
528 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_NO_CONTENT);
533 cont = CMS_dataInit(cms, dcont);
536 r = cms_copy_content(out, cont, flags);
541 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
543 CMS_ContentInfo *cms;
545 comp_nid = NID_zlib_compression;
546 cms = cms_CompressedData_create(comp_nid);
550 if(!(flags & CMS_DETACHED))
551 CMS_set_detached(cms, 0);
553 if ((flags & CMS_STREAM) || CMS_final(cms, in, flags))
556 CMS_ContentInfo_free(cms);
562 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
565 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
569 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
571 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);