8#include <botan/der_enc.h>
10#include <botan/asn1_obj.h>
11#include <botan/bigint.h>
12#include <botan/internal/bit_ops.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/loadstor.h>
24void encode_tag(std::vector<uint8_t>& encoded_tag,
ASN1_Type type_tag_e,
ASN1_Class class_tag_e) {
25 const uint32_t type_tag =
static_cast<uint32_t
>(type_tag_e);
26 const uint32_t class_tag =
static_cast<uint32_t
>(class_tag_e);
28 if((class_tag | 0xE0) != 0xE0) {
29 throw Encoding_Error(
fmt(
"DER_Encoder: Invalid class tag {}", std::to_string(class_tag)));
33 encoded_tag.push_back(
static_cast<uint8_t
>(type_tag | class_tag));
35 size_t blocks =
high_bit(
static_cast<uint32_t
>(type_tag)) + 6;
36 blocks = (blocks - (blocks % 7)) / 7;
40 encoded_tag.push_back(
static_cast<uint8_t
>(class_tag | 0x1F));
41 for(
size_t i = 0; i != blocks - 1; ++i) {
42 encoded_tag.push_back(0x80 | ((type_tag >> 7 * (blocks - i - 1)) & 0x7F));
44 encoded_tag.push_back(type_tag & 0x7F);
51void encode_length(std::vector<uint8_t>& encoded_length,
size_t length) {
53 encoded_length.push_back(
static_cast<uint8_t
>(length));
57 encoded_length.push_back(
static_cast<uint8_t
>(0x80 | bytes_needed));
59 for(
size_t i =
sizeof(length) - bytes_needed; i <
sizeof(length); ++i) {
68 m_append_output = [&vec](
const uint8_t
b[],
size_t l) { vec.insert(vec.end(),
b,
b + l); };
72 m_append_output = [&vec](
const uint8_t
b[],
size_t l) { vec.insert(vec.end(),
b,
b + l); };
78void DER_Encoder::DER_Sequence::push_contents(
DER_Encoder& der) {
82 std::sort(m_set_contents.begin(), m_set_contents.end());
83 for(
const auto& set_elem : m_set_contents) {
84 m_contents += set_elem;
86 m_set_contents.clear();
89 der.
add_object(m_type_tag, real_class_tag, m_contents.data(), m_contents.size());
96void DER_Encoder::DER_Sequence::add_bytes(
const uint8_t data[],
size_t length) {
100 m_contents += std::make_pair(data, length);
104void DER_Encoder::DER_Sequence::add_bytes(
const uint8_t hdr[],
size_t hdr_len,
const uint8_t val[],
size_t val_len) {
107 m.reserve(hdr_len + val_len);
108 m += std::make_pair(hdr, hdr_len);
109 m += std::make_pair(val, val_len);
110 m_set_contents.push_back(std::move(m));
112 m_contents += std::make_pair(hdr, hdr_len);
113 m_contents += std::make_pair(val, val_len);
120uint32_t DER_Encoder::DER_Sequence::tag_of()
const {
121 return m_type_tag | m_class_tag;
127DER_Encoder::DER_Sequence::DER_Sequence(
ASN1_Type t1,
ASN1_Class t2) : m_type_tag(t1), m_class_tag(t2) {}
133 if(!m_subsequences.empty()) {
134 throw Invalid_State(
"DER_Encoder: Sequence hasn't been marked done");
137 if(m_append_output) {
138 throw Invalid_State(
"DER_Encoder Cannot get contents when using output vector");
142 std::swap(output, m_default_outbuf);
147 if(!m_subsequences.empty()) {
148 throw Invalid_State(
"DER_Encoder: Sequence hasn't been marked done");
151 if(m_append_output) {
152 throw Invalid_State(
"DER_Encoder Cannot get contents when using output vector");
155 std::vector<uint8_t> output(m_default_outbuf.begin(), m_default_outbuf.end());
156 m_default_outbuf.clear();
164 m_subsequences.push_back(DER_Sequence(type_tag, class_tag));
172 if(m_subsequences.empty()) {
173 throw Invalid_State(
"DER_Encoder::end_cons: No such sequence");
176 DER_Sequence last_seq = std::move(m_subsequences[m_subsequences.size() - 1]);
177 m_subsequences.pop_back();
178 last_seq.push_contents(*
this);
191 throw Internal_Error(
"DER_Encoder.start_explicit(SET) not supported");
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);
213 m_default_outbuf += std::make_pair(bytes, length);
223 std::vector<uint8_t> hdr;
224 encode_tag(hdr, type_tag, class_tag);
225 encode_length(hdr, length);
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);
233 m_default_outbuf += hdr;
234 m_default_outbuf += std::make_pair(rep, length);
279 uint8_t val = is_true ? 0xFF : 0x00;
280 return add_object(type_tag, class_tag, &val, 1);
298 const size_t extra_zero = (n.
bits() % 8 == 0) ? 1 : 0;
302 for(
unsigned char& content : contents) {
305 for(
size_t i = contents.size(); i > 0; --i) {
306 if(++contents[i - 1]) {
312 return add_object(type_tag, class_tag, contents);
326 encoded.push_back(0);
327 encoded += std::make_pair(bytes, length);
328 return add_object(type_tag, class_tag, encoded);
330 return add_object(type_tag, class_tag, bytes, length);
344 const size_t rep_len = rep_str.size();
345 return add_object(type_tag, class_tag, rep, rep_len);
352 return add_object(type_tag, class_tag, &rep, 1);
#define BOTAN_ASSERT_NOMSG(expr)
virtual void encode_into(DER_Encoder &to) const =0
static BigInt from_u64(uint64_t n)
T serialize(size_t len) const
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
secure_vector< uint8_t > get_contents()
DER_Encoder & end_explicit()
DER_Encoder & start_explicit(uint16_t type_tag)
DER_Encoder & start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
DER_Encoder & encode_null()
std::vector< uint8_t > get_contents_unlocked()
DER_Encoder & encode(bool b)
std::string fmt(std::string_view format, const T &... args)
constexpr size_t high_bit(T n)
std::vector< T, secure_allocator< T > > secure_vector
constexpr uint8_t get_byte_var(size_t byte_num, T input)
constexpr size_t significant_bytes(T n)
const uint8_t * cast_char_ptr_to_uint8(const char *s)