Coverage Report

Created: 2021-02-21 07:20

/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
685
   {
23
2.10k
   for(size_t i = 0; i != bits_len; ++i)
24
1.84k
      {
25
1.84k
      int c = bits[i];
26
1.84k
      if(c > 127)
27
202
         return false;
28
29
1.64k
      if((std::isalnum(c) || c == '.' || c == ':' || c == '/' || c == '-') == false)
30
218
         return false;
31
1.64k
      }
32
265
   return true;
33
685
   }
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.33k
      return false;
42
43
1.49k
   if(bits[0] != 0x82 && bits[0] != 0x86)
44
716
      return false;
45
46
781
   if(bits[1] != bits_len - 2)
47
96
      return false;
48
49
685
   if(all_printable_chars(bits + 2, bits_len - 2) == false)
50
420
      return false;
51
52
265
   return true;
53
265
   }
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.83k
   {
68
2.83k
   BER_Decoder dec(in, len);
69
2.83k
   decode(output, dec, 0);
70
2.83k
   }
71
72
void ASN1_Formatter::decode(std::ostream& output,
73
                            BER_Decoder& decoder,
74
                            size_t level) const
75
19.8k
   {
76
19.8k
   BER_Object obj = decoder.get_next_object();
77
78
19.8k
   const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
79
80
91.1k
   while(obj.is_set())
81
71.3k
      {
82
71.3k
      const ASN1_Type type_tag = obj.type();
83
71.3k
      const ASN1_Class class_tag = obj.get_class();
84
71.3k
      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
71.3k
      std::vector<uint8_t> bits;
89
71.3k
      DER_Encoder(bits).add_object(type_tag, class_tag, obj.bits(), obj.length());
90
91
71.3k
      BER_Decoder data(bits);
92
93
71.3k
      if(intersects(class_tag, ASN1_Class::Constructed))
94
4.64k
         {
95
4.64k
         BER_Decoder cons_info(obj.bits(), obj.length());
96
97
4.64k
         if(recurse_deeper)
98
4.44k
            {
99
4.44k
            output << format(type_tag, class_tag, level, length, "");
100
4.44k
            decode(output, cons_info, level + 1); // recurse
101
4.44k
            }
102
205
         else
103
205
            {
104
205
            output << format(type_tag, class_tag, level, length,
105
205
                             format_bin(type_tag, class_tag, bits));
106
205
            }
107
4.64k
         }
108
66.6k
      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
265
                  {
118
265
                  output << format(type_tag, class_tag, level, level,
119
265
                                   std::string(cast_uint8_ptr_to_char(&bits[2]), bits.size() - 2));
120
265
                  success_parsing_cs = true;
121
265
                  }
122
3.56k
               else if(recurse_deeper)
123
3.36k
                  {
124
3.36k
                  std::vector<uint8_t> inner_bits;
125
3.36k
                  data.decode(inner_bits, type_tag);
126
127
3.36k
                  BER_Decoder inner(inner_bits);
128
3.36k
                  std::ostringstream inner_data;
129
3.36k
                  decode(inner_data, inner, level + 1); // recurse
130
3.36k
                  output << inner_data.str();
131
3.36k
                  success_parsing_cs = true;
132
3.36k
                  }
133
3.83k
               }
134
3.83k
            catch(...)
135
3.36k
               {
136
3.36k
               }
137
3.83k
            }
138
139
3.83k
         if(success_parsing_cs == false)
140
3.56k
            {
141
3.56k
            output << format(type_tag, class_tag, level, length,
142
3.56k
                             format_bin(type_tag, class_tag, bits));
143
3.56k
            }
144
3.83k
         }
145
62.8k
      else if(type_tag == ASN1_Type::ObjectId)
146
4.44k
         {
147
4.44k
         OID oid;
148
4.44k
         data.decode(oid);
149
150
4.44k
         std::string out = OIDS::oid2str_or_empty(oid);
151
4.44k
         if(out.empty())
152
3.19k
            {
153
3.19k
            out = oid.to_string();
154
3.19k
            }
155
1.25k
         else
156
1.25k
            {
157
1.25k
            out += " [" + oid.to_string() + "]";
158
1.25k
            }
159
160
4.44k
         output << format(type_tag, class_tag, level, length, out);
161
4.44k
         }
162
58.4k
      else if(type_tag == ASN1_Type::Integer || type_tag == ASN1_Type::Enumerated)
163
3.69k
         {
164
3.69k
         BigInt number;
165
166
3.69k
         if(type_tag == ASN1_Type::Integer)
167
2.59k
            {
168
2.59k
            data.decode(number);
169
2.59k
            }
170
1.10k
         else if(type_tag == ASN1_Type::Enumerated)
171
1.10k
            {
172
1.10k
            data.decode(number, ASN1_Type::Enumerated, class_tag);
173
1.10k
            }
174
175
3.69k
         std::vector<uint8_t> rep = BigInt::encode(number);
176
3.69k
         if(rep.empty()) // if zero
177
1.44k
            rep.resize(1);
178
179
3.69k
         output << format(type_tag, class_tag, level, length, hex_encode(rep));
180
3.69k
         }
181
54.7k
      else if(type_tag == ASN1_Type::Boolean)
182
690
         {
183
690
         bool boolean;
184
690
         data.decode(boolean);
185
377
         output << format(type_tag, class_tag, level, length, (boolean ? "true" : "false"));
186
690
         }
187
54.0k
      else if(type_tag == ASN1_Type::Null)
188
216
         {
189
216
         output << format(type_tag, class_tag, level, length, "");
190
216
         }
191
53.8k
      else if(type_tag == ASN1_Type::OctetString || type_tag == ASN1_Type::BitString)
192
13.1k
         {
193
13.1k
         std::vector<uint8_t> decoded_bits;
194
13.1k
         data.decode(decoded_bits, type_tag);
195
13.1k
         bool printing_octet_string_worked = false;
196
197
13.1k
         if(recurse_deeper)
198
12.5k
            {
199
12.5k
            try
200
12.5k
               {
201
12.5k
               BER_Decoder inner(decoded_bits);
202
203
12.5k
               std::ostringstream inner_data;
204
12.5k
               decode(inner_data, inner, level + 1); // recurse
205
206
12.5k
               output << format(type_tag, class_tag, level, length, "");
207
12.5k
               output << inner_data.str();
208
12.5k
               printing_octet_string_worked = true;
209
12.5k
               }
210
12.5k
            catch(...)
211
7.20k
               {
212
7.20k
               }
213
12.5k
            }
214
215
13.1k
         if(!printing_octet_string_worked)
216
7.40k
            {
217
7.40k
            output << format(type_tag, class_tag, level, length,
218
7.40k
                             format_bin(type_tag, class_tag, decoded_bits));
219
7.40k
            }
220
13.1k
         }
221
40.6k
      else if(ASN1_String::is_string_type(type_tag))
222
3.16k
         {
223
3.16k
         ASN1_String str;
224
3.16k
         data.decode(str);
225
3.16k
         output << format(type_tag, class_tag, level, length, str.value());
226
3.16k
         }
227
37.4k
      else if(type_tag == ASN1_Type::UtcTime || type_tag == ASN1_Type::GeneralizedTime)
228
8.21k
         {
229
8.21k
         ASN1_Time time;
230
8.21k
         data.decode(time);
231
8.21k
         output << format(type_tag, class_tag, level, length, time.readable_string());
232
8.21k
         }
233
29.2k
      else
234
29.2k
         {
235
29.2k
         output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag)
236
29.2k
                << " type=" << static_cast<int>(type_tag) << "\n";
237
29.2k
         }
238
239
71.3k
      obj = decoder.get_next_object();
240
71.3k
      }
241
19.8k
   }
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
}