Coverage Report

Created: 2021-05-04 09:02

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