Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/asn1/asn1_print.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2014,2015,2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/asn1_print.h>
8
#include <botan/bigint.h>
9
#include <botan/hex.h>
10
#include <botan/der_enc.h>
11
#include <botan/ber_dec.h>
12
#include <botan/oids.h>
13
#include <iomanip>
14
#include <sstream>
15
#include <cctype>
16
17
namespace Botan {
18
19
namespace {
20
21
bool all_printable_chars(const uint8_t bits[], size_t bits_len)
22
707
   {
23
2.28k
   for(size_t i = 0; i != bits_len; ++i)
24
2.04k
      {
25
2.04k
      int c = bits[i];
26
2.04k
      if(c > 127)
27
200
         return false;
28
29
1.84k
      if((std::isalnum(c) || c == '.' || c == ':' || c == '/' || c == '-') == false)
30
258
         return false;
31
1.84k
      }
32
249
   return true;
33
707
   }
34
35
/*
36
* Special hack to handle GeneralName [2] and [6] (DNS name and URI)
37
*/
38
bool possibly_a_general_name(const uint8_t bits[], size_t bits_len)
39
3.83k
   {
40
3.83k
   if(bits_len <= 2)
41
2.52k
      return false;
42
43
1.30k
   if(bits[0] != 0x82 && bits[0] != 0x86)
44
518
      return false;
45
46
786
   if(bits[1] != bits_len - 2)
47
79
      return false;
48
49
707
   if(all_printable_chars(bits + 2, bits_len - 2) == false)
50
458
      return false;
51
52
249
   return true;
53
249
   }
54
55
}
56
57
std::string ASN1_Formatter::print(const uint8_t in[], size_t len) const
58
0
   {
59
0
   std::ostringstream output;
60
0
   print_to_stream(output, in, len);
61
0
   return output.str();
62
0
   }
63
64
void ASN1_Formatter::print_to_stream(std::ostream& output,
65
                                     const uint8_t in[],
66
                                     size_t len) const
67
2.86k
   {
68
2.86k
   BER_Decoder dec(in, len);
69
2.86k
   decode(output, dec, 0);
70
2.86k
   }
71
72
void ASN1_Formatter::decode(std::ostream& output,
73
                            BER_Decoder& decoder,
74
                            size_t level) const
75
19.9k
   {
76
19.9k
   BER_Object obj = decoder.get_next_object();
77
78
19.9k
   const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
79
80
87.8k
   while(obj.is_set())
81
67.8k
      {
82
67.8k
      const ASN1_Type type_tag = obj.type();
83
67.8k
      const ASN1_Class class_tag = obj.get_class();
84
67.8k
      const size_t length = obj.length();
85
86
      /* hack to insert the tag+length back in front of the stuff now
87
         that we've gotten the type info */
88
67.8k
      std::vector<uint8_t> bits;
89
67.8k
      DER_Encoder(bits).add_object(type_tag, class_tag, obj.bits(), obj.length());
90
91
67.8k
      BER_Decoder data(bits);
92
93
67.8k
      if(intersects(class_tag, ASN1_Class::Constructed))
94
4.58k
         {
95
4.58k
         BER_Decoder cons_info(obj.bits(), obj.length());
96
97
4.58k
         if(recurse_deeper)
98
4.38k
            {
99
4.38k
            output << format(type_tag, class_tag, level, length, "");
100
4.38k
            decode(output, cons_info, level + 1); // recurse
101
4.38k
            }
102
201
         else
103
201
            {
104
201
            output << format(type_tag, class_tag, level, length,
105
201
                             format_bin(type_tag, class_tag, bits));
106
201
            }
107
4.58k
         }
108
63.2k
      else if(intersects(class_tag, ASN1_Class::Application) || intersects(class_tag, ASN1_Class::ContextSpecific))
109
3.83k
         {
110
3.83k
         bool success_parsing_cs = false;
111
112
3.83k
         if(m_print_context_specific)
113
3.83k
            {
114
3.83k
            try
115
3.83k
               {
116
3.83k
               if(possibly_a_general_name(bits.data(), bits.size()))
117
249
                  {
118
249
                  output << format(type_tag, class_tag, level, level,
119
249
                                   std::string(cast_uint8_ptr_to_char(&bits[2]), bits.size() - 2));
120
249
                  success_parsing_cs = true;
121
249
                  }
122
3.58k
               else if(recurse_deeper)
123
3.37k
                  {
124
3.37k
                  std::vector<uint8_t> inner_bits;
125
3.37k
                  data.decode(inner_bits, type_tag);
126
127
3.37k
                  BER_Decoder inner(inner_bits);
128
3.37k
                  std::ostringstream inner_data;
129
3.37k
                  decode(inner_data, inner, level + 1); // recurse
130
3.37k
                  output << inner_data.str();
131
3.37k
                  success_parsing_cs = true;
132
3.37k
                  }
133
3.83k
               }
134
3.83k
            catch(...)
135
3.37k
               {
136
3.37k
               }
137
3.83k
            }
138
139
3.83k
         if(success_parsing_cs == false)
140
3.58k
            {
141
3.58k
            output << format(type_tag, class_tag, level, length,
142
3.58k
                             format_bin(type_tag, class_tag, bits));
143
3.58k
            }
144
3.83k
         }
145
59.4k
      else if(type_tag == ASN1_Type::ObjectId)
146
5.47k
         {
147
5.47k
         OID oid;
148
5.47k
         data.decode(oid);
149
150
5.47k
         std::string out = OIDS::oid2str_or_empty(oid);
151
5.47k
         if(out.empty())
152
3.88k
            {
153
3.88k
            out = oid.to_string();
154
3.88k
            }
155
1.58k
         else
156
1.58k
            {
157
1.58k
            out += " [" + oid.to_string() + "]";
158
1.58k
            }
159
160
5.47k
         output << format(type_tag, class_tag, level, length, out);
161
5.47k
         }
162
53.9k
      else if(type_tag == ASN1_Type::Integer || type_tag == ASN1_Type::Enumerated)
163
3.75k
         {
164
3.75k
         BigInt number;
165
166
3.75k
         if(type_tag == ASN1_Type::Integer)
167
3.34k
            {
168
3.34k
            data.decode(number);
169
3.34k
            }
170
407
         else if(type_tag == ASN1_Type::Enumerated)
171
407
            {
172
407
            data.decode(number, ASN1_Type::Enumerated, class_tag);
173
407
            }
174
175
3.75k
         std::vector<uint8_t> rep = BigInt::encode(number);
176
3.75k
         if(rep.empty()) // if zero
177
1.02k
            rep.resize(1);
178
179
3.75k
         output << format(type_tag, class_tag, level, length, hex_encode(rep));
180
3.75k
         }
181
50.2k
      else if(type_tag == ASN1_Type::Boolean)
182
627
         {
183
627
         bool boolean;
184
627
         data.decode(boolean);
185
381
         output << format(type_tag, class_tag, level, length, (boolean ? "true" : "false"));
186
627
         }
187
49.5k
      else if(type_tag == ASN1_Type::Null)
188
211
         {
189
211
         output << format(type_tag, class_tag, level, length, "");
190
211
         }
191
49.3k
      else if(type_tag == ASN1_Type::OctetString || type_tag == ASN1_Type::BitString)
192
13.4k
         {
193
13.4k
         std::vector<uint8_t> decoded_bits;
194
13.4k
         data.decode(decoded_bits, type_tag);
195
13.4k
         bool printing_octet_string_worked = false;
196
197
13.4k
         if(recurse_deeper)
198
12.7k
            {
199
12.7k
            try
200
12.7k
               {
201
12.7k
               BER_Decoder inner(decoded_bits);
202
203
12.7k
               std::ostringstream inner_data;
204
12.7k
               decode(inner_data, inner, level + 1); // recurse
205
206
12.7k
               output << format(type_tag, class_tag, level, length, "");
207
12.7k
               output << inner_data.str();
208
12.7k
               printing_octet_string_worked = true;
209
12.7k
               }
210
12.7k
            catch(...)
211
7.49k
               {
212
7.49k
               }
213
12.7k
            }
214
215
13.4k
         if(!printing_octet_string_worked)
216
7.69k
            {
217
7.69k
            output << format(type_tag, class_tag, level, length,
218
7.69k
                             format_bin(type_tag, class_tag, decoded_bits));
219
7.69k
            }
220
13.4k
         }
221
35.9k
      else if(ASN1_String::is_string_type(type_tag))
222
3.21k
         {
223
3.21k
         ASN1_String str;
224
3.21k
         data.decode(str);
225
3.21k
         output << format(type_tag, class_tag, level, length, str.value());
226
3.21k
         }
227
32.7k
      else if(type_tag == ASN1_Type::UtcTime || type_tag == ASN1_Type::GeneralizedTime)
228
8.61k
         {
229
8.61k
         ASN1_Time time;
230
8.61k
         data.decode(time);
231
8.61k
         output << format(type_tag, class_tag, level, length, time.readable_string());
232
8.61k
         }
233
24.1k
      else
234
24.1k
         {
235
24.1k
         output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag)
236
24.1k
                << " type=" << static_cast<int>(type_tag) << "\n";
237
24.1k
         }
238
239
67.8k
      obj = decoder.get_next_object();
240
67.8k
      }
241
19.9k
   }
242
243
namespace {
244
245
std::string format_type(ASN1_Type type_tag, ASN1_Class class_tag)
246
0
   {
247
0
   if(class_tag == ASN1_Class::Universal)
248
0
      return asn1_tag_to_string(type_tag);
249
250
0
   if(class_tag == ASN1_Class::Constructed && (type_tag == ASN1_Type::Sequence || type_tag == ASN1_Type::Set))
251
0
      return asn1_tag_to_string(type_tag);
252
253
0
   std::string name;
254
255
0
   if(intersects(class_tag, ASN1_Class::Constructed))
256
0
      name += "cons ";
257
258
0
   name += "[" + std::to_string(static_cast<uint32_t>(type_tag)) + "]";
259
260
0
   if(intersects(class_tag, ASN1_Class::Application))
261
0
      {
262
0
      name += " appl";
263
0
      }
264
0
   if(intersects(class_tag, ASN1_Class::ContextSpecific))
265
0
      {
266
0
      name += " context";
267
0
      }
268
269
0
   return name;
270
0
   }
271
272
}
273
274
std::string ASN1_Pretty_Printer::format(ASN1_Type type_tag,
275
                                        ASN1_Class class_tag,
276
                                        size_t level,
277
                                        size_t length,
278
                                        const std::string& value) const
279
0
   {
280
0
   bool should_skip = false;
281
282
0
   if(value.length() > m_print_limit)
283
0
      {
284
0
      should_skip = true;
285
0
      }
286
287
0
   if((type_tag == ASN1_Type::OctetString || type_tag == ASN1_Type::BitString) &&
288
0
      value.length() > m_print_binary_limit)
289
0
      {
290
0
      should_skip = true;
291
0
      }
292
293
0
   level += m_initial_level;
294
295
0
   std::ostringstream oss;
296
297
0
   oss << "  d=" << std::setw(2) << level
298
0
       << ", l=" << std::setw(4) << length << ":"
299
0
       << std::string(level + 1, ' ') << format_type(type_tag, class_tag);
300
301
0
   if(value != "" && !should_skip)
302
0
      {
303
0
      const size_t current_pos = static_cast<size_t>(oss.tellp());
304
0
      const size_t spaces_to_align =
305
0
         (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
306
307
0
      oss << std::string(spaces_to_align, ' ') << value;
308
0
      }
309
310
0
   oss << "\n";
311
312
0
   return oss.str();
313
0
   }
314
315
std::string ASN1_Pretty_Printer::format_bin(ASN1_Type /*type_tag*/,
316
                                            ASN1_Class /*class_tag*/,
317
                                            const std::vector<uint8_t>& vec) const
318
0
   {
319
0
   if(all_printable_chars(vec.data(), vec.size()))
320
0
      {
321
0
      return std::string(cast_uint8_ptr_to_char(vec.data()), vec.size());
322
0
      }
323
0
   else
324
0
      return hex_encode(vec);
325
0
   }
326
327
}