Botan::BER_Decoder Class Reference

#include <ber_dec.h>

List of all members.

Public Member Functions

 BER_Decoder (const BER_Decoder &)
 BER_Decoder (const MemoryRegion< byte > &)
 BER_Decoder (const byte[], u32bit)
 BER_Decoder (DataSource &)
BER_Decoderdecode (class ASN1_Object &)
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (class BigInt &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (u32bit &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (bool &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag)
BER_Decoderdecode (class BigInt &)
BER_Decoderdecode (u32bit &)
BER_Decoderdecode (bool &)
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 257 of file ber_dec.cpp.

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

Referenced by decode_optional().

00258    {
00259    source = &src;
00260    owns = false;
00261    pushed.type_tag = pushed.class_tag = NO_OBJECT;
00262    parent = 0;
00263    }

Botan::BER_Decoder::BER_Decoder ( const byte  data[],
u32bit  length 
)

Definition at line 268 of file ber_dec.cpp.

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

00269    {
00270    source = new DataSource_Memory(data, length);
00271    owns = true;
00272    pushed.type_tag = pushed.class_tag = NO_OBJECT;
00273    parent = 0;
00274    }

Botan::BER_Decoder::BER_Decoder ( const MemoryRegion< byte > &  data  ) 

Definition at line 279 of file ber_dec.cpp.

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

00280    {
00281    source = new DataSource_Memory(data);
00282    owns = true;
00283    pushed.type_tag = pushed.class_tag = NO_OBJECT;
00284    parent = 0;
00285    }

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

Definition at line 290 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, owns, parent, source, and Botan::BER_Object::type_tag.

00291    {
00292    source = other.source;
00293    owns = false;
00294    if(other.owns)
00295       {
00296       other.owns = false;
00297       owns = true;
00298       }
00299    pushed.type_tag = pushed.class_tag = NO_OBJECT;
00300    parent = other.parent;
00301    }

Botan::BER_Decoder::~BER_Decoder (  ) 

Definition at line 306 of file ber_dec.cpp.

00307    {
00308    if(owns)
00309       delete source;
00310    source = 0;
00311    }


Member Function Documentation

BER_Decoder & Botan::BER_Decoder::decode ( class ASN1_Object obj  ) 

Definition at line 316 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

00317    {
00318    obj.decode_from(*this);
00319    return (*this);
00320    }

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 438 of file ber_dec.cpp.

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

00441    {
00442    if(real_type != OCTET_STRING && real_type != BIT_STRING)
00443       throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
00444 
00445    BER_Object obj = get_next_object();
00446    obj.assert_is_a(type_tag, class_tag);
00447 
00448    if(real_type == OCTET_STRING)
00449       buffer = obj.value;
00450    else
00451       {
00452       if(obj.value[0] >= 8)
00453          throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
00454       buffer.set(obj.value + 1, obj.value.size() - 1);
00455       }
00456    return (*this);
00457    }

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

Definition at line 397 of file ber_dec.cpp.

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

00399    {
00400    BER_Object obj = get_next_object();
00401    obj.assert_is_a(type_tag, class_tag);
00402 
00403    if(obj.value.empty())
00404       out = 0;
00405    else
00406       {
00407       const bool negative = (obj.value[0] & 0x80) ? true : false;
00408 
00409       if(negative)
00410          {
00411          for(u32bit j = obj.value.size(); j > 0; --j)
00412             if(obj.value[j-1]--)
00413                break;
00414          for(u32bit j = 0; j != obj.value.size(); ++j)
00415             obj.value[j] = ~obj.value[j];
00416          }
00417 
00418       out = BigInt(obj.value, obj.value.size());
00419 
00420       if(negative)
00421          out.flip_sign();
00422       }
00423 
00424    return (*this);
00425    }

BER_Decoder & Botan::BER_Decoder::decode ( u32bit out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 385 of file ber_dec.cpp.

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

00387    {
00388    BigInt integer;
00389    decode(integer, type_tag, class_tag);
00390    out = integer.to_u32bit();
00391    return (*this);
00392    }

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

Definition at line 369 of file ber_dec.cpp.

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

00371    {
00372    BER_Object obj = get_next_object();
00373    obj.assert_is_a(type_tag, class_tag);
00374 
00375    if(obj.value.size() != 1)
00376       throw BER_Decoding_Error("BER boolean value had invalid size");
00377 
00378    out = (obj.value[0]) ? true : false;
00379    return (*this);
00380    }

BER_Decoder & Botan::BER_Decoder::decode ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type 
)

Definition at line 430 of file ber_dec.cpp.

References decode(), and Botan::UNIVERSAL.

00431    {
00432    return decode(out, real_type, real_type, UNIVERSAL);
00433    }

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

Definition at line 353 of file ber_dec.cpp.

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

00354    {
00355    return decode(out, INTEGER, UNIVERSAL);
00356    }

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

Definition at line 345 of file ber_dec.cpp.

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

00346    {
00347    return decode(out, INTEGER, UNIVERSAL);
00348    }

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

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::BER::decode().

00064          {
00065          T actual;
00066          decode(actual);
00067 
00068          if(actual != expected)
00069             throw Decoding_Error(error_msg);
00070 
00071          return (*this);
00072          }

template<typename T >
BER_Decoder & Botan::BER_Decoder::decode_list ( std::vector< T > &  out,
bool  clear_out = true 
) [inline]

Definition at line 125 of file ber_dec.h.

References decode(), and more_items().

00126    {
00127    if(clear_it)
00128       vec.clear();
00129 
00130    while(more_items())
00131       {
00132       T value;
00133       decode(value);
00134       vec.push_back(value);
00135       }
00136    return (*this);
00137    }

BER_Decoder & Botan::BER_Decoder::decode_null (  ) 

Definition at line 325 of file ber_dec.cpp.

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

00326    {
00327    BER_Object obj = get_next_object();
00328    obj.assert_is_a(NULL_TAG, UNIVERSAL);
00329    if(obj.value.size())
00330       throw BER_Decoding_Error("NULL object had nonzero size");
00331    return (*this);
00332    }

BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( class BigInt out  ) 

Definition at line 358 of file ber_dec.cpp.

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

00359    {
00360    SecureVector<byte> out_vec;
00361    decode(out_vec, OCTET_STRING);
00362    out = BigInt::decode(&out_vec[0], out_vec.size());
00363    return (*this);
00364    }

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() 
) [inline]

Definition at line 95 of file ber_dec.h.

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

Referenced by Botan::Extensions::decode_from().

00099    {
00100    BER_Object obj = get_next_object();
00101 
00102    if(obj.type_tag == type_tag && obj.class_tag == class_tag)
00103       {
00104       if(class_tag & CONSTRUCTED)
00105          BER_Decoder(obj.value).decode(out).verify_end();
00106       else
00107          {
00108          push_back(obj);
00109          decode(out, type_tag, class_tag);
00110          }
00111       }
00112    else
00113       {
00114       out = default_value;
00115       push_back(obj);
00116       }
00117 
00118    return (*this);
00119    }

BER_Decoder & Botan::BER_Decoder::decode_optional_string ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type,
u16bit  type_no 
)

Definition at line 462 of file ber_dec.cpp.

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

00465    {
00466    BER_Object obj = get_next_object();
00467 
00468    ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
00469 
00470    out.clear();
00471    push_back(obj);
00472 
00473    if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC)
00474       decode(out, real_type, type_tag, CONTEXT_SPECIFIC);
00475 
00476    return (*this);
00477    }

BER_Decoder & Botan::BER_Decoder::discard_remaining (  ) 

Definition at line 181 of file ber_dec.cpp.

References Botan::DataSource::read_byte().

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

00182    {
00183    byte buf;
00184    while(source->read_byte(buf))
00185       ;
00186    return (*this);
00187    }

BER_Decoder & Botan::BER_Decoder::end_cons (  ) 

Definition at line 245 of file ber_dec.cpp.

References Botan::DataSource::end_of_data().

Referenced by Botan::Extensions::decode_from(), Botan::X509_DN::decode_from(), Botan::Attribute::decode_from(), Botan::AlgorithmIdentifier::decode_from(), and Botan::X509_DN::do_decode().

00246    {
00247    if(!parent)
00248       throw Invalid_State("BER_Decoder::end_cons called with NULL parent");
00249    if(!source->end_of_data())
00250       throw Decoding_Error("BER_Decoder::end_cons called with data left");
00251    return (*parent);
00252    }

BER_Object Botan::BER_Decoder::get_next_object (  ) 

Definition at line 192 of file ber_dec.cpp.

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

Referenced by Botan::BER::decode(), decode(), Botan::EAC_Time::decode_from(), Botan::ASN1_EAC_String::decode_from(), Botan::X509_Time::decode_from(), Botan::ASN1_String::decode_from(), Botan::OID::decode_from(), Botan::AlternativeName::decode_from(), decode_null(), decode_optional(), decode_optional_string(), Botan::EC_Domain_Params::EC_Domain_Params(), and start_cons().

00193    {
00194    BER_Object next;
00195 
00196    if(pushed.type_tag != NO_OBJECT)
00197       {
00198       next = pushed;
00199       pushed.class_tag = pushed.type_tag = NO_OBJECT;
00200       return next;
00201       }
00202 
00203    decode_tag(source, next.type_tag, next.class_tag);
00204    if(next.type_tag == NO_OBJECT)
00205       return next;
00206 
00207    u32bit length = decode_length(source);
00208    next.value.resize(length);
00209    if(source->read(next.value, length) != length)
00210       throw BER_Decoding_Error("Value truncated");
00211 
00212    if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
00213       return get_next_object();
00214 
00215    return next;
00216    }

bool Botan::BER_Decoder::more_items (  )  const

Definition at line 149 of file ber_dec.cpp.

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

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

00150    {
00151    if(source->end_of_data() && (pushed.type_tag == NO_OBJECT))
00152       return false;
00153    return true;
00154    }

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

Definition at line 221 of file ber_dec.cpp.

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

Referenced by decode_optional(), and decode_optional_string().

00222    {
00223    if(pushed.type_tag != NO_OBJECT)
00224       throw Invalid_State("BER_Decoder: Only one push back is allowed");
00225    pushed = obj;
00226    }

BER_Decoder & Botan::BER_Decoder::raw_bytes ( MemoryRegion< byte > &  out  ) 

Definition at line 169 of file ber_dec.cpp.

References Botan::MemoryRegion< T >::append(), Botan::MemoryRegion< T >::destroy(), and Botan::DataSource::read_byte().

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

00170    {
00171    out.destroy();
00172    byte buf;
00173    while(source->read_byte(buf))
00174       out.append(buf);
00175    return (*this);
00176    }

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

BER_Decoder & Botan::BER_Decoder::verify_end (  ) 

Definition at line 159 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::Extensions::decode_from(), Botan::AlternativeName::decode_from(), and Botan::X509_DN::do_decode().

00160    {
00161    if(!source->end_of_data() || (pushed.type_tag != NO_OBJECT))
00162       throw Invalid_State("BER_Decoder::verify_end called, but data remains");
00163    return (*this);
00164    }


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

Generated on Tue Jun 29 08:56:41 2010 for Botan by  doxygen 1.5.8