Coverage Report

Created: 2022-08-24 06:28

/src/botan/src/lib/asn1/asn1_obj.cpp
Line
Count
Source (jump to first uncovered line)
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
0
   {
18
0
   std::vector<uint8_t> output;
19
0
   DER_Encoder der(output);
20
0
   this->encode_into(der);
21
0
   return output;
22
0
   }
23
24
/*
25
* Check a type invariant on BER data
26
*/
27
void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag,
28
                             const std::string& descr) const
29
0
   {
30
0
   if(this->is_a(expected_type_tag, expected_class_tag) == false)
31
0
      {
32
0
      std::stringstream msg;
33
34
0
      msg << "Tag mismatch when decoding " << descr << " got ";
35
36
0
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject)
37
0
         {
38
0
         msg << "EOF";
39
0
         }
40
0
      else
41
0
         {
42
0
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed)
43
0
            {
44
0
            msg << asn1_tag_to_string(m_type_tag);
45
0
            }
46
0
         else
47
0
            {
48
0
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
49
0
            }
50
51
0
         msg << "/" << asn1_class_to_string(m_class_tag);
52
0
         }
53
54
0
      msg << " expected ";
55
56
0
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed)
57
0
         {
58
0
         msg << asn1_tag_to_string(expected_type_tag);
59
0
         }
60
0
      else
61
0
         {
62
0
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
63
0
         }
64
65
0
      msg << "/" << asn1_class_to_string(expected_class_tag);
66
67
0
      throw BER_Decoding_Error(msg.str());
68
0
      }
69
0
   }
70
71
bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const
72
0
   {
73
0
   return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
74
0
   }
75
76
bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const
77
0
   {
78
0
   return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
79
0
   }
80
81
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag)
82
0
   {
83
0
   m_type_tag = type_tag;
84
0
   m_class_tag = class_tag;
85
0
   }
86
87
std::string asn1_class_to_string(ASN1_Class type)
88
0
   {
89
0
   switch(type)
90
0
      {
91
0
      case ASN1_Class::Universal:
92
0
         return "UNIVERSAL";
93
0
      case ASN1_Class::Constructed:
94
0
         return "CONSTRUCTED";
95
0
      case ASN1_Class::ContextSpecific:
96
0
         return "CONTEXT_SPECIFIC";
97
0
      case ASN1_Class::Application:
98
0
         return "APPLICATION";
99
0
      case ASN1_Class::Private:
100
0
         return "PRIVATE";
101
0
      case ASN1_Class::NoObject:
102
0
         return "NO_OBJECT";
103
0
      default:
104
0
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
105
0
      }
106
0
   }
107
108
std::string asn1_tag_to_string(ASN1_Type type)
109
0
   {
110
0
   switch(type)
111
0
      {
112
0
      case ASN1_Type::Sequence:
113
0
         return "SEQUENCE";
114
115
0
      case ASN1_Type::Set:
116
0
         return "SET";
117
118
0
      case ASN1_Type::PrintableString:
119
0
         return "PRINTABLE STRING";
120
121
0
      case ASN1_Type::NumericString:
122
0
         return "NUMERIC STRING";
123
124
0
      case ASN1_Type::Ia5String:
125
0
         return "IA5 STRING";
126
127
0
      case ASN1_Type::TeletexString:
128
0
         return "T61 STRING";
129
130
0
      case ASN1_Type::Utf8String:
131
0
         return "UTF8 STRING";
132
133
0
      case ASN1_Type::VisibleString:
134
0
         return "VISIBLE STRING";
135
136
0
      case ASN1_Type::BmpString:
137
0
         return "BMP STRING";
138
139
0
      case ASN1_Type::UniversalString:
140
0
         return "UNIVERSAL STRING";
141
142
0
      case ASN1_Type::UtcTime:
143
0
         return "UTC TIME";
144
145
0
      case ASN1_Type::GeneralizedTime:
146
0
         return "GENERALIZED TIME";
147
148
0
      case ASN1_Type::OctetString:
149
0
         return "OCTET STRING";
150
151
0
      case ASN1_Type::BitString:
152
0
         return "BIT STRING";
153
154
0
      case ASN1_Type::Enumerated:
155
0
         return "ENUMERATED";
156
157
0
      case ASN1_Type::Integer:
158
0
         return "INTEGER";
159
160
0
      case ASN1_Type::Null:
161
0
         return "NULL";
162
163
0
      case ASN1_Type::ObjectId:
164
0
         return "OBJECT";
165
166
0
      case ASN1_Type::Boolean:
167
0
         return "BOOLEAN";
168
169
0
      case ASN1_Type::NoObject:
170
0
         return "NO_OBJECT";
171
172
0
      default:
173
0
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
174
0
      }
175
0
   }
176
177
/*
178
* BER Decoding Exceptions
179
*/
180
BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
181
0
   Decoding_Error("BER: " + str) {}
182
183
BER_Bad_Tag::BER_Bad_Tag(const std::string& str, uint32_t tagging) :
184
0
   BER_Decoding_Error(str + ": " + std::to_string(tagging)) {}
185
186
namespace ASN1 {
187
188
/*
189
* Put some arbitrary bytes into a SEQUENCE
190
*/
191
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
192
0
   {
193
0
   return ASN1::put_in_sequence(contents.data(), contents.size());
194
0
   }
195
196
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
197
0
   {
198
0
   std::vector<uint8_t> output;
199
0
   DER_Encoder(output)
200
0
      .start_sequence()
201
0
         .raw_bytes(bits, len)
202
0
      .end_cons();
203
0
   return output;
204
0
   }
205
206
/*
207
* Convert a BER object into a string object
208
*/
209
std::string to_string(const BER_Object& obj)
210
0
   {
211
0
   return std::string(cast_uint8_ptr_to_char(obj.bits()),
212
0
                      obj.length());
213
0
   }
214
215
/*
216
* Do heuristic tests for BER data
217
*/
218
bool maybe_BER(DataSource& source)
219
0
   {
220
0
   uint8_t first_u8;
221
0
   if(!source.peek_byte(first_u8))
222
0
      {
223
0
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
224
0
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
225
0
      }
226
227
0
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
228
0
   if(first_u8 == cons_seq)
229
0
      return true;
230
0
   return false;
231
0
   }
232
233
}
234
235
}