8 #include <botan/ber_dec.h>
9 #include <botan/bigint.h>
10 #include <botan/get_byte.h>
22 if(!ber->read_byte(b))
28 if((b & 0x1F) != 0x1F)
41 if(!ber->read_byte(b))
42 throw BER_Decoding_Error(
"Long-form tag truncated");
43 if(tag_buf & 0xFF000000)
44 throw BER_Decoding_Error(
"Long-form tag overflowed 32 bits");
46 tag_buf = (tag_buf << 7) | (b & 0x7F);
47 if((b & 0x80) == 0)
break;
56 size_t find_eoc(DataSource*);
61 size_t decode_length(DataSource* ber,
size_t& field_size)
64 if(!ber->read_byte(b))
65 throw BER_Decoding_Error(
"Length field not found");
70 field_size += (b & 0x7F);
71 if(field_size == 1)
return find_eoc(ber);
73 throw BER_Decoding_Error(
"Length field is too large");
77 for(
size_t i = 0; i != field_size - 1; ++i)
80 throw BER_Decoding_Error(
"Field length overflow");
81 if(!ber->read_byte(b))
82 throw BER_Decoding_Error(
"Corrupted length field");
83 length = (length << 8) | b;
91 size_t decode_length(DataSource* ber)
94 return decode_length(ber, dummy);
100 size_t find_eoc(DataSource* ber)
102 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE), data;
106 const size_t got = ber->peek(&buffer[0], buffer.size(), data.size());
110 data += std::make_pair(&buffer[0], got);
113 DataSource_Memory source(data);
120 size_t tag_size = decode_tag(&source, type_tag, class_tag);
124 size_t length_size = 0;
125 size_t item_size = decode_length(&source, length_size);
126 source.discard_next(item_size);
128 length += item_size + length_size + tag_size;
143 if(this->type_tag != type_tag || this->class_tag != class_tag)
163 throw Invalid_State(
"BER_Decoder::verify_end called, but data remains");
208 size_t length = decode_length(source);
210 if(source->
read(&next.
value[0], length) != length)
225 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
239 result.parent =
this;
249 throw Invalid_State(
"BER_Decoder::end_cons called with NULL parent");
251 throw Decoding_Error(
"BER_Decoder::end_cons called with data left");
293 source = other.source;
301 parent = other.parent;
379 out = (obj.
value[0]) ?
true :
false;
390 decode(integer, type_tag, class_tag);
392 if(integer.
bits() > 32)
396 for(
size_t i = 0; i != 4; ++i)
397 out = (out << 8) | integer.
byte_at(3-i);
415 const bool negative = (obj.
value[0] & 0x80) ?
true :
false;
419 for(
size_t i = obj.
value.
size(); i > 0; --i)
422 for(
size_t i = 0; i != obj.
value.
size(); ++i)
451 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING", real_type);
460 if(obj.
value[0] >= 8)
virtual size_t read(byte out[], size_t length)=0
BER_Decoder & decode_optional_string(MemoryRegion< byte > &, ASN1_Tag, u16bit)
BER_Decoder(DataSource &)
BER_Decoder & decode(bool &)
byte byte_at(size_t n) const
byte get_byte(size_t byte_num, T input)
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
void assert_is_a(ASN1_Tag, ASN1_Tag)
BER_Decoder & decode_octet_string_bigint(class BigInt &)
BER_Decoder & decode_null()
void push_back(const BER_Object &)
virtual bool end_of_data() const =0
size_t read_byte(byte &out)
void copy_mem(T *out, const T *in, size_t n)
BER_Decoder & discard_remaining()
SecureVector< byte > value
BER_Object get_next_object()
BER_Decoder & raw_bytes(MemoryRegion< byte > &)
BER_Decoder & verify_end()
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
virtual void decode_from(class BER_Decoder &from)=0