Botan  1.10.12
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 byte[], size_t)
 
 BER_Decoder (const MemoryRegion< byte > &)
 
 BER_Decoder (const BER_Decoder &)
 
BER_Decoderdecode (bool &)
 
BER_Decoderdecode (size_t &)
 
BER_Decoderdecode (class BigInt &)
 
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag)
 
BER_Decoderdecode (bool &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (size_t &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (class BigInt &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (class ASN1_Object &)
 
template<typename T >
BER_Decoderdecode_and_check (const T &expected, const std::string &error_msg)
 
template<typename T >
BER_Decoderdecode_list (std::vector< T > &out, bool clear_out=true)
 
BER_Decoderdecode_null ()
 
BER_Decoderdecode_octet_string_bigint (class BigInt &)
 
template<typename T >
BER_Decoderdecode_optional (T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
 
BER_Decoderdecode_optional_string (MemoryRegion< byte > &, ASN1_Tag, u16bit)
 
BER_Decoderdiscard_remaining ()
 
BER_Decoderend_cons ()
 
BER_Object get_next_object ()
 
bool more_items () const
 
void push_back (const BER_Object &)
 
BER_Decoderraw_bytes (MemoryRegion< byte > &)
 
BER_Decoder start_cons (ASN1_Tag, ASN1_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)

Definition at line 261 of file ber_dec.cpp.

References Botan::NO_OBJECT.

262  {
263  source = &src;
264  owns = false;
265  pushed.type_tag = pushed.class_tag = NO_OBJECT;
266  parent = 0;
267  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::BER_Decoder ( const byte  data[],
size_t  length 
)

Definition at line 272 of file ber_dec.cpp.

References Botan::NO_OBJECT.

273  {
274  source = new DataSource_Memory(data, length);
275  owns = true;
276  pushed.type_tag = pushed.class_tag = NO_OBJECT;
277  parent = 0;
278  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::BER_Decoder ( const MemoryRegion< byte > &  data)

Definition at line 283 of file ber_dec.cpp.

References Botan::NO_OBJECT.

284  {
285  source = new DataSource_Memory(data);
286  owns = true;
287  pushed.type_tag = pushed.class_tag = NO_OBJECT;
288  parent = 0;
289  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::BER_Decoder ( const BER_Decoder other)

Definition at line 294 of file ber_dec.cpp.

References Botan::NO_OBJECT.

295  {
296  source = other.source;
297  owns = false;
298  if(other.owns)
299  {
300  other.owns = false;
301  owns = true;
302  }
303  pushed.type_tag = pushed.class_tag = NO_OBJECT;
304  parent = other.parent;
305  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::~BER_Decoder ( )

Definition at line 310 of file ber_dec.cpp.

311  {
312  if(owns)
313  delete source;
314  source = 0;
315  }

Member Function Documentation

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

Definition at line 341 of file ber_dec.cpp.

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

Referenced by Botan::DL_Group::BER_decode(), Botan::CMS_Encoder::can_compress_with(), Botan::Certificate_Req::Certificate_Req(), Botan::PK_Verifier::check_signature(), Botan::Cert_Extension::CRL_Number::copy(), Botan::CRL_Entry::decode_from(), Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), Botan::Extensions::decode_from(), Botan::AlternativeName::decode_from(), Botan::EAC1_1_gen_CVC< Derived >::decode_info(), decode_optional(), Botan::DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(), Botan::DL_Scheme_PublicKey::DL_Scheme_PublicKey(), Botan::EAC1_1_ADO::EAC1_1_ADO(), Botan::EC_Group::EC_Group(), Botan::ECDSA_Signature::ECDSA_Signature(), Botan::PBE_PKCS5v15::end_msg(), Botan::PBE_PKCS5v20::end_msg(), Botan::EAC1_1_ADO::get_car(), Botan::EAC1_1_CVC::get_chat_value(), Botan::Cert_Extension::Basic_Constraints::get_path_limit(), Botan::IF_Scheme_PublicKey::IF_Scheme_PublicKey(), Botan::CMS_Decoder::layer_info(), Botan::X509::load_key(), Botan::EAC1_1_Req::operator==(), Botan::PKCS10_Request::PKCS10_Request(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), Botan::X509_Certificate::X509_Certificate(), Botan::X509_CRL::X509_CRL(), and Botan::X509_Object::X509_Object().

342  {
343  return decode(out, BOOLEAN, UNIVERSAL);
344  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
BER_Decoder & Botan::BER_Decoder::decode ( size_t &  out)

Definition at line 349 of file ber_dec.cpp.

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

350  {
351  return decode(out, INTEGER, UNIVERSAL);
352  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt out)

Definition at line 357 of file ber_dec.cpp.

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

358  {
359  return decode(out, INTEGER, UNIVERSAL);
360  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
BER_Decoder & Botan::BER_Decoder::decode ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type 
)

Definition at line 441 of file ber_dec.cpp.

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

442  {
443  return decode(out, real_type, real_type, UNIVERSAL);
444  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
BER_Decoder & Botan::BER_Decoder::decode ( bool &  out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 373 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

375  {
376  BER_Object obj = get_next_object();
377  obj.assert_is_a(type_tag, class_tag);
378 
379  if(obj.value.size() != 1)
380  throw BER_Decoding_Error("BER boolean value had invalid size");
381 
382  out = (obj.value[0]) ? true : false;
383  return (*this);
384  }
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode ( size_t &  out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 389 of file ber_dec.cpp.

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

391  {
392  BigInt integer;
393  decode(integer, type_tag, class_tag);
394 
395  if(integer.bits() > 32)
396  throw BER_Decoding_Error("Decoded integer value larger than expected");
397 
398  out = 0;
399  for(size_t i = 0; i != 4; ++i)
400  out = (out << 8) | integer.byte_at(3-i);
401 
402  return (*this);
403  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 408 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::MemoryRegion< T >::empty(), Botan::BigInt::flip_sign(), Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

410  {
411  BER_Object obj = get_next_object();
412  obj.assert_is_a(type_tag, class_tag);
413 
414  if(obj.value.empty())
415  out = 0;
416  else
417  {
418  const bool negative = (obj.value[0] & 0x80) ? true : false;
419 
420  if(negative)
421  {
422  for(size_t i = obj.value.size(); i > 0; --i)
423  if(obj.value[i-1]--)
424  break;
425  for(size_t i = 0; i != obj.value.size(); ++i)
426  obj.value[i] = ~obj.value[i];
427  }
428 
429  out = BigInt(&obj.value[0], obj.value.size());
430 
431  if(negative)
432  out.flip_sign();
433  }
434 
435  return (*this);
436  }
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode ( MemoryRegion< byte > &  buffer,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 449 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::BIT_STRING, Botan::copy_mem(), Botan::MemoryRegion< T >::empty(), Botan::OCTET_STRING, Botan::MemoryRegion< T >::resize(), Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

452  {
453  if(real_type != OCTET_STRING && real_type != BIT_STRING)
454  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
455 
456  BER_Object obj = get_next_object();
457  obj.assert_is_a(type_tag, class_tag);
458 
459  if(real_type == OCTET_STRING)
460  buffer = obj.value;
461  else
462  {
463  if(obj.value.empty())
464  throw BER_Decoding_Error("Invalid BIT STRING");
465  if(obj.value[0] >= 8)
466  throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
467 
468  buffer.resize(obj.value.size() - 1);
469  copy_mem(&buffer[0], &obj.value[1], obj.value.size() - 1);
470  }
471  return (*this);
472  }
void resize(size_t n)
Definition: secmem.h:211
size_t size() const
Definition: secmem.h:29
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode ( class ASN1_Object obj)

Definition at line 320 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

321  {
322  obj.decode_from(*this);
323  return (*this);
324  }
template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_and_check ( const T &  expected,
const std::string &  error_msg 
)
inline

Definition at line 62 of file ber_dec.h.

References Botan::PEM_Code::decode().

Referenced by Botan::EC_Group::EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), and Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey().

64  {
65  T actual;
66  decode(actual);
67 
68  if(actual != expected)
69  throw Decoding_Error(error_msg);
70 
71  return (*this);
72  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
template<typename T >
BER_Decoder & Botan::BER_Decoder::decode_list ( std::vector< T > &  out,
bool  clear_out = true 
)

Definition at line 125 of file ber_dec.h.

References Botan::PEM_Code::decode().

Referenced by Botan::Cert_Extension::Issuer_Alternative_Name::Issuer_Alternative_Name().

126  {
127  if(clear_it)
128  vec.clear();
129 
130  while(more_items())
131  {
132  T value;
133  decode(value);
134  vec.push_back(value);
135  }
136  return (*this);
137  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
bool more_items() const
Definition: ber_dec.cpp:150
BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 329 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::NULL_TAG, Botan::MemoryRegion< T >::size(), Botan::UNIVERSAL, and Botan::BER_Object::value.

330  {
331  BER_Object obj = get_next_object();
332  obj.assert_is_a(NULL_TAG, UNIVERSAL);
333  if(obj.value.size())
334  throw BER_Decoding_Error("NULL object had nonzero size");
335  return (*this);
336  }
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( class BigInt out)

Definition at line 362 of file ber_dec.cpp.

References Botan::PEM_Code::decode(), Botan::BigInt::decode(), Botan::OCTET_STRING, and Botan::MemoryRegion< T >::size().

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

363  {
364  SecureVector<byte> out_vec;
365  decode(out_vec, OCTET_STRING);
366  out = BigInt::decode(&out_vec[0], out_vec.size());
367  return (*this);
368  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
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 95 of file ber_dec.h.

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

Referenced by Botan::Extensions::decode_from(), Botan::PBE_PKCS5v20::end_msg(), Botan::Cert_Extension::Basic_Constraints::get_path_limit(), Botan::X509_Certificate::X509_Certificate(), and Botan::X509_CRL::X509_CRL().

99  {
100  BER_Object obj = get_next_object();
101 
102  if(obj.type_tag == type_tag && obj.class_tag == class_tag)
103  {
104  if(class_tag & CONSTRUCTED)
105  BER_Decoder(obj.value).decode(out).verify_end();
106  else
107  {
108  push_back(obj);
109  decode(out, type_tag, class_tag);
110  }
111  }
112  else
113  {
114  out = default_value;
115  push_back(obj);
116  }
117 
118  return (*this);
119  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:261
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
void push_back(const BER_Object &)
Definition: ber_dec.cpp:225
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode_optional_string ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type,
u16bit  type_no 
)

Definition at line 477 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::MemoryRegion< T >::clear(), Botan::CONTEXT_SPECIFIC, Botan::PEM_Code::decode(), and Botan::BER_Object::type_tag.

Referenced by Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), and Botan::X509_Certificate::X509_Certificate().

480  {
481  BER_Object obj = get_next_object();
482 
483  ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
484 
485  out.clear();
486  push_back(obj);
487 
488  if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC)
489  decode(out, real_type, type_tag, CONTEXT_SPECIFIC);
490 
491  return (*this);
492  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
void push_back(const BER_Object &)
Definition: ber_dec.cpp:225
ASN1_Tag
Definition: asn1_int.h:19
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Definition at line 182 of file ber_dec.cpp.

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

183  {
184  byte buf;
185  while(source->read_byte(buf))
186  ;
187  return (*this);
188  }
unsigned char byte
Definition: types.h:22
size_t read_byte(byte &out)
Definition: data_src.cpp:19
BER_Decoder & Botan::BER_Decoder::end_cons ( )
BER_Object Botan::BER_Decoder::get_next_object ( )

Definition at line 193 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::EOC, Botan::NO_OBJECT, Botan::MemoryRegion< T >::resize(), Botan::BER_Object::type_tag, Botan::UNIVERSAL, and Botan::BER_Object::value.

Referenced by Botan::BER::decode(), Botan::ASN1_String::decode_from(), Botan::OID::decode_from(), Botan::EAC_Time::decode_from(), Botan::X509_Time::decode_from(), Botan::AlternativeName::decode_from(), Botan::ASN1_EAC_String::decode_from(), Botan::EC_Group::EC_Group(), Botan::Cert_Extension::Basic_Constraints::get_path_limit(), Botan::PKCS10_Request::PKCS10_Request(), Botan::X509_Certificate::X509_Certificate(), and Botan::X509_CRL::X509_CRL().

194  {
195  BER_Object next;
196 
197  if(pushed.type_tag != NO_OBJECT)
198  {
199  next = pushed;
200  pushed.class_tag = pushed.type_tag = NO_OBJECT;
201  return next;
202  }
203 
204  decode_tag(source, next.type_tag, next.class_tag);
205  if(next.type_tag == NO_OBJECT)
206  return next;
207 
208  const size_t length = decode_length(source);
209  if(!source->check_available(length))
210  throw BER_Decoding_Error("Value truncated");
211 
212  next.value.resize(length);
213  if(source->read(&next.value[0], length) != length)
214  throw BER_Decoding_Error("Value truncated");
215 
216  if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
217  return get_next_object();
218 
219  return next;
220  }
virtual bool check_available(size_t n)=0
virtual size_t read(byte out[], size_t length)=0
BER_Object get_next_object()
Definition: ber_dec.cpp:193
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
bool Botan::BER_Decoder::more_items ( ) const
void Botan::BER_Decoder::push_back ( const BER_Object obj)

Definition at line 225 of file ber_dec.cpp.

References Botan::NO_OBJECT.

226  {
227  if(pushed.type_tag != NO_OBJECT)
228  throw Invalid_State("BER_Decoder: Only one push back is allowed");
229  pushed = obj;
230  }
ASN1_Tag type_tag
Definition: asn1_int.h:82
BER_Decoder & Botan::BER_Decoder::raw_bytes ( MemoryRegion< byte > &  out)
BER_Decoder Botan::BER_Decoder::start_cons ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag = UNIVERSAL 
)

Definition at line 235 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::CONSTRUCTED, Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

Referenced by Botan::DL_Group::BER_decode(), Botan::CMS_Encoder::can_compress_with(), Botan::PK_Verifier::check_signature(), Botan::CRL_Entry::decode_from(), Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), Botan::Extensions::decode_from(), Botan::AlternativeName::decode_from(), Botan::EAC1_1_gen_CVC< Derived >::decode_info(), Botan::EAC1_1_ADO::EAC1_1_ADO(), Botan::EC_Group::EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::ECDSA_Signature::ECDSA_Signature(), Botan::PBE_PKCS5v15::end_msg(), Botan::PBE_PKCS5v20::end_msg(), Botan::EAC1_1_ADO::get_car(), Botan::EAC1_1_CVC::get_chat_value(), Botan::Cert_Extension::Basic_Constraints::get_path_limit(), Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(), Botan::IF_Scheme_PublicKey::IF_Scheme_PublicKey(), Botan::Cert_Extension::Issuer_Alternative_Name::Issuer_Alternative_Name(), Botan::CMS_Decoder::layer_info(), Botan::X509::load_key(), Botan::EAC1_1_Req::operator==(), Botan::DER_Encoder::start_explicit(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), Botan::X509_Certificate::X509_Certificate(), and Botan::X509_Object::X509_Object().

237  {
238  BER_Object obj = get_next_object();
239  obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
240 
241  BER_Decoder result(&obj.value[0], obj.value.size());
242  result.parent = this;
243  return result;
244  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:261
ASN1_Tag
Definition: asn1_int.h:19
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::verify_end ( )

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