Coverage Report

Created: 2020-02-14 15:38

/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
11.9k
   {
18
11.9k
   std::vector<uint8_t> output;
19
11.9k
   DER_Encoder der(output);
20
11.9k
   this->encode_into(der);
21
11.9k
   return output;
22
11.9k
   }
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
715k
   {
30
715k
   if(this->is_a(type_tag_, class_tag_) == false)
31
13.5k
      {
32
13.5k
      std::stringstream msg;
33
13.5k
34
13.5k
      msg << "Tag mismatch when decoding " << descr << " got ";
35
13.5k
36
13.5k
      if(class_tag == NO_OBJECT && type_tag == NO_OBJECT)
37
1.62k
         {
38
1.62k
         msg << "EOF";
39
1.62k
         }
40
11.9k
      else
41
11.9k
         {
42
11.9k
         if(class_tag == UNIVERSAL || class_tag == CONSTRUCTED)
43
7.89k
            {
44
7.89k
            msg << asn1_tag_to_string(type_tag);
45
7.89k
            }
46
4.00k
         else
47
4.00k
            {
48
4.00k
            msg << std::to_string(type_tag);
49
4.00k
            }
50
11.9k
51
11.9k
         msg << "/" << asn1_class_to_string(class_tag);
52
11.9k
         }
53
13.5k
54
13.5k
      msg << " expected ";
55
13.5k
56
13.5k
      if(class_tag_ == UNIVERSAL || class_tag_ == CONSTRUCTED)
57
13.3k
         {
58
13.3k
         msg << asn1_tag_to_string(type_tag_);
59
13.3k
         }
60
210
      else
61
210
         {
62
210
         msg << std::to_string(type_tag_);
63
210
         }
64
13.5k
65
13.5k
      msg << "/" << asn1_class_to_string(class_tag_);
66
13.5k
67
13.5k
      throw BER_Decoding_Error(msg.str());
68
13.5k
      }
69
715k
   }
70
71
bool BER_Object::is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_) const
72
1.03M
   {
73
1.03M
   return (type_tag == type_tag_ && class_tag == class_tag_);
74
1.03M
   }
75
76
bool BER_Object::is_a(int type_tag_, ASN1_Tag class_tag_) const
77
214k
   {
78
214k
   return is_a(ASN1_Tag(type_tag_), class_tag_);
79
214k
   }
80
81
void BER_Object::set_tagging(ASN1_Tag t, ASN1_Tag c)
82
1.40M
   {
83
1.40M
   type_tag = t;
84
1.40M
   class_tag = c;
85
1.40M
   }
86
87
std::string asn1_class_to_string(ASN1_Tag type)
88
25.4k
   {
89
25.4k
   switch(type)
90
25.4k
      {
91
25.4k
      case UNIVERSAL:
92
8.86k
         return "UNIVERSAL";
93
25.4k
      case CONSTRUCTED:
94
12.3k
         return "CONSTRUCTED";
95
25.4k
      case CONTEXT_SPECIFIC:
96
1.08k
         return "CONTEXT_SPECIFIC";
97
25.4k
      case APPLICATION:
98
963
         return "APPLICATION";
99
25.4k
      case CONSTRUCTED | CONTEXT_SPECIFIC:
100
500
         return "PRIVATE";
101
25.4k
      case Botan::NO_OBJECT:
102
0
         return "NO_OBJECT";
103
25.4k
      default:
104
1.66k
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
105
25.4k
      }
106
25.4k
   }
107
108
std::string asn1_tag_to_string(ASN1_Tag type)
109
21.2k
   {
110
21.2k
   switch(type)
111
21.2k
      {
112
21.2k
      case Botan::SEQUENCE:
113
8.00k
         return "SEQUENCE";
114
21.2k
115
21.2k
      case Botan::SET:
116
1.08k
         return "SET";
117
21.2k
118
21.2k
      case Botan::PRINTABLE_STRING:
119
247
         return "PRINTABLE STRING";
120
21.2k
121
21.2k
      case Botan::NUMERIC_STRING:
122
287
         return "NUMERIC STRING";
123
21.2k
124
21.2k
      case Botan::IA5_STRING:
125
181
         return "IA5 STRING";
126
21.2k
127
21.2k
      case Botan::T61_STRING:
128
200
         return "T61 STRING";
129
21.2k
130
21.2k
      case Botan::UTF8_STRING:
131
160
         return "UTF8 STRING";
132
21.2k
133
21.2k
      case Botan::VISIBLE_STRING:
134
231
         return "VISIBLE STRING";
135
21.2k
136
21.2k
      case Botan::BMP_STRING:
137
82
         return "BMP STRING";
138
21.2k
139
21.2k
      case Botan::UNIVERSAL_STRING:
140
60
         return "UNIVERSAL STRING";
141
21.2k
142
21.2k
      case Botan::UTC_TIME:
143
115
         return "UTC TIME";
144
21.2k
145
21.2k
      case Botan::GENERALIZED_TIME:
146
224
         return "GENERALIZED TIME";
147
21.2k
148
21.2k
      case Botan::OCTET_STRING:
149
2.20k
         return "OCTET STRING";
150
21.2k
151
21.2k
      case Botan::BIT_STRING:
152
2.46k
         return "BIT STRING";
153
21.2k
154
21.2k
      case Botan::ENUMERATED:
155
1.43k
         return "ENUMERATED";
156
21.2k
157
21.2k
      case Botan::INTEGER:
158
1.05k
         return "INTEGER";
159
21.2k
160
21.2k
      case Botan::NULL_TAG:
161
177
         return "NULL";
162
21.2k
163
21.2k
      case Botan::OBJECT_ID:
164
258
         return "OBJECT";
165
21.2k
166
21.2k
      case Botan::BOOLEAN:
167
322
         return "BOOLEAN";
168
21.2k
169
21.2k
      case Botan::NO_OBJECT:
170
6
         return "NO_OBJECT";
171
21.2k
172
21.2k
      default:
173
2.40k
         return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")";
174
21.2k
      }
175
21.2k
   }
176
177
/*
178
* BER Decoding Exceptions
179
*/
180
BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
181
29.9k
   Decoding_Error("BER: " + str) {}
182
183
BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
184
2.23k
      BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
185
186
BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
187
                         ASN1_Tag tag1, ASN1_Tag tag2) :
188
0
   BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {}
189
190
namespace ASN1 {
191
192
/*
193
* Put some arbitrary bytes into a SEQUENCE
194
*/
195
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
196
46.9k
   {
197
46.9k
   return ASN1::put_in_sequence(contents.data(), contents.size());
198
46.9k
   }
199
200
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
201
46.9k
   {
202
46.9k
   std::vector<uint8_t> output;
203
46.9k
   DER_Encoder(output)
204
46.9k
      .start_cons(SEQUENCE)
205
46.9k
         .raw_bytes(bits, len)
206
46.9k
      .end_cons();
207
46.9k
   return output;
208
46.9k
   }
209
210
/*
211
* Convert a BER object into a string object
212
*/
213
std::string to_string(const BER_Object& obj)
214
176k
   {
215
176k
   return std::string(cast_uint8_ptr_to_char(obj.bits()),
216
176k
                      obj.length());
217
176k
   }
218
219
/*
220
* Do heuristic tests for BER data
221
*/
222
bool maybe_BER(DataSource& source)
223
33.9k
   {
224
33.9k
   uint8_t first_u8;
225
33.9k
   if(!source.peek_byte(first_u8))
226
13
      {
227
13
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
228
13
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
229
33.8k
      }
230
33.8k
231
33.8k
   if(first_u8 == (SEQUENCE | CONSTRUCTED))
232
27.4k
      return true;
233
6.47k
   return false;
234
6.47k
   }
235
236
}
237
238
}