Botan  2.4.0
Crypto and TLS for C++11
Classes | Public Member Functions | List of all members
Botan::DER_Encoder Class Referencefinal

#include <der_enc.h>

Public Member Functions

DER_Encoderadd_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
 
DER_Encoderadd_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const std::vector< uint8_t > &rep)
 
DER_Encoderadd_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const secure_vector< uint8_t > &rep)
 
DER_Encoderadd_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string &str)
 
DER_Encoderadd_object (ASN1_Tag type_tag, ASN1_Tag class_tag, uint8_t val)
 
DER_Encoderencode (bool b)
 
DER_Encoderencode (size_t s)
 
DER_Encoderencode (const BigInt &n)
 
DER_Encoderencode (const uint8_t val[], size_t len, ASN1_Tag real_type)
 
template<typename Alloc >
DER_Encoderencode (const std::vector< uint8_t, Alloc > &vec, ASN1_Tag real_type)
 
DER_Encoderencode (bool b, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
DER_Encoderencode (size_t s, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
DER_Encoderencode (const BigInt &n, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
DER_Encoderencode (const std::vector< uint8_t > &v, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
DER_Encoderencode (const secure_vector< uint8_t > &v, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
DER_Encoderencode (const uint8_t v[], size_t len, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
 
DER_Encoderencode (const ASN1_Object &obj)
 
DER_Encoderencode_if (bool pred, DER_Encoder &enc)
 
DER_Encoderencode_if (bool pred, const ASN1_Object &obj)
 
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 ()
 
DER_Encoderraw_bytes (const uint8_t val[], size_t len)
 
template<typename Alloc >
DER_Encoderraw_bytes (const std::vector< uint8_t, Alloc > &val)
 
DER_Encoderstart_cons (ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
 
DER_Encoderstart_explicit (uint16_t type_tag)
 

Detailed Description

General DER Encoding Object

Definition at line 22 of file der_enc.h.

Member Function Documentation

◆ add_object() [1/5]

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

Definition at line 347 of file der_enc.cpp.

References Botan::BER_Decoder::raw_bytes().

Referenced by Botan::ASN1_String::encode_into(), Botan::X509_Time::encode_into(), Botan::OID::encode_into(), Botan::AlternativeName::has_items(), Botan::ASN1_Formatter::print_to_stream(), and Botan::Cert_Extension::Certificate_Policies::validate().

349  {
350  secure_vector<uint8_t> buffer;
351  buffer += encode_tag(type_tag, class_tag);
352  buffer += encode_length(length);
353  buffer += std::make_pair(rep, length);
354 
355  return raw_bytes(buffer);
356  }
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:181

◆ add_object() [2/5]

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

Definition at line 110 of file der_enc.h.

112  {
113  return add_object(type_tag, class_tag, rep.data(), rep.size());
114  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347

◆ add_object() [3/5]

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

Definition at line 116 of file der_enc.h.

118  {
119  return add_object(type_tag, class_tag, rep.data(), rep.size());
120  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347

◆ add_object() [4/5]

DER_Encoder & Botan::DER_Encoder::add_object ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag,
const std::string &  str 
)

Definition at line 361 of file der_enc.cpp.

References Botan::cast_char_ptr_to_uint8().

363  {
364  const uint8_t* rep = cast_char_ptr_to_uint8(rep_str.data());
365  const size_t rep_len = rep_str.size();
366  return add_object(type_tag, class_tag, rep, rep_len);
367  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:120

◆ add_object() [5/5]

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

Definition at line 372 of file der_enc.cpp.

374  {
375  return add_object(type_tag, class_tag, &rep, 1);
376  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347

◆ encode() [1/12]

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

Definition at line 202 of file der_enc.cpp.

References Botan::BOOLEAN, Botan::PEM_Code::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::TLS::Certificate_Req::Certificate_Req(), Botan::PSSR::config_for_x509(), Botan::Cert_Extension::CRL_Number::copy(), Botan::X509::create_cert_req(), Botan::EC_Group::DER_encode(), Botan::TLS::Session::DER_encode(), Botan::DL_Group::DER_encode(), Botan::Attribute::encode_into(), Botan::AlternativeName::encode_into(), Botan::AlgorithmIdentifier::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::CRL_Entry::encode_into(), Botan::X509_Object::encode_into(), Botan::Extensions::encode_into(), Botan::AlternativeName::has_items(), Botan::X509_Object::make_signed(), Botan::operator<(), Botan::Curve25519_PrivateKey::private_key_bits(), Botan::Ed25519_PrivateKey::private_key_bits(), Botan::DL_Scheme_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::EC_PrivateKey::private_key_bits(), Botan::RSA_PrivateKey::private_key_bits(), Botan::Private_Key::private_key_info(), Botan::DL_Scheme_PublicKey::public_key_bits(), Botan::RSA_PublicKey::public_key_bits(), Botan::McEliece_PublicKey::public_key_bits(), Botan::GOST_3410_PublicKey::public_key_bits(), Botan::TPM_PrivateKey::public_key_bits(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), Botan::Public_Key::subject_public_key(), Botan::X509_CA::update_crl(), and Botan::Cert_Extension::Certificate_Policies::validate().

203  {
204  return encode(is_true, BOOLEAN, UNIVERSAL);
205  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [2/12]

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

Definition at line 210 of file der_enc.cpp.

References Botan::PEM_Code::encode(), Botan::INTEGER, and Botan::UNIVERSAL.

211  {
212  return encode(BigInt(n), INTEGER, UNIVERSAL);
213  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [3/12]

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

Definition at line 218 of file der_enc.cpp.

References Botan::PEM_Code::encode(), Botan::INTEGER, and Botan::UNIVERSAL.

219  {
220  return encode(n, INTEGER, UNIVERSAL);
221  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [4/12]

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

Definition at line 226 of file der_enc.cpp.

References Botan::PEM_Code::encode(), and Botan::UNIVERSAL.

228  {
229  return encode(bytes, length, real_type, real_type, UNIVERSAL);
230  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [5/12]

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

Definition at line 55 of file der_enc.h.

References Botan::CONTEXT_SPECIFIC, and Botan::PEM_Code::encode().

56  {
57  return encode(vec.data(), vec.size(), real_type);
58  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [6/12]

DER_Encoder & Botan::DER_Encoder::encode ( bool  b,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 235 of file der_enc.cpp.

237  {
238  uint8_t val = is_true ? 0xFF : 0x00;
239  return add_object(type_tag, class_tag, &val, 1);
240  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347

◆ encode() [7/12]

DER_Encoder & Botan::DER_Encoder::encode ( size_t  s,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 245 of file der_enc.cpp.

References Botan::PEM_Code::encode().

247  {
248  return encode(BigInt(n), type_tag, class_tag);
249  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [8/12]

DER_Encoder & Botan::DER_Encoder::encode ( const BigInt n,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 254 of file der_enc.cpp.

References Botan::BigInt::bits(), Botan::BigInt::bytes(), and Botan::BigInt::encode().

256  {
257  if(n == 0)
258  return add_object(type_tag, class_tag, 0);
259 
260  const size_t extra_zero = (n.bits() % 8 == 0) ? 1 : 0;
261  secure_vector<uint8_t> contents(extra_zero + n.bytes());
262  BigInt::encode(&contents[extra_zero], n);
263  if(n < 0)
264  {
265  for(size_t i = 0; i != contents.size(); ++i)
266  contents[i] = ~contents[i];
267  for(size_t i = contents.size(); i > 0; --i)
268  if(++contents[i-1])
269  break;
270  }
271 
272  return add_object(type_tag, class_tag, contents);
273  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54

◆ encode() [9/12]

DER_Encoder & Botan::DER_Encoder::encode ( const std::vector< uint8_t > &  v,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 289 of file der_enc.cpp.

References Botan::PEM_Code::encode().

292  {
293  return encode(bytes.data(), bytes.size(),
294  real_type, type_tag, class_tag);
295  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [10/12]

DER_Encoder & Botan::DER_Encoder::encode ( const secure_vector< uint8_t > &  v,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 278 of file der_enc.cpp.

References Botan::PEM_Code::encode().

281  {
282  return encode(bytes.data(), bytes.size(),
283  real_type, type_tag, class_tag);
284  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode() [11/12]

DER_Encoder & Botan::DER_Encoder::encode ( const uint8_t  v[],
size_t  len,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 300 of file der_enc.cpp.

References Botan::BIT_STRING, and Botan::OCTET_STRING.

303  {
304  if(real_type != OCTET_STRING && real_type != BIT_STRING)
305  throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
306 
307  if(real_type == BIT_STRING)
308  {
309  secure_vector<uint8_t> encoded;
310  encoded.push_back(0);
311  encoded += std::make_pair(bytes, length);
312  return add_object(type_tag, class_tag, encoded);
313  }
314  else
315  return add_object(type_tag, class_tag, bytes, length);
316  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347

◆ encode() [12/12]

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

Definition at line 338 of file der_enc.cpp.

References Botan::ASN1_Object::encode_into().

339  {
340  obj.encode_into(*this);
341  return (*this);
342  }

◆ encode_if() [1/2]

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

Definition at line 321 of file der_enc.cpp.

References get_contents(), and Botan::BER_Decoder::raw_bytes().

Referenced by Botan::Cert_Extension::Basic_Constraints::get_path_limit().

322  {
323  if(cond)
324  return raw_bytes(codec.get_contents());
325  return (*this);
326  }
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:181

◆ encode_if() [2/2]

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

Definition at line 328 of file der_enc.cpp.

References Botan::PEM_Code::encode().

329  {
330  if(cond)
331  encode(obj);
332  return (*this);
333  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode_list()

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

Definition at line 96 of file der_enc.h.

References Botan::PEM_Code::encode().

Referenced by Botan::PK_Signer::signature(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), and Botan::X509_CA::update_crl().

97  {
98  for(size_t i = 0; i != values.size(); ++i)
99  encode(values[i]);
100  return (*this);
101  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ encode_null()

DER_Encoder & Botan::DER_Encoder::encode_null ( )

Definition at line 194 of file der_enc.cpp.

References Botan::NULL_TAG, and Botan::UNIVERSAL.

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

195  {
196  return add_object(NULL_TAG, UNIVERSAL, nullptr, 0);
197  }
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347

◆ encode_optional()

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

Definition at line 88 of file der_enc.h.

References Botan::PEM_Code::encode().

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

89  {
90  if(value != default_value)
91  encode(value);
92  return (*this);
93  }
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202

◆ end_cons()

DER_Encoder & Botan::DER_Encoder::end_cons ( )

Definition at line 146 of file der_enc.cpp.

References Botan::BER_Decoder::raw_bytes().

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::PSSR::config_for_x509(), Botan::X509::create_cert_req(), Botan::EC_Group::DER_encode(), Botan::TLS::Session::DER_encode(), Botan::DL_Group::DER_encode(), Botan::Attribute::encode_into(), Botan::AlternativeName::encode_into(), Botan::AlgorithmIdentifier::encode_into(), Botan::X509_DN::encode_into(), Botan::OCSP::CertID::encode_into(), Botan::CRL_Entry::encode_into(), Botan::X509_Object::encode_into(), Botan::Extensions::encode_into(), Botan::X509_Object::make_signed(), Botan::operator<(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::EC_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(), Botan::ASN1::put_in_sequence(), Botan::PK_Signer::signature(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), Botan::Public_Key::subject_public_key(), Botan::X509_CA::update_crl(), and Botan::Cert_Extension::Certificate_Policies::validate().

147  {
148  if(m_subsequences.empty())
149  throw Invalid_State("DER_Encoder::end_cons: No such sequence");
150 
151  secure_vector<uint8_t> seq = m_subsequences[m_subsequences.size()-1].get_contents();
152  m_subsequences.pop_back();
153  raw_bytes(seq);
154  return (*this);
155  }
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:181

◆ end_explicit()

DER_Encoder & Botan::DER_Encoder::end_explicit ( )

Definition at line 173 of file der_enc.cpp.

References Botan::BER_Decoder::end_cons().

Referenced by Botan::OCSP::Request::BER_encode(), Botan::X509::create_cert_req(), and Botan::AlternativeName::encode_into().

174  {
175  return end_cons();
176  }
DER_Encoder & end_cons()
Definition: der_enc.cpp:146

◆ get_contents()

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

Definition at line 123 of file der_enc.cpp.

Referenced by Botan::TLS::Certificate_Req::Certificate_Req(), Botan::X509::create_cert_req(), Botan::TLS::Session::DER_encode(), encode_if(), Botan::Curve25519_PrivateKey::private_key_bits(), Botan::Ed25519_PrivateKey::private_key_bits(), Botan::McEliece_PrivateKey::private_key_bits(), Botan::EC_PrivateKey::private_key_bits(), and Botan::Private_Key::private_key_info().

124  {
125  if(m_subsequences.size() != 0)
126  throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
127 
128  secure_vector<uint8_t> output;
129  std::swap(output, m_contents);
130  return output;
131  }

◆ get_contents_unlocked()

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

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

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

182  {
183  if(m_subsequences.size())
184  m_subsequences[m_subsequences.size()-1].add_bytes(bytes, length);
185  else
186  m_contents += std::make_pair(bytes, length);
187 
188  return (*this);
189  }

◆ raw_bytes() [2/2]

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

Definition at line 43 of file der_enc.h.

References Botan::PEM_Code::encode().

44  {
45  return raw_bytes(val.data(), val.size());
46  }
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:181

◆ start_cons()

DER_Encoder & Botan::DER_Encoder::start_cons ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag = UNIVERSAL 
)

◆ start_explicit()

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

Definition at line 160 of file der_enc.cpp.

References Botan::CONTEXT_SPECIFIC, Botan::SET, and Botan::BER_Decoder::start_cons().

Referenced by Botan::OCSP::Request::BER_encode(), Botan::X509::create_cert_req(), and Botan::AlternativeName::encode_into().

161  {
162  ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
163 
164  if(type_tag == SET)
165  throw Internal_Error("DER_Encoder.start_explicit(SET); cannot perform");
166 
167  return start_cons(type_tag, CONTEXT_SPECIFIC);
168  }
ASN1_Tag
Definition: asn1_obj.h:22
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136

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