Botan  2.6.0
Crypto and TLS for C++11
der_enc.h
Go to the documentation of this file.
1 /*
2 * DER Encoder
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_DER_ENCODER_H_
9 #define BOTAN_DER_ENCODER_H_
10 
11 #include <botan/asn1_obj.h>
12 #include <vector>
13 
14 namespace Botan {
15 
16 class BigInt;
17 class ASN1_Object;
18 
19 /**
20 * General DER Encoding Object
21 */
22 class BOTAN_PUBLIC_API(2,0) DER_Encoder final
23  {
24  public:
25  secure_vector<uint8_t> get_contents();
26 
27  std::vector<uint8_t> get_contents_unlocked()
28  { return unlock(get_contents()); }
29 
30  DER_Encoder& start_cons(ASN1_Tag type_tag,
31  ASN1_Tag class_tag = UNIVERSAL);
32  DER_Encoder& end_cons();
33 
34  DER_Encoder& start_explicit(uint16_t type_tag);
35  DER_Encoder& end_explicit();
36 
37  /**
38  * Insert raw bytes directly into the output stream
39  */
40  DER_Encoder& raw_bytes(const uint8_t val[], size_t len);
41 
42  template<typename Alloc>
43  DER_Encoder& raw_bytes(const std::vector<uint8_t, Alloc>& val)
44  {
45  return raw_bytes(val.data(), val.size());
46  }
47 
48  DER_Encoder& encode_null();
49  DER_Encoder& encode(bool b);
50  DER_Encoder& encode(size_t s);
51  DER_Encoder& encode(const BigInt& n);
52  DER_Encoder& encode(const uint8_t val[], size_t len, ASN1_Tag real_type);
53 
54  template<typename Alloc>
55  DER_Encoder& encode(const std::vector<uint8_t, Alloc>& vec, ASN1_Tag real_type)
56  {
57  return encode(vec.data(), vec.size(), real_type);
58  }
59 
60  DER_Encoder& encode(bool b,
61  ASN1_Tag type_tag,
62  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
63 
64  DER_Encoder& encode(size_t s,
65  ASN1_Tag type_tag,
66  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
67 
68  DER_Encoder& encode(const BigInt& n,
69  ASN1_Tag type_tag,
70  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
71 
72  DER_Encoder& encode(const std::vector<uint8_t>& v,
73  ASN1_Tag real_type,
74  ASN1_Tag type_tag,
75  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
76 
78  ASN1_Tag real_type,
79  ASN1_Tag type_tag,
80  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
81 
82  DER_Encoder& encode(const uint8_t v[], size_t len,
83  ASN1_Tag real_type,
84  ASN1_Tag type_tag,
85  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
86 
87  template<typename T>
88  DER_Encoder& encode_optional(const T& value, const T& default_value)
89  {
90  if(value != default_value)
91  encode(value);
92  return (*this);
93  }
94 
95  template<typename T>
96  DER_Encoder& encode_list(const std::vector<T>& values)
97  {
98  for(size_t i = 0; i != values.size(); ++i)
99  encode(values[i]);
100  return (*this);
101  }
102 
103  DER_Encoder& encode(const ASN1_Object& obj);
104  DER_Encoder& encode_if(bool pred, DER_Encoder& enc);
105  DER_Encoder& encode_if(bool pred, const ASN1_Object& obj);
106 
107  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
108  const uint8_t rep[], size_t length);
109 
110  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
111  const std::vector<uint8_t>& rep)
112  {
113  return add_object(type_tag, class_tag, rep.data(), rep.size());
114  }
115 
116  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
117  const secure_vector<uint8_t>& rep)
118  {
119  return add_object(type_tag, class_tag, rep.data(), rep.size());
120  }
121 
122  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
123  const std::string& str);
124 
125  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
126  uint8_t val);
127 
128  private:
129  class DER_Sequence final
130  {
131  public:
132  ASN1_Tag tag_of() const;
133  secure_vector<uint8_t> get_contents();
134  void add_bytes(const uint8_t[], size_t);
135  DER_Sequence(ASN1_Tag, ASN1_Tag);
136  private:
137  ASN1_Tag m_type_tag, m_class_tag;
138  secure_vector<uint8_t> m_contents;
139  std::vector< secure_vector<uint8_t> > m_set_contents;
140  };
141 
142  secure_vector<uint8_t> m_contents;
143  std::vector<DER_Sequence> m_subsequences;
144  };
145 
146 }
147 
148 #endif
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::vector< uint8_t > &rep)
Definition: der_enc.h:110
DER_Encoder & encode_list(const std::vector< T > &values)
Definition: der_enc.h:96
DER_Encoder & encode_optional(const T &value, const T &default_value)
Definition: der_enc.h:88
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
DER_Encoder & encode(const std::vector< uint8_t, Alloc > &vec, ASN1_Tag real_type)
Definition: der_enc.h:55
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
ASN1_Tag
Definition: asn1_obj.h:22
DER_Encoder & raw_bytes(const std::vector< uint8_t, Alloc > &val)
Definition: der_enc.h:43
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
Definition: alg_id.cpp:13
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const secure_vector< uint8_t > &rep)
Definition: der_enc.h:116
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95
fe T
Definition: ge.cpp:37
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88