Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::BER_Decoder Class Referencefinal

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (const uint8_t buf[], size_t len)
 
 BER_Decoder (const secure_vector< uint8_t > &vec)
 
 BER_Decoder (const std::vector< uint8_t > &vec)
 
 BER_Decoder (DataSource &src)
 
 BER_Decoder (const BER_Object &obj)
 
 BER_Decoder (BER_Object &&obj)
 
 BER_Decoder (const BER_Decoder &other)
 
BER_Decoderdecode (bool &out)
 
BER_Decoderdecode (size_t &out)
 
BER_Decoderdecode (BigInt &out)
 
template<typename Alloc >
BER_Decoderdecode (std::vector< uint8_t, Alloc > &out, ASN1_Tag real_type)
 
BER_Decoderdecode (bool &v, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (size_t &v, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (BigInt &v, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (std::vector< uint8_t > &v, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (secure_vector< uint8_t > &v, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (class ASN1_Object &obj, ASN1_Tag type_tag=NO_OBJECT, ASN1_Tag class_tag=NO_OBJECT)
 
template<typename T >
BER_Decoderdecode_and_check (const T &expected, const std::string &error_msg)
 
uint64_t decode_constrained_integer (ASN1_Tag type_tag, ASN1_Tag class_tag, size_t T_bytes)
 
template<typename T >
BER_Decoderdecode_integer_type (T &out)
 
template<typename T >
BER_Decoderdecode_integer_type (T &out, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
template<typename T >
BER_Decoderdecode_list (std::vector< T > &out, ASN1_Tag type_tag=SEQUENCE, ASN1_Tag class_tag=UNIVERSAL)
 
BER_Decoderdecode_null ()
 
BER_Decoderdecode_octet_string_bigint (BigInt &b)
 
template<typename T >
BER_Decoderdecode_optional (T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
 
template<typename T >
BER_Decoderdecode_optional_implicit (T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, ASN1_Tag real_type, ASN1_Tag real_class, const T &default_value=T())
 
template<typename Alloc >
BER_Decoderdecode_optional_string (std::vector< uint8_t, Alloc > &out, ASN1_Tag real_type, uint16_t type_no, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
BER_Decoderdiscard_remaining ()
 
BER_Decoderend_cons ()
 
BER_Decoderget_next (BER_Object &ber)
 
BER_Object get_next_object ()
 
std::vector< uint8_t > get_next_octet_string ()
 
template<typename T >
BER_Decoderget_next_value (T &out, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
bool more_items () const
 
BER_Decoderoperator= (const BER_Decoder &)=delete
 
void push_back (const BER_Object &obj)
 
void push_back (BER_Object &&obj)
 
template<typename Alloc >
BER_Decoderraw_bytes (std::vector< uint8_t, Alloc > &out)
 
BER_Decoder start_cons (ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
 
BER_Decoderverify_end ()
 
BER_Decoderverify_end (const std::string &err_msg)
 

Detailed Description

BER Decoding Object

Definition at line 21 of file ber_dec.h.

Constructor & Destructor Documentation

◆ BER_Decoder() [1/7]

Botan::BER_Decoder::BER_Decoder ( const uint8_t  buf[],
size_t  len 
)

Set up to BER decode the data in buf of length len

Definition at line 327 of file ber_dec.cpp.

Referenced by decode_optional(), and start_cons().

328  {
329  m_data_src.reset(new DataSource_Memory(data, length));
330  m_source = m_data_src.get();
331  }

◆ BER_Decoder() [2/7]

Botan::BER_Decoder::BER_Decoder ( const secure_vector< uint8_t > &  vec)
explicit

Set up to BER decode the data in vec

Definition at line 336 of file ber_dec.cpp.

337  {
338  m_data_src.reset(new DataSource_Memory(data));
339  m_source = m_data_src.get();
340  }

◆ BER_Decoder() [3/7]

Botan::BER_Decoder::BER_Decoder ( const std::vector< uint8_t > &  vec)
explicit

Set up to BER decode the data in vec

Definition at line 345 of file ber_dec.cpp.

346  {
347  m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
348  m_source = m_data_src.get();
349  }

◆ BER_Decoder() [4/7]

Botan::BER_Decoder::BER_Decoder ( DataSource src)
explicit

Set up to BER decode the data in src

Definition at line 319 of file ber_dec.cpp.

320  {
321  m_source = &src;
322  }

◆ BER_Decoder() [5/7]

Botan::BER_Decoder::BER_Decoder ( const BER_Object obj)
inline

Set up to BER decode the data in obj

Definition at line 47 of file ber_dec.h.

47  :
48  BER_Decoder(obj.bits(), obj.length()) {}
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:327

◆ BER_Decoder() [6/7]

Botan::BER_Decoder::BER_Decoder ( BER_Object &&  obj)
inline

Set up to BER decode the data in obj

Definition at line 53 of file ber_dec.h.

53  :
54  BER_Decoder(std::move(obj), nullptr) {}
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:327

◆ BER_Decoder() [7/7]

Botan::BER_Decoder::BER_Decoder ( const BER_Decoder other)

Definition at line 354 of file ber_dec.cpp.

355  {
356  m_source = other.m_source;
357 
358  // take ownership
359  std::swap(m_data_src, other.m_data_src);
360  m_parent = other.m_parent;
361  }

Member Function Documentation

◆ decode() [1/10]

BER_Decoder& Botan::BER_Decoder::decode ( bool &  out)
inline

◆ decode() [2/10]

BER_Decoder& Botan::BER_Decoder::decode ( size_t &  out)
inline

Definition at line 178 of file ber_dec.h.

References Botan::PEM_Code::decode(), Botan::INTEGER, and Botan::UNIVERSAL.

179  {
180  return decode(out, INTEGER, UNIVERSAL);
181  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170

◆ decode() [3/10]

BER_Decoder& Botan::BER_Decoder::decode ( BigInt out)
inline

Definition at line 186 of file ber_dec.h.

References Botan::PEM_Code::decode(), Botan::INTEGER, and Botan::UNIVERSAL.

187  {
188  return decode(out, INTEGER, UNIVERSAL);
189  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170

◆ decode() [4/10]

template<typename Alloc >
BER_Decoder& Botan::BER_Decoder::decode ( std::vector< uint8_t, Alloc > &  out,
ASN1_Tag  real_type 
)
inline

Definition at line 202 of file ber_dec.h.

References Botan::PEM_Code::decode(), and Botan::UNIVERSAL.

203  {
204  return decode(out, real_type, real_type, UNIVERSAL);
205  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170

◆ decode() [5/10]

BER_Decoder & Botan::BER_Decoder::decode ( bool &  v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 396 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::BER_Object::bits(), get_next_object(), and Botan::BER_Object::length().

398  {
399  BER_Object obj = get_next_object();
400  obj.assert_is_a(type_tag, class_tag);
401 
402  if(obj.length() != 1)
403  throw BER_Decoding_Error("BER boolean value had invalid size");
404 
405  out = (obj.bits()[0]) ? true : false;
406  return (*this);
407  }
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode() [6/10]

BER_Decoder & Botan::BER_Decoder::decode ( size_t &  v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 412 of file ber_dec.cpp.

References Botan::BigInt::bits(), Botan::BigInt::byte_at(), decode(), and Botan::BigInt::is_negative().

415  {
416  BigInt integer;
417  decode(integer, type_tag, class_tag);
418 
419  if(integer.is_negative())
420  throw BER_Decoding_Error("Decoded small integer value was negative");
421 
422  if(integer.bits() > 32)
423  throw BER_Decoding_Error("Decoded integer value larger than expected");
424 
425  out = 0;
426  for(size_t i = 0; i != 4; ++i)
427  out = (out << 8) | integer.byte_at(3-i);
428 
429  return (*this);
430  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170

◆ decode() [7/10]

BER_Decoder & Botan::BER_Decoder::decode ( BigInt v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 458 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::BER_Object::bits(), Botan::BigInt::flip_sign(), get_next_object(), and Botan::BER_Object::length().

461  {
462  BER_Object obj = get_next_object();
463  obj.assert_is_a(type_tag, class_tag);
464 
465  if(obj.length() == 0)
466  {
467  out = 0;
468  }
469  else
470  {
471  const bool negative = (obj.bits()[0] & 0x80) ? true : false;
472 
473  if(negative)
474  {
475  secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
476  for(size_t i = obj.length(); i > 0; --i)
477  if(vec[i-1]--)
478  break;
479  for(size_t i = 0; i != obj.length(); ++i)
480  vec[i] = ~vec[i];
481  out = BigInt(vec.data(), vec.size());
482  out.flip_sign();
483  }
484  else
485  {
486  out = BigInt(obj.bits(), obj.length());
487  }
488  }
489 
490  return (*this);
491  }
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode() [8/10]

BER_Decoder & Botan::BER_Decoder::decode ( std::vector< uint8_t > &  v,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 538 of file ber_dec.cpp.

References Botan::BIT_STRING, get_next_object(), and Botan::OCTET_STRING.

541  {
542  if(real_type != OCTET_STRING && real_type != BIT_STRING)
543  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
544 
545  asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
546  return (*this);
547  }
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode() [9/10]

BER_Decoder & Botan::BER_Decoder::decode ( secure_vector< uint8_t > &  v,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 527 of file ber_dec.cpp.

References Botan::BIT_STRING, get_next_object(), and Botan::OCTET_STRING.

530  {
531  if(real_type != OCTET_STRING && real_type != BIT_STRING)
532  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
533 
534  asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
535  return (*this);
536  }
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode() [10/10]

BER_Decoder & Botan::BER_Decoder::decode ( class ASN1_Object obj,
ASN1_Tag  type_tag = NO_OBJECT,
ASN1_Tag  class_tag = NO_OBJECT 
)

Definition at line 366 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

368  {
369  obj.decode_from(*this);
370  return (*this);
371  }

◆ decode_and_check()

template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_and_check ( const T expected,
const std::string &  error_msg 
)
inline

Definition at line 277 of file ber_dec.h.

References Botan::PEM_Code::decode(), and T.

Referenced by Botan::EC_PrivateKey::EC_PrivateKey(), Botan::OCSP::Response::Response(), Botan::RSA_PrivateKey::RSA_PrivateKey(), and Botan::TLS::Session::Session().

279  {
280  T actual;
281  decode(actual);
282 
283  if(actual != expected)
284  throw Decoding_Error(error_msg);
285 
286  return (*this);
287  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
fe T
Definition: ge.cpp:37

◆ decode_constrained_integer()

uint64_t Botan::BER_Decoder::decode_constrained_integer ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag,
size_t  T_bytes 
)

Definition at line 435 of file ber_dec.cpp.

References Botan::BigInt::bits(), Botan::BigInt::byte_at(), and decode().

438  {
439  if(T_bytes > 8)
440  throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
441 
442  BigInt integer;
443  decode(integer, type_tag, class_tag);
444 
445  if(integer.bits() > 8*T_bytes)
446  throw BER_Decoding_Error("Decoded integer value larger than expected");
447 
448  uint64_t out = 0;
449  for(size_t i = 0; i != 8; ++i)
450  out = (out << 8) | integer.byte_at(7-i);
451 
452  return out;
453  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170

◆ decode_integer_type() [1/2]

template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_integer_type ( T out)
inline

Definition at line 242 of file ber_dec.h.

References Botan::INTEGER, and Botan::UNIVERSAL.

Referenced by Botan::TLS::Session::Session().

243  {
244  return decode_integer_type<T>(out, INTEGER, UNIVERSAL);
245  }

◆ decode_integer_type() [2/2]

template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_integer_type ( T out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)
inline

Definition at line 248 of file ber_dec.h.

References T.

251  {
252  out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
253  return (*this);
254  }
uint64_t decode_constrained_integer(ASN1_Tag type_tag, ASN1_Tag class_tag, size_t T_bytes)
Definition: ber_dec.cpp:435
fe T
Definition: ge.cpp:37

◆ decode_list()

template<typename T >
BER_Decoder & Botan::BER_Decoder::decode_list ( std::vector< T > &  out,
ASN1_Tag  type_tag = SEQUENCE,
ASN1_Tag  class_tag = UNIVERSAL 
)

Definition at line 398 of file ber_dec.h.

References decode(), end_cons(), more_items(), push_back(), start_cons(), and T.

Referenced by Botan::OCSP::Response::Response().

401  {
402  BER_Decoder list = start_cons(type_tag, class_tag);
403 
404  while(list.more_items())
405  {
406  T value;
407  list.decode(value);
408  vec.push_back(std::move(value));
409  }
410 
411  list.end_cons();
412 
413  return (*this);
414  }
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:327
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:290
fe T
Definition: ge.cpp:37

◆ decode_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 376 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), get_next_object(), Botan::BER_Object::length(), Botan::NULL_TAG, and Botan::UNIVERSAL.

377  {
378  BER_Object obj = get_next_object();
379  obj.assert_is_a(NULL_TAG, UNIVERSAL);
380  if(obj.length() > 0)
381  throw BER_Decoding_Error("NULL object had nonzero size");
382  return (*this);
383  }
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode_octet_string_bigint()

BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( BigInt b)

Decode an integer value which is typed as an octet string

Definition at line 385 of file ber_dec.cpp.

References decode(), Botan::BigInt::decode(), and Botan::OCTET_STRING.

386  {
387  secure_vector<uint8_t> out_vec;
388  decode(out_vec, OCTET_STRING);
389  out = BigInt::decode(out_vec.data(), out_vec.size());
390  return (*this);
391  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114

◆ decode_optional()

template<typename T >
BER_Decoder & Botan::BER_Decoder::decode_optional ( T out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag,
const T default_value = T() 
)

Definition at line 337 of file ber_dec.h.

References BER_Decoder(), Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, decode(), get_next_object(), Botan::BER_Object::is_a(), and push_back().

Referenced by Botan::OCSP::SingleResponse::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::Extensions::decode_from(), Botan::EC_PrivateKey::EC_PrivateKey(), and Botan::OCSP::Response::Response().

341  {
342  BER_Object obj = get_next_object();
343 
344  if(obj.is_a(type_tag, class_tag))
345  {
346  if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
347  {
348  BER_Decoder(std::move(obj)).decode(out).verify_end();
349  }
350  else
351  {
352  push_back(std::move(obj));
353  decode(out, type_tag, class_tag);
354  }
355  }
356  else
357  {
358  out = default_value;
359  push_back(std::move(obj));
360  }
361 
362  return (*this);
363  }
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:276
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:327
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode_optional_implicit()

template<typename T >
BER_Decoder & Botan::BER_Decoder::decode_optional_implicit ( T out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag,
ASN1_Tag  real_type,
ASN1_Tag  real_class,
const T default_value = T() 
)

Definition at line 369 of file ber_dec.h.

References decode(), get_next_object(), Botan::BER_Object::is_a(), and push_back().

Referenced by Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from().

376  {
377  BER_Object obj = get_next_object();
378 
379  if(obj.is_a(type_tag, class_tag))
380  {
381  obj.set_tagging(real_type, real_class);
382  push_back(std::move(obj));
383  decode(out, real_type, real_class);
384  }
385  else
386  {
387  // Not what we wanted, push it back on the stream
388  out = default_value;
389  push_back(std::move(obj));
390  }
391 
392  return (*this);
393  }
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:276
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ decode_optional_string()

template<typename Alloc >
BER_Decoder& Botan::BER_Decoder::decode_optional_string ( std::vector< uint8_t, Alloc > &  out,
ASN1_Tag  real_type,
uint16_t  type_no,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)
inline

Definition at line 293 of file ber_dec.h.

References Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, Botan::PEM_Code::decode(), decode(), Botan::BER_Object::is_a(), and verify_end().

Referenced by Botan::EC_PrivateKey::EC_PrivateKey(), and Botan::OCSP::Response::Response().

297  {
298  BER_Object obj = get_next_object();
299 
300  ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
301 
302  if(obj.is_a(type_tag, class_tag))
303  {
304  if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
305  {
306  BER_Decoder(std::move(obj)).decode(out, real_type).verify_end();
307  }
308  else
309  {
310  push_back(std::move(obj));
311  decode(out, real_type, type_tag, class_tag);
312  }
313  }
314  else
315  {
316  out.clear();
317  push_back(std::move(obj));
318  }
319 
320  return (*this);
321  }
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:276
ASN1_Tag
Definition: asn1_obj.h:22
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:327
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ discard_remaining()

BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Discard any data that remains unread Returns (*this)

Definition at line 226 of file ber_dec.cpp.

References Botan::DataSource::read_byte().

Referenced by Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), and Botan::Ed25519_PrivateKey::Ed25519_PrivateKey().

227  {
228  uint8_t buf;
229  while(m_source->read_byte(buf))
230  {}
231  return (*this);
232  }
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:23

◆ end_cons()

BER_Decoder & Botan::BER_Decoder::end_cons ( )

Finish decoding a constructed data, throws if any data remains. Returns the parent of *this (ie the object on which start_cons was called).

Definition at line 300 of file ber_dec.cpp.

References Botan::DataSource::end_of_data().

Referenced by Botan::Attribute::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::X509_DN::decode_from(), Botan::CRL_Entry::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::X509_Object::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::Extensions::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), decode_list(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::X509::load_key(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), Botan::McEliece_PublicKey::McEliece_PublicKey(), Botan::pbes2_decrypt(), Botan::OCSP::Response::Response(), Botan::RSA_PublicKey::RSA_PublicKey(), and Botan::TLS::Session::Session().

301  {
302  if(!m_parent)
303  throw Invalid_State("BER_Decoder::end_cons called with null parent");
304  if(!m_source->end_of_data())
305  throw Decoding_Error("BER_Decoder::end_cons called with data left");
306  return (*m_parent);
307  }
virtual bool end_of_data() const =0

◆ get_next()

BER_Decoder& Botan::BER_Decoder::get_next ( BER_Object ber)
inline

Definition at line 66 of file ber_dec.h.

Referenced by Botan::OCSP::SingleResponse::decode_from().

67  {
68  ber = get_next_object();
69  return (*this);
70  }
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ get_next_object()

BER_Object Botan::BER_Decoder::get_next_object ( )

Get the next object in the data stream. If EOF, returns an object with type NO_OBJECT.

Definition at line 237 of file ber_dec.cpp.

References Botan::DataSource::check_available(), Botan::EOC, Botan::BER_Object::is_set(), Botan::DataSource::read(), and Botan::BER_Object::tagging().

Referenced by decode(), Botan::ASN1_String::decode_from(), Botan::OID::decode_from(), Botan::AlternativeName::decode_from(), Botan::X509_Time::decode_from(), Botan::GeneralName::decode_from(), decode_null(), decode_optional(), decode_optional_implicit(), and start_cons().

238  {
239  BER_Object next;
240 
241  if(m_pushed.is_set())
242  {
243  std::swap(next, m_pushed);
244  return next;
245  }
246 
247  for(;;)
248  {
249  ASN1_Tag type_tag, class_tag;
250  decode_tag(m_source, type_tag, class_tag);
251  next.set_tagging(type_tag, class_tag);
252  if(next.is_set() == false) // no more objects
253  return next;
254 
255  size_t field_size;
256  const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
257  if(!m_source->check_available(length))
258  throw BER_Decoding_Error("Value truncated");
259 
260  uint8_t* out = next.mutable_bits(length);
261  if(m_source->read(out, length) != length)
262  throw BER_Decoding_Error("Value truncated");
263 
264  if(next.tagging() == EOC)
265  continue;
266  else
267  break;
268  }
269 
270  return next;
271  }
virtual bool check_available(size_t n)=0
ASN1_Tag
Definition: asn1_obj.h:22
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0
bool is_set() const
Definition: asn1_obj.h:111

◆ get_next_octet_string()

std::vector<uint8_t> Botan::BER_Decoder::get_next_octet_string ( )
inline

Definition at line 191 of file ber_dec.h.

References Botan::PEM_Code::decode(), and Botan::OCTET_STRING.

Referenced by Botan::OCSP::Response::Response().

192  {
193  std::vector<uint8_t> out_vec;
194  decode(out_vec, OCTET_STRING);
195  return out_vec;
196  }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170

◆ get_next_value()

template<typename T >
BER_Decoder& Botan::BER_Decoder::get_next_value ( T out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)
inline

Get next object and copy value to POD type Asserts value length is equal to POD type sizeof. Asserts Type tag and optional Class tag according to parameters. Copy value to POD type (struct, union, C-style array, std::array, etc.).

Parameters
outPOD type reference where to copy object value
type_tagASN1_Tag enum to assert type on object read
class_tagASN1_Tag enum to assert class on object read (default: CONTEXT_SPECIFIC)
Returns
this reference

Definition at line 131 of file ber_dec.h.

References Botan::BER_Object::assert_is_a(), Botan::BER_Object::bits(), Botan::copy_mem(), Botan::BER_Object::length(), T, and Botan::ASN1::to_string().

134  {
135  static_assert(std::is_pod<T>::value, "Type must be POD");
136 
137  BER_Object obj = get_next_object();
138  obj.assert_is_a(type_tag, class_tag);
139 
140  if (obj.length() != sizeof(T))
141  throw BER_Decoding_Error(
142  "Size mismatch. Object value size is " +
143  std::to_string(obj.length()) +
144  "; Output type size is " +
145  std::to_string(sizeof(T)));
146 
147  copy_mem(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
148 
149  return (*this);
150  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:210
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
BER_Object get_next_object()
Definition: ber_dec.cpp:237
fe T
Definition: ge.cpp:37

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Return true if there is at least one more item remaining

Definition at line 198 of file ber_dec.cpp.

References Botan::DataSource::end_of_data(), and Botan::BER_Object::is_set().

Referenced by Botan::PK_Verifier::check_signature(), Botan::AlternativeName::decode_from(), Botan::X509_DN::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Extensions::decode_from(), decode_list(), and Botan::OCSP::Response::Response().

199  {
200  if(m_source->end_of_data() && !m_pushed.is_set())
201  return false;
202  return true;
203  }
virtual bool end_of_data() const =0
bool is_set() const
Definition: asn1_obj.h:111

◆ operator=()

BER_Decoder& Botan::BER_Decoder::operator= ( const BER_Decoder )
delete

◆ push_back() [1/2]

void Botan::BER_Decoder::push_back ( const BER_Object obj)

Push an object back onto the stream. Throws if another object was previously pushed and has not been subsequently read out.

Definition at line 276 of file ber_dec.cpp.

References Botan::BER_Object::is_set().

Referenced by decode_list(), decode_optional(), decode_optional_implicit(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), and raw_bytes().

277  {
278  if(m_pushed.is_set())
279  throw Invalid_State("BER_Decoder: Only one push back is allowed");
280  m_pushed = obj;
281  }
bool is_set() const
Definition: asn1_obj.h:111

◆ push_back() [2/2]

void Botan::BER_Decoder::push_back ( BER_Object &&  obj)

Push an object back onto the stream. Throws if another object was previously pushed and has not been subsequently read out.

Definition at line 283 of file ber_dec.cpp.

References Botan::BER_Object::is_set().

284  {
285  if(m_pushed.is_set())
286  throw Invalid_State("BER_Decoder: Only one push back is allowed");
287  m_pushed = std::move(obj);
288  }
bool is_set() const
Definition: asn1_obj.h:111

◆ raw_bytes()

template<typename Alloc >
BER_Decoder& Botan::BER_Decoder::raw_bytes ( std::vector< uint8_t, Alloc > &  out)
inline

Definition at line 156 of file ber_dec.h.

References push_back().

Referenced by Botan::Attribute::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::X509_DN::decode_from(), Botan::X509_Object::decode_from(), and Botan::OCSP::Response::Response().

157  {
158  out.clear();
159  uint8_t buf;
160  while(m_source->read_byte(buf))
161  out.push_back(buf);
162  return (*this);
163  }
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:23

◆ start_cons()

BER_Decoder Botan::BER_Decoder::start_cons ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag = UNIVERSAL 
)

Start decoding a constructed data (sequence or set)

Definition at line 290 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), BER_Decoder(), Botan::CONSTRUCTED, and get_next_object().

Referenced by Botan::PK_Verifier::check_signature(), Botan::Attribute::decode_from(), Botan::AlternativeName::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::X509_DN::decode_from(), Botan::CRL_Entry::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::X509_Object::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::Extensions::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), decode_list(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::X509::load_key(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), Botan::McEliece_PublicKey::McEliece_PublicKey(), Botan::pbes2_decrypt(), Botan::OCSP::Response::Response(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::RSA_PublicKey::RSA_PublicKey(), and Botan::TLS::Session::Session().

291  {
292  BER_Object obj = get_next_object();
293  obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
294  return BER_Decoder(std::move(obj), this);
295  }
ASN1_Tag
Definition: asn1_obj.h:22
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:327
BER_Object get_next_object()
Definition: ber_dec.cpp:237

◆ verify_end() [1/2]

BER_Decoder & Botan::BER_Decoder::verify_end ( )

Verify the stream is concluded, throws otherwise. Returns (*this)

Definition at line 208 of file ber_dec.cpp.

Referenced by Botan::AlternativeName::decode_from(), Botan::Extensions::decode_from(), decode_optional_string(), and Botan::TLS::Session::Session().

209  {
210  return verify_end("BER_Decoder::verify_end called, but data remains");
211  }
BER_Decoder & verify_end()
Definition: ber_dec.cpp:208

◆ verify_end() [2/2]

BER_Decoder & Botan::BER_Decoder::verify_end ( const std::string &  err_msg)

Verify the stream is concluded, throws otherwise. Returns (*this)

Definition at line 216 of file ber_dec.cpp.

References Botan::DataSource::end_of_data(), and Botan::BER_Object::is_set().

217  {
218  if(!m_source->end_of_data() || m_pushed.is_set())
219  throw Decoding_Error(err);
220  return (*this);
221  }
virtual bool end_of_data() const =0
bool is_set() const
Definition: asn1_obj.h:111

The documentation for this class was generated from the following files: