8 #include <botan/der_enc.h>     9 #include <botan/asn1_int.h>    10 #include <botan/bigint.h>    11 #include <botan/get_byte.h>    12 #include <botan/parsing.h>    13 #include <botan/internal/bit_ops.h>    25    if((class_tag | 0xE0) != 0xE0)
    26       throw Encoding_Error(
"DER_Encoder: Invalid class tag " +
    29    SecureVector<byte> encoded_tag;
    31       encoded_tag.push_back(static_cast<byte>(type_tag | class_tag));
    34       size_t blocks = 
high_bit(type_tag) + 6;
    35       blocks = (blocks - (blocks % 7)) / 7;
    37       encoded_tag.push_back(class_tag | 0x1F);
    38       for(
size_t i = 0; i != blocks - 1; ++i)
    39          encoded_tag.push_back(0x80 | ((type_tag >> 7*(blocks-i-1)) & 0x7F));
    40       encoded_tag.push_back(type_tag & 0x7F);
    49 SecureVector<byte> encode_length(
size_t length)
    51    SecureVector<byte> encoded_length;
    53       encoded_length.push_back(static_cast<byte>(length));
    58       encoded_length.push_back(static_cast<byte>(0x80 | top_byte));
    60       for(
size_t i = 
sizeof(length) - top_byte; i != 
sizeof(length); ++i)
    61          encoded_length.push_back(
get_byte(i, length));
    63    return encoded_length;
    71 SecureVector<byte> DER_Encoder::DER_Sequence::get_contents()
    77       std::sort(set_contents.begin(), set_contents.end());
    78       for(
size_t i = 0; i != set_contents.size(); ++i)
    79          contents += set_contents[i];
    83    SecureVector<byte> result;
    84    result += encode_tag(type_tag, real_class_tag);
    85    result += encode_length(contents.size());
    95 void DER_Encoder::DER_Sequence::add_bytes(
const byte data[], 
size_t length)
    98       set_contents.push_back(SecureVector<byte>(data, length));
   100       contents += std::make_pair(data, length);
   106 ASN1_Tag DER_Encoder::DER_Sequence::tag_of()
 const   108    return ASN1_Tag(type_tag | class_tag);
   115    type_tag(t1), class_tag(t2)
   124    if(subsequences.size() != 0)
   125       throw Invalid_State(
"DER_Encoder: Sequence hasn't been marked done");
   138    subsequences.push_back(DER_Sequence(type_tag, class_tag));
   147    if(subsequences.empty())
   148       throw Invalid_State(
"DER_Encoder::end_cons: No such sequence");
   151    subsequences.pop_back();
   164       throw Internal_Error(
"DER_Encoder.start_explicit(SET); cannot perform");
   190    if(subsequences.size())
   191       subsequences[subsequences.size()-1].add_bytes(bytes, length);
   193       contents += std::make_pair(bytes, length);
   255    byte val = is_true ? 0xFF : 0x00;
   256    return add_object(type_tag, class_tag, &val, 1);
   275       return add_object(type_tag, class_tag, 0);
   277    bool extra_zero = (n.
bits() % 8 == 0);
   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)
   289    return add_object(type_tag, class_tag, contents);
   300                  real_type, type_tag, class_tag);
   317       encoded += std::make_pair(bytes, length);
   318       return add_object(type_tag, class_tag, encoded);
   321       return add_object(type_tag, class_tag, bytes, length);
   347                                      const byte rep[], 
size_t length)
   350    buffer += encode_tag(type_tag, class_tag);
   351    buffer += encode_length(length);
   352    buffer += std::make_pair(rep, length);
   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);
   372                                      const std::string& rep_str)
   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);
   385    return add_object(type_tag, class_tag, &rep, 1);
 
SecureVector< byte > get_contents()
DER_Encoder & raw_bytes(const byte val[], size_t len)
size_t significant_bytes(T n)
std::invalid_argument Invalid_Argument
DER_Encoder & end_explicit()
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
DER_Encoder & start_explicit(u16bit type_tag)
byte get_byte(size_t byte_num, T input)
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
DER_Encoder & encode(bool b)
DER_Encoder & encode_null()
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
BER_Decoder & raw_bytes(MemoryRegion< byte > &)
DER_Encoder & encode_if(bool pred, DER_Encoder &enc)
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
std::string to_string(u64bit n, size_t min_len)
void swap(Botan::MemoryRegion< T > &x, Botan::MemoryRegion< T > &y)
virtual void encode_into(class DER_Encoder &to) const =0
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const byte rep[], size_t length)