Botan 3.11.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/secmem.h>
13#include <cstring>
14#include <memory>
15#include <optional>
16
17namespace Botan {
18
19class BigInt;
20class DataSource;
21
22/**
23* BER Decoding Object
24*/
25class BOTAN_PUBLIC_API(2, 0) BER_Decoder final {
26 public:
27 /**
28 * Set up to BER decode the data in buf of length len
29 */
30 BER_Decoder(const uint8_t buf[], size_t len);
31
32 /**
33 * Set up to BER decode the data in buf of length len
34 */
35 BOTAN_FUTURE_EXPLICIT BER_Decoder(std::span<const uint8_t> buf) : BER_Decoder(buf.data(), buf.size()) {}
36
37 /**
38 * Set up to BER decode the data in vec
39 */
40 explicit BER_Decoder(const secure_vector<uint8_t>& vec);
41
42 /**
43 * Set up to BER decode the data in vec
44 */
45 explicit BER_Decoder(const std::vector<uint8_t>& vec);
46
47 /**
48 * Set up to BER decode the data in src
49 */
50 explicit BER_Decoder(DataSource& src);
51
52 /**
53 * Set up to BER decode the data in obj
54 */
55 BOTAN_FUTURE_EXPLICIT BER_Decoder(const BER_Object& obj) : BER_Decoder(obj.bits(), obj.length()) {}
56
57 /**
58 * Set up to BER decode the data in obj
59 */
60 BOTAN_FUTURE_EXPLICIT BER_Decoder(BER_Object&& obj) : BER_Decoder(std::move(obj), nullptr) {}
61
62 BER_Decoder(const BER_Decoder& other);
63 BER_Decoder(BER_Decoder&& other) noexcept;
64
67
68 /**
69 * Get the next object in the data stream.
70 * If EOF, returns an object with type NO_OBJECT.
71 */
73
75 ber = get_next_object();
76 return (*this);
77 }
78
79 /**
80 * Peek at the next object without removing it from the stream
81 *
82 * If an object has been pushed, then it returns that object.
83 * Otherwise it reads the next object and pushes it. Thus, a you
84 * call peek_next_object followed by push_back without a
85 * subsequent read, it will fail.
86 */
87 const BER_Object& peek_next_object();
88
89 /**
90 * Push an object back onto the stream. Throws if another
91 * object was previously pushed and has not been subsequently
92 * read out.
93 */
94 void push_back(const BER_Object& obj);
95
96 /**
97 * Push an object back onto the stream. Throws if another
98 * object was previously pushed and has not been subsequently
99 * read out.
100 */
101 void push_back(BER_Object&& obj);
102
103 /**
104 * Return true if there is at least one more item remaining
105 */
106 bool more_items() const;
107
108 /**
109 * Verify the stream is concluded, throws otherwise.
110 * Returns (*this)
111 */
112 BER_Decoder& verify_end();
113
114 /**
115 * Verify the stream is concluded, throws otherwise.
116 * Returns (*this)
117 */
118 BER_Decoder& verify_end(std::string_view err_msg);
119
120 /**
121 * Discard any data that remains unread
122 * Returns (*this)
123 */
124 BER_Decoder& discard_remaining();
125
126 BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag);
127
129
131
135
139
140 /**
141 * Finish decoding a constructed data, throws if any data remains.
142 * Returns the parent of *this (ie the object on which start_cons was called).
143 */
144 BER_Decoder& end_cons();
145
146 /**
147 * Get next object and copy value to POD type
148 * Asserts value length is equal to POD type sizeof.
149 * Asserts Type tag and optional Class tag according to parameters.
150 * Copy value to POD type (struct, union, C-style array, std::array, etc.).
151 * @param out POD type reference where to copy object value
152 * @param type_tag ASN1_Type enum to assert type on object read
153 * @param class_tag ASN1_Type enum to assert class on object read (default: CONTEXT_SPECIFIC)
154 * @return this reference
155 */
156 template <typename T>
158 requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
159 {
160 const BER_Object obj = get_next_value(sizeof(T), type_tag, class_tag);
161
162 std::memcpy(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
163
164 return (*this);
165 }
166
167 /*
168 * Save all the bytes remaining in the source
169 */
170 template <typename Alloc>
171 BER_Decoder& raw_bytes(std::vector<uint8_t, Alloc>& out) {
172 out.clear();
173 for(;;) {
174 if(auto next = this->read_next_byte()) {
175 out.push_back(*next);
176 } else {
177 break;
178 }
179 }
180 return (*this);
181 }
182
183 BER_Decoder& decode_null();
184
185 /**
186 * Decode a BER encoded BOOLEAN
187 */
189
190 /*
191 * Decode a small BER encoded INTEGER
192 */
194
195 /*
196 * Decode a BER encoded INTEGER
197 */
199
200 std::vector<uint8_t> get_next_octet_string() {
201 std::vector<uint8_t> out_vec;
203 return out_vec;
204 }
205
206 /*
207 * BER decode a BIT STRING or OCTET STRING
208 */
209 template <typename Alloc>
210 BER_Decoder& decode(std::vector<uint8_t, Alloc>& out, ASN1_Type real_type) {
211 return decode(out, real_type, real_type, ASN1_Class::Universal);
212 }
213
214 BER_Decoder& decode(bool& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
215
216 BER_Decoder& decode(size_t& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
217
218 BER_Decoder& decode(BigInt& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
219
220 BER_Decoder& decode(std::vector<uint8_t>& v,
221 ASN1_Type real_type,
222 ASN1_Type type_tag,
224
226 ASN1_Type real_type,
227 ASN1_Type type_tag,
229
230 BER_Decoder& decode(ASN1_Object& obj,
232 ASN1_Class class_tag = ASN1_Class::NoObject);
233
234 /**
235 * Decode an integer value which is typed as an octet string
236 */
237 BER_Decoder& decode_octet_string_bigint(BigInt& b);
238
239 uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes);
240
241 template <typename T>
245
246 template <typename T>
248 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
249 return (*this);
250 }
251
252 template <typename T>
253 BER_Decoder& decode_optional(T& out, ASN1_Type type_tag, ASN1_Class class_tag, const T& default_value = T()) {
254 std::optional<T> optval;
255 this->decode_optional(optval, type_tag, class_tag);
256 out = optval ? *optval : default_value;
257 return (*this);
258 }
259
260 template <typename T>
261 BER_Decoder& decode_optional(std::optional<T>& out, ASN1_Type type_tag, ASN1_Class class_tag);
262
263 template <typename T>
264 BER_Decoder& decode_optional_implicit(T& out,
265 ASN1_Type type_tag,
266 ASN1_Class class_tag,
267 ASN1_Type real_type,
268 ASN1_Class real_class,
269 const T& default_value = T());
270
271 template <typename T>
272 BER_Decoder& decode_list(std::vector<T>& out,
275
276 template <typename T>
277 bool decode_optional_list(std::vector<T>& out,
280
281 template <typename T>
282 BER_Decoder& decode_and_check(const T& expected, std::string_view error_msg) {
283 T actual;
284 decode(actual);
285
286 if(actual != expected) {
287 throw Decoding_Error(error_msg);
288 }
289
290 return (*this);
291 }
292
293 /*
294 * Decode an OPTIONAL string type
295 */
296 template <typename Alloc>
297 BER_Decoder& decode_optional_string(std::vector<uint8_t, Alloc>& out,
298 ASN1_Type real_type,
299 uint32_t expected_tag,
302
303 const ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
304
305 if(obj.is_a(type_tag, class_tag)) {
306 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
307 BER_Decoder(std::move(obj)).decode(out, real_type).verify_end();
308 } else {
309 push_back(std::move(obj));
310 decode(out, real_type, type_tag, class_tag);
311 }
312 } else {
313 out.clear();
314 push_back(std::move(obj));
315 }
316
317 return (*this);
318 }
319
320 template <typename Alloc>
321 BER_Decoder& decode_optional_string(std::vector<uint8_t, Alloc>& out,
322 ASN1_Type real_type,
323 ASN1_Type expected_tag,
325 return decode_optional_string(out, real_type, static_cast<uint32_t>(expected_tag), class_tag);
326 }
327
329
330 private:
331 BER_Decoder(BER_Object&& obj, BER_Decoder* parent);
332
333 std::optional<uint8_t> read_next_byte();
334
335 BER_Object get_next_value(size_t sizeofT, ASN1_Type type_tag, ASN1_Class class_tag);
336
337 BER_Decoder* m_parent = nullptr;
338 BER_Object m_pushed;
339 // either m_data_src.get() or an unowned pointer
340 DataSource* m_source;
341 mutable std::unique_ptr<DataSource> m_data_src;
342};
343
344/*
345* Decode an OPTIONAL or DEFAULT element
346*/
347template <typename T>
348BER_Decoder& BER_Decoder::decode_optional(std::optional<T>& optval, ASN1_Type type_tag, ASN1_Class class_tag) {
350
351 if(obj.is_a(type_tag, class_tag)) {
352 T out{};
353 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
354 BER_Decoder(std::move(obj)).decode(out).verify_end();
355 } else {
356 this->push_back(std::move(obj));
357 this->decode(out, type_tag, class_tag);
358 }
359 optval = std::move(out);
360 } else {
361 this->push_back(std::move(obj));
362 optval = std::nullopt;
363 }
364
365 return (*this);
366}
367
368/*
369* Decode an OPTIONAL or DEFAULT element
370*/
371template <typename T>
373 ASN1_Type type_tag,
374 ASN1_Class class_tag,
375 ASN1_Type real_type,
376 ASN1_Class real_class,
377 const T& default_value) {
379
380 if(obj.is_a(type_tag, class_tag)) {
381 obj.set_tagging(real_type, real_class);
382 push_back(std::move(obj));
383 decode(out, real_type, real_class);
384 } else {
385 // Not what we wanted, push it back on the stream
386 out = default_value;
387 push_back(std::move(obj));
388 }
389
390 return (*this);
391}
392
393/*
394* Decode a list of homogeneously typed values
395*/
396template <typename T>
397BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, ASN1_Type type_tag, ASN1_Class class_tag) {
398 BER_Decoder list = start_cons(type_tag, class_tag);
399
400 while(list.more_items()) {
401 T value;
402 list.decode(value);
403 vec.push_back(std::move(value));
404 }
405
406 list.end_cons();
407
408 return (*this);
409}
410
411/*
412* Decode an optional list of homogeneously typed values
413*/
414template <typename T>
415bool BER_Decoder::decode_optional_list(std::vector<T>& vec, ASN1_Type type_tag, ASN1_Class class_tag) {
416 if(peek_next_object().is_a(type_tag, class_tag)) {
417 decode_list(vec, type_tag, class_tag);
418 return true;
419 }
420
421 return false;
422}
423
424} // namespace Botan
425
426#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
BOTAN_FUTURE_EXPLICIT BER_Decoder(BER_Object &&obj)
Definition ber_dec.h:60
BER_Decoder start_set()
Definition ber_dec.h:130
BER_Decoder(const uint8_t buf[], size_t len)
Definition ber_dec.cpp:360
BER_Decoder(BER_Decoder &&other) noexcept
const BER_Object & peek_next_object()
Definition ber_dec.cpp:250
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:314
BER_Object get_next_object()
Definition ber_dec.cpp:261
BER_Decoder & get_next_value(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:157
BER_Decoder & decode(bool &out)
Definition ber_dec.h:188
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
Definition ber_dec.cpp:465
bool more_items() const
Definition ber_dec.cpp:207
BOTAN_FUTURE_EXPLICIT BER_Decoder(const BER_Object &obj)
Definition ber_dec.h:55
BER_Decoder & decode(BigInt &out)
Definition ber_dec.h:198
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:321
BER_Decoder & raw_bytes(std::vector< uint8_t, Alloc > &out)
Definition ber_dec.h:171
BER_Decoder & decode_integer_type(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:247
std::vector< uint8_t > get_next_octet_string()
Definition ber_dec.h:200
BER_Decoder & decode(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type)
Definition ber_dec.h:210
BER_Decoder start_explicit_context_specific(uint32_t tag)
Definition ber_dec.h:136
BER_Decoder & end_cons()
Definition ber_dec.cpp:337
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition ber_dec.cpp:328
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:397
BER_Decoder start_sequence()
Definition ber_dec.h:128
BER_Decoder & operator=(BER_Decoder &&) noexcept
BER_Decoder start_context_specific(uint32_t tag)
Definition ber_dec.h:132
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:253
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:282
bool decode_optional_list(std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.h:415
BER_Decoder & decode(size_t &out)
Definition ber_dec.h:193
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:297
BOTAN_FUTURE_EXPLICIT BER_Decoder(std::span< const uint8_t > buf)
Definition ber_dec.h:35
BER_Decoder & decode_integer_type(T &out)
Definition ber_dec.h:242
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:372
BER_Decoder & operator=(const BER_Decoder &)=delete
BER_Decoder & get_next(BER_Object &ber)
Definition ber_dec.h:74
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:66
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:68