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