Botan  2.7.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
 
std::vector< uint8_t > BER_encode () 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
 
bool has_value () const
 
 OID (const std::string &str="")
 
 OID (std::initializer_list< uint32_t > init)
 
OIDoperator+= (uint32_t new_comp)
 
bool operator== (const OID &) const
 
std::string to_string () const
 

Detailed Description

This class represents ASN.1 object identifiers.

Definition at line 20 of file asn1_oid.h.

Constructor & Destructor Documentation

◆ OID() [1/2]

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:195

◆ OID() [2/2]

Botan::OID::OID ( std::initializer_list< uint32_t >  init)
inlineexplicit

Definition at line 80 of file asn1_oid.h.

80 : m_id(init) {}

Member Function Documentation

◆ as_string()

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

Get this OID as a string

Returns
string representing this OID

Definition at line 48 of file asn1_oid.h.

References Botan::to_string().

Referenced by Botan::EC_Group::EC_Group(), Botan::X509_Object::hash_used_for_signature(), Botan::load_private_key(), Botan::load_public_key(), and Botan::X509_Certificate::to_string().

48 { return this->to_string(); }
std::string to_string() const
Definition: asn1_oid.cpp:50

◆ BER_encode()

std::vector< uint8_t > Botan::ASN1_Object::BER_encode ( ) const
inherited

Return the encoding of this object. This is a convenience method when just one object needs to be serialized. Use DER_Encoder for complicated encodings.

Definition at line 16 of file asn1_obj.cpp.

References Botan::ASN1_Object::encode_into().

Referenced by Botan::PSSR::config_for_x509(), Botan::Certificate_Store_In_SQL::find_all_certs(), Botan::Certificate_Store_In_SQL::find_cert(), Botan::X509_Certificate::fingerprint(), Botan::Certificate_Store_In_SQL::insert_cert(), Botan::X509_Object::PEM_encode(), and Botan::Certificate_Store_In_SQL::revoke_cert().

17  {
18  std::vector<uint8_t> output;
19  DER_Encoder der(output);
20  this->encode_into(der);
21  return output;
22  }
virtual void encode_into(DER_Encoder &to) const =0

◆ 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::bits(), clear(), Botan::BER_Decoder::get_next_object(), Botan::BER_Object::length(), Botan::OBJECT_ID, and Botan::BER_Object::tagging().

162  {
163  BER_Object obj = decoder.get_next_object();
164  if(obj.tagging() != OBJECT_ID)
165  throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
166 
167  const size_t length = obj.length();
168  const uint8_t* bits = obj.bits();
169 
170  if(length < 2 && !(length == 1 && bits[0] == 0))
171  {
172  throw BER_Decoding_Error("OID encoding is too short");
173  }
174 
175  clear();
176  m_id.push_back(bits[0] / 40);
177  m_id.push_back(bits[0] % 40);
178 
179  size_t i = 0;
180  while(i != length - 1)
181  {
182  uint32_t component = 0;
183  while(i != length - 1)
184  {
185  ++i;
186 
187  if(component >> (32-7))
188  throw Decoding_Error("OID component overflow");
189 
190  component = (component << 7) + (bits[i] & 0x7F);
191 
192  if(!(bits[i] & 0x80))
193  break;
194  }
195  m_id.push_back(component);
196  }
197  }
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.

Referenced by Botan::EC_Group::DER_encode(), Botan::EC_Group::EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::EC_PublicKey::EC_PublicKey(), and Botan::EC_PublicKey::set_parameter_encoding().

30 { return m_id.empty(); }

◆ 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:43
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 42 of file asn1_oid.h.

Referenced by Botan::operator<().

42 { return m_id; }

◆ has_value()

bool Botan::OID::has_value ( ) const
inline

Find out whether this OID has a value

Returns
true is this OID has a value

Definition at line 36 of file asn1_oid.h.

36 { return (m_id.empty() == false); }

◆ 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  }

◆ to_string()

std::string Botan::OID::to_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().

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:210

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