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

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (DataSource &)
 
 BER_Decoder (const uint8_t[], size_t)
 
 BER_Decoder (const secure_vector< uint8_t > &)
 
 BER_Decoder (const std::vector< uint8_t > &vec)
 
 BER_Decoder (const BER_Decoder &)
 
BER_Decoderdecode (bool &v)
 
BER_Decoderdecode (size_t &v)
 
BER_Decoderdecode (class BigInt &v)
 
BER_Decoderdecode (std::vector< uint8_t > &v, ASN1_Tag type_tag)
 
BER_Decoderdecode (secure_vector< uint8_t > &v, ASN1_Tag type_tag)
 
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 (class 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 (class 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)
 
BER_Decoderraw_bytes (secure_vector< uint8_t > &v)
 
BER_Decoderraw_bytes (std::vector< uint8_t > &v)
 
BER_Decoder start_cons (ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
 
BER_Decoderverify_end ()
 
 ~BER_Decoder ()
 

Detailed Description

BER Decoding Object

Definition at line 19 of file ber_dec.h.

Constructor & Destructor Documentation

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

Definition at line 284 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

Referenced by decode_optional().

285  {
286  m_source = &src;
287  m_owns = false;
288  m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT;
289  m_parent = nullptr;
290  }
ASN1_Tag class_tag
Definition: asn1_obj.h:91
ASN1_Tag type_tag
Definition: asn1_obj.h:91
Botan::BER_Decoder::BER_Decoder ( const uint8_t  data[],
size_t  length 
)

Definition at line 295 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

296  {
297  m_source = new DataSource_Memory(data, length);
298  m_owns = true;
299  m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT;
300  m_parent = nullptr;
301  }
ASN1_Tag class_tag
Definition: asn1_obj.h:91
ASN1_Tag type_tag
Definition: asn1_obj.h:91
Botan::BER_Decoder::BER_Decoder ( const secure_vector< uint8_t > &  data)
explicit

Definition at line 306 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

307  {
308  m_source = new DataSource_Memory(data);
309  m_owns = true;
310  m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT;
311  m_parent = nullptr;
312  }
ASN1_Tag class_tag
Definition: asn1_obj.h:91
ASN1_Tag type_tag
Definition: asn1_obj.h:91
Botan::BER_Decoder::BER_Decoder ( const std::vector< uint8_t > &  vec)
explicit

Definition at line 317 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

318  {
319  m_source = new DataSource_Memory(data.data(), data.size());
320  m_owns = true;
321  m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT;
322  m_parent = nullptr;
323  }
ASN1_Tag class_tag
Definition: asn1_obj.h:91
ASN1_Tag type_tag
Definition: asn1_obj.h:91
Botan::BER_Decoder::BER_Decoder ( const BER_Decoder other)

Definition at line 328 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

329  {
330  m_source = other.m_source;
331  m_owns = false;
332  if(other.m_owns)
333  {
334  other.m_owns = false;
335  m_owns = true;
336  }
337  m_pushed.type_tag = m_pushed.class_tag = NO_OBJECT;
338  m_parent = other.m_parent;
339  }
ASN1_Tag class_tag
Definition: asn1_obj.h:91
ASN1_Tag type_tag
Definition: asn1_obj.h:91
Botan::BER_Decoder::~BER_Decoder ( )

Definition at line 344 of file ber_dec.cpp.

345  {
346  if(m_owns)
347  delete m_source;
348  m_source = nullptr;
349  }

Member Function Documentation

BER_Decoder & Botan::BER_Decoder::decode ( bool &  v)

Definition at line 376 of file ber_dec.cpp.

References Botan::BOOLEAN, and Botan::UNIVERSAL.

Referenced by Botan::DL_Group::BER_decode(), Botan::PK_Verifier::check_signature(), Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), Botan::Curve25519_PublicKey::Curve25519_PublicKey(), decode(), decode_constrained_integer(), Botan::Attribute::decode_from(), Botan::AlternativeName::decode_from(), Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::CRL_Entry::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::X509_Object::decode_from(), Botan::Extensions::decode_from(), Botan::GeneralSubtree::decode_from(), decode_list(), decode_octet_string_bigint(), decode_optional(), decode_optional_implicit(), decode_optional_string(), Botan::DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(), Botan::DL_Scheme_PublicKey::DL_Scheme_PublicKey(), Botan::EC_Group::EC_Group(), get_next_octet_string(), Botan::GOST_3410_PublicKey::GOST_3410_PublicKey(), 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().

377  {
378  return decode(out, BOOLEAN, UNIVERSAL);
379  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Decoder & Botan::BER_Decoder::decode ( size_t &  v)

Definition at line 384 of file ber_dec.cpp.

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

385  {
386  return decode(out, INTEGER, UNIVERSAL);
387  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt v)

Definition at line 392 of file ber_dec.cpp.

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

393  {
394  return decode(out, INTEGER, UNIVERSAL);
395  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Decoder & Botan::BER_Decoder::decode ( std::vector< uint8_t > &  v,
ASN1_Tag  type_tag 
)

Definition at line 514 of file ber_dec.cpp.

References decode(), and Botan::UNIVERSAL.

515  {
516  return decode(out, real_type, real_type, UNIVERSAL);
517  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Decoder & Botan::BER_Decoder::decode ( secure_vector< uint8_t > &  v,
ASN1_Tag  type_tag 
)

Definition at line 506 of file ber_dec.cpp.

References decode(), and Botan::UNIVERSAL.

507  {
508  return decode(out, real_type, real_type, UNIVERSAL);
509  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Decoder & Botan::BER_Decoder::decode ( bool &  v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 415 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), get_next_object(), and Botan::BER_Object::value.

417  {
418  BER_Object obj = get_next_object();
419  obj.assert_is_a(type_tag, class_tag);
420 
421  if(obj.value.size() != 1)
422  throw BER_Decoding_Error("BER boolean value had invalid size");
423 
424  out = (obj.value[0]) ? true : false;
425  return (*this);
426  }
BER_Object get_next_object()
Definition: ber_dec.cpp:210
BER_Decoder & Botan::BER_Decoder::decode ( size_t &  v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 431 of file ber_dec.cpp.

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

433  {
434  BigInt integer;
435  decode(integer, type_tag, class_tag);
436 
437  if(integer.bits() > 32)
438  throw BER_Decoding_Error("Decoded integer value larger than expected");
439 
440  out = 0;
441  for(size_t i = 0; i != 4; ++i)
442  out = (out << 8) | integer.byte_at(3-i);
443 
444  return (*this);
445  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 473 of file ber_dec.cpp.

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

475  {
476  BER_Object obj = get_next_object();
477  obj.assert_is_a(type_tag, class_tag);
478 
479  if(obj.value.empty())
480  out = 0;
481  else
482  {
483  const bool negative = (obj.value[0] & 0x80) ? true : false;
484 
485  if(negative)
486  {
487  for(size_t i = obj.value.size(); i > 0; --i)
488  if(obj.value[i-1]--)
489  break;
490  for(size_t i = 0; i != obj.value.size(); ++i)
491  obj.value[i] = ~obj.value[i];
492  }
493 
494  out = BigInt(&obj.value[0], obj.value.size());
495 
496  if(negative)
497  out.flip_sign();
498  }
499 
500  return (*this);
501  }
BER_Object get_next_object()
Definition: ber_dec.cpp:210
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 547 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::BIT_STRING, Botan::copy_mem(), get_next_object(), Botan::OCTET_STRING, Botan::unlock(), and Botan::BER_Object::value.

550  {
551  if(real_type != OCTET_STRING && real_type != BIT_STRING)
552  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
553 
554  BER_Object obj = get_next_object();
555  obj.assert_is_a(type_tag, class_tag);
556 
557  if(real_type == OCTET_STRING)
558  buffer = unlock(obj.value);
559  else
560  {
561  if(obj.value.empty())
562  throw BER_Decoding_Error("Invalid BIT STRING");
563  if(obj.value[0] >= 8)
564  throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
565 
566  buffer.resize(obj.value.size() - 1);
567  copy_mem(buffer.data(), &obj.value[1], obj.value.size() - 1);
568  }
569  return (*this);
570  }
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
BER_Object get_next_object()
Definition: ber_dec.cpp:210
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125
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 522 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::BIT_STRING, Botan::copy_mem(), get_next_object(), Botan::OCTET_STRING, and Botan::BER_Object::value.

525  {
526  if(real_type != OCTET_STRING && real_type != BIT_STRING)
527  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
528 
529  BER_Object obj = get_next_object();
530  obj.assert_is_a(type_tag, class_tag);
531 
532  if(real_type == OCTET_STRING)
533  buffer = obj.value;
534  else
535  {
536  if(obj.value.empty())
537  throw BER_Decoding_Error("Invalid BIT STRING");
538  if(obj.value[0] >= 8)
539  throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
540 
541  buffer.resize(obj.value.size() - 1);
542  copy_mem(buffer.data(), &obj.value[1], obj.value.size() - 1);
543  }
544  return (*this);
545  }
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
BER_Object get_next_object()
Definition: ber_dec.cpp:210
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 354 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

356  {
357  obj.decode_from(*this);
358  return (*this);
359  }
template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_and_check ( const T &  expected,
const std::string &  error_msg 
)
inline

Definition at line 146 of file ber_dec.h.

References Botan::PEM_Code::decode().

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

148  {
149  T actual;
150  decode(actual);
151 
152  if(actual != expected)
153  throw Decoding_Error(error_msg);
154 
155  return (*this);
156  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
uint64_t Botan::BER_Decoder::decode_constrained_integer ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag,
size_t  T_bytes 
)

Definition at line 450 of file ber_dec.cpp.

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

453  {
454  if(T_bytes > 8)
455  throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
456 
457  BigInt integer;
458  decode(integer, type_tag, class_tag);
459 
460  if(integer.bits() > 8*T_bytes)
461  throw BER_Decoding_Error("Decoded integer value larger than expected");
462 
463  uint64_t out = 0;
464  for(size_t i = 0; i != 8; ++i)
465  out = (out << 8) | integer.byte_at(7-i);
466 
467  return out;
468  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_integer_type ( T &  out)
inline

Definition at line 111 of file ber_dec.h.

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

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

112  {
113  return decode_integer_type<T>(out, INTEGER, UNIVERSAL);
114  }
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 117 of file ber_dec.h.

120  {
121  out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
122  return (*this);
123  }
uint64_t decode_constrained_integer(ASN1_Tag type_tag, ASN1_Tag class_tag, size_t T_bytes)
Definition: ber_dec.cpp:450
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 272 of file ber_dec.h.

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

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

275  {
276  BER_Decoder list = start_cons(type_tag, class_tag);
277 
278  while(list.more_items())
279  {
280  T value;
281  list.decode(value);
282  vec.push_back(value);
283  }
284 
285  list.end_cons();
286 
287  return (*this);
288  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:284
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:258
BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 364 of file ber_dec.cpp.

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

365  {
366  BER_Object obj = get_next_object();
367  obj.assert_is_a(NULL_TAG, UNIVERSAL);
368  if(obj.value.size())
369  throw BER_Decoding_Error("NULL object had nonzero size");
370  return (*this);
371  }
BER_Object get_next_object()
Definition: ber_dec.cpp:210
BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( class BigInt b)

Definition at line 397 of file ber_dec.cpp.

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

Referenced by Botan::EC_Group::EC_Group().

398  {
399  secure_vector<uint8_t> out_vec;
400  decode(out_vec, OCTET_STRING);
401  out = BigInt::decode(out_vec.data(), out_vec.size());
402  return (*this);
403  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
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 213 of file ber_dec.h.

References BER_Decoder(), Botan::BER_Object::class_tag, Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, decode(), get_next_object(), push_back(), Botan::BER_Object::type_tag, and Botan::BER_Object::value.

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

217  {
218  BER_Object obj = get_next_object();
219 
220  if(obj.type_tag == type_tag && obj.class_tag == class_tag)
221  {
222  if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
223  BER_Decoder(obj.value).decode(out).verify_end();
224  else
225  {
226  push_back(obj);
227  decode(out, type_tag, class_tag);
228  }
229  }
230  else
231  {
232  out = default_value;
233  push_back(obj);
234  }
235 
236  return (*this);
237  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:284
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:248
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Object get_next_object()
Definition: ber_dec.cpp:210
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 243 of file ber_dec.h.

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

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

250  {
251  BER_Object obj = get_next_object();
252 
253  if(obj.type_tag == type_tag && obj.class_tag == class_tag)
254  {
255  obj.type_tag = real_type;
256  obj.class_tag = real_class;
257  push_back(obj);
258  decode(out, real_type, real_class);
259  }
260  else
261  {
262  out = default_value;
263  push_back(obj);
264  }
265 
266  return (*this);
267  }
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:248
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
BER_Object get_next_object()
Definition: ber_dec.cpp:210
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 162 of file ber_dec.h.

References Botan::BER_Object::class_tag, Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, Botan::PEM_Code::decode(), decode(), Botan::BER_Object::type_tag, Botan::BER_Object::value, and verify_end().

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

166  {
167  BER_Object obj = get_next_object();
168 
169  ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
170 
171  if(obj.type_tag == type_tag && obj.class_tag == class_tag)
172  {
173  if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
174  BER_Decoder(obj.value).decode(out, real_type).verify_end();
175  else
176  {
177  push_back(obj);
178  decode(out, real_type, type_tag, class_tag);
179  }
180  }
181  else
182  {
183  out.clear();
184  push_back(obj);
185  }
186 
187  return (*this);
188  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:284
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:248
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
ASN1_Tag
Definition: asn1_obj.h:22
BER_Object get_next_object()
Definition: ber_dec.cpp:210
BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Definition at line 199 of file ber_dec.cpp.

References Botan::DataSource::read_byte().

Referenced by Botan::DL_Group::BER_decode().

200  {
201  uint8_t buf;
202  while(m_source->read_byte(buf))
203  ;
204  return (*this);
205  }
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:22
BER_Decoder & Botan::BER_Decoder::end_cons ( )
BER_Decoder & Botan::BER_Decoder::get_next ( BER_Object ber)

Definition at line 239 of file ber_dec.cpp.

References get_next_object().

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

240  {
241  ber = get_next_object();
242  return (*this);
243  }
BER_Object get_next_object()
Definition: ber_dec.cpp:210
BER_Object Botan::BER_Decoder::get_next_object ( )

Definition at line 210 of file ber_dec.cpp.

References Botan::DataSource::check_available(), Botan::BER_Object::class_tag, Botan::EOC, Botan::NO_OBJECT, Botan::DataSource::read(), Botan::BER_Object::type_tag, Botan::UNIVERSAL, and Botan::BER_Object::value.

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(), Botan::EC_Group::EC_Group(), get_next(), and start_cons().

211  {
212  BER_Object next;
213 
214  if(m_pushed.type_tag != NO_OBJECT)
215  {
216  next = m_pushed;
217  m_pushed.class_tag = m_pushed.type_tag = NO_OBJECT;
218  return next;
219  }
220 
221  decode_tag(m_source, next.type_tag, next.class_tag);
222  if(next.type_tag == NO_OBJECT)
223  return next;
224 
225  const size_t length = decode_length(m_source);
226  if(!m_source->check_available(length))
227  throw BER_Decoding_Error("Value truncated");
228 
229  next.value.resize(length);
230  if(m_source->read(next.value.data(), length) != length)
231  throw BER_Decoding_Error("Value truncated");
232 
233  if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
234  return get_next_object();
235 
236  return next;
237  }
virtual bool check_available(size_t n)=0
virtual size_t read(uint8_t out[], size_t length) BOTAN_WARN_UNUSED_RESULT=0
BER_Object get_next_object()
Definition: ber_dec.cpp:210
ASN1_Tag class_tag
Definition: asn1_obj.h:91
ASN1_Tag type_tag
Definition: asn1_obj.h:91
std::vector< uint8_t > Botan::BER_Decoder::get_next_octet_string ( )

Definition at line 405 of file ber_dec.cpp.

References decode(), and Botan::OCTET_STRING.

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

406  {
407  std::vector<uint8_t> out_vec;
408  decode(out_vec, OCTET_STRING);
409  return out_vec;
410  }
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
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 48 of file ber_dec.h.

References Botan::BER_Object::assert_is_a(), Botan::copy_mem(), Botan::ASN1::to_string(), and Botan::BER_Object::value.

51  {
52  static_assert(std::is_pod<T>::value, "Type must be POD");
53 
54  BER_Object obj = get_next_object();
55  obj.assert_is_a(type_tag, class_tag);
56 
57  if (obj.value.size() != sizeof(T))
58  throw BER_Decoding_Error(
59  "Size mismatch. Object value size is " +
60  std::to_string(obj.value.size()) +
61  "; Output type size is " +
62  std::to_string(sizeof(T)));
63 
64  copy_mem(reinterpret_cast<uint8_t*>(&out), obj.value.data(), obj.value.size());
65 
66  return (*this);
67  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
BER_Object get_next_object()
Definition: ber_dec.cpp:210
bool Botan::BER_Decoder::more_items ( ) const
BER_Decoder& Botan::BER_Decoder::operator= ( const BER_Decoder )
delete
void Botan::BER_Decoder::push_back ( const BER_Object obj)

Definition at line 248 of file ber_dec.cpp.

References Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

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

249  {
250  if(m_pushed.type_tag != NO_OBJECT)
251  throw Invalid_State("BER_Decoder: Only one push back is allowed");
252  m_pushed = obj;
253  }
ASN1_Tag type_tag
Definition: asn1_obj.h:91
BER_Decoder & Botan::BER_Decoder::raw_bytes ( secure_vector< uint8_t > &  v)

Definition at line 178 of file ber_dec.cpp.

References push_back(), and Botan::DataSource::read_byte().

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().

179  {
180  out.clear();
181  uint8_t buf;
182  while(m_source->read_byte(buf))
183  out.push_back(buf);
184  return (*this);
185  }
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:22
BER_Decoder & Botan::BER_Decoder::raw_bytes ( std::vector< uint8_t > &  v)

Definition at line 187 of file ber_dec.cpp.

References push_back(), and Botan::DataSource::read_byte().

188  {
189  out.clear();
190  uint8_t buf;
191  while(m_source->read_byte(buf))
192  out.push_back(buf);
193  return (*this);
194  }
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:22
BER_Decoder Botan::BER_Decoder::start_cons ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag = UNIVERSAL 
)

Definition at line 258 of file ber_dec.cpp.

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

Referenced by Botan::DL_Group::BER_decode(), Botan::PK_Verifier::check_signature(), Botan::Curve25519_PrivateKey::Curve25519_PrivateKey(), Botan::Curve25519_PublicKey::Curve25519_PublicKey(), Botan::Attribute::decode_from(), Botan::AlternativeName::decode_from(), Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::CRL_Entry::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::X509_Object::decode_from(), Botan::Extensions::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), decode_list(), Botan::EC_Group::EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::GOST_3410_PublicKey::GOST_3410_PublicKey(), 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().

260  {
261  BER_Object obj = get_next_object();
262  obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
263 
264  BER_Decoder result(obj.value.data(), obj.value.size());
265  result.m_parent = this;
266  return result;
267  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:284
ASN1_Tag
Definition: asn1_obj.h:22
BER_Object get_next_object()
Definition: ber_dec.cpp:210
BER_Decoder & Botan::BER_Decoder::verify_end ( )

Definition at line 168 of file ber_dec.cpp.

References Botan::DataSource::end_of_data(), Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

Referenced by Botan::DL_Group::BER_decode(), Botan::AlternativeName::decode_from(), Botan::Extensions::decode_from(), decode_optional_string(), Botan::EC_Group::EC_Group(), Botan::pbes2_decrypt(), and Botan::TLS::Session::Session().

169  {
170  if(!m_source->end_of_data() || (m_pushed.type_tag != NO_OBJECT))
171  throw Invalid_State("BER_Decoder::verify_end called, but data remains");
172  return (*this);
173  }
virtual bool end_of_data() const =0
ASN1_Tag type_tag
Definition: asn1_obj.h:91

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