Coverage Report

Created: 2025-04-11 06:34

/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
10
#include <botan/data_src.h>
11
#include <botan/der_enc.h>
12
#include <botan/mem_ops.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/stl_util.h>
15
#include <sstream>
16
17
namespace Botan {
18
19
52.1k
std::vector<uint8_t> ASN1_Object::BER_encode() const {
20
52.1k
   std::vector<uint8_t> output;
21
52.1k
   DER_Encoder der(output);
22
52.1k
   this->encode_into(der);
23
52.1k
   return output;
24
52.1k
}
25
26
/*
27
* Check a type invariant on BER data
28
*/
29
1.13M
void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag, std::string_view descr) const {
30
1.13M
   if(this->is_a(expected_type_tag, expected_class_tag) == false) {
31
21.0k
      std::stringstream msg;
32
33
21.0k
      msg << "Tag mismatch when decoding " << descr << " got ";
34
35
21.0k
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
36
4.29k
         msg << "EOF";
37
16.7k
      } else {
38
16.7k
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
39
11.4k
            msg << asn1_tag_to_string(m_type_tag);
40
11.4k
         } else {
41
5.36k
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
42
5.36k
         }
43
44
16.7k
         msg << "/" << asn1_class_to_string(m_class_tag);
45
16.7k
      }
46
47
21.0k
      msg << " expected ";
48
49
21.0k
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
50
20.6k
         msg << asn1_tag_to_string(expected_type_tag);
51
20.6k
      } else {
52
401
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
53
401
      }
54
55
21.0k
      msg << "/" << asn1_class_to_string(expected_class_tag);
56
57
21.0k
      throw BER_Decoding_Error(msg.str());
58
21.0k
   }
59
1.13M
}
60
61
2.04M
bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const {
62
2.04M
   return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
63
2.04M
}
64
65
670k
bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const {
66
670k
   return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
67
670k
}
68
69
2.43M
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
70
2.43M
   m_type_tag = type_tag;
71
2.43M
   m_class_tag = class_tag;
72
2.43M
}
73
74
37.8k
std::string asn1_class_to_string(ASN1_Class type) {
75
37.8k
   switch(type) {
76
10.7k
      case ASN1_Class::Universal:
77
10.7k
         return "UNIVERSAL";
78
21.3k
      case ASN1_Class::Constructed:
79
21.3k
         return "CONSTRUCTED";
80
1.50k
      case ASN1_Class::ContextSpecific:
81
1.50k
         return "CONTEXT_SPECIFIC";
82
933
      case ASN1_Class::Application:
83
933
         return "APPLICATION";
84
1.15k
      case ASN1_Class::Private:
85
1.15k
         return "PRIVATE";
86
0
      case ASN1_Class::NoObject:
87
0
         return "NO_OBJECT";
88
2.16k
      default:
89
2.16k
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
90
37.8k
   }
91
37.8k
}
92
93
32.2k
std::string asn1_tag_to_string(ASN1_Type type) {
94
32.2k
   switch(type) {
95
14.2k
      case ASN1_Type::Sequence:
96
14.2k
         return "SEQUENCE";
97
98
1.86k
      case ASN1_Type::Set:
99
1.86k
         return "SET";
100
101
337
      case ASN1_Type::PrintableString:
102
337
         return "PRINTABLE STRING";
103
104
530
      case ASN1_Type::NumericString:
105
530
         return "NUMERIC STRING";
106
107
315
      case ASN1_Type::Ia5String:
108
315
         return "IA5 STRING";
109
110
495
      case ASN1_Type::TeletexString:
111
495
         return "T61 STRING";
112
113
451
      case ASN1_Type::Utf8String:
114
451
         return "UTF8 STRING";
115
116
185
      case ASN1_Type::VisibleString:
117
185
         return "VISIBLE STRING";
118
119
131
      case ASN1_Type::BmpString:
120
131
         return "BMP STRING";
121
122
153
      case ASN1_Type::UniversalString:
123
153
         return "UNIVERSAL STRING";
124
125
319
      case ASN1_Type::UtcTime:
126
319
         return "UTC TIME";
127
128
370
      case ASN1_Type::GeneralizedTime:
129
370
         return "GENERALIZED TIME";
130
131
1.96k
      case ASN1_Type::OctetString:
132
1.96k
         return "OCTET STRING";
133
134
2.42k
      case ASN1_Type::BitString:
135
2.42k
         return "BIT STRING";
136
137
1.24k
      case ASN1_Type::Enumerated:
138
1.24k
         return "ENUMERATED";
139
140
2.31k
      case ASN1_Type::Integer:
141
2.31k
         return "INTEGER";
142
143
300
      case ASN1_Type::Null:
144
300
         return "NULL";
145
146
481
      case ASN1_Type::ObjectId:
147
481
         return "OBJECT";
148
149
614
      case ASN1_Type::Boolean:
150
614
         return "BOOLEAN";
151
152
59
      case ASN1_Type::NoObject:
153
59
         return "NO_OBJECT";
154
155
3.49k
      default:
156
3.49k
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
157
32.2k
   }
158
32.2k
}
159
160
/*
161
* BER Decoding Exceptions
162
*/
163
47.2k
BER_Decoding_Error::BER_Decoding_Error(std::string_view str) : Decoding_Error(fmt("BER: {}", str)) {}
164
165
4.30k
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
166
167
namespace ASN1 {
168
169
/*
170
* Put some arbitrary bytes into a SEQUENCE
171
*/
172
63.0k
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) {
173
63.0k
   return ASN1::put_in_sequence(contents.data(), contents.size());
174
63.0k
}
175
176
63.0k
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) {
177
63.0k
   std::vector<uint8_t> output;
178
63.0k
   DER_Encoder(output).start_sequence().raw_bytes(bits, len).end_cons();
179
63.0k
   return output;
180
63.0k
}
181
182
/*
183
* Convert a BER object into a string object
184
*/
185
238k
std::string to_string(const BER_Object& obj) {
186
238k
   return std::string(cast_uint8_ptr_to_char(obj.bits()), obj.length());
187
238k
}
188
189
/*
190
* Do heuristic tests for BER data
191
*/
192
53.0k
bool maybe_BER(DataSource& source) {
193
53.0k
   uint8_t first_u8;
194
53.0k
   if(!source.peek_byte(first_u8)) {
195
2.25k
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
196
2.25k
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
197
2.25k
   }
198
199
50.7k
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
200
50.7k
   if(first_u8 == cons_seq) {
201
34.9k
      return true;
202
34.9k
   }
203
15.8k
   return false;
204
50.7k
}
205
206
}  // namespace ASN1
207
208
}  // namespace Botan