Added SILC_ASN1_ANY_PRIMITIVE to encode/decode any pre-encoded
[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 - 2005 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           assert(enc == SILC_BER_ENC_CONSTRUCTED);
188
189           /* Now encode with implicit tagging */
190           len = silc_ber_encoded_len(tag, d_len, FALSE);
191           dest = silc_buffer_srealloc_size(stack1, dest,
192                                            silc_buffer_truelen(dest) + len);
193           ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
194                                 tag, d, d_len, FALSE);
195           if (!ret)
196             goto fail;
197         } else {
198           /* Copy the data directly into the tree. */
199           len = silc_buffer_len(node);
200           dest = silc_buffer_srealloc_size(stack1, dest,
201                                            silc_buffer_truelen(dest) + len);
202           silc_buffer_put(dest, node->data, len);
203         }
204         break;
205       }
206
207     case SILC_ASN1_TAG_ANY_PRIMITIVE:
208       {
209         /* ANY_PRIMITIVE is any primitive in encoded format. */
210         SilcBuffer prim = va_arg(asn1->ap, SilcBuffer);
211         if (!prim)
212           break;
213
214         /* Encode the primitive data */
215         len = silc_ber_encoded_len(tag, silc_buffer_len(prim), FALSE);
216         dest = silc_buffer_srealloc_size(stack1, dest,
217                                          silc_buffer_truelen(dest) + len);
218         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
219                               tag, prim->data, silc_buffer_len(prim), FALSE);
220         if (!ret)
221           goto fail;
222         break;
223       }
224
225     case SILC_ASN1_TAG_SEQUENCE:
226     case SILC_ASN1_TAG_SET:
227       {
228         /* SEQUENCE/SET is a sequence of types. Sequences are opened and
229            encoded recursively by calling this same encoder. */
230         memset(&buf, 0, sizeof(buf));
231
232         /* Get type, tag and options for the first argument in recursion */
233         SILC_ASN1_ARGS(asn1, rtype, rtag, rclass, ropts);
234
235         silc_stack_push(stack2, &frame);
236         ret = silc_asn1_encoder(asn1, stack2, stack1, rtype, rtag, rclass,
237                                 ropts, &buf, depth + 1, FALSE);
238         silc_stack_pop(stack2);
239         if (!ret) {
240           SILC_LOG_DEBUG(("Error traversing a SEQUENCE/SET"));
241           goto fail;
242         }
243
244         /* Encode the sequence */
245         len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
246         dest = silc_buffer_srealloc_size(stack1, dest,
247                                          silc_buffer_truelen(dest) + len);
248         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
249                               tag, buf.data, silc_buffer_len(&buf), indef);
250         if (!ret)
251           goto fail;
252         break;
253       }
254
255     case SILC_ASN1_TAG_INTEGER:
256     case SILC_ASN1_TAG_ENUM:
257       {
258         /* Integer */
259         SilcMPInt *mpint = va_arg(asn1->ap, SilcMPInt *);
260         if (!mpint)
261           break;
262
263         memset(&buf, 0, sizeof(buf));
264         if (silc_mp_cmp_ui(mpint, 0) < 0) {
265           /* XXX TODO, negative integer.  Take 2s complement, then store
266              bytes in 1s complement */
267         } else {
268           /* Positive */
269           len = (silc_mp_sizeinbase(mpint, 2) + 7) / 8;
270           len += len & 7 ? 1: 0;
271           silc_stack_push(stack2, &frame);
272           silc_buffer_srealloc_size(stack2, &buf,
273                                     silc_buffer_truelen(&buf) + len);
274           silc_mp_mp2bin_noalloc(mpint, buf.data, silc_buffer_len(&buf));
275         }
276
277         /* Encode the integer */
278         len = silc_ber_encoded_len(tag, len, indef);
279         dest = silc_buffer_srealloc_size(stack1, dest,
280                                          silc_buffer_truelen(dest) + len);
281         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
282                               tag, buf.data, silc_buffer_len(&buf), FALSE);
283         silc_stack_pop(stack2);
284         if (!ret)
285           goto fail;
286         break;
287       }
288
289     case SILC_ASN1_TAG_OID:
290       {
291         /* Object identifier */
292         char *cp, *oidstr = va_arg(asn1->ap, char *);
293         SilcUInt32 words[24], oid, mask;
294         int i, c = -1;
295         if (!oidstr)
296           break;
297
298         /* Get OID words from the string */
299         cp = strchr(oidstr, '.');
300         while (cp) {
301           c = sscanf(oidstr, "%lu", (unsigned long *)&oid);
302           if (c < 1) {
303             SILC_LOG_DEBUG(("Malformed OID string"));
304             goto fail;
305           }
306           if (c + 1 > sizeof(words) / sizeof(words[0]))
307             goto fail;
308           words[c++] = oid;
309           oidstr = cp + 1;
310           cp = strchr(oidstr, '.');
311         }
312         if (c < 2) {
313           SILC_LOG_DEBUG(("Malfromed OID string"));
314           goto fail;
315         }
316
317         /* Get OID data length */
318         for (i = 2, len = 1; i < c; i++) {
319           if (words[i]) {
320             for (oid = words[i]; oid; oid >>= 7)
321               len++;
322             continue;
323           }
324           len++;
325         }
326
327         /* Encode the OID */
328         memset(&buf, 0, sizeof(buf));
329         silc_stack_push(stack2, &frame);
330         silc_buffer_srealloc_size(stack2, &buf,
331                                   silc_buffer_truelen(&buf) + len);
332         buf.data[0] = words[0] * 40 + words[1];
333         for (i = 2, len = 1; i < c; i++) {
334           oid = words[i];
335           if (oid) {
336             c = len;
337             mask = 0;
338             while (oid) {
339               buf.data[len++] = (oid & 0x7f) | mask;
340               oid >>= 7;
341               mask |= 0x80;
342             }
343             mask = len - 1;
344             while (c < mask) {
345               oid = buf.data[c];
346               buf.data[c] = buf.data[mask];
347               buf.data[mask] = oid;
348               c++;
349               mask--;
350             }
351
352             continue;
353           }
354           buf.data[len++] = 0x00;
355         }
356
357         len = silc_ber_encoded_len(tag, len, indef);
358         dest = silc_buffer_srealloc_size(stack1, dest,
359                                          silc_buffer_truelen(dest) + len);
360         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
361                               tag, buf.data, silc_buffer_len(&buf), FALSE);
362         silc_stack_pop(stack2);
363         if (!ret)
364           goto fail;
365         break;
366       }
367
368     case SILC_ASN1_TAG_BOOLEAN:
369       {
370         /* Encodes boolean (TRUE/FALSE) value */
371         unsigned char val[1];
372         val[0] = (va_arg(asn1->ap, SilcUInt32) ? 0xff : 0x00);
373
374         assert(indef == FALSE);
375         len = silc_ber_encoded_len(tag, 1, FALSE);
376         dest = silc_buffer_srealloc_size(stack1, dest,
377                                          silc_buffer_truelen(dest) + len);
378         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
379                               tag, val, 1, FALSE);
380         if (!ret)
381           goto fail;
382         break;
383       }
384
385     case SILC_ASN1_TAG_BIT_STRING:
386       {
387         /* Encode the data as is, with the bit padding. d_len is in bits. */
388         unsigned char *d = va_arg(asn1->ap, unsigned char *);
389         SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
390         unsigned char pad[1];
391         if (!d)
392           break;
393
394         pad[0] = (8 - (d_len & 7)) & 7;
395         d_len = ((d_len + 7) / 8) + 1;
396
397         memset(&buf, 0, sizeof(buf));
398         silc_stack_push(stack2, &frame);
399         silc_buffer_srealloc_size(stack2, &buf,
400                                   silc_buffer_truelen(&buf) + d_len);
401         silc_buffer_put(&buf, pad, 1);
402         silc_buffer_pull(&buf, 1);
403         silc_buffer_put(&buf, d, d_len - 1);
404         silc_buffer_push(&buf, 1);
405
406         len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
407         dest = silc_buffer_srealloc_size(stack1, dest,
408                                          silc_buffer_truelen(dest) + len);
409         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
410                               tag, buf.data, silc_buffer_len(&buf), indef);
411         silc_stack_pop(stack2);
412         if (!ret)
413           goto fail;
414         break;
415       }
416
417     case SILC_ASN1_TAG_NULL:
418       {
419         /* Encode empty BER block */
420         assert(indef == FALSE);
421         len = silc_ber_encoded_len(tag, 0, FALSE);
422         dest = silc_buffer_srealloc_size(stack1, dest,
423                                          silc_buffer_truelen(dest) + len);
424         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
425                               tag, NULL, 0, FALSE);
426         if (!ret)
427           goto fail;
428         break;
429       }
430
431     case SILC_ASN1_TAG_UTC_TIME:
432       {
433         /* Universal encoded time string */
434         SilcTime timeval = va_arg(asn1->ap, SilcTime);
435         char timestr[32];
436         if (!timeval)
437           break;
438
439         if (!silc_time_universal_string(timeval, timestr, sizeof(timestr))) {
440           SILC_LOG_DEBUG(("Could not encode universal time string"));
441           goto fail;
442         }
443
444         len = silc_ber_encoded_len(tag, strlen(timestr), indef);
445         dest = silc_buffer_srealloc_size(stack1, dest,
446                                          silc_buffer_truelen(dest) + len);
447         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
448                               tag, timestr, strlen(timestr), indef);
449         if (!ret)
450           goto fail;
451         break;
452       }
453
454     case SILC_ASN1_TAG_GENERALIZED_TIME:
455       {
456         /* Generalized encoded time string */
457         SilcTime timeval = va_arg(asn1->ap, SilcTime);
458         char timestr[32];
459         if (!timeval)
460           break;
461
462         if (!silc_time_generalized_string(timeval, timestr, sizeof(timestr))) {
463           SILC_LOG_DEBUG(("Could not encode generalized time string"));
464           goto fail;
465         }
466
467         len = silc_ber_encoded_len(tag, strlen(timestr), indef);
468         dest = silc_buffer_srealloc_size(stack1, dest,
469                                          silc_buffer_truelen(dest) + len);
470         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
471                               tag, timestr, strlen(timestr), indef);
472         if (!ret)
473           goto fail;
474         break;
475       }
476
477     case SILC_ASN1_TAG_UTF8_STRING:
478       {
479         /* UTF-8 string */
480         unsigned char *d = va_arg(asn1->ap, unsigned char *);
481         SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
482         if (!d)
483           break;
484
485         /* By default all strings that get here should already be UTF-8 */
486         if (!silc_utf8_valid(d, d_len)) {
487           SILC_LOG_DEBUG(("Malformed UTF-8 string"));
488           goto fail;
489         }
490
491         len = silc_ber_encoded_len(tag, d_len, indef);
492         dest = silc_buffer_srealloc_size(stack1, dest,
493                                          silc_buffer_truelen(dest) + len);
494         ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
495                               tag, d, d_len, indef);
496         if (!ret)
497           goto fail;
498         break;
499       }
500
501     case SILC_ASN1_TAG_OCTET_STRING:
502       {
503         /* Octet string.  We put it in as 8-bit ASCII */
504         SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
505         break;
506       }
507
508     case SILC_ASN1_TAG_NUMERIC_STRING:
509       {
510         /* Numerical (digit) string */
511         SILC_ASN1_ENCODE_STRING(SILC_STRING_NUMERICAL);
512         break;
513       }
514
515     case SILC_ASN1_TAG_PRINTABLE_STRING:
516       {
517         /* Printable string */
518         SILC_ASN1_ENCODE_STRING(SILC_STRING_PRINTABLE);
519         break;
520       }
521
522     case SILC_ASN1_TAG_TELETEX_STRING:
523       {
524         /* Teletex (T61) string */
525         SILC_ASN1_ENCODE_STRING(SILC_STRING_TELETEX);
526         break;
527       }
528
529     case SILC_ASN1_TAG_IA5_STRING:
530       {
531         /* US ASCII string */
532         SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
533         break;
534       }
535
536     case SILC_ASN1_TAG_VISIBLE_STRING:
537       {
538         /* Visible string */
539         SILC_ASN1_ENCODE_STRING(SILC_STRING_VISIBLE);
540         break;
541       }
542
543     case SILC_ASN1_TAG_UNIVERSAL_STRING:
544       {
545         /* Universal (UCS-4) string */
546         SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
547         break;
548       }
549
550     case SILC_ASN1_TAG_UNRESTRICTED_STRING:
551     case SILC_ASN1_TAG_GENERAL_STRING:
552       {
553         /* Handle now unrestricted and general as 8-bit ascii, which
554            probably isn't correct. */
555         SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
556         break;
557       }
558
559     case SILC_ASN1_TAG_BMP_STRING:
560       {
561         /* BMP (UCS-2) string */
562         SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
563         break;
564       }
565
566     case SILC_ASN1_TAG_ODE:
567     case SILC_ASN1_TAG_ETI:
568     case SILC_ASN1_TAG_REAL:
569     case SILC_ASN1_TAG_EMBEDDED:
570     case SILC_ASN1_TAG_ROI:
571     case SILC_ASN1_TAG_VIDEOTEX_STRING:
572     case SILC_ASN1_TAG_GRAPHIC_STRING:
573       {
574         SILC_NOT_IMPLEMENTED("Unsupported ASN.1 tag");
575         ret = FALSE;
576         goto fail;
577         break;
578       }
579
580     default:
581       SILC_LOG_DEBUG(("Invalid ASN.1 tag `%d'. Cannot encode ASN.1.", type));
582       ret = FALSE;
583       goto fail;
584       break;
585     }
586
587   cont:
588     if (len)
589       silc_buffer_pull(dest, len);
590     if (primitive) {
591       ret = TRUE;
592       goto ok;
593     }
594
595     /* Get next type, tag and options */
596     SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
597     if (type == SILC_ASN1_END) {
598       ret = TRUE;
599       goto ok;
600     }
601   }
602
603  fail:
604   SILC_LOG_DEBUG(("Error encoding type %d (depth %d)", type, depth));
605
606  ok:
607   if (ptr)
608     len = dest->data - ptr;
609   else
610     len = dest->data - dest->head;
611   silc_buffer_push(dest, len);
612
613   return ret;
614 }
615
616 SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...)
617 {
618   SilcAsn1Tag type, tag;
619   SilcAsn1Options opts;
620   SilcBerClass ber_class;
621   SilcStackFrame frame1, frame2;
622   SilcStack stack1 = NULL;
623   SilcBool ret;
624
625   if (!asn1)
626     return FALSE;
627
628   va_start(asn1->ap, dest);
629
630   /* Get the first arguments and call the encoder. */
631   SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
632   if (!type) {
633     va_end(asn1->ap);
634     asn1->ap = NULL;
635     return FALSE;
636   }
637
638   /* Handle internal options for encoder. */
639   if (type == SILC_ASN1_TAG_OPTS) {
640     SilcUInt32 o = va_arg(asn1->ap, SilcUInt32);
641
642     if (o & SILC_ASN1_ALLOC) {
643       /* User wants to alloate everything.  Set the stack to NULL so
644          that stack aware calls revert to normal allocation routines. */
645       stack1 = asn1->stack1;
646       asn1->stack1 = NULL;
647     }
648
649     if (o & SILC_ASN1_ACCUMUL) {
650       /* If accumul flag is not set yet, then push the stack. */
651       if (!asn1->accumul) {
652         silc_stack_push(asn1->stack1, NULL);
653         asn1->accumul = 1;
654       }
655     }
656
657     /* Take again the arguments */
658     SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
659   } else {
660     /* No flags set, all flags will be reset. */
661
662     /* If accumul flag is set now pop the stack so that all accumulated
663        memory becomes free again. */
664     if (asn1->accumul) {
665       silc_stack_pop(asn1->stack1);
666       asn1->accumul = 0;
667     }
668   }
669
670   /* Push the stack for normal allocation from stack. */
671   if (!asn1->accumul)
672     silc_stack_push(asn1->stack1, &frame1);
673
674   /* Start encoding */
675   silc_stack_push(asn1->stack2, &frame2);
676   ret = silc_asn1_encoder(asn1, asn1->stack1, asn1->stack2,
677                           type, tag, ber_class, opts, dest, 0, FALSE);
678   silc_stack_pop(asn1->stack2);
679
680   /* Pop the stack to free normal allocations from stack. */
681   if (!asn1->accumul)
682     silc_stack_pop(asn1->stack1);
683
684   /* If SILC_ASN1_ALLOC flag was set, restore the stack. */
685   if (stack1 && !asn1->stack1)
686     asn1->stack1 = stack1;
687
688   va_end(asn1->ap);
689   asn1->ap = NULL;
690
691   return ret;
692 }