Botan  1.10.9
Public Member Functions | List of all members
Botan::OID Class Reference

#include <asn1_oid.h>

Inheritance diagram for Botan::OID:
Botan::ASN1_Object

Public Member Functions

std::string as_string () const
 
void clear ()
 
void decode_from (class BER_Decoder &)
 
void encode_into (class DER_Encoder &) const
 
std::vector< u32bitget_id () const
 
bool is_empty () const
 
 OID (const std::string &str="")
 
OIDoperator+= (u32bit new_comp)
 
bool operator== (const OID &) const
 

Detailed Description

This class represents ASN.1 object identifiers.

Definition at line 20 of file asn1_oid.h.

Constructor & Destructor Documentation

Botan::OID::OID ( const std::string &  str = "")

Construct an OID from a string.

Parameters
stra string in the form "a.b.c" etc., where a,b,c are numbers

Definition at line 19 of file asn1_oid.cpp.

References Botan::parse_asn1_oid().

20  {
21  if(oid_str != "")
22  {
23  try
24  {
25  id = parse_asn1_oid(oid_str);
26  }
27  catch(...)
28  {
29  throw Invalid_OID(oid_str);
30  }
31 
32  if(id.size() < 2 || id[0] > 2)
33  throw Invalid_OID(oid_str);
34  if((id[0] == 0 || id[0] == 1) && id[1] > 39)
35  throw Invalid_OID(oid_str);
36  }
37  }
std::vector< u32bit > parse_asn1_oid(const std::string &oid)
Definition: parsing.cpp:180

Member Function Documentation

std::string Botan::OID::as_string ( ) const

Get this OID as a string

Returns
string representing this OID

Definition at line 50 of file asn1_oid.cpp.

References Botan::to_string().

Referenced by Botan::OIDS::add_oid(), Botan::Extensions::decode_from(), Botan::EC_Group::EC_Group(), Botan::get_pbe(), Botan::X509_Object::hash_used_for_signature(), Botan::PKCS8::load_key(), Botan::OIDS::lookup(), Botan::make_private_key(), and Botan::make_public_key().

51  {
52  std::string oid_str;
53  for(size_t i = 0; i != id.size(); ++i)
54  {
55  oid_str += to_string(id[i]);
56  if(i != id.size() - 1)
57  oid_str += '.';
58  }
59  return oid_str;
60  }
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
void Botan::OID::clear ( )

Reset this instance to an empty OID.

Definition at line 42 of file asn1_oid.cpp.

Referenced by decode_from().

43  {
44  id.clear();
45  }
void Botan::OID::decode_from ( class BER_Decoder from)
virtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 155 of file asn1_oid.cpp.

References Botan::BER_Object::class_tag, clear(), Botan::BER_Decoder::get_next_object(), Botan::OBJECT_ID, Botan::MemoryRegion< T >::size(), Botan::BER_Object::type_tag, Botan::UNIVERSAL, and Botan::BER_Object::value.

156  {
157  BER_Object obj = decoder.get_next_object();
158  if(obj.type_tag != OBJECT_ID || obj.class_tag != UNIVERSAL)
159  throw BER_Bad_Tag("Error decoding OID, unknown tag",
160  obj.type_tag, obj.class_tag);
161  if(obj.value.size() < 2)
162  throw BER_Decoding_Error("OID encoding is too short");
163 
164 
165  clear();
166  id.push_back(obj.value[0] / 40);
167  id.push_back(obj.value[0] % 40);
168 
169  size_t i = 0;
170  while(i != obj.value.size() - 1)
171  {
172  u32bit component = 0;
173  while(i != obj.value.size() - 1)
174  {
175  ++i;
176  component = (component << 7) + (obj.value[i] & 0x7F);
177  if(!(obj.value[i] & 0x80))
178  break;
179  }
180  id.push_back(component);
181  }
182  }
void clear()
Definition: asn1_oid.cpp:42
unsigned int u32bit
Definition: types.h:32
void Botan::OID::encode_into ( class DER_Encoder to) const
virtual

Encode whatever this object is into to

Parameters
tothe DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 127 of file asn1_oid.cpp.

References Botan::DER_Encoder::add_object(), Botan::high_bit(), Botan::OBJECT_ID, Botan::MemoryRegion< T >::push_back(), and Botan::UNIVERSAL.

128  {
129  if(id.size() < 2)
130  throw Invalid_Argument("OID::encode_into: OID is invalid");
131 
132  MemoryVector<byte> encoding;
133  encoding.push_back(40 * id[0] + id[1]);
134 
135  for(size_t i = 2; i != id.size(); ++i)
136  {
137  if(id[i] == 0)
138  encoding.push_back(0);
139  else
140  {
141  size_t blocks = high_bit(id[i]) + 6;
142  blocks = (blocks - (blocks % 7)) / 7;
143 
144  for(size_t j = 0; j != blocks - 1; ++j)
145  encoding.push_back(0x80 | ((id[i] >> 7*(blocks-j-1)) & 0x7F));
146  encoding.push_back(id[i] & 0x7F);
147  }
148  }
149  der.add_object(OBJECT_ID, UNIVERSAL, encoding);
150  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
size_t high_bit(T n)
Definition: bit_ops.h:33
std::vector<u32bit> Botan::OID::get_id ( ) const
inline

Get this OID as list (vector) of its components.

Returns
vector representing this OID

Definition at line 36 of file asn1_oid.h.

Referenced by Botan::operator<().

36 { return id; }
bool Botan::OID::is_empty ( ) const
inline

Find out whether this OID is empty

Returns
true is no OID value is set

Definition at line 30 of file asn1_oid.h.

30 { return id.size() == 0; }
OID & Botan::OID::operator+= ( u32bit  new_comp)

Add a component to this OID.

Parameters
new_compthe new component to add to the end of this OID
Returns
reference to *this

Definition at line 78 of file asn1_oid.cpp.

79  {
80  id.push_back(component);
81  return (*this);
82  }
bool Botan::OID::operator== ( const OID oid) const

Compare two OIDs.

Returns
true if they are equal, false otherwise

Definition at line 65 of file asn1_oid.cpp.

66  {
67  if(id.size() != oid.id.size())
68  return false;
69  for(size_t i = 0; i != id.size(); ++i)
70  if(id[i] != oid.id[i])
71  return false;
72  return true;
73  }
OID oid
Definition: x509_ext.cpp:446

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