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.
20 #include "silcincludes.h"
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 bool 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. */
95 if (ber_class == SILC_BER_CLASS_UNIVERSAL) {
97 opts & SILC_ASN1_IMPLICIT ||
98 opts & SILC_ASN1_EXPLICIT)
99 ber_class = SILC_BER_CLASS_CONTEXT;
104 ("%04d: %sEncode %s [%d] %s %s %s %s", depth, sp[0] ? sp : "",
105 silc_asn1_tag_name(type), tag,
106 ber_class == SILC_BER_CLASS_UNIVERSAL ? "univ" :
107 ber_class == SILC_BER_CLASS_APPLICATION ? "appl" :
108 ber_class == SILC_BER_CLASS_CONTEXT ? "cont" : "priv",
109 (type != SILC_ASN1_TAG_SEQUENCE && type != SILC_ASN1_TAG_SET) ?
110 opts & SILC_ASN1_EXPLICIT ? "constr" :
111 type == SILC_ASN1_TAG_ANY &&
112 !(opts & SILC_ASN1_EXPLICIT) ? "constr" : "primit" : "constr",
113 indef ? opts & SILC_ASN1_EXPLICIT ? "defin" : "indef" : "defin",
114 opts & SILC_ASN1_IMPLICIT ? "implicit" :
115 opts & SILC_ASN1_EXPLICIT ? "explicit" : ""));
116 #endif /* SILC_DEBUG */
118 /* If tagging is explicit we add constructed type before the underlaying
119 types. The underlaying types are encoded recursively with this
121 if (opts & SILC_ASN1_EXPLICIT) {
122 memset(&buf, 0, sizeof(buf));
124 primitive = (type != SILC_ASN1_TAG_SEQUENCE &&
125 type != SILC_ASN1_TAG_SET);
126 opts &= ~SILC_ASN1_EXPLICIT;
128 silc_stack_push(stack2, &frame);
129 ret = silc_asn1_encoder(asn1, stack2, stack1, type, type,
130 SILC_BER_CLASS_UNIVERSAL, opts,
131 &buf, depth + 1, primitive);
132 silc_stack_pop(stack2);
135 SILC_LOG_DEBUG(("Error encoding explicit tag"));
139 /* Encode the explicit tag */
140 len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), FALSE);
141 dest = silc_buffer_srealloc_size(stack1, dest,
142 silc_buffer_truelen(dest) + len);
143 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
144 tag, buf.data, silc_buffer_len(&buf), FALSE);
154 /* Encode by the type */
157 case SILC_ASN1_TAG_ANY:
159 /* ANY is another ASN.1 node which is added to this tree */
160 SilcBuffer node = va_arg(asn1->ap, SilcBuffer);
164 /* Encode ASN.1 node into the tree. */
165 if (opts & SILC_ASN1_IMPLICIT || type != tag) {
166 /* We are tagging implicitly so we need to change the identifier
167 of the underlaying type. Only constructed type is allowed with
168 ANY when tagging implicitly. */
169 const unsigned char *d;
173 /* Get the underlaying data */
174 ret = silc_ber_decode(node, NULL, &enc, NULL, &d, &d_len,
177 SILC_LOG_DEBUG(("Error decoding underlaying node for ANY"));
180 assert(enc == SILC_BER_ENC_CONSTRUCTED);
182 /* Now encode with implicit tagging */
183 len = silc_ber_encoded_len(tag, d_len, FALSE);
184 dest = silc_buffer_srealloc_size(stack1, dest,
185 silc_buffer_truelen(dest) + len);
186 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED,
187 tag, d, d_len, FALSE);
191 /* Copy the data directly into the tree. */
192 len = silc_buffer_len(node);
193 dest = silc_buffer_srealloc_size(stack1, dest,
194 silc_buffer_truelen(dest) + len);
195 silc_buffer_put(dest, node->data, len);
200 case SILC_ASN1_TAG_SEQUENCE:
201 case SILC_ASN1_TAG_SET:
203 /* SEQUENCE/SET is a sequence of types. Sequences are opened and
204 encoded recursively by calling this same encoder. */
205 memset(&buf, 0, sizeof(buf));
207 /* Get type, tag and options for the first argument in recursion */
208 SILC_ASN1_ARGS(asn1, rtype, rtag, rclass, ropts);
210 silc_stack_push(stack2, &frame);
211 ret = silc_asn1_encoder(asn1, stack2, stack1, rtype, rtag, rclass,
212 ropts, &buf, depth + 1, FALSE);
213 silc_stack_pop(stack2);
215 SILC_LOG_DEBUG(("Error traversing a SEQUENCE/SET"));
219 /* Encode the sequence */
220 len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
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_CONSTRUCTED,
224 tag, buf.data, silc_buffer_len(&buf), indef);
230 case SILC_ASN1_TAG_INTEGER:
231 case SILC_ASN1_TAG_ENUM:
234 SilcMPInt *mpint = va_arg(asn1->ap, SilcMPInt *);
238 memset(&buf, 0, sizeof(buf));
239 if (silc_mp_cmp_ui(mpint, 0) < 0) {
240 /* XXX TODO, negative integer. Take 2s complement, then store
241 bytes in 1s complement */
244 len = (silc_mp_sizeinbase(mpint, 2) + 7) / 8;
245 len += len & 7 ? 1: 0;
246 silc_stack_push(stack2, &frame);
247 silc_buffer_srealloc_size(stack2, &buf,
248 silc_buffer_truelen(&buf) + len);
249 silc_mp_mp2bin_noalloc(mpint, buf.data, silc_buffer_len(&buf));
252 /* Encode the integer */
253 len = silc_ber_encoded_len(tag, len, indef);
254 dest = silc_buffer_srealloc_size(stack1, dest,
255 silc_buffer_truelen(dest) + len);
256 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
257 tag, buf.data, silc_buffer_len(&buf), FALSE);
258 silc_stack_pop(stack2);
264 case SILC_ASN1_TAG_OID:
266 /* Object identifier */
267 char *cp, *oidstr = va_arg(asn1->ap, char *);
268 SilcUInt32 words[24], oid, mask;
273 /* Get OID words from the string */
274 cp = strchr(oidstr, '.');
276 c = sscanf(oidstr, "%lu", (unsigned long *)&oid);
278 SILC_LOG_DEBUG(("Malformed OID string"));
281 if (c + 1 > sizeof(words) / sizeof(words[0]))
285 cp = strchr(oidstr, '.');
288 SILC_LOG_DEBUG(("Malfromed OID string"));
292 /* Get OID data length */
293 for (i = 2, len = 1; i < c; i++) {
295 for (oid = words[i]; oid; oid >>= 7)
303 memset(&buf, 0, sizeof(buf));
304 silc_stack_push(stack2, &frame);
305 silc_buffer_srealloc_size(stack2, &buf,
306 silc_buffer_truelen(&buf) + len);
307 buf.data[0] = words[0] * 40 + words[1];
308 for (i = 2, len = 1; i < c; i++) {
314 buf.data[len++] = (oid & 0x7f) | mask;
321 buf.data[c] = buf.data[mask];
322 buf.data[mask] = oid;
329 buf.data[len++] = 0x00;
332 len = silc_ber_encoded_len(tag, len, indef);
333 dest = silc_buffer_srealloc_size(stack1, dest,
334 silc_buffer_truelen(dest) + len);
335 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
336 tag, buf.data, silc_buffer_len(&buf), FALSE);
337 silc_stack_pop(stack2);
343 case SILC_ASN1_TAG_BOOLEAN:
345 /* Encodes boolean (TRUE/FALSE) value */
346 unsigned char val[1];
347 val[0] = (va_arg(asn1->ap, SilcUInt32) ? 0xff : 0x00);
349 assert(indef == FALSE);
350 len = silc_ber_encoded_len(tag, 1, FALSE);
351 dest = silc_buffer_srealloc_size(stack1, dest,
352 silc_buffer_truelen(dest) + len);
353 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
360 case SILC_ASN1_TAG_BIT_STRING:
362 /* Encode the data as is, with the bit padding. d_len is in bits. */
363 unsigned char *d = va_arg(asn1->ap, unsigned char *);
364 SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
365 unsigned char pad[1];
369 pad[0] = (8 - (d_len & 7)) & 7;
370 d_len = ((d_len + 7) / 8) + 1;
372 memset(&buf, 0, sizeof(buf));
373 silc_stack_push(stack2, &frame);
374 silc_buffer_srealloc_size(stack2, &buf,
375 silc_buffer_truelen(&buf) + d_len);
376 silc_buffer_put(&buf, pad, 1);
377 silc_buffer_pull(&buf, 1);
378 silc_buffer_put(&buf, d, d_len - 1);
379 silc_buffer_push(&buf, 1);
381 len = silc_ber_encoded_len(tag, silc_buffer_len(&buf), indef);
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,
385 tag, buf.data, silc_buffer_len(&buf), indef);
386 silc_stack_pop(stack2);
392 case SILC_ASN1_TAG_NULL:
394 /* Encode empty BER block */
395 assert(indef == FALSE);
396 len = silc_ber_encoded_len(tag, 0, FALSE);
397 dest = silc_buffer_srealloc_size(stack1, dest,
398 silc_buffer_truelen(dest) + len);
399 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
400 tag, NULL, 0, FALSE);
406 case SILC_ASN1_TAG_UTC_TIME:
408 /* Universal encoded time string */
409 SilcTime timeval = va_arg(asn1->ap, SilcTime);
414 if (!silc_time_universal_string(timeval, timestr, sizeof(timestr))) {
415 SILC_LOG_DEBUG(("Could not encode universal time string"));
419 len = silc_ber_encoded_len(tag, strlen(timestr), indef);
420 dest = silc_buffer_srealloc_size(stack1, dest,
421 silc_buffer_truelen(dest) + len);
422 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
423 tag, timestr, strlen(timestr), indef);
429 case SILC_ASN1_TAG_GENERALIZED_TIME:
431 /* Generalized encoded time string */
432 SilcTime timeval = va_arg(asn1->ap, SilcTime);
437 if (!silc_time_generalized_string(timeval, timestr, sizeof(timestr))) {
438 SILC_LOG_DEBUG(("Could not encode generalized time string"));
442 len = silc_ber_encoded_len(tag, strlen(timestr), indef);
443 dest = silc_buffer_srealloc_size(stack1, dest,
444 silc_buffer_truelen(dest) + len);
445 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
446 tag, timestr, strlen(timestr), indef);
452 case SILC_ASN1_TAG_UTF8_STRING:
455 unsigned char *d = va_arg(asn1->ap, unsigned char *);
456 SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32);
460 /* By default all strings that get here should already be UTF-8 */
461 if (!silc_utf8_valid(d, d_len)) {
462 SILC_LOG_DEBUG(("Malformed UTF-8 string"));
466 len = silc_ber_encoded_len(tag, d_len, indef);
467 dest = silc_buffer_srealloc_size(stack1, dest,
468 silc_buffer_truelen(dest) + len);
469 ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE,
470 tag, d, d_len, indef);
476 case SILC_ASN1_TAG_OCTET_STRING:
478 /* Octet string. We put it in as 8-bit ASCII */
479 SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
483 case SILC_ASN1_TAG_NUMERIC_STRING:
485 /* Numerical (digit) string */
486 SILC_ASN1_ENCODE_STRING(SILC_STRING_NUMERICAL);
490 case SILC_ASN1_TAG_PRINTABLE_STRING:
492 /* Printable string */
493 SILC_ASN1_ENCODE_STRING(SILC_STRING_PRINTABLE);
497 case SILC_ASN1_TAG_TELETEX_STRING:
499 /* Teletex (T61) string */
500 SILC_ASN1_ENCODE_STRING(SILC_STRING_TELETEX);
504 case SILC_ASN1_TAG_IA5_STRING:
506 /* US ASCII string */
507 SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
511 case SILC_ASN1_TAG_VISIBLE_STRING:
514 SILC_ASN1_ENCODE_STRING(SILC_STRING_VISIBLE);
518 case SILC_ASN1_TAG_UNIVERSAL_STRING:
520 /* Universal (UCS-4) string */
521 SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
525 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
526 case SILC_ASN1_TAG_GENERAL_STRING:
528 /* Handle now unrestricted and general as 8-bit ascii, which
529 probably isn't correct. */
530 SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII);
534 case SILC_ASN1_TAG_BMP_STRING:
536 /* BMP (UCS-2) string */
537 SILC_ASN1_ENCODE_STRING(SILC_STRING_UNIVERSAL);
541 case SILC_ASN1_TAG_ODE:
542 case SILC_ASN1_TAG_ETI:
543 case SILC_ASN1_TAG_REAL:
544 case SILC_ASN1_TAG_EMBEDDED:
545 case SILC_ASN1_TAG_ROI:
546 case SILC_ASN1_TAG_VIDEOTEX_STRING:
547 case SILC_ASN1_TAG_GRAPHIC_STRING:
549 SILC_NOT_IMPLEMENTED("Unsupported ASN.1 tag");
556 SILC_LOG_DEBUG(("Invalid ASN.1 tag `%d'. Cannot encode ASN.1.", type));
564 silc_buffer_pull(dest, len);
570 /* Get next type, tag and options */
571 SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
572 if (type == SILC_ASN1_END) {
579 SILC_LOG_DEBUG(("Error encoding type %d (depth %d)", type, depth));
583 len = dest->data - ptr;
585 len = dest->data - dest->head;
586 silc_buffer_push(dest, len);
591 bool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...)
593 SilcAsn1Tag type, tag;
594 SilcAsn1Options opts;
595 SilcBerClass ber_class;
596 SilcStackFrame frame1, frame2;
597 SilcStack stack1 = NULL;
603 va_start(asn1->ap, dest);
605 /* Get the first arguments and call the encoder. */
606 SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
613 /* Handle internal options for encoder. */
614 if (type == SILC_ASN1_TAG_OPTS) {
615 SilcUInt32 o = va_arg(asn1->ap, SilcUInt32);
617 if (o & SILC_ASN1_ALLOC) {
618 /* User wants to alloate everything. Set the stack to NULL so
619 that stack aware calls revert to normal allocation routines. */
620 stack1 = asn1->stack1;
624 if (o & SILC_ASN1_ACCUMUL) {
625 /* If accumul flag is not set yet, then push the stack. */
626 if (!asn1->accumul) {
627 silc_stack_push(asn1->stack1, NULL);
632 /* Take again the arguments */
633 SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
635 /* No flags set, all flags will be reset. */
637 /* If accumul flag is set now pop the stack so that all accumulated
638 memory becomes free again. */
640 silc_stack_pop(asn1->stack1);
645 /* Push the stack for normal allocation from stack. */
647 silc_stack_push(asn1->stack1, &frame1);
650 silc_stack_push(asn1->stack2, &frame2);
651 ret = silc_asn1_encoder(asn1, asn1->stack1, asn1->stack2,
652 type, tag, ber_class, opts, dest, 0, FALSE);
653 silc_stack_pop(asn1->stack2);
655 /* Pop the stack to free normal allocations from stack. */
657 silc_stack_pop(asn1->stack1);
659 /* If SILC_ASN1_ALLOC flag was set, restore the stack. */
660 if (stack1 && !asn1->stack1)
661 asn1->stack1 = stack1;