
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!
337 lines
8.3 KiB
C
337 lines
8.3 KiB
C
/*
|
|
* Copyright (c) 2019 Kungliga Tekniska Högskolan
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
#include "der_locl.h"
|
|
#include <hex.h>
|
|
|
|
#include "cms_asn1.h"
|
|
#include "crmf_asn1.h"
|
|
#include "digest_asn1.h"
|
|
#include "krb5_asn1.h"
|
|
#include "kx509_asn1.h"
|
|
#include "ocsp_asn1.h"
|
|
#include "pkcs10_asn1.h"
|
|
#include "pkcs12_asn1.h"
|
|
#include "pkcs8_asn1.h"
|
|
#include "pkcs9_asn1.h"
|
|
#include "pkinit_asn1.h"
|
|
#include "rfc2459_asn1.h"
|
|
#include "rfc4108_asn1.h"
|
|
|
|
|
|
struct sym_oid {
|
|
const char *sym;
|
|
const heim_oid *oid;
|
|
};
|
|
|
|
#ifndef WIN32
|
|
#define DEFINE_OID_WITH_NAME(sym) \
|
|
{ #sym, &asn1_oid_ ## sym },
|
|
|
|
static const struct sym_oid sym_oids[] = {
|
|
#include "cms_asn1_oids.x"
|
|
#include "crmf_asn1_oids.x"
|
|
#include "digest_asn1_oids.x"
|
|
#include "krb5_asn1_oids.x"
|
|
#include "kx509_asn1_oids.x"
|
|
#include "ocsp_asn1_oids.x"
|
|
#include "pkcs10_asn1_oids.x"
|
|
#include "pkcs12_asn1_oids.x"
|
|
#include "pkcs8_asn1_oids.x"
|
|
#include "pkcs9_asn1_oids.x"
|
|
#include "pkinit_asn1_oids.x"
|
|
#include "rfc2459_asn1_oids.x"
|
|
#include "rfc4108_asn1_oids.x"
|
|
};
|
|
|
|
static size_t num_sym_oids = sizeof(sym_oids) / sizeof(sym_oids[0]);
|
|
|
|
#undef DEFINE_OID_WITH_NAME
|
|
|
|
#define init_sym_oids()
|
|
|
|
#else
|
|
|
|
/*
|
|
* We can't use C99 non-literal initializers for static objects in the Windows
|
|
* build...
|
|
*/
|
|
|
|
static struct sym_oid *sym_oids;
|
|
static size_t num_sym_oids;
|
|
|
|
#define DEFINE_OID_WITH_NAME(sym) (c++);
|
|
static size_t
|
|
count_sym_oids(void)
|
|
{
|
|
size_t c = 0;
|
|
#include "cms_asn1_oids.x"
|
|
#include "crmf_asn1_oids.x"
|
|
#include "digest_asn1_oids.x"
|
|
#include "krb5_asn1_oids.x"
|
|
#include "kx509_asn1_oids.x"
|
|
#include "ocsp_asn1_oids.x"
|
|
#include "pkcs10_asn1_oids.x"
|
|
#include "pkcs12_asn1_oids.x"
|
|
#include "pkcs8_asn1_oids.x"
|
|
#include "pkcs9_asn1_oids.x"
|
|
#include "pkinit_asn1_oids.x"
|
|
#include "rfc2459_asn1_oids.x"
|
|
return c;
|
|
}
|
|
#undef DEFINE_OID_WITH_NAME
|
|
|
|
#define DEFINE_OID_WITH_NAME(s) \
|
|
tmp[i].sym = #s; \
|
|
tmp[i++].oid = &asn1_oid_ ## s;
|
|
|
|
static void
|
|
init_sym_oids(void)
|
|
{
|
|
static struct sym_oid *tmp;
|
|
size_t i = 0;
|
|
size_t c;
|
|
|
|
if (!sym_oids &&
|
|
(c = count_sym_oids()) &&
|
|
(tmp = calloc(c, sizeof(tmp[0])))) {
|
|
#include "cms_asn1_oids.x"
|
|
#include "crmf_asn1_oids.x"
|
|
#include "digest_asn1_oids.x"
|
|
#include "krb5_asn1_oids.x"
|
|
#include "kx509_asn1_oids.x"
|
|
#include "ocsp_asn1_oids.x"
|
|
#include "pkcs10_asn1_oids.x"
|
|
#include "pkcs12_asn1_oids.x"
|
|
#include "pkcs8_asn1_oids.x"
|
|
#include "pkcs9_asn1_oids.x"
|
|
#include "pkinit_asn1_oids.x"
|
|
#include "rfc2459_asn1_oids.x"
|
|
num_sym_oids = c;
|
|
sym_oids = tmp;
|
|
}
|
|
}
|
|
#undef DEFINE_OID_WITH_NAME
|
|
|
|
#endif
|
|
|
|
static struct sym_oid *sym_oids_sorted_by_name;
|
|
static struct sym_oid *sym_oids_sorted_by_oid;
|
|
|
|
static int
|
|
sym_cmp_name(const void *va, const void *vb)
|
|
{
|
|
const struct sym_oid *a = va;
|
|
const struct sym_oid *b = vb;
|
|
|
|
return (strcmp(a->sym, b->sym));
|
|
}
|
|
|
|
static int
|
|
sym_cmp_oid(const void *va, const void *vb)
|
|
{
|
|
const struct sym_oid *a = va;
|
|
const struct sym_oid *b = vb;
|
|
|
|
return der_heim_oid_cmp(a->oid, b->oid);
|
|
}
|
|
|
|
static struct sym_oid *
|
|
sort_sym_oids(int (*cmp)(const void *, const void *))
|
|
{
|
|
struct sym_oid *tmp;
|
|
|
|
init_sym_oids();
|
|
if ((tmp = calloc(num_sym_oids, sizeof(tmp[0]))) == NULL)
|
|
return NULL;
|
|
|
|
memcpy(tmp, sym_oids, num_sym_oids * sizeof(tmp[0]));
|
|
qsort(tmp, num_sym_oids, sizeof(struct sym_oid), cmp);
|
|
return tmp;
|
|
}
|
|
|
|
static int
|
|
fix_oid_name(const char **namep, char **freeme)
|
|
{
|
|
char *dash = strchr(*namep, '-');
|
|
|
|
*freeme = NULL;
|
|
if (dash == NULL)
|
|
return 0;
|
|
if ((*freeme = strdup(*namep)) == NULL)
|
|
return ENOMEM;
|
|
*namep = *freeme;
|
|
for (dash = strchr(*namep, '-'); dash; dash = strchr(dash, '-'))
|
|
*dash = '_';
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
der_find_heim_oid_by_name(const char *str, const heim_oid **oid)
|
|
{
|
|
size_t right = num_sym_oids - 1;
|
|
size_t left = 0;
|
|
char *s = NULL;
|
|
int ret;
|
|
|
|
*oid = NULL;
|
|
if (sym_oids_sorted_by_name == NULL &&
|
|
(sym_oids_sorted_by_name = sort_sym_oids(sym_cmp_name)) == NULL)
|
|
return ENOMEM;
|
|
|
|
if ((ret = fix_oid_name(&str, &s)))
|
|
return ret;
|
|
|
|
while (left <= right) {
|
|
size_t mid = left + (right - left) / 2;
|
|
int cmp;
|
|
|
|
cmp = strcmp(str, sym_oids_sorted_by_name[mid].sym);
|
|
if (cmp == 0) {
|
|
*oid = sym_oids_sorted_by_name[mid].oid;
|
|
free(s);
|
|
return 0;
|
|
}
|
|
if (cmp < 0 && mid > 0) {/* avoid underflow */
|
|
right = mid - 1;
|
|
} else if (cmp < 0) {
|
|
free(s);
|
|
return -1;
|
|
} else {
|
|
left = mid + 1;
|
|
}
|
|
}
|
|
free(s);
|
|
return -1;
|
|
}
|
|
|
|
int
|
|
der_find_or_parse_heim_oid(const char *str, const char *sep, heim_oid *oid)
|
|
{
|
|
const heim_oid *found = NULL;
|
|
|
|
switch (der_find_heim_oid_by_name(str, &found)) {
|
|
case 0: return der_copy_oid(found, oid);
|
|
case -1: return der_parse_heim_oid(str, sep, oid);
|
|
default: return ENOMEM;
|
|
}
|
|
}
|
|
|
|
int
|
|
der_find_heim_oid_by_oid(const heim_oid *oid, const char **name)
|
|
{
|
|
size_t right = num_sym_oids;
|
|
size_t left = 0;
|
|
|
|
*name = NULL;
|
|
if (sym_oids_sorted_by_oid == NULL &&
|
|
(sym_oids_sorted_by_oid = sort_sym_oids(sym_cmp_oid)) == NULL)
|
|
return ENOMEM;
|
|
|
|
while (left <= right) {
|
|
size_t mid = left + (right - left) / 2;
|
|
int cmp;
|
|
|
|
cmp = der_heim_oid_cmp(oid, sym_oids_sorted_by_oid[mid].oid);
|
|
if (cmp == 0) {
|
|
*name = sym_oids_sorted_by_oid[mid].sym;
|
|
return 0;
|
|
}
|
|
if (cmp < 0 && right)
|
|
right = mid - 1;
|
|
else if (cmp < 0)
|
|
return -1;
|
|
else if (mid < num_sym_oids - 1)
|
|
left = mid + 1;
|
|
else
|
|
return -1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int
|
|
der_match_heim_oid_by_name(const char *str, int *c, const heim_oid **oid)
|
|
{
|
|
size_t i;
|
|
char *s = NULL;
|
|
int ret;
|
|
|
|
if ((ret = fix_oid_name(&str, &s)))
|
|
return ret;
|
|
|
|
if (*c < 0)
|
|
*c = 0;
|
|
|
|
init_sym_oids();
|
|
for (i = (size_t)*c; i < num_sym_oids; i++) {
|
|
/*
|
|
* XXX We need a lib/roken strcasestr(), or maybe we should support
|
|
* globbing here.
|
|
*/
|
|
if (strstr(sym_oids[i].sym, str)) {
|
|
*oid = sym_oids[i].oid;
|
|
free(s);
|
|
if (i >= INT_MAX)
|
|
return -1;
|
|
*c = i + 1; /* num_sym_oids is much less than INT_MAX */
|
|
return 0;
|
|
}
|
|
}
|
|
free(s);
|
|
return -1;
|
|
}
|
|
|
|
/* Warning: der_print_heim_oid_sym() will not round-trip */
|
|
|
|
int
|
|
der_print_heim_oid_sym(const heim_oid *oid, char delim, char **strp)
|
|
{
|
|
const char *sym;
|
|
char *s1 = NULL;
|
|
char *s2 = NULL;
|
|
int ret;
|
|
|
|
if (der_find_heim_oid_by_oid(oid, &sym))
|
|
return der_print_heim_oid(oid, delim, strp);
|
|
|
|
if ((ret = der_print_heim_oid(oid, delim, &s1)))
|
|
return ret;
|
|
if (asprintf(&s2, "%s (%s)", s1, sym) == -1 || s2 == NULL) {
|
|
*strp = s1;
|
|
return 0;
|
|
}
|
|
*strp = s2;
|
|
free(s1);
|
|
return 0;
|
|
}
|