Botan  1.11.15
Public Member Functions
Botan::BER_Decoder Class Reference

#include <ber_dec.h>

List of all members.

Public Member Functions

 BER_Decoder (DataSource &)
 BER_Decoder (const byte[], size_t)
 BER_Decoder (const secure_vector< byte > &)
 BER_Decoder (const std::vector< byte > &vec)
 BER_Decoder (const BER_Decoder &)
BER_Decoderdecode (bool &v)
BER_Decoderdecode (size_t &v)
BER_Decoderdecode (class BigInt &v)
BER_Decoderdecode (std::vector< byte > &v, ASN1_Tag type_tag)
BER_Decoderdecode (secure_vector< byte > &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< byte > &v, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (secure_vector< byte > &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)
u64bit 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< byte, Alloc > &out, ASN1_Tag real_type, u16bit 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< byteget_next_octet_string ()
bool more_items () const
BER_Decoderoperator= (const BER_Decoder &)
void push_back (const BER_Object &obj)
BER_Decoderraw_bytes (secure_vector< byte > &v)
BER_Decoderraw_bytes (std::vector< byte > &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

Definition at line 277 of file ber_dec.cpp.

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

Referenced by decode_optional().

   {
   source = &src;
   owns = false;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   parent = nullptr;
   }
Botan::BER_Decoder::BER_Decoder ( const byte  data[],
size_t  length 
)

Definition at line 288 of file ber_dec.cpp.

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

   {
   source = new DataSource_Memory(data, length);
   owns = true;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   parent = nullptr;
   }
Botan::BER_Decoder::BER_Decoder ( const secure_vector< byte > &  data)

Definition at line 299 of file ber_dec.cpp.

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

   {
   source = new DataSource_Memory(data);
   owns = true;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   parent = nullptr;
   }
Botan::BER_Decoder::BER_Decoder ( const std::vector< byte > &  vec)

Definition at line 310 of file ber_dec.cpp.

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

   {
   source = new DataSource_Memory(&data[0], data.size());
   owns = true;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   parent = nullptr;
   }

Definition at line 321 of file ber_dec.cpp.

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

   {
   source = other.source;
   owns = false;
   if(other.owns)
      {
      other.owns = false;
      owns = true;
      }
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   parent = other.parent;
   }

Definition at line 337 of file ber_dec.cpp.

   {
   if(owns)
      delete source;
   source = nullptr;
   }

Member Function Documentation

Definition at line 377 of file ber_dec.cpp.

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

   {
   return decode(out, INTEGER, UNIVERSAL);
   }

Definition at line 385 of file ber_dec.cpp.

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

   {
   return decode(out, INTEGER, UNIVERSAL);
   }
BER_Decoder & Botan::BER_Decoder::decode ( std::vector< byte > &  v,
ASN1_Tag  type_tag 
)

Definition at line 507 of file ber_dec.cpp.

References decode(), and Botan::UNIVERSAL.

   {
   return decode(out, real_type, real_type, UNIVERSAL);
   }
BER_Decoder & Botan::BER_Decoder::decode ( secure_vector< byte > &  v,
ASN1_Tag  type_tag 
)

Definition at line 499 of file ber_dec.cpp.

References decode(), and Botan::UNIVERSAL.

   {
   return decode(out, real_type, real_type, UNIVERSAL);
   }
BER_Decoder & Botan::BER_Decoder::decode ( bool &  v,
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(), get_next_object(), and Botan::BER_Object::value.

   {
   BER_Object obj = get_next_object();
   obj.assert_is_a(type_tag, class_tag);

   if(obj.value.size() != 1)
      throw BER_Decoding_Error("BER boolean value had invalid size");

   out = (obj.value[0]) ? true : false;
   return (*this);
   }
BER_Decoder & Botan::BER_Decoder::decode ( size_t &  v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 424 of file ber_dec.cpp.

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

   {
   BigInt integer;
   decode(integer, type_tag, class_tag);

   if(integer.bits() > 32)
      throw BER_Decoding_Error("Decoded integer value larger than expected");

   out = 0;
   for(size_t i = 0; i != 4; ++i)
      out = (out << 8) | integer.byte_at(3-i);

   return (*this);
   }
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt v,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 466 of file ber_dec.cpp.

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

   {
   BER_Object obj = get_next_object();
   obj.assert_is_a(type_tag, class_tag);

   if(obj.value.empty())
      out = 0;
   else
      {
      const bool negative = (obj.value[0] & 0x80) ? true : false;

      if(negative)
         {
         for(size_t i = obj.value.size(); i > 0; --i)
            if(obj.value[i-1]--)
               break;
         for(size_t i = 0; i != obj.value.size(); ++i)
            obj.value[i] = ~obj.value[i];
         }

      out = BigInt(&obj.value[0], obj.value.size());

      if(negative)
         out.flip_sign();
      }

   return (*this);
   }
BER_Decoder & Botan::BER_Decoder::decode ( std::vector< byte > &  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::BER_Object::assert_is_a(), Botan::BIT_STRING, Botan::copy_mem(), get_next_object(), Botan::OCTET_STRING, Botan::unlock(), and Botan::BER_Object::value.

   {
   if(real_type != OCTET_STRING && real_type != BIT_STRING)
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);

   BER_Object obj = get_next_object();
   obj.assert_is_a(type_tag, class_tag);

   if(real_type == OCTET_STRING)
      buffer = unlock(obj.value);
   else
      {
      if(obj.value[0] >= 8)
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");

      buffer.resize(obj.value.size() - 1);
      copy_mem(&buffer[0], &obj.value[1], obj.value.size() - 1);
      }
   return (*this);
   }
BER_Decoder & Botan::BER_Decoder::decode ( secure_vector< byte > &  v,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 515 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.

   {
   if(real_type != OCTET_STRING && real_type != BIT_STRING)
      throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);

   BER_Object obj = get_next_object();
   obj.assert_is_a(type_tag, class_tag);

   if(real_type == OCTET_STRING)
      buffer = obj.value;
   else
      {
      if(obj.value[0] >= 8)
         throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");

      buffer.resize(obj.value.size() - 1);
      copy_mem(&buffer[0], &obj.value[1], obj.value.size() - 1);
      }
   return (*this);
   }
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 347 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

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

Definition at line 114 of file ber_dec.h.

References Botan::BER::decode().

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

         {
         T actual;
         decode(actual);

         if(actual != expected)
            throw Decoding_Error(error_msg);

         return (*this);
         }
u64bit Botan::BER_Decoder::decode_constrained_integer ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag,
size_t  T_bytes 
)

Definition at line 443 of file ber_dec.cpp.

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

   {
   if(T_bytes > 8)
      throw BER_Decoding_Error("Can't decode small integer over 8 bytes");

   BigInt integer;
   decode(integer, type_tag, class_tag);

   if(integer.bits() > 8*T_bytes)
      throw BER_Decoding_Error("Decoded integer value larger than expected");

   u64bit out = 0;
   for(size_t i = 0; i != 8; ++i)
      out = (out << 8) | integer.byte_at(7-i);

   return out;
   }
template<typename T >
BER_Decoder& Botan::BER_Decoder::decode_integer_type ( T &  out) [inline]

Definition at line 79 of file ber_dec.h.

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

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

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

         {
         out = decode_constrained_integer(type_tag, class_tag, sizeof(out));
         return (*this);
         }
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 240 of file ber_dec.h.

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

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

   {
   BER_Decoder list = start_cons(type_tag, class_tag);

   while(list.more_items())
      {
      T value;
      list.decode(value);
      vec.push_back(value);
      }

   list.end_cons();

   return (*this);
   }

Definition at line 357 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.

   {
   BER_Object obj = get_next_object();
   obj.assert_is_a(NULL_TAG, UNIVERSAL);
   if(obj.value.size())
      throw BER_Decoding_Error("NULL object had nonzero size");
   return (*this);
   }

Definition at line 390 of file ber_dec.cpp.

References decode(), and Botan::OCTET_STRING.

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

   {
   secure_vector<byte> out_vec;
   decode(out_vec, OCTET_STRING);
   out = BigInt::decode(&out_vec[0], out_vec.size());
   return (*this);
   }
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 181 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::EC_PrivateKey::EC_PrivateKey(), Botan::pbes2_decrypt(), and Botan::OCSP::Response::Response().

   {
   BER_Object obj = get_next_object();

   if(obj.type_tag == type_tag && obj.class_tag == class_tag)
      {
      if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
         BER_Decoder(obj.value).decode(out).verify_end();
      else
         {
         push_back(obj);
         decode(out, type_tag, class_tag);
         }
      }
   else
      {
      out = default_value;
      push_back(obj);
      }

   return (*this);
   }
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 211 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().

   {
   BER_Object obj = get_next_object();

   if(obj.type_tag == type_tag && obj.class_tag == class_tag)
      {
      obj.type_tag = real_type;
      obj.class_tag = real_class;
      push_back(obj);
      decode(out, real_type, real_class);
      }
   else
      {
      out = default_value;
      push_back(obj);
      }

   return (*this);
   }
template<typename Alloc >
BER_Decoder& Botan::BER_Decoder::decode_optional_string ( std::vector< byte, Alloc > &  out,
ASN1_Tag  real_type,
u16bit  type_no,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
) [inline]

Definition at line 130 of file ber_dec.h.

References Botan::BER_Object::class_tag, Botan::CONSTRUCTED, Botan::CONTEXT_SPECIFIC, Botan::BER::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().

         {
         BER_Object obj = get_next_object();

         ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);

         if(obj.type_tag == type_tag && obj.class_tag == class_tag)
            {
            if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
               BER_Decoder(obj.value).decode(out, real_type).verify_end();
            else
               {
               push_back(obj);
               decode(out, real_type, type_tag, class_tag);
               }
            }
         else
            {
            out.clear();
            push_back(obj);
            }

         return (*this);
         }

Definition at line 195 of file ber_dec.cpp.

References Botan::DataSource::read_byte().

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

   {
   byte buf;
   while(source->read_byte(buf))
      ;
   return (*this);
   }

Definition at line 232 of file ber_dec.cpp.

References get_next_object().

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

   {
   ber = get_next_object();
   return (*this);
   }

Definition at line 206 of file ber_dec.cpp.

References 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 Botan::BER::decode(), decode(), Botan::ASN1_String::decode_from(), Botan::X509_Time::decode_from(), Botan::OID::decode_from(), Botan::AlternativeName::decode_from(), Botan::EAC_Time::decode_from(), Botan::ASN1_EAC_String::decode_from(), decode_null(), decode_optional(), decode_optional_implicit(), Botan::EC_Group::EC_Group(), get_next(), and start_cons().

   {
   BER_Object next;

   if(pushed.type_tag != NO_OBJECT)
      {
      next = pushed;
      pushed.class_tag = pushed.type_tag = NO_OBJECT;
      return next;
      }

   decode_tag(source, next.type_tag, next.class_tag);
   if(next.type_tag == NO_OBJECT)
      return next;

   size_t length = decode_length(source);
   next.value.resize(length);
   if(source->read(&next.value[0], length) != length)
      throw BER_Decoding_Error("Value truncated");

   if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
      return get_next_object();

   return next;
   }

Definition at line 398 of file ber_dec.cpp.

References decode(), and Botan::OCTET_STRING.

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

   {
   std::vector<byte> out_vec;
   decode(out_vec, OCTET_STRING);
   return out_vec;
   }
BER_Decoder& Botan::BER_Decoder::operator= ( const BER_Decoder )

Definition at line 241 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(), raw_bytes(), and Botan::OCSP::Response::Response().

   {
   if(pushed.type_tag != NO_OBJECT)
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
   pushed = obj;
   }
BER_Decoder & Botan::BER_Decoder::raw_bytes ( secure_vector< byte > &  v)
BER_Decoder & Botan::BER_Decoder::raw_bytes ( std::vector< byte > &  v)

Definition at line 183 of file ber_dec.cpp.

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

   {
   out.clear();
   byte buf;
   while(source->read_byte(buf))
      out.push_back(buf);
   return (*this);
   }

Definition at line 251 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::Extensions::decode_from(), Botan::X509_Object::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), Botan::EAC1_1_gen_CVC< Derived >::decode_info(), decode_list(), Botan::EC_Group::EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::ECDSA_Signature::ECDSA_Signature(), Botan::GOST_3410_PublicKey::GOST_3410_PublicKey(), Botan::IF_Scheme_PrivateKey::IF_Scheme_PrivateKey(), Botan::IF_Scheme_PublicKey::IF_Scheme_PublicKey(), Botan::X509::load_key(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), Botan::McEliece_PublicKey::McEliece_PublicKey(), Botan::pbes2_decrypt(), Botan::OCSP::Response::Response(), and Botan::TLS::Session::Session().

   {
   BER_Object obj = get_next_object();
   obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));

   BER_Decoder result(&obj.value[0], obj.value.size());
   result.parent = this;
   return result;
   }

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