Botan  1.10.9
der_enc.h
Go to the documentation of this file.
1 /*
2 * DER Encoder
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_DER_ENCODER_H__
9 #define BOTAN_DER_ENCODER_H__
10 
11 #include <botan/asn1_int.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  SecureVector<byte> get_contents();
26 
27  DER_Encoder& start_cons(ASN1_Tag type_tag,
28  ASN1_Tag class_tag = UNIVERSAL);
29  DER_Encoder& end_cons();
30 
31  DER_Encoder& start_explicit(u16bit type_tag);
32  DER_Encoder& end_explicit();
33 
34  DER_Encoder& raw_bytes(const byte val[], size_t len);
35  DER_Encoder& raw_bytes(const MemoryRegion<byte>& val);
36 
37  DER_Encoder& encode_null();
38  DER_Encoder& encode(bool b);
39  DER_Encoder& encode(size_t s);
40  DER_Encoder& encode(const BigInt& n);
41  DER_Encoder& encode(const MemoryRegion<byte>& v, ASN1_Tag real_type);
42  DER_Encoder& encode(const byte val[], size_t len, ASN1_Tag real_type);
43 
44  DER_Encoder& encode(bool b,
45  ASN1_Tag type_tag,
46  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
47 
48  DER_Encoder& encode(size_t s,
49  ASN1_Tag type_tag,
50  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
51 
52  DER_Encoder& encode(const BigInt& n,
53  ASN1_Tag type_tag,
54  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
55 
57  ASN1_Tag real_type,
58  ASN1_Tag type_tag,
59  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
60 
61  DER_Encoder& encode(const byte v[], size_t len,
62  ASN1_Tag real_type,
63  ASN1_Tag type_tag,
64  ASN1_Tag class_tag = CONTEXT_SPECIFIC);
65 
66  template<typename T>
67  DER_Encoder& encode_optional(const T& value, const T& default_value)
68  {
69  if(value != default_value)
70  encode(value);
71  return (*this);
72  }
73 
74  template<typename T>
75  DER_Encoder& encode_list(const std::vector<T>& values)
76  {
77  for(size_t i = 0; i != values.size(); ++i)
78  encode(values[i]);
79  return (*this);
80  }
81 
82  DER_Encoder& encode(const ASN1_Object& obj);
83  DER_Encoder& encode_if(bool pred, DER_Encoder& enc);
84 
85  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
86  const byte rep[], size_t length);
87 
88  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
89  const MemoryRegion<byte>& rep);
90 
91  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
92  const std::string& str);
93 
94  DER_Encoder& add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
95  byte val);
96 
97  private:
98  class DER_Sequence
99  {
100  public:
101  ASN1_Tag tag_of() const;
102  SecureVector<byte> get_contents();
103  void add_bytes(const byte[], size_t);
104  DER_Sequence(ASN1_Tag, ASN1_Tag);
105  private:
106  ASN1_Tag type_tag, class_tag;
107  SecureVector<byte> contents;
108  std::vector< SecureVector<byte> > set_contents;
109  };
110 
111  SecureVector<byte> contents;
112  std::vector<DER_Sequence> subsequences;
113  };
114 
115 }
116 
117 #endif
DER_Encoder & encode_list(const std::vector< T > &values)
Definition: der_enc.h:75
DER_Encoder & encode_optional(const T &value, const T &default_value)
Definition: der_enc.h:67
BigInt n
Definition: numthry.cpp:26
unsigned char byte
Definition: types.h:22
ASN1_Tag
Definition: asn1_int.h:19
unsigned short u16bit
Definition: types.h:27
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
size_t s
Definition: numthry.cpp:27