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;
30 auto b = ber->read_byte();
38 if((*b & 0x1F) != 0x1F) {
53 if((tag_buf >> 24) != 0) {
57 if(tag_bytes == 0 && b == 0x80) {
61 tag_buf = (tag_buf << 7) | (*b & 0x7F);
62 if((*b & 0x80) == 0) {
73size_t find_eoc(
DataSource* src,
size_t allow_indef);
78size_t decode_length(
DataSource* ber,
size_t& field_size,
size_t allow_indef) {
80 if(ber->read_byte(b) == 0) {
88 field_size += (b & 0x7F);
94 if(allow_indef == 0) {
95 throw BER_Decoding_Error(
"Nested EOC markers too deep, rejecting to avoid stack exhaustion");
97 return find_eoc(ber, allow_indef - 1);
103 for(
size_t i = 0; i != field_size - 1; ++i) {
107 if(ber->read_byte(b) == 0) {
110 length = (length << 8) | b;
118size_t find_eoc(
DataSource* ber,
size_t allow_indef) {
123 const size_t got = ber->peek(buffer.data(), buffer.size(), data.size());
128 data += std::make_pair(buffer.data(), got);
138 const size_t tag_size = decode_tag(&source, type_tag, class_tag);
143 size_t length_size = 0;
144 const size_t item_size = decode_length(&source, length_size, allow_indef);
145 source.discard_next(item_size);
147 if(
auto new_len =
checked_add(length, item_size, tag_size, length_size)) {
148 length = new_len.value();
160class DataSource_BERObject final :
public DataSource {
162 size_t read(uint8_t out[],
size_t length)
override {
164 const size_t got = std::min<size_t>(m_obj.length() - m_offset, length);
165 copy_mem(out, m_obj.bits() + m_offset, got);
170 size_t peek(uint8_t out[],
size_t length,
size_t peek_offset)
const override {
172 const size_t bytes_left = m_obj.length() - m_offset;
174 if(peek_offset >= bytes_left) {
178 const size_t got = std::min(bytes_left - peek_offset, length);
179 copy_mem(out, m_obj.bits() + m_offset + peek_offset, got);
183 bool check_available(
size_t n)
override {
185 return (n <= (m_obj.length() - m_offset));
188 bool end_of_data()
const override {
return get_bytes_read() == m_obj.length(); }
190 size_t get_bytes_read()
const override {
return m_offset; }
192 explicit DataSource_BERObject(BER_Object&& obj) : m_obj(std::move(obj)) {}
205 if(m_source->end_of_data() && !m_pushed.is_set()) {
215 return verify_end(
"BER_Decoder::verify_end called, but data remains");
222 if(!m_source->end_of_data() || m_pushed.is_set()) {
233 while(m_source->read_byte(buf) != 0) {}
238 if(!m_pushed.is_set()) {
251 if(m_pushed.is_set()) {
252 std::swap(next, m_pushed);
259 decode_tag(m_source, type_tag, class_tag);
260 next.set_tagging(type_tag, class_tag);
261 if(next.
is_set() ==
false) {
265 size_t field_size = 0;
266 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
267 if(!m_source->check_available(length)) {
271 uint8_t* out = next.mutable_bits(length);
272 if(m_source->read(out, length) != length) {
290 if(m_pushed.is_set()) {
291 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
297 if(m_pushed.is_set()) {
298 throw Invalid_State(
"BER_Decoder: Only one push back is allowed");
300 m_pushed = std::move(obj);
313 if(m_parent ==
nullptr) {
314 throw Invalid_State(
"BER_Decoder::end_cons called with null parent");
316 if(!m_source->end_of_data()) {
317 throw Decoding_Error(
"BER_Decoder::end_cons called with data left");
323 m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
324 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();
361 std::swap(m_data_src, other.m_data_src);
402 const uint8_t val = obj.
bits()[0];
405 out = (val != 0) ?
true :
false;
415 decode(integer, type_tag, class_tag);
421 if(integer.
bits() > 32) {
426 for(
size_t i = 0; i != 4; ++i) {
427 out = (out << 8) | integer.
byte_at(3 - i);
442 decode(integer, type_tag, class_tag);
444 if(integer.
bits() > 8 * T_bytes) {
449 for(
size_t i = 0; i != 8; ++i) {
450 out = (out << 8) | integer.
byte_at(7 - i);
466 const uint8_t first = obj.
bits()[0];
467 const bool negative = (first & 0x80) == 0x80;
471 for(
size_t i = obj.
length(); i > 0; --i) {
472 const bool gt0 = (vec[i - 1] > 0);
478 for(
size_t i = 0; i != obj.
length(); ++i) {
493template <
typename Alloc>
494void asn1_decode_binary_string(std::vector<uint8_t, Alloc>& buffer,
505 throw BER_Decoding_Error(
"Invalid BIT STRING");
507 if(obj.
bits()[0] >= 8) {
511 buffer.resize(obj.
length() - 1);
529 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING",
static_cast<uint32_t
>(real_type));
532 asn1_decode_binary_string(buffer,
get_next_object(), real_type, type_tag, class_tag);
541 throw BER_Bad_Tag(
"Bad tag for {BIT,OCTET} STRING",
static_cast<uint32_t
>(real_type));
544 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)
constexpr uint8_t get_byte(T input)
constexpr std::optional< T > checked_add(T a, T b)
constexpr void copy_mem(T *out, const T *in, size_t n)
std::vector< T, secure_allocator< T > > secure_vector
constexpr size_t DefaultBufferSize