Coverage Report

Created: 2020-02-14 15:38

/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
647
   {
25
1.74k
   for(size_t i = 0; i != bits_len; ++i)
26
1.52k
      {
27
1.52k
      int c = bits[i];
28
1.52k
      if(c > 127)
29
202
         return false;
30
1.32k
31
1.32k
      if((std::isalnum(c) || c == '.' || c == ':' || c == '/' || c == '-') == false)
32
219
         return false;
33
1.32k
      }
34
647
   return true;
35
647
   }
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.31k
   {
42
3.31k
   if(bits_len <= 2)
43
1.95k
      return false;
44
1.36k
45
1.36k
   if(bits[0] != 0x82 && bits[0] != 0x86)
46
624
      return false;
47
743
48
743
   if(bits[1] != bits_len - 2)
49
96
      return false;
50
647
51
647
   if(all_printable_chars(bits + 2, bits_len - 2) == false)
52
421
      return false;
53
226
54
226
   return true;
55
226
   }
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.70k
   {
70
2.70k
   BER_Decoder dec(in, len);
71
2.70k
   decode(output, dec, 0);
72
2.70k
   }
73
74
void ASN1_Formatter::decode(std::ostream& output,
75
                            BER_Decoder& decoder,
76
                            size_t level) const
77
20.0k
   {
78
20.0k
   BER_Object obj = decoder.get_next_object();
79
20.0k
80
20.0k
   const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
81
20.0k
82
95.5k
   while(obj.is_set())
83
75.5k
      {
84
75.5k
      const ASN1_Tag type_tag = obj.type();
85
75.5k
      const ASN1_Tag class_tag = obj.get_class();
86
75.5k
      const size_t length = obj.length();
87
75.5k
88
75.5k
      /* hack to insert the tag+length back in front of the stuff now
89
75.5k
         that we've gotten the type info */
90
75.5k
      std::vector<uint8_t> bits;
91
75.5k
      DER_Encoder(bits).add_object(type_tag, class_tag, obj.bits(), obj.length());
92
75.5k
93
75.5k
      BER_Decoder data(bits);
94
75.5k
95
75.5k
      if(class_tag & CONSTRUCTED)
96
4.64k
         {
97
4.64k
         BER_Decoder cons_info(obj.bits(), obj.length());
98
4.64k
99
4.64k
         if(recurse_deeper)
100
4.44k
            {
101
4.44k
            output << format(type_tag, class_tag, level, length, "");
102
4.44k
            decode(output, cons_info, level + 1); // recurse
103
4.44k
            }
104
202
         else
105
202
            {
106
202
            output << format(type_tag, class_tag, level, length,
107
202
                             format_bin(type_tag, class_tag, bits));
108
202
            }
109
4.64k
         }
110
70.9k
      else if((class_tag & APPLICATION) || (class_tag & CONTEXT_SPECIFIC))
111
3.31k
         {
112
3.31k
         bool success_parsing_cs = false;
113
3.31k
114
3.31k
         if(m_print_context_specific)
115
3.31k
            {
116
3.31k
            try
117
3.31k
               {
118
3.31k
               if(possibly_a_general_name(bits.data(), bits.size()))
119
226
                  {
120
226
                  output << format(type_tag, class_tag, level, level,
121
226
                                   std::string(cast_uint8_ptr_to_char(&bits[2]), bits.size() - 2));
122
226
                  success_parsing_cs = true;
123
226
                  }
124
3.09k
               else if(recurse_deeper)
125
2.89k
                  {
126
2.89k
                  std::vector<uint8_t> inner_bits;
127
2.89k
                  data.decode(inner_bits, type_tag);
128
2.89k
129
2.89k
                  BER_Decoder inner(inner_bits);
130
2.89k
                  std::ostringstream inner_data;
131
2.89k
                  decode(inner_data, inner, level + 1); // recurse
132
2.89k
                  output << inner_data.str();
133
2.89k
                  success_parsing_cs = true;
134
2.89k
                  }
135
3.31k
               }
136
3.31k
            catch(...)
137
3.31k
               {
138
2.89k
               }
139
3.31k
            }
140
3.31k
141
3.31k
         if(success_parsing_cs == false)
142
3.09k
            {
143
3.09k
            output << format(type_tag, class_tag, level, length,
144
3.09k
                             format_bin(type_tag, class_tag, bits));
145
3.09k
            }
146
3.31k
         }
147
67.5k
      else if(type_tag == OBJECT_ID)
148
4.56k
         {
149
4.56k
         OID oid;
150
4.56k
         data.decode(oid);
151
4.56k
152
4.56k
         std::string out = OIDS::oid2str_or_empty(oid);
153
4.56k
         if(out.empty())
154
3.35k
            {
155
3.35k
            out = oid.to_string();
156
3.35k
            }
157
1.21k
         else
158
1.21k
            {
159
1.21k
            out += " [" + oid.to_string() + "]";
160
1.21k
            }
161
4.56k
162
4.56k
         output << format(type_tag, class_tag, level, length, out);
163
4.56k
         }
164
63.0k
      else if(type_tag == INTEGER || type_tag == ENUMERATED)
165
3.65k
         {
166
3.65k
         BigInt number;
167
3.65k
168
3.65k
         if(type_tag == INTEGER)
169
3.21k
            {
170
3.21k
            data.decode(number);
171
3.21k
            }
172
440
         else if(type_tag == ENUMERATED)
173
440
            {
174
440
            data.decode(number, ENUMERATED, class_tag);
175
440
            }
176
3.65k
177
3.65k
         std::vector<uint8_t> rep = BigInt::encode(number);
178
3.65k
         if(rep.empty()) // if zero
179
1.29k
            rep.resize(1);
180
3.65k
181
3.65k
         output << format(type_tag, class_tag, level, length, hex_encode(rep));
182
3.65k
         }
183
59.3k
      else if(type_tag == BOOLEAN)
184
630
         {
185
630
         bool boolean;
186
630
         data.decode(boolean);
187
630
         output << format(type_tag, class_tag, level, length, (boolean ? "true" : "false"));
188
630
         }
189
58.7k
      else if(type_tag == NULL_TAG)
190
375
         {
191
375
         output << format(type_tag, class_tag, level, length, "");
192
375
         }
193
58.3k
      else if(type_tag == OCTET_STRING || type_tag == BIT_STRING)
194
13.5k
         {
195
13.5k
         std::vector<uint8_t> decoded_bits;
196
13.5k
         data.decode(decoded_bits, type_tag);
197
13.5k
         bool printing_octet_string_worked = false;
198
13.5k
199
13.5k
         if(recurse_deeper)
200
12.8k
            {
201
12.8k
            try
202
12.8k
               {
203
12.8k
               BER_Decoder inner(decoded_bits);
204
12.8k
205
12.8k
               std::ostringstream inner_data;
206
12.8k
               decode(inner_data, inner, level + 1); // recurse
207
12.8k
208
12.8k
               output << format(type_tag, class_tag, level, length, "");
209
12.8k
               output << inner_data.str();
210
12.8k
               printing_octet_string_worked = true;
211
12.8k
               }
212
12.8k
            catch(...)
213
12.8k
               {
214
7.73k
               }
215
12.8k
            }
216
13.5k
217
13.5k
         if(!printing_octet_string_worked)
218
7.94k
            {
219
7.94k
            output << format(type_tag, class_tag, level, length,
220
7.94k
                             format_bin(type_tag, class_tag, decoded_bits));
221
7.94k
            }
222
13.5k
         }
223
44.7k
      else if(ASN1_String::is_string_type(type_tag))
224
2.97k
         {
225
2.97k
         ASN1_String str;
226
2.97k
         data.decode(str);
227
2.97k
         output << format(type_tag, class_tag, level, length, str.value());
228
2.97k
         }
229
41.8k
      else if(type_tag == UTC_TIME || type_tag == GENERALIZED_TIME)
230
9.25k
         {
231
9.25k
         X509_Time time;
232
9.25k
         data.decode(time);
233
9.25k
         output << format(type_tag, class_tag, level, length, time.readable_string());
234
9.25k
         }
235
32.5k
      else
236
32.5k
         {
237
32.5k
         output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag)
238
32.5k
                << " type=" << static_cast<int>(type_tag) << "\n";
239
32.5k
         }
240
75.5k
241
75.5k
      obj = decoder.get_next_object();
242
75.5k
      }
243
20.0k
   }
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
}