Botan 3.0.0
Crypto and TLS for C&
Classes | Public Member Functions | List of all members
Botan::Extensions Class Referencefinal

#include <pkix_types.h>

Inheritance diagram for Botan::Extensions:
Botan::ASN1_Object

Public Member Functions

void add (std::unique_ptr< Certificate_Extension > extn, bool critical=false)
 
bool add_new (std::unique_ptr< Certificate_Extension > extn, bool critical=false)
 
std::vector< uint8_t > BER_encode () const
 
bool critical_extension_set (const OID &oid) const
 
void decode_from (BER_Decoder &) override
 
void encode_into (DER_Encoder &) const override
 
bool extension_set (const OID &oid) const
 
 Extensions ()
 
std::vector< std::pair< std::unique_ptr< Certificate_Extension >, bool > > extensions () const
 
 Extensions (const Extensions &)=default
 
 Extensions (Extensions &&)=default
 
std::map< OID, std::pair< std::vector< uint8_t >, bool > > extensions_raw () const
 
std::unique_ptr< Certificate_Extensionget (const OID &oid) const
 
std::vector< uint8_t > get_extension_bits (const OID &oid) const
 
const Certificate_Extensionget_extension_object (const OID &oid) const
 
template<typename T >
const Tget_extension_object_as (const OID &oid=T::static_oid()) const
 
const std::vector< OID > & get_extension_oids () const
 
template<typename T >
std::unique_ptr< Tget_raw (const OID &oid) const
 
Extensionsoperator= (const Extensions &)=default
 
Extensionsoperator= (Extensions &&)=default
 
bool remove (const OID &oid)
 
void replace (std::unique_ptr< Certificate_Extension > extn, bool critical=false)
 

Detailed Description

X.509 Certificate Extension List

Definition at line 399 of file pkix_types.h.

Constructor & Destructor Documentation

◆ Extensions() [1/3]

Botan::Extensions::Extensions ( )
inline

Definition at line 545 of file pkix_types.h.

545{}

◆ Extensions() [2/3]

Botan::Extensions::Extensions ( const Extensions )
default

◆ Extensions() [3/3]

Botan::Extensions::Extensions ( Extensions &&  )
default

Member Function Documentation

◆ add()

void Botan::Extensions::add ( std::unique_ptr< Certificate_Extension extn,
bool  critical = false 
)

Adds a new extension to the list.

Parameters
extnpointer to the certificate extension (Extensions takes ownership)
criticalwhether this extension should be marked as critical
Exceptions
Invalid_Argumentif the extension is already present in the list

Definition at line 118 of file x509_ext.cpp.

119 {
120 // sanity check: we don't want to have the same extension more than once
121 if(m_extension_info.contains(extn->oid_of()))
122 {
123 const std::string name = extn->oid_name();
124 throw Invalid_Argument("Extension " + name + " already present in Extensions::add");
125 }
126
127 const OID oid = extn->oid_of();
128 Extensions_Info info(critical, std::move(extn));
129 m_extension_oids.push_back(oid);
130 m_extension_info.emplace(oid, info);
131 }
std::string name

References name.

◆ add_new()

bool Botan::Extensions::add_new ( std::unique_ptr< Certificate_Extension extn,
bool  critical = false 
)

Adds a new extension to the list unless it already exists. If the extension already exists within the Extensions object, the extn pointer will be deleted.

Parameters
extnpointer to the certificate extension (Extensions takes ownership)
criticalwhether this extension should be marked as critical
Returns
true if the object was added false if the extension was already used

Definition at line 133 of file x509_ext.cpp.

134 {
135 if(m_extension_info.contains(extn->oid_of()))
136 {
137 return false; // already exists
138 }
139
140 const OID oid = extn->oid_of();
141 Extensions_Info info(critical, std::move(extn));
142 m_extension_oids.push_back(oid);
143 m_extension_info.emplace(oid, info);
144 return true;
145 }

Referenced by Botan::X509::create_cert_req(), and Botan::X509::create_self_signed_cert().

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

18 {
19 std::vector<uint8_t> output;
20 DER_Encoder der(output);
21 this->encode_into(der);
22 return output;
23 }
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().

◆ critical_extension_set()

bool Botan::Extensions::critical_extension_set ( const OID oid) const

Return true if an extesion was set and marked critical

Definition at line 175 of file x509_ext.cpp.

176 {
177 auto i = m_extension_info.find(oid);
178 if(i != m_extension_info.end())
179 return i->second.is_critical();
180 return false;
181 }

Referenced by Botan::X509_Certificate::is_critical().

◆ decode_from()

void Botan::Extensions::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 264 of file x509_ext.cpp.

265 {
266 m_extension_oids.clear();
267 m_extension_info.clear();
268
269 BER_Decoder sequence = from_source.start_sequence();
270
271 while(sequence.more_items())
272 {
273 OID oid;
274 bool critical;
275 std::vector<uint8_t> bits;
276
277 sequence.start_sequence()
278 .decode(oid)
279 .decode_optional(critical, ASN1_Type::Boolean, ASN1_Class::Universal, false)
280 .decode(bits, ASN1_Type::OctetString)
281 .end_cons();
282
283 auto obj = create_extn_obj(oid, critical, bits);
284 Extensions_Info info(critical, bits, std::move(obj));
285
286 m_extension_oids.push_back(oid);
287 m_extension_info.emplace(oid, info);
288 }
289 sequence.verify_end();
290 }

References Botan::Boolean, Botan::BER_Decoder::decode(), Botan::BER_Decoder::decode_optional(), Botan::BER_Decoder::end_cons(), Botan::BER_Decoder::more_items(), Botan::OctetString, Botan::BER_Decoder::start_sequence(), Botan::Universal, and Botan::BER_Decoder::verify_end().

◆ encode_into()

void Botan::Extensions::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 240 of file x509_ext.cpp.

241 {
242 for(const auto& ext_info : m_extension_info)
243 {
244 const OID& oid = ext_info.first;
245 const bool should_encode = ext_info.second.obj().should_encode();
246
247 if(should_encode)
248 {
249 const bool is_critical = ext_info.second.is_critical();
250 const std::vector<uint8_t>& ext_value = ext_info.second.bits();
251
252 to_object.start_sequence()
253 .encode(oid)
254 .encode_optional(is_critical, false)
255 .encode(ext_value, ASN1_Type::OctetString)
256 .end_cons();
257 }
258 }
259 }

References Botan::DER_Encoder::encode(), Botan::DER_Encoder::encode_optional(), Botan::DER_Encoder::end_cons(), Botan::OctetString, and Botan::DER_Encoder::start_sequence().

◆ extension_set()

bool Botan::Extensions::extension_set ( const OID oid) const

Return true if an extension was set

Definition at line 170 of file x509_ext.cpp.

171 {
172 return (m_extension_info.find(oid) != m_extension_info.end());
173 }

◆ extensions()

std::vector< std::pair< std::unique_ptr< Certificate_Extension >, bool > > Botan::Extensions::extensions ( ) const

Returns a copy of the list of extensions together with the corresponding criticality flag. All extensions are encoded as some object, falling back to Unknown_Extension class which simply allows reading the bytes as well as the criticality flag.

Definition at line 210 of file x509_ext.cpp.

211 {
212 std::vector<std::pair<std::unique_ptr<Certificate_Extension>, bool>> exts;
213 exts.reserve(m_extension_info.size());
214 for(auto&& ext : m_extension_info)
215 {
216 exts.push_back(
217 std::make_pair(
218 ext.second.obj().copy(),
219 ext.second.is_critical())
220 );
221 }
222 return exts;
223 }

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

◆ extensions_raw()

std::map< OID, std::pair< std::vector< uint8_t >, bool > > Botan::Extensions::extensions_raw ( ) const

Returns the list of extensions as raw, encoded bytes together with the corresponding criticality flag. Contains all extensions, including any extensions encoded as Unknown_Extension

Definition at line 225 of file x509_ext.cpp.

226 {
227 std::map<OID, std::pair<std::vector<uint8_t>, bool>> out;
228 for(auto&& ext : m_extension_info)
229 {
230 out.emplace(ext.first,
231 std::make_pair(ext.second.bits(),
232 ext.second.is_critical()));
233 }
234 return out;
235 }

◆ get()

std::unique_ptr< Certificate_Extension > Botan::Extensions::get ( const OID oid) const

Searches for an extension by OID and returns the result. Only the known extensions types declared in this header are searched for by this function.

Returns
Copy of extension with oid, nullptr if not found. Can avoid creating a copy by using get_extension_object function

Definition at line 201 of file x509_ext.cpp.

202 {
203 if(const Certificate_Extension* ext = this->get_extension_object(oid))
204 {
205 return ext->copy();
206 }
207 return nullptr;
208 }
const Certificate_Extension * get_extension_object(const OID &oid) const
Definition: x509_ext.cpp:192

References get_extension_object().

◆ get_extension_bits()

std::vector< uint8_t > Botan::Extensions::get_extension_bits ( const OID oid) const

Return the raw bytes of the extension Will throw if OID was not set as an extension.

Definition at line 183 of file x509_ext.cpp.

184 {
185 auto i = m_extension_info.find(oid);
186 if(i == m_extension_info.end())
187 throw Invalid_Argument("Extensions::get_extension_bits no such extension set");
188
189 return i->second.bits();
190 }

◆ get_extension_object()

const Certificate_Extension * Botan::Extensions::get_extension_object ( const OID oid) const

Look up an object in the extensions, based on OID Returns nullptr if not set, if the extension was either absent or not handled. The pointer returned is owned by the Extensions object. This would be better with an optional<T> return value

Definition at line 192 of file x509_ext.cpp.

193 {
194 auto extn = m_extension_info.find(oid);
195 if(extn == m_extension_info.end())
196 return nullptr;
197
198 return &extn->second.obj();
199 }

Referenced by get().

◆ get_extension_object_as()

template<typename T >
const T * Botan::Extensions::get_extension_object_as ( const OID oid = T::static_oid()) const
inline

Definition at line 412 of file pkix_types.h.

413 {
414 if(const Certificate_Extension* extn = get_extension_object(oid))
415 {
416 // Unknown_Extension oid_name is empty
417 if(extn->oid_name().empty())
418 {
419 return nullptr;
420 }
421 else if(const T* extn_as_T = dynamic_cast<const T*>(extn))
422 {
423 return extn_as_T;
424 }
425 else
426 {
427 throw Decoding_Error("Exception::get_extension_object_as dynamic_cast failed");
428 }
429 }
430
431 return nullptr;
432 }
FE_25519 T
Definition: ge.cpp:36

References T.

◆ get_extension_oids()

const std::vector< OID > & Botan::Extensions::get_extension_oids ( ) const
inline

Return the set of extensions in the order they appeared in the certificate (or as they were added, if constructed)

Definition at line 438 of file pkix_types.h.

439 {
440 return m_extension_oids;
441 }

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

◆ get_raw()

template<typename T >
std::unique_ptr< T > Botan::Extensions::get_raw ( const OID oid) const
inline

Searches for an extension by OID and returns the result decoding it to some arbitrary extension type chosen by the application.

Only the unknown extensions, that is, extensions types that are not declared in this header, are searched for by this function.

Returns
Pointer to new extension with oid, nullptr if not found.

Definition at line 513 of file pkix_types.h.

514 {
515 auto extn_info = m_extension_info.find(oid);
516
517 if(extn_info != m_extension_info.end())
518 {
519 // Unknown_Extension oid_name is empty
520 if(extn_info->second.obj().oid_name() == "")
521 {
522 auto ext = std::make_unique<T>();
523 ext->decode_inner(extn_info->second.bits());
524 return ext;
525 }
526 }
527 return nullptr;
528 }

◆ operator=() [1/2]

Extensions & Botan::Extensions::operator= ( const Extensions )
default

◆ operator=() [2/2]

Extensions & Botan::Extensions::operator= ( Extensions &&  )
default

◆ remove()

bool Botan::Extensions::remove ( const OID oid)

Remove an extension from the list. Returns true if the extension had been set, false otherwise.

Definition at line 147 of file x509_ext.cpp.

148 {
149 const bool erased = m_extension_info.erase(oid) > 0;
150
151 if(erased)
152 {
153 m_extension_oids.erase(std::find(m_extension_oids.begin(), m_extension_oids.end(), oid));
154 }
155
156 return erased;
157 }

Referenced by replace().

◆ replace()

void Botan::Extensions::replace ( std::unique_ptr< Certificate_Extension extn,
bool  critical = false 
)

Adds an extension to the list or replaces it.

Parameters
extnthe certificate extension
criticalwhether this extension should be marked as critical

Definition at line 159 of file x509_ext.cpp.

160 {
161 // Remove it if it existed
162 remove(extn->oid_of());
163
164 const OID oid = extn->oid_of();
165 Extensions_Info info(critical, std::move(extn));
166 m_extension_oids.push_back(oid);
167 m_extension_info.emplace(oid, info);
168 }
bool remove(const OID &oid)
Definition: x509_ext.cpp:147

References remove().

Referenced by Botan::X509_CA::choose_extensions().


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