8#include <botan/ber_dec.h>
10#include <botan/bigint.h>
11#include <botan/internal/int_utils.h>
12#include <botan/internal/loadstor.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 if(tag_bytes == 0 &&
b == 0x80) {
56 throw BER_Decoding_Error(
"Long form tag with leading zero");
59 tag_buf = (tag_buf << 7) | (
b & 0x7F);
71size_t find_eoc(DataSource* src,
size_t allow_indef);
76size_t decode_length(DataSource* ber,
size_t& field_size,
size_t allow_indef) {
78 if(!ber->read_byte(
b)) {
79 throw BER_Decoding_Error(
"Length field not found");
86 field_size += (
b & 0x7F);
88 throw BER_Decoding_Error(
"Length field is too large");
92 if(allow_indef == 0) {
93 throw BER_Decoding_Error(
"Nested EOC markers too deep, rejecting to avoid stack exhaustion");
95 return find_eoc(ber, allow_indef - 1);
101 for(
size_t i = 0; i != field_size - 1; ++i) {
103 throw BER_Decoding_Error(
"Field length overflow");
105 if(!ber->read_byte(
b)) {
106 throw BER_Decoding_Error(
"Corrupted length field");
108 length = (length << 8) |
b;
116size_t find_eoc(DataSource* ber,
size_t allow_indef) {
120 const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
125 data += std::make_pair(buffer.data(), got);
128 DataSource_Memory source(data);
135 const size_t tag_size = decode_tag(&source, type_tag, class_tag);
140 size_t length_size = 0;
141 const size_t item_size = decode_length(&source, length_size, allow_indef);
142 source.discard_next(item_size);
144 if(
auto new_len =
checked_add(length, item_size, tag_size, length_size)) {
145 length = new_len.value();
147 throw Decoding_Error(
"Integer overflow while decoding DER");
157class DataSource_BERObject
final :
public DataSource {
159 size_t read(uint8_t out[],
size_t length)
override {
161 const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
162 copy_mem(out, m_obj.bits() + m_offset, got);
167 size_t peek(uint8_t out[],
size_t length,
size_t peek_offset)
const override {
169 const size_t bytes_left = m_obj.length() - m_offset;
171 if(peek_offset >= bytes_left) {
175 const size_t got = std::min(bytes_left - peek_offset, length);
176 copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
180 bool check_available(
size_t n)
override {
182 return (n <= (m_obj.length() - m_offset));
185 bool end_of_data()
const override {
return get_bytes_read() == m_obj.length(); }
187 size_t get_bytes_read()
const override {
return m_offset; }
189 explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)), m_offset(0) {}
212 return verify_end(
"BER_Decoder::verify_end called, but data remains");
249 std::swap(next, m_pushed);
256 decode_tag(m_source, type_tag, class_tag);
257 next.set_tagging(type_tag, class_tag);
258 if(next.
is_set() ==
false) {
263 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
268 uint8_t* out = next.mutable_bits(length);
269 if(m_source->
read(out, length) != length) {
288 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
295 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
297 m_pushed = std::move(obj);
311 throw Invalid_State(
"BER_Decoder::end_cons called with null parent");
314 throw Decoding_Error(
"BER_Decoder::end_cons called with data left");
320 m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
321 m_source = m_data_src.get();
336 m_data_src = std::make_unique<DataSource_Memory>(data, length);
337 m_source = m_data_src.get();
344 m_data_src = std::make_unique<DataSource_Memory>(data);
345 m_source = m_data_src.get();
352 m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
353 m_source = m_data_src.get();
360 m_source = other.m_source;
363 std::swap(m_data_src, other.m_data_src);
364 m_parent = other.m_parent;
405 out = (obj.
bits()[0]) ? true :
false;
414 decode(integer, type_tag, class_tag);
420 if(integer.
bits() > 32) {
425 for(
size_t i = 0; i != 4; ++i) {
426 out = (out << 8) | integer.
byte_at(3 - i);
441 decode(integer, type_tag, class_tag);
443 if(integer.
bits() > 8 * T_bytes) {
448 for(
size_t i = 0; i != 8; ++i) {
449 out = (out << 8) | integer.
byte_at(7 - i);
465 const bool negative = (obj.
bits()[0] & 0x80) ? true :
false;
469 for(
size_t i = obj.
length(); i > 0; --i) {
474 for(
size_t i = 0; i != obj.
length(); ++i) {
489template <
typename Alloc>
490void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
501 throw BER_Decoding_Error(
"Invalid BIT STRING");
503 if(obj.
bits()[0] >= 8) {
504 throw BER_Decoding_Error(
"Bad number of unused bits in BIT STRING");
507 buffer.resize(obj.
length() - 1);
525 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING",
static_cast<uint32_t
>(real_type));
528 asn1_decode_binary_string(buffer,
get_next_object(), real_type, type_tag, class_tag);
537 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING",
static_cast<uint32_t
>(real_type));
540 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)
const BER_Object & peek_next_object()
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
std::span< const uint8_t > data() const
static BigInt from_bytes(std::span< const uint8_t > bytes)
uint8_t byte_at(size_t n) const
void _assign_from_bytes(std::span< const uint8_t > bytes)
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 uint8_t get_byte(T input)
constexpr std::optional< T > checked_add(T a, T b)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)