Botan 1.10.17
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

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

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
Invalid_OID(const std::string &oid)
Definition exceptn.h:158

References Botan::Invalid_OID::Invalid_OID(), and Botan::parse_asn1_oid().

Referenced by Botan::EC_Group::DER_encode(), Botan::EC_Group::EC_Group(), Botan::OIDS::lookup(), operator+=(), and operator==().

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.

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

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

◆ clear()

void Botan::OID::clear ( )

Reset this instance to an empty OID.

Definition at line 42 of file asn1_oid.cpp.

43 {
44 id.clear();
45 }

Referenced by decode_from().

◆ decode_from()

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.

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
@ UNIVERSAL
Definition asn1_int.h:20
@ OBJECT_ID
Definition asn1_int.h:33
unsigned int u32bit
Definition types.h:32
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition asn1_int.cpp:22
BER_Decoding_Error(const std::string &)
Definition asn1_int.cpp:19

References Botan::BER_Bad_Tag::BER_Bad_Tag(), Botan::BER_Decoding_Error::BER_Decoding_Error(), 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.

◆ encode_into()

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.

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

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

◆ get_id()

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.

36{ return id; }

Referenced by Botan::operator<().

◆ is_empty()

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

◆ operator+=()

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 }

References OID().

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

References OID().


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