5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2005 Pekka Riikonen
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.
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.
24 /************************** ASN.1 Encoder routines **************************/
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); \
33 s_len = silc_utf8_decoded_len(d, d_len, (enc)); \
35 SILC_LOG_DEBUG(("Malformed %d string value", (enc))); \
38 silc_stack_push(asn1->stack2, &frame); \
39 s = silc_smalloc_ua(stack2, s_len + 1); \
41 silc_utf8_decode(d, d_len, (enc), s, s_len); \
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); \
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. */
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,
65 unsigned char *ptr = dest->data;
66 SilcAsn1Tag rtype, rtag;
67 SilcAsn1Options ropts;
70 SilcBool ret = FALSE, indef;
75 char sp[SILC_ASN1_RECURSION_DEPTH + 1];
76 memset(sp, 0, sizeof(sp));
78 memset(sp, 32, depth);
79 #endif /* SILC_DEBUG */
81 if (depth >= SILC_ASN1_RECURSION_DEPTH) {
82 SILC_LOG_DEBUG(("Maximum recursion depth reached"));
87 /* These options cannot be used in encoding */
88 opts &= ~SILC_ASN1_OPTIONAL;
90 /* Get length encoding */
91 indef = (opts & SILC_ASN1_INDEFINITE ? TRUE : FALSE);
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;
103 opts & SILC_ASN1_IMPLICIT ||
104 opts & SILC_ASN1_EXPLICIT)
105 ber_class = SILC_BER_CLASS_CONTEXT;
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 */
125 /* If tagging is explicit we add constructed type before the underlaying
126 types. The underlaying types are encoded recursively with this
128 if (opts & SILC_ASN1_EXPLICIT) {
129 memset(&buf, 0, sizeof(buf));
131 primitive = (type != SILC_ASN1_TAG_SEQUENCE &&
132 type != SILC_ASN1_TAG_SET);
133 opts &= ~SILC_ASN1_EXPLICIT;
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);
142 SILC_LOG_DEBUG(("Error encoding explicit tag"));
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);
161 /* Encode by the type */
164 case SILC_ASN1_TAG_ANY:
166 /* ANY is another ASN.1 node which is added to this tree */
167 SilcBuffer node = va_arg(asn1->ap, SilcBuffer);
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;
180 /* Get the underlaying data */
181 ret = silc_ber_decode(node, NULL, &enc, NULL, &d, &d_len,
184 SILC_LOG_DEBUG(("Error decoding underlaying node for ANY"));
187 assert(enc == SILC_BER_ENC_CONSTRUCTED);
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);
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);
204 silc_buffer_put(dest, node->data, len);
209 case SILC_ASN1_TAG_ANY_PRIMITIVE:
211 /* ANY_PRIMITIVE is any primitive in encoded format. */
212 SilcBuffer prim = va_arg(asn1->ap, SilcBuffer);
216 /* Encode the primitive data */
217 len = silc_ber_encoded_len(tag, silc_buffer_len(prim), FALSE);
218 dest = silc_buffer_srealloc_size(stack1, dest,
219 silc_buffer_truelen(dest) + len);
220 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
221 tag, prim->data, silc_buffer_len(prim), FALSE);
227 case SILC_ASN1_TAG_SEQUENCE:
228 case SILC_ASN1_TAG_SET:
230 /* SEQUENCE/SET is a sequence of types. Sequences are opened and
231 encoded recursively by calling this same encoder. */
232 memset(&buf, 0, sizeof(buf));
234 /* Get type, tag and options for the first argument in recursion */
235 SILC_ASN1_ARGS(asn1, rtype, rtag, rclass, ropts);
237 silc_stack_push(stack2, &frame);
238 ret = silc_asn1_encoder(asn1, stack2, stack1, rtype, rtag, rclass,
239 ropts, &buf, depth + 1, FALSE);
240 silc_stack_pop(stack2);
242 SILC_LOG_DEBUG(("Error traversing a SEQUENCE/SET"));
246 /* Encode the sequence */
247 len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
248 dest = silc_buffer_srealloc_size(stack1, dest,
249 silc_buffer_truelen(dest) + len);
250 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
251 tag, buf.data, silc_buffer_len(&buf), indef);
257 case SILC_ASN1_TAG_INTEGER:
258 case SILC_ASN1_TAG_ENUM:
261 SilcMPInt *mpint = va_arg(asn1->ap, SilcMPInt *);
265 memset(&buf, 0, sizeof(buf));
266 if (silc_mp_cmp_ui(mpint, 0) < 0) {
267 /* XXX TODO, negative integer. Take 2s complement, then store
268 bytes in 1s complement */
271 len = silc_mp_sizeinbase(mpint, 2);
273 len = ((len + 7) / 8) + 1;
276 silc_stack_push(stack2, &frame);
277 silc_buffer_srealloc_size(stack2, &buf,
278 silc_buffer_truelen(&buf) + len);
280 silc_mp_mp2bin_noalloc(mpint, buf.data, silc_buffer_len(&buf));
283 /* Encode the integer */
284 len = silc_ber_encoded_len(tag, len, indef);
285 dest = silc_buffer_srealloc_size(stack1, dest,
286 silc_buffer_truelen(dest) + len);
287 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
288 tag, buf.data, silc_buffer_len(&buf), FALSE);
289 silc_stack_pop(stack2);
295 case SILC_ASN1_TAG_OID:
297 /* Object identifier */
298 char *cp, *oidstr = va_arg(asn1->ap, char *);
299 SilcUInt32 words[24], oid, mask;
304 /* Get OID words from the string */
305 cp = strchr(oidstr, '.');
307 c = sscanf(oidstr, "%lu", (unsigned long *)&oid);
309 SILC_LOG_DEBUG(("Malformed OID string"));
312 if (c + 1 > sizeof(words) / sizeof(words[0]))
316 cp = strchr(oidstr, '.');
319 SILC_LOG_DEBUG(("Malfromed OID string"));
323 /* Get OID data length */
324 for (i = 2, len = 1; i < c; i++) {
326 for (oid = words[i]; oid; oid >>= 7)
334 memset(&buf, 0, sizeof(buf));
335 silc_stack_push(stack2, &frame);
336 silc_buffer_srealloc_size(stack2, &buf,
337 silc_buffer_truelen(&buf) + len);
338 buf.data[0] = words[0] * 40 + words[1];
339 for (i = 2, len = 1; i < c; i++) {
345 buf.data[len++] = (oid & 0x7f) | mask;
352 buf.data[c] = buf.data[mask];
353 buf.data[mask] = oid;
360 buf.data[len++] = 0x00;
363 len = silc_ber_encoded_len(tag, len, indef);
364 dest = silc_buffer_srealloc_size(stack1, dest,
365 silc_buffer_truelen(dest) + len);
366 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
367 tag, buf.data, silc_buffer_len(&buf), FALSE);
368 silc_stack_pop(stack2);
374 case SILC_ASN1_TAG_BOOLEAN:
376 /* Encodes boolean (TRUE/FALSE) value */
377 unsigned char val[1];
378 val[0] = (va_arg(asn1->ap, SilcUInt32) ? 0xff : 0x00);
380 assert(indef == FALSE);
381 len = silc_ber_encoded_len(tag, 1, FALSE);
382 dest = silc_buffer_srealloc_size(stack1, dest,
383 silc_buffer_truelen(dest) + len);
384 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
391 case SILC_ASN1_TAG_BIT_STRING:
393 /* Encode the data as is, with the bit padding. d_len is in bits. */
394 unsigned char *d = va_arg(asn1->ap, unsigned char *);
395 SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
396 unsigned char pad[1];
400 pad[0] = (8 - (d_len & 7)) & 7;
401 d_len = ((d_len + 7) / 8) + 1;
403 memset(&buf, 0, sizeof(buf));
404 silc_stack_push(stack2, &frame);
405 silc_buffer_srealloc_size(stack2, &buf,
406 silc_buffer_truelen(&buf) + d_len);
407 silc_buffer_put(&buf, pad, 1);
408 silc_buffer_pull(&buf, 1);
409 silc_buffer_put(&buf, d, d_len - 1);
410 silc_buffer_push(&buf, 1);
412 len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
413 dest = silc_buffer_srealloc_size(stack1, dest,
414 silc_buffer_truelen(dest) + len);
415 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
416 tag, buf.data, silc_buffer_len(&buf), indef);
417 silc_stack_pop(stack2);
423 case SILC_ASN1_TAG_NULL:
425 /* Encode empty BER block */
426 assert(indef == FALSE);
427 len = silc_ber_encoded_len(tag, 0, FALSE);
428 dest = silc_buffer_srealloc_size(stack1, dest,
429 silc_buffer_truelen(dest) + len);
430 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
431 tag, NULL, 0, FALSE);
437 case SILC_ASN1_TAG_UTC_TIME:
439 /* Universal encoded time string */
440 SilcTime timeval = va_arg(asn1->ap, SilcTime);
445 if (!silc_time_universal_string(timeval, timestr, sizeof(timestr))) {
446 SILC_LOG_DEBUG(("Could not encode universal time string"));
450 len = silc_ber_encoded_len(tag, strlen(timestr), indef);
451 dest = silc_buffer_srealloc_size(stack1, dest,
452 silc_buffer_truelen(dest) + len);
453 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
454 tag, timestr, strlen(timestr), indef);
460 case SILC_ASN1_TAG_GENERALIZED_TIME:
462 /* Generalized encoded time string */
463 SilcTime timeval = va_arg(asn1->ap, SilcTime);
468 if (!silc_time_generalized_string(timeval, timestr, sizeof(timestr))) {
469 SILC_LOG_DEBUG(("Could not encode generalized time string"));
473 len = silc_ber_encoded_len(tag, strlen(timestr), indef);
474 dest = silc_buffer_srealloc_size(stack1, dest,
475 silc_buffer_truelen(dest) + len);
476 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
477 tag, timestr, strlen(timestr), indef);
483 case SILC_ASN1_TAG_UTF8_STRING:
486 unsigned char *d = va_arg(asn1->ap, unsigned char *);
487 SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
491 /* By default all strings that get here should already be UTF-8 */
492 if (!silc_utf8_valid(d, d_len)) {
493 SILC_LOG_DEBUG(("Malformed UTF-8 string"));
497 len = silc_ber_encoded_len(tag, d_len, indef);
498 dest = silc_buffer_srealloc_size(stack1, dest,
499 silc_buffer_truelen(dest) + len);
500 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
501 tag, d, d_len, indef);
507 case SILC_ASN1_TAG_OCTET_STRING:
509 /* Octet string. We put it in as 8-bit ASCII */
510 SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
514 case SILC_ASN1_TAG_NUMERIC_STRING:
516 /* Numerical (digit) string */
517 SILC_ASN1_ENCODE_STRING(SILC_STRING_NUMERICAL);
521 case SILC_ASN1_TAG_PRINTABLE_STRING:
523 /* Printable string */
524 SILC_ASN1_ENCODE_STRING(SILC_STRING_PRINTABLE);
528 case SILC_ASN1_TAG_TELETEX_STRING:
530 /* Teletex (T61) string */
531 SILC_ASN1_ENCODE_STRING(SILC_STRING_TELETEX);
535 case SILC_ASN1_TAG_IA5_STRING:
537 /* US ASCII string */
538 SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
542 case SILC_ASN1_TAG_VISIBLE_STRING:
545 SILC_ASN1_ENCODE_STRING(SILC_STRING_VISIBLE);
549 case SILC_ASN1_TAG_UNIVERSAL_STRING:
551 /* Universal (UCS-4) string */
552 SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
556 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
557 case SILC_ASN1_TAG_GENERAL_STRING:
559 /* Handle now unrestricted and general as 8-bit ascii, which
560 probably isn't correct. */
561 SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
565 case SILC_ASN1_TAG_BMP_STRING:
567 /* BMP (UCS-2) string */
568 SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
572 case SILC_ASN1_TAG_ODE:
573 case SILC_ASN1_TAG_ETI:
574 case SILC_ASN1_TAG_REAL:
575 case SILC_ASN1_TAG_EMBEDDED:
576 case SILC_ASN1_TAG_ROI:
577 case SILC_ASN1_TAG_VIDEOTEX_STRING:
578 case SILC_ASN1_TAG_GRAPHIC_STRING:
580 SILC_NOT_IMPLEMENTED("Unsupported ASN.1 tag");
587 SILC_LOG_DEBUG(("Invalid ASN.1 tag `%d'. Cannot encode ASN.1.", type));
595 silc_buffer_pull(dest, len);
601 /* Get next type, tag and options */
602 SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
603 if (type == SILC_ASN1_END) {
610 SILC_LOG_DEBUG(("Error encoding type %d (depth %d)", type, depth));
614 len = dest->data - ptr;
616 len = dest->data - dest->head;
617 silc_buffer_push(dest, len);
622 SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...)
624 SilcAsn1Tag type, tag;
625 SilcAsn1Options opts;
626 SilcBerClass ber_class;
627 SilcStackFrame frame1, frame2;
628 SilcStack stack1 = NULL;
634 va_start(asn1->ap, dest);
636 /* Get the first arguments and call the encoder. */
637 SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
644 /* Handle internal options for encoder. */
645 if (type == SILC_ASN1_TAG_OPTS) {
646 SilcUInt32 o = va_arg(asn1->ap, SilcUInt32);
648 if (o & SILC_ASN1_ALLOC) {
649 /* User wants to alloate everything. Set the stack to NULL so
650 that stack aware calls revert to normal allocation routines. */
651 stack1 = asn1->stack1;
655 if (o & SILC_ASN1_ACCUMUL) {
656 /* If accumul flag is not set yet, then push the stack. */
657 if (!asn1->accumul) {
658 silc_stack_push(asn1->stack1, NULL);
663 /* Take again the arguments */
664 SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
666 /* No flags set, all flags will be reset. */
668 /* If accumul flag is set now pop the stack so that all accumulated
669 memory becomes free again. */
671 silc_stack_pop(asn1->stack1);
676 /* Push the stack for normal allocation from stack. */
678 silc_stack_push(asn1->stack1, &frame1);
681 silc_stack_push(asn1->stack2, &frame2);
682 ret = silc_asn1_encoder(asn1, asn1->stack1, asn1->stack2,
683 type, tag, ber_class, opts, dest, 0, FALSE);
684 silc_stack_pop(asn1->stack2);
686 /* Pop the stack to free normal allocations from stack. */
688 silc_stack_pop(asn1->stack1);
690 /* If SILC_ASN1_ALLOC flag was set, restore the stack. */
691 if (stack1 && !asn1->stack1)
692 asn1->stack1 = stack1;