Botan 3.7.1
Crypto and TLS for C&
Botan::OID Class Referencefinal

#include <asn1_obj.h>

Inheritance diagram for Botan::OID:
Botan::ASN1_Object

Public Member Functions

std::vector< uint8_t > BER_encode () const
 
void decode_from (BER_Decoder &) override
 
bool empty () const
 
void encode_into (DER_Encoder &) const override
 
const std::vector< uint32_t > & get_components () const
 
const std::vector< uint32_t > & get_id () const
 
bool has_value () const
 
size_t hash_code () const
 
std::string human_name_or_empty () const
 
 OID ()=default
 
 OID (std::initializer_list< uint32_t > init)
 
 OID (std::string_view str)
 
 OID (std::vector< uint32_t > &&init)
 
bool operator== (const OID &other) const
 
bool registered_oid () const
 
std::string to_formatted_string () const
 
std::string to_string () const
 

Static Public Member Functions

static std::optional< OIDfrom_name (std::string_view name)
 
static OID from_string (std::string_view str)
 
static void register_oid (const OID &oid, std::string_view name)
 

Detailed Description

This class represents ASN.1 object identifiers.

Definition at line 216 of file asn1_obj.h.

Constructor & Destructor Documentation

◆ OID() [1/4]

Botan::OID::OID ( )
explicitdefault

Create an uninitialied OID object

Referenced by from_string().

◆ OID() [2/4]

Botan::OID::OID ( std::string_view str)
explicit

Construct an OID from a string.

Parameters
stra string in the form "a.b.c" etc., where a,b,c are integers

Note: it is currently required that each integer fit into 32 bits

Definition at line 115 of file asn1_oid.cpp.

115 {
116 if(!oid_str.empty()) {
117 m_id = parse_oid_str(oid_str);
118 oid_valid_check(m_id);
119 }
120}

◆ OID() [3/4]

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

Initialize an OID from a sequence of integer values

Definition at line 104 of file asn1_oid.cpp.

104 : m_id(init) {
105 oid_valid_check(m_id);
106}
int(* init)(CTX *)

◆ OID() [4/4]

Botan::OID::OID ( std::vector< uint32_t > && init)
explicit

Initialize an OID from a vector of integer values

Definition at line 108 of file asn1_oid.cpp.

108 : m_id(std::move(init)) {
109 oid_valid_check(m_id);
110}

Member Function Documentation

◆ 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 19 of file asn1_obj.cpp.

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

References Botan::ASN1_Object::encode_into().

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

◆ decode_from()

void Botan::OID::decode_from ( 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 216 of file asn1_oid.cpp.

216 {
217 BER_Object obj = decoder.get_next_object();
218 if(obj.tagging() != (ASN1_Class::Universal | ASN1_Type::ObjectId)) {
219 throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
220 }
221
222 if(obj.length() == 0) {
223 throw BER_Decoding_Error("OID encoding is too short");
224 }
225
226 auto consume = [](BufferSlicer& data) -> uint32_t {
227 BOTAN_ASSERT_NOMSG(!data.empty());
228 uint32_t b = data.take_byte();
229
230 if(b > 0x7F) {
231 b &= 0x7F;
232
233 // Even BER requires that the OID have minimal length, ie that
234 // the first byte of a multibyte encoding cannot be zero
235 // See X.690 section 8.19.2
236 if(b == 0) {
237 throw Decoding_Error("Leading zero byte in multibyte OID encoding");
238 }
239
240 while(true) {
241 if(data.empty()) {
242 throw Decoding_Error("Truncated OID value");
243 }
244
245 const uint8_t next = data.take_byte();
246 const bool more = (next & 0x80);
247 const uint8_t value = next & 0x7F;
248
249 if((b >> (32 - 7)) != 0) {
250 throw Decoding_Error("OID component overflow");
251 }
252
253 b = (b << 7) | value;
254
255 if(!more) {
256 break;
257 }
258 }
259 }
260
261 return b;
262 };
263
264 BufferSlicer data(obj.data());
265 std::vector<uint32_t> parts;
266 while(!data.empty()) {
267 const uint32_t comp = consume(data);
268
269 if(parts.empty()) {
270 // divide into root and second arc
271
272 const uint32_t root_arc = [](uint32_t b0) -> uint32_t {
273 if(b0 < 40) {
274 return 0;
275 } else if(b0 < 80) {
276 return 1;
277 } else {
278 return 2;
279 }
280 }(comp);
281
282 parts.push_back(root_arc);
283 BOTAN_ASSERT_NOMSG(comp >= 40 * root_arc);
284 parts.push_back(comp - 40 * root_arc);
285 } else {
286 parts.push_back(comp);
287 }
288 }
289
290 m_id = parts;
291}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
const SIMD_8x32 & b

References Botan::b, BOTAN_ASSERT_NOMSG, Botan::BER_Object::data(), Botan::BufferSlicer::empty(), Botan::BER_Decoder::get_next_object(), Botan::BER_Object::length(), Botan::ObjectId, Botan::BER_Object::tagging(), and Botan::Universal.

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

266{ return m_id.empty(); }

References empty().

Referenced by Botan::EC_Group_Data::EC_Group_Data(), empty(), and Botan::EC_Group_Data::set_oid().

◆ encode_into()

void Botan::OID::encode_into ( 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 177 of file asn1_oid.cpp.

177 {
178 if(m_id.size() < 2) {
179 throw Invalid_Argument("OID::encode_into: OID is invalid");
180 }
181
182 auto append = [](std::vector<uint8_t>& encoding, uint32_t z) {
183 if(z <= 0x7F) {
184 encoding.push_back(static_cast<uint8_t>(z));
185 } else {
186 size_t z7 = (high_bit(z) + 7 - 1) / 7;
187
188 for(size_t j = 0; j != z7; ++j) {
189 uint8_t zp = static_cast<uint8_t>(z >> (7 * (z7 - j - 1)) & 0x7F);
190
191 if(j != z7 - 1) {
192 zp |= 0x80;
193 }
194
195 encoding.push_back(zp);
196 }
197 }
198 };
199
200 std::vector<uint8_t> encoding;
201
202 // We know 40 * root can't overflow because root is between 0 and 2
203 auto first = BOTAN_ASSERT_IS_SOME(checked_add(40 * m_id[0], m_id[1]));
204
205 append(encoding, first);
206
207 for(size_t i = 2; i != m_id.size(); ++i) {
208 append(encoding, m_id[i]);
209 }
210 der.add_object(ASN1_Type::ObjectId, ASN1_Class::Universal, encoding);
211}
constexpr std::optional< T > checked_add(T a, T b)
Definition int_utils.h:19
constexpr size_t high_bit(T n)
Definition bit_ops.h:61
#define BOTAN_ASSERT_IS_SOME(v)
Definition stl_util.h:398

References Botan::DER_Encoder::add_object(), BOTAN_ASSERT_IS_SOME, Botan::checked_add(), Botan::high_bit(), Botan::ObjectId, and Botan::Universal.

◆ from_name()

std::optional< OID > Botan::OID::from_name ( std::string_view name)
static

Construct an OID from a name

Parameters
nameany known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier")

Definition at line 72 of file asn1_oid.cpp.

72 {
73 if(name.empty()) {
74 throw Invalid_Argument("OID::from_name argument must be non-empty");
75 }
76
78 if(o.has_value()) {
79 return std::optional(o);
80 }
81
82 return std::nullopt;
83}
static OID_Map & global_registry()
Definition oid_map.cpp:16
OID str2oid(std::string_view str)
Definition oid_map.cpp:64
OID()=default
std::string name

References Botan::OID_Map::global_registry(), has_value(), name, and Botan::OID_Map::str2oid().

Referenced by Botan::EC_Group::from_name().

◆ from_string()

OID Botan::OID::from_string ( std::string_view 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 86 of file asn1_oid.cpp.

86 {
87 if(str.empty()) {
88 throw Invalid_Argument("OID::from_string argument must be non-empty");
89 }
90
92 if(o.has_value()) {
93 return o;
94 }
95
96 // Try to parse as a dotted decimal
97 try {
98 return OID(str);
99 } catch(...) {}
100
101 throw Lookup_Error(fmt("No OID associated with name '{}'", str));
102}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt(), Botan::OID_Map::global_registry(), has_value(), OID(), and Botan::OID_Map::str2oid().

Referenced by Botan::X509_DN::add_attribute(), Botan::X509_Cert_Options::add_ex_constraint(), Botan::TLS::Signature_Scheme::algorithm_identifier(), Botan::XMSS_Signature_Operation::algorithm_identifier(), Botan::X509_Certificate::allowed_extended_usage(), 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::X509_DN::has_field(), Botan::PKCS10_Request::is_CA(), Botan::X509_Certificate::is_critical(), Botan::Asymmetric_Key::object_identifier(), Botan::Classic_McEliece_Parameters::object_identifier(), Botan::DilithiumMode::object_identifier(), Botan::FrodoKEMMode::object_identifier(), Botan::HSS_LMS_PublicKeyInternal::object_identifier(), Botan::KyberMode::object_identifier(), Botan::PKCS10_Request::path_limit(), and Botan::HSS_LMS_PrivateKey::pkcs8_algorithm_identifier().

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

317 {
318 return m_id;
319 }

Referenced by Botan::operator<().

◆ get_id()

const std::vector< uint32_t > & Botan::OID::get_id ( ) const
inline

Definition at line 322 of file asn1_obj.h.

322 {
323 return m_id;
324 }

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

272{ return !empty(); }
bool empty() const
Definition asn1_obj.h:266

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

◆ hash_code()

size_t Botan::OID::hash_code ( ) const

Return a hash code for this OID

This value is only meant as a std::unsorted_map hash and can change value from release to release.

Definition at line 155 of file asn1_oid.cpp.

155 {
156 constexpr uint64_t mod = 0xffffffffffffffc5;
157 uint64_t hash = 0;
158 for(auto id : m_id) {
159 hash = (hash * 257 + id) % mod;
160 }
161 return static_cast<size_t>(hash);
162}

◆ human_name_or_empty()

std::string Botan::OID::human_name_or_empty ( ) const

If there is a known name associated with this OID, return that. Otherwise return the empty string.

Definition at line 147 of file asn1_oid.cpp.

147 {
148 return OID_Map::global_registry().oid2str(*this);
149}
std::string oid2str(const OID &oid)
Definition oid_map.cpp:53

References Botan::OID_Map::global_registry(), and Botan::OID_Map::oid2str().

Referenced by Botan::OIDS::oid2str_or_throw(), Botan::pbes2_decrypt(), registered_oid(), and to_formatted_string().

◆ operator==()

bool Botan::OID::operator== ( const OID & other) const
inline

Compare two OIDs.

Returns
true if they are equal, false otherwise

Definition at line 302 of file asn1_obj.h.

302{ return m_id == other.m_id; }

◆ register_oid()

void Botan::OID::register_oid ( const OID & oid,
std::string_view name )
static

Register a new OID in the internal table

Definition at line 67 of file asn1_oid.cpp.

67 {
69}
void add_oid(const OID &oid, std::string_view str)
Definition oid_map.cpp:21

References Botan::OID_Map::add_oid(), Botan::OID_Map::global_registry(), and name.

◆ registered_oid()

bool Botan::OID::registered_oid ( ) const

Return true if the OID in *this is registered in the internal set of constants as a known OID.

Definition at line 151 of file asn1_oid.cpp.

151 {
152 return !human_name_or_empty().empty();
153}
std::string human_name_or_empty() const
Definition asn1_oid.cpp:147

References human_name_or_empty().

Referenced by Botan::PKIX::check_chain().

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

139 {
140 std::string s = this->human_name_or_empty();
141 if(!s.empty()) {
142 return s;
143 }
144 return this->to_string();
145}
std::string to_string() const
Definition asn1_oid.cpp:125

References human_name_or_empty(), and to_string().

Referenced by Botan::Dilithium_PublicKey::algo_name(), Botan::Sphincs_Parameters::create(), Botan::Classic_McEliece_Parameter_Set::from_oid(), Botan::TPM2::get_tpm2_curve_id(), Botan::OCSP::CertID::is_id_for(), Botan::load_private_key(), Botan::load_public_key(), Botan::X942_PRF::name(), Botan::X509_Certificate::to_string(), and Botan::PK_Ops::Verification_with_Hash::Verification_with_Hash().

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

125 {
126 std::ostringstream out;
127
128 for(size_t i = 0; i != m_id.size(); ++i) {
129 // avoid locale issues with integer formatting
130 out << std::to_string(m_id[i]);
131 if(i != m_id.size() - 1) {
132 out << ".";
133 }
134 }
135
136 return out.str();
137}

Referenced by Botan::EC_Group::from_OID(), Botan::operator<<(), and to_formatted_string().


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