cms_io.c revision 193770
1231858Sbz/* crypto/cms/cms_io.c */
2231858Sbz/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3231858Sbz * project.
4231858Sbz */
5231858Sbz/* ====================================================================
6231858Sbz * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7231858Sbz *
8231858Sbz * Redistribution and use in source and binary forms, with or without
9231858Sbz * modification, are permitted provided that the following conditions
10231858Sbz * are met:
11231858Sbz *
12231858Sbz * 1. Redistributions of source code must retain the above copyright
13231858Sbz *    notice, this list of conditions and the following disclaimer.
14231858Sbz *
15231858Sbz * 2. Redistributions in binary form must reproduce the above copyright
16231858Sbz *    notice, this list of conditions and the following disclaimer in
17231858Sbz *    the documentation and/or other materials provided with the
18231858Sbz *    distribution.
19231858Sbz *
20231858Sbz * 3. All advertising materials mentioning features or use of this
21231858Sbz *    software must display the following acknowledgment:
22231858Sbz *    "This product includes software developed by the OpenSSL Project
23231858Sbz *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24231858Sbz *
25231858Sbz * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26231858Sbz *    endorse or promote products derived from this software without
27231858Sbz *    prior written permission. For written permission, please contact
28231858Sbz *    licensing@OpenSSL.org.
29231858Sbz *
30231858Sbz * 5. Products derived from this software may not be called "OpenSSL"
31231858Sbz *    nor may "OpenSSL" appear in their names without prior written
32231858Sbz *    permission of the OpenSSL Project.
33231858Sbz *
34231858Sbz * 6. Redistributions of any form whatsoever must retain the following
35231858Sbz *    acknowledgment:
36231858Sbz *    "This product includes software developed by the OpenSSL Project
37231858Sbz *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38231858Sbz *
39231858Sbz * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40231858Sbz * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41231858Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42231858Sbz * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43231858Sbz * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44231858Sbz * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45231858Sbz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46231858Sbz * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47231858Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48231858Sbz * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49231858Sbz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50231858Sbz * OF THE POSSIBILITY OF SUCH DAMAGE.
51231858Sbz * ====================================================================
52231858Sbz */
53231858Sbz
54231858Sbz#include <openssl/asn1t.h>
55231858Sbz#include <openssl/x509.h>
56231858Sbz#include <openssl/err.h>
57231858Sbz#include <openssl/pem.h>
58231858Sbz#include "cms.h"
59231858Sbz#include "cms_lcl.h"
60231858Sbz
61231858SbzCMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
62231858Sbz	{
63231858Sbz	return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
64231858Sbz	}
65231858Sbz
66231858Sbzint i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
67231858Sbz	{
68231858Sbz	return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
69231858Sbz	}
70231858Sbz
71231858SbzIMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
72231858Sbz
73231858Sbz/* Callback for int_smime_write_ASN1 */
74231858Sbz
75231858Sbzstatic int cms_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
76231858Sbz					const ASN1_ITEM *it)
77231858Sbz	{
78231858Sbz	CMS_ContentInfo *cms = (CMS_ContentInfo *)val;
79231858Sbz	BIO *tmpbio, *cmsbio;
80231858Sbz	int r = 0;
81231858Sbz
82231858Sbz	if (!(flags & SMIME_DETACHED))
83231858Sbz		{
84231858Sbz		SMIME_crlf_copy(data, out, flags);
85231858Sbz		return 1;
86231858Sbz		}
87231858Sbz
88231858Sbz	/* Let CMS code prepend any needed BIOs */
89231858Sbz
90231858Sbz	cmsbio = CMS_dataInit(cms, out);
91231858Sbz
92231858Sbz	if (!cmsbio)
93231858Sbz		return 0;
94231858Sbz
95231858Sbz	/* Copy data across, passing through filter BIOs for processing */
96231858Sbz	SMIME_crlf_copy(data, cmsbio, flags);
97231858Sbz
98231858Sbz	/* Finalize structure */
99231858Sbz	if (CMS_dataFinal(cms, cmsbio) <= 0)
100231858Sbz		goto err;
101231858Sbz
102231858Sbz	r = 1;
103231858Sbz
104231858Sbz	err:
105231858Sbz
106231858Sbz	/* Now remove any digests prepended to the BIO */
107231858Sbz
108231858Sbz	while (cmsbio != out)
109231858Sbz		{
110231858Sbz		tmpbio = BIO_pop(cmsbio);
111231858Sbz		BIO_free(cmsbio);
112231858Sbz		cmsbio = tmpbio;
113231858Sbz		}
114231858Sbz
115231858Sbz	return 1;
116231858Sbz
117231858Sbz	}
118231858Sbz
119231858Sbz
120231858Sbzint SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
121231858Sbz	{
122231858Sbz	STACK_OF(X509_ALGOR) *mdalgs;
123231858Sbz	int ctype_nid = OBJ_obj2nid(cms->contentType);
124231858Sbz	int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
125231858Sbz	if (ctype_nid == NID_pkcs7_signed)
126231858Sbz		mdalgs = cms->d.signedData->digestAlgorithms;
127231858Sbz	else
128231858Sbz		mdalgs = NULL;
129231858Sbz
130231858Sbz	return int_smime_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags,
131231858Sbz					ctype_nid, econt_nid, mdalgs,
132231858Sbz					cms_output_data,
133231858Sbz					ASN1_ITEM_rptr(CMS_ContentInfo));
134231858Sbz	}
135231858Sbz
136231858SbzCMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
137231858Sbz	{
138231858Sbz	return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont,
139231858Sbz					ASN1_ITEM_rptr(CMS_ContentInfo));
140231858Sbz	}
141231858Sbz