8 #include <botan/ber_dec.h> 9 #include <botan/bigint.h> 10 #include <botan/get_byte.h> 11 #include <botan/internal/safeint.h> 23 if(!ber->read_byte(b))
29 if((b & 0x1F) != 0x1F)
42 if(!ber->read_byte(b))
43 throw BER_Decoding_Error(
"Long-form tag truncated");
44 if(tag_buf & 0xFF000000)
45 throw BER_Decoding_Error(
"Long-form tag overflowed 32 bits");
47 tag_buf = (tag_buf << 7) | (b & 0x7F);
48 if((b & 0x80) == 0)
break;
57 size_t find_eoc(DataSource*);
62 size_t decode_length(DataSource* ber,
size_t& field_size)
65 if(!ber->read_byte(b))
66 throw BER_Decoding_Error(
"Length field not found");
71 field_size += (b & 0x7F);
72 if(field_size == 1)
return find_eoc(ber);
74 throw BER_Decoding_Error(
"Length field is too large");
78 for(
size_t i = 0; i != field_size - 1; ++i)
81 throw BER_Decoding_Error(
"Field length overflow");
82 if(!ber->read_byte(b))
83 throw BER_Decoding_Error(
"Corrupted length field");
84 length = (length << 8) | b;
92 size_t decode_length(DataSource* ber)
95 return decode_length(ber, dummy);
101 size_t find_eoc(DataSource* ber)
103 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE), data;
107 const size_t got = ber->peek(&buffer[0], buffer.size(), data.size());
111 data += std::make_pair(&buffer[0], got);
114 DataSource_Memory source(data);
121 size_t tag_size = decode_tag(&source, type_tag, class_tag);
125 size_t length_size = 0;
126 size_t item_size = decode_length(&source, length_size);
127 source.discard_next(item_size);
146 if(this->type_tag != type_tag || this->class_tag != class_tag)
155 if(source->end_of_data() && (pushed.type_tag ==
NO_OBJECT))
165 if(!source->end_of_data() || (pushed.type_tag !=
NO_OBJECT))
166 throw Invalid_State(
"BER_Decoder::verify_end called, but data remains");
177 while(source->read_byte(buf))
188 while(source->read_byte(buf))
211 const size_t length = decode_length(source);
212 if(!source->check_available(length))
216 if(source->read(&next.
value[0], length) != length)
220 return get_next_object();
231 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
245 result.parent =
this;
255 throw Invalid_State(
"BER_Decoder::end_cons called with NULL parent");
256 if(!source->end_of_data())
257 throw Decoding_Error(
"BER_Decoder::end_cons called with data left");
268 pushed.type_tag = pushed.class_tag =
NO_OBJECT;
279 pushed.type_tag = pushed.class_tag =
NO_OBJECT;
290 pushed.type_tag = pushed.class_tag =
NO_OBJECT;
299 source = other.source;
306 pushed.type_tag = pushed.class_tag =
NO_OBJECT;
307 parent = other.parent;
385 out = (obj.
value[0]) ?
true :
false;
396 decode(integer, type_tag, class_tag);
398 if(integer.
bits() > 32)
402 for(
size_t i = 0; i != 4; ++i)
403 out = (out << 8) | integer.
byte_at(3-i);
421 const bool negative = (obj.
value[0] & 0x80) ?
true :
false;
425 for(
size_t i = obj.
value.
size(); i > 0; --i)
428 for(
size_t i = 0; i != obj.
value.
size(); ++i)
457 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING", real_type);
468 if(obj.
value[0] >= 8)
BER_Decoder & decode_optional_string(MemoryRegion< byte > &, ASN1_Tag, u16bit)
BER_Decoder(DataSource &)
BER_Decoder & decode(bool &)
byte get_byte(size_t byte_num, T input)
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
#define BOTAN_CHECKED_ADD(x, y)
SecureVector< byte > decode(DataSource &source, std::string &label)
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 &)
void copy_mem(T *out, const T *in, size_t n)
BER_Decoder & discard_remaining()
SecureVector< byte > value
byte byte_at(size_t n) const
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