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
  
START_MARKER 
 
test_do( add_constant("Types", Standards.ASN1.Types); ) 
test_do( add_constant("P", lambda(object o) 
{ 
  switch(o->type_name) 
  { 
  case "OBJECT IDENTIFIER": 
  case "INTEGER": 
  case "BIT STRING": 
  case "BOOLEAN": 
  case "REAL": 
  case "NULL": 
  case "": 
    return sprintf("%O",o)-"Standards.ASN1."-"Types."; 
  default: 
    return o->type_name + "(" + o->value + ")"; 
  } 
}); ) 
 
test_eval_error(Standards.ASN1.Decode.simple_der_decode("")) 
 
define(test_decode,[[ 
  test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string($1))),$2) 
  test_eq(Standards.ASN1.Decode.simple_der_decode(String.hex2string($1))->get_der(), 
          String.hex2string($1)) 
  test_eq(decode_value(encode_value(Standards.ASN1.Decode.simple_der_decode(String.hex2string($1-" "))))->get_der(), 
          String.hex2string($1)) 
]]) 
 
test_decode("1f 7f 00", "Decode.Primitive(508)") 
test_decode("5f 7f 00", "Decode.Primitive(509)") 
test_decode("9f 7f 00", "Decode.Primitive(510)") 
test_decode("df 7f 00", "Decode.Primitive(511)") 
test_decode("9f ff 00 00", "Decode.Primitive(65026)") 
 
test_eval_error(Standards.ASN1.Decode.simple_der_decode("\0\xff")) 
test_eval_error(Standards.ASN1.Decode.simple_der_decode("\0\x80")) 
test_eval_error(Standards.ASN1.Decode.simple_der_decode("\0\0")) 
 
test_eq(Standards.ASN1.Decode.simple_der_decode(Types.OctetString("A"*127)->get_der())->value, 
  "A"*127) 
test_eq(Standards.ASN1.Decode.simple_der_decode(Types.OctetString("A"*128)->get_der())->value, 
  "A"*128) 
 
test_decode("010100","Boolean(FALSE)") 
test_decode("0101ff","Boolean(TRUE)") 
test_eval_error(Standards.ASN1.Decode.simple_der_decode("\1\0")) 
 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("090380fb05"))),"Real(0.15625)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("090390fe0a"))),"Real(0.15625)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("0903acfe05"))),"Real(0.15625)") 
 
test_decode("180f31393932303532313030303030305a", "GeneralizedTime(19920521000000Z)") 
test_decode("180f31393932303632323132333432315a", "GeneralizedTime(19920622123421Z)") 
test_decode("181131393932303732323133323130302e335a", "GeneralizedTime(19920722132100.3Z)") 
 
dnl 
dnl --- START tests from 
dnl A Layman's Guide to a Subset of ASN.1, BER, and DER 
dnl 
 
dnl 5.4 
test_decode("03 04 06 6e 5d c0", "BitString(18 011011100101110111)") 
test_false(Standards.ASN1.Decode.simple_der_decode(String.hex2string("03 04 06 6e 5d e0"))) 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("03 81 04 06 6e 5d c0"))), "BitString(18 011011100101110111)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("23 09 03 03 00 6e 5d 03 02 06 c0"))), "BitString(18 011011100101110111)") 
 
dnl 5.6 
test_decode("16 0d 74 65 73 74 31 40 72 73 61 2e 63 6f 6d", 
            "IA5STRING(test1@rsa.com)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("16 81 0d 74 65 73 74 31 40 72 73 61 2e 63 6f 6d"))), "IA5STRING(test1@rsa.com)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("36 13 16 05 74 65 73 74 31 16 01 40 16 07 72 73 61 2e 63 6f 6d"))), "IA5STRING(test1@rsa.com)") 
 
dnl 5.6 
test_decode("02 01 00", "Integer(0)") 
test_decode("02 01 7f", "Integer(7 127)") 
test_decode("02 02 00 80", "Integer(8 128)") 
test_decode("02 02 01 00", "Integer(9 256)") 
test_decode("02 01 80", "Integer(8 -128)") 
test_decode("02 02 ff 7f", "Integer(8 -129)") 
 
dnl 5.8 
test_decode("05 00", "Null()") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("05 81 00"))), "Null()") 
test_eq( Types.Null()->get_der(), "\5\0" ) 
 
dnl 5.9 
test_decode("06 06 2a 86 48 86 f7 0d", "Identifier(1.2.840.113549)") 
test_eq(Types.Identifier(1,2,840,113549)->get_der(), 
  String.hex2string("06062a864886f70d")) 
 
dnl 5.10 
test_decode("04 08 01 23 45 67 89 ab cd ef", "OCTET STRING(\1#Eg\211\253\315\357)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("04 81 08 01 23 45 67 89 ab cd ef"))), "OCTET STRING(\1#Eg\211\253\315\357)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("24 0c 04 04 01 23 45 67 04 04 89 ab cd ef"))), "OCTET STRING(\1#Eg\211\253\315\357)") 
 
dnl 5.11 
test_decode("13 0b 54 65 73 74 20 55 73 65 72 20 31", "PrintableString(Test User 1)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("13 81 0b 54 65 73 74 20 55 73 65 72 20 31"))), "PrintableString(Test User 1)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("33 0f 13 05 54 65 73 74 20 13 06 55 73 65 72 20 31"))), "PrintableString(Test User 1)") 
 
dnl 5.16 
dnl dnl FIXME: T61 is not encoded 
dnl dnl test_decode("14 0f 63 6c c2 65 73 20 70 75 62 6c 69 71 75 65 73", "TeletexString(cl\351s publiques)") 
dnl test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("14 81 0f 63 6c c2 65 73 20 70 75 62 6c 69 71 75 65 73"))), "TeletexString(cl\351s publiques)") 
dnl test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("34 15 14 05 63 6c c2 65 73 14 01 20 14 09 70 75 62 6c 69 71 75 65 73"))), "TeletexString(cl\351s publiques)") 
dnl 
test_decode("14 0e 63 6c e9 73 20 70 75 62 6c 69 71 75 65 73", "TeletexString(cl\351s publiques)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("14 81 0e 63 6c e9 73 20 70 75 62 6c 69 71 75 65 73"))), "TeletexString(cl\351s publiques)") 
test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("34 14 14 04 63 6c e9 73 14 01 20 14 09 70 75 62 6c 69 71 75 65 73"))), "TeletexString(cl\351s publiques)") 
 
dnl 5.17 
test_decode("17 0d 39 31 30 35 30 36 32 33 34 35 34 30 5a", "UTCTime(910506234540Z)") 
dnl FIXME: Should time be normalized? 
dnl test_eq(P(Standards.ASN1.Decode.simple_der_decode(String.hex2string("17 11 39 31 30 35 30 36 31 36 34 35 34 30 2D 30 37 30 30"))), "UTCTime(910506234540Z)") 
 
dnl 
dnl --- END tests from 
dnl A Layman's Guide to a Subset of ASN.1, BER, and DER) 
dnl 
 
dnl §8.9 
test_any([[ 
  import Types; 
  return Sequence( ({ IA5String("Smith"), Boolean(17) }) )->get_der(); 
]], "0\n\26\5Smith\1\1\xff") 
 
dnl §8.19 
test_eq(Types.Identifier(2,100,3)->get_der(), String.hex2string("0603813403")) 
 
dnl §8.21 
test_eq( Types.VisibleString("Jones")->get_der(), "\32\5Jones" ) 
 
test_eq( Types.BitString()->set_from_ascii("011011100101110111")->get_der(), 
  String.hex2string("0304066e5dc0") ) 
 
 
test_eq(Types.OctetString(String.hex2string("0123456789abcdef"))->get_der(), 
  String.hex2string("04080123456789abcdef") ) 
 
test_eq(Types.PrintableString("Test User 1")->get_der(), 
  String.hex2string("130b5465737420557365722031")) 
 
test_eq(Types.UTC()->set_posix(42)->get_posix(), 42) 
 
test_eq(Types.UTC("910506214540Z")->get_der(), 
  String.hex2string("170d3931303530363231343534305a")) 
 
test_eq(Types.UTC()->set_posix(673566340)->get_der(), 
  String.hex2string("170d3931303530363231343534305a")) 
 
test_eq(Types.UTC("910506214540Z")->get_posix(), 673566340) 
 
dnl test_eq(Types.TeletexString("clés publiques")->get_der(), 
dnl   String.hex2string("140f636cc26573207075626c6971756573")) 
 
test_eq(Types.BrokenTeletexString("clés publiques")->get_der(), 
  String.hex2string("140e636ce973207075626c6971756573")) 
 
test_any([[ 
  import Types; 
  Sequence countryName = Sequence( ({ Identifier(2,5,4,6), 
                                      PrintableString("US") }) ); 
  Sequence organizationName = Sequence( ({ Identifier(2,5,4,10), 
                                 PrintableString("Example Organization") }) ); 
  Sequence commonName = Sequence( ({ Identifier(2,5,4,11), 
                                     PrintableString("Test User 1") }) ); 
  return Sequence( ({ 
    Set( ({ countryName }) ), 
    Set( ({ organizationName }) ), 
    Set( ({ commonName }) ), 
  }) )->get_der(); 
]], String.hex2string( 
  "3042" 
  "310b" "3009""0603550406""13025553" 
  "311d" "301b""060355040a""13144578616d706c65204f7267616e697a6174696f6e" 
  "3114" "3012""060355040b""130b5465737420557365722031" 
)) 
 
test_any([[ 
 object o = Types.BitString("\xbf"); 
 return o->get_der() 
]], "\3\2\0\277") 
test_any([[ 
 object o = Types.BitString("\xbf")->set_length(3); 
 return o->get_der() 
]], "\3\2\5\240") 
 
test_eq([[ Types.Identifier(1,2,3) ]], [[ Types.Identifier(1,2,3) ]]) 
test_equal([[ Types.Identifier(1,2,3) ]], [[ Types.Identifier(1,2,3) ]]) 
test_eq([[ ([ Types.Identifier(1,2,3) : "x" ])[ Types.Identifier(1,2,3) ] ]], "x") 
test_eq([[ ({ Types.Identifier(1,2,3) }) - ({ Types.Identifier(1,2,3) }) ]], ({})) 
test_any([[ 
  multiset m = (< Types.Identifier(1,2,3) >); 
  m[Types.Identifier(1,2,3)] = 0; 
  return sizeof(m); 
]], 0) 
test_equal([[ sort( ({ 
  Types.Identifier(1,3), 
  Types.Identifier(1,2), 
  Types.Identifier(1,1), 
  Types.Identifier(1,2,1,1), 
  Types.Identifier(1,2,1), 
  Types.Identifier(1,2,2), 
})) ]], [[ ({ 
  Types.Identifier(1,1), 
  Types.Identifier(1,2), 
  Types.Identifier(1,2,1), 
  Types.Identifier(1,2,1,1), 
  Types.Identifier(1,2,2), 
  Types.Identifier(1,3), 
}) ]]) 
 
test_eq(Standards.ASN1.Decode.secure_der_decode("\1\1\0")->get_der(), 
"\1\1\0") 
test_eq(Standards.ASN1.Decode.secure_der_decode("\1\1\0\0"), 0) 
 
test_any([[ 
  // Sequence(Integer(4),Integer(7)) 
  Types.Sequence s = Standards.ASN1.Decode.simple_der_decode("0\6\2\1\4\2\1\a"); 
  s->elements[0] = Types.Integer(8); 
  return s->get_der(); 
]], "0\6\2\1\b\2\1\a") 
 
test_decode("170d3338303131393033313430385a", "UTCTime(380119031408Z)") 
test_eq(Types.UTC()->set_posix(1<<31)->get_der(), "\27\r380119031408Z") 
test_eval_error(Types.UTC()->set_posix(1<<32)) 
 
test_decode("180f32303338303131393033313430385a", "GeneralizedTime(20380119031408Z)") 
test_eq(Types.GeneralizedTime()->set_posix(1<<31)->get_der(), "\30\17""20380119031408Z") 
test_decode("180f32313036303230373036323831365a", "GeneralizedTime(21060207062816Z)") 
test_eq(Types.GeneralizedTime()->set_posix(1<<32)->get_der(), "\30\17""21060207062816Z") 
test_decode("180f32323432303331363132353633325a", "GeneralizedTime(22420316125632Z)") 
test_eq(Types.GeneralizedTime()->set_posix(1<<33)->get_der(), "\30\17""22420316125632Z") 
 
test_do( add_constant("P"); ) 
test_do( add_constant("Types"); ) 
 
END_MARKER