4 silc_asn1_encode(asn1, node,
5 SILC_ASN1_BOOLEAN(SilcBool),
7 silc_asn1_encode(asn1, dest,
8 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
9 SILC_ASN1_SEQUENCE_T(0, 9),
11 SILC_ASN1_ANY_T(0, 33, node),
12 SILC_ASN1_BOOLEAN_T(0, 4, boolv),
13 SILC_ASN1_BOOLEAN(SilcBool),
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).
25 silc_asn1_encode(asn1, node,
26 SILC_ASN1_BOOLEAN(SilcBool),
28 silc_asn1_encode(asn1, dest,
29 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
30 SILC_ASN1_SEQUENCE_T(0, 9),
32 SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, node),
33 SILC_ASN1_BOOLEAN_T(0, 4, boolv),
34 SILC_ASN1_BOOLEAN(SilcBool),
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.
46 int main(int argc, char **argv)
48 SilcBufferStruct node, node2;
50 SilcBool success = FALSE;
57 memset(&node, 0, sizeof(node));
58 memset(&node2, 0, sizeof(node2));
60 if (argc > 1 && !strcmp(argv[1], "-d")) {
62 silc_log_debug_hexdump(TRUE);
63 silc_log_set_debug_string("*asn1*,*ber*");
66 SILC_LOG_DEBUG(("Allocating ASN.1 context"));
67 asn1 = silc_asn1_alloc();
71 SILC_LOG_DEBUG(("Encoding ASN.1 tree 1"));
74 silc_asn1_encode(asn1, &node,
76 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
77 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
78 SILC_ASN1_INDEFINITE, 0),
79 SILC_ASN1_BOOLEAN_T(0, 4, val),
80 SILC_ASN1_BOOLEAN(val),
83 SILC_ASN1_END, SILC_ASN1_END);
85 SILC_LOG_DEBUG(("Encoding failed"));
88 SILC_LOG_DEBUG(("Encoding success"));
89 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
90 SILC_LOG_DEBUG(("Decoding ASN.1 tree 1"));
92 silc_asn1_decode(asn1, &node,
94 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
95 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
96 SILC_ASN1_INDEFINITE, 0),
97 SILC_ASN1_BOOLEAN_T(0, 4, &val),
98 SILC_ASN1_BOOLEAN(&val),
101 SILC_ASN1_END, SILC_ASN1_END);
103 SILC_LOG_DEBUG(("Decoding failed"));
106 SILC_LOG_DEBUG(("Decoding success"));
107 SILC_LOG_DEBUG(("Boolean val %d", val));
110 memset(&node, 0, sizeof(node));
111 SILC_LOG_DEBUG(("Encoding ASN.1 tree 1"));
114 silc_asn1_encode(asn1, &node,
116 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
117 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
118 SILC_ASN1_INDEFINITE, 0),
119 SILC_ASN1_BOOLEAN_T(0, 4, val),
120 SILC_ASN1_BOOLEAN(val),
123 SILC_ASN1_END, SILC_ASN1_END);
125 SILC_LOG_DEBUG(("Encoding failed"));
128 SILC_LOG_DEBUG(("Encoding success"));
129 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
130 SILC_LOG_DEBUG(("Decoding ASN.1 tree 1"));
132 silc_asn1_decode(asn1, &node,
134 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
135 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT |
136 SILC_ASN1_INDEFINITE, 0),
137 SILC_ASN1_BOOLEAN_T(0, 4, &val),
138 SILC_ASN1_BOOLEAN(&val),
141 SILC_ASN1_END, SILC_ASN1_END);
143 SILC_LOG_DEBUG(("Decoding failed"));
146 SILC_LOG_DEBUG(("Decoding success"));
147 SILC_LOG_DEBUG(("Boolean val %d", val));
151 memset(&node, 0, sizeof(node));
152 SILC_LOG_DEBUG(("Encoding ASN.1 tree 2"));
155 silc_asn1_encode(asn1, &node,
157 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
158 SILC_ASN1_SEQUENCE_T(SILC_ASN1_INDEFINITE, 0),
159 SILC_ASN1_BOOLEAN_T(0, 4, val),
160 SILC_ASN1_BOOLEAN(val),
163 SILC_ASN1_END, SILC_ASN1_END);
165 SILC_LOG_DEBUG(("Encoding failed"));
168 SILC_LOG_DEBUG(("Encoding success"));
169 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
170 SILC_LOG_DEBUG(("Decoding ASN.1 tree 2"));
172 silc_asn1_decode(asn1, &node,
174 SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9),
175 SILC_ASN1_SEQUENCE_T(SILC_ASN1_INDEFINITE, 0),
176 SILC_ASN1_BOOLEAN_T(0, 4, &val),
177 SILC_ASN1_BOOLEAN(&val),
180 SILC_ASN1_END, SILC_ASN1_END);
182 SILC_LOG_DEBUG(("Decoding failed"));
185 SILC_LOG_DEBUG(("Decoding success"));
186 SILC_LOG_DEBUG(("Boolean val %d", val));
190 memset(&node, 0, sizeof(node));
191 SILC_LOG_DEBUG(("Encoding ASN.1 tree 3"));
194 silc_asn1_encode(asn1, &node,
196 SILC_ASN1_SEQUENCE_T(0, 9),
198 SILC_ASN1_BOOLEAN_T(0, 4, val),
199 SILC_ASN1_BOOLEAN(val),
202 SILC_ASN1_END, SILC_ASN1_END);
204 SILC_LOG_DEBUG(("Encoding failed"));
207 SILC_LOG_DEBUG(("Encoding success"));
208 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
209 SILC_LOG_DEBUG(("Decoding ASN.1 tree 3"));
211 silc_asn1_decode(asn1, &node,
213 SILC_ASN1_SEQUENCE_T(0, 9),
215 SILC_ASN1_BOOLEAN_T(0, 4, &val),
216 SILC_ASN1_BOOLEAN(&val),
219 SILC_ASN1_END, SILC_ASN1_END);
221 SILC_LOG_DEBUG(("Decoding failed"));
224 SILC_LOG_DEBUG(("Decoding success"));
225 SILC_LOG_DEBUG(("Boolean val %d", val));
229 memset(&node, 0, sizeof(node));
230 SILC_LOG_DEBUG(("Encoding ASN.1 tree 4"));
233 silc_asn1_encode(asn1, &node,
234 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE |
235 SILC_ASN1_EXPLICIT, 101),
236 SILC_ASN1_SEQUENCE_T(0, 9),
238 SILC_ASN1_BOOLEAN_T(0, 4, val),
239 SILC_ASN1_BOOLEAN(val),
242 SILC_ASN1_END, SILC_ASN1_END);
244 SILC_LOG_DEBUG(("Encoding failed"));
247 SILC_LOG_DEBUG(("Encoding success"));
248 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
249 SILC_LOG_DEBUG(("Decoding ASN.1 tree 4"));
251 silc_asn1_decode(asn1, &node,
252 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE |
253 SILC_ASN1_EXPLICIT, 101),
254 SILC_ASN1_SEQUENCE_T(0, 9),
256 SILC_ASN1_BOOLEAN_T(0, 4, &val),
257 SILC_ASN1_BOOLEAN(&val),
260 SILC_ASN1_END, SILC_ASN1_END);
262 SILC_LOG_DEBUG(("Decoding failed"));
265 SILC_LOG_DEBUG(("Decoding success"));
266 SILC_LOG_DEBUG(("Boolean val %d", val));
270 memset(&node, 0, sizeof(node));
271 SILC_LOG_DEBUG(("Encoding ASN.1 tree 5"));
273 silc_asn1_encode(asn1, &node2,
274 SILC_ASN1_BOOLEAN(val),
276 SILC_LOG_DEBUG(("Encoding success"));
278 silc_asn1_encode(asn1, &node,
279 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
280 SILC_ASN1_SEQUENCE_T(0, 9),
282 SILC_ASN1_ANY(&node2),
283 SILC_ASN1_BOOLEAN_T(0, 4, val),
284 SILC_ASN1_BOOLEAN(val),
287 SILC_ASN1_END, SILC_ASN1_END);
289 SILC_LOG_DEBUG(("Encoding failed"));
292 SILC_LOG_DEBUG(("Encoding success"));
293 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
294 memset(&node2, 0, sizeof(node2));
295 SILC_LOG_DEBUG(("Decoding ASN.1 tree 5"));
297 silc_asn1_decode(asn1, &node,
298 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
299 SILC_ASN1_SEQUENCE_T(0, 9),
301 SILC_ASN1_ANY(&node2),
302 SILC_ASN1_BOOLEAN_T(0, 4, &val),
303 SILC_ASN1_BOOLEAN(&val),
306 SILC_ASN1_END, SILC_ASN1_END);
308 SILC_LOG_DEBUG(("Decoding failed"));
311 SILC_LOG_DEBUG(("Decoding success"));
312 SILC_LOG_DEBUG(("Boolean val %d", val));
314 silc_asn1_decode(asn1, &node2,
315 SILC_ASN1_BOOLEAN(&val),
318 SILC_LOG_DEBUG(("Decoding failed"));
321 SILC_LOG_DEBUG(("Decoding success"));
322 SILC_LOG_DEBUG(("Boolean val %d", val));
323 memset(&node2, 0, sizeof(node2));
327 memset(&node, 0, sizeof(node));
328 SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
329 memset(tmp, 0, sizeof(tmp));
331 silc_buffer_set(&node2, tmp, 1);
332 SILC_LOG_DEBUG(("Encoding success"));
334 silc_asn1_encode(asn1, &node,
336 SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
338 SILC_ASN1_END, SILC_ASN1_END);
340 SILC_LOG_DEBUG(("Encoding failed"));
343 SILC_LOG_DEBUG(("Encoding success"));
344 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
345 memset(&node2, 0, sizeof(node2));
346 SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
348 silc_asn1_decode(asn1, &node,
350 SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
352 SILC_ASN1_END, SILC_ASN1_END);
354 SILC_LOG_DEBUG(("Decoding failed"));
357 SILC_LOG_DEBUG(("Boolean val %d", node2.data[0]));
358 if (node2.data[0] != 0xff) {
359 SILC_LOG_DEBUG(("Decoding failed"));
362 SILC_LOG_DEBUG(("Decoding success"));
363 memset(&node, 0, sizeof(node));
364 SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
365 memset(tmp, 0, sizeof(tmp));
367 silc_buffer_set(&node2, tmp, 1);
368 SILC_LOG_DEBUG(("Encoding success"));
370 silc_asn1_encode(asn1, &node,
372 SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
374 SILC_ASN1_END, SILC_ASN1_END);
376 SILC_LOG_DEBUG(("Encoding failed"));
379 SILC_LOG_DEBUG(("Encoding success"));
380 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
381 memset(&node2, 0, sizeof(node2));
382 SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
384 silc_asn1_decode(asn1, &node,
386 SILC_ASN1_BOOLEAN(&val),
387 SILC_ASN1_END, SILC_ASN1_END);
389 SILC_LOG_DEBUG(("Decoding failed"));
392 SILC_LOG_DEBUG(("Decoding success"));
393 SILC_LOG_DEBUG(("Boolean val %d", val));
394 memset(&node, 0, sizeof(node));
395 SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
396 memset(tmp, 0, sizeof(tmp));
398 silc_buffer_set(&node2, tmp, 1);
399 SILC_LOG_DEBUG(("Encoding success"));
401 silc_asn1_encode(asn1, &node,
403 SILC_ASN1_BOOLEAN(val),
404 SILC_ASN1_END, SILC_ASN1_END);
406 SILC_LOG_DEBUG(("Encoding failed"));
409 SILC_LOG_DEBUG(("Encoding success"));
410 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
411 memset(&node2, 0, sizeof(node2));
412 SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
414 silc_asn1_decode(asn1, &node,
416 SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
418 SILC_ASN1_END, SILC_ASN1_END);
420 SILC_LOG_DEBUG(("Decoding failed"));
423 SILC_LOG_DEBUG(("Boolean val %d", node2.data[0]));
424 if (node2.data[0] != 0xff) {
425 SILC_LOG_DEBUG(("Decoding failed"));
428 SILC_LOG_DEBUG(("Decoding success"));
429 memset(&node2, 0, sizeof(node2));
433 memset(&node, 0, sizeof(node));
434 SILC_LOG_DEBUG(("Encoding ASN.1 tree 6"));
436 silc_asn1_encode(asn1, &node2,
437 SILC_ASN1_BOOLEAN(val),
439 SILC_LOG_DEBUG(("Encoding success"));
441 silc_asn1_encode(asn1, &node,
442 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
443 SILC_ASN1_SEQUENCE_T(0, 9),
445 SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, &node2),
446 SILC_ASN1_BOOLEAN_T(0, 4, val),
447 SILC_ASN1_BOOLEAN(val),
450 SILC_ASN1_END, SILC_ASN1_END);
452 SILC_LOG_DEBUG(("Encoding failed"));
455 SILC_LOG_DEBUG(("Encoding success"));
456 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
457 memset(&node2, 0, sizeof(node2));
458 SILC_LOG_DEBUG(("Decoding ASN.1 tree 6"));
460 silc_asn1_decode(asn1, &node,
461 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
462 SILC_ASN1_SEQUENCE_T(0, 9),
464 SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, &node2),
465 SILC_ASN1_BOOLEAN_T(0, 4, &val),
466 SILC_ASN1_BOOLEAN(&val),
469 SILC_ASN1_END, SILC_ASN1_END);
471 SILC_LOG_DEBUG(("Decoding failed"));
474 SILC_LOG_DEBUG(("Decoding success"));
475 SILC_LOG_DEBUG(("Boolean val %d", val));
477 silc_asn1_decode(asn1, &node2,
478 SILC_ASN1_BOOLEAN(&val),
481 SILC_LOG_DEBUG(("Decoding failed"));
484 SILC_LOG_DEBUG(("Decoding success"));
485 SILC_LOG_DEBUG(("Boolean val %d", val));
486 memset(&node2, 0, sizeof(node2));
490 memset(&node, 0, sizeof(node));
491 SILC_LOG_DEBUG(("Encoding ASN.1 tree 7"));
494 silc_asn1_encode(asn1, &node2,
496 SILC_ASN1_BOOLEAN(val),
497 SILC_ASN1_BOOLEAN(val),
498 SILC_ASN1_BOOLEAN(val),
499 SILC_ASN1_BOOLEAN(val),
500 SILC_ASN1_END, SILC_ASN1_END);
501 SILC_LOG_DEBUG(("Encoding success"));
504 silc_asn1_encode(asn1, &node,
505 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
506 SILC_ASN1_SEQUENCE_T(0, 9),
508 SILC_ASN1_ANY_T(0, 11, &node2),
509 SILC_ASN1_BOOLEAN_T(0, 4, val),
510 SILC_ASN1_BOOLEAN(val),
513 SILC_ASN1_END, SILC_ASN1_END);
515 SILC_LOG_DEBUG(("Encoding failed"));
518 SILC_LOG_DEBUG(("Encoding success"));
519 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
520 memset(&node2, 0, sizeof(node2));
521 SILC_LOG_DEBUG(("Decoding ASN.1 tree 7"));
523 silc_asn1_decode(asn1, &node,
524 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
525 SILC_ASN1_SEQUENCE_T(0, 9),
527 SILC_ASN1_ANY_T(0, 11, &node2), /* NOTE: tag */
528 SILC_ASN1_BOOLEAN_T(0, 4, &val),
529 SILC_ASN1_BOOLEAN(&val),
532 SILC_ASN1_END, SILC_ASN1_END);
534 SILC_LOG_DEBUG(("Decoding failed"));
537 SILC_LOG_DEBUG(("Decoding success"));
538 SILC_LOG_DEBUG(("Boolean val %d", val));
540 silc_asn1_decode(asn1, &node2,
541 SILC_ASN1_SEQUENCE_T(0, 11), /* NOTE: using implicit
543 SILC_ASN1_BOOLEAN(&val),
544 SILC_ASN1_BOOLEAN(&val),
545 SILC_ASN1_BOOLEAN(&val),
546 SILC_ASN1_BOOLEAN(&val),
547 SILC_ASN1_END, SILC_ASN1_END);
549 SILC_LOG_DEBUG(("Decoding failed"));
552 SILC_LOG_DEBUG(("Decoding success"));
553 SILC_LOG_DEBUG(("Boolean val %d", val));
554 memset(&node2, 0, sizeof(node2));
558 memset(&node, 0, sizeof(node));
559 SILC_LOG_DEBUG(("Encoding ASN.1 tree 8"));
561 silc_asn1_encode(asn1, &node,
563 SILC_ASN1_BOOLEAN_T(SILC_ASN1_IMPLICIT, 9999, val),
564 SILC_ASN1_END, SILC_ASN1_END);
566 SILC_LOG_DEBUG(("Encoding failed"));
569 SILC_LOG_DEBUG(("Encoding success"));
570 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
571 SILC_LOG_DEBUG(("Decoding ASN.1 tree 8"));
573 silc_asn1_decode(asn1, &node,
575 SILC_ASN1_BOOLEAN_T(0, 9999, &val),
576 SILC_ASN1_END, SILC_ASN1_END);
578 SILC_LOG_DEBUG(("Decoding failed"));
581 SILC_LOG_DEBUG(("Decoding success"));
582 SILC_LOG_DEBUG(("Boolean val %d", val));
583 memset(&node, 0, sizeof(node));
586 memset(&node, 0, sizeof(node));
587 SILC_LOG_DEBUG(("Encoding ASN.1 tree 9"));
589 silc_asn1_encode(asn1, &node,
591 SILC_ASN1_SEQUENCE_T(0, 9),
593 SILC_ASN1_BOOLEAN_T(0, 4, val),
594 SILC_ASN1_BOOLEAN(val),
596 SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 99, val),
597 SILC_ASN1_BOOLEAN_T(0, 100, val),
601 SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 0, val),
602 SILC_ASN1_OCTET_STRING("foobar", 6),
603 SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE, 43, val),
604 SILC_ASN1_BOOLEAN_T(SILC_ASN1_APP |
605 SILC_ASN1_EXPLICIT, 1, val),
607 SILC_ASN1_END, SILC_ASN1_END);
609 SILC_LOG_DEBUG(("Encoding failed"));
612 SILC_LOG_DEBUG(("Encoding success"));
613 SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
614 SILC_LOG_DEBUG(("Decoding ASN.1 tree 9"));
616 silc_asn1_decode(asn1, &node,
618 SILC_ASN1_SEQUENCE_T(0, 9),
620 SILC_ASN1_BOOLEAN_T(0, 4, &val),
621 SILC_ASN1_BOOLEAN(&val),
623 SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 99, &val),
624 SILC_ASN1_BOOLEAN_T(0, 100, &val),
628 SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 0, &val),
629 SILC_ASN1_OCTET_STRING(&str, &str_len),
630 SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE, 43, &val),
631 SILC_ASN1_BOOLEAN_T(SILC_ASN1_APP |
632 SILC_ASN1_EXPLICIT, 1, &val),
634 SILC_ASN1_END, SILC_ASN1_END);
636 SILC_LOG_DEBUG(("Decoding failed"));
639 SILC_LOG_DEBUG(("Decoding success"));
640 SILC_LOG_DEBUG(("Boolean val %d", val));
641 SILC_LOG_DEBUG(("Ooctet-string %s, len %d", str, str_len));
645 silc_asn1_free(asn1);