Botan  2.7.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,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 
139  case Botan::UTC_TIME:
140  return "UTC TIME";
141 
143  return "GENERALIZED TIME";
144 
145  case Botan::OCTET_STRING:
146  return "OCTET STRING";
147 
148  case Botan::BIT_STRING:
149  return "BIT STRING";
150 
151  case Botan::ENUMERATED:
152  return "ENUMERATED";
153 
154  case Botan::INTEGER:
155  return "INTEGER";
156 
157  case Botan::NULL_TAG:
158  return "NULL";
159 
160  case Botan::OBJECT_ID:
161  return "OBJECT";
162 
163  case Botan::BOOLEAN:
164  return "BOOLEAN";
165 
166  case Botan::NO_OBJECT:
167  return "NO_OBJECT";
168 
169  default:
170  return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")";
171  }
172  }
173 
174 /*
175 * BER Decoding Exceptions
176 */
177 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
178  Decoding_Error("BER: " + str) {}
179 
180 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
181  BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
182 
183 BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
184  ASN1_Tag tag1, ASN1_Tag tag2) :
185  BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {}
186 
187 namespace ASN1 {
188 
189 /*
190 * Put some arbitrary bytes into a SEQUENCE
191 */
192 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
193  {
194  return ASN1::put_in_sequence(contents.data(), contents.size());
195  }
196 
197 std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
198  {
199  std::vector<uint8_t> output;
200  DER_Encoder(output)
202  .raw_bytes(bits, len)
203  .end_cons();
204  return output;
205  }
206 
207 /*
208 * Convert a BER object into a string object
209 */
210 std::string to_string(const BER_Object& obj)
211  {
212  return std::string(cast_uint8_ptr_to_char(obj.bits()),
213  obj.length());
214  }
215 
216 /*
217 * Do heuristic tests for BER data
218 */
219 bool maybe_BER(DataSource& source)
220  {
221  uint8_t first_u8;
222  if(!source.peek_byte(first_u8))
223  {
224  BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
225  throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
226  }
227 
228  if(first_u8 == (SEQUENCE | CONSTRUCTED))
229  return true;
230  return false;
231  }
232 
233 }
234 
235 }
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:796
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
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
BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES class_tag
Definition: asn1_obj.h:135
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:69
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition: asn1_obj.cpp:180
size_t length() const
Definition: asn1_obj.h:120
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:192
const uint8_t * bits() const
Definition: asn1_obj.h:118
virtual void encode_into(DER_Encoder &to) const =0
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
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:136
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:177