Botan: src/cms/cms_comp.cpp Source File

Botan 1.10.17

cms_comp.cpp
Go to the documentation of this file.
1 /*
2 * CMS Compression
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/cms_enc.h>
9 #include <botan/cms_dec.h>
10 #include <botan/der_enc.h>
11 #include <botan/ber_dec.h>
12 #include <botan/oids.h>
13 #include <botan/pipe.h>
14
15 #if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
16 #include <botan/zlib.h>
17 #endif
18
19 namespace Botan {
20
21 /*
22 * Compress a message
23 */
24 void CMS_Encoder::compress(const std::string& algo)
25 {
26 if(!CMS_Encoder::can_compress_with(algo))
27 throw Invalid_Argument("CMS_Encoder: Cannot compress with " + algo);
28
29 Filter* compressor = 0;
30
31 #if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
32 if(algo == "Zlib") compressor = new Zlib_Compression;
33 #endif
34
35 if(compressor == 0)
36 throw Internal_Error("CMS: Couldn't get ahold of a compressor");
37
38 Pipe pipe(compressor);
39 pipe.process_msg(data);
40 SecureVector<byte> compressed = pipe.read_all();
41
42 DER_Encoder encoder;
43 encoder.start_cons(SEQUENCE).
44 encode(static_cast<size_t>(0)).
45 encode(AlgorithmIdentifier("Compression." + algo,
46 MemoryVector<byte>())).
47 raw_bytes(make_econtent(compressed, type)).
48 end_cons();
49
50 add_layer("CMS.CompressedData", encoder);
51 }
52
53 /*
54 * See if the named compression algo is available
55 */
56 bool CMS_Encoder::can_compress_with(const std::string& algo)
57 {
58 if(algo == "")
59 throw Invalid_Algorithm_Name("Empty string to can_compress_with");
60
61 #if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
62 if(algo == "Zlib")
63 return true;
64 #endif
65
66 return false;
67 }
68
69 /*
70 * Decompress a message
71 */
72 void CMS_Decoder::decompress(BER_Decoder& decoder)
73 {
74 size_t version;
75 AlgorithmIdentifier comp_algo;
76
77 BER_Decoder comp_info = decoder.start_cons(SEQUENCE);
78
79 comp_info.decode(version);
80 if(version != 0)
81 throw Decoding_Error("CMS: Unknown version for CompressedData");
82
83 comp_info.decode(comp_algo);
84 read_econtent(comp_info);
85 comp_info.end_cons();
86
87 Filter* decompressor = 0;
88
89 info = comp_algo.oid.as_string();
90
91 #if defined(BOTAN_HAS_COMPRESSOR_ZLIB)
92 if(comp_algo.oid == OIDS::lookup("Compression.Zlib"))
93 {
94 decompressor = new Zlib_Decompression;
95 info = "Zlib";
96 }
97 #endif
98
99 if(!decompressor)
100 status = FAILURE;
101
102 Pipe pipe(decompressor);
103 pipe.process_msg(data);
104 data = pipe.read_all();
105 }
106
107 }
Botan::CMS_Decoder::FAILURE
Definition: cms_dec.h:24
Botan::Zlib_Compression
Definition: zlib.h:19
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::OID::as_string
std::string as_string() const
Definition: asn1_oid.cpp:50
Botan::Zlib_Decompression
Definition: zlib.h:50
Botan::Internal_Error
Definition: exceptn.h:45
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:238
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::Pipe::read_all
SecureVector< byte > read_all(message_id msg=DEFAULT_MESSAGE)
Definition: pipe_rw.cpp:105
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:252
Botan::Filter
Definition: filter.h:19
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::AlgorithmIdentifier::oid
OID oid
Definition: alg_id.h:35
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan
Definition: algo_base.h:14
Botan::MemoryVector< byte >
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
Botan::Invalid_Algorithm_Name
Definition: exceptn.h:119
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::SecureVector< byte >
Botan::Pipe
Definition: pipe.h:25
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::Pipe::process_msg
void process_msg(const byte in[], size_t length)
Definition: pipe.cpp:116
Botan::CMS_Encoder::can_compress_with
static bool can_compress_with(const std::string &)
Definition: cms_comp.cpp:56
Botan::CMS_Encoder::compress
void compress(const std::string &)
Definition: cms_comp.cpp:24