Fixed OID encoding.
[silc.git] / lib / silcasn1 / silcasn1_encode.c
1 /*
2
3   silcasn1_encode.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2003 - 2006 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include "silcasn1.h"
22 #include "silcber.h"
23
24 /************************** ASN.1 Encoder routines **************************/
25
26 /* Encode string from UTF-8 string to other string encodings.  Encodes
27    diretly to BER blob. */
28 #define SILC_ASN1_ENCODE_STRING(enc)                                    \
29   unsigned char *s, *d = va_arg(asn1->ap, unsigned char *);             \
30   SilcUInt32 s_len, d_len = va_arg(asn1->ap, SilcUInt32);               \
31   if (!d)                                                               \
32     break;                                                              \
33   s_len = silc_utf8_decoded_len(d, d_len, (enc));                       \
34   if (s_len == 0) {                                                     \
35     SILC_LOG_DEBUG(("Malformed %d string value", (enc)));               \
36     goto fail;                                                          \
37   }                                                                     \
38   silc_stack_push(asn1->stack2, &frame);                                \
39   s = silc_smalloc_ua(stack2, s_len + 1);                               \
40   if (s) {                                                              \
41     silc_utf8_decode(d, d_len, (enc), s, s_len);                        \
42     s[s_len] = '\0';                                                    \
43   }                                                                     \
44   len = silc_ber_encoded_len(tag, s_len, indef);                        \
45   dest = silc_buffer_srealloc_size(stack1, dest,                        \
46                                    silc_buffer_truelen(dest) + len);    \
47   ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,        \
48                         tag, s, s_len, indef);                          \
49   silc_stack_pop(asn1->stack2);                                         \
50   if (!ret)                                                             \
51     goto fail;
52
53 /* The internal ASN.1 encoder.  The `type', `tag' and `opts' are the
54    first arguments (either very first or first for recursion) for a type.
55    The `depth' includes the current depth of recursion.  The `primitive'
56    is TRUE if this encoder receives one primitive type as argument.  If
57    it is a constructed type it must be FALSE value. */
58
59 static SilcBool
60 silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2,
61                   SilcAsn1Tag type, SilcAsn1Tag tag, SilcBerClass ber_class,
62                   SilcAsn1Options opts, SilcBuffer dest, SilcUInt32 depth,
63                   SilcBool primitive)
64 {
65   unsigned char *ptr = dest->data;
66   SilcAsn1Tag rtype, rtag;
67   SilcAsn1Options ropts;
68   SilcBerClass rclass;
69   SilcUInt32 len = 0;
70   SilcBool ret = FALSE, indef;
71   SilcBufferStruct buf;
72   SilcStackFrame frame;
73
74 #ifdef SILC_DEBUG
75   char sp[SILC_ASN1_RECURSION_DEPTH + 1];
76   memset(sp, 0, sizeof(sp));
77   if (depth)
78     memset(sp, 32, depth);
79 #endif /* SILC_DEBUG */
80
81   if (depth >= SILC_ASN1_RECURSION_DEPTH) {
82     SILC_LOG_DEBUG(("Maximum recursion depth reached"));
83     return FALSE;
84   }
85
86   while (1) {
87     /* These options cannot be used in encoding */
88     opts &= ~SILC_ASN1_OPTIONAL;
89
90     /* Get length encoding */
91     indef = (opts & SILC_ASN1_INDEFINITE ? TRUE : FALSE);
92
93     /* By default UNIVERSAL is implied unless the following conditions
94        are met when CONTEXT will apply.  For SILC_ASN1_TAG_ANY_PRIMITIVE
95        the class is changed only if flags dictate it. */
96     if (ber_class == SILC_BER_CLASS_UNIVERSAL) {
97       if (type == SILC_ASN1_TAG_ANY_PRIMITIVE) {
98         if (opts & SILC_ASN1_IMPLICIT ||
99             opts & SILC_ASN1_EXPLICIT)
100           ber_class = SILC_BER_CLASS_CONTEXT;
101       } else {
102         if (tag != type ||
103             opts & SILC_ASN1_IMPLICIT ||
104             opts & SILC_ASN1_EXPLICIT)
105           ber_class = SILC_BER_CLASS_CONTEXT;
106       }
107     }
108
109 #ifdef SILC_DEBUG
110     SILC_LOG_DEBUG(
111       ("%04d: %sEncode %s [%d] %s %s %s %s", depth, sp[0] ? sp : "",
112        silc_asn1_tag_name(type), tag,
113        ber_class == SILC_BER_CLASS_UNIVERSAL   ? "univ" :
114        ber_class == SILC_BER_CLASS_APPLICATION ? "appl" :
115        ber_class == SILC_BER_CLASS_CONTEXT     ? "cont" : "priv",
116        (type != SILC_ASN1_TAG_SEQUENCE && type != SILC_ASN1_TAG_SET) ?
117        opts & SILC_ASN1_EXPLICIT ? "constr" :
118        type == SILC_ASN1_TAG_ANY &&
119        !(opts & SILC_ASN1_EXPLICIT) ? "constr" : "primit" : "constr",
120        indef ? opts & SILC_ASN1_EXPLICIT ? "defin" : "indef" : "defin",
121        opts & SILC_ASN1_IMPLICIT ? "implicit" :
122        opts & SILC_ASN1_EXPLICIT ? "explicit" : ""));
123 #endif /* SILC_DEBUG */
124
125     /* If tagging is explicit we add constructed type before the underlaying
126        types.  The underlaying types are encoded recursively with this
127        encoder. */
128     if (opts & SILC_ASN1_EXPLICIT) {
129       memset(&buf, 0, sizeof(buf));
130
131       primitive = (type != SILC_ASN1_TAG_SEQUENCE &&
132                    type != SILC_ASN1_TAG_SET);
133       opts &= ~SILC_ASN1_EXPLICIT;
134
135       silc_stack_push(stack2, &frame);
136       ret = silc_asn1_encoder(asn1, stack2, stack1, type, type,
137                               SILC_BER_CLASS_UNIVERSAL, opts,
138                               &buf, depth + 1, primitive);
139       silc_stack_pop(stack2);
140
141       if (!ret) {
142         SILC_LOG_DEBUG(("Error encoding explicit tag"));
143         goto fail;
144       }
145
146       /* Encode the explicit tag */
147       len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), FALSE);
148       dest = silc_buffer_srealloc_size(stack1, dest,
149                                        silc_buffer_truelen(dest) + len);
150       ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
151                             tag, buf.data, silc_buffer_len(&buf), FALSE);
152       if (!ret)
153         goto fail;
154       if (primitive) {
155         primitive = FALSE;
156         goto cont;
157       }
158       goto ok;
159     }
160
161     /* Encode by the type */
162     switch (type) {
163
164     case SILC_ASN1_TAG_ANY:
165       {
166         /* ANY is another ASN.1 node which is added to this tree */
167         SilcBuffer node = va_arg(asn1->ap, SilcBuffer);
168         if (!node)
169           break;
170
171         /* Encode ASN.1 node into the tree. */
172         if (opts & SILC_ASN1_IMPLICIT || type != tag) {
173           /* We are tagging implicitly so we need to change the identifier
174              of the underlaying type.  Only constructed type is allowed with
175              ANY when tagging implicitly. */
176           const unsigned char *d;
177           SilcUInt32 d_len;
178           SilcBerEncoding enc;
179
180           /* Get the underlaying data */
181           ret = silc_ber_decode(node, NULL, &enc, NULL, &d, &d_len,
182                                 NULL, NULL);
183           if (!ret) {
184             SILC_LOG_DEBUG(("Error decoding underlaying node for ANY"));
185             goto fail;
186           }
187           if (enc != SILC_BER_ENC_CONSTRUCTED) {
188             SILC_LOG_DEBUG(("ANY was not constructed type"));
189             goto fail;
190           }
191
192           /* Now encode with implicit tagging */
193           len = silc_ber_encoded_len(tag, d_len, FALSE);
194           dest = silc_buffer_srealloc_size(stack1, dest,
195                                            silc_buffer_truelen(dest) + len);
196           ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
197                                 tag, d, d_len, FALSE);
198           if (!ret)
199             goto fail;
200         } else {
201           /* Copy the data directly into the tree. */
202           len = silc_buffer_len(node);
203           dest = silc_buffer_srealloc_size(stack1, dest,
204                                            silc_buffer_truelen(dest) + len);
205           if (!dest)
206             goto fail;
207           silc_buffer_put(dest, node->data, len);
208         }
209         break;
210       }
211
212     case SILC_ASN1_TAG_ANY_PRIMITIVE:
213       {
214         /* ANY_PRIMITIVE is any primitive in encoded format. */
215         SilcBuffer prim = va_arg(asn1->ap, SilcBuffer);
216         if (!prim)
217           break;
218
219         /* Encode the primitive data */
220         len = silc_ber_encoded_len(tag, silc_buffer_len(prim), FALSE);
221         dest = silc_buffer_srealloc_size(stack1, dest,
222                                          silc_buffer_truelen(dest) + len);
223         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
224                               tag, prim->data, silc_buffer_len(prim), FALSE);
225         if (!ret)
226           goto fail;
227         break;
228       }
229
230     case SILC_ASN1_TAG_SEQUENCE:
231     case SILC_ASN1_TAG_SET:
232       {
233         /* SEQUENCE/SET is a sequence of types. Sequences are opened and
234            encoded recursively by calling this same encoder. */
235         memset(&buf, 0, sizeof(buf));
236
237         /* Get type, tag and options for the first argument in recursion */
238         SILC_ASN1_ARGS(asn1, rtype, rtag, rclass, ropts);
239
240         silc_stack_push(stack2, &frame);
241         ret = silc_asn1_encoder(asn1, stack2, stack1, rtype, rtag, rclass,
242                                 ropts, &buf, depth + 1, FALSE);
243         silc_stack_pop(stack2);
244         if (!ret) {
245           SILC_LOG_DEBUG(("Error traversing a SEQUENCE/SET"));
246           goto fail;
247         }
248
249         /* Encode the sequence */
250         len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
251         dest = silc_buffer_srealloc_size(stack1, dest,
252                                          silc_buffer_truelen(dest) + len);
253         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
254                               tag, buf.data, silc_buffer_len(&buf), indef);
255         if (!ret)
256           goto fail;
257         break;
258       }
259
260     case SILC_ASN1_TAG_INTEGER:
261     case SILC_ASN1_TAG_ENUM:
262       {
263         /* Integer */
264         SilcMPInt *mpint = va_arg(asn1->ap, SilcMPInt *);
265         if (!mpint)
266           break;
267
268         memset(&buf, 0, sizeof(buf));
269         if (silc_mp_cmp_ui(mpint, 0) < 0) {
270           /* XXX TODO, negative integer.  Take 2s complement, then store
271              bytes in 1s complement */
272         } else {
273           /* Positive */
274           len = silc_mp_sizeinbase(mpint, 2);
275           if (!(len & 7))
276             len = ((len + 7) / 8) + 1;
277           else
278             len = (len + 7) / 8;
279           silc_stack_push(stack2, &frame);
280           silc_buffer_srealloc_size(stack2, &buf,
281                                     silc_buffer_truelen(&buf) + len);
282           buf.data[0] = 0x00;
283           silc_mp_mp2bin_noalloc(mpint, buf.data, silc_buffer_len(&buf));
284         }
285
286         /* Encode the integer */
287         len = silc_ber_encoded_len(tag, len, indef);
288         dest = silc_buffer_srealloc_size(stack1, dest,
289                                          silc_buffer_truelen(dest) + len);
290         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
291                               tag, buf.data, silc_buffer_len(&buf), FALSE);
292         silc_stack_pop(stack2);
293         if (!ret)
294           goto fail;
295         break;
296       }
297
298     case SILC_ASN1_TAG_OID:
299       {
300         /* Object identifier */
301         char *cp, *oidstr = va_arg(asn1->ap, char *);
302         SilcUInt32 words[24], oid, mask;
303         int i, k, c = 0;
304         if (!oidstr)
305           break;
306
307         /* Get OID words from the string */
308         cp = strchr(oidstr, '.');
309         while (cp) {
310           if (sscanf(oidstr, "%lu", (unsigned long *)&oid) != 1) {
311             SILC_LOG_DEBUG(("Malformed OID string"));
312             goto fail;
313           }
314           if (c + 1 > sizeof(words) / sizeof(words[0]))
315             goto fail;
316           words[c++] = oid;
317           oidstr = cp + 1;
318           cp = strchr(oidstr, '.');
319
320           if (!cp) {
321             if (sscanf(oidstr, "%lu", (unsigned long *)&oid) != 1) {
322               SILC_LOG_DEBUG(("Malformed OID string"));
323               goto fail;
324             }
325             if (c + 1 > sizeof(words) / sizeof(words[0]))
326               goto fail;
327             words[c++] = oid;
328             break;
329           }
330         }
331         if (c < 2) {
332           SILC_LOG_DEBUG(("Malfromed OID string"));
333           goto fail;
334         }
335
336         /* Get OID data length */
337         for (i = 2, len = 1; i < c; i++) {
338           if (words[i]) {
339             for (oid = words[i]; oid; oid >>= 7)
340               len++;
341             continue;
342           }
343           len++;
344         }
345
346         /* Encode the OID */
347         memset(&buf, 0, sizeof(buf));
348         silc_stack_push(stack2, &frame);
349         silc_buffer_srealloc_size(stack2, &buf,
350                                   silc_buffer_truelen(&buf) + len);
351         buf.data[0] = words[0] * 40 + words[1];
352         for (i = 2, len = 1; i < c; i++) {
353           oid = words[i];
354           if (oid) {
355             k = len;
356             mask = 0;
357             while (oid) {
358               buf.data[len++] = (oid & 0x7f) | mask;
359               oid >>= 7;
360               mask |= 0x80;
361             }
362             mask = len - 1;
363             while (k < mask) {
364               oid = buf.data[k];
365               buf.data[k] = buf.data[mask];
366               buf.data[mask] = oid;
367               k++;
368               mask--;
369             }
370
371             continue;
372           }
373           buf.data[len++] = 0x00;
374         }
375
376         len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
377         dest = silc_buffer_srealloc_size(stack1, dest,
378                                          silc_buffer_truelen(dest) + len);
379         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
380                               tag, buf.data, silc_buffer_len(&buf), FALSE);
381         silc_stack_pop(stack2);
382         if (!ret)
383           goto fail;
384         break;
385       }
386
387     case SILC_ASN1_TAG_BOOLEAN:
388       {
389         /* Encodes boolean (TRUE/FALSE) value */
390         unsigned char val[1];
391         val[0] = (va_arg(asn1->ap, SilcUInt32) ? 0xff : 0x00);
392
393         assert(indef == FALSE);
394         len = silc_ber_encoded_len(tag, 1, FALSE);
395         dest = silc_buffer_srealloc_size(stack1, dest,
396                                          silc_buffer_truelen(dest) + len);
397         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
398                               tag, val, 1, FALSE);
399         if (!ret)
400           goto fail;
401         break;
402       }
403
404     case SILC_ASN1_TAG_BIT_STRING:
405       {
406         /* Encode the data as is, with the bit padding. d_len is in bits. */
407         unsigned char *d = va_arg(asn1->ap, unsigned char *);
408         SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
409         unsigned char pad[1];
410         if (!d)
411           break;
412
413         pad[0] = (8 - (d_len & 7)) & 7;
414         d_len = ((d_len + 7) / 8) + 1;
415
416         memset(&buf, 0, sizeof(buf));
417         silc_stack_push(stack2, &frame);
418         silc_buffer_srealloc_size(stack2, &buf,
419                                   silc_buffer_truelen(&buf) + d_len);
420         silc_buffer_put(&buf, pad, 1);
421         silc_buffer_pull(&buf, 1);
422         silc_buffer_put(&buf, d, d_len - 1);
423         silc_buffer_push(&buf, 1);
424
425         len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
426         dest = silc_buffer_srealloc_size(stack1, dest,
427                                          silc_buffer_truelen(dest) + len);
428         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
429                               tag, buf.data, silc_buffer_len(&buf), indef);
430         silc_stack_pop(stack2);
431         if (!ret)
432           goto fail;
433         break;
434       }
435
436     case SILC_ASN1_TAG_NULL:
437       {
438         /* Encode empty BER block */
439         assert(indef == FALSE);
440         len = silc_ber_encoded_len(tag, 0, FALSE);
441         dest = silc_buffer_srealloc_size(stack1, dest,
442                                          silc_buffer_truelen(dest) + len);
443         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
444                               tag, NULL, 0, FALSE);
445         if (!ret)
446           goto fail;
447         break;
448       }
449
450     case SILC_ASN1_TAG_UTC_TIME:
451       {
452         /* Universal encoded time string */
453         SilcTime timeval = va_arg(asn1->ap, SilcTime);
454         char timestr[32];
455         if (!timeval)
456           break;
457
458         if (!silc_time_universal_string(timeval, timestr, sizeof(timestr))) {
459           SILC_LOG_DEBUG(("Could not encode universal time string"));
460           goto fail;
461         }
462
463         len = silc_ber_encoded_len(tag, strlen(timestr), indef);
464         dest = silc_buffer_srealloc_size(stack1, dest,
465                                          silc_buffer_truelen(dest) + len);
466         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
467                               tag, timestr, strlen(timestr), indef);
468         if (!ret)
469           goto fail;
470         break;
471       }
472
473     case SILC_ASN1_TAG_GENERALIZED_TIME:
474       {
475         /* Generalized encoded time string */
476         SilcTime timeval = va_arg(asn1->ap, SilcTime);
477         char timestr[32];
478         if (!timeval)
479           break;
480
481         if (!silc_time_generalized_string(timeval, timestr, sizeof(timestr))) {
482           SILC_LOG_DEBUG(("Could not encode generalized time string"));
483           goto fail;
484         }
485
486         len = silc_ber_encoded_len(tag, strlen(timestr), indef);
487         dest = silc_buffer_srealloc_size(stack1, dest,
488                                          silc_buffer_truelen(dest) + len);
489         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
490                               tag, timestr, strlen(timestr), indef);
491         if (!ret)
492           goto fail;
493         break;
494       }
495
496     case SILC_ASN1_TAG_UTF8_STRING:
497       {
498         /* UTF-8 string */
499         unsigned char *d = va_arg(asn1->ap, unsigned char *);
500         SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
501         if (!d)
502           break;
503
504         /* By default all strings that get here should already be UTF-8 */
505         if (!silc_utf8_valid(d, d_len)) {
506           SILC_LOG_DEBUG(("Malformed UTF-8 string"));
507           goto fail;
508         }
509
510         len = silc_ber_encoded_len(tag, d_len, indef);
511         dest = silc_buffer_srealloc_size(stack1, dest,
512                                          silc_buffer_truelen(dest) + len);
513         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
514                               tag, d, d_len, indef);
515         if (!ret)
516           goto fail;
517         break;
518       }
519
520     case SILC_ASN1_TAG_OCTET_STRING:
521       {
522         /* Octet string.  We put it in as 8-bit ASCII */
523         SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
524         break;
525       }
526
527     case SILC_ASN1_TAG_NUMERIC_STRING:
528       {
529         /* Numerical (digit) string */
530         SILC_ASN1_ENCODE_STRING(SILC_STRING_NUMERICAL);
531         break;
532       }
533
534     case SILC_ASN1_TAG_PRINTABLE_STRING:
535       {
536         /* Printable string */
537         SILC_ASN1_ENCODE_STRING(SILC_STRING_PRINTABLE);
538         break;
539       }
540
541     case SILC_ASN1_TAG_TELETEX_STRING:
542       {
543         /* Teletex (T61) string */
544         SILC_ASN1_ENCODE_STRING(SILC_STRING_TELETEX);
545         break;
546       }
547
548     case SILC_ASN1_TAG_IA5_STRING:
549       {
550         /* US ASCII string */
551         SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
552         break;
553       }
554
555     case SILC_ASN1_TAG_VISIBLE_STRING:
556       {
557         /* Visible string */
558         SILC_ASN1_ENCODE_STRING(SILC_STRING_VISIBLE);
559         break;
560       }
561
562     case SILC_ASN1_TAG_UNIVERSAL_STRING:
563       {
564         /* Universal (UCS-4) string */
565         SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
566         break;
567       }
568
569     case SILC_ASN1_TAG_UNRESTRICTED_STRING:
570     case SILC_ASN1_TAG_GENERAL_STRING:
571       {
572         /* Handle now unrestricted and general as 8-bit ascii, which
573            probably isn't correct. */
574         SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
575         break;
576       }
577
578     case SILC_ASN1_TAG_BMP_STRING:
579       {
580         /* BMP (UCS-2) string */
581         SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
582         break;
583       }
584
585     case SILC_ASN1_TAG_ODE:
586     case SILC_ASN1_TAG_ETI:
587     case SILC_ASN1_TAG_REAL:
588     case SILC_ASN1_TAG_EMBEDDED:
589     case SILC_ASN1_TAG_ROI:
590     case SILC_ASN1_TAG_VIDEOTEX_STRING:
591     case SILC_ASN1_TAG_GRAPHIC_STRING:
592       {
593         SILC_NOT_IMPLEMENTED("Unsupported ASN.1 tag");
594         ret = FALSE;
595         goto fail;
596         break;
597       }
598
599     default:
600       SILC_LOG_DEBUG(("Invalid ASN.1 tag `%d'. Cannot encode ASN.1.", type));
601       ret = FALSE;
602       goto fail;
603       break;
604     }
605
606   cont:
607     if (len)
608       silc_buffer_pull(dest, len);
609     if (primitive) {
610       ret = TRUE;
611       goto ok;
612     }
613
614     /* Get next type, tag and options */
615     SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
616     if (type == SILC_ASN1_END) {
617       ret = TRUE;
618       goto ok;
619     }
620   }
621
622  fail:
623   SILC_LOG_DEBUG(("Error encoding type %d (depth %d)", type, depth));
624
625  ok:
626   if (ptr)
627     len = dest->data - ptr;
628   else
629     len = dest->data - dest->head;
630   silc_buffer_push(dest, len);
631
632   return ret;
633 }
634
635 SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...)
636 {
637   SilcAsn1Tag type, tag;
638   SilcAsn1Options opts;
639   SilcBerClass ber_class;
640   SilcStackFrame frame1, frame2;
641   SilcStack stack1 = NULL;
642   SilcBool ret;
643
644   if (!asn1)
645     return FALSE;
646
647   va_start(asn1->ap, dest);
648
649   /* Get the first arguments and call the encoder. */
650   SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
651   if (!type) {
652     va_end(asn1->ap);
653     asn1->ap = NULL;
654     return FALSE;
655   }
656
657   /* Handle internal options for encoder. */
658   if (type == SILC_ASN1_TAG_OPTS) {
659     SilcUInt32 o = va_arg(asn1->ap, SilcUInt32);
660
661     if (o & SILC_ASN1_ALLOC) {
662       /* User wants to alloate everything.  Set the stack to NULL so
663          that stack aware calls revert to normal allocation routines. */
664       stack1 = asn1->stack1;
665       asn1->stack1 = NULL;
666     }
667
668     if (o & SILC_ASN1_ACCUMUL) {
669       /* If accumul flag is not set yet, then push the stack. */
670       if (!asn1->accumul) {
671         silc_stack_push(asn1->stack1, NULL);
672         asn1->accumul = 1;
673       }
674     }
675
676     /* Take again the arguments */
677     SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
678   } else {
679     /* No flags set, all flags will be reset. */
680
681     /* If accumul flag is set now pop the stack so that all accumulated
682        memory becomes free again. */
683     if (asn1->accumul) {
684       silc_stack_pop(asn1->stack1);
685       asn1->accumul = 0;
686     }
687   }
688
689   /* Push the stack for normal allocation from stack. */
690   if (!asn1->accumul)
691     silc_stack_push(asn1->stack1, &frame1);
692
693   /* Start encoding */
694   silc_stack_push(asn1->stack2, &frame2);
695   ret = silc_asn1_encoder(asn1, asn1->stack1, asn1->stack2,
696                           type, tag, ber_class, opts, dest, 0, FALSE);
697   silc_stack_pop(asn1->stack2);
698
699   /* Pop the stack to free normal allocations from stack. */
700   if (!asn1->accumul)
701     silc_stack_pop(asn1->stack1);
702
703   /* If SILC_ASN1_ALLOC flag was set, restore the stack. */
704   if (stack1 && !asn1->stack1)
705     asn1->stack1 = stack1;
706
707   va_end(asn1->ap);
708   asn1->ap = NULL;
709
710   return ret;
711 }