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

#include <asn1_obj.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_components () const
 
const std::vector< uint32_t > & get_id () const
 
bool has_value () const
 
 OID ()
 
 OID (const std::string &str)
 
 OID (std::initializer_list< uint32_t > init)
 
 OID (std::vector< uint32_t > &&init)
 
OIDoperator+= (uint32_t new_comp)
 
bool operator== (const OID &other) const
 
std::string to_formatted_string () const
 
std::string to_string () const
 

Static Public Member Functions

static OID from_string (const std::string &str)
 

Detailed Description

This class represents ASN.1 object identifiers.

Definition at line 193 of file asn1_obj.h.

Constructor & Destructor Documentation

Botan::OID::OID ( )
inlineexplicit

Create an uninitialied OID object

Definition at line 200 of file asn1_obj.h.

Referenced by from_string().

200 {}
Botan::OID::OID ( const std::string &  str)
explicit

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 82 of file asn1_oid.cpp.

83  {
84  if(!oid_str.empty())
85  {
86  m_id = parse_oid_str(oid_str);
87 
88  if(m_id.size() < 2 || m_id[0] > 2)
89  throw Invalid_OID(oid_str);
90  if((m_id[0] == 0 || m_id[0] == 1) && m_id[1] > 39)
91  throw Invalid_OID(oid_str);
92  }
93  }
Botan::OID::OID ( std::initializer_list< uint32_t >  init)
inlineexplicit

Initialize an OID from a sequence of integer values

Definition at line 211 of file asn1_obj.h.

211 : m_id(init) {}
int(* init)(CTX *)
Botan::OID::OID ( std::vector< uint32_t > &&  init)
inlineexplicit

Initialize an OID from a vector of integer values

Definition at line 216 of file asn1_obj.h.

216 : m_id(init) {}
int(* init)(CTX *)

Member Function Documentation

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

Get this OID as a string

Returns
string representing this OID

Definition at line 252 of file asn1_obj.h.

References Botan::to_string().

253  {
254  return this->to_string();
255  }
std::string to_string() const
Definition: asn1_oid.cpp:98
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
void Botan::OID::clear ( )
inline

Reset this instance to an empty OID.

Definition at line 281 of file asn1_obj.h.

281 { m_id.clear(); }
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 178 of file asn1_oid.cpp.

References Botan::BER_Object::bits(), Botan::BER_Decoder::get_next_object(), Botan::BER_Object::length(), Botan::OBJECT_ID, and Botan::BER_Object::tagging().

179  {
180  BER_Object obj = decoder.get_next_object();
181  if(obj.tagging() != OBJECT_ID)
182  throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
183 
184  const size_t length = obj.length();
185  const uint8_t* bits = obj.bits();
186 
187  if(length < 2 && !(length == 1 && bits[0] == 0))
188  {
189  throw BER_Decoding_Error("OID encoding is too short");
190  }
191 
192  m_id.clear();
193  m_id.push_back(bits[0] / 40);
194  m_id.push_back(bits[0] % 40);
195 
196  size_t i = 0;
197  while(i != length - 1)
198  {
199  uint32_t component = 0;
200  while(i != length - 1)
201  {
202  ++i;
203 
204  if(component >> (32-7))
205  throw Decoding_Error("OID component overflow");
206 
207  component = (component << 7) + (bits[i] & 0x7F);
208 
209  if(!(bits[i] & 0x80))
210  break;
211  }
212  m_id.push_back(component);
213  }
214  }
bool Botan::OID::empty ( ) const
inline

Find out whether this OID is empty

Returns
true is no OID value is set

Definition at line 232 of file asn1_obj.h.

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

232 { return m_id.empty(); }
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 144 of file asn1_oid.cpp.

References Botan::DER_Encoder::add_object(), BOTAN_ASSERT, Botan::high_bit(), Botan::OBJECT_ID, and Botan::UNIVERSAL.

145  {
146  if(m_id.size() < 2)
147  throw Invalid_Argument("OID::encode_into: OID is invalid");
148 
149  std::vector<uint8_t> encoding;
150 
151  if(m_id[0] > 2 || m_id[1] >= 40)
152  throw Encoding_Error("Invalid OID prefix, cannot encode");
153 
154  encoding.push_back(static_cast<uint8_t>(40 * m_id[0] + m_id[1]));
155 
156  for(size_t i = 2; i != m_id.size(); ++i)
157  {
158  if(m_id[i] == 0)
159  encoding.push_back(0);
160  else
161  {
162  size_t blocks = high_bit(m_id[i]) + 6;
163  blocks = (blocks - (blocks % 7)) / 7;
164 
165  BOTAN_ASSERT(blocks > 0, "Math works");
166 
167  for(size_t j = 0; j != blocks - 1; ++j)
168  encoding.push_back(0x80 | ((m_id[i] >> 7*(blocks-j-1)) & 0x7F));
169  encoding.push_back(m_id[i] & 0x7F);
170  }
171  }
172  der.add_object(OBJECT_ID, UNIVERSAL, encoding);
173  }
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
size_t high_bit(T n)
Definition: bit_ops.h:55
OID Botan::OID::from_string ( const std::string &  str)
static

Construct an OID from a string.

Parameters
stra string in the form "a.b.c" etc., where a,b,c are numbers or any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier")

Definition at line 62 of file asn1_oid.cpp.

References has_value(), OID(), and Botan::OIDS::str2oid_or_empty().

Referenced by Botan::X509_DN::add_attribute(), Botan::X509_Cert_Options::add_ex_constraint(), Botan::X509_Certificate::allowed_extended_usage(), Botan::EMSA1::config_for_x509(), Botan::EMSA_PKCS1v15::config_for_x509(), Botan::PKCS10_Request::constraints(), Botan::EC_Group::EC_Group(), Botan::PKCS10_Request::ex_constraints(), Botan::X509_DN::get_attribute(), Botan::X509_DN::get_first_attribute(), Botan::X509_Certificate::has_ex_constraint(), Botan::PKCS10_Request::is_CA(), Botan::X509_Certificate::is_critical(), Botan::PKCS10_Request::path_limit(), and Botan::TLS::Callbacks::tls_ecdh_agree().

63  {
64  if(str.empty())
65  throw Invalid_Argument("OID::from_string argument must be non-empty");
66 
67  const OID o = OIDS::str2oid_or_empty(str);
68  if(o.has_value())
69  return o;
70 
71  std::vector<uint32_t> raw = parse_oid_str(str);
72 
73  if(raw.size() > 0)
74  return OID(std::move(raw));
75 
76  throw Lookup_Error("No OID associated with name " + str);
77  }
BOTAN_UNSTABLE_API OID str2oid_or_empty(const std::string &name)
Definition: oids.cpp:116
const std::vector<uint32_t>& Botan::OID::get_components ( ) const
inline

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

Returns
vector representing this OID

Definition at line 244 of file asn1_obj.h.

Referenced by Botan::operator+(), Botan::operator<(), and Botan::parse_asn1_oid().

244 { return m_id; }
const std::vector<uint32_t>& Botan::OID::get_id ( ) const
inline

Definition at line 246 of file asn1_obj.h.

246 { return get_components(); }
const std::vector< uint32_t > & get_components() const
Definition: asn1_obj.h:244
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 238 of file asn1_obj.h.

Referenced by Botan::X509::create_self_signed_cert(), Botan::EC_Group::EC_Group(), from_string(), and Botan::X509_DN::has_field().

238 { return (m_id.empty() == false); }
OID& Botan::OID::operator+= ( uint32_t  new_comp)
inline

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 288 of file asn1_obj.h.

289  {
290  m_id.push_back(new_comp);
291  return (*this);
292  }
bool Botan::OID::operator== ( const OID other) const
inline

Compare two OIDs.

Returns
true if they are equal, false otherwise

Definition at line 273 of file asn1_obj.h.

274  {
275  return m_id == other.m_id;
276  }
std::string Botan::OID::to_formatted_string ( ) const

If there is a known name associated with this OID, return that. Otherwise return the result of to_string

Definition at line 111 of file asn1_oid.cpp.

References Botan::OIDS::oid2str_or_empty(), and to_string().

Referenced by Botan::X509_Object::hash_used_for_signature(), Botan::OCSP::CertID::is_id_for(), Botan::load_private_key(), Botan::load_public_key(), Botan::X942_PRF::name(), Botan::X509_Certificate::to_string(), Botan::X509_Object::verify_signature(), and Botan::OCSP::Response::verify_signature().

112  {
113  const std::string s = OIDS::oid2str_or_empty(*this);
114  if(!s.empty())
115  return s;
116  return this->to_string();
117  }
BOTAN_UNSTABLE_API std::string oid2str_or_empty(const OID &oid)
Definition: oids.cpp:111
std::string to_string() const
Definition: asn1_oid.cpp:98
std::string Botan::OID::to_string ( ) const

Get this OID as a dotted-decimal string

Returns
string representing this OID

Definition at line 98 of file asn1_oid.cpp.

Referenced by Botan::EC_Group::EC_Group(), Botan::X509_Object::hash_used_for_signature(), Botan::OIDS::oid2str_or_throw(), to_formatted_string(), and Botan::X509_Certificate::to_string().

99  {
100  std::ostringstream oss;
101  oss.imbue(std::locale("C"));
102  for(size_t i = 0; i != m_id.size(); ++i)
103  {
104  oss << m_id[i];
105  if(i != m_id.size() - 1)
106  oss << ".";
107  }
108  return oss.str();
109  }

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