asn1: X.681/682/683 magic handling of open types
Status: - And it works! - We have an extensive test based on decoding a rich EK certficate. This test exercises all of: - decoding - encoding with and without decoded open types - copying of decoded values with decoded open types - freeing of decoded values with decoded open types Valgrind finds no memory errors. - Added a manual page for the compiler. - rfc2459.asn1 now has all three primary PKIX types that we care about defined as in RFC5912, with IOS constraints and parameterization: - `Extension` (embeds open type in an `OCTET STRING`) - `OtherName` (embeds open type in an `ANY`-like type) - `SingleAttribute` (embeds open type in an `ANY`-like type) - `AttributeSet` (embeds open type in a `SET OF ANY`-like type) All of these use OIDs as the open type type ID field, but integer open type type ID fields are also supported (and needed, for Kerberos). That will cover every typed hole pattern in all our ASN.1 modules. With this we'll be able to automatically and recursively decode through all subject DN attributes even when the subject DN is a directoryName SAN, and subjectDirectoryAttributes, and all extensions, and all SANs, and all authorization-data elements, and PA-data, and... We're not really using `SingleAttribute` and `AttributeSet` yet because various changes are needed in `lib/hx509` for that. - `asn1_compile` builds and recognizes the subset of X.681/682/683 that we need for, and now use in, rfc2459.asn1. It builds the necessary AST, generates the correct C types, and generates templating for object sets and open types! - See READMEs for details. - Codegen backend not tested; I won't make it implement automatic open type handling, but it should at least not crash by substituting `heim_any` for open types not embedded in `OCTET STRING`. - We're _really_ starting to have problems with the ITU-T ASN.1 grammar and our version of it... Type names have to start with upper-case, value names with lower-case, but it's not enough to disambiguate. The fact the we've allowed value and type names to violate their respective start-with case rules is causing us trouble now that we're adding grammar from X.681/682/683, and we're going to have to undo that. In preparation for that I'm capitalizing the `heim_any` and `heim_any_set` types, and doing some additional cleanup, which requires changes to other parts of Heimdal (all in this same commit for now). Problems we have because of this: - We cannot IMPORT values into modules because we have no idea if a symbol being imported refers to a value or a type because the only clue we would have is the symbol's name, so we assume IMPORTed symbols are for types. This means we can't import OIDs, for example, which is super annoying. One thing we might be able to do here is mark imported symbols as being of an undetermined-but-not-undefined type, then coerce the symbol's type the first time it's used in a context where its type is inferred as type, value, object, object set, or class. (Though since we don't generate C symbols for objects or classes, we won't be able to import them, especially since we need to know them at compile time and cannot defer their handling to link- or run-time.) - The `NULL` type name, and the `NULL` value name now cause two reduce/reduce conflicts via the `FieldSetting` production. - Various shift/reduce conflicts involving `NULL` values in non-top-level contexts (in constraints, for example). - Currently I have a bug where to disambiguate the grammar I have a CLASS_IDENTIFIER token that is all caps, while TYPE_IDENTIFIER must start with a capital but not be all caps, but this breaks Kerberos since all its types are all capitalized -- oof! To fix this I made it so class names have to be all caps and start with an underscore (ick). TBD: - Check all the XXX comments and address them - Apply this treatment to Kerberos! Automatic handling of authz-data sounds useful :) - Apply this treatment to PKCS#10 (CSRs) and other ASN.1 modules too. - Replace various bits of code in `lib/hx509/` with uses of this feature. - Add JER. - Enhance `hxtool` and `asn1_print`. Getting there!
This commit is contained in:
@@ -295,6 +295,8 @@ static int tlist_cmp(const struct tlist *, const struct tlist *);
|
||||
|
||||
static void add_line_pointer(struct templatehead *, const char *, const char *, const char *, ...)
|
||||
__attribute__ ((__format__ (__printf__, 4, 5)));
|
||||
static void add_line_pointer_reference(struct templatehead *, const char *, const char *, const char *, ...)
|
||||
__attribute__ ((__format__ (__printf__, 4, 5)));
|
||||
|
||||
|
||||
static struct tlisthead tlistmaster = HEIM_TAILQ_HEAD_INITIALIZER(tlistmaster);
|
||||
@@ -426,7 +428,7 @@ tlist_find_dup(const struct tlist *tl)
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* Add an entry to a template.
|
||||
*/
|
||||
|
||||
static struct template *
|
||||
@@ -442,6 +444,10 @@ add_line(struct templatehead *t, const char *fmt, ...)
|
||||
return q;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add an entry to a template, with the pointer field bein a symbol name of a
|
||||
* template (i.e., an array, which decays to a pointer as usual in C).
|
||||
*/
|
||||
static void
|
||||
add_line_pointer(struct templatehead *t,
|
||||
const char *ptr,
|
||||
@@ -464,6 +470,28 @@ add_line_pointer(struct templatehead *t,
|
||||
q->ptr = strdup(ptr);
|
||||
}
|
||||
|
||||
static void
|
||||
add_line_pointer_reference(struct templatehead *t,
|
||||
const char *ptr,
|
||||
const char *offset,
|
||||
const char *ttfmt,
|
||||
...)
|
||||
{
|
||||
struct template *q;
|
||||
va_list ap;
|
||||
char *tt = NULL;
|
||||
|
||||
va_start(ap, ttfmt);
|
||||
if (vasprintf(&tt, ttfmt, ap) < 0 || tt == NULL)
|
||||
errx(1, "malloc");
|
||||
va_end(ap);
|
||||
|
||||
q = add_line(t, "{ %s, %s, (const void *)&asn1_%s }", tt, offset, ptr);
|
||||
q->tt = tt;
|
||||
q->offset = strdup(offset);
|
||||
q->ptr = strdup(ptr);
|
||||
}
|
||||
|
||||
static int
|
||||
use_extern(const Symbol *s)
|
||||
{
|
||||
@@ -598,6 +626,229 @@ defval(struct templatehead *temp, Member *m)
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
objid_cmp(struct objid *oida, struct objid *oidb)
|
||||
{
|
||||
struct objid *p;
|
||||
size_t ai, bi, alen, blen;
|
||||
int avals[20];
|
||||
int bvals[20];
|
||||
int c;
|
||||
|
||||
/*
|
||||
* Our OID values are backwards here. Comparing them is hard.
|
||||
*/
|
||||
|
||||
for (p = oida, alen = 0;
|
||||
p && alen < sizeof(avals)/sizeof(avals[0]);
|
||||
p = p->next)
|
||||
avals[alen++] = p->value;
|
||||
for (p = oidb, blen = 0;
|
||||
p && blen < sizeof(bvals)/sizeof(bvals[0]);
|
||||
p = p->next)
|
||||
bvals[blen++] = p->value;
|
||||
if (alen >= sizeof(avals)/sizeof(avals[0]) ||
|
||||
blen >= sizeof(bvals)/sizeof(bvals[0]))
|
||||
err(1, "OIDs with more components than %llu not supported",
|
||||
(unsigned long long)sizeof(avals)/sizeof(avals[0]));
|
||||
|
||||
for (ai = 0, bi = 0; ai < alen && bi < blen;)
|
||||
if ((c = avals[(alen-1)-(ai++)] - bvals[(blen-1)-(bi++)]))
|
||||
return c;
|
||||
|
||||
if (ai == alen && bi == blen)
|
||||
return 0;
|
||||
if (ai == alen)
|
||||
return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int
|
||||
object_cmp(const void *va, const void *vb)
|
||||
{
|
||||
const IOSObject *oa = *(const IOSObject * const *)va;
|
||||
const IOSObject *ob = *(const IOSObject * const *)vb;
|
||||
|
||||
switch (oa->typeidf->value->type) {
|
||||
case booleanvalue:
|
||||
return oa->typeidf->value->u.booleanvalue -
|
||||
ob->typeidf->value->u.booleanvalue;
|
||||
case nullvalue:
|
||||
return 0;
|
||||
case integervalue:
|
||||
return oa->typeidf->value->u.integervalue -
|
||||
ob->typeidf->value->u.integervalue;
|
||||
case stringvalue:
|
||||
return strcmp(oa->typeidf->value->u.stringvalue,
|
||||
ob->typeidf->value->u.stringvalue);
|
||||
case objectidentifiervalue: {
|
||||
return objid_cmp(oa->typeidf->value->u.objectidentifiervalue,
|
||||
ob->typeidf->value->u.objectidentifiervalue);
|
||||
}
|
||||
default:
|
||||
abort();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
sort_object_set(IOSObjectSet *os, /* Object set to sort fields of */
|
||||
Field *typeidfield, /* Field to sort by */
|
||||
IOSObject ***objectsp, /* Output: array of objects */
|
||||
size_t *nobjsp) /* Output: count of objects */
|
||||
{
|
||||
IOSObject **objects;
|
||||
IOSObject *o;
|
||||
size_t i, nobjs = 0;
|
||||
|
||||
/* FIXME: This would be a good place to check field UNIQUE constraints */
|
||||
|
||||
HEIM_TAILQ_FOREACH(o, os->objects, objects) {
|
||||
ObjectField *typeidobjf = NULL;
|
||||
ObjectField *of;
|
||||
|
||||
HEIM_TAILQ_FOREACH(of, o->objfields, objfields) {
|
||||
if (strcmp(of->name, typeidfield->name) == 0)
|
||||
typeidobjf = of;
|
||||
}
|
||||
if (!typeidobjf) {
|
||||
warnx("Ignoring incomplete object specification of %s "
|
||||
"(missing type ID field)",
|
||||
o->symbol ? o->symbol->name : "<unknown>");
|
||||
continue;
|
||||
}
|
||||
o->typeidf = typeidobjf;
|
||||
nobjs++;
|
||||
}
|
||||
*nobjsp = nobjs;
|
||||
|
||||
if ((objects = calloc(nobjs, sizeof(*objects))) == NULL)
|
||||
err(1, "Out of memory");
|
||||
*objectsp = objects;
|
||||
|
||||
i = 0;
|
||||
HEIM_TAILQ_FOREACH(o, os->objects, objects) {
|
||||
ObjectField *typeidobjf = NULL;
|
||||
ObjectField *of;
|
||||
|
||||
HEIM_TAILQ_FOREACH(of, o->objfields, objfields) {
|
||||
if (strcmp(of->name, typeidfield->name) == 0)
|
||||
typeidobjf = of;
|
||||
}
|
||||
if (typeidobjf)
|
||||
objects[i++] = o;
|
||||
}
|
||||
qsort(objects, nobjs, sizeof(*objects), object_cmp);
|
||||
}
|
||||
|
||||
static void
|
||||
template_object_set(IOSObjectSet *os, Field *typeidfield, Field *opentypefield)
|
||||
{
|
||||
IOSObject **objects;
|
||||
IOSObject *o;
|
||||
struct tlist *tl;
|
||||
size_t nobjs, i;
|
||||
|
||||
if (os->symbol->emitted_template)
|
||||
return;
|
||||
|
||||
sort_object_set(os, typeidfield, &objects, &nobjs);
|
||||
|
||||
tl = tlist_new(os->symbol->name);
|
||||
for (i = 0; i < nobjs; i++) {
|
||||
ObjectField *typeidobjf = NULL, *opentypeobjf = NULL;
|
||||
ObjectField *of;
|
||||
char *s = NULL;
|
||||
|
||||
o = objects[i];
|
||||
|
||||
HEIM_TAILQ_FOREACH(of, o->objfields, objfields) {
|
||||
if (strcmp(of->name, typeidfield->name) == 0)
|
||||
typeidobjf = of;
|
||||
else if (strcmp(of->name, opentypefield->name) == 0)
|
||||
opentypeobjf = of;
|
||||
}
|
||||
if (!typeidobjf)
|
||||
continue; /* We've warned about this one already when sorting */
|
||||
if (!opentypeobjf) {
|
||||
warnx("Ignoring incomplete object specification of %s "
|
||||
"(missing open type field)",
|
||||
o->symbol ? o->symbol->name : "<unknown>");
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* Some of this logic could stand to move into sanity checks of object
|
||||
* definitions in asn1parse.y.
|
||||
*/
|
||||
switch (typeidobjf->value->type) {
|
||||
case integervalue:
|
||||
add_line(&tl->template,
|
||||
"{ A1_OP_OPENTYPE_ID | A1_OTI_IS_INTEGER, 0, (void *)%lld }",
|
||||
(long long)typeidobjf->value->u.integervalue);
|
||||
break;
|
||||
case objectidentifiervalue:
|
||||
if (asprintf(&s, "oid_%s",
|
||||
typeidobjf->value->s->gen_name) == -1 || !s)
|
||||
err(1, "Out of memory");
|
||||
add_line_pointer_reference(&tl->template, s, "0", "A1_OP_OPENTYPE_ID");
|
||||
free(s);
|
||||
s = NULL;
|
||||
break;
|
||||
default:
|
||||
errx(1, "Only integer and OID types supported "
|
||||
"for open type type-ID fields");
|
||||
}
|
||||
|
||||
if (asprintf(&s, "sizeof(%s)",
|
||||
opentypeobjf->type->symbol->gen_name) == -1 || !s)
|
||||
err(1, "Out of memory");
|
||||
add_line_pointer_reference(&tl->template,
|
||||
opentypeobjf->type->symbol->gen_name, s,
|
||||
"A1_OP_OPENTYPE");
|
||||
free(s);
|
||||
}
|
||||
free(objects);
|
||||
|
||||
tlist_header(tl, "{ 0, 0, ((void *)%lu) }", nobjs);
|
||||
tlist_print(tl);
|
||||
tlist_add(tl);
|
||||
os->symbol->emitted_template = 1;
|
||||
}
|
||||
|
||||
static void
|
||||
template_open_type(struct templatehead *temp,
|
||||
const char *basetype,
|
||||
const Type *t,
|
||||
size_t typeididx,
|
||||
size_t opentypeidx,
|
||||
Field *typeidfield,
|
||||
Field *opentypefield,
|
||||
Member *m,
|
||||
int is_array_of_open_type)
|
||||
{
|
||||
char *s = NULL;
|
||||
|
||||
if (typeididx >= 1<<10 || opentypeidx >= 1<<10)
|
||||
errx(1, "SET/SEQUENCE with too many members (%s)", basetype);
|
||||
|
||||
if (asprintf(&s, "offsetof(%s, _ioschoice_%s)",
|
||||
basetype, m->gen_name) == -1 || !s)
|
||||
err(1, "Out of memory");
|
||||
|
||||
template_object_set(t->actual_parameter, typeidfield, opentypefield);
|
||||
add_line_pointer(temp, t->actual_parameter->symbol->gen_name, s,
|
||||
/*
|
||||
* We always sort object sets for now as we can't import
|
||||
* values yet, so they must all be known.
|
||||
*/
|
||||
"A1_OP_OPENTYPE_OBJSET | A1_OS_IS_SORTED |%s | (%llu << 10) | %llu",
|
||||
is_array_of_open_type ? "A1_OS_OT_IS_ARRAY" : "0",
|
||||
(unsigned long long)opentypeidx,
|
||||
(unsigned long long)typeididx);
|
||||
free(s);
|
||||
}
|
||||
|
||||
static void
|
||||
template_members(struct templatehead *temp,
|
||||
const char *basetype,
|
||||
@@ -742,7 +993,18 @@ template_members(struct templatehead *temp,
|
||||
break;
|
||||
}
|
||||
case TSet: {
|
||||
Member *opentypemember = NULL;
|
||||
Member *typeidmember = NULL;
|
||||
Field *opentypefield = NULL;
|
||||
Field *typeidfield = NULL;
|
||||
Member *m;
|
||||
size_t i = 0, typeididx = 0, opentypeidx = 0;
|
||||
int is_array_of_open_type = 0;
|
||||
|
||||
if (isstruct && t->actual_parameter)
|
||||
get_open_type_defn_fields(t, &typeidmember, &opentypemember,
|
||||
&typeidfield, &opentypefield,
|
||||
&is_array_of_open_type);
|
||||
|
||||
fprintf(get_code_file(), "/* tset: members isstruct: %d */\n", isstruct);
|
||||
|
||||
@@ -752,6 +1014,9 @@ template_members(struct templatehead *temp,
|
||||
if (m->ellipsis)
|
||||
continue;
|
||||
|
||||
if (typeidmember == m) typeididx = i;
|
||||
if (opentypemember == m) opentypeidx = i;
|
||||
|
||||
if (name) {
|
||||
if (asprintf(&newbasename, "%s_%s", basetype, name) < 0)
|
||||
errx(1, "malloc");
|
||||
@@ -766,12 +1031,28 @@ template_members(struct templatehead *temp,
|
||||
template_members(temp, newbasename, m->gen_name, m->type, m->optional, m->defval ? 1 : 0, 0, isstruct, 1);
|
||||
|
||||
free(newbasename);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (isstruct && t->actual_parameter)
|
||||
template_open_type(temp, basetype, t, typeididx, opentypeidx,
|
||||
typeidfield, opentypefield, opentypemember,
|
||||
is_array_of_open_type);
|
||||
break;
|
||||
}
|
||||
case TSequence: {
|
||||
Member *opentypemember = NULL;
|
||||
Member *typeidmember = NULL;
|
||||
Field *opentypefield = NULL;
|
||||
Field *typeidfield = NULL;
|
||||
Member *m;
|
||||
size_t i = 0, typeididx = 0, opentypeidx = 0;
|
||||
int is_array_of_open_type = 0;
|
||||
|
||||
if (isstruct && t->actual_parameter)
|
||||
get_open_type_defn_fields(t, &typeidmember, &opentypemember,
|
||||
&typeidfield, &opentypefield,
|
||||
&is_array_of_open_type);
|
||||
|
||||
fprintf(get_code_file(), "/* tsequence: members isstruct: %d */\n", isstruct);
|
||||
|
||||
@@ -781,6 +1062,9 @@ template_members(struct templatehead *temp,
|
||||
if (m->ellipsis)
|
||||
continue;
|
||||
|
||||
if (typeidmember == m) typeididx = i;
|
||||
if (opentypemember == m) opentypeidx = i;
|
||||
|
||||
if (name) {
|
||||
if (asprintf(&newbasename, "%s_%s", basetype, name) < 0)
|
||||
errx(1, "malloc");
|
||||
@@ -795,8 +1079,13 @@ template_members(struct templatehead *temp,
|
||||
template_members(temp, newbasename, m->gen_name, m->type, m->optional, m->defval ? 1 : 0, 0, isstruct, 1);
|
||||
|
||||
free(newbasename);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (isstruct && t->actual_parameter)
|
||||
template_open_type(temp, basetype, t, typeididx, opentypeidx,
|
||||
typeidfield, opentypefield, opentypemember,
|
||||
is_array_of_open_type);
|
||||
break;
|
||||
}
|
||||
case TTag: {
|
||||
@@ -1010,6 +1299,15 @@ generate_template_type_forward(const char *name)
|
||||
fprintf(get_code_file(), "extern const struct asn1_template asn1_%s[];\n", name);
|
||||
}
|
||||
|
||||
void
|
||||
generate_template_objectset_forwards(const Symbol *s)
|
||||
{
|
||||
if (!template_flag)
|
||||
return;
|
||||
fprintf(get_code_file(), "extern const struct asn1_template asn1_%s[];\n",
|
||||
s->gen_name);
|
||||
}
|
||||
|
||||
static void
|
||||
generate_template_type(const char *varname,
|
||||
const char **dupname,
|
||||
|
Reference in New Issue
Block a user