1183234Ssimon/* crypto/cms/cms_cd.c */
2280297Sjkim/*
3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4183234Ssimon * project.
5183234Ssimon */
6183234Ssimon/* ====================================================================
7183234Ssimon * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8183234Ssimon *
9183234Ssimon * Redistribution and use in source and binary forms, with or without
10183234Ssimon * modification, are permitted provided that the following conditions
11183234Ssimon * are met:
12183234Ssimon *
13183234Ssimon * 1. Redistributions of source code must retain the above copyright
14280297Sjkim *    notice, this list of conditions and the following disclaimer.
15183234Ssimon *
16183234Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17183234Ssimon *    notice, this list of conditions and the following disclaimer in
18183234Ssimon *    the documentation and/or other materials provided with the
19183234Ssimon *    distribution.
20183234Ssimon *
21183234Ssimon * 3. All advertising materials mentioning features or use of this
22183234Ssimon *    software must display the following acknowledgment:
23183234Ssimon *    "This product includes software developed by the OpenSSL Project
24183234Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25183234Ssimon *
26183234Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27183234Ssimon *    endorse or promote products derived from this software without
28183234Ssimon *    prior written permission. For written permission, please contact
29183234Ssimon *    licensing@OpenSSL.org.
30183234Ssimon *
31183234Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32183234Ssimon *    nor may "OpenSSL" appear in their names without prior written
33183234Ssimon *    permission of the OpenSSL Project.
34183234Ssimon *
35183234Ssimon * 6. Redistributions of any form whatsoever must retain the following
36183234Ssimon *    acknowledgment:
37183234Ssimon *    "This product includes software developed by the OpenSSL Project
38183234Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39183234Ssimon *
40183234Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41183234Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42183234Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43183234Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44183234Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45183234Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46183234Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47183234Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48183234Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49183234Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50183234Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51183234Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52183234Ssimon * ====================================================================
53183234Ssimon */
54183234Ssimon
55183234Ssimon#include "cryptlib.h"
56183234Ssimon#include <openssl/asn1t.h>
57183234Ssimon#include <openssl/pem.h>
58183234Ssimon#include <openssl/x509v3.h>
59183234Ssimon#include <openssl/err.h>
60183234Ssimon#include <openssl/cms.h>
61183234Ssimon#include <openssl/bio.h>
62246772Sjkim#ifndef OPENSSL_NO_COMP
63280297Sjkim# include <openssl/comp.h>
64246772Sjkim#endif
65183234Ssimon#include "cms_lcl.h"
66183234Ssimon
67183234SsimonDECLARE_ASN1_ITEM(CMS_CompressedData)
68183234Ssimon
69183234Ssimon#ifdef ZLIB
70183234Ssimon
71183234Ssimon/* CMS CompressedData Utilities */
72183234Ssimon
73183234SsimonCMS_ContentInfo *cms_CompressedData_create(int comp_nid)
74280297Sjkim{
75280297Sjkim    CMS_ContentInfo *cms;
76280297Sjkim    CMS_CompressedData *cd;
77280297Sjkim    /*
78280297Sjkim     * Will need something cleverer if there is ever more than one
79280297Sjkim     * compression algorithm or parameters have some meaning...
80280297Sjkim     */
81280297Sjkim    if (comp_nid != NID_zlib_compression) {
82280297Sjkim        CMSerr(CMS_F_CMS_COMPRESSEDDATA_CREATE,
83280297Sjkim               CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
84280297Sjkim        return NULL;
85280297Sjkim    }
86280297Sjkim    cms = CMS_ContentInfo_new();
87280297Sjkim    if (!cms)
88280297Sjkim        return NULL;
89183234Ssimon
90280297Sjkim    cd = M_ASN1_new_of(CMS_CompressedData);
91183234Ssimon
92280297Sjkim    if (!cd)
93280297Sjkim        goto err;
94183234Ssimon
95280297Sjkim    cms->contentType = OBJ_nid2obj(NID_id_smime_ct_compressedData);
96280297Sjkim    cms->d.compressedData = cd;
97183234Ssimon
98280297Sjkim    cd->version = 0;
99183234Ssimon
100280297Sjkim    X509_ALGOR_set0(cd->compressionAlgorithm,
101280297Sjkim                    OBJ_nid2obj(NID_zlib_compression), V_ASN1_UNDEF, NULL);
102183234Ssimon
103280297Sjkim    cd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
104183234Ssimon
105280297Sjkim    return cms;
106183234Ssimon
107280297Sjkim err:
108183234Ssimon
109280297Sjkim    if (cms)
110280297Sjkim        CMS_ContentInfo_free(cms);
111183234Ssimon
112280297Sjkim    return NULL;
113280297Sjkim}
114183234Ssimon
115183234SsimonBIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms)
116280297Sjkim{
117280297Sjkim    CMS_CompressedData *cd;
118280297Sjkim    ASN1_OBJECT *compoid;
119280297Sjkim    if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_compressedData) {
120280297Sjkim        CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
121280297Sjkim               CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA);
122280297Sjkim        return NULL;
123280297Sjkim    }
124280297Sjkim    cd = cms->d.compressedData;
125280297Sjkim    X509_ALGOR_get0(&compoid, NULL, NULL, cd->compressionAlgorithm);
126280297Sjkim    if (OBJ_obj2nid(compoid) != NID_zlib_compression) {
127280297Sjkim        CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
128280297Sjkim               CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
129280297Sjkim        return NULL;
130280297Sjkim    }
131280297Sjkim    return BIO_new(BIO_f_zlib());
132280297Sjkim}
133183234Ssimon
134183234Ssimon#endif
135