Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | List of all members
Botan::OID Class Referencefinal

`#include <asn1_oid.h>`

Inheritance diagram for Botan::OID:

## 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)
OID & | operator+= (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 20 of file asn1_oid.h.

## Constructor & Destructor Documentation

Botan::OID::OID | ( |  | ) |
| inline explicit

Create an uninitialied OID object

Definition at line 27 of file asn1_oid.h.

Referenced by from_string().

27 {}

Botan::OID::OID | ( | const std::string & | str | ) |
| explicit

Construct an OID from a string.

Parameters

  str | a 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 }

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

Initialize an OID from a sequence of integer values

Definition at line 38 of file asn1_oid.h.

38 : m_id(init) {}
init
int(* init)(CTX *)
Definition: commoncrypto_hash.cpp:26

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

Initialize an OID from a vector of integer values

Definition at line 43 of file asn1_oid.h.

43 : m_id(init) {}
init
int(* init)(CTX *)
Definition: commoncrypto_hash.cpp:26

## Member Function Documentation

std::string Botan::OID::as_string | ( |  | ) | const
| inline

Get this OID as a string

Returns
  string representing this OID

Definition at line 79 of file asn1_oid.h.

References Botan::to_string().

80 {
81 return this->to_string();
82 }
Botan::OID::to_string
std::string to_string() const
Definition: asn1_oid.cpp:98

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.

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

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

void Botan::OID::clear | ( |  | ) |
| inline

Reset this instance to an empty OID.

Definition at line 108 of file asn1_oid.h.

108 { m_id.clear(); }

void Botan::OID::decode_from | ( | class BER_Decoder & | from | ) |
| override virtual

Decode whatever this object is from from

Parameters

  from | the BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 177 of file asn1_oid.cpp.

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

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 }
Botan::OBJECT_ID
Definition: asn1_obj.h:38

bool Botan::OID::empty | ( |  | ) | const
| inline

Find out whether this OID is empty

Returns
  true is no OID value is set

Definition at line 59 of file asn1_oid.h.

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

59 { return m_id.empty(); }

void Botan::OID::encode_into | ( | class DER_Encoder & | to | ) | const
| override virtual

Encode whatever this object is into to

Parameters

  to | the DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 143 of file asn1_oid.cpp.

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

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 }
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
Botan::UNIVERSAL
Definition: asn1_obj.h:24
Botan::high_bit
size_t high_bit(T n)
Definition: bit_ops.h:55
Botan::OBJECT_ID
Definition: asn1_obj.h:38

OID Botan::OID::from_string | ( | const std::string & | str | ) |
| static

Construct an OID from a string.

Parameters

  str | a 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.

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

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::OID::OID
OID()
Definition: asn1_oid.h:27
Botan::OIDS::str2oid_or_empty
BOTAN_UNSTABLE_API OID str2oid_or_empty(const std::string &name)
Definition: oids.cpp:116

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 71 of file asn1_oid.h.

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

71 { return m_id; }

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

Definition at line 73 of file asn1_oid.h.

73 { return get_components(); }
Botan::OID::get_components
const std::vector< uint32_t > & get_components() const
Definition: asn1_oid.h:71

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 65 of file asn1_oid.h.

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

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

OID& Botan::OID::operator+= | ( | uint32_t | new_comp | ) |
| inline

Add a component to this OID.

Parameters

  new_comp | the new component to add to the end of this OID

Returns
  reference to *this

Definition at line 115 of file asn1_oid.h.

116 {
117 m_id.push_back(new_comp);
118 return (*this);
119 }

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

Compare two OIDs.

Returns
  true if they are equal, false otherwise

Definition at line 100 of file asn1_oid.h.

101 {
102 return m_id == other.m_id;
103 }

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.

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

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::OIDS::oid2str_or_empty
BOTAN_UNSTABLE_API std::string oid2str_or_empty(const OID &oid)
Definition: oids.cpp:111
Botan::OID::to_string
std::string to_string() const
Definition: asn1_oid.cpp:98

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.

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

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 }

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

- src/lib/asn1/asn1_oid.h
- src/lib/asn1/asn1_oid.cpp