Coverage Report

Created: 2022-09-23 06:05

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