Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silcasn1 / tests / test_silcasn1.c
1 #include "silc.h"
2
3 /*
4 silc_asn1_encode(asn1, node,
5                  SILC_ASN1_BOOLEAN(SilcBool),
6                  SILC_ASN1_END);
7 silc_asn1_encode(asn1, dest,
8                  SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
9                    SILC_ASN1_SEQUENCE_T(0, 9),
10                      SILC_ASN1_SEQUENCE,
11                        SILC_ASN1_ANY_T(0, 33, node),
12                        SILC_ASN1_BOOLEAN_T(0, 4, boolv),
13                        SILC_ASN1_BOOLEAN(SilcBool),
14                      SILC_ASN1_END,
15                    SILC_ASN1_END,
16                  SILC_ASN1_END);
17
18   FATAL ERROR: Adding primitive node with implicit tagging is not possible.
19   The node either must be constructed (SEQUENCE or SET), or the tagging
20   must be explicit (in which case end result is same).
21 */
22
23
24 /*
25 silc_asn1_encode(asn1, node,
26                  SILC_ASN1_BOOLEAN(SilcBool),
27                  SILC_ASN1_END);
28 silc_asn1_encode(asn1, dest,
29                  SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
30                    SILC_ASN1_SEQUENCE_T(0, 9),
31                      SILC_ASN1_SEQUENCE,
32                        SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, node),
33                        SILC_ASN1_BOOLEAN_T(0, 4, boolv),
34                        SILC_ASN1_BOOLEAN(SilcBool),
35                      SILC_ASN1_END,
36                    SILC_ASN1_END,
37                  SILC_ASN1_END);
38
39   CORRECT: the tagging is now explicit.  Also note that tagging primitive
40   node explicitly is analougous of having a constructed node and tagging
41   that implicitly: the end result is same.
42
43 */
44
45
46 int main(int argc, char **argv)
47 {
48   SilcBufferStruct node, node2;
49   SilcAsn1 asn1;
50   SilcBool success = FALSE;
51   SilcBool val = TRUE;
52   int i;
53   unsigned char *str;
54   SilcUInt32 str_len, tmpint;
55   char tmp[32];
56   SilcRng rng;
57   SilcMPInt mpint, mpint2;
58
59   memset(&node, 0, sizeof(node));
60   memset(&node2, 0, sizeof(node2));
61
62   if (argc > 1 && !strcmp(argv[1], "-d")) {
63     silc_log_debug(TRUE);
64     silc_log_debug_hexdump(TRUE);
65     silc_log_set_debug_string("*asn1*,*ber*");
66   }
67
68   silc_hash_register_default();
69   rng = silc_rng_alloc();
70   silc_rng_init(rng);
71
72   SILC_LOG_DEBUG(("Allocating ASN.1 context"));
73   asn1 = silc_asn1_alloc();
74   if (!asn1)
75     goto out;
76
77   SILC_LOG_DEBUG(("Encoding ASN.1 tree 1"));
78   val = 1;
79   success =
80     silc_asn1_encode(asn1, &node,
81                      SILC_ASN1_SEQUENCE,
82                        SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
83                          SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
84                                               SILC_ASN1_INDEFINITE, 0),
85                            SILC_ASN1_BOOLEAN_T(0, 4, val),
86                            SILC_ASN1_BOOLEAN(val),
87                          SILC_ASN1_END,
88                        SILC_ASN1_END,
89                      SILC_ASN1_END, SILC_ASN1_END);
90   if (!success) {
91     SILC_LOG_DEBUG(("Encoding failed"));
92     goto out;
93   }
94   SILC_LOG_DEBUG(("Encoding success"));
95   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
96   SILC_LOG_DEBUG(("Decoding ASN.1 tree 1"));
97   success =
98     silc_asn1_decode(asn1, &node,
99                      SILC_ASN1_SEQUENCE,
100                        SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
101                          SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
102                                               SILC_ASN1_INDEFINITE, 0),
103                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
104                            SILC_ASN1_BOOLEAN(&val),
105                          SILC_ASN1_END,
106                        SILC_ASN1_END,
107                      SILC_ASN1_END, SILC_ASN1_END);
108   if (!success) {
109     SILC_LOG_DEBUG(("Decoding failed"));
110     goto out;
111   }
112   SILC_LOG_DEBUG(("Decoding success"));
113   SILC_LOG_DEBUG(("Boolean val %d", val));
114
115 #if 1
116   memset(&node, 0, sizeof(node));
117   SILC_LOG_DEBUG(("Encoding ASN.1 tree 1"));
118   val = 0;
119   success =
120     silc_asn1_encode(asn1, &node,
121                      SILC_ASN1_SEQUENCE,
122                        SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
123                          SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
124                                               SILC_ASN1_INDEFINITE, 0),
125                            SILC_ASN1_BOOLEAN_T(0, 4, val),
126                            SILC_ASN1_BOOLEAN(val),
127                          SILC_ASN1_END,
128                        SILC_ASN1_END,
129                      SILC_ASN1_END, SILC_ASN1_END);
130   if (!success) {
131     SILC_LOG_DEBUG(("Encoding failed"));
132     goto out;
133   }
134   SILC_LOG_DEBUG(("Encoding success"));
135   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
136   SILC_LOG_DEBUG(("Decoding ASN.1 tree 1"));
137   success =
138     silc_asn1_decode(asn1, &node,
139                      SILC_ASN1_SEQUENCE,
140                        SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
141                          SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
142                                               SILC_ASN1_INDEFINITE, 0),
143                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
144                            SILC_ASN1_BOOLEAN(&val),
145                          SILC_ASN1_END,
146                        SILC_ASN1_END,
147                      SILC_ASN1_END, SILC_ASN1_END);
148   if (!success) {
149     SILC_LOG_DEBUG(("Decoding failed"));
150     goto out;
151   }
152   SILC_LOG_DEBUG(("Decoding success"));
153   SILC_LOG_DEBUG(("Boolean val %d", val));
154   printf("\n");
155
156
157   memset(&node, 0, sizeof(node));
158   SILC_LOG_DEBUG(("Encoding ASN.1 tree 2"));
159   val = 1;
160   success =
161     silc_asn1_encode(asn1, &node,
162                      SILC_ASN1_SEQUENCE,
163                        SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
164                          SILC_ASN1_SEQUENCE_T(SILC_ASN1_INDEFINITE, 0),
165                            SILC_ASN1_BOOLEAN_T(0, 4, val),
166                            SILC_ASN1_BOOLEAN(val),
167                          SILC_ASN1_END,
168                        SILC_ASN1_END,
169                      SILC_ASN1_END, SILC_ASN1_END);
170   if (!success) {
171     SILC_LOG_DEBUG(("Encoding failed"));
172     goto out;
173   }
174   SILC_LOG_DEBUG(("Encoding success"));
175   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
176   SILC_LOG_DEBUG(("Decoding ASN.1 tree 2"));
177   success =
178     silc_asn1_decode(asn1, &node,
179                      SILC_ASN1_SEQUENCE,
180                        SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
181                          SILC_ASN1_SEQUENCE_T(SILC_ASN1_INDEFINITE, 0),
182                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
183                            SILC_ASN1_BOOLEAN(&val),
184                          SILC_ASN1_END,
185                        SILC_ASN1_END,
186                      SILC_ASN1_END, SILC_ASN1_END);
187   if (!success) {
188     SILC_LOG_DEBUG(("Decoding failed"));
189     goto out;
190   }
191   SILC_LOG_DEBUG(("Decoding success"));
192   SILC_LOG_DEBUG(("Boolean val %d", val));
193   printf("\n");
194
195
196   memset(&node, 0, sizeof(node));
197   SILC_LOG_DEBUG(("Encoding ASN.1 tree 3"));
198   val = 0;
199   success =
200     silc_asn1_encode(asn1, &node,
201                      SILC_ASN1_SEQUENCE,
202                        SILC_ASN1_SEQUENCE_T(0, 9),
203                          SILC_ASN1_SEQUENCE,
204                            SILC_ASN1_BOOLEAN_T(0, 4, val),
205                            SILC_ASN1_BOOLEAN(val),
206                          SILC_ASN1_END,
207                        SILC_ASN1_END,
208                      SILC_ASN1_END, SILC_ASN1_END);
209   if (!success) {
210     SILC_LOG_DEBUG(("Encoding failed"));
211     goto out;
212   }
213   SILC_LOG_DEBUG(("Encoding success"));
214   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
215   SILC_LOG_DEBUG(("Decoding ASN.1 tree 3"));
216   success =
217     silc_asn1_decode(asn1, &node,
218                      SILC_ASN1_SEQUENCE,
219                        SILC_ASN1_SEQUENCE_T(0, 9),
220                          SILC_ASN1_SEQUENCE,
221                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
222                            SILC_ASN1_BOOLEAN(&val),
223                          SILC_ASN1_END,
224                        SILC_ASN1_END,
225                      SILC_ASN1_END, SILC_ASN1_END);
226   if (!success) {
227     SILC_LOG_DEBUG(("Decoding failed"));
228     goto out;
229   }
230   SILC_LOG_DEBUG(("Decoding success"));
231   SILC_LOG_DEBUG(("Boolean val %d", val));
232   printf("\n");
233
234
235   memset(&node, 0, sizeof(node));
236   SILC_LOG_DEBUG(("Encoding ASN.1 tree 4"));
237   val = 1;
238   success =
239     silc_asn1_encode(asn1, &node,
240                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE |
241                                           SILC_ASN1_EXPLICIT, 101),
242                        SILC_ASN1_SEQUENCE_T(0, 9),
243                          SILC_ASN1_SEQUENCE,
244                            SILC_ASN1_BOOLEAN_T(0, 4, val),
245                            SILC_ASN1_BOOLEAN(val),
246                          SILC_ASN1_END,
247                        SILC_ASN1_END,
248                      SILC_ASN1_END, SILC_ASN1_END);
249   if (!success) {
250     SILC_LOG_DEBUG(("Encoding failed"));
251     goto out;
252   }
253   SILC_LOG_DEBUG(("Encoding success"));
254   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
255   SILC_LOG_DEBUG(("Decoding ASN.1 tree 4"));
256   success =
257     silc_asn1_decode(asn1, &node,
258                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE |
259                                           SILC_ASN1_EXPLICIT, 101),
260                        SILC_ASN1_SEQUENCE_T(0, 9),
261                          SILC_ASN1_SEQUENCE,
262                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
263                            SILC_ASN1_BOOLEAN(&val),
264                          SILC_ASN1_END,
265                        SILC_ASN1_END,
266                      SILC_ASN1_END, SILC_ASN1_END);
267   if (!success) {
268     SILC_LOG_DEBUG(("Decoding failed"));
269     goto out;
270   }
271   SILC_LOG_DEBUG(("Decoding success"));
272   SILC_LOG_DEBUG(("Boolean val %d", val));
273   printf("\n");
274
275
276   memset(&node, 0, sizeof(node));
277   SILC_LOG_DEBUG(("Encoding ASN.1 tree 5"));
278   success =
279     silc_asn1_encode(asn1, &node2,
280                      SILC_ASN1_BOOLEAN(val),
281                      SILC_ASN1_END);
282   SILC_LOG_DEBUG(("Encoding success"));
283   success =
284     silc_asn1_encode(asn1, &node,
285                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
286                        SILC_ASN1_SEQUENCE_T(0, 9),
287                          SILC_ASN1_SEQUENCE,
288                            SILC_ASN1_ANY(&node2),
289                            SILC_ASN1_BOOLEAN_T(0, 4, val),
290                            SILC_ASN1_BOOLEAN(val),
291                          SILC_ASN1_END,
292                        SILC_ASN1_END,
293                      SILC_ASN1_END, SILC_ASN1_END);
294   if (!success) {
295     SILC_LOG_DEBUG(("Encoding failed"));
296     goto out;
297   }
298   SILC_LOG_DEBUG(("Encoding success"));
299   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
300   memset(&node2, 0, sizeof(node2));
301   SILC_LOG_DEBUG(("Decoding ASN.1 tree 5"));
302   success =
303     silc_asn1_decode(asn1, &node,
304                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
305                        SILC_ASN1_SEQUENCE_T(0, 9),
306                          SILC_ASN1_SEQUENCE,
307                            SILC_ASN1_ANY(&node2),
308                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
309                            SILC_ASN1_BOOLEAN(&val),
310                          SILC_ASN1_END,
311                        SILC_ASN1_END,
312                      SILC_ASN1_END, SILC_ASN1_END);
313   if (!success) {
314     SILC_LOG_DEBUG(("Decoding failed"));
315     goto out;
316   }
317   SILC_LOG_DEBUG(("Decoding success"));
318   SILC_LOG_DEBUG(("Boolean val %d", val));
319   success =
320     silc_asn1_decode(asn1, &node2,
321                      SILC_ASN1_BOOLEAN(&val),
322                      SILC_ASN1_END);
323   if (!success) {
324     SILC_LOG_DEBUG(("Decoding failed"));
325     goto out;
326   }
327   SILC_LOG_DEBUG(("Decoding success"));
328   SILC_LOG_DEBUG(("Boolean val %d", val));
329   memset(&node2, 0, sizeof(node2));
330   printf("\n");
331
332
333   memset(&node, 0, sizeof(node));
334   SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
335   memset(tmp, 0, sizeof(tmp));
336   tmp[0] = 0xff;
337   silc_buffer_set(&node2, tmp, 1);
338   SILC_LOG_DEBUG(("Encoding success"));
339   success =
340     silc_asn1_encode(asn1, &node,
341                      SILC_ASN1_SEQUENCE,
342                        SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
343                                                &node2),
344                      SILC_ASN1_END, SILC_ASN1_END);
345   if (!success) {
346     SILC_LOG_DEBUG(("Encoding failed"));
347     goto out;
348   }
349   SILC_LOG_DEBUG(("Encoding success"));
350   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
351   memset(&node2, 0, sizeof(node2));
352   SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
353   success =
354     silc_asn1_decode(asn1, &node,
355                      SILC_ASN1_SEQUENCE,
356                        SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
357                                                &node2),
358                      SILC_ASN1_END, SILC_ASN1_END);
359   if (!success) {
360     SILC_LOG_DEBUG(("Decoding failed"));
361     goto out;
362   }
363   SILC_LOG_DEBUG(("Boolean val %d", node2.data[0]));
364   if (node2.data[0] != 0xff) {
365     SILC_LOG_DEBUG(("Decoding failed"));
366     goto out;
367   }
368   SILC_LOG_DEBUG(("Decoding success"));
369   memset(&node, 0, sizeof(node));
370   SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
371   memset(tmp, 0, sizeof(tmp));
372   tmp[0] = 0xff;
373   silc_buffer_set(&node2, tmp, 1);
374   SILC_LOG_DEBUG(("Encoding success"));
375   success =
376     silc_asn1_encode(asn1, &node,
377                      SILC_ASN1_SEQUENCE,
378                        SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
379                                                &node2),
380                      SILC_ASN1_END, SILC_ASN1_END);
381   if (!success) {
382     SILC_LOG_DEBUG(("Encoding failed"));
383     goto out;
384   }
385   SILC_LOG_DEBUG(("Encoding success"));
386   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
387   memset(&node2, 0, sizeof(node2));
388   SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
389   success =
390     silc_asn1_decode(asn1, &node,
391                      SILC_ASN1_SEQUENCE,
392                        SILC_ASN1_BOOLEAN(&val),
393                      SILC_ASN1_END, SILC_ASN1_END);
394   if (!success) {
395     SILC_LOG_DEBUG(("Decoding failed"));
396     goto out;
397   }
398   SILC_LOG_DEBUG(("Decoding success"));
399   SILC_LOG_DEBUG(("Boolean val %d", val));
400   memset(&node, 0, sizeof(node));
401   SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
402   memset(tmp, 0, sizeof(tmp));
403   tmp[0] = 0xff;
404   silc_buffer_set(&node2, tmp, 1);
405   SILC_LOG_DEBUG(("Encoding success"));
406   success =
407     silc_asn1_encode(asn1, &node,
408                      SILC_ASN1_SEQUENCE,
409                        SILC_ASN1_BOOLEAN(val),
410                      SILC_ASN1_END, SILC_ASN1_END);
411   if (!success) {
412     SILC_LOG_DEBUG(("Encoding failed"));
413     goto out;
414   }
415   SILC_LOG_DEBUG(("Encoding success"));
416   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
417   memset(&node2, 0, sizeof(node2));
418   SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
419   success =
420     silc_asn1_decode(asn1, &node,
421                      SILC_ASN1_SEQUENCE,
422                        SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
423                                                &node2),
424                      SILC_ASN1_END, SILC_ASN1_END);
425   if (!success) {
426     SILC_LOG_DEBUG(("Decoding failed"));
427     goto out;
428   }
429   SILC_LOG_DEBUG(("Boolean val %d", node2.data[0]));
430   if (node2.data[0] != 0xff) {
431     SILC_LOG_DEBUG(("Decoding failed"));
432     goto out;
433   }
434   SILC_LOG_DEBUG(("Decoding success"));
435   memset(&node2, 0, sizeof(node2));
436   printf("\n");
437
438
439   memset(&node, 0, sizeof(node));
440   SILC_LOG_DEBUG(("Encoding ASN.1 tree 6"));
441   success =
442     silc_asn1_encode(asn1, &node2,
443                      SILC_ASN1_BOOLEAN(val),
444                      SILC_ASN1_END);
445   SILC_LOG_DEBUG(("Encoding success"));
446   success =
447     silc_asn1_encode(asn1, &node,
448                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
449                        SILC_ASN1_SEQUENCE_T(0, 9),
450                          SILC_ASN1_SEQUENCE,
451                            SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, &node2),
452                            SILC_ASN1_BOOLEAN_T(0, 4, val),
453                            SILC_ASN1_BOOLEAN(val),
454                          SILC_ASN1_END,
455                        SILC_ASN1_END,
456                      SILC_ASN1_END, SILC_ASN1_END);
457   if (!success) {
458     SILC_LOG_DEBUG(("Encoding failed"));
459     goto out;
460   }
461   SILC_LOG_DEBUG(("Encoding success"));
462   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
463   memset(&node2, 0, sizeof(node2));
464   SILC_LOG_DEBUG(("Decoding ASN.1 tree 6"));
465   success =
466     silc_asn1_decode(asn1, &node,
467                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
468                        SILC_ASN1_SEQUENCE_T(0, 9),
469                          SILC_ASN1_SEQUENCE,
470                            SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, &node2),
471                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
472                            SILC_ASN1_BOOLEAN(&val),
473                          SILC_ASN1_END,
474                        SILC_ASN1_END,
475                      SILC_ASN1_END, SILC_ASN1_END);
476   if (!success) {
477     SILC_LOG_DEBUG(("Decoding failed"));
478     goto out;
479   }
480   SILC_LOG_DEBUG(("Decoding success"));
481   SILC_LOG_DEBUG(("Boolean val %d", val));
482   success =
483     silc_asn1_decode(asn1, &node2,
484                      SILC_ASN1_BOOLEAN(&val),
485                      SILC_ASN1_END);
486   if (!success) {
487     SILC_LOG_DEBUG(("Decoding failed"));
488     goto out;
489   }
490   SILC_LOG_DEBUG(("Decoding success"));
491   SILC_LOG_DEBUG(("Boolean val %d", val));
492   memset(&node2, 0, sizeof(node2));
493   printf("\n");
494
495
496   memset(&node, 0, sizeof(node));
497   SILC_LOG_DEBUG(("Encoding ASN.1 tree 7"));
498   val = 0;
499   success =
500     silc_asn1_encode(asn1, &node2,
501                      SILC_ASN1_SEQUENCE,
502                        SILC_ASN1_BOOLEAN(val),
503                        SILC_ASN1_BOOLEAN(val),
504                        SILC_ASN1_BOOLEAN(val),
505                        SILC_ASN1_BOOLEAN(val),
506                      SILC_ASN1_END, SILC_ASN1_END);
507   SILC_LOG_DEBUG(("Encoding success"));
508   val = 1;
509   success =
510     silc_asn1_encode(asn1, &node,
511                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
512                        SILC_ASN1_SEQUENCE_T(0, 9),
513                          SILC_ASN1_SEQUENCE,
514                            SILC_ASN1_ANY_T(0, 11, &node2),
515                            SILC_ASN1_BOOLEAN_T(0, 4, val),
516                            SILC_ASN1_BOOLEAN(val),
517                          SILC_ASN1_END,
518                        SILC_ASN1_END,
519                      SILC_ASN1_END, SILC_ASN1_END);
520   if (!success) {
521     SILC_LOG_DEBUG(("Encoding failed"));
522     goto out;
523   }
524   SILC_LOG_DEBUG(("Encoding success"));
525   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
526   memset(&node2, 0, sizeof(node2));
527   SILC_LOG_DEBUG(("Decoding ASN.1 tree 7"));
528   success =
529     silc_asn1_decode(asn1, &node,
530                      SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
531                        SILC_ASN1_SEQUENCE_T(0, 9),
532                          SILC_ASN1_SEQUENCE,
533                            SILC_ASN1_ANY_T(0, 11, &node2), /* NOTE: tag */
534                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
535                            SILC_ASN1_BOOLEAN(&val),
536                          SILC_ASN1_END,
537                        SILC_ASN1_END,
538                      SILC_ASN1_END, SILC_ASN1_END);
539   if (!success) {
540     SILC_LOG_DEBUG(("Decoding failed"));
541     goto out;
542   }
543   SILC_LOG_DEBUG(("Decoding success"));
544   SILC_LOG_DEBUG(("Boolean val %d", val));
545   success =
546     silc_asn1_decode(asn1, &node2,
547                      SILC_ASN1_SEQUENCE_T(0, 11), /* NOTE: using implicit
548                                                      tag! */
549                        SILC_ASN1_BOOLEAN(&val),
550                        SILC_ASN1_BOOLEAN(&val),
551                        SILC_ASN1_BOOLEAN(&val),
552                        SILC_ASN1_BOOLEAN(&val),
553                      SILC_ASN1_END, SILC_ASN1_END);
554   if (!success) {
555     SILC_LOG_DEBUG(("Decoding failed"));
556     goto out;
557   }
558   SILC_LOG_DEBUG(("Decoding success"));
559   SILC_LOG_DEBUG(("Boolean val %d", val));
560   memset(&node2, 0, sizeof(node2));
561   printf("\n");
562
563
564   memset(&node, 0, sizeof(node));
565   SILC_LOG_DEBUG(("Encoding ASN.1 tree 8"));
566   success =
567     silc_asn1_encode(asn1, &node,
568                      SILC_ASN1_SEQUENCE,
569                        SILC_ASN1_BOOLEAN_T(SILC_ASN1_IMPLICIT, 9999, val),
570                      SILC_ASN1_END, SILC_ASN1_END);
571   if (!success) {
572     SILC_LOG_DEBUG(("Encoding failed"));
573     goto out;
574   }
575   SILC_LOG_DEBUG(("Encoding success"));
576   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
577   SILC_LOG_DEBUG(("Decoding ASN.1 tree 8"));
578   success =
579     silc_asn1_decode(asn1, &node,
580                      SILC_ASN1_SEQUENCE,
581                        SILC_ASN1_BOOLEAN_T(0, 9999, &val),
582                      SILC_ASN1_END, SILC_ASN1_END);
583   if (!success) {
584     SILC_LOG_DEBUG(("Decoding failed"));
585     goto out;
586   }
587   SILC_LOG_DEBUG(("Decoding success"));
588   SILC_LOG_DEBUG(("Boolean val %d", val));
589   memset(&node, 0, sizeof(node));
590   printf("\n");
591
592
593   memset(&node, 0, sizeof(node));
594   SILC_LOG_DEBUG(("Encoding ASN.1 tree 9"));
595   success =
596     silc_asn1_encode(asn1, &node,
597                      SILC_ASN1_SEQUENCE,
598                        SILC_ASN1_SEQUENCE_T(0, 9),
599                          SILC_ASN1_SEQUENCE,
600                            SILC_ASN1_BOOLEAN_T(0, 4, val),
601                            SILC_ASN1_BOOLEAN(val),
602                          SILC_ASN1_END,
603                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 99, val),
604                          SILC_ASN1_BOOLEAN_T(0, 100, val),
605                        SILC_ASN1_END,
606                        SILC_ASN1_SEQUENCE,
607                          SILC_ASN1_NULL,
608                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 0, val),
609                          SILC_ASN1_OCTET_STRING("foobar", 6),
610                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE, 43, val),
611                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_APP |
612                                              SILC_ASN1_EXPLICIT, 1, val),
613                        SILC_ASN1_END,
614                      SILC_ASN1_END, SILC_ASN1_END);
615   if (!success) {
616     SILC_LOG_DEBUG(("Encoding failed"));
617     goto out;
618   }
619   SILC_LOG_DEBUG(("Encoding success"));
620   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
621   SILC_LOG_DEBUG(("Decoding ASN.1 tree 9"));
622   success =
623     silc_asn1_decode(asn1, &node,
624                      SILC_ASN1_SEQUENCE,
625                        SILC_ASN1_SEQUENCE_T(0, 9),
626                          SILC_ASN1_SEQUENCE,
627                            SILC_ASN1_BOOLEAN_T(0, 4, &val),
628                            SILC_ASN1_BOOLEAN(&val),
629                          SILC_ASN1_END,
630                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 99, &val),
631                          SILC_ASN1_BOOLEAN_T(0, 100, &val),
632                        SILC_ASN1_END,
633                        SILC_ASN1_SEQUENCE,
634                          SILC_ASN1_NULL,
635                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 0, &val),
636                          SILC_ASN1_OCTET_STRING(&str, &str_len),
637                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE, 43, &val),
638                          SILC_ASN1_BOOLEAN_T(SILC_ASN1_APP |
639                                              SILC_ASN1_EXPLICIT, 1, &val),
640                        SILC_ASN1_END,
641                      SILC_ASN1_END, SILC_ASN1_END);
642   if (!success) {
643     SILC_LOG_DEBUG(("Decoding failed"));
644     goto out;
645   }
646   SILC_LOG_DEBUG(("Decoding success"));
647   SILC_LOG_DEBUG(("Boolean val %d", val));
648   SILC_LOG_DEBUG(("Ooctet-string %s, len %d", str, str_len));
649   printf("\n");
650
651
652   memset(&node, 0, sizeof(node));
653   SILC_LOG_DEBUG(("Encoding ASN.1 tree 10 (INTEGER)"));
654   str = silc_rng_get_rn_data(rng, 256);
655   silc_mp_init(&mpint);
656   silc_mp_init(&mpint2);
657   silc_mp_bin2mp(str, 256, &mpint);
658   success =
659     silc_asn1_encode(asn1, &node,
660                      SILC_ASN1_INT(&mpint),
661                      SILC_ASN1_END);
662   if (!success) {
663     SILC_LOG_DEBUG(("Encoding failed"));
664     goto out;
665   }
666   SILC_LOG_DEBUG(("Encoding success"));
667   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
668   SILC_LOG_DEBUG(("Decoding ASN.1 tree 10 (INTEGER)"));
669   success =
670     silc_asn1_decode(asn1, &node,
671                      SILC_ASN1_INT(&mpint2),
672                      SILC_ASN1_END);
673   if (silc_mp_cmp(&mpint, &mpint2) != 0) {
674     SILC_LOG_DEBUG(("INTEGER MISMATCH"));
675     goto out;
676   }
677   if (!success) {
678     SILC_LOG_DEBUG(("Decoding failed"));
679     goto out;
680   }
681   SILC_LOG_DEBUG(("Decoding success"));
682   printf("\n");
683
684
685   memset(&node, 0, sizeof(node));
686   SILC_LOG_DEBUG(("Encoding ASN.1 tree 11 (OID)"));
687   success =
688     silc_asn1_encode(asn1, &node,
689                      SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
690                      SILC_ASN1_OID("1.2.840.113549"),
691                      SILC_ASN1_END);
692   if (!success) {
693     SILC_LOG_DEBUG(("Encoding failed"));
694     goto out;
695   }
696   SILC_LOG_DEBUG(("Encoding success"));
697   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
698   SILC_LOG_DEBUG(("Decoding ASN.1 tree 11 (OID)"));
699   success =
700     silc_asn1_decode(asn1, &node,
701                      SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
702                      SILC_ASN1_OID(&str),
703                      SILC_ASN1_END);
704   if (!success) {
705     SILC_LOG_DEBUG(("Decoding failed"));
706     goto out;
707   }
708   SILC_LOG_DEBUG(("Decoding success"));
709   SILC_LOG_DEBUG(("OID %s", str));
710   printf("\n");
711
712
713   memset(&node, 0, sizeof(node));
714   SILC_LOG_DEBUG(("Encoding ASN.1 tree 12 (SHORT INTEGER)"));
715   str_len = 198761;
716   tmpint = 0;
717   SILC_LOG_DEBUG(("Short integer: %d", str_len));
718   SILC_LOG_DEBUG(("Short integer: %d", tmpint));
719   success =
720     silc_asn1_encode(asn1, &node,
721                      SILC_ASN1_SHORT_INT(str_len),
722                      SILC_ASN1_SHORT_INT_T(SILC_ASN1_IMPLICIT, 100, tmpint),
723                      SILC_ASN1_END);
724   if (!success) {
725     SILC_LOG_DEBUG(("Encoding failed"));
726     goto out;
727   }
728   SILC_LOG_DEBUG(("Encoding success"));
729   SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
730   SILC_LOG_DEBUG(("Decoding ASN.1 tree 12 (SHORT INTEGER)"));
731   success =
732     silc_asn1_decode(asn1, &node,
733                      SILC_ASN1_SHORT_INT(&str_len),
734                      SILC_ASN1_SHORT_INT_T(SILC_ASN1_IMPLICIT, 100, &tmpint),
735                      SILC_ASN1_END);
736   if (!success) {
737     SILC_LOG_DEBUG(("Decoding failed"));
738     goto out;
739   }
740   SILC_LOG_DEBUG(("Short integer: %d", str_len));
741   SILC_LOG_DEBUG(("Short integer: %d", tmpint));
742   SILC_LOG_DEBUG(("Decoding success"));
743   printf("\n");
744
745 #endif /* 1 */
746   silc_asn1_free(asn1);
747
748   success = TRUE;
749  out:
750   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
751   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
752
753   return success;
754 }