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
  
#pike __REAL_VERSION__ 
#pragma strict_types 
 
#ifdef ASN1_DEBUG 
#define DBG werror 
#else 
#define DBG(X ...) 
#endif 
 
//! Decodes a DER object. 
 
//! Primitive unconstructed ASN1 data type. 
class Primitive (int cls, int tag, string(8bit) raw) 
{ 
  //! @decl inherit Types.Object 
  inherit .Types.Object; 
 
  //! Get raw encoded contents of object 
  string(8bit) get_der_content() { return raw; } 
 
  protected string _sprintf(int t) { 
    return t=='O' && sprintf("%O(%d)", this_program, get_combined_tag()); 
  } 
 
  array _encode() 
  { 
    return ({ cls, tag, raw }); 
  } 
 
  void _decode(array(int|string(8bit)) x) 
  { 
    [ cls, tag, raw ] = x; 
  } 
} 
 
//! Constructed type 
class Constructed (int cls, int tag, string(8bit) raw, array(.Types.Object) elements) 
{ 
  inherit .Types.Compound; 
  constant type_name = "CONSTRUCTED"; 
 
  //! Get raw encoded contents of object 
  string(8bit) get_der_content() { return raw; } 
} 
 
protected int read_varint(Stdio.Buffer data) 
{ 
  int ret, byte; 
  do { 
    ret <<= 7; 
    byte = data->read_int8(); 
    ret |= (byte & 0x7f); 
  } while (byte & 0x80); 
  return ret; 
} 
 
// @returns 
// @array 
//   @elem int 1 
//     Class 
//   @elem bool 2 
//     Constructed 
//   @elem int 3 
//     Tag 
// @endarray 
protected array(int) read_identifier(Stdio.Buffer data) 
{ 
  int byte = data->read_int8(); 
 
  int cls = byte >> 6; 
  int constructed = (byte >> 5) & 1; 
  int tag = byte & 0x1f; 
 
  if( tag == 0x1f ) 
    tag = read_varint(data); 
 
  return ({ cls, constructed, tag }); 
} 
 
//! @param data 
//!   An instance of Stdio.Buffer containing the DER encoded data. 
//! 
//! @param types 
//!   A mapping from combined tag numbers to classes from or derived 
//!   from @[Standards.ASN1.Types]. Combined tag numbers may be 
//!   generated using @[Standards.ASN1.Types.make_combined_tag]. 
//! 
//! @returns 
//!   An object from @[Standards.ASN1.Types] or, either 
//!   @[Standards.ASN1.Decode.Primitive] or 
//!   @[Standards.ASN1.Decode.constructed], if the type is unknown. 
//!   Throws an exception if the data could not be decoded. 
//! 
//! @fixme 
//!   Handling of implicit and explicit ASN.1 tagging, as well as 
//!   other context dependence, is next to non_existant. 
.Types.Object der_decode(Stdio.Buffer data, 
                         mapping(int:program(.Types.Object)) types) 
{ 
  [int(0..3) cls, int constructed, int(0..) tag] = read_identifier(data); 
 
  int len = data->read_int8(); 
  // if( !cls && !constructed && !tag && !len ) 
  //   error("End-of-contents not supported.\n"); 
  if( !cls && !tag ) 
    error("Tag 0 reserved for BER encoding.\n"); 
  if (len & 0x80) 
  { 
    if (len == 0xff) 
      error("Illegal size.\n"); 
    if (len == 0x80) 
      error("Indefinite length form not supported.\n"); 
    len = data->read_int(len & 0x7f); 
  } 
  DBG("class %O, constructed=%d, tag=%d, length=%d\n", 
      ({"universal","application","context","private"})[cls], 
      constructed, tag, len); 
 
  data = [object(Stdio.Buffer)]data->read_buffer(len); 
 
  program(.Types.Object) p = types[ .Types.make_combined_tag(cls, tag) ]; 
 
  if (constructed) 
  { 
    DBG("Decoding constructed %O\n", p); 
 
    if (!p) 
    { 
      array(.Types.Object) elements = ({ }); 
 
      while (sizeof(data)) 
        elements += ({ der_decode(data, types) }); 
 
      if (cls==2 && sizeof(elements)==1) 
      { 
        // Context-specific constructed compound with a single element. 
        // ==> Probably a TaggedType. 
        DBG("Probable tagged type.\n"); 
        return .Types.MetaExplicit(2, tag)(elements[0]); 
      } 
 
      DBG("Unknown constructed type.\n"); 
      return Constructed(cls, tag, (string(8bit))data, elements); 
    } 
 
    .Types.Object res = p(); 
    res->cls = cls; 
    res->tag = tag; 
    res->begin_decode_constructed((string(8bit))data); 
 
    // Ask object which types it expects for field i, decode it, and 
    // record the decoded object 
    for(int i = 0; sizeof(data); i++) 
    { 
      DBG("Element %d\n", i); 
      res->decode_constructed_element 
        (i, der_decode(data, 
                       res->element_types(i, types))); 
    } 
    return res; 
  } 
 
  DBG("Decoding Primitive\n"); 
 
  if (p) 
  { 
    .Types.Object res = p(); 
    res->cls = cls; 
    res->tag = tag; 
    return res->decode_primitive((string(8bit))data, this, types); 
  } 
 
  return Primitive(cls, tag, (string(8bit))data); 
} 
 
#define U(x) .Types.make_combined_tag(0, (x)) 
 
mapping(int:program(.Types.Object)) universal_types = 
([ U(1) : .Types.Boolean, 
   U(2) : .Types.Integer, 
   U(3) : .Types.BitString, 
   U(4) : .Types.OctetString, 
   U(5) : .Types.Null, 
   U(6) : .Types.Identifier, 
   // 7 : Object descriptor type 
   // 8 : External type / instance-of 
   U(9) : .Types.Real, 
   U(10) : .Types.Enumerated, 
   // 11 : Embedded-pdv 
   U(12) : .Types.UTF8String, 
   // 13 : Relative object identifier 
   // 14 : Time 
   // 15 : reserved 
   U(16) : .Types.Sequence, 
   U(17) : .Types.Set, 
   U(19) : .Types.PrintableString, 
   U(20) : .Types.BrokenTeletexString, 
   U(22) : .Types.IA5String, 
   U(23) : .Types.UTC, 
   U(24) : .Types.GeneralizedTime, 
   U(28) : .Types.UniversalString, 
   // 29 : UnrestrictedCharacterString 
   U(30) : .Types.BMPString, 
   // 31 : DATE 
   // 32 : TIME-OF-DAY 
   // 33 : DATE-TIME 
   // 34 : DURATION 
   // 35 : OID internationalized resource identifier 
   // 36 : Relative OID internationalized resource identifier 
  ]); 
 
//! decode a DER encoded object using universal data types 
//! 
//! @param data 
//!   a DER encoded object 
//! 
//! @param types 
//!   An optional set of application-specific types. Should map 
//!   combined tag numbers to classes from or derived from 
//!   @[Standards.ASN1.Types]. Combined tag numbers may be generated 
//!   using @[Standards.ASN1.Types.make_combined_tag]. This set is 
//!   used to extend @[universal_types]. 
//! 
//! @returns 
//!   an object from @[Standards.ASN1.Types] or 
//!   either @[Standards.ASN1.Decode.Primitive] or 
//!   @[Standards.ASN1.Decode.Constructed] if the type is unknown. 
.Types.Object simple_der_decode(string(0..255) data, 
                                mapping(int:program(.Types.Object))|void types) 
{ 
  types = types ? universal_types+types : universal_types; 
  return der_decode(Stdio.Buffer(data)->set_error_mode(1), types); 
} 
 
//! Works just like @[simple_der_decode], except it will return 
//! @expr{0@} if there is trailing data in the provided ASN.1 @[data]. 
//! 
//! @seealso 
//!   @[simple_der_decode] 
.Types.Object secure_der_decode(string(0..255) data, 
                                mapping(int:program(.Types.Object))|void types) 
{ 
  types = types ? universal_types+types : universal_types; 
  Stdio.Buffer buf = Stdio.Buffer(data); 
  .Types.Object ret = der_decode(buf, types); 
  if( sizeof(buf) ) return 0; 
  return ret; 
}