aboutsummaryrefslogtreecommitdiffstats
path: root/doc/examples/asn1.cpp
blob: 84fb6b276a20870bc6a68e917b24528509da3f38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
  A simple ASN.1 parser, similiar to 'dumpasn1' or 'openssl asn1parse', though
  without some of the bells and whistles of those. Primarily used for testing
  the BER decoder. The output format is modeled loosely on 'asn1parse -i'

  The output is actually less precise than the other decoders named, because
  the underlying BER_Decoder hides quite a bit from userspace, such as the use
  of indefinite length encodings (and the EOC markers). At some point it will
  also hide the constructed string types from the user, but right now you'll
  seem them as-is.

  Written by Jack Lloyd, November 9-10, 2003
    - Nov 22: Updated to new BER_Object format (tag -> class_tag/type_tag)
    - Nov 25: Much improved BIT STRING output
              Can deal with non-constructed taggings
              Can produce UTF-8 output

  This file is in the public domain.
*/

/*******************************************************************/

// Set this if your terminal understands UTF-8; otherwise output is in Latin-1
#define UTF8_TERMINAL 1

/*
   What level the outermost layer of stuff is at. Probably 0 or 1; asn1parse
   uses 0 as the outermost, while 1 makes more sense to me. 2+ doesn't make
   much sense at all.
*/
#define INITIAL_LEVEL 0

/*******************************************************************/

#include <botan/botan.h>
#include <botan/bigint.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/asn1_obj.h>
#include <botan/oids.h>
#include <botan/pem.h>
#include <botan/charset.h>
using namespace Botan;

#include <stdio.h>
#include <ctype.h>

void decode(BER_Decoder&, u32bit);
void emit(const std::string&, u32bit, u32bit, const std::string& = "");
std::string type_name(ASN1_Tag);

int main(int argc, char* argv[])
   {
   if(argc != 2)
      {
      printf("Usage: %s <file>\n", argv[0]);
      return 1;
      }

   try {
      LibraryInitializer init;

      DataSource_Stream in(argv[1]);

      if(!PEM_Code::matches(in))
         {
         BER_Decoder decoder(in);
         decode(decoder, INITIAL_LEVEL);
         }
      else
         {
         std::string label; // ignored
         BER_Decoder decoder(PEM_Code::decode(in, label));
         decode(decoder, INITIAL_LEVEL);
         }

   }
   catch(std::exception& e)
      {
      printf("%s\n", e.what());
      return 1;
      }
   return 0;
   }

void decode(BER_Decoder& decoder, u32bit level)
   {
   BER_Object obj = decoder.get_next_object();

   while(obj.type_tag != NO_OBJECT)
      {
      const ASN1_Tag type_tag = obj.type_tag;
      const ASN1_Tag class_tag = obj.class_tag;
      const u32bit length = obj.value.size();

      /* hack to insert the tag+length back in front of the stuff now
         that we've gotten the type info */
      DER_Encoder encoder;
      encoder.add_object(type_tag, class_tag, obj.value, obj.value.size());
      SecureVector<byte> bits = encoder.get_contents();

      BER_Decoder data(bits);

      if(class_tag & CONSTRUCTED)
         {
         BER_Decoder cons_info(obj.value);
         if(type_tag == SEQUENCE)
            {
            emit("SEQUENCE", level, length);
            decode(cons_info, level+1);
            }
         else if(type_tag == SET)
            {
            emit("SET", level, length);
            decode(cons_info, level+1);
            }
         else
            {
            std::string name;

            if((class_tag & APPLICATION) || (class_tag & CONTEXT_SPECIFIC) ||
               (class_tag & PRIVATE))
               name = "cons [" + to_string(type_tag) + "]";
            else
               name = type_name(type_tag) + " (cons)";

            emit(name, level, length);
            decode(cons_info, level+1);
            }
         }
      else if(class_tag == APPLICATION || class_tag == CONTEXT_SPECIFIC ||
              class_tag == PRIVATE)
         {
         bool not_text = false;

         for(u32bit j = 0; j != bits.size(); j++)
            if(!isgraph(bits[j]) && !isspace(bits[j]))
               not_text = true;

         Pipe pipe(((not_text) ? new Hex_Encoder : 0));
         pipe.process_msg(bits);
         emit("[" + to_string(type_tag) + "]", level, length,
              pipe.read_all_as_string());
         }
      else if(type_tag == OBJECT_ID)
         {
         OID oid;
         data.decode(oid);

         std::string out = OIDS::lookup(oid);
         if(out != oid.as_string())
            out += " [" + oid.as_string() + "]";

         emit(type_name(type_tag), level, length, out);
         }
      else if(type_tag == INTEGER)
         {
         BigInt number;
         data.decode(number);

         SecureVector<byte> rep;

         /* If it's small, it's probably a number, not a hash */
         if(number.bits() <= 16)
            rep = BigInt::encode(number, BigInt::Decimal);
         else
            rep = BigInt::encode(number, BigInt::Hexadecimal);

         std::string str;
         for(u32bit j = 0; j != rep.size(); j++)
            str += (char)rep[j];

         emit(type_name(type_tag), level, length, str);
         }
      else if(type_tag == BOOLEAN)
         {
         bool boolean;
         data.decode(boolean);
         emit(type_name(type_tag),
              level, length, (boolean ? "true" : "false"));
         }
      else if(type_tag == NULL_TAG)
         {
         emit(type_name(type_tag), level, length);
         }
      else if(type_tag == OCTET_STRING)
         {
         SecureVector<byte> bits;
         data.decode(bits, type_tag);
         bool not_text = false;

         for(u32bit j = 0; j != bits.size(); j++)
            if(!isgraph(bits[j]) && !isspace(bits[j]))
               not_text = true;

         Pipe pipe(((not_text) ? new Hex_Encoder : 0));
         pipe.process_msg(bits);
         emit(type_name(type_tag), level, length, pipe.read_all_as_string());
         }
      else if(type_tag == BIT_STRING)
         {
         SecureVector<byte> bits;
         data.decode(bits, type_tag);

         std::vector<bool> bit_set;

         for(u32bit j = 0; j != bits.size(); j++)
            for(u32bit k = 0; k != 8; k++)
               bit_set.push_back((bool)((bits[bits.size()-j-1] >> (7-k)) & 1));

         std::string bit_str;
         for(u32bit j = 0; j != bit_set.size(); j++)
            {
            bool the_bit = bit_set[bit_set.size()-j-1];

            if(!the_bit && bit_str.size() == 0)
               continue;
            bit_str += (the_bit ? "1" : "0");
            }

         emit(type_name(type_tag), level, length, bit_str);
         }
      else if(type_tag == PRINTABLE_STRING ||
              type_tag == NUMERIC_STRING ||
              type_tag == IA5_STRING ||
              type_tag == T61_STRING ||
              type_tag == VISIBLE_STRING ||
              type_tag == UTF8_STRING ||
              type_tag == BMP_STRING)
         {
         ASN1_String str;
         data.decode(str);
         if(UTF8_TERMINAL)
            emit(type_name(type_tag), level, length,
                 Charset::transcode(str.iso_8859(),
                                    LATIN1_CHARSET, UTF8_CHARSET));
         else
            emit(type_name(type_tag), level, length, str.iso_8859());
         }
      else if(type_tag == UTC_TIME || type_tag == GENERALIZED_TIME)
         {
         X509_Time time;
         data.decode(time);
         emit(type_name(type_tag), level, length, time.readable_string());
         }
      else
         fprintf(stderr, "Unknown tag: class=%02X, type=%02X\n",
                 class_tag, type_tag);

      obj = decoder.get_next_object();
      }
   }

void emit(const std::string& type, u32bit level, u32bit length,
          const std::string& value)
   {
   const u32bit LIMIT = 128;
   const u32bit BIN_LIMIT = 64;

   int written = 0;
   written += printf("  d=%2d, l=%4d: ", level, length);
   for(u32bit j = INITIAL_LEVEL; j != level; j++)
      written += printf(" ");
   written += printf("%s   ", type.c_str());

   bool should_skip = false;
   if(value.length() > LIMIT) should_skip = true;
   if((type == "OCTET STRING" || type == "BIT STRING") &&
      value.length() > BIN_LIMIT)
      should_skip = true;

   if(value != "" && !should_skip)
      {
      if(written % 2 == 0) printf(" ");
      while(written < 50) written += printf("  ");
      printf(":%s\n", value.c_str());
      }
   else
      printf("\n");
   }

std::string type_name(ASN1_Tag type)
   {
   if(type == PRINTABLE_STRING) return "PRINTABLE STRING";
   if(type == NUMERIC_STRING)   return "NUMERIC STRING";
   if(type == IA5_STRING)       return "IA5 STRING";
   if(type == T61_STRING)       return "T61 STRING";
   if(type == UTF8_STRING)      return "UTF8 STRING";
   if(type == VISIBLE_STRING)   return "VISIBLE STRING";
   if(type == BMP_STRING)       return "BMP STRING";

   if(type == UTC_TIME)         return "UTC TIME";
   if(type == GENERALIZED_TIME) return "GENERALIZED TIME";

   if(type == OCTET_STRING)     return "OCTET STRING";
   if(type == BIT_STRING)       return "BIT STRING";

   if(type == INTEGER)          return "INTEGER";
   if(type == NULL_TAG)         return "NULL";
   if(type == OBJECT_ID)        return "OBJECT";
   if(type == BOOLEAN)          return "BOOLEAN";
   return "(UNKNOWN)";
   }