1183234Ssimon/* crypto/cms/cms_cd.c */
2183234Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3183234Ssimon * project.
4183234Ssimon */
5183234Ssimon/* ====================================================================
6183234Ssimon * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7183234Ssimon *
8183234Ssimon * Redistribution and use in source and binary forms, with or without
9183234Ssimon * modification, are permitted provided that the following conditions
10183234Ssimon * are met:
11183234Ssimon *
12183234Ssimon * 1. Redistributions of source code must retain the above copyright
13183234Ssimon *    notice, this list of conditions and the following disclaimer.
14183234Ssimon *
15183234Ssimon * 2. Redistributions in binary form must reproduce the above copyright
16183234Ssimon *    notice, this list of conditions and the following disclaimer in
17183234Ssimon *    the documentation and/or other materials provided with the
18183234Ssimon *    distribution.
19183234Ssimon *
20183234Ssimon * 3. All advertising materials mentioning features or use of this
21183234Ssimon *    software must display the following acknowledgment:
22183234Ssimon *    "This product includes software developed by the OpenSSL Project
23183234Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24183234Ssimon *
25183234Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26183234Ssimon *    endorse or promote products derived from this software without
27183234Ssimon *    prior written permission. For written permission, please contact
28183234Ssimon *    licensing@OpenSSL.org.
29183234Ssimon *
30183234Ssimon * 5. Products derived from this software may not be called "OpenSSL"
31183234Ssimon *    nor may "OpenSSL" appear in their names without prior written
32183234Ssimon *    permission of the OpenSSL Project.
33183234Ssimon *
34183234Ssimon * 6. Redistributions of any form whatsoever must retain the following
35183234Ssimon *    acknowledgment:
36183234Ssimon *    "This product includes software developed by the OpenSSL Project
37183234Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38183234Ssimon *
39183234Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40183234Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41183234Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42183234Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43183234Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44183234Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45183234Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46183234Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47183234Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48183234Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49183234Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50183234Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
51183234Ssimon * ====================================================================
52183234Ssimon */
53183234Ssimon
54183234Ssimon#include "cryptlib.h"
55183234Ssimon#include <openssl/asn1t.h>
56183234Ssimon#include <openssl/pem.h>
57183234Ssimon#include <openssl/x509v3.h>
58183234Ssimon#include <openssl/err.h>
59183234Ssimon#include <openssl/cms.h>
60183234Ssimon#include <openssl/bio.h>
61246772Sjkim#ifndef OPENSSL_NO_COMP
62183234Ssimon#include <openssl/comp.h>
63246772Sjkim#endif
64183234Ssimon#include "cms_lcl.h"
65183234Ssimon
66183234SsimonDECLARE_ASN1_ITEM(CMS_CompressedData)
67183234Ssimon
68183234Ssimon#ifdef ZLIB
69183234Ssimon
70183234Ssimon/* CMS CompressedData Utilities */
71183234Ssimon
72183234SsimonCMS_ContentInfo *cms_CompressedData_create(int comp_nid)
73183234Ssimon	{
74183234Ssimon	CMS_ContentInfo *cms;
75183234Ssimon	CMS_CompressedData *cd;
76183234Ssimon	/* Will need something cleverer if there is ever more than one
77183234Ssimon	 * compression algorithm or parameters have some meaning...
78183234Ssimon	 */
79183234Ssimon	if (comp_nid != NID_zlib_compression)
80183234Ssimon		{
81183234Ssimon		CMSerr(CMS_F_CMS_COMPRESSEDDATA_CREATE,
82183234Ssimon				CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
83183234Ssimon		return NULL;
84183234Ssimon		}
85183234Ssimon	cms = CMS_ContentInfo_new();
86183234Ssimon	if (!cms)
87183234Ssimon		return NULL;
88183234Ssimon
89183234Ssimon	cd = M_ASN1_new_of(CMS_CompressedData);
90183234Ssimon
91183234Ssimon	if (!cd)
92183234Ssimon		goto err;
93183234Ssimon
94183234Ssimon	cms->contentType = OBJ_nid2obj(NID_id_smime_ct_compressedData);
95183234Ssimon	cms->d.compressedData = cd;
96183234Ssimon
97183234Ssimon	cd->version = 0;
98183234Ssimon
99183234Ssimon	X509_ALGOR_set0(cd->compressionAlgorithm,
100183234Ssimon			OBJ_nid2obj(NID_zlib_compression),
101183234Ssimon			V_ASN1_UNDEF, NULL);
102183234Ssimon
103183234Ssimon	cd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
104183234Ssimon
105183234Ssimon	return cms;
106183234Ssimon
107183234Ssimon	err:
108183234Ssimon
109183234Ssimon	if (cms)
110183234Ssimon		CMS_ContentInfo_free(cms);
111183234Ssimon
112183234Ssimon	return NULL;
113183234Ssimon	}
114183234Ssimon
115183234SsimonBIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms)
116183234Ssimon	{
117183234Ssimon	CMS_CompressedData *cd;
118183234Ssimon	ASN1_OBJECT *compoid;
119183234Ssimon	if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_compressedData)
120183234Ssimon		{
121183234Ssimon		CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
122183234Ssimon				CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA);
123183234Ssimon		return NULL;
124183234Ssimon		}
125183234Ssimon	cd = cms->d.compressedData;
126183234Ssimon	X509_ALGOR_get0(&compoid, NULL, NULL, cd->compressionAlgorithm);
127183234Ssimon	if (OBJ_obj2nid(compoid) != NID_zlib_compression)
128183234Ssimon		{
129183234Ssimon		CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
130183234Ssimon				CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
131183234Ssimon		return NULL;
132183234Ssimon		}
133183234Ssimon	return BIO_new(BIO_f_zlib());
134183234Ssimon	}
135183234Ssimon
136183234Ssimon#endif
137