Botan 3.4.0
Crypto and TLS for C&
asn1_obj.h
Go to the documentation of this file.
1/*
2* (C) 1999-2007,2018,2020 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_ASN1_OBJECT_TYPES_H_
8#define BOTAN_ASN1_OBJECT_TYPES_H_
9
10#include <botan/exceptn.h>
11#include <botan/secmem.h>
12#include <chrono>
13#include <iosfwd>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <unordered_map>
18#include <vector>
19
20namespace Botan {
21
22class BER_Decoder;
23class DER_Encoder;
24
25/**
26* ASN.1 Class Tags
27*/
28enum class ASN1_Class : uint32_t {
29 Universal = 0b0000'0000,
30 Application = 0b0100'0000,
31 ContextSpecific = 0b1000'0000,
32 Private = 0b1100'0000,
33
34 Constructed = 0b0010'0000,
36
37 NoObject = 0xFF00
38};
39
40/**
41* ASN.1 Type Tags
42*/
43enum class ASN1_Type : uint32_t {
44 Eoc = 0x00,
45 Boolean = 0x01,
46 Integer = 0x02,
47 BitString = 0x03,
48 OctetString = 0x04,
49 Null = 0x05,
50 ObjectId = 0x06,
51 Enumerated = 0x0A,
52 Sequence = 0x10,
53 Set = 0x11,
54
55 Utf8String = 0x0C,
56 NumericString = 0x12,
57 PrintableString = 0x13,
58 TeletexString = 0x14,
59 Ia5String = 0x16,
60 VisibleString = 0x1A,
61 UniversalString = 0x1C,
62 BmpString = 0x1E,
63
64 UtcTime = 0x17,
65 GeneralizedTime = 0x18,
66
67 NoObject = 0xFF00,
68};
69
70inline bool intersects(ASN1_Class x, ASN1_Class y) {
71 return static_cast<uint32_t>(x) & static_cast<uint32_t>(y);
72}
73
75 return static_cast<ASN1_Type>(static_cast<uint32_t>(x) | static_cast<uint32_t>(y));
76}
77
79 return static_cast<ASN1_Class>(static_cast<uint32_t>(x) | static_cast<uint32_t>(y));
80}
81
82inline uint32_t operator|(ASN1_Type x, ASN1_Class y) {
83 return static_cast<uint32_t>(x) | static_cast<uint32_t>(y);
84}
85
86inline uint32_t operator|(ASN1_Class x, ASN1_Type y) {
87 return static_cast<uint32_t>(x) | static_cast<uint32_t>(y);
88}
89
92
93/**
94* Basic ASN.1 Object Interface
95*/
97 public:
98 /**
99 * Encode whatever this object is into to
100 * @param to the DER_Encoder that will be written to
101 */
102 virtual void encode_into(DER_Encoder& to) const = 0;
103
104 /**
105 * Decode whatever this object is from from
106 * @param from the BER_Decoder that will be read from
107 */
108 virtual void decode_from(BER_Decoder& from) = 0;
109
110 /**
111 * Return the encoding of this object. This is a convenience
112 * method when just one object needs to be serialized. Use
113 * DER_Encoder for complicated encodings.
114 */
115 std::vector<uint8_t> BER_encode() const;
116
117 ASN1_Object() = default;
118 ASN1_Object(const ASN1_Object&) = default;
120 virtual ~ASN1_Object() = default;
121};
122
123/**
124* BER Encoded Object
125*/
127 public:
128 BER_Object() : m_type_tag(ASN1_Type::NoObject), m_class_tag(ASN1_Class::Universal) {}
129
130 BER_Object(const BER_Object& other) = default;
131
132 BER_Object& operator=(const BER_Object& other) = default;
133
134 BER_Object(BER_Object&& other) = default;
135
136 BER_Object& operator=(BER_Object&& other) = default;
137
138 bool is_set() const { return m_type_tag != ASN1_Type::NoObject; }
139
140 uint32_t tagging() const { return type_tag() | class_tag(); }
141
142 ASN1_Type type_tag() const { return m_type_tag; }
143
144 ASN1_Class class_tag() const { return m_class_tag; }
145
146 ASN1_Type type() const { return m_type_tag; }
147
148 ASN1_Class get_class() const { return m_class_tag; }
149
150 const uint8_t* bits() const { return m_value.data(); }
151
152 size_t length() const { return m_value.size(); }
153
154 void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr = "object") const;
155
156 bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const;
157
158 bool is_a(int type_tag, ASN1_Class class_tag) const;
159
160 private:
161 ASN1_Type m_type_tag;
162 ASN1_Class m_class_tag;
164
165 friend class BER_Decoder;
166
167 void set_tagging(ASN1_Type type_tag, ASN1_Class class_tag);
168
169 uint8_t* mutable_bits(size_t length) {
170 m_value.resize(length);
171 return m_value.data();
172 }
173};
174
175/*
176* ASN.1 Utility Functions
177*/
178class DataSource;
179
180namespace ASN1 {
181
182std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& val);
183std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len);
184std::string to_string(const BER_Object& obj);
185
186/**
187* Heuristics tests; is this object possibly BER?
188* @param src a data source that will be peeked at but not modified
189*/
190bool maybe_BER(DataSource& src);
191
192} // namespace ASN1
193
194/**
195* General BER Decoding Error Exception
196*/
198 public:
199 explicit BER_Decoding_Error(std::string_view);
200};
201
202/**
203* Exception For Incorrect BER Taggings
204*/
206 public:
207 BER_Bad_Tag(std::string_view msg, uint32_t tagging);
208};
209
210/**
211* This class represents ASN.1 object identifiers.
212*/
213class BOTAN_PUBLIC_API(2, 0) OID final : public ASN1_Object {
214 public:
215 /**
216 * Create an uninitialied OID object
217 */
218 explicit OID() = default;
219
220 /**
221 * Construct an OID from a string.
222 * @param str a string in the form "a.b.c" etc., where a,b,c are numbers
223 */
224 explicit OID(std::string_view str);
225
226 /**
227 * Initialize an OID from a sequence of integer values
228 */
229 explicit OID(std::initializer_list<uint32_t> init) : m_id(init) {
230 BOTAN_ARG_CHECK(m_id.size() > 2 && m_id[0] <= 2 && (m_id[0] != 2 || m_id[1] <= 39), "Invalid OID");
231 }
232
233 /**
234 * Initialize an OID from a vector of integer values
235 */
236 explicit OID(std::vector<uint32_t>&& init) : m_id(init) {
237 BOTAN_ARG_CHECK(m_id.size() > 2 && m_id[0] <= 2 && (m_id[0] != 2 || m_id[1] <= 39), "Invalid OID");
238 }
239
240 /**
241 * Construct an OID from a string.
242 * @param str a string in the form "a.b.c" etc., where a,b,c are numbers
243 * or any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier")
244 */
245 static OID from_string(std::string_view str);
246
247 /**
248 * Construct an OID from a name
249 * @param name any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier")
250 */
251 static std::optional<OID> from_name(std::string_view name);
252
253 /**
254 * Register a new OID in the internal table
255 */
256 static void register_oid(const OID& oid, std::string_view name);
257
258 void encode_into(DER_Encoder&) const override;
259 void decode_from(BER_Decoder&) override;
260
261 /**
262 * Find out whether this OID is empty
263 * @return true is no OID value is set
264 */
265 bool empty() const { return m_id.empty(); }
266
267 /**
268 * Find out whether this OID has a value
269 * @return true is this OID has a value
270 */
271 bool has_value() const { return (m_id.empty() == false); }
272
273 /**
274 * Get this OID as list (vector) of its components.
275 * @return vector representing this OID
276 */
277 const std::vector<uint32_t>& get_components() const { return m_id; }
278
279 const std::vector<uint32_t>& get_id() const { return get_components(); }
280
281 /**
282 * Get this OID as a dotted-decimal string
283 * @return string representing this OID
284 */
285 std::string to_string() const;
286
287 /**
288 * If there is a known name associated with this OID, return that.
289 * Otherwise return the result of to_string
290 */
291 std::string to_formatted_string() const;
292
293 /**
294 * If there is a known name associated with this OID, return that.
295 * Otherwise return the empty string.
296 */
297 std::string human_name_or_empty() const;
298
299 /**
300 * Return true if the OID in *this is registered in the internal
301 * set of constants as a known OID.
302 */
303 bool registered_oid() const;
304
305 /**
306 * Compare two OIDs.
307 * @return true if they are equal, false otherwise
308 */
309 bool operator==(const OID& other) const { return m_id == other.m_id; }
310
311 private:
312 std::unordered_map<std::string, std::string> load_oid2str_map();
313 std::unordered_map<std::string, OID> load_str2oid_map();
314
315 std::vector<uint32_t> m_id;
316};
317
318std::ostream& operator<<(std::ostream& out, const OID& oid);
319
320/**
321* Compare two OIDs.
322* @param a the first OID
323* @param b the second OID
324* @return true if a is not equal to b
325*/
326inline bool operator!=(const OID& a, const OID& b) {
327 return !(a == b);
328}
329
330/**
331* Compare two OIDs.
332* @param a the first OID
333* @param b the second OID
334* @return true if a is lexicographically smaller than b
335*/
336bool BOTAN_PUBLIC_API(2, 0) operator<(const OID& a, const OID& b);
337
338/**
339* Time (GeneralizedTime/UniversalTime)
340*/
342 public:
343 /// DER encode a ASN1_Time
344 void encode_into(DER_Encoder&) const override;
345
346 // Decode a BER encoded ASN1_Time
347 void decode_from(BER_Decoder&) override;
348
349 /// Return an internal string representation of the time
350 std::string to_string() const;
351
352 /// Returns a human friendly string replesentation of no particular formatting
353 std::string readable_string() const;
354
355 /// Return if the time has been set somehow
356 bool time_is_set() const;
357
358 /// Compare this time against another
359 int32_t cmp(const ASN1_Time& other) const;
360
361 /// Create an invalid ASN1_Time
362 ASN1_Time() = default;
363
364 /// Create a ASN1_Time from a time point
365 explicit ASN1_Time(const std::chrono::system_clock::time_point& time);
366
367 /// Create an ASN1_Time from string
368 ASN1_Time(std::string_view t_spec);
369
370 /// Create an ASN1_Time from string and a specified tagging (Utc or Generalized)
371 ASN1_Time(std::string_view t_spec, ASN1_Type tag);
372
373 /// Returns a STL timepoint object
374 std::chrono::system_clock::time_point to_std_timepoint() const;
375
376 /// Return time since epoch
377 uint64_t time_since_epoch() const;
378
379 private:
380 void set_to(std::string_view t_spec, ASN1_Type type);
381 bool passes_sanity_check() const;
382
383 uint32_t m_year = 0;
384 uint32_t m_month = 0;
385 uint32_t m_day = 0;
386 uint32_t m_hour = 0;
387 uint32_t m_minute = 0;
388 uint32_t m_second = 0;
390};
391
392/*
393* Comparison Operations
394*/
395bool BOTAN_PUBLIC_API(2, 0) operator==(const ASN1_Time&, const ASN1_Time&);
396bool BOTAN_PUBLIC_API(2, 0) operator!=(const ASN1_Time&, const ASN1_Time&);
397bool BOTAN_PUBLIC_API(2, 0) operator<=(const ASN1_Time&, const ASN1_Time&);
398bool BOTAN_PUBLIC_API(2, 0) operator>=(const ASN1_Time&, const ASN1_Time&);
399bool BOTAN_PUBLIC_API(2, 0) operator<(const ASN1_Time&, const ASN1_Time&);
400bool BOTAN_PUBLIC_API(2, 0) operator>(const ASN1_Time&, const ASN1_Time&);
401
403
404/**
405* ASN.1 string type
406* This class normalizes all inputs to a UTF-8 std::string
407*/
409 public:
410 void encode_into(DER_Encoder&) const override;
411 void decode_from(BER_Decoder&) override;
412
413 ASN1_Type tagging() const { return m_tag; }
414
415 const std::string& value() const { return m_utf8_str; }
416
417 size_t size() const { return value().size(); }
418
419 bool empty() const { return m_utf8_str.empty(); }
420
421 /**
422 * Return true iff this is a tag for a known string type we can handle.
423 */
424 static bool is_string_type(ASN1_Type tag);
425
426 bool operator==(const ASN1_String& other) const { return value() == other.value(); }
427
428 explicit ASN1_String(std::string_view utf8 = "");
429 ASN1_String(std::string_view utf8, ASN1_Type tag);
430
431 private:
432 std::vector<uint8_t> m_data;
433 std::string m_utf8_str;
434 ASN1_Type m_tag;
435};
436
437/**
438* Algorithm Identifier
439*/
441 public:
442 enum Encoding_Option { USE_NULL_PARAM, USE_EMPTY_PARAM };
443
444 void encode_into(DER_Encoder&) const override;
445 void decode_from(BER_Decoder&) override;
446
448
449 AlgorithmIdentifier(const OID& oid, Encoding_Option enc);
450 AlgorithmIdentifier(std::string_view oid_name, Encoding_Option enc);
451
452 AlgorithmIdentifier(const OID& oid, const std::vector<uint8_t>& params);
453 AlgorithmIdentifier(std::string_view oid_name, const std::vector<uint8_t>& params);
454
455 const OID& oid() const { return m_oid; }
456
457 const std::vector<uint8_t>& parameters() const { return m_parameters; }
458
459 BOTAN_DEPRECATED("Use AlgorithmIdentifier::oid")
460
461 const OID& get_oid() const { return m_oid; }
462
463 BOTAN_DEPRECATED("Use AlgorithmIdentifier::parameters")
464
465 const std::vector<uint8_t>& get_parameters() const { return m_parameters; }
466
467 bool parameters_are_null() const;
468
469 bool parameters_are_empty() const { return m_parameters.empty(); }
470
471 bool parameters_are_null_or_empty() const { return parameters_are_empty() || parameters_are_null(); }
472
473 bool empty() const { return m_oid.empty() && m_parameters.empty(); }
474
475 private:
476 OID m_oid;
477 std::vector<uint8_t> m_parameters;
478};
479
480/*
481* Comparison Operations
482*/
483bool BOTAN_PUBLIC_API(2, 0) operator==(const AlgorithmIdentifier&, const AlgorithmIdentifier&);
484bool BOTAN_PUBLIC_API(2, 0) operator!=(const AlgorithmIdentifier&, const AlgorithmIdentifier&);
485
486} // namespace Botan
487
488#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
ASN1_Object & operator=(const ASN1_Object &)=default
ASN1_Object(const ASN1_Object &)=default
virtual ~ASN1_Object()=default
virtual void decode_from(BER_Decoder &from)=0
ASN1_Object()=default
virtual void encode_into(DER_Encoder &to) const =0
const std::string & value() const
Definition asn1_obj.h:415
bool empty() const
Definition asn1_obj.h:419
size_t size() const
Definition asn1_obj.h:417
bool operator==(const ASN1_String &other) const
Definition asn1_obj.h:426
ASN1_Type tagging() const
Definition asn1_obj.h:413
ASN1_Time()=default
Create an invalid ASN1_Time.
bool parameters_are_empty() const
Definition asn1_obj.h:469
bool parameters_are_null_or_empty() const
Definition asn1_obj.h:471
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:457
const OID & oid() const
Definition asn1_obj.h:455
size_t length() const
Definition asn1_obj.h:152
BER_Object(BER_Object &&other)=default
const uint8_t * bits() const
Definition asn1_obj.h:150
BER_Object & operator=(const BER_Object &other)=default
ASN1_Type type_tag() const
Definition asn1_obj.h:142
uint32_t tagging() const
Definition asn1_obj.h:140
BER_Object(const BER_Object &other)=default
bool is_set() const
Definition asn1_obj.h:138
BER_Object & operator=(BER_Object &&other)=default
ASN1_Type type() const
Definition asn1_obj.h:146
ASN1_Class get_class() const
Definition asn1_obj.h:148
ASN1_Class class_tag() const
Definition asn1_obj.h:144
const std::vector< uint32_t > & get_id() const
Definition asn1_obj.h:279
const std::vector< uint32_t > & get_components() const
Definition asn1_obj.h:277
OID(std::initializer_list< uint32_t > init)
Definition asn1_obj.h:229
bool operator==(const OID &other) const
Definition asn1_obj.h:309
OID(std::vector< uint32_t > &&init)
Definition asn1_obj.h:236
OID()=default
bool has_value() const
Definition asn1_obj.h:271
bool empty() const
Definition asn1_obj.h:265
int(* init)(CTX *)
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_UNSTABLE_API
Definition compiler.h:44
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition asn1_obj.cpp:172
bool maybe_BER(DataSource &source)
Definition asn1_obj.cpp:192
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:185
ASN1_Type operator|(ASN1_Type x, ASN1_Type y)
Definition asn1_obj.h:74
std::string asn1_tag_to_string(ASN1_Type type)
Definition asn1_obj.cpp:93
ASN1_Class
Definition asn1_obj.h:28
ASN1_Type
Definition asn1_obj.h:43
std::ostream & operator<<(std::ostream &out, const OID &oid)
Definition asn1_oid.cpp:140
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition alg_id.cpp:69
std::string asn1_class_to_string(ASN1_Class type)
Definition asn1_obj.cpp:74
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
bool intersects(ASN1_Class x, ASN1_Class y)
Definition asn1_obj.h:70
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13