Botan 3.5.0
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
 
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::PSS_Params::decode_from(), 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 207 of file asn1_oid.cpp.

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

References 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::DER_encode(), empty(), and Botan::EC_PublicKey::set_parameter_encoding().

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

168 {
169 if(m_id.size() < 2) {
170 throw Invalid_Argument("OID::encode_into: OID is invalid");
171 }
172
173 auto append = [](std::vector<uint8_t>& encoding, uint32_t z) {
174 if(z <= 0x7F) {
175 encoding.push_back(static_cast<uint8_t>(z));
176 } else {
177 size_t z7 = (high_bit(z) + 7 - 1) / 7;
178
179 for(size_t j = 0; j != z7; ++j) {
180 uint8_t zp = static_cast<uint8_t>(z >> (7 * (z7 - j - 1)) & 0x7F);
181
182 if(j != z7 - 1) {
183 zp |= 0x80;
184 }
185
186 encoding.push_back(zp);
187 }
188 }
189 };
190
191 std::vector<uint8_t> encoding;
192
193 // We know 40 * root can't overflow because root is between 0 and 2
194 auto first = BOTAN_ASSERT_IS_SOME(checked_add(40 * m_id[0], m_id[1]));
195
196 append(encoding, first);
197
198 for(size_t i = 2; i != m_id.size(); ++i) {
199 append(encoding, m_id[i]);
200 }
201 der.add_object(ASN1_Type::ObjectId, ASN1_Class::Universal, encoding);
202}
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:58
#define BOTAN_ASSERT_IS_SOME(v)
Definition stl_util.h:389

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:69
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::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 309 of file asn1_obj.h.

309 {
310 return m_id;
311 }

Referenced by Botan::operator<().

◆ get_id()

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

Definition at line 314 of file asn1_obj.h.

314 {
315 return m_id;
316 }

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

◆ 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:56

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::PSS_Params::hash_function(), Botan::OCSP::CertID::is_id_for(), Botan::load_private_key(), Botan::load_public_key(), Botan::PSS_Params::mgf_function(), 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::OID_Map::add_oid(), Botan::OID_Map::add_oid2str(), Botan::EC_Group::from_OID(), Botan::OID_Map::oid2str(), Botan::operator<<(), to_formatted_string(), and Botan::X509_Certificate::to_string().


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