Botan  2.4.0
Crypto and TLS for C++11
asn1_print.cpp
Go to the documentation of this file.
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  {
25  for(size_t i = 0; i != bits_len; ++i)
26  {
27  int c = bits[i];
28  if(c > 127)
29  return false;
30 
31  if((std::isalnum(c) || c == '.' || c == ':' || c == '/' || c == '-') == false)
32  return false;
33  }
34  return true;
35  }
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  {
42  if(bits_len <= 2)
43  return false;
44 
45  if(bits[0] != 0x82 && bits[0] != 0x86)
46  return false;
47 
48  if(bits[1] != bits_len - 2)
49  return false;
50 
51  if(all_printable_chars(bits + 2, bits_len - 2) == false)
52  return false;
53 
54  return true;
55  }
56 
57 }
58 
59 std::string ASN1_Formatter::print(const uint8_t in[], size_t len) const
60  {
61  std::ostringstream output;
62  print_to_stream(output, in, len);
63  return output.str();
64  }
65 
66 void ASN1_Formatter::print_to_stream(std::ostream& output,
67  const uint8_t in[],
68  size_t len) const
69  {
70  BER_Decoder dec(in, len);
71  decode(output, dec, 0);
72  }
73 
74 void ASN1_Formatter::decode(std::ostream& output,
75  BER_Decoder& decoder,
76  size_t level) const
77  {
78  BER_Object obj = decoder.get_next_object();
79 
80  while(obj.type_tag != NO_OBJECT)
81  {
82  const ASN1_Tag type_tag = obj.type_tag;
83  const ASN1_Tag class_tag = obj.class_tag;
84  const size_t length = obj.value.size();
85 
86  /* hack to insert the tag+length back in front of the stuff now
87  that we've gotten the type info */
88  DER_Encoder encoder;
89  encoder.add_object(type_tag, class_tag, obj.value);
90  const std::vector<uint8_t> bits = encoder.get_contents_unlocked();
91 
92  BER_Decoder data(bits);
93 
94  if(class_tag & CONSTRUCTED)
95  {
96  BER_Decoder cons_info(obj.value);
97  output << format(type_tag, class_tag, level, length, "");
98  decode(output, cons_info, level + 1); // recurse
99  }
100  else if((class_tag & APPLICATION) || (class_tag & CONTEXT_SPECIFIC))
101  {
102  bool success_parsing_cs = false;
103 
104  if(m_print_context_specific)
105  {
106  try
107  {
108  if(possibly_a_general_name(bits.data(), bits.size()))
109  {
110  output << format(type_tag, class_tag, level, level,
111  std::string(cast_uint8_ptr_to_char(&bits[2]), bits.size() - 2));
112  success_parsing_cs = true;
113  }
114  else
115  {
116  std::vector<uint8_t> inner_bits;
117  data.decode(inner_bits, type_tag);
118 
119  BER_Decoder inner(inner_bits);
120  std::ostringstream inner_data;
121  decode(inner_data, inner, level + 1); // recurse
122  output << inner_data.str();
123  success_parsing_cs = true;
124  }
125  }
126  catch(...)
127  {
128  }
129  }
130 
131  if(success_parsing_cs == false)
132  {
133  output << format(type_tag, class_tag, level, length,
134  format_bin(type_tag, class_tag, bits));
135  }
136  }
137  else if(type_tag == OBJECT_ID)
138  {
139  OID oid;
140  data.decode(oid);
141 
142  std::string out = OIDS::lookup(oid);
143  if(out.empty())
144  {
145  out = oid.as_string();
146  }
147  else
148  {
149  out += " [" + oid.as_string() + "]";
150  }
151 
152  output << format(type_tag, class_tag, level, length, out);
153  }
154  else if(type_tag == INTEGER || type_tag == ENUMERATED)
155  {
156  BigInt number;
157 
158  if(type_tag == INTEGER)
159  {
160  data.decode(number);
161  }
162  else if(type_tag == ENUMERATED)
163  {
164  data.decode(number, ENUMERATED, class_tag);
165  }
166 
167  const std::vector<uint8_t> rep = BigInt::encode(number, BigInt::Hexadecimal);
168 
169  std::string str;
170  for(size_t i = 0; i != rep.size(); ++i)
171  {
172  str += static_cast<char>(rep[i]);
173  }
174 
175  output << format(type_tag, class_tag, level, length, str);
176  }
177  else if(type_tag == BOOLEAN)
178  {
179  bool boolean;
180  data.decode(boolean);
181  output << format(type_tag, class_tag, level, length, (boolean ? "true" : "false"));
182  }
183  else if(type_tag == NULL_TAG)
184  {
185  output << format(type_tag, class_tag, level, length, "");
186  }
187  else if(type_tag == OCTET_STRING || type_tag == BIT_STRING)
188  {
189  std::vector<uint8_t> decoded_bits;
190  data.decode(decoded_bits, type_tag);
191 
192  try
193  {
194  BER_Decoder inner(decoded_bits);
195 
196  std::ostringstream inner_data;
197  decode(inner_data, inner, level + 1); // recurse
198 
199  output << format(type_tag, class_tag, level, length, "");
200  output << inner_data.str();
201  }
202  catch(...)
203  {
204  output << format(type_tag, class_tag, level, length,
205  format_bin(type_tag, class_tag, decoded_bits));
206  }
207  }
208  else if(ASN1_String::is_string_type(type_tag))
209  {
210  ASN1_String str;
211  data.decode(str);
212  output << format(type_tag, class_tag, level, length, str.value());
213  }
214  else if(type_tag == UTC_TIME || type_tag == GENERALIZED_TIME)
215  {
216  X509_Time time;
217  data.decode(time);
218  output << format(type_tag, class_tag, level, length, time.readable_string());
219  }
220  else
221  {
222  output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag)
223  << " type=" << static_cast<int>(type_tag) << "\n";;
224  }
225 
226  obj = decoder.get_next_object();
227  }
228  }
229 
230 namespace {
231 
232 std::string format_type(ASN1_Tag type_tag, ASN1_Tag class_tag)
233  {
234  if(class_tag == UNIVERSAL)
235  return asn1_tag_to_string(type_tag);
236 
237  if(class_tag == CONSTRUCTED && (type_tag == SEQUENCE || type_tag == SET))
238  return asn1_tag_to_string(type_tag);
239 
240  std::string name;
241 
242  if(class_tag & CONSTRUCTED)
243  name += "cons ";
244 
245  name += "[" + std::to_string(type_tag) + "]";
246 
247  if(class_tag & APPLICATION)
248  {
249  name += " appl";
250  }
251  if(class_tag & CONTEXT_SPECIFIC)
252  {
253  name += " context";
254  }
255 
256  return name;
257  }
258 
259 }
260 
261 std::string ASN1_Pretty_Printer::format(ASN1_Tag type_tag,
262  ASN1_Tag class_tag,
263  size_t level,
264  size_t length,
265  const std::string& value) const
266  {
267  bool should_skip = false;
268 
269  if(value.length() > m_print_limit)
270  {
271  should_skip = true;
272  }
273 
274  if((type_tag == OCTET_STRING || type_tag == BIT_STRING) &&
275  value.length() > m_print_binary_limit)
276  {
277  should_skip = true;
278  }
279 
280  level += m_initial_level;
281 
282  std::ostringstream oss;
283 
284  oss << " d=" << std::setw(2) << level
285  << ", l=" << std::setw(4) << length << ":"
286  << std::string(level + 1, ' ') << format_type(type_tag, class_tag);
287 
288  if(value != "" && !should_skip)
289  {
290  const size_t current_pos = static_cast<size_t>(oss.tellp());
291  const size_t spaces_to_align =
292  (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
293 
294  oss << std::string(spaces_to_align, ' ') << value;
295  }
296 
297  oss << "\n";
298 
299  return oss.str();
300  }
301 
302 std::string ASN1_Pretty_Printer::format_bin(ASN1_Tag /*type_tag*/,
303  ASN1_Tag /*class_tag*/,
304  const std::vector<uint8_t>& vec) const
305  {
306  if(all_printable_chars(vec.data(), vec.size()))
307  {
308  return std::string(cast_uint8_ptr_to_char(vec.data()), vec.size());
309  }
310  else
311  return hex_encode(vec);
312  }
313 
314 }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
Definition: der_enc.cpp:347
static bool is_string_type(ASN1_Tag tag)
Definition: asn1_str.cpp:68
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
std::string as_string() const
Definition: asn1_oid.cpp:50
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:15
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:355
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
ASN1_Tag
Definition: asn1_obj.h:22
secure_vector< uint8_t > value
Definition: asn1_obj.h:97
std::string readable_string() const
Returns a human friendly string replesentation of no particular formatting.
Definition: asn1_time.cpp:93
std::string print(const uint8_t in[], size_t len) const
Definition: asn1_print.cpp:59
Definition: alg_id.cpp:13
BER_Object get_next_object()
Definition: ber_dec.cpp:197
const std::string & value() const
Definition: asn1_str.h:27
ASN1_Tag class_tag
Definition: asn1_obj.h:94
ASN1_Tag type_tag
Definition: asn1_obj.h:94
virtual std::string format(ASN1_Tag type_tag, ASN1_Tag class_tag, size_t level, size_t length, const std::string &value) const =0
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:125
virtual std::string format_bin(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::vector< uint8_t > &vec) const =0
void print_to_stream(std::ostream &out, const uint8_t in[], size_t len) const
Definition: asn1_print.cpp:66
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54
std::string lookup(const OID &oid)
Definition: oids.cpp:18