Botan 2.17.3
Crypto and TLS for C&
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

◆ OID() [1/4]

Botan::OID::OID ( )
inlineexplicit

Create an uninitialied OID object

Definition at line 200 of file asn1_obj.h.

200{}

Referenced by from_string().

◆ OID() [2/4]

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 }

◆ OID() [3/4]

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 *)

◆ OID() [4/4]

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) {}

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

253 {
254 return this->to_string();
255 }
std::string to_string() const
Definition: asn1_oid.cpp:98

References Botan::to_string().

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

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

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().

◆ clear()

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(); }

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

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

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

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

232{ return m_id.empty(); }

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().

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

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

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

◆ from_string()

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.

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

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().

◆ get_components()

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.

244{ return m_id; }

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

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

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

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

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

◆ operator+=()

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 }

◆ operator==()

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 }

◆ to_formatted_string()

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

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

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().

◆ to_string()

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.

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

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().


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