8#include <botan/ber_dec.h>
10#include <botan/bigint.h>
11#include <botan/internal/loadstor.h>
12#include <botan/internal/safeint.h>
24const size_t ALLOWED_EOC_NESTINGS = 16;
31 if(!ber->read_byte(b)) {
37 if((b & 0x1F) != 0x1F) {
48 if(!ber->read_byte(b)) {
49 throw BER_Decoding_Error(
"Long-form tag truncated");
51 if(tag_buf & 0xFF000000) {
52 throw BER_Decoding_Error(
"Long-form tag overflowed 32 bits");
55 tag_buf = (tag_buf << 7) | (b & 0x7F);
67size_t find_eoc(DataSource* src,
size_t allow_indef);
72size_t decode_length(DataSource* ber,
size_t& field_size,
size_t allow_indef) {
74 if(!ber->read_byte(b)) {
75 throw BER_Decoding_Error(
"Length field not found");
82 field_size += (b & 0x7F);
84 throw BER_Decoding_Error(
"Length field is too large");
88 if(allow_indef == 0) {
89 throw BER_Decoding_Error(
"Nested EOC markers too deep, rejecting to avoid stack exhaustion");
91 return find_eoc(ber, allow_indef - 1);
97 for(
size_t i = 0; i != field_size - 1; ++i) {
98 if(get_byte<0>(length) != 0) {
99 throw BER_Decoding_Error(
"Field length overflow");
101 if(!ber->read_byte(b)) {
102 throw BER_Decoding_Error(
"Corrupted length field");
104 length = (length << 8) | b;
112size_t find_eoc(DataSource* ber,
size_t allow_indef) {
116 const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
121 data += std::make_pair(buffer.data(), got);
124 DataSource_Memory source(data);
131 size_t tag_size = decode_tag(&source, type_tag, class_tag);
136 size_t length_size = 0;
137 size_t item_size = decode_length(&source, length_size, allow_indef);
138 source.discard_next(item_size);
151class DataSource_BERObject
final :
public DataSource {
153 size_t read(uint8_t out[],
size_t length)
override {
155 const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
156 copy_mem(out, m_obj.bits() + m_offset, got);
161 size_t peek(uint8_t out[],
size_t length,
size_t peek_offset)
const override {
163 const size_t bytes_left = m_obj.length() - m_offset;
165 if(peek_offset >= bytes_left) {
169 const size_t got = std::min(bytes_left - peek_offset, length);
170 copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
174 bool check_available(
size_t n)
override {
176 return (n <= (m_obj.length() - m_offset));
179 bool end_of_data()
const override {
return get_bytes_read() == m_obj.length(); }
181 size_t get_bytes_read()
const override {
return m_offset; }
183 explicit DataSource_BERObject(BER_Object&& obj) : m_obj(
std::move(obj)), m_offset(0) {}
206 return verify_end(
"BER_Decoder::verify_end called, but data remains");
235 std::swap(next, m_pushed);
242 decode_tag(m_source, type_tag, class_tag);
243 next.set_tagging(type_tag, class_tag);
244 if(next.
is_set() ==
false) {
249 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
254 uint8_t* out = next.mutable_bits(length);
255 if(m_source->
read(out, length) != length) {
274 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
281 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
283 m_pushed = std::move(obj);
297 throw Invalid_State(
"BER_Decoder::end_cons called with null parent");
300 throw Decoding_Error(
"BER_Decoder::end_cons called with data left");
306 m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
307 m_source = m_data_src.get();
322 m_data_src = std::make_unique<DataSource_Memory>(data, length);
323 m_source = m_data_src.get();
330 m_data_src = std::make_unique<DataSource_Memory>(data);
331 m_source = m_data_src.get();
338 m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
339 m_source = m_data_src.get();
346 m_source = other.m_source;
349 std::swap(m_data_src, other.m_data_src);
350 m_parent = other.m_parent;
391 out = (obj.
bits()[0]) ?
true :
false;
400 decode(integer, type_tag, class_tag);
406 if(integer.
bits() > 32) {
411 for(
size_t i = 0; i != 4; ++i) {
412 out = (out << 8) | integer.
byte_at(3 - i);
427 decode(integer, type_tag, class_tag);
429 if(integer.
bits() > 8 * T_bytes) {
434 for(
size_t i = 0; i != 8; ++i) {
435 out = (out << 8) | integer.
byte_at(7 - i);
451 const bool negative = (obj.
bits()[0] & 0x80) ?
true :
false;
455 for(
size_t i = obj.
length(); i > 0; --i) {
460 for(
size_t i = 0; i != obj.
length(); ++i) {
463 out =
BigInt(vec.data(), vec.size());
475template <
typename Alloc>
476void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
487 throw BER_Decoding_Error(
"Invalid BIT STRING");
489 if(obj.
bits()[0] >= 8) {
490 throw BER_Decoding_Error(
"Bad number of unused bits in BIT STRING");
493 buffer.resize(obj.
length() - 1);
511 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING",
static_cast<uint32_t
>(real_type));
514 asn1_decode_binary_string(buffer,
get_next_object(), real_type, type_tag, class_tag);
523 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING",
static_cast<uint32_t
>(real_type));
526 asn1_decode_binary_string(buffer,
get_next_object(), real_type, type_tag, class_tag);
#define BOTAN_ASSERT_NOMSG(expr)
virtual void decode_from(BER_Decoder &from)=0
BER_Decoder(const uint8_t buf[], size_t len)
void push_back(const BER_Object &obj)
BER_Object get_next_object()
BER_Decoder & decode(bool &out)
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
BER_Decoder & verify_end()
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
BER_Decoder & discard_remaining()
BER_Decoder & decode_octet_string_bigint(BigInt &b)
BER_Decoder & decode_null()
const uint8_t * bits() const
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr="object") const
static BigInt decode(const uint8_t buf[], size_t length)
uint8_t byte_at(size_t n) const
size_t read_byte(uint8_t &out)
virtual size_t read(uint8_t out[], size_t length)=0
virtual bool check_available(size_t n)=0
virtual bool end_of_data() const =0
int(* final)(unsigned char *, CTX *)
#define BOTAN_DEFAULT_BUFFER_SIZE
constexpr void copy_mem(T *out, const T *in, size_t n)
std::vector< T, secure_allocator< T > > secure_vector
#define BOTAN_CHECKED_ADD(x, y)