Botan  2.7.0
Crypto and TLS for C++11
asn1_obj.h
Go to the documentation of this file.
1 /*
2 * ASN.1 Internals
3 * (C) 1999-2007,2018 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_ASN1_H_
9 #define BOTAN_ASN1_H_
10 
11 #include <botan/secmem.h>
12 #include <botan/exceptn.h>
13 
14 namespace Botan {
15 
16 class BER_Decoder;
17 class DER_Encoder;
18 
19 /**
20 * ASN.1 Type and Class Tags
21 */
22 enum ASN1_Tag : uint32_t {
23  UNIVERSAL = 0x00,
24  APPLICATION = 0x40,
26 
27  CONSTRUCTED = 0x20,
28 
30 
31  EOC = 0x00,
32  BOOLEAN = 0x01,
33  INTEGER = 0x02,
34  BIT_STRING = 0x03,
35  OCTET_STRING = 0x04,
36  NULL_TAG = 0x05,
37  OBJECT_ID = 0x06,
38  ENUMERATED = 0x0A,
39  SEQUENCE = 0x10,
40  SET = 0x11,
41 
42  UTF8_STRING = 0x0C,
45  T61_STRING = 0x14,
46  IA5_STRING = 0x16,
49  BMP_STRING = 0x1E,
50 
51  UTC_TIME = 0x17,
54 
55  NO_OBJECT = 0xFF00,
57 };
58 
61 
62 /**
63 * Basic ASN.1 Object Interface
64 */
66  {
67  public:
68  /**
69  * Encode whatever this object is into to
70  * @param to the DER_Encoder that will be written to
71  */
72  virtual void encode_into(DER_Encoder& to) const = 0;
73 
74  /**
75  * Decode whatever this object is from from
76  * @param from the BER_Decoder that will be read from
77  */
78  virtual void decode_from(BER_Decoder& from) = 0;
79 
80  /**
81  * Return the encoding of this object. This is a convenience
82  * method when just one object needs to be serialized. Use
83  * DER_Encoder for complicated encodings.
84  */
85  std::vector<uint8_t> BER_encode() const;
86 
87  ASN1_Object() = default;
88  ASN1_Object(const ASN1_Object&) = default;
89  ASN1_Object & operator=(const ASN1_Object&) = default;
90  virtual ~ASN1_Object() = default;
91  };
92 
93 /**
94 * BER Encoded Object
95 */
96 class BOTAN_PUBLIC_API(2,0) BER_Object final
97  {
98  public:
99  BER_Object() : type_tag(NO_OBJECT), class_tag(UNIVERSAL) {}
100 
101  BER_Object(const BER_Object& other) = default;
102 
103  BER_Object& operator=(const BER_Object& other) = default;
104 
105 #if !defined(BOTAN_BUILD_COMPILER_IS_MSVC_2013)
106  BER_Object(BER_Object&& other) = default;
107 
108  BER_Object& operator=(BER_Object&& other) = default;
109 #endif
110 
111  bool is_set() const { return type_tag != NO_OBJECT; }
112 
113  ASN1_Tag tagging() const { return ASN1_Tag(type() | get_class()); }
114 
115  ASN1_Tag type() const { return type_tag; }
116  ASN1_Tag get_class() const { return class_tag; }
117 
118  const uint8_t* bits() const { return value.data(); }
119 
120  size_t length() const { return value.size(); }
121 
122  void assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag,
123  const std::string& descr = "object") const;
124 
125  bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const;
126 
127  bool is_a(int type_tag, ASN1_Tag class_tag) const;
128 
129  BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES:
130  /*
131  * The following member variables are public for historical reasons, but
132  * will be made private in a future major release. Use the accessor
133  * functions above.
134  */
135  ASN1_Tag type_tag, class_tag;
137 
138  private:
139 
140  friend class BER_Decoder;
141 
142  void set_tagging(ASN1_Tag type_tag, ASN1_Tag class_tag);
143 
144  uint8_t* mutable_bits(size_t length)
145  {
146  value.resize(length);
147  return value.data();
148  }
149  };
150 
151 /*
152 * ASN.1 Utility Functions
153 */
154 class DataSource;
155 
156 namespace ASN1 {
157 
158 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& val);
159 std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len);
160 std::string to_string(const BER_Object& obj);
161 
162 /**
163 * Heuristics tests; is this object possibly BER?
164 * @param src a data source that will be peeked at but not modified
165 */
166 bool maybe_BER(DataSource& src);
167 
168 }
169 
170 /**
171 * General BER Decoding Error Exception
172 */
174  {
175  public:
176  explicit BER_Decoding_Error(const std::string&);
177  };
178 
179 /**
180 * Exception For Incorrect BER Taggings
181 */
183  {
184  public:
185  BER_Bad_Tag(const std::string& msg, ASN1_Tag tag);
186  BER_Bad_Tag(const std::string& msg, ASN1_Tag tag1, ASN1_Tag tag2);
187  };
188 
189 }
190 
191 #endif
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:108
ASN1_Tag tagging() const
Definition: asn1_obj.h:113
#define BOTAN_UNSTABLE_API
Definition: compiler.h:34
std::string asn1_class_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:87
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:219
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:210
ASN1_Tag
Definition: asn1_obj.h:22
MechanismType type
ASN1_Tag type() const
Definition: asn1_obj.h:115
ASN1_Tag get_class() const
Definition: asn1_obj.h:116
secure_vector< uint8_t > value
Definition: asn1_obj.h:136
BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES class_tag
Definition: asn1_obj.h:135
size_t length() const
Definition: asn1_obj.h:120
Definition: alg_id.cpp:13
secure_vector< uint8_t > BER_encode(const Private_Key &key)
Definition: pkcs8.cpp:139
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:192
const uint8_t * bits() const
Definition: asn1_obj.h:118
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
bool is_set() const
Definition: asn1_obj.h:111