Botan: src/cms/cms_dec.cpp Source File

Botan 1.10.17

cms_dec.cpp
Go to the documentation of this file.
1 /*
2 * CMS Decoding
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/cms_dec.h>
9 #include <botan/ber_dec.h>
10 #include <botan/asn1_int.h>
11 #include <botan/oids.h>
12 #include <botan/pem.h>
13
14 namespace Botan {
15
16 /*
17 * CMS_Decoder Constructor
18 */
19 CMS_Decoder::CMS_Decoder(DataSource& in, const X509_Store& x509store,
20 Private_Key* key) :
21 store(x509store)
22 {
23 status = GOOD;
24
25 add_key(key);
26
27 if(ASN1::maybe_BER(in) && !PEM_Code::matches(in))
28 initial_read(in);
29 else
30 {
31 DataSource_Memory ber(PEM_Code::decode_check_label(in, "PKCS7"));
32 initial_read(ber);
33 }
34 }
35
36 /*
37 * Read the outermost ContentInfo
38 */
39 void CMS_Decoder::initial_read(DataSource&)
40 {
41 // FIXME...
42
43 /*
44 BER_Decoder decoder(in);
45 BER_Decoder content_info = decoder.start_cons(SEQUENCE);
46
47 content_info.decode(next_type);
48
49
50 BER_Decoder content_type = BER::get_subsequence(content_info, ASN1_Tag(0));
51 data = content_type.get_remaining();
52 */
53
54 decode_layer();
55 }
56
57 /*
58 * Add another private key to use
59 */
60 void CMS_Decoder::add_key(Private_Key* key)
61 {
62 if(!key)
63 return;
64
65 #if 0
66 for(u32bit j = 0; j != keys.size(); j++)
67 if(keys[j]->key_id() == key->key_id())
68 return;
69 #endif
70
71 keys.push_back(key);
72 }
73
74 /*
75 * Return the status information
76 */
77 CMS_Decoder::Status CMS_Decoder::layer_status() const
78 {
79 return status;
80 }
81
82 /*
83 * Return the final data content
84 */
85 std::string CMS_Decoder::get_data() const
86 {
87 if(layer_type() != DATA)
88 throw Invalid_State("CMS: Cannot retrieve data from non-DATA layer");
89
90 return std::string(reinterpret_cast<const char*>(&data[0]),
91 data.size());
92 }
93
94 /*
95 * Return the content type of this layer
96 */
97 CMS_Decoder::Content_Type CMS_Decoder::layer_type() const
98 {
99 if(type == OIDS::lookup("CMS.DataContent")) return DATA;
100 if(type == OIDS::lookup("CMS.EnvelopedData")) return ENVELOPED;
101 if(type == OIDS::lookup("CMS.CompressedData")) return COMPRESSED;
102 if(type == OIDS::lookup("CMS.SignedData")) return SIGNED;
103 if(type == OIDS::lookup("CMS.AuthenticatedData")) return AUTHENTICATED;
104 if(type == OIDS::lookup("CMS.DigestedData")) return DIGESTED;
105 return UNKNOWN;
106 }
107
108 /*
109 * Return some information about this layer
110 */
111 std::string CMS_Decoder::layer_info() const
112 {
113 return info;
114 }
115
116 /*
117 * Return some information about this layer
118 */
119 void CMS_Decoder::read_econtent(BER_Decoder& decoder)
120 {
121 BER_Decoder econtent_info = decoder.start_cons(SEQUENCE);
122 econtent_info.decode(next_type);
123
124 // FIXME
125 //BER_Decoder econtent = BER::get_subsequence(econtent_info, ASN1_Tag(0));
126 //econtent.decode(data, OCTET_STRING);
127 }
128
129 }
Botan::CMS_Decoder::DIGESTED
Definition: cms_dec.h:27
Botan::CMS_Decoder::SIGNED
Definition: cms_dec.h:26
Botan::CMS_Decoder::layer_type
Content_Type layer_type() const
Definition: cms_dec.cpp:97
Botan::Private_Key
Definition: pk_keys.h:86
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::X509_Store
Definition: x509stor.h:48
Botan::CMS_Decoder::Status
Status
Definition: cms_dec.h:24
Botan::CMS_Decoder::ENVELOPED
Definition: cms_dec.h:26
Botan::Invalid_State
Definition: exceptn.h:25
Botan::CMS_Decoder::Content_Type
Content_Type
Definition: cms_dec.h:26
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_int.cpp:55
Botan::DataSource
Definition: data_src.h:20
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:238
Botan::CMS_Decoder::COMPRESSED
Definition: cms_dec.h:26
Botan::CMS_Decoder::GOOD
Definition: cms_dec.h:24
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::CMS_Decoder::layer_info
std::string layer_info() const
Definition: cms_dec.cpp:111
Botan::CMS_Decoder::AUTHENTICATED
Definition: cms_dec.h:27
Botan::CMS_Decoder::UNKNOWN
Definition: cms_dec.h:26
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::CMS_Decoder::get_data
std::string get_data() const
Definition: cms_dec.cpp:85
Botan::CMS_Decoder::layer_status
Status layer_status() const
Definition: cms_dec.cpp:77
Botan::CMS_Decoder::add_key
void add_key(Private_Key *)
Definition: cms_dec.cpp:60
Botan
Definition: algo_base.h:14
Botan::PEM_Code::matches
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:116
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::CMS_Decoder::CMS_Decoder
CMS_Decoder(DataSource &, const X509_Store &, Private_Key *=0)
Definition: cms_dec.cpp:19
Botan::CMS_Decoder::DATA
Definition: cms_dec.h:26
Botan::DataSource_Memory
Definition: data_src.h:94
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::PEM_Code::decode_check_label
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:42
Botan::u32bit
unsigned int u32bit
Definition: types.h:32