Botan  2.4.0
Crypto and TLS for C++11
asn1_obj.cpp
Go to the documentation of this file.
1 /*
2 * ASN.1 Internals
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/asn1_obj.h>
9 #include <botan/der_enc.h>
10 #include <botan/data_src.h>
11 #include <botan/internal/stl_util.h>
12 
13 namespace Botan {
14 
16  {
17  switch(type)
18  {
19  case Botan::SEQUENCE:
20  return "SEQUENCE";
21 
22  case Botan::SET:
23  return "SET";
24 
26  return "PRINTABLE STRING";
27 
29  return "NUMERIC STRING";
30 
31  case Botan::IA5_STRING:
32  return "IA5 STRING";
33 
34  case Botan::T61_STRING:
35  return "T61 STRING";
36 
37  case Botan::UTF8_STRING:
38  return "UTF8 STRING";
39 
41  return "VISIBLE STRING";
42 
43  case Botan::BMP_STRING:
44  return "BMP STRING";
45 
46  case Botan::UTC_TIME:
47  return "UTC TIME";
48 
50  return "GENERALIZED TIME";
51 
53  return "OCTET STRING";
54 
55  case Botan::BIT_STRING:
56  return "BIT STRING";
57 
58  case Botan::ENUMERATED:
59  return "ENUMERATED";
60 
61  case Botan::INTEGER:
62  return "INTEGER";
63 
64  case Botan::NULL_TAG:
65  return "NULL";
66 
67  case Botan::OBJECT_ID:
68  return "OBJECT";
69 
70  case Botan::BOOLEAN:
71  return "BOOLEAN";
72 
73  default:
74  return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")";
75  }
76  }
77 
78 /*
79 * BER Decoding Exceptions
80 */
81 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
82  Decoding_Error("BER: " + str) {}
83 
84 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
85  BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
86 
87 BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
88  ASN1_Tag tag1, ASN1_Tag tag2) :
89  BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {}
90 
91 namespace ASN1 {
92 
93 /*
94 * Put some arbitrary bytes into a SEQUENCE
95 */
96 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
97  {
98  return DER_Encoder()
100  .raw_bytes(contents)
101  .end_cons()
103  }
104 
105 /*
106 * Convert a BER object into a string object
107 */
108 std::string to_string(const BER_Object& obj)
109  {
110  return to_string(obj.value);
111  }
112 
113 /*
114 * Do heuristic tests for BER data
115 */
116 bool maybe_BER(DataSource& source)
117  {
118  uint8_t first_u8;
119  if(!source.peek_byte(first_u8))
120  {
121  BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
122  throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
123  }
124 
125  if(first_u8 == (SEQUENCE | CONSTRUCTED))
126  return true;
127  return false;
128  }
129 
130 }
131 
132 }
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:15
Definition: bigint.h:635
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:116
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
ASN1_Tag
Definition: asn1_obj.h:22
MechanismType type
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:181
secure_vector< uint8_t > value
Definition: asn1_obj.h:97
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:55
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition: asn1_obj.cpp:84
Definition: alg_id.cpp:13
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:23
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:96
std::string to_string(const secure_vector< uint8_t > &bytes)
Definition: stl_util.h:25
size_t peek_byte(uint8_t &out) const
Definition: data_src.cpp:31
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
BER_Decoding_Error(const std::string &)
Definition: asn1_obj.cpp:81