Botan 3.4.0
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::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 213 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 numbers

Definition at line 96 of file asn1_oid.cpp.

96 {
97 if(!oid_str.empty()) {
98 m_id = parse_oid_str(oid_str);
99 if(m_id.size() < 2 || m_id[0] > 2 || (m_id[0] < 2 && m_id[1] > 39)) {
100 throw Decoding_Error(fmt("Invalid OID '{}'", oid_str));
101 }
102 }
103}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt().

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

229 : m_id(init) {
230 BOTAN_ARG_CHECK(m_id.size() > 2 && m_id[0] <= 2 && (m_id[0] != 2 || m_id[1] <= 39), "Invalid OID");
231 }
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
int(* init)(CTX *)

References BOTAN_ARG_CHECK.

◆ OID() [4/4]

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

Initialize an OID from a vector of integer values

Definition at line 236 of file asn1_obj.h.

236 : m_id(init) {
237 BOTAN_ARG_CHECK(m_id.size() > 2 && m_id[0] <= 2 && (m_id[0] != 2 || m_id[1] <= 39), "Invalid OID");
238 }

References BOTAN_ARG_CHECK.

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

191 {
192 BER_Object obj = decoder.get_next_object();
193 if(obj.tagging() != (ASN1_Class::Universal | ASN1_Type::ObjectId)) {
194 throw BER_Bad_Tag("Error decoding OID, unknown tag", obj.tagging());
195 }
196
197 const size_t length = obj.length();
198 const uint8_t* bits = obj.bits();
199
200 if(length < 2 && !(length == 1 && bits[0] == 0)) {
201 throw BER_Decoding_Error("OID encoding is too short");
202 }
203
204 m_id.clear();
205 m_id.push_back(bits[0] / 40);
206 m_id.push_back(bits[0] % 40);
207
208 size_t i = 0;
209 while(i != length - 1) {
210 uint32_t component = 0;
211 while(i != length - 1) {
212 ++i;
213
214 if(component >> (32 - 7)) {
215 throw Decoding_Error("OID component overflow");
216 }
217
218 component = (component << 7) + (bits[i] & 0x7F);
219
220 if(!(bits[i] & 0x80)) {
221 break;
222 }
223 }
224 m_id.push_back(component);
225 }
226}

References Botan::BER_Object::bits(), 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 265 of file asn1_obj.h.

265{ return m_id.empty(); }

Referenced by Botan::EC_Group::DER_encode(), 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 157 of file asn1_oid.cpp.

157 {
158 if(m_id.size() < 2) {
159 throw Invalid_Argument("OID::encode_into: OID is invalid");
160 }
161
162 std::vector<uint8_t> encoding;
163
164 if(m_id[0] > 2 || m_id[1] >= 40) {
165 throw Encoding_Error("Invalid OID prefix, cannot encode");
166 }
167
168 encoding.push_back(static_cast<uint8_t>(40 * m_id[0] + m_id[1]));
169
170 for(size_t i = 2; i != m_id.size(); ++i) {
171 if(m_id[i] == 0) {
172 encoding.push_back(0);
173 } else {
174 size_t blocks = high_bit(m_id[i]) + 6;
175 blocks = (blocks - (blocks % 7)) / 7;
176
177 BOTAN_ASSERT(blocks > 0, "Math works");
178
179 for(size_t j = 0; j != blocks - 1; ++j) {
180 encoding.push_back(0x80 | ((m_id[i] >> 7 * (blocks - j - 1)) & 0x7F));
181 }
182 encoding.push_back(m_id[i] & 0x7F);
183 }
184 }
185 der.add_object(ASN1_Type::ObjectId, ASN1_Class::Universal, encoding);
186}
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
constexpr size_t high_bit(T n)
Definition bit_ops.h:58

References Botan::DER_Encoder::add_object(), BOTAN_ASSERT, 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 60 of file asn1_oid.cpp.

60 {
61 if(name.empty()) {
62 throw Invalid_Argument("OID::from_name argument must be non-empty");
63 }
64
66 if(o.has_value()) {
67 return std::optional(o);
68 }
69
70 return std::nullopt;
71}
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().

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

74 {
75 if(str.empty()) {
76 throw Invalid_Argument("OID::from_string argument must be non-empty");
77 }
78
80 if(o.has_value()) {
81 return o;
82 }
83
84 std::vector<uint32_t> raw = parse_oid_str(str);
85
86 if(!raw.empty()) {
87 return OID(std::move(raw));
88 }
89
90 throw Lookup_Error(fmt("No OID associated with name '{}'", str));
91}

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::DilithiumMode::object_identifier(), Botan::FrodoKEMMode::object_identifier(), Botan::KyberMode::object_identifier(), Botan::Asymmetric_Key::object_identifier(), and Botan::PKCS10_Request::path_limit().

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

277{ return m_id; }

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

◆ get_id()

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

Definition at line 279 of file asn1_obj.h.

279{ return get_components(); }
const std::vector< uint32_t > & get_components() const
Definition asn1_obj.h:277

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

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

Referenced by Botan::X509::create_self_signed_cert(), 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 122 of file asn1_oid.cpp.

122 {
123 return OID_Map::global_registry().oid2str(*this);
124}
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 309 of file asn1_obj.h.

309{ 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 55 of file asn1_oid.cpp.

55 {
57}
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 126 of file asn1_oid.cpp.

126 {
127 return !human_name_or_empty().empty();
128}
std::string human_name_or_empty() const
Definition asn1_oid.cpp:122

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

114 {
115 std::string s = this->human_name_or_empty();
116 if(!s.empty()) {
117 return s;
118 }
119 return this->to_string();
120}
std::string to_string() const
Definition asn1_oid.cpp:108

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

108 {
109 std::ostringstream out;
110 out << (*this);
111 return out.str();
112}

Referenced by Botan::OID_Map::add_oid(), Botan::OID_Map::add_oid2str(), Botan::EC_Group::EC_Group(), Botan::OID_Map::oid2str(), to_formatted_string(), and Botan::X509_Certificate::to_string().


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