Botan 1.10.17

Classes | Public Member Functions | List of all members
Botan::DER_Encoder Class Reference

`#include <der_enc.h>`

## Public Member Functions
DER_Encoder & | add_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
DER_Encoder & | add_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const MemoryRegion< byte > &rep)
DER_Encoder & | add_object (ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string &str)
DER_Encoder & | add_object (ASN1_Tag type_tag, ASN1_Tag class_tag, byte val)
DER_Encoder & | encode (bool b)
DER_Encoder & | encode (size_t s)
DER_Encoder & | encode (const BigInt &n)
DER_Encoder & | encode (const MemoryRegion< byte > &v, ASN1_Tag real_type)
DER_Encoder & | encode (const byte val[], size_t len, ASN1_Tag real_type)
DER_Encoder & | encode (bool b, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
DER_Encoder & | encode (size_t s, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
DER_Encoder & | encode (const BigInt &n, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
DER_Encoder & | encode (const MemoryRegion< byte > &v, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
DER_Encoder & | encode (const byte v[], size_t len, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
DER_Encoder & | encode (const ASN1_Object &obj)
DER_Encoder & | encode_if (bool pred, DER_Encoder &enc)
template<typename T >
DER_Encoder & | encode_list (const std::vector< T > &values)
DER_Encoder & | encode_null ()
template<typename T >
DER_Encoder & | encode_optional (const T &value, const T &default_value)
DER_Encoder & | end_cons ()
DER_Encoder & | end_explicit ()
SecureVector< byte > | get_contents ()
DER_Encoder & | raw_bytes (const byte val[], size_t len)
DER_Encoder & | raw_bytes (const MemoryRegion< byte > &val)
DER_Encoder & | start_cons (ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
DER_Encoder & | start_explicit (u16bit type_tag)

## Detailed Description

General DER Encoding Object

Definition at line 22 of file der_enc.h.

## Member Function Documentation

## ◆ add_object() [1/4]

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

Definition at line 346 of file der_enc.cpp.

References raw_bytes().

Referenced by add_object(), encode(), Botan::ASN1_String::encode_into(), Botan::OID::encode_into(), Botan::EAC_Time::encode_into(), Botan::X509_Time::encode_into(), Botan::ASN1_EAC_String::encode_into(), and encode_null().

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

## ◆ add_object() [2/4]

DER_Encoder & Botan::DER_Encoder::add_object | ( | ASN1_Tag | type_tag,
|  | ASN1_Tag | class_tag,
|  | const MemoryRegion< byte > & | rep
| ) |  |

Definition at line 360 of file der_enc.cpp.

References add_object(), and Botan::MemoryRegion< T >::size().

362 {
363 const byte* rep = &rep_buf[0];
364 const size_t rep_len = rep_buf.size();
365 return add_object(type_tag, class_tag, rep, rep_len);
366 }
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ add_object() [3/4]

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

Definition at line 371 of file der_enc.cpp.

References add_object().

373 {
374 const byte* rep = reinterpret_cast<const byte*>(rep_str.data());
375 const size_t rep_len = rep_str.size();
376 return add_object(type_tag, class_tag, rep, rep_len);
377 }
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ add_object() [4/4]

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

Definition at line 382 of file der_enc.cpp.

References add_object().

384 {
385 return add_object(type_tag, class_tag, &rep, 1);
386 }
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ encode() [1/11]

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

Definition at line 209 of file der_enc.cpp.

References Botan::BOOLEAN, and Botan::UNIVERSAL.

Referenced by Botan::X509::BER_encode(), Botan::X509_Object::BER_encode(), Botan::PKCS8::BER_encode(), Botan::ECDSA_Signature::DER_encode(), Botan::EC_Group::DER_encode(), Botan::DL_Group::DER_encode(), Botan::CMS_Encoder::digest(), encode(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::encode(), Botan::CRL_Entry::encode_into(), Botan::AlgorithmIdentifier::encode_into(), Botan::Attribute::encode_into(), Botan::Extensions::encode_into(), Botan::AlternativeName::encode_into(), Botan::IF_Scheme_PrivateKey::pkcs8_private_key(), Botan::DL_Scheme_PrivateKey::pkcs8_private_key(), Botan::EC_PrivateKey::pkcs8_private_key(), Botan::DL_Scheme_PublicKey::x509_subject_public_key(), and Botan::IF_Scheme_PublicKey::x509_subject_public_key().

210 {
211 return encode(is_true, BOOLEAN, UNIVERSAL);
212 }
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::BOOLEAN
Definition: asn1_int.h:28

## ◆ encode() [2/11]

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

Definition at line 217 of file der_enc.cpp.

218 {
219 return encode(BigInt(n), INTEGER, UNIVERSAL);
220 }
Botan::INTEGER
Definition: asn1_int.h:29
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode() [3/11]

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

Definition at line 225 of file der_enc.cpp.

226 {
227 return encode(n, INTEGER, UNIVERSAL);
228 }
Botan::INTEGER
Definition: asn1_int.h:29
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode() [4/11]

DER_Encoder & Botan::DER_Encoder::encode | ( | const MemoryRegion< byte > & | v,
|  | ASN1_Tag | real_type
| ) |  |

Definition at line 233 of file der_enc.cpp.

References encode(), Botan::MemoryRegion< T >::size(), and Botan::UNIVERSAL.

235 {
236 return encode(&bytes[0], bytes.size(),
237 real_type, real_type, UNIVERSAL);
238 }
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode() [5/11]

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

Definition at line 243 of file der_enc.cpp.

References encode(), and Botan::UNIVERSAL.

245 {
246 return encode(bytes, length, real_type, real_type, UNIVERSAL);
247 }
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode() [6/11]

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

Definition at line 252 of file der_enc.cpp.

References add_object().

254 {
255 byte val = is_true ? 0xFF : 0x00;
256 return add_object(type_tag, class_tag, &val, 1);
257 }
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ encode() [7/11]

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

Definition at line 262 of file der_enc.cpp.

264 {
265 return encode(BigInt(n), type_tag, class_tag);
266 }
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode() [8/11]

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

Definition at line 271 of file der_enc.cpp.

273 {
274 if(n == 0)
275 return add_object(type_tag, class_tag, 0);
276
277 bool extra_zero = (n.bits() % 8 == 0);
278 SecureVector<byte> contents(extra_zero + n.bytes());
279 BigInt::encode(&contents[extra_zero], n);
280 if(n < 0)
281 {
282 for(size_t i = 0; i != contents.size(); ++i)
283 contents[i] = ~contents[i];
284 for(size_t i = contents.size(); i > 0; --i)
285 if(++contents[i-1])
286 break;
287 }
288
289 return add_object(type_tag, class_tag, contents);
290 }
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ encode() [9/11]

DER_Encoder & Botan::DER_Encoder::encode | ( | const MemoryRegion< byte > & | v,
|  | ASN1_Tag | real_type,
|  | ASN1_Tag | type_tag,
|  | ASN1_Tag | class_tag = `CONTEXT_SPECIFIC`
| ) |  |

Definition at line 295 of file der_enc.cpp.

References encode(), and Botan::MemoryRegion< T >::size().

298 {
299 return encode(&bytes[0], bytes.size(),
300 real_type, type_tag, class_tag);
301 }
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode() [10/11]

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

Definition at line 306 of file der_enc.cpp.

References add_object(), Botan::BIT_STRING, Botan::OCTET_STRING, and Botan::MemoryRegion< T >::push_back().

309 {
310 if(real_type != OCTET_STRING && real_type != BIT_STRING)
311 throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
312
313 if(real_type == BIT_STRING)
314 {
315 SecureVector<byte> encoded;
316 encoded.push_back(0);
317 encoded += std::make_pair(bytes, length);
318 return add_object(type_tag, class_tag, encoded);
319 }
320 else
321 return add_object(type_tag, class_tag, bytes, length);
322 }
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::OCTET_STRING
Definition: asn1_int.h:31
Botan::BIT_STRING
Definition: asn1_int.h:30
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ encode() [11/11]

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

Definition at line 337 of file der_enc.cpp.

References Botan::ASN1_Object::encode_into().

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

## ◆ encode_if()

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

Definition at line 327 of file der_enc.cpp.

References get_contents(), and raw_bytes().

328 {
329 if(cond)
330 return raw_bytes(codec.get_contents());
331 return (*this);
332 }
Botan::DER_Encoder::raw_bytes
DER_Encoder & raw_bytes(const byte val[], size_t len)
Definition: der_enc.cpp:188

## ◆ encode_list()

template<typename T >

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

Definition at line 75 of file der_enc.h.

References Botan::PEM_Code::encode().

76 {
77 for(size_t i = 0; i != values.size(); ++i)
78 encode(values[i]);
79 return (*this);
80 }
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ encode_null()

DER_Encoder & Botan::DER_Encoder::encode_null | ( |  | ) |

Definition at line 201 of file der_enc.cpp.

References add_object(), Botan::NULL_TAG, and Botan::UNIVERSAL.

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

202 {
203 return add_object(NULL_TAG, UNIVERSAL, 0, 0);
204 }
Botan::UNIVERSAL
Definition: asn1_int.h:20
Botan::NULL_TAG
Definition: asn1_int.h:32
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)
Definition: der_enc.cpp:346

## ◆ encode_optional()

template<typename T >

DER_Encoder& Botan::DER_Encoder::encode_optional | ( | const T & | value,
|  | const T & | default_value
| ) |  |
| inline

Definition at line 67 of file der_enc.h.

References Botan::PEM_Code::encode().

68 {
69 if(value != default_value)
70 encode(value);
71 return (*this);
72 }
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209

## ◆ end_cons()

DER_Encoder & Botan::DER_Encoder::end_cons | ( |  | ) |

Definition at line 145 of file der_enc.cpp.

References raw_bytes().

Referenced by Botan::X509_Object::BER_encode(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::build_cert_body(), Botan::ECDSA_Signature::DER_encode(), Botan::EC_Group::DER_encode(), Botan::DL_Group::DER_encode(), Botan::CMS_Encoder::digest(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::encode(), Botan::CRL_Entry::encode_into(), Botan::X509_DN::encode_into(), Botan::AlgorithmIdentifier::encode_into(), Botan::Attribute::encode_into(), Botan::AlternativeName::encode_into(), end_explicit(), Botan::IF_Scheme_PrivateKey::pkcs8_private_key(), Botan::EC_PrivateKey::pkcs8_private_key(), Botan::ASN1::put_in_sequence(), and Botan::IF_Scheme_PublicKey::x509_subject_public_key().

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

## ◆ end_explicit()

DER_Encoder & Botan::DER_Encoder::end_explicit | ( |  | ) |

Definition at line 172 of file der_enc.cpp.

References end_cons().

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

173 {
174 return end_cons();
175 }
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:145

## ◆ get_contents()

SecureVector< byte > Botan::DER_Encoder::get_contents | ( |  | ) |

Definition at line 122 of file der_enc.cpp.

References std::swap().

Referenced by Botan::X509_Object::BER_encode(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::build_cert_body(), Botan::ECDSA_Signature::DER_encode(), Botan::EC_Group::DER_encode(), Botan::DL_Group::DER_encode(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::encode(), encode_if(), Botan::CMS_Encoder::get_contents(), Botan::IF_Scheme_PrivateKey::pkcs8_private_key(), Botan::DL_Scheme_PrivateKey::pkcs8_private_key(), Botan::EC_PrivateKey::pkcs8_private_key(), Botan::ASN1::put_in_sequence(), Botan::DL_Scheme_PublicKey::x509_subject_public_key(), and Botan::IF_Scheme_PublicKey::x509_subject_public_key().

123 {
124 if(subsequences.size() != 0)
125 throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
126
127 SecureVector<byte> output;
128 std::swap(output, contents);
129 return output;
130 }
std::swap
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
Definition: secmem.h:438

## ◆ raw_bytes() [1/2]

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

Definition at line 188 of file der_enc.cpp.

Referenced by add_object(), Botan::X509_Object::BER_encode(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::build_cert_body(), Botan::CMS_Encoder::digest(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::encode(), encode_if(), Botan::X509_DN::encode_into(), Botan::AlgorithmIdentifier::encode_into(), Botan::Attribute::encode_into(), end_cons(), Botan::ASN1::put_in_sequence(), and raw_bytes().

189 {
190 if(subsequences.size())
191 subsequences[subsequences.size()-1].add_bytes(bytes, length);
192 else
193 contents += std::make_pair(bytes, length);
194
195 return (*this);
196 }

## ◆ raw_bytes() [2/2]

DER_Encoder & Botan::DER_Encoder::raw_bytes | ( | const MemoryRegion< byte > & | val | ) |

Definition at line 180 of file der_enc.cpp.

References raw_bytes(), and Botan::MemoryRegion< T >::size().

181 {
182 return raw_bytes(&val[0], val.size());
183 }
Botan::DER_Encoder::raw_bytes
DER_Encoder & raw_bytes(const byte val[], size_t len)
Definition: der_enc.cpp:188
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29

## ◆ start_cons()

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

Definition at line 135 of file der_enc.cpp.

Referenced by Botan::X509::BER_encode(), Botan::X509_Object::BER_encode(), Botan::PKCS8::BER_encode(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::build_cert_body(), Botan::CMS_Encoder::compress(), Botan::ECDSA_Signature::DER_encode(), Botan::EC_Group::DER_encode(), Botan::DL_Group::DER_encode(), Botan::CMS_Encoder::digest(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::encode(), Botan::CRL_Entry::encode_into(), Botan::X509_DN::encode_into(), Botan::AlgorithmIdentifier::encode_into(), Botan::Attribute::encode_into(), Botan::Extensions::encode_into(), Botan::AlternativeName::encode_into(), Botan::CMS_Encoder::get_contents(), Botan::IF_Scheme_PrivateKey::pkcs8_private_key(), Botan::EC_PrivateKey::pkcs8_private_key(), Botan::ASN1::put_in_sequence(), start_explicit(), and Botan::IF_Scheme_PublicKey::x509_subject_public_key().

137 {
138 subsequences.push_back(DER_Sequence(type_tag, class_tag));
139 return (*this);
140 }

## ◆ start_explicit()

DER_Encoder & Botan::DER_Encoder::start_explicit | ( | u16bit | type_tag | ) |

Definition at line 159 of file der_enc.cpp.

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

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

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

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

- src/asn1/der_enc.h
- src/asn1/der_enc.cpp