Botan  2.12.1
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,2018 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 #include <sstream>
13 
14 namespace Botan {
15 
16 std::vector<uint8_t> ASN1_Object::BER_encode() const
17  {
18  std::vector<uint8_t> output;
19  DER_Encoder der(output);
20  this->encode_into(der);
21  return output;
22  }
23 
24 /*
25 * Check a type invariant on BER data
26 */
27 void BER_Object::assert_is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_,
28  const std::string& descr) const
29  {
30  if(this->is_a(type_tag_, class_tag_) == false)
31  {
32  std::stringstream msg;
33 
34  msg << "Tag mismatch when decoding " << descr << " got ";
35 
36  if(class_tag == NO_OBJECT && type_tag == NO_OBJECT)
37  {
38  msg << "EOF";
39  }
40  else
41  {
43  {
44  msg << asn1_tag_to_string(type_tag);
45  }
46  else
47  {
48  msg << std::to_string(type_tag);
49  }
50 
51  msg << "/" << asn1_class_to_string(class_tag);
52  }
53 
54  msg << " expected ";
55 
56  if(class_tag_ == UNIVERSAL || class_tag_ == CONSTRUCTED)
57  {
58  msg << asn1_tag_to_string(type_tag_);
59  }
60  else
61  {
62  msg << std::to_string(type_tag_);
63  }
64 
65  msg << "/" << asn1_class_to_string(class_tag_);
66 
67  throw BER_Decoding_Error(msg.str());
68  }
69  }
70 
71 bool BER_Object::is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_) const
72  {
73  return (type_tag == type_tag_ && class_tag == class_tag_);
74  }
75 
76 bool BER_Object::is_a(int type_tag_, ASN1_Tag class_tag_) const
77  {
78  return is_a(ASN1_Tag(type_tag_), class_tag_);
79  }
80 
81 void BER_Object::set_tagging(ASN1_Tag t, ASN1_Tag c)
82  {
83  type_tag = t;
84  class_tag = c;
85  }
86 
88  {
89  switch(type)
90  {
91  case UNIVERSAL:
92  return "UNIVERSAL";
93  case CONSTRUCTED:
94  return "CONSTRUCTED";
95  case CONTEXT_SPECIFIC:
96  return "CONTEXT_SPECIFIC";
97  case APPLICATION:
98  return "APPLICATION";
100  return "PRIVATE";
101  case Botan::NO_OBJECT:
102  return "NO_OBJECT";
103  default:
104  return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
105  }
106  }
107 
109  {
110  switch(type)
111  {
112  case Botan::SEQUENCE:
113  return "SEQUENCE";
114 
115  case Botan::SET:
116  return "SET";
117 
119  return "PRINTABLE STRING";
120 
122  return "NUMERIC STRING";
123 
124  case Botan::IA5_STRING:
125  return "IA5 STRING";
126 
127  case Botan::T61_STRING:
128  return "T61 STRING";
129 
130  case Botan::UTF8_STRING:
131  return "UTF8 STRING";
132 
134  return "VISIBLE STRING";
135 
136  case Botan::BMP_STRING:
137  return "BMP STRING";
138 
140  return "UNIVERSAL STRING";
141 
142  case Botan::UTC_TIME:
143  return "UTC TIME";
144 
146  return "GENERALIZED TIME";
147 
148  case Botan::OCTET_STRING:
149  return "OCTET STRING";
150 
151  case Botan::BIT_STRING:
152  return "BIT STRING";
153 
154  case Botan::ENUMERATED:
155  return "ENUMERATED";
156 
157  case Botan::INTEGER:
158  return "INTEGER";
159 
160  case Botan::NULL_TAG:
161  return "NULL";
162 
163  case Botan::OBJECT_ID:
164  return "OBJECT";
165 
166  case Botan::BOOLEAN:
167  return "BOOLEAN";
168 
169  case Botan::NO_OBJECT:
170  return "NO_OBJECT";
171 
172  default:
173  return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")";
174  }
175  }
176 
177 /*
178 * BER Decoding Exceptions
179 */
180 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
181  Decoding_Error("BER: " + str) {}
182 
183 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
184  BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
185 
186 BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
187  ASN1_Tag tag1, ASN1_Tag tag2) :
188  BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {}
189 
190 namespace ASN1 {
191 
192 /*
193 * Put some arbitrary bytes into a SEQUENCE
194 */
195 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
196  {
197  return ASN1::put_in_sequence(contents.data(), contents.size());
198  }
199 
200 std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
201  {
202  std::vector<uint8_t> output;
203  DER_Encoder(output)
205  .raw_bytes(bits, len)
206  .end_cons();
207  return output;
208  }
209 
210 /*
211 * Convert a BER object into a string object
212 */
213 std::string to_string(const BER_Object& obj)
214  {
215  return std::string(cast_uint8_ptr_to_char(obj.bits()),
216  obj.length());
217  }
218 
219 /*
220 * Do heuristic tests for BER data
221 */
222 bool maybe_BER(DataSource& source)
223  {
224  uint8_t first_u8;
225  if(!source.peek_byte(first_u8))
226  {
227  BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
228  throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
229  }
230 
231  if(first_u8 == (SEQUENCE | CONSTRUCTED))
232  return true;
233  return false;
234  }
235 
236 }
237 
238 }
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:108
std::string asn1_class_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:87
bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const
Definition: asn1_obj.cpp:71
void assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string &descr="object") const
Definition: asn1_obj.cpp:27
std::vector< uint8_t > BER_encode() const
Definition: asn1_obj.cpp:16
Definition: bigint.h:1135
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:222
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
ASN1_Tag
Definition: asn1_obj.h:23
MechanismType type
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:228
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES class_tag
Definition: asn1_obj.h:134
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:81
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition: asn1_obj.cpp:183
size_t length() const
Definition: asn1_obj.h:119
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:195
const uint8_t * bits() const
Definition: asn1_obj.h:117
virtual void encode_into(DER_Encoder &to) const =0
size_t peek_byte(uint8_t &out) const
Definition: data_src.cpp:31
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:169
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:181
BER_Decoding_Error(const std::string &)
Definition: asn1_obj.cpp:180