Botan 3.13.0
Crypto and TLS for C&
Botan::DER_Encoder Class Referencefinal

#include <der_enc.h>

Public Types

typedef std::function< void(const uint8_t[], size_t)> append_fn

Public Member Functions

DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, const secure_vector< uint8_t > &rep)
DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, const std::vector< uint8_t > &rep)
DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, std::span< const uint8_t > rep)
DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, std::string_view str)
DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, uint8_t val)
 DER_Encoder ()=default
BOTAN_FUTURE_EXPLICIT DER_Encoder (append_fn append)
BOTAN_FUTURE_EXPLICIT DER_Encoder (secure_vector< uint8_t > &vec)
BOTAN_FUTURE_EXPLICIT DER_Encoder (std::vector< uint8_t > &vec)
DER_Encoderencode (bool b)
DER_Encoderencode (bool b, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
DER_Encoderencode (const ASN1_Object &obj)
DER_Encoderencode (const BigInt &n)
DER_Encoderencode (const BigInt &n, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
DER_Encoderencode (const uint8_t v[], size_t len, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
DER_Encoderencode (const uint8_t val[], size_t len, ASN1_Type real_type)
DER_Encoderencode (size_t s)
DER_Encoderencode (size_t s, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
DER_Encoderencode (std::span< const uint8_t > val, ASN1_Type real_type)
DER_Encoderencode (std::span< const uint8_t > value, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
DER_Encoderencode_bitstring (const ASN1_BitString &bits, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
DER_Encoderencode_bitstring (std::span< const uint8_t > bits, size_t unused_bits=0, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
DER_Encoderencode_if (bool pred, bool num)
DER_Encoderencode_if (bool pred, const ASN1_Object &obj)
DER_Encoderencode_if (bool pred, DER_Encoder &enc)
DER_Encoderencode_if (bool pred, size_t num)
template<typename T>
DER_Encoderencode_implicit (const T &value, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
DER_Encoderencode_list (const std::vector< T > &values)
DER_Encoderencode_named_bitstring (uint64_t bits, size_t width, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
DER_Encoderencode_null ()
DER_Encoderencode_octet_aligned_bitstring (std::span< const uint8_t > bytes, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
template<typename T>
DER_Encoderencode_optional (const std::optional< T > &value)
template<typename T>
DER_Encoderencode_optional (const T &value, const T &default_value)
DER_Encoderend_cons ()
DER_Encoderend_explicit ()
secure_vector< uint8_t > get_contents ()
std::vector< uint8_t > get_contents_unlocked ()
DER_Encoderraw_bytes (const uint8_t val[], size_t len)
DER_Encoderraw_bytes (std::span< const uint8_t > val)
DER_Encoderstart_cons (ASN1_Type type_tag, ASN1_Class class_tag)
DER_Encoderstart_context_specific (uint32_t tag)
DER_Encoderstart_explicit (uint16_t type_tag)
DER_Encoderstart_explicit_context_specific (uint32_t tag)
DER_Encoderstart_sequence ()
DER_Encoderstart_set ()
DER_Encoderstart_set (ASN1_Type type_tag, ASN1_Class class_tag)
DER_Encoderstart_set (uint32_t tag)

Detailed Description

General DER Encoding Object

Definition at line 25 of file der_enc.h.

Member Typedef Documentation

◆ append_fn

typedef std::function<void(const uint8_t[], size_t)> Botan::DER_Encoder::append_fn

Callback type invoked with each chunk of encoded output

Definition at line 30 of file der_enc.h.

Constructor & Destructor Documentation

◆ DER_Encoder() [1/4]

◆ DER_Encoder() [2/4]

Botan::DER_Encoder::DER_Encoder ( secure_vector< uint8_t > & vec)

DER encode, writing to

Parameters
vecIf this constructor is used, get_contents* may not be called.

Definition at line 81 of file der_enc.cpp.

81 {
82 m_append_output = [&vec](const uint8_t b[], size_t l) {
83 if(l > 0) {
84 vec.insert(vec.end(), b, b + l);
85 }
86 };
87}

◆ DER_Encoder() [3/4]

Botan::DER_Encoder::DER_Encoder ( std::vector< uint8_t > & vec)

DER encode, writing to

Parameters
vecIf this constructor is used, get_contents* may not be called.

Definition at line 89 of file der_enc.cpp.

89 {
90 m_append_output = [&vec](const uint8_t b[], size_t l) {
91 if(l > 0) {
92 vec.insert(vec.end(), b, b + l);
93 }
94 };
95}

◆ DER_Encoder() [4/4]

BOTAN_FUTURE_EXPLICIT Botan::DER_Encoder::DER_Encoder ( append_fn append)
inline

DER encode, calling append to write output If this constructor is used, get_contents* may not be called.

Definition at line 55 of file der_enc.h.

55: m_append_output(std::move(append)) {}

References BOTAN_FUTURE_EXPLICIT.

Member Function Documentation

◆ add_object() [1/6]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Type type_tag,
ASN1_Class class_tag,
const secure_vector< uint8_t > & rep )
inline

Write a tag and length header followed by the given contents

Parameters
type_tagthe type tag to encode with
class_tagthe class tag to encode with
repthe contents of the object

Definition at line 432 of file der_enc.h.

432 {
433 return add_object(type_tag, class_tag, std::span{rep});
434 }
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition der_enc.cpp:285

References add_object(), and DER_Encoder().

◆ add_object() [2/6]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Type type_tag,
ASN1_Class class_tag,
const std::vector< uint8_t > & rep )
inline

Write a tag and length header followed by the given contents

Parameters
type_tagthe type tag to encode with
class_tagthe class tag to encode with
repthe contents of the object

Definition at line 421 of file der_enc.h.

421 {
422 return add_object(type_tag, class_tag, std::span{rep});
423 }

References add_object(), and DER_Encoder().

◆ add_object() [3/6]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Type type_tag,
ASN1_Class class_tag,
const uint8_t rep[],
size_t length )

Write a tag and length header followed by the given contents

Parameters
type_tagthe type tag to encode with
class_tagthe class tag to encode with
repthe contents of the object
lengththe length of rep in bytes

Definition at line 285 of file der_enc.cpp.

285 {
286 std::vector<uint8_t> hdr;
287 encode_tag(hdr, type_tag, class_tag);
288 encode_length(hdr, length);
289
290 if(!m_subsequences.empty()) {
291 m_subsequences[m_subsequences.size() - 1].add_bytes(hdr.data(), hdr.size(), rep, length);
292 } else if(m_append_output) {
293 m_append_output(hdr.data(), hdr.size());
294 m_append_output(rep, length);
295 } else {
296 m_default_outbuf += hdr;
297 m_default_outbuf += std::make_pair(rep, length);
298 }
299
300 return (*this);
301}

References DER_Encoder().

Referenced by add_object(), add_object(), add_object(), add_object(), add_object(), Botan::AlternativeName::decode_from(), encode(), encode(), encode(), encode_bitstring(), Botan::AlternativeName::encode_into(), Botan::ASN1_String::encode_into(), Botan::ASN1_Time::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::encode_into(), Botan::GeneralName::encode_into(), Botan::OCSP::SingleResponse::encode_into(), Botan::OID::encode_into(), Botan::X509_Serial_Number::encode_into(), encode_null(), and Botan::PKCS12::export_to().

◆ add_object() [4/6]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Type type_tag,
ASN1_Class class_tag,
std::span< const uint8_t > rep )
inline

Write a tag and length header followed by the given contents

Parameters
type_tagthe type tag to encode with
class_tagthe class tag to encode with
repthe contents of the object

Definition at line 410 of file der_enc.h.

410 {
411 return add_object(type_tag, class_tag, rep.data(), rep.size());
412 }

References add_object(), and DER_Encoder().

◆ add_object() [5/6]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Type type_tag,
ASN1_Class class_tag,
std::string_view str )

Write a tag and length header followed by the given contents

Parameters
type_tagthe type tag to encode with
class_tagthe class tag to encode with
strthe contents of the object

Definition at line 475 of file der_enc.cpp.

475 {
476 return add_object(type_tag, class_tag, as_span_of_bytes(rep_str));
477}
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:59

References add_object(), Botan::as_span_of_bytes(), and DER_Encoder().

◆ add_object() [6/6]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Type type_tag,
ASN1_Class class_tag,
uint8_t val )

Write a tag and length header followed by a single byte of contents

Parameters
type_tagthe type tag to encode with
class_tagthe class tag to encode with
valthe contents of the object

Definition at line 482 of file der_enc.cpp.

482 {
483 return add_object(type_tag, class_tag, std::span<const uint8_t>{&rep, 1});
484}

References add_object(), and DER_Encoder().

◆ encode() [1/11]

DER_Encoder & Botan::DER_Encoder::encode ( bool b)

Encode a BOOLEAN

Definition at line 313 of file der_enc.cpp.

313 {
315}
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313

References Botan::Boolean, DER_Encoder(), encode(), and Botan::Universal.

Referenced by Botan::AlternativeName::add_other_name(), Botan::GOST_3410_PublicKey::algorithm_identifier(), Botan::OCSP::Request::BER_encode(), Botan::PKCS8::BER_encode(), Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(), Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(), Botan::PKCS10_Request::create(), Botan::DL_Group::DER_encode(), Botan::DL_PrivateKey::DER_encode(), Botan::DL_PublicKey::DER_encode(), Botan::EC_Group::DER_encode(), Botan::TLS::Session::DER_encode(), Botan::EC_Group_Data::EC_Group_Data(), encode(), encode(), encode(), encode(), encode(), encode(), encode(), encode_if(), encode_if(), encode_if(), Botan::AlgorithmIdentifier::encode_into(), Botan::AlternativeName::encode_into(), Botan::Attribute::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdOrRange::encode_into(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::encode_into(), Botan::CRL_Entry::encode_into(), Botan::Extensions::encode_into(), Botan::GeneralSubtree::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::OCSP::SingleResponse::encode_into(), Botan::PSS_Params::encode_into(), Botan::X509_DN::encode_into(), Botan::X509_Object::encode_into(), encode_list(), encode_optional(), encode_optional(), Botan::PKCS12::export_to(), Botan::X509_Object::make_signed(), Botan::pkcs12_pbe_encrypt(), Botan::EC_PrivateKey::private_key_bits(), Botan::Ed25519_PrivateKey::private_key_bits(), Botan::Ed448_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::private_key_bits(), Botan::X25519_PrivateKey::private_key_bits(), Botan::X448_PrivateKey::private_key_bits(), Botan::XMSS_PrivateKey::private_key_bits(), Botan::Private_Key::private_key_info(), Botan::GOST_3410_PublicKey::public_key_bits(), Botan::McEliece_PublicKey::public_key_bits(), Botan::RSA_PublicKey::public_key_bits(), Botan::TPM_PrivateKey::public_key_bits(), Botan::XMSS_PublicKey::public_key_bits(), Botan::PSS_Params::serialize(), Botan::TLS::Certificate_Request_12::serialize(), Botan::EC_Group_Data::set_oid(), and Botan::Public_Key::subject_public_key().

◆ encode() [2/11]

DER_Encoder & Botan::DER_Encoder::encode ( bool b,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Encode a BOOLEAN with an IMPLICIT tagging

Parameters
bthe value to encode
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 341 of file der_enc.cpp.

341 {
342 const uint8_t val = is_true ? 0xFF : 0x00;
343 return add_object(type_tag, class_tag, &val, 1);
344}

References add_object(), and DER_Encoder().

◆ encode() [3/11]

DER_Encoder & Botan::DER_Encoder::encode ( const ASN1_Object & obj)

Request for an object to encode itself to this stream

Parameters
objthe object to encode

Definition at line 467 of file der_enc.cpp.

467 {
468 obj.encode_into(*this);
469 return (*this);
470}

References DER_Encoder(), and Botan::ASN1_Object::encode_into().

◆ encode() [4/11]

DER_Encoder & Botan::DER_Encoder::encode ( const BigInt & n)

Encode an INTEGER

Definition at line 327 of file der_enc.cpp.

References DER_Encoder(), encode(), Botan::Integer, and Botan::Universal.

◆ encode() [5/11]

DER_Encoder & Botan::DER_Encoder::encode ( const BigInt & n,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Encode an INTEGER with an IMPLICIT tagging

Parameters
nthe value to encode
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 389 of file der_enc.cpp.

389 {
390 return add_object(type_tag, class_tag, ASN1::integer_contents(n));
391}
std::vector< uint8_t > integer_contents(const BigInt &n)
Definition der_enc.cpp:356

References add_object(), DER_Encoder(), and Botan::ASN1::integer_contents().

◆ encode() [6/11]

DER_Encoder & Botan::DER_Encoder::encode ( const uint8_t v[],
size_t len,
ASN1_Type real_type,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Encode an OCTET STRING or octet aligned BIT STRING with an IMPLICIT tagging

Parameters
vthe contents of the object
lenthe length of v in bytes
real_typeeither ASN1_Type::OctetString or ASN1_Type::BitString
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 285 of file der_enc.h.

289 {
290 return encode(std::span{v, len}, real_type, type_tag, class_tag);
291 }

References Botan::ContextSpecific, DER_Encoder(), and encode().

◆ encode() [7/11]

DER_Encoder & Botan::DER_Encoder::encode ( const uint8_t val[],
size_t len,
ASN1_Type real_type )
inline

Encode an OCTET STRING or an octet aligned BIT STRING

Parameters
valthe contents of the object
lenthe length of val in bytes
real_typeeither ASN1_Type::OctetString or ASN1_Type::BitString

Definition at line 232 of file der_enc.h.

232 {
233 return this->encode(std::span{val, len}, real_type);
234 }

References DER_Encoder(), and encode().

◆ encode() [8/11]

DER_Encoder & Botan::DER_Encoder::encode ( size_t s)

Encode an INTEGER

Definition at line 320 of file der_enc.cpp.

320 {
322}
static BigInt from_u64(uint64_t n)
Definition bigint.cpp:30

References DER_Encoder(), encode(), Botan::BigInt::from_u64(), Botan::Integer, and Botan::Universal.

◆ encode() [9/11]

DER_Encoder & Botan::DER_Encoder::encode ( size_t s,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Encode an INTEGER with an IMPLICIT tagging

Parameters
sthe value to encode
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 349 of file der_enc.cpp.

349 {
350 return encode(BigInt::from_u64(n), type_tag, class_tag);
351}

References DER_Encoder(), encode(), and Botan::BigInt::from_u64().

◆ encode() [10/11]

DER_Encoder & Botan::DER_Encoder::encode ( std::span< const uint8_t > val,
ASN1_Type real_type )

Encode an OCTET STRING or an octet aligned BIT STRING

Parameters
valthe contents of the object
real_typeeither ASN1_Type::OctetString or ASN1_Type::BitString

Definition at line 334 of file der_enc.cpp.

334 {
335 return encode(bytes, real_type, real_type, ASN1_Class::Universal);
336}

References DER_Encoder(), encode(), and Botan::Universal.

◆ encode() [11/11]

DER_Encoder & Botan::DER_Encoder::encode ( std::span< const uint8_t > value,
ASN1_Type real_type,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Encode an OCTET STRING or octet aligned BIT STRING with an IMPLICIT tagging

Parameters
valuethe contents of the object
real_typeeither ASN1_Type::OctetString or ASN1_Type::BitString
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 396 of file der_enc.cpp.

399 {
400 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
401 throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
402 }
403
404 if(real_type == ASN1_Type::BitString) {
405 return encode_bitstring(bytes, 0, type_tag, class_tag);
406 } else {
407 return add_object(type_tag, class_tag, bytes);
408 }
409}
DER_Encoder & encode_bitstring(std::span< const uint8_t > bits, size_t unused_bits=0, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
Definition der_enc.cpp:411

References add_object(), Botan::BitString, DER_Encoder(), encode_bitstring(), and Botan::OctetString.

◆ encode_bitstring() [1/2]

DER_Encoder & Botan::DER_Encoder::encode_bitstring ( const ASN1_BitString & bits,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )

Encode a BIT STRING

Parameters
bitsthe value to encode
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 434 of file der_enc.cpp.

434 {
435 return encode_bitstring(bits.bytes(), bits.unused_bits(), type_tag, class_tag);
436}

References Botan::ASN1_BitString::bytes(), DER_Encoder(), encode_bitstring(), and Botan::ASN1_BitString::unused_bits().

◆ encode_bitstring() [2/2]

DER_Encoder & Botan::DER_Encoder::encode_bitstring ( std::span< const uint8_t > bits,
size_t unused_bits = 0,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )

Encode a BIT STRING, with bits not including the initial unused-bits octet.

Definition at line 411 of file der_enc.cpp.

414 {
415 if(unused_bits >= 8) {
416 throw Invalid_Argument("DER_Encoder: Invalid unused bit count for BIT STRING");
417 }
418
419 if(bits.empty() && unused_bits != 0) {
420 throw Invalid_Argument("DER_Encoder: Empty BIT STRING cannot have unused bits");
421 }
422
423 if(unused_bits > 0 && (bits.back() & ((1U << unused_bits) - 1)) != 0) {
424 throw Invalid_Argument("DER_Encoder: BIT STRING unused bits must be zero");
425 }
426
428 encoded.reserve(1 + bits.size());
429 encoded.push_back(static_cast<uint8_t>(unused_bits));
430 encoded.insert(encoded.end(), bits.begin(), bits.end());
431 return add_object(type_tag, class_tag, encoded);
432}
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128

References add_object(), and DER_Encoder().

Referenced by encode(), encode_bitstring(), Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::encode_into(), encode_named_bitstring(), and encode_octet_aligned_bitstring().

◆ encode_if() [1/4]

DER_Encoder & Botan::DER_Encoder::encode_if ( bool pred,
bool num )
inline

Encode a BOOLEAN if pred is true

Parameters
predif false nothing is written
numthe value to encode

Definition at line 386 of file der_enc.h.

386 {
387 if(pred) {
388 encode(num);
389 }
390 return (*this);
391 }

References DER_Encoder(), and encode().

◆ encode_if() [2/4]

DER_Encoder & Botan::DER_Encoder::encode_if ( bool pred,
const ASN1_Object & obj )
inline

Encode an object if pred is true

Parameters
predif false nothing is written
objthe object to encode

Definition at line 360 of file der_enc.h.

360 {
361 if(pred) {
362 encode(obj);
363 }
364 return (*this);
365 }

References DER_Encoder(), and encode().

◆ encode_if() [3/4]

DER_Encoder & Botan::DER_Encoder::encode_if ( bool pred,
DER_Encoder & enc )
inline

Write the contents of another encoder to this stream if pred is true

Parameters
predif false nothing is written
encthe encoder whose contents are written

Definition at line 347 of file der_enc.h.

347 {
348 if(pred) {
349 return raw_bytes(enc.get_contents());
350 }
351 return (*this);
352 }
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition der_enc.cpp:237

References DER_Encoder(), get_contents(), and raw_bytes().

◆ encode_if() [4/4]

DER_Encoder & Botan::DER_Encoder::encode_if ( bool pred,
size_t num )
inline

Encode an INTEGER if pred is true

Parameters
predif false nothing is written
numthe value to encode

Definition at line 373 of file der_enc.h.

373 {
374 if(pred) {
375 encode(num);
376 }
377 return (*this);
378 }

References DER_Encoder(), and encode().

◆ encode_implicit()

template<typename T>
DER_Encoder & Botan::DER_Encoder::encode_implicit ( const T & value,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Encode value and emit just its body bytes under an IMPLICIT type_tag/class_tag (e.g. for [N] IMPLICIT OBJECT IDENTIFIER where the body is an OID's arc bytes but the tag must be [N]). The primitive/constructed bit is copied from value.

Definition at line 461 of file der_enc.h.

463 {
464 std::vector<uint8_t> tlv;
465 DER_Encoder(tlv).encode(value);
466 return add_object_tlv(type_tag, class_tag, std::move(tlv));
467 }
DER_Encoder()=default

References Botan::ContextSpecific, and DER_Encoder().

Referenced by Botan::AlternativeName::encode_into().

◆ encode_list()

template<typename T>
DER_Encoder & Botan::DER_Encoder::encode_list ( const std::vector< T > & values)
inline

Encode each element of the vector in turn

Parameters
valuesthe values to encode

Definition at line 327 of file der_enc.h.

327 {
328 for(size_t i = 0; i != values.size(); ++i) {
329 encode(values[i]);
330 }
331 return (*this);
332 }

References DER_Encoder(), and encode().

Referenced by Botan::TLS::Session::DER_encode(), Botan::Cert_Extension::ASBlocks::ASIdentifierChoice::encode_into(), and Botan::Cert_Extension::IPAddressBlocks::IPAddressChoice< V >::encode_into().

◆ encode_named_bitstring()

DER_Encoder & Botan::DER_Encoder::encode_named_bitstring ( uint64_t bits,
size_t width,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )

Helper for encoding BIT STRING elements that are actually bit sets, rather than being OCTET STRINGS with the wrong type.

Definition at line 438 of file der_enc.cpp.

441 {
442 if(width > 64) {
443 throw Invalid_Argument("DER_Encoder: Named BIT STRING width is too large");
444 }
445
446 if(width < 64 && (bits >> width) != 0) {
447 throw Invalid_Argument("DER_Encoder: Named BIT STRING has bits outside range");
448 }
449
450 if(bits == 0) {
451 return encode_bitstring({}, 0, type_tag, class_tag);
452 }
453
454 const size_t bit_length = width - ctz(bits);
455 const size_t byte_length = (bit_length + 7) / 8;
456 std::vector<uint8_t> encoded(byte_length);
457
458 for(size_t bit = 0; bit != bit_length; ++bit) {
459 if((bits & (uint64_t(1) << (width - 1 - bit))) != 0) {
460 encoded[bit / 8] |= static_cast<uint8_t>(0x80 >> (bit % 8));
461 }
462 }
463
464 return encode_bitstring(encoded, byte_length * 8 - bit_length, type_tag, class_tag);
465}
BOTAN_FORCE_INLINE constexpr size_t ctz(T n)
Definition bit_ops.h:115

References Botan::ctz(), DER_Encoder(), and encode_bitstring().

◆ encode_null()

◆ encode_octet_aligned_bitstring()

DER_Encoder & Botan::DER_Encoder::encode_octet_aligned_bitstring ( std::span< const uint8_t > bytes,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )
inline

Encode a BIT STRING with no unused bits

Parameters
bytesthe bits to encode
type_tagthe type tag to encode with
class_tagthe class tag to encode with

Definition at line 210 of file der_enc.h.

212 {
213 return encode_bitstring(bytes, 0, type_tag, class_tag);
214 }

References Botan::BitString, DER_Encoder(), encode_bitstring(), and Botan::Universal.

Referenced by Botan::X509_Object::encode_into(), Botan::X509_Object::make_signed(), Botan::EC_PrivateKey::private_key_bits(), and Botan::Public_Key::subject_public_key().

◆ encode_optional() [1/2]

template<typename T>
DER_Encoder & Botan::DER_Encoder::encode_optional ( const std::optional< T > & value)
inline

Encode a value if it is set, otherwise write nothing

Parameters
valuethe value to encode

Definition at line 314 of file der_enc.h.

314 {
315 if(value) {
316 encode(*value);
317 }
318 return (*this);
319 }

References DER_Encoder(), and encode().

◆ encode_optional() [2/2]

template<typename T>
DER_Encoder & Botan::DER_Encoder::encode_optional ( const T & value,
const T & default_value )
inline

Encode a value unless it is equal to the DEFAULT

Parameters
valuethe value to encode
default_valuethe value which should be omitted

Definition at line 301 of file der_enc.h.

301 {
302 if(value != default_value) {
303 encode(value);
304 }
305 return (*this);
306 }

References DER_Encoder(), encode(), and encode_optional().

Referenced by Botan::Extensions::encode_into(), and encode_optional().

◆ end_cons()

DER_Encoder & Botan::DER_Encoder::end_cons ( )

Finish the innermost open constructed encoding

Throws Invalid_State if no constructed encoding is open.

Definition at line 208 of file der_enc.cpp.

208 {
209 if(m_subsequences.empty()) {
210 throw Invalid_State("DER_Encoder::end_cons: No such sequence");
211 }
212
213 DER_Sequence last_seq = std::move(m_subsequences[m_subsequences.size() - 1]);
214 m_subsequences.pop_back();
215 last_seq.push_contents(*this);
216
217 return (*this);
218}

References DER_Encoder().

Referenced by Botan::GOST_3410_PublicKey::algorithm_identifier(), Botan::OCSP::Request::BER_encode(), Botan::PKCS8::BER_encode(), Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(), Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(), Botan::PKCS10_Request::create(), Botan::DL_Group::DER_encode(), Botan::EC_Group::DER_encode(), Botan::TLS::Session::DER_encode(), Botan::AlgorithmIdentifier::encode_into(), Botan::AlternativeName::encode_into(), Botan::Attribute::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifierChoice::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdOrRange::encode_into(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressChoice< V >::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::encode_into(), Botan::CRL_Entry::encode_into(), Botan::Extensions::encode_into(), Botan::GeneralSubtree::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::OCSP::SingleResponse::encode_into(), Botan::PSS_Params::encode_into(), Botan::X509_DN::encode_into(), Botan::X509_Object::encode_into(), end_explicit(), Botan::PKCS12::export_to(), Botan::X509_Object::make_signed(), Botan::pkcs12_pbe_encrypt(), Botan::EC_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::private_key_bits(), Botan::Private_Key::private_key_info(), Botan::McEliece_PublicKey::public_key_bits(), Botan::RSA_PublicKey::public_key_bits(), Botan::TPM_PrivateKey::public_key_bits(), and Botan::Public_Key::subject_public_key().

◆ end_explicit()

DER_Encoder & Botan::DER_Encoder::end_explicit ( )

Finish the innermost open constructed encoding, an alias for end_cons()

Definition at line 230 of file der_enc.cpp.

230 {
231 return end_cons();
232}
DER_Encoder & end_cons()
Definition der_enc.cpp:208

References DER_Encoder(), and end_cons().

Referenced by Botan::OCSP::Request::BER_encode(), Botan::PKCS10_Request::create(), Botan::AlternativeName::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::encode_into(), and Botan::OCSP::SingleResponse::encode_into().

◆ get_contents()

secure_vector< uint8_t > Botan::DER_Encoder::get_contents ( )

Return the encoded contents

Throws Invalid_State if any constructed encoding is still open, or if this encoder was constructed with an output vector or append function.

Definition at line 161 of file der_enc.cpp.

161 {
162 if(!m_subsequences.empty()) {
163 throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
164 }
165
166 if(m_append_output) {
167 throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
168 }
169
171 std::swap(output, m_default_outbuf);
172 return output;
173}

Referenced by Botan::DL_PrivateKey::DER_encode(), Botan::TLS::Session::DER_encode(), encode_if(), Botan::EC_PrivateKey::private_key_bits(), Botan::Ed25519_PrivateKey::private_key_bits(), Botan::Ed448_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::private_key_bits(), Botan::X25519_PrivateKey::private_key_bits(), Botan::X448_PrivateKey::private_key_bits(), Botan::XMSS_PrivateKey::private_key_bits(), Botan::Private_Key::private_key_info(), and Botan::TLS::Certificate_Request_12::serialize().

◆ get_contents_unlocked()

std::vector< uint8_t > Botan::DER_Encoder::get_contents_unlocked ( )

Return the encoded contents as a std::vector

If using this function, instead pass a std::vector to the constructor of DER_Encoder where the output will be placed. This avoids several unnecessary copies.

Definition at line 175 of file der_enc.cpp.

175 {
176 if(!m_subsequences.empty()) {
177 throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
178 }
179
180 if(m_append_output) {
181 throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
182 }
183
184 std::vector<uint8_t> output(m_default_outbuf.begin(), m_default_outbuf.end());
185 m_default_outbuf.clear();
186 return output;
187}

Referenced by Botan::PKCS10_Request::create().

◆ raw_bytes() [1/2]

DER_Encoder & Botan::DER_Encoder::raw_bytes ( const uint8_t val[],
size_t len )

Insert raw bytes directly into the output stream

Definition at line 237 of file der_enc.cpp.

237 {
238 if(!m_subsequences.empty()) {
239 m_subsequences[m_subsequences.size() - 1].add_bytes(bytes, length);
240 } else if(m_append_output) {
241 m_append_output(bytes, length);
242 } else {
243 m_default_outbuf += std::make_pair(bytes, length);
244 }
245
246 return (*this);
247}

References DER_Encoder().

Referenced by Botan::PKCS10_Request::create(), encode_if(), Botan::AlgorithmIdentifier::encode_into(), Botan::AlternativeName::encode_into(), Botan::Attribute::encode_into(), Botan::X509_DN::encode_into(), Botan::X509_Object::encode_into(), and Botan::X509_Object::make_signed().

◆ raw_bytes() [2/2]

DER_Encoder & Botan::DER_Encoder::raw_bytes ( std::span< const uint8_t > val)
inline

Insert raw bytes directly into the output stream

Definition at line 154 of file der_enc.h.

154{ return raw_bytes(val.data(), val.size()); }

References DER_Encoder(), and raw_bytes().

Referenced by raw_bytes().

◆ start_cons()

DER_Encoder & Botan::DER_Encoder::start_cons ( ASN1_Type type_tag,
ASN1_Class class_tag )

Start a constructed encoding with the given tagging. Must be closed with end_cons(). Contents are emitted in the order they are encoded.

Parameters
type_tagthe type tag of the constructed encoding
class_tagthe class tag of the constructed encoding

Definition at line 192 of file der_enc.cpp.

192 {
193 return start_cons(type_tag, class_tag, false);
194}
DER_Encoder & start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition der_enc.cpp:192

References DER_Encoder(), and start_cons().

Referenced by Botan::OCSP::SingleResponse::encode_into(), start_cons(), start_context_specific(), start_explicit(), start_explicit_context_specific(), start_sequence(), start_set(), and start_set().

◆ start_context_specific()

DER_Encoder & Botan::DER_Encoder::start_context_specific ( uint32_t tag)
inline

Start an IMPLICIT context specific constructed encoding

Parameters
tagthe context specific tag number

Definition at line 113 of file der_enc.h.

113 {
115 }
ASN1_Type
Definition asn1_obj.h:47

References Botan::ContextSpecific, DER_Encoder(), and start_cons().

Referenced by Botan::PSS_Params::encode_into(), and Botan::PKCS12::export_to().

◆ start_explicit()

DER_Encoder & Botan::DER_Encoder::start_explicit ( uint16_t type_tag)

Start a context specific constructed encoding, an alias for start_context_specific()

Parameters
type_tagthe context specific tag number

Definition at line 223 of file der_enc.cpp.

223 {
224 return start_cons(static_cast<ASN1_Type>(type_no), ASN1_Class::ContextSpecific);
225}

References Botan::ContextSpecific, DER_Encoder(), and start_cons().

Referenced by Botan::OCSP::Request::BER_encode(), Botan::PKCS10_Request::create(), Botan::AlternativeName::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::encode_into(), and Botan::OCSP::SingleResponse::encode_into().

◆ start_explicit_context_specific()

DER_Encoder & Botan::DER_Encoder::start_explicit_context_specific ( uint32_t tag)
inline

Start an EXPLICIT context specific constructed encoding

Parameters
tagthe context specific tag number

Definition at line 122 of file der_enc.h.

References DER_Encoder(), Botan::ExplicitContextSpecific, and start_cons().

Referenced by Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::encode_into(), and Botan::EC_PrivateKey::private_key_bits().

◆ start_sequence()

DER_Encoder & Botan::DER_Encoder::start_sequence ( )
inline

Start a SEQUENCE. Must be closed with end_cons().

Definition at line 86 of file der_enc.h.

References DER_Encoder(), Botan::Sequence, start_cons(), start_sequence(), and Botan::Universal.

Referenced by Botan::GOST_3410_PublicKey::algorithm_identifier(), Botan::OCSP::Request::BER_encode(), Botan::PKCS8::BER_encode(), Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(), Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(), Botan::PKCS10_Request::create(), Botan::DL_Group::DER_encode(), Botan::EC_Group::DER_encode(), Botan::TLS::Session::DER_encode(), Botan::AlgorithmIdentifier::encode_into(), Botan::AlternativeName::encode_into(), Botan::Attribute::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifierChoice::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::encode_into(), Botan::Cert_Extension::ASBlocks::ASIdOrRange::encode_into(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressChoice< V >::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::encode_into(), Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::encode_into(), Botan::CRL_Entry::encode_into(), Botan::Extensions::encode_into(), Botan::GeneralSubtree::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::OCSP::SingleResponse::encode_into(), Botan::PSS_Params::encode_into(), Botan::X509_DN::encode_into(), Botan::X509_Object::encode_into(), Botan::PKCS12::export_to(), Botan::X509_Object::make_signed(), Botan::pkcs12_pbe_encrypt(), Botan::EC_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::private_key_bits(), Botan::Private_Key::private_key_info(), Botan::McEliece_PublicKey::public_key_bits(), Botan::RSA_PublicKey::public_key_bits(), Botan::TPM_PrivateKey::public_key_bits(), start_sequence(), and Botan::Public_Key::subject_public_key().

◆ start_set() [1/3]

DER_Encoder & Botan::DER_Encoder::start_set ( )
inline

Start a SET/SET OF. Must be closed with end_cons(). Contents are DER sorted.

Definition at line 91 of file der_enc.h.

References DER_Encoder(), Botan::Set, start_cons(), and Botan::Universal.

Referenced by Botan::Attribute::encode_into(), and Botan::X509_DN::encode_into().

◆ start_set() [2/3]

DER_Encoder & Botan::DER_Encoder::start_set ( ASN1_Type type_tag,
ASN1_Class class_tag )

Start a SET/SET OF with an alternate tag. Contents are still DER sorted.

Parameters
type_tagthe type tag of the constructed encoding
class_tagthe class tag of the constructed encoding

Definition at line 196 of file der_enc.cpp.

196 {
197 return start_cons(type_tag, class_tag, true);
198}

References DER_Encoder(), and start_cons().

◆ start_set() [3/3]

DER_Encoder & Botan::DER_Encoder::start_set ( uint32_t tag)
inline

Start a SET/SET OF with a context specific tag. Contents are still DER sorted.

Parameters
tagthe context specific tag number

Definition at line 106 of file der_enc.h.

DER_Encoder & start_set()
Definition der_enc.h:91

References Botan::ContextSpecific, DER_Encoder(), and start_set().

Referenced by start_set().


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