]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cms/cms_cd.c
2abc867a59856c3320f748706151c3a002c1584f
[thirdparty/openssl.git] / crypto / cms / cms_cd.c
1 /*
2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include <openssl/bio.h>
17 #include <openssl/comp.h>
18 #include "cms_local.h"
19
20 #ifdef ZLIB
21
22 /* CMS CompressedData Utilities */
23
24 CMS_ContentInfo *cms_CompressedData_create(int comp_nid, OPENSSL_CTX *libctx,
25 const char *propq)
26 {
27 CMS_ContentInfo *cms;
28 CMS_CompressedData *cd;
29
30 /*
31 * Will need something cleverer if there is ever more than one
32 * compression algorithm or parameters have some meaning...
33 */
34 if (comp_nid != NID_zlib_compression) {
35 CMSerr(CMS_F_CMS_COMPRESSEDDATA_CREATE,
36 CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
37 return NULL;
38 }
39 cms = CMS_ContentInfo_new_ex(libctx, propq);
40 if (cms == NULL)
41 return NULL;
42
43 cd = M_ASN1_new_of(CMS_CompressedData);
44
45 if (cd == NULL)
46 goto err;
47
48 cms->contentType = OBJ_nid2obj(NID_id_smime_ct_compressedData);
49 cms->d.compressedData = cd;
50
51 cd->version = 0;
52
53 X509_ALGOR_set0(cd->compressionAlgorithm,
54 OBJ_nid2obj(NID_zlib_compression), V_ASN1_UNDEF, NULL);
55
56 cd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
57
58 return cms;
59
60 err:
61 CMS_ContentInfo_free(cms);
62 return NULL;
63 }
64
65 BIO *cms_CompressedData_init_bio(const CMS_ContentInfo *cms)
66 {
67 CMS_CompressedData *cd;
68 const ASN1_OBJECT *compoid;
69
70 if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_compressedData) {
71 CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
72 CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA);
73 return NULL;
74 }
75 cd = cms->d.compressedData;
76 X509_ALGOR_get0(&compoid, NULL, NULL, cd->compressionAlgorithm);
77 if (OBJ_obj2nid(compoid) != NID_zlib_compression) {
78 CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
79 CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
80 return NULL;
81 }
82 return BIO_new(BIO_f_zlib());
83 }
84
85 #endif