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