Coverage Report

Created: 2020-05-23 13:54

/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/asn1_time.h>
13
#include <botan/asn1_str.h>
14
#include <botan/oids.h>
15
#include <iomanip>
16
#include <sstream>
17
#include <cctype>
18
19
namespace Botan {
20
21
namespace {
22
23
bool all_printable_chars(const uint8_t bits[], size_t bits_len)
24
643
   {
25
1.76k
   for(size_t i = 0; i != bits_len; ++i)
26
1.53k
      {
27
1.53k
      int c = bits[i];
28
1.53k
      if(c > 127)
29
200
         return false;
30
1.33k
31
1.33k
      if((std::isalnum(c) || c == '.' || c == ':' || c == '/' || c == '-') == false)
32
215
         return false;
33
1.33k
      }
34
643
   return true;
35
643
   }
36
37
/*
38
* Special hack to handle GeneralName [2] and [6] (DNS name and URI)
39
*/
40
bool possibly_a_general_name(const uint8_t bits[], size_t bits_len)
41
3.25k
   {
42
3.25k
   if(bits_len <= 2)
43
2.01k
      return false;
44
1.24k
45
1.24k
   if(bits[0] != 0x82 && bits[0] != 0x86)
46
522
      return false;
47
724
48
724
   if(bits[1] != bits_len - 2)
49
81
      return false;
50
643
51
643
   if(all_printable_chars(bits + 2, bits_len - 2) == false)
52
415
      return false;
53
228
54
228
   return true;
55
228
   }
56
57
}
58
59
std::string ASN1_Formatter::print(const uint8_t in[], size_t len) const
60
0
   {
61
0
   std::ostringstream output;
62
0
   print_to_stream(output, in, len);
63
0
   return output.str();
64
0
   }
65
66
void ASN1_Formatter::print_to_stream(std::ostream& output,
67
                                     const uint8_t in[],
68
                                     size_t len) const
69
2.51k
   {
70
2.51k
   BER_Decoder dec(in, len);
71
2.51k
   decode(output, dec, 0);
72
2.51k
   }
73
74
void ASN1_Formatter::decode(std::ostream& output,
75
                            BER_Decoder& decoder,
76
                            size_t level) const
77
19.3k
   {
78
19.3k
   BER_Object obj = decoder.get_next_object();
79
19.3k
80
19.3k
   const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
81
19.3k
82
89.9k
   while(obj.is_set())
83
70.6k
      {
84
70.6k
      const ASN1_Tag type_tag = obj.type();
85
70.6k
      const ASN1_Tag class_tag = obj.get_class();
86
70.6k
      const size_t length = obj.length();
87
70.6k
88
70.6k
      /* hack to insert the tag+length back in front of the stuff now
89
70.6k
         that we've gotten the type info */
90
70.6k
      std::vector<uint8_t> bits;
91
70.6k
      DER_Encoder(bits).add_object(type_tag, class_tag, obj.bits(), obj.length());
92
70.6k
93
70.6k
      BER_Decoder data(bits);
94
70.6k
95
70.6k
      if(class_tag & CONSTRUCTED)
96
4.49k
         {
97
4.49k
         BER_Decoder cons_info(obj.bits(), obj.length());
98
4.49k
99
4.49k
         if(recurse_deeper)
100
4.26k
            {
101
4.26k
            output << format(type_tag, class_tag, level, length, "");
102
4.26k
            decode(output, cons_info, level + 1); // recurse
103
4.26k
            }
104
224
         else
105
224
            {
106
224
            output << format(type_tag, class_tag, level, length,
107
224
                             format_bin(type_tag, class_tag, bits));
108
224
            }
109
4.49k
         }
110
66.1k
      else if((class_tag & APPLICATION) || (class_tag & CONTEXT_SPECIFIC))
111
3.25k
         {
112
3.25k
         bool success_parsing_cs = false;
113
3.25k
114
3.25k
         if(m_print_context_specific)
115
3.25k
            {
116
3.25k
            try
117
3.25k
               {
118
3.25k
               if(possibly_a_general_name(bits.data(), bits.size()))
119
228
                  {
120
228
                  output << format(type_tag, class_tag, level, level,
121
228
                                   std::string(cast_uint8_ptr_to_char(&bits[2]), bits.size() - 2));
122
228
                  success_parsing_cs = true;
123
228
                  }
124
3.03k
               else if(recurse_deeper)
125
2.81k
                  {
126
2.81k
                  std::vector<uint8_t> inner_bits;
127
2.81k
                  data.decode(inner_bits, type_tag);
128
2.81k
129
2.81k
                  BER_Decoder inner(inner_bits);
130
2.81k
                  std::ostringstream inner_data;
131
2.81k
                  decode(inner_data, inner, level + 1); // recurse
132
2.81k
                  output << inner_data.str();
133
2.81k
                  success_parsing_cs = true;
134
2.81k
                  }
135
3.25k
               }
136
3.25k
            catch(...)
137
3.25k
               {
138
2.81k
               }
139
3.25k
            }
140
3.25k
141
3.25k
         if(success_parsing_cs == false)
142
3.03k
            {
143
3.03k
            output << format(type_tag, class_tag, level, length,
144
3.03k
                             format_bin(type_tag, class_tag, bits));
145
3.03k
            }
146
3.25k
         }
147
62.8k
      else if(type_tag == OBJECT_ID)
148
5.25k
         {
149
5.25k
         OID oid;
150
5.25k
         data.decode(oid);
151
5.25k
152
5.25k
         std::string out = OIDS::oid2str_or_empty(oid);
153
5.25k
         if(out.empty())
154
3.98k
            {
155
3.98k
            out = oid.to_string();
156
3.98k
            }
157
1.27k
         else
158
1.27k
            {
159
1.27k
            out += " [" + oid.to_string() + "]";
160
1.27k
            }
161
5.25k
162
5.25k
         output << format(type_tag, class_tag, level, length, out);
163
5.25k
         }
164
57.6k
      else if(type_tag == INTEGER || type_tag == ENUMERATED)
165
3.69k
         {
166
3.69k
         BigInt number;
167
3.69k
168
3.69k
         if(type_tag == INTEGER)
169
3.27k
            {
170
3.27k
            data.decode(number);
171
3.27k
            }
172
418
         else if(type_tag == ENUMERATED)
173
418
            {
174
418
            data.decode(number, ENUMERATED, class_tag);
175
418
            }
176
3.69k
177
3.69k
         std::vector<uint8_t> rep = BigInt::encode(number);
178
3.69k
         if(rep.empty()) // if zero
179
1.34k
            rep.resize(1);
180
3.69k
181
3.69k
         output << format(type_tag, class_tag, level, length, hex_encode(rep));
182
3.69k
         }
183
53.9k
      else if(type_tag == BOOLEAN)
184
613
         {
185
613
         bool boolean;
186
613
         data.decode(boolean);
187
613
         output << format(type_tag, class_tag, level, length, (boolean ? "true" : "false"));
188
613
         }
189
53.3k
      else if(type_tag == NULL_TAG)
190
199
         {
191
199
         output << format(type_tag, class_tag, level, length, "");
192
199
         }
193
53.1k
      else if(type_tag == OCTET_STRING || type_tag == BIT_STRING)
194
13.2k
         {
195
13.2k
         std::vector<uint8_t> decoded_bits;
196
13.2k
         data.decode(decoded_bits, type_tag);
197
13.2k
         bool printing_octet_string_worked = false;
198
13.2k
199
13.2k
         if(recurse_deeper)
200
12.5k
            {
201
12.5k
            try
202
12.5k
               {
203
12.5k
               BER_Decoder inner(decoded_bits);
204
12.5k
205
12.5k
               std::ostringstream inner_data;
206
12.5k
               decode(inner_data, inner, level + 1); // recurse
207
12.5k
208
12.5k
               output << format(type_tag, class_tag, level, length, "");
209
12.5k
               output << inner_data.str();
210
12.5k
               printing_octet_string_worked = true;
211
12.5k
               }
212
12.5k
            catch(...)
213
12.5k
               {
214
7.59k
               }
215
12.5k
            }
216
13.2k
217
13.2k
         if(!printing_octet_string_worked)
218
7.79k
            {
219
7.79k
            output << format(type_tag, class_tag, level, length,
220
7.79k
                             format_bin(type_tag, class_tag, decoded_bits));
221
7.79k
            }
222
13.2k
         }
223
39.8k
      else if(ASN1_String::is_string_type(type_tag))
224
2.78k
         {
225
2.78k
         ASN1_String str;
226
2.78k
         data.decode(str);
227
2.78k
         output << format(type_tag, class_tag, level, length, str.value());
228
2.78k
         }
229
37.0k
      else if(type_tag == UTC_TIME || type_tag == GENERALIZED_TIME)
230
8.98k
         {
231
8.98k
         X509_Time time;
232
8.98k
         data.decode(time);
233
8.98k
         output << format(type_tag, class_tag, level, length, time.readable_string());
234
8.98k
         }
235
28.0k
      else
236
28.0k
         {
237
28.0k
         output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag)
238
28.0k
                << " type=" << static_cast<int>(type_tag) << "\n";
239
28.0k
         }
240
70.6k
241
70.6k
      obj = decoder.get_next_object();
242
70.6k
      }
243
19.3k
   }
244
245
namespace {
246
247
std::string format_type(ASN1_Tag type_tag, ASN1_Tag class_tag)
248
0
   {
249
0
   if(class_tag == UNIVERSAL)
250
0
      return asn1_tag_to_string(type_tag);
251
0
252
0
   if(class_tag == CONSTRUCTED && (type_tag == SEQUENCE || type_tag == SET))
253
0
      return asn1_tag_to_string(type_tag);
254
0
255
0
   std::string name;
256
0
257
0
   if(class_tag & CONSTRUCTED)
258
0
      name += "cons ";
259
0
260
0
   name += "[" + std::to_string(type_tag) + "]";
261
0
262
0
   if(class_tag & APPLICATION)
263
0
      {
264
0
      name += " appl";
265
0
      }
266
0
   if(class_tag & CONTEXT_SPECIFIC)
267
0
      {
268
0
      name += " context";
269
0
      }
270
0
271
0
   return name;
272
0
   }
273
274
}
275
276
std::string ASN1_Pretty_Printer::format(ASN1_Tag type_tag,
277
                                        ASN1_Tag class_tag,
278
                                        size_t level,
279
                                        size_t length,
280
                                        const std::string& value) const
281
0
   {
282
0
   bool should_skip = false;
283
0
284
0
   if(value.length() > m_print_limit)
285
0
      {
286
0
      should_skip = true;
287
0
      }
288
0
289
0
   if((type_tag == OCTET_STRING || type_tag == BIT_STRING) &&
290
0
      value.length() > m_print_binary_limit)
291
0
      {
292
0
      should_skip = true;
293
0
      }
294
0
295
0
   level += m_initial_level;
296
0
297
0
   std::ostringstream oss;
298
0
299
0
   oss << "  d=" << std::setw(2) << level
300
0
       << ", l=" << std::setw(4) << length << ":"
301
0
       << std::string(level + 1, ' ') << format_type(type_tag, class_tag);
302
0
303
0
   if(value != "" && !should_skip)
304
0
      {
305
0
      const size_t current_pos = static_cast<size_t>(oss.tellp());
306
0
      const size_t spaces_to_align =
307
0
         (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
308
0
309
0
      oss << std::string(spaces_to_align, ' ') << value;
310
0
      }
311
0
312
0
   oss << "\n";
313
0
314
0
   return oss.str();
315
0
   }
316
317
std::string ASN1_Pretty_Printer::format_bin(ASN1_Tag /*type_tag*/,
318
                                            ASN1_Tag /*class_tag*/,
319
                                            const std::vector<uint8_t>& vec) const
320
0
   {
321
0
   if(all_printable_chars(vec.data(), vec.size()))
322
0
      {
323
0
      return std::string(cast_uint8_ptr_to_char(vec.data()), vec.size());
324
0
      }
325
0
   else
326
0
      return hex_encode(vec);
327
0
   }
328
329
}