Botan 3.4.0
Crypto and TLS for C&
ber_dec.h
Go to the documentation of this file.
1/*
2* BER Decoder
3* (C) 1999-2010,2018 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_BER_DECODER_H_
9#define BOTAN_BER_DECODER_H_
10
11#include <botan/asn1_obj.h>
12#include <botan/data_src.h>
13#include <botan/mem_ops.h>
14
15namespace Botan {
16
17class BigInt;
18
19/**
20* BER Decoding Object
21*/
23 public:
24 /**
25 * Set up to BER decode the data in buf of length len
26 */
27 BER_Decoder(const uint8_t buf[], size_t len);
28
29 /**
30 * Set up to BER decode the data in buf of length len
31 */
32 BER_Decoder(std::span<const uint8_t> buf) : BER_Decoder(buf.data(), buf.size()) {}
33
34 /**
35 * Set up to BER decode the data in vec
36 */
37 explicit BER_Decoder(const secure_vector<uint8_t>& vec);
38
39 /**
40 * Set up to BER decode the data in vec
41 */
42 explicit BER_Decoder(const std::vector<uint8_t>& vec);
43
44 /**
45 * Set up to BER decode the data in src
46 */
47 explicit BER_Decoder(DataSource& src);
48
49 /**
50 * Set up to BER decode the data in obj
51 */
52 BER_Decoder(const BER_Object& obj) : BER_Decoder(obj.bits(), obj.length()) {}
53
54 /**
55 * Set up to BER decode the data in obj
56 */
57 BER_Decoder(BER_Object&& obj) : BER_Decoder(std::move(obj), nullptr) {}
58
59 BER_Decoder(const BER_Decoder& other);
60
62
63 /**
64 * Get the next object in the data stream.
65 * If EOF, returns an object with type NO_OBJECT.
66 */
67 BER_Object get_next_object();
68
70 ber = get_next_object();
71 return (*this);
72 }
73
74 /**
75 * Push an object back onto the stream. Throws if another
76 * object was previously pushed and has not been subsequently
77 * read out.
78 */
79 void push_back(const BER_Object& obj);
80
81 /**
82 * Push an object back onto the stream. Throws if another
83 * object was previously pushed and has not been subsequently
84 * read out.
85 */
86 void push_back(BER_Object&& obj);
87
88 /**
89 * Return true if there is at least one more item remaining
90 */
91 bool more_items() const;
92
93 /**
94 * Verify the stream is concluded, throws otherwise.
95 * Returns (*this)
96 */
97 BER_Decoder& verify_end();
98
99 /**
100 * Verify the stream is concluded, throws otherwise.
101 * Returns (*this)
102 */
103 BER_Decoder& verify_end(std::string_view err_msg);
104
105 /**
106 * Discard any data that remains unread
107 * Returns (*this)
108 */
109 BER_Decoder& discard_remaining();
110
111 BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag);
112
113 BER_Decoder start_sequence() { return start_cons(ASN1_Type::Sequence, ASN1_Class::Universal); }
114
115 BER_Decoder start_set() { return start_cons(ASN1_Type::Set, ASN1_Class::Universal); }
116
118 return start_cons(ASN1_Type(tag), ASN1_Class::ContextSpecific);
119 }
120
122 return start_cons(ASN1_Type(tag), ASN1_Class::ExplicitContextSpecific);
123 }
124
125 /**
126 * Finish decoding a constructed data, throws if any data remains.
127 * Returns the parent of *this (ie the object on which start_cons was called).
128 */
129 BER_Decoder& end_cons();
130
131 /**
132 * Get next object and copy value to POD type
133 * Asserts value length is equal to POD type sizeof.
134 * Asserts Type tag and optional Class tag according to parameters.
135 * Copy value to POD type (struct, union, C-style array, std::array, etc.).
136 * @param out POD type reference where to copy object value
137 * @param type_tag ASN1_Type enum to assert type on object read
138 * @param class_tag ASN1_Type enum to assert class on object read (default: CONTEXT_SPECIFIC)
139 * @return this reference
140 */
141 template <typename T>
142 BER_Decoder& get_next_value(T& out, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific)
143 requires std::is_standard_layout<T>::value && std::is_trivial<T>::value
144 {
145 BER_Object obj = get_next_object();
146 obj.assert_is_a(type_tag, class_tag);
147
148 if(obj.length() != sizeof(T)) {
149 throw BER_Decoding_Error("Size mismatch. Object value size is " + std::to_string(obj.length()) +
150 "; Output type size is " + std::to_string(sizeof(T)));
151 }
152
153 copy_mem(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
154
155 return (*this);
156 }
157
158 /*
159 * Save all the bytes remaining in the source
160 */
161 template <typename Alloc>
162 BER_Decoder& raw_bytes(std::vector<uint8_t, Alloc>& out) {
163 out.clear();
164 uint8_t buf;
165 while(m_source->read_byte(buf)) {
166 out.push_back(buf);
167 }
168 return (*this);
169 }
170
171 BER_Decoder& decode_null();
172
173 /**
174 * Decode a BER encoded BOOLEAN
175 */
176 BER_Decoder& decode(bool& out) { return decode(out, ASN1_Type::Boolean, ASN1_Class::Universal); }
177
178 /*
179 * Decode a small BER encoded INTEGER
180 */
181 BER_Decoder& decode(size_t& out) { return decode(out, ASN1_Type::Integer, ASN1_Class::Universal); }
182
183 /*
184 * Decode a BER encoded INTEGER
185 */
186 BER_Decoder& decode(BigInt& out) { return decode(out, ASN1_Type::Integer, ASN1_Class::Universal); }
187
188 std::vector<uint8_t> get_next_octet_string() {
189 std::vector<uint8_t> out_vec;
190 decode(out_vec, ASN1_Type::OctetString);
191 return out_vec;
192 }
193
194 /*
195 * BER decode a BIT STRING or OCTET STRING
196 */
197 template <typename Alloc>
198 BER_Decoder& decode(std::vector<uint8_t, Alloc>& out, ASN1_Type real_type) {
199 return decode(out, real_type, real_type, ASN1_Class::Universal);
200 }
201
202 BER_Decoder& decode(bool& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
203
204 BER_Decoder& decode(size_t& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
205
206 BER_Decoder& decode(BigInt& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
207
208 BER_Decoder& decode(std::vector<uint8_t>& v,
209 ASN1_Type real_type,
210 ASN1_Type type_tag,
211 ASN1_Class class_tag = ASN1_Class::ContextSpecific);
212
214 ASN1_Type real_type,
215 ASN1_Type type_tag,
216 ASN1_Class class_tag = ASN1_Class::ContextSpecific);
217
218 BER_Decoder& decode(ASN1_Object& obj,
219 ASN1_Type type_tag = ASN1_Type::NoObject,
220 ASN1_Class class_tag = ASN1_Class::NoObject);
221
222 /**
223 * Decode an integer value which is typed as an octet string
224 */
225 BER_Decoder& decode_octet_string_bigint(BigInt& b);
226
227 uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes);
228
229 template <typename T>
231 return decode_integer_type<T>(out, ASN1_Type::Integer, ASN1_Class::Universal);
232 }
233
234 template <typename T>
235 BER_Decoder& decode_integer_type(T& out, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific) {
236 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
237 return (*this);
238 }
239
240 template <typename T>
241 BER_Decoder& decode_optional(T& out, ASN1_Type type_tag, ASN1_Class class_tag, const T& default_value = T());
242
243 template <typename T>
244 BER_Decoder& decode_optional_implicit(T& out,
245 ASN1_Type type_tag,
246 ASN1_Class class_tag,
247 ASN1_Type real_type,
248 ASN1_Class real_class,
249 const T& default_value = T());
250
251 template <typename T>
252 BER_Decoder& decode_list(std::vector<T>& out,
253 ASN1_Type type_tag = ASN1_Type::Sequence,
254 ASN1_Class class_tag = ASN1_Class::Universal);
255
256 template <typename T>
257 BER_Decoder& decode_and_check(const T& expected, std::string_view error_msg) {
258 T actual;
259 decode(actual);
260
261 if(actual != expected) {
262 throw Decoding_Error(error_msg);
263 }
264
265 return (*this);
266 }
267
268 /*
269 * Decode an OPTIONAL string type
270 */
271 template <typename Alloc>
272 BER_Decoder& decode_optional_string(std::vector<uint8_t, Alloc>& out,
273 ASN1_Type real_type,
274 uint32_t expected_tag,
275 ASN1_Class class_tag = ASN1_Class::ContextSpecific) {
276 BER_Object obj = get_next_object();
277
278 ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
279
280 if(obj.is_a(type_tag, class_tag)) {
281 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
282 BER_Decoder(std::move(obj)).decode(out, real_type).verify_end();
283 } else {
284 push_back(std::move(obj));
285 decode(out, real_type, type_tag, class_tag);
286 }
287 } else {
288 out.clear();
289 push_back(std::move(obj));
290 }
291
292 return (*this);
293 }
294
295 template <typename Alloc>
296 BER_Decoder& decode_optional_string(std::vector<uint8_t, Alloc>& out,
297 ASN1_Type real_type,
298 ASN1_Type expected_tag,
299 ASN1_Class class_tag = ASN1_Class::ContextSpecific) {
300 return decode_optional_string(out, real_type, static_cast<uint32_t>(expected_tag), class_tag);
301 }
302
303 private:
304 BER_Decoder(BER_Object&& obj, BER_Decoder* parent);
305
306 BER_Decoder* m_parent = nullptr;
307 BER_Object m_pushed;
308 // either m_data_src.get() or an unowned pointer
309 DataSource* m_source;
310 mutable std::unique_ptr<DataSource> m_data_src;
311};
312
313/*
314* Decode an OPTIONAL or DEFAULT element
315*/
316template <typename T>
317BER_Decoder& BER_Decoder::decode_optional(T& out, ASN1_Type type_tag, ASN1_Class class_tag, const T& default_value) {
319
320 if(obj.is_a(type_tag, class_tag)) {
321 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
322 BER_Decoder(std::move(obj)).decode(out).verify_end();
323 } else {
324 push_back(std::move(obj));
325 decode(out, type_tag, class_tag);
326 }
327 } else {
328 out = default_value;
329 push_back(std::move(obj));
330 }
331
332 return (*this);
333}
334
335/*
336* Decode an OPTIONAL or DEFAULT element
337*/
338template <typename T>
340 ASN1_Type type_tag,
341 ASN1_Class class_tag,
342 ASN1_Type real_type,
343 ASN1_Class real_class,
344 const T& default_value) {
346
347 if(obj.is_a(type_tag, class_tag)) {
348 obj.set_tagging(real_type, real_class);
349 push_back(std::move(obj));
350 decode(out, real_type, real_class);
351 } else {
352 // Not what we wanted, push it back on the stream
353 out = default_value;
354 push_back(std::move(obj));
355 }
356
357 return (*this);
358}
359
360/*
361* Decode a list of homogenously typed values
362*/
363template <typename T>
364BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, ASN1_Type type_tag, ASN1_Class class_tag) {
365 BER_Decoder list = start_cons(type_tag, class_tag);
366
367 while(list.more_items()) {
368 T value;
369 list.decode(value);
370 vec.push_back(std::move(value));
371 }
372
373 list.end_cons();
374
375 return (*this);
376}
377
378} // namespace Botan
379
380#endif
BER_Decoder start_set()
Definition ber_dec.h:115
BER_Decoder & get_next_value(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:142
BER_Decoder(const uint8_t buf[], size_t len)
Definition ber_dec.cpp:321
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:272
BER_Object get_next_object()
Definition ber_dec.cpp:231
BER_Decoder & decode(bool &out)
Definition ber_dec.h:176
bool more_items() const
Definition ber_dec.cpp:195
BER_Decoder & decode(BigInt &out)
Definition ber_dec.h:186
BER_Decoder(BER_Object &&obj)
Definition ber_dec.h:57
BER_Decoder & decode_optional_string(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:296
BER_Decoder & raw_bytes(std::vector< uint8_t, Alloc > &out)
Definition ber_dec.h:162
BER_Decoder & decode_integer_type(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:235
std::vector< uint8_t > get_next_octet_string()
Definition ber_dec.h:188
BER_Decoder & verify_end()
Definition ber_dec.cpp:205
BER_Decoder & decode(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type)
Definition ber_dec.h:198
BER_Decoder start_explicit_context_specific(uint32_t tag)
Definition ber_dec.h:121
BER_Decoder & end_cons()
Definition ber_dec.cpp:295
BER_Decoder(std::span< const uint8_t > buf)
Definition ber_dec.h:32
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition ber_dec.cpp:286
BER_Decoder(const BER_Object &obj)
Definition ber_dec.h:52
BER_Decoder & decode_list(std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.h:364
BER_Decoder start_sequence()
Definition ber_dec.h:113
BER_Decoder start_context_specific(uint32_t tag)
Definition ber_dec.h:117
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:317
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:257
BER_Decoder & decode(size_t &out)
Definition ber_dec.h:181
BER_Decoder & decode_optional_string(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:272
BER_Decoder & decode_integer_type(T &out)
Definition ber_dec.h:230
BER_Decoder & decode_optional_implicit(T &out, ASN1_Type type_tag, ASN1_Class class_tag, ASN1_Type real_type, ASN1_Class real_class, const T &default_value=T())
Definition ber_dec.h:339
BER_Decoder & operator=(const BER_Decoder &)=delete
BER_Decoder & get_next(BER_Object &ber)
Definition ber_dec.h:69
size_t length() const
Definition asn1_obj.h:152
const uint8_t * bits() const
Definition asn1_obj.h:150
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
Definition asn1_obj.cpp:61
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr="object") const
Definition asn1_obj.cpp:29
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
FE_25519 T
Definition ge.cpp:34
ASN1_Class
Definition asn1_obj.h:28
ASN1_Type
Definition asn1_obj.h:43
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146