9 #include <botan/ber_dec.h>
10 #include <botan/bigint.h>
11 #include <botan/loadstor.h>
12 #include <botan/internal/safeint.h>
24 if(!ber->read_byte(b))
30 if((b & 0x1F) != 0x1F)
43 if(!ber->read_byte(b))
44 throw BER_Decoding_Error(
"Long-form tag truncated");
45 if(tag_buf & 0xFF000000)
46 throw BER_Decoding_Error(
"Long-form tag overflowed 32 bits");
48 tag_buf = (tag_buf << 7) | (b & 0x7F);
49 if((b & 0x80) == 0)
break;
58 size_t find_eoc(DataSource*);
63 size_t decode_length(DataSource* ber,
size_t& field_size)
66 if(!ber->read_byte(b))
67 throw BER_Decoding_Error(
"Length field not found");
72 field_size += (b & 0x7F);
73 if(field_size == 1)
return find_eoc(ber);
75 throw BER_Decoding_Error(
"Length field is too large");
79 for(
size_t i = 0; i != field_size - 1; ++i)
82 throw BER_Decoding_Error(
"Field length overflow");
83 if(!ber->read_byte(b))
84 throw BER_Decoding_Error(
"Corrupted length field");
85 length = (length << 8) | b;
93 size_t decode_length(DataSource* ber)
96 return decode_length(ber, dummy);
102 size_t find_eoc(DataSource* ber)
104 secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE), data;
108 const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
112 data += std::make_pair(buffer.data(), got);
115 DataSource_Memory source(data);
122 size_t tag_size = decode_tag(&source, type_tag, class_tag);
126 size_t length_size = 0;
127 size_t item_size = decode_length(&source, length_size);
128 source.discard_next(item_size);
147 if(type_tag != type_tag_ || class_tag != class_tag_)
171 throw Invalid_State(
"BER_Decoder::verify_end called, but data remains");
225 const size_t length = decode_length(m_source);
229 next.
value.resize(length);
230 if(m_source->
read(next.
value.data(), length) != length)
251 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
265 result.m_parent =
this;
275 throw Invalid_State(
"BER_Decoder::end_cons called with NULL parent");
277 throw Decoding_Error(
"BER_Decoder::end_cons called with data left");
330 m_source = other.m_source;
334 other.m_owns =
false;
338 m_parent = other.m_parent;
407 std::vector<uint8_t> out_vec;
421 if(obj.
value.size() != 1)
424 out = (obj.
value[0]) ?
true :
false;
435 decode(integer, type_tag, class_tag);
437 if(integer.
bits() > 32)
441 for(
size_t i = 0; i != 4; ++i)
442 out = (out << 8) | integer.
byte_at(3-i);
458 decode(integer, type_tag, class_tag);
460 if(integer.
bits() > 8*T_bytes)
464 for(
size_t i = 0; i != 8; ++i)
465 out = (out << 8) | integer.
byte_at(7-i);
479 if(obj.
value.empty())
483 const bool negative = (obj.
value[0] & 0x80) ?
true :
false;
487 for(
size_t i = obj.
value.size(); i > 0; --i)
490 for(
size_t i = 0; i != obj.
value.size(); ++i)
527 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING", real_type);
536 if(obj.
value.empty())
538 if(obj.
value[0] >= 8)
541 buffer.resize(obj.
value.size() - 1);
552 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING", real_type);
561 if(obj.
value.empty())
563 if(obj.
value[0] >= 8)
566 buffer.resize(obj.
value.size() - 1);
virtual bool check_available(size_t n)=0
virtual void decode_from(BER_Decoder &from)=0
BER_Decoder(DataSource &)
void push_back(const BER_Object &obj)
BER_Decoder & decode(bool &v)
std::string to_string(const BER_Object &obj)
BER_Decoder & decode_octet_string_bigint(class BigInt &b)
#define BOTAN_CHECKED_ADD(x, y)
BER_Decoder & get_next(BER_Object &ber)
void assert_is_a(ASN1_Tag, ASN1_Tag)
uint64_t decode_constrained_integer(ASN1_Tag type_tag, ASN1_Tag class_tag, size_t T_bytes)
std::vector< T, secure_allocator< T >> secure_vector
BER_Decoder & decode_null()
secure_vector< uint8_t > value
virtual bool end_of_data() const =0
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
void copy_mem(T *out, const T *in, size_t n)
BER_Decoder & discard_remaining()
size_t read_byte(uint8_t &out)
BER_Object get_next_object()
std::vector< T > unlock(const secure_vector< T > &in)
BER_Decoder & verify_end()
uint8_t get_byte(size_t byte_num, T input)
std::vector< uint8_t > get_next_octet_string()
BER_Decoder & raw_bytes(secure_vector< uint8_t > &v)
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
uint8_t byte_at(size_t n) const