Botan 3.4.0
Crypto and TLS for C&
Public Types | Public Member Functions | List of all members
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::string_view str)
 
DER_Encoderadd_object (ASN1_Type type_tag, ASN1_Class class_tag, uint8_t val)
 
 DER_Encoder ()=default
 
 DER_Encoder (append_fn append)
 
 DER_Encoder (secure_vector< uint8_t > &vec)
 
 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)
 
template<typename Alloc >
DER_Encoderencode (const std::vector< uint8_t, Alloc > &bytes, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag)
 
template<typename Alloc >
DER_Encoderencode (const std::vector< uint8_t, Alloc > &vec, ASN1_Type real_type)
 
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_if (bool pred, const ASN1_Object &obj)
 
DER_Encoderencode_if (bool pred, DER_Encoder &enc)
 
template<typename T >
DER_Encoderencode_list (const std::vector< T > &values)
 
DER_Encoderencode_null ()
 
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 ()
 
template<typename Alloc >
DER_Encoderraw_bytes (const std::vector< uint8_t, Alloc > &val)
 
DER_Encoderraw_bytes (const uint8_t val[], size_t len)
 
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 ()
 

Detailed Description

General DER Encoding Object

Definition at line 22 of file der_enc.h.

Member Typedef Documentation

◆ append_fn

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

Definition at line 24 of file der_enc.h.

Constructor & Destructor Documentation

◆ DER_Encoder() [1/4]

Botan::DER_Encoder::DER_Encoder ( )
default

DER encode, writing to an internal buffer Use get_contents or get_contents_unlocked to read the results after all encoding is completed.

◆ 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 67 of file der_enc.cpp.

67 {
68 m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); };
69}

◆ 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 71 of file der_enc.cpp.

71 {
72 m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); };
73}

◆ DER_Encoder() [4/4]

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 49 of file der_enc.h.

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

Member Function Documentation

◆ add_object() [1/5]

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

Definition at line 167 of file der_enc.h.

167 {
168 return add_object(type_tag, class_tag, rep.data(), rep.size());
169 }
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition der_enc.cpp:222

◆ add_object() [2/5]

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

Definition at line 163 of file der_enc.h.

163 {
164 return add_object(type_tag, class_tag, rep.data(), rep.size());
165 }

◆ add_object() [3/5]

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

Definition at line 222 of file der_enc.cpp.

222 {
223 std::vector<uint8_t> hdr;
224 encode_tag(hdr, type_tag, class_tag);
225 encode_length(hdr, length);
226
227 if(!m_subsequences.empty()) {
228 m_subsequences[m_subsequences.size() - 1].add_bytes(hdr.data(), hdr.size(), rep, length);
229 } else if(m_append_output) {
230 m_append_output(hdr.data(), hdr.size());
231 m_append_output(rep, length);
232 } else {
233 m_default_outbuf += hdr;
234 m_default_outbuf += std::make_pair(rep, length);
235 }
236
237 return (*this);
238}

Referenced by add_object(), add_object(), encode(), encode(), encode(), Botan::OID::encode_into(), Botan::ASN1_Time::encode_into(), Botan::ASN1_String::encode_into(), and encode_null().

◆ add_object() [4/5]

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

Definition at line 342 of file der_enc.cpp.

342 {
343 const uint8_t* rep = cast_char_ptr_to_uint8(rep_str.data());
344 const size_t rep_len = rep_str.size();
345 return add_object(type_tag, class_tag, rep, rep_len);
346}
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:275

References add_object(), and Botan::cast_char_ptr_to_uint8().

◆ add_object() [5/5]

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

Definition at line 351 of file der_enc.cpp.

351 {
352 return add_object(type_tag, class_tag, &rep, 1);
353}

References add_object().

◆ encode() [1/11]

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

Definition at line 250 of file der_enc.cpp.

250 {
252}
DER_Encoder & encode(bool b)
Definition der_enc.cpp:250

References Botan::Boolean, encode(), 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_PublicKey::DER_encode(), Botan::DL_PrivateKey::DER_encode(), Botan::TLS::Session::DER_encode(), Botan::DL_Group::DER_encode(), Botan::EC_Group::DER_encode(), encode(), encode(), encode(), encode(), encode(), Botan::AlgorithmIdentifier::encode_into(), Botan::X509_DN::encode_into(), Botan::AlternativeName::encode_into(), Botan::Extensions::encode_into(), Botan::CRL_Entry::encode_into(), Botan::PSS_Params::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::Attribute::encode_into(), Botan::X509_Object::encode_into(), Botan::X509_Object::make_signed(), Botan::EC_PrivateKey::private_key_bits(), Botan::Curve25519_PrivateKey::private_key_bits(), Botan::Ed448_PrivateKey::private_key_bits(), Botan::X448_PrivateKey::private_key_bits(), Botan::Ed25519_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::private_key_bits(), Botan::XMSS_PrivateKey::private_key_bits(), Botan::Private_Key::private_key_info(), Botan::TPM_PrivateKey::public_key_bits(), Botan::GOST_3410_PublicKey::public_key_bits(), Botan::McEliece_PublicKey::public_key_bits(), Botan::RSA_PublicKey::public_key_bits(), Botan::XMSS_PublicKey::public_key_bits(), Botan::PSS_Params::serialize(), Botan::TLS::Certificate_Request_12::serialize(), 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 )

Definition at line 278 of file der_enc.cpp.

278 {
279 uint8_t val = is_true ? 0xFF : 0x00;
280 return add_object(type_tag, class_tag, &val, 1);
281}

References add_object().

◆ encode() [3/11]

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

Definition at line 334 of file der_enc.cpp.

334 {
335 obj.encode_into(*this);
336 return (*this);
337}

References Botan::ASN1_Object::encode_into().

◆ encode() [4/11]

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

Definition at line 264 of file der_enc.cpp.

References 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 )

Definition at line 293 of file der_enc.cpp.

293 {
294 if(n == 0) {
295 return add_object(type_tag, class_tag, 0);
296 }
297
298 const size_t extra_zero = (n.bits() % 8 == 0) ? 1 : 0;
299 secure_vector<uint8_t> contents(extra_zero + n.bytes());
300 n.binary_encode(&contents[extra_zero]);
301 if(n < 0) {
302 for(unsigned char& content : contents) {
303 content = ~content;
304 }
305 for(size_t i = contents.size(); i > 0; --i) {
306 if(++contents[i - 1]) {
307 break;
308 }
309 }
310 }
311
312 return add_object(type_tag, class_tag, contents);
313}

References add_object(), Botan::BigInt::binary_encode(), Botan::BigInt::bits(), and Botan::BigInt::bytes().

◆ encode() [6/11]

template<typename Alloc >
DER_Encoder & Botan::DER_Encoder::encode ( const std::vector< uint8_t, Alloc > & bytes,
ASN1_Type real_type,
ASN1_Type type_tag,
ASN1_Class class_tag )
inline

Definition at line 116 of file der_enc.h.

119 {
120 return encode(bytes.data(), bytes.size(), real_type, type_tag, class_tag);
121 }

◆ encode() [7/11]

template<typename Alloc >
DER_Encoder & Botan::DER_Encoder::encode ( const std::vector< uint8_t, Alloc > & vec,
ASN1_Type real_type )
inline

Definition at line 99 of file der_enc.h.

99 {
100 return encode(vec.data(), vec.size(), real_type);
101 }

◆ encode() [8/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 )

Definition at line 318 of file der_enc.cpp.

319 {
320 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
321 throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
322 }
323
324 if(real_type == ASN1_Type::BitString) {
325 secure_vector<uint8_t> encoded;
326 encoded.push_back(0);
327 encoded += std::make_pair(bytes, length);
328 return add_object(type_tag, class_tag, encoded);
329 } else {
330 return add_object(type_tag, class_tag, bytes, length);
331 }
332}

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

◆ encode() [9/11]

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

Definition at line 271 of file der_enc.cpp.

271 {
272 return encode(bytes, length, real_type, real_type, ASN1_Class::Universal);
273}

References encode(), and Botan::Universal.

◆ encode() [10/11]

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

Definition at line 257 of file der_enc.cpp.

257 {
259}
static BigInt from_u64(uint64_t n)
Definition bigint.cpp:28

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

◆ encode() [11/11]

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

Definition at line 286 of file der_enc.cpp.

286 {
287 return encode(BigInt::from_u64(n), type_tag, class_tag);
288}

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

◆ encode_if() [1/2]

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

Definition at line 154 of file der_enc.h.

154 {
155 if(pred) {
156 encode(obj);
157 }
158 return (*this);
159 }

◆ encode_if() [2/2]

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

Definition at line 147 of file der_enc.h.

147 {
148 if(pred) {
149 return raw_bytes(enc.get_contents());
150 }
151 return (*this);
152 }
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition der_enc.cpp:207

References get_contents().

◆ encode_list()

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

Definition at line 132 of file der_enc.h.

132 {
133 for(size_t i = 0; i != values.size(); ++i) {
134 encode(values[i]);
135 }
136 return (*this);
137 }

Referenced by Botan::TLS::Session::DER_encode().

◆ encode_null()

DER_Encoder & Botan::DER_Encoder::encode_null ( )

Definition at line 243 of file der_enc.cpp.

243 {
245}

References add_object(), Botan::Null, and Botan::Universal.

Referenced by Botan::EC_Group::DER_encode().

◆ encode_optional()

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

Definition at line 124 of file der_enc.h.

124 {
125 if(value != default_value) {
126 encode(value);
127 }
128 return (*this);
129 }

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

◆ end_cons()

DER_Encoder & Botan::DER_Encoder::end_cons ( )

Definition at line 171 of file der_enc.cpp.

171 {
172 if(m_subsequences.empty()) {
173 throw Invalid_State("DER_Encoder::end_cons: No such sequence");
174 }
175
176 DER_Sequence last_seq = std::move(m_subsequences[m_subsequences.size() - 1]);
177 m_subsequences.pop_back();
178 last_seq.push_contents(*this);
179
180 return (*this);
181}

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::TLS::Session::DER_encode(), Botan::DL_Group::DER_encode(), Botan::EC_Group::DER_encode(), Botan::AlgorithmIdentifier::encode_into(), Botan::X509_DN::encode_into(), Botan::AlternativeName::encode_into(), Botan::Extensions::encode_into(), Botan::CRL_Entry::encode_into(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::encode_into(), Botan::PSS_Params::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::Attribute::encode_into(), Botan::X509_Object::encode_into(), end_explicit(), Botan::X509_Object::make_signed(), 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::TPM_PrivateKey::public_key_bits(), Botan::McEliece_PublicKey::public_key_bits(), Botan::RSA_PublicKey::public_key_bits(), Botan::ASN1::put_in_sequence(), and Botan::Public_Key::subject_public_key().

◆ end_explicit()

DER_Encoder & Botan::DER_Encoder::end_explicit ( )

Definition at line 200 of file der_enc.cpp.

200 {
201 return end_cons();
202}
DER_Encoder & end_cons()
Definition der_enc.cpp:171

References end_cons().

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

◆ get_contents()

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

Definition at line 132 of file der_enc.cpp.

132 {
133 if(!m_subsequences.empty()) {
134 throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
135 }
136
137 if(m_append_output) {
138 throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
139 }
140
141 secure_vector<uint8_t> output;
142 std::swap(output, m_default_outbuf);
143 return output;
144}

Referenced by Botan::PKCS10_Request::create(), Botan::DL_PrivateKey::DER_encode(), Botan::TLS::Session::DER_encode(), encode_if(), Botan::EC_PrivateKey::private_key_bits(), Botan::Curve25519_PrivateKey::private_key_bits(), Botan::Ed448_PrivateKey::private_key_bits(), Botan::X448_PrivateKey::private_key_bits(), Botan::Ed25519_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::RSA_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 contructor of DER_Encoder where the output will be placed. This avoids several unecessary copies.

Definition at line 146 of file der_enc.cpp.

146 {
147 if(!m_subsequences.empty()) {
148 throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
149 }
150
151 if(m_append_output) {
152 throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
153 }
154
155 std::vector<uint8_t> output(m_default_outbuf.begin(), m_default_outbuf.end());
156 m_default_outbuf.clear();
157 return output;
158}

◆ raw_bytes() [1/2]

template<typename Alloc >
DER_Encoder & Botan::DER_Encoder::raw_bytes ( const std::vector< uint8_t, Alloc > & val)
inline

Definition at line 88 of file der_enc.h.

88 {
89 return raw_bytes(val.data(), val.size());
90 }

◆ raw_bytes() [2/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 207 of file der_enc.cpp.

207 {
208 if(!m_subsequences.empty()) {
209 m_subsequences[m_subsequences.size() - 1].add_bytes(bytes, length);
210 } else if(m_append_output) {
211 m_append_output(bytes, length);
212 } else {
213 m_default_outbuf += std::make_pair(bytes, length);
214 }
215
216 return (*this);
217}

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

◆ start_cons()

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

Definition at line 163 of file der_enc.cpp.

163 {
164 m_subsequences.push_back(DER_Sequence(type_tag, class_tag));
165 return (*this);
166}

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

◆ start_context_specific()

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

Definition at line 69 of file der_enc.h.

69 {
71 }
DER_Encoder & start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition der_enc.cpp:163
ASN1_Type
Definition asn1_obj.h:43

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

◆ start_explicit()

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

Definition at line 186 of file der_enc.cpp.

186 {
187 ASN1_Type type_tag = static_cast<ASN1_Type>(type_no);
188
189 // This would confuse DER_Sequence
190 if(type_tag == ASN1_Type::Set) {
191 throw Internal_Error("DER_Encoder.start_explicit(SET) not supported");
192 }
193
194 return start_cons(type_tag, ASN1_Class::ContextSpecific);
195}

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

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

◆ start_explicit_context_specific()

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

Definition at line 73 of file der_enc.h.

Referenced by Botan::EC_PrivateKey::private_key_bits().

◆ start_sequence()

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

◆ start_set()

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

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