Botan  2.4.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::OID Class Referencefinal

#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 &) override
 
bool empty () const
 
void encode_into (class DER_Encoder &) const override
 
const std::vector< uint32_t > & get_id () const
 
 OID (const std::string &str="")
 
OIDoperator+= (uint32_t 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

◆ OID()

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.empty())
22  {
23  try
24  {
25  m_id = parse_asn1_oid(oid_str);
26  }
27  catch(...)
28  {
29  throw Invalid_OID(oid_str);
30  }
31 
32  if(m_id.size() < 2 || m_id[0] > 2)
33  throw Invalid_OID(oid_str);
34  if((m_id[0] == 0 || m_id[0] == 1) && m_id[1] > 39)
35  throw Invalid_OID(oid_str);
36  }
37  }
std::vector< uint32_t > parse_asn1_oid(const std::string &oid)
Definition: parsing.cpp:193

Member Function Documentation

◆ as_string()

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::ASN1::to_string().

Referenced by Botan::EC_Group::EC_Group(), Botan::X509_Object::hash_used_for_signature(), Botan::load_private_key(), Botan::load_public_key(), Botan::OIDS::lookup(), Botan::pbes2_decrypt(), Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(), Botan::ASN1_Formatter::print_to_stream(), and Botan::X509_Certificate::to_string().

51  {
52  std::string oid_str;
53  for(size_t i = 0; i != m_id.size(); ++i)
54  {
55  oid_str += std::to_string(m_id[i]);
56  if(i != m_id.size() - 1)
57  oid_str += ".";
58  }
59  return oid_str;
60  }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108

◆ clear()

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  m_id.clear();
45  }

◆ decode_from()

void Botan::OID::decode_from ( class BER_Decoder from)
overridevirtual

Decode whatever this object is from from

Parameters
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 161 of file asn1_oid.cpp.

References Botan::BER_Object::class_tag, clear(), Botan::BER_Decoder::get_next_object(), Botan::OBJECT_ID, Botan::BER_Object::type_tag, Botan::UNIVERSAL, and Botan::BER_Object::value.

162  {
163  BER_Object obj = decoder.get_next_object();
164  if(obj.type_tag != OBJECT_ID || obj.class_tag != UNIVERSAL)
165  throw BER_Bad_Tag("Error decoding OID, unknown tag",
166  obj.type_tag, obj.class_tag);
167  if(obj.value.size() < 2)
168  throw BER_Decoding_Error("OID encoding is too short");
169 
170 
171  clear();
172  m_id.push_back(obj.value[0] / 40);
173  m_id.push_back(obj.value[0] % 40);
174 
175  size_t i = 0;
176  while(i != obj.value.size() - 1)
177  {
178  uint32_t component = 0;
179  while(i != obj.value.size() - 1)
180  {
181  ++i;
182 
183  if(component >> (32-7))
184  throw Decoding_Error("OID component overflow");
185 
186  component = (component << 7) + (obj.value[i] & 0x7F);
187 
188  if(!(obj.value[i] & 0x80))
189  break;
190  }
191  m_id.push_back(component);
192  }
193  }
void clear()
Definition: asn1_oid.cpp:42

◆ empty()

bool Botan::OID::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 m_id.size() == 0; }

◆ encode_into()

void Botan::OID::encode_into ( class DER_Encoder to) const
overridevirtual

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_ASSERT, Botan::high_bit(), Botan::OBJECT_ID, and Botan::UNIVERSAL.

128  {
129  if(m_id.size() < 2)
130  throw Invalid_Argument("OID::encode_into: OID is invalid");
131 
132  std::vector<uint8_t> encoding;
133 
134  if(m_id[0] > 2 || m_id[1] >= 40)
135  throw Encoding_Error("Invalid OID prefix, cannot encode");
136 
137  encoding.push_back(static_cast<uint8_t>(40 * m_id[0] + m_id[1]));
138 
139  for(size_t i = 2; i != m_id.size(); ++i)
140  {
141  if(m_id[i] == 0)
142  encoding.push_back(0);
143  else
144  {
145  size_t blocks = high_bit(m_id[i]) + 6;
146  blocks = (blocks - (blocks % 7)) / 7;
147 
148  BOTAN_ASSERT(blocks > 0, "Math works");
149 
150  for(size_t j = 0; j != blocks - 1; ++j)
151  encoding.push_back(0x80 | ((m_id[i] >> 7*(blocks-j-1)) & 0x7F));
152  encoding.push_back(m_id[i] & 0x7F);
153  }
154  }
155  der.add_object(OBJECT_ID, UNIVERSAL, encoding);
156  }
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
size_t high_bit(T n)
Definition: bit_ops.h:37

◆ get_id()

const std::vector<uint32_t>& 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.

References BOTAN_PUBLIC_API, Botan::operator!=(), Botan::operator+(), Botan::operator+=(), Botan::operator<(), and Botan::operator==().

Referenced by Botan::operator<().

36 { return m_id; }

◆ operator+=()

OID & Botan::OID::operator+= ( uint32_t  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  m_id.push_back(component);
81  return (*this);
82  }

◆ operator==()

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(m_id.size() != oid.m_id.size())
68  return false;
69  for(size_t i = 0; i != m_id.size(); ++i)
70  if(m_id[i] != oid.m_id[i])
71  return false;
72  return true;
73  }

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