3
* (C) 1999-2010 Jack Lloyd
5
* Botan is released under the Simplified BSD License (see license.txt)
8
#ifndef BOTAN_BER_DECODER_H_
9
#define BOTAN_BER_DECODER_H_
11
#include <botan/asn1_obj.h>
12
#include <botan/data_src.h>
19
class BOTAN_PUBLIC_API(2,0) BER_Decoder final
22
BER_Object get_next_object();
24
std::vector<uint8_t> get_next_octet_string();
26
void push_back(const BER_Object& obj);
28
bool more_items() const;
29
BER_Decoder& verify_end();
30
BER_Decoder& discard_remaining();
32
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag = UNIVERSAL);
33
BER_Decoder& end_cons();
35
BER_Decoder& get_next(BER_Object& ber);
38
* Get next object and copy value to POD type
39
* Asserts value length is equal to POD type sizeof.
40
* Asserts Type tag and optional Class tag according to parameters.
41
* Copy value to POD type (struct, union, C-style array, std::array, etc.).
42
* @param out POD type reference where to copy object value
43
* @param type_tag ASN1_Tag enum to assert type on object read
44
* @param class_tag ASN1_Tag enum to assert class on object read (default: CONTEXT_SPECIFIC)
45
* @return this reference
48
BER_Decoder& get_next_value(T &out,
50
ASN1_Tag class_tag = CONTEXT_SPECIFIC)
52
static_assert(std::is_pod<T>::value, "Type must be POD");
54
BER_Object obj = get_next_object();
55
obj.assert_is_a(type_tag, class_tag);
57
if (obj.value.size() != sizeof(T))
58
throw BER_Decoding_Error(
59
"Size mismatch. Object value size is " +
60
std::to_string(obj.value.size()) +
61
"; Output type size is " +
62
std::to_string(sizeof(T)));
64
copy_mem(reinterpret_cast<uint8_t*>(&out), obj.value.data(), obj.value.size());
70
* Save all the bytes remaining in the source
72
template<typename Alloc>
73
BER_Decoder& raw_bytes(std::vector<uint8_t, Alloc>& out)
77
while(m_source->read_byte(buf))
82
BER_Decoder& decode_null();
83
BER_Decoder& decode(bool& v);
84
BER_Decoder& decode(size_t& v);
85
BER_Decoder& decode(class BigInt& v);
88
* BER decode a BIT STRING or OCTET STRING
90
template<typename Alloc>
91
BER_Decoder& decode(std::vector<uint8_t, Alloc>& out, ASN1_Tag real_type)
93
return decode(out, real_type, real_type, UNIVERSAL);
96
BER_Decoder& decode(bool& v,
98
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
100
BER_Decoder& decode(size_t& v,
102
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
104
BER_Decoder& decode(class BigInt& v,
106
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
108
BER_Decoder& decode(std::vector<uint8_t>& v,
111
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
113
BER_Decoder& decode(secure_vector<uint8_t>& v,
116
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
118
BER_Decoder& decode(class ASN1_Object& obj,
119
ASN1_Tag type_tag = NO_OBJECT,
120
ASN1_Tag class_tag = NO_OBJECT);
122
BER_Decoder& decode_octet_string_bigint(class BigInt& b);
124
uint64_t decode_constrained_integer(ASN1_Tag type_tag,
128
template<typename T> BER_Decoder& decode_integer_type(T& out)
130
return decode_integer_type<T>(out, INTEGER, UNIVERSAL);
134
BER_Decoder& decode_integer_type(T& out,
136
ASN1_Tag class_tag = CONTEXT_SPECIFIC)
138
out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
143
BER_Decoder& decode_optional(T& out,
146
const T& default_value = T());
149
BER_Decoder& decode_optional_implicit(
155
const T& default_value = T());
158
BER_Decoder& decode_list(std::vector<T>& out,
159
ASN1_Tag type_tag = SEQUENCE,
160
ASN1_Tag class_tag = UNIVERSAL);
163
BER_Decoder& decode_and_check(const T& expected,
164
const std::string& error_msg)
169
if(actual != expected)
170
throw Decoding_Error(error_msg);
176
* Decode an OPTIONAL string type
178
template<typename Alloc>
179
BER_Decoder& decode_optional_string(std::vector<uint8_t, Alloc>& out,
182
ASN1_Tag class_tag = CONTEXT_SPECIFIC)
184
BER_Object obj = get_next_object();
186
ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
188
if(obj.type_tag == type_tag && obj.class_tag == class_tag)
190
if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
191
BER_Decoder(obj.value).decode(out, real_type).verify_end();
195
decode(out, real_type, type_tag, class_tag);
207
BER_Decoder& operator=(const BER_Decoder&) = delete;
209
explicit BER_Decoder(DataSource&);
211
BER_Decoder(const uint8_t[], size_t);
213
explicit BER_Decoder(const secure_vector<uint8_t>&);
215
explicit BER_Decoder(const std::vector<uint8_t>& vec);
217
BER_Decoder(const BER_Decoder&);
219
BER_Decoder* m_parent;
221
// either m_data_src.get() or an unowned pointer
222
DataSource* m_source;
223
mutable std::unique_ptr<DataSource> m_data_src;
227
* Decode an OPTIONAL or DEFAULT element
230
BER_Decoder& BER_Decoder::decode_optional(T& out,
233
const T& default_value)
235
BER_Object obj = get_next_object();
237
if(obj.type_tag == type_tag && obj.class_tag == class_tag)
239
if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
240
BER_Decoder(obj.value).decode(out).verify_end();
244
decode(out, type_tag, class_tag);
257
* Decode an OPTIONAL or DEFAULT element
260
BER_Decoder& BER_Decoder::decode_optional_implicit(
266
const T& default_value)
268
BER_Object obj = get_next_object();
270
if(obj.type_tag == type_tag && obj.class_tag == class_tag)
272
obj.type_tag = real_type;
273
obj.class_tag = real_class;
275
decode(out, real_type, real_class);
286
* Decode a list of homogenously typed values
289
BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec,
293
BER_Decoder list = start_cons(type_tag, class_tag);
295
while(list.more_items())
299
vec.push_back(value);