Commit much improved ASN.1 compiler from joda-choice-branch.

Highlighs for the compiler is support for CHOICE and in general better
support for tags. This compiler support most of what is needed for
PK-INIT, LDAP, X.509, PKCS-12 and many other protocols.


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@15617 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Love Hörnquist Åstrand
2005-07-12 06:27:42 +00:00
parent bed557d8e7
commit b838707d0e
48 changed files with 7670 additions and 2442 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997 - 2002 Kungliga Tekniska H<>gskolan
* Copyright (c) 1997 - 2005 Kungliga Tekniska H<>gskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* All rights reserved.
*
@@ -37,55 +37,20 @@
#include <sys/stat.h>
#include <getarg.h>
#include <err.h>
#include <der.h>
RCSID("$Id$");
const char *class_names[] = {
"UNIV", /* 0 */
"APPL", /* 1 */
"CONTEXT", /* 2 */
"PRIVATE" /* 3 */
};
static int indent_flag = 1;
const char *type_names[] = {
"PRIM", /* 0 */
"CONS" /* 1 */
};
static unsigned long indefinite_form_loop;
static unsigned long indefinite_form_loop_max = 10000;
const char *tag_names[] = {
NULL, /* 0 */
NULL, /* 1 */
"Integer", /* 2 */
"BitString", /* 3 */
"OctetString", /* 4 */
"Null", /* 5 */
"ObjectID", /* 6 */
NULL, /* 7 */
NULL, /* 8 */
NULL, /* 9 */
"Enumerated", /* 10 */
NULL, /* 11 */
NULL, /* 12 */
NULL, /* 13 */
NULL, /* 14 */
NULL, /* 15 */
"Sequence", /* 16 */
"Set", /* 17 */
NULL, /* 18 */
"PrintableString", /* 19 */
NULL, /* 20 */
NULL, /* 21 */
"IA5String", /* 22 */
"UTCTime", /* 23 */
"GeneralizedTime", /* 24 */
NULL, /* 25 */
"VisibleString", /* 26 */
"GeneralString" /* 27 */
};
static int
static size_t
loop (unsigned char *buf, size_t len, int indent)
{
unsigned char *start_buf = buf;
while (len > 0) {
int ret;
Der_class class;
@@ -93,7 +58,9 @@ loop (unsigned char *buf, size_t len, int indent)
int tag;
size_t sz;
size_t length;
int i;
size_t loop_length = 0;
int end_tag = 0;
const char *tagname;
ret = der_get_tag (buf, len, &class, &type, &tag, &sz);
if (ret)
@@ -103,62 +70,99 @@ loop (unsigned char *buf, size_t len, int indent)
(unsigned)sz, (unsigned)len);
buf += sz;
len -= sz;
for (i = 0; i < indent; ++i)
printf (" ");
printf ("%s %s ", class_names[class], type_names[type]);
if (tag_names[tag])
printf ("%s = ", tag_names[tag]);
if (indent_flag) {
int i;
for (i = 0; i < indent; ++i)
printf (" ");
}
printf ("%s %s ", der_get_class_name(class), der_get_type_name(type));
tagname = der_get_tag_name(tag);
if (class == ASN1_C_UNIV && tagname != NULL)
printf ("%s = ", tagname);
else
printf ("tag %d = ", tag);
ret = der_get_length (buf, len, &length, &sz);
if (ret)
errx (1, "der_get_tag: %s", error_message (ret));
if (sz > len)
errx (1, "unreasonable tag length (%u) > %u",
(unsigned)sz, (unsigned)len);
buf += sz;
len -= sz;
if (class == ASN1_C_CONTEXT) {
printf ("[%d]\n", tag);
loop (buf, length, indent);
if (length == ASN1_INDEFINITE) {
if ((class == ASN1_C_UNIV && type == PRIM && tag == UT_OctetString) ||
(class == ASN1_C_CONTEXT && type == CONS) ||
(class == ASN1_C_UNIV && type == CONS && tag == UT_Sequence) ||
(class == ASN1_C_UNIV && type == CONS && tag == UT_Set)) {
printf("*INDEFINITE FORM*");
} else {
fflush(stdout);
errx(1, "indef form used on unsupported object");
}
end_tag = 1;
if (indefinite_form_loop > indefinite_form_loop_max)
errx(1, "indefinite form used recursively more then %lu "
"times, aborting", indefinite_form_loop_max);
indefinite_form_loop++;
length = len;
} else if (length > len) {
printf("\n");
fflush(stdout);
errx (1, "unreasonable inner length (%u) > %u",
(unsigned)length, (unsigned)len);
}
if (class == ASN1_C_CONTEXT || class == ASN1_C_APPL) {
printf ("%d bytes [%d]\n", length, tag);
if (type == CONS)
loop_length = loop (buf, length, indent + 2);
} else if (class == ASN1_C_UNIV) {
switch (tag) {
case UT_EndOfContent:
printf (" INDEFINITE length was %lu\n",
(unsigned long)(buf - start_buf));
break;
case UT_Set :
case UT_Sequence :
printf ("{\n");
loop (buf, length, indent + 2);
for (i = 0; i < indent; ++i)
printf (" ");
printf ("}\n");
printf ("%lu bytes {\n", (unsigned long)length);
loop_length = loop (buf, length, indent + 2);
if (indent_flag) {
int i;
for (i = 0; i < indent; ++i)
printf (" ");
printf ("}\n");
} else
printf ("} indent = %d\n", indent / 2);
break;
case UT_Integer : {
int val;
ret = der_get_int (buf, length, &val, NULL);
if (ret)
errx (1, "der_get_int: %s", error_message (ret));
printf ("integer %d\n", val);
if (length <= sizeof(val)) {
ret = der_get_integer (buf, length, &val, NULL);
if (ret)
errx (1, "der_get_integer: %s", error_message (ret));
printf ("integer %d\n", val);
} else {
printf ("BIG NUM integer: length %d\n", length);
}
break;
}
case UT_OctetString : {
heim_octet_string str;
int i;
unsigned char *uc;
ret = der_get_octet_string (buf, length, &str, NULL);
if (ret)
errx (1, "der_get_octet_string: %s", error_message (ret));
printf ("(length %lu)%s", (unsigned long)str.length,
str.length > 0 ? ", " : "");
printf ("(length %lu), ", (unsigned long)length);
uc = (unsigned char *)str.data;
length = str.length;
if (length > 16)
length = 16;
for (i = 0; i < length; ++i)
for (i = 0; i < min(16,length); ++i)
printf ("%02x", uc[i]);
printf ("\n");
free (str.data);
break;
}
case UT_GeneralizedTime :
case UT_IA5String:
case UT_UTF8String :
case UT_GeneralString : {
heim_general_string str;
@@ -172,6 +176,7 @@ loop (unsigned char *buf, size_t len, int indent)
}
case UT_OID: {
heim_oid o;
int i;
ret = der_get_oid(buf, length, &o, NULL);
if (ret)
@@ -185,9 +190,9 @@ loop (unsigned char *buf, size_t len, int indent)
break;
}
case UT_Enumerated: {
unsigned num;
int num;
ret = der_get_int (buf, length, &num, NULL);
ret = der_get_integer (buf, length, &num, NULL);
if (ret)
errx (1, "der_get_enum: %s", error_message (ret));
@@ -199,6 +204,17 @@ loop (unsigned char *buf, size_t len, int indent)
break;
}
}
if (end_tag) {
if (loop_length == 0)
errx(1, "zero length INDEFINITE data ? indent = %d\n",
indent / 2);
if (loop_length < length)
length = loop_length;
if (indefinite_form_loop == 0)
errx(1, "internal error in indefinite form loop detection");
indefinite_form_loop--;
} else if (loop_length)
errx(1, "internal error for INDEFINITE form");
buf += length;
len -= length;
}
@@ -219,21 +235,20 @@ doit (const char *filename)
if (fstat (fd, &sb) < 0)
err (1, "stat %s", filename);
len = sb.st_size;
buf = malloc (len);
if (buf == NULL)
err (1, "malloc %u", (unsigned)len);
buf = emalloc (len);
if (read (fd, buf, len) != len)
errx (1, "read failed");
close (fd);
ret = loop (buf, len, 0);
free (buf);
return ret;
return 0;
}
static int version_flag;
static int help_flag;
struct getargs args[] = {
{ "indent", 0, arg_negative_flag, &indent_flag },
{ "version", 0, arg_flag, &version_flag },
{ "help", 0, arg_flag, &help_flag }
};
@@ -249,11 +264,11 @@ usage(int code)
int
main(int argc, char **argv)
{
int optidx = 0;
int optind = 0;
setprogname (argv[0]);
initialize_asn1_error_table ();
if(getarg(args, num_args, argc, argv, &optidx))
if(getarg(args, num_args, argc, argv, &optind))
usage(1);
if(help_flag)
usage(0);
@@ -261,8 +276,8 @@ main(int argc, char **argv)
print_version(NULL);
exit(0);
}
argv += optidx;
argc -= optidx;
argv += optind;
argc -= optind;
if (argc != 1)
usage (1);
return doit (argv[0]);