Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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_DLL DER_Encoder
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 DER_Encoder& raw_bytes(const uint8_t val[], size_t len);
38 DER_Encoder& raw_bytes(const secure_vector<uint8_t>& val);
39 DER_Encoder& raw_bytes(const std::vector<uint8_t>& val);
40
41 DER_Encoder& encode_null();
42 DER_Encoder& encode(bool b);
43 DER_Encoder& encode(size_t s);
44 DER_Encoder& encode(const BigInt& n);
45 DER_Encoder& encode(const secure_vector<uint8_t>& v, ASN1_Tag real_type);
46 DER_Encoder& encode(const std::vector<uint8_t>& v, ASN1_Tag real_type);
47 DER_Encoder& encode(const uint8_t val[], size_t len, ASN1_Tag real_type);
48
49 DER_Encoder& encode(bool b,
50 ASN1_Tag type_tag,
51 ASN1_Tag class_tag = CONTEXT_SPECIFIC);
52
53 DER_Encoder& encode(size_t s,
54 ASN1_Tag type_tag,
55 ASN1_Tag class_tag = CONTEXT_SPECIFIC);
56
57 DER_Encoder& encode(const BigInt& n,
58 ASN1_Tag type_tag,
59 ASN1_Tag class_tag = CONTEXT_SPECIFIC);
60
61 DER_Encoder& encode(const std::vector<uint8_t>& v,
62 ASN1_Tag real_type,
63 ASN1_Tag type_tag,
64 ASN1_Tag class_tag = CONTEXT_SPECIFIC);
65
66 DER_Encoder& encode(const secure_vector<uint8_t>& v,
67 ASN1_Tag real_type,
68 ASN1_Tag type_tag,
69 ASN1_Tag class_tag = CONTEXT_SPECIFIC);
70
71 DER_Encoder& encode(const uint8_t v[], size_t len,
72 ASN1_Tag real_type,
73 ASN1_Tag type_tag,
74 ASN1_Tag class_tag = CONTEXT_SPECIFIC);
75
76 template<typename T>
77 DER_Encoder& encode_optional(const T& value, const T& default_value)
78 {
79 if(value != default_value)
80 encode(value);
81 return (*this);
82 }
83
84 template<typename T>
85 DER_Encoder& encode_list(const std::vector<T>& values)
86 {
87 for(size_t i = 0; i != values.size(); ++i)
88 encode(values[i]);
89 return (*this);
90 }
91
92 DER_Encoder& encode(const ASN1_Object& obj);
93 DER_Encoder& encode_if(bool pred, DER_Encoder& enc);
94 DER_Encoder& encode_if(bool pred, const ASN1_Object& obj);
95
96 DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
97 const uint8_t rep[], size_t length);
98
99 DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
100 const std::vector<uint8_t>& rep)
101 {
102 return add_object(type_tag, class_tag, rep.data(), rep.size());
103 }
104
105 DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
106 const secure_vector<uint8_t>& rep)
107 {
108 return add_object(type_tag, class_tag, rep.data(), rep.size());
109 }
110
111 DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
112 const std::string& str);
113
114 DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
115 uint8_t val);
116
117 private:
118 class DER_Sequence
119 {
120 public:
121 ASN1_Tag tag_of() const;
122 secure_vector<uint8_t> get_contents();
123 void add_bytes(const uint8_t[], size_t);
124 DER_Sequence(ASN1_Tag, ASN1_Tag);
125 private:
126 ASN1_Tag m_type_tag, m_class_tag;
127 secure_vector<uint8_t> m_contents;
128 std::vector< secure_vector<uint8_t> > m_set_contents;
129 };
130
131 secure_vector<uint8_t> m_contents;
132 std::vector<DER_Sequence> m_subsequences;
133 };
134
135 }
136
137 #endif
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::vector< uint8_t > &rep)
Definition: der_enc.h:99
Botan::DER_Encoder::encode_list
DER_Encoder & encode_list(const std::vector< T > &values)
Definition: der_enc.h:85
Botan::CONTEXT_SPECIFIC
Definition: asn1_obj.h:25
Botan::DER_Encoder::encode_optional
DER_Encoder & encode_optional(const T &value, const T &default_value)
Definition: der_enc.h:77
Botan::ASN1_Object
Definition: asn1_obj.h:61
Botan::BigInt
Definition: bigint.h:23
Botan::DER_Encoder::get_contents_unlocked
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
Botan::PEM_Code::encode
std::string encode(const uint8_t der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:43
Botan::UNIVERSAL
Definition: asn1_obj.h:23
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_obj.h:22
Botan
Definition: alg_id.cpp:13
Botan::DER_Encoder::add_object
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const secure_vector< uint8_t > &rep)
Definition: der_enc.h:105
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125