
This commit _mostly_ fixes the Heimdal ASN.1 compiler to properly support IMPLICIT tagging in most if not all the many cases where it didn't already, as you could see in lib/asn1/canthandle.asn1 prior to this commit. This fix is a bit of a hack in that a proper fix would change the function prototypes of the encode/decode/length functions generated by the compiler to take an optional IMPLICIT tag to tag with instead of the type they code. That fix would not be localized to lib/asn1/ however, and would change the API and ABI of generated code (which is mostly not an ABI for Heimdal, but still, some external projects would have to make changes). Instead, for IMPLICIT tags we currently depend on the IMPLICIT tag and the sub-type's tag having the same size -- this can be fixed with extra allocation on the encoder side as we do on the decoder side, but we might leave it for later. The issue we're fixing manifested as: -- The [CONTEXT 0] tag in Bar below was turned into an EXPLICIT tag -- instead of an IMPLICIT one, netting the DER encoding for the `foo` -- member as: -- [CONTEXT 0] [UNIVERSAL Seq] [UNIVERSAL Int] <encoding of i> -- instead of the correct: -- [CONTEXT 0] [UNIVERSAL Int] <encoding of i> Foo ::= SEQUENCE { i INTEGER } Bar ::= SEQUENCE { foo [0] IMPLICIT Foo } or Foo ::= INTEGER Bar ::= SEQUENCE { foo [0] IMPLICIT Foo } -- tag context 0 marked -- constructed! I've reviewed this in part by reviewing the output of the compiler before and after this change using this procedure: - Run an earlier version of the ASN.1 compiler output for all modules in lib/asn1/. Save these in a different location. - Run this (or later) version of the ASN.1 compiler output for the same modules, adding --original-order for modules that have been manually sorted already (e.g., rfc2459.asn1). - Run clang-format on the saved and newest generated C source and header files. - Diff the generated output. Substantial differences will relate to handling of IMPLICIT tagging. These are particularly evident in the tcg.asn1 module, which uses a lot of those. Later commits add test data (certificates with extensions that use IMPLICIT tagging) taken from external specifications as well, which exercise this fix. Non-urgent brokenness yet to be fixed: - When the IMPLICIT tag and the tag of the underlying type require differing numbers of bytes to encode, the encoding and decoding will fail. The prototypes of generated length_*() functions make it impossible to do much better. - SET OF <primitive> still crashes the compiler (not a new bug). Futures: - Unwind hackery in cms.asn1 that worked around our lack of proper IMPLICIT tagging support. Here are some of the generated code deltas one expects to see around this commit: $ git checkout $earlier_version $ ./autogen.sh $ mkdir build $ cd build $ ../configure ... $ make -j4 $ make check $ cd lib/asn1 $ for i in *.c; do [[ $i = asn1parse.? || $i = lex.? || $i = *.h ]] && continue clang-format -i $i $i cmp /tmp/save/$i $i && echo NO DIFFS: $i && continue; echo DIFF: $i done NO DIFFS: asn1_cms_asn1.c NO DIFFS: asn1_digest_asn1.c NO DIFFS: asn1_err.c NO DIFFS: asn1_krb5_asn1.c /tmp/save/asn1_kx509_asn1.c asn1_kx509_asn1.c differ: byte 6433, line 264 DIFF: asn1_kx509_asn1.c NO DIFFS: asn1_ocsp_asn1.c NO DIFFS: asn1_pkcs10_asn1.c /tmp/save/asn1_pkcs12_asn1.c asn1_pkcs12_asn1.c differ: byte 12934, line 455 DIFF: asn1_pkcs12_asn1.c NO DIFFS: asn1_pkcs8_asn1.c NO DIFFS: asn1_pkcs9_asn1.c NO DIFFS: asn1_pkinit_asn1.c /tmp/save/asn1_rfc2459_asn1.c asn1_rfc2459_asn1.c differ: byte 20193, line 532 DIFF: asn1_rfc2459_asn1.c NO DIFFS: asn1_rfc4043_asn1.c /tmp/save/asn1_rfc4108_asn1.c asn1_rfc4108_asn1.c differ: byte 595, line 26 DIFF: asn1_rfc4108_asn1.c /tmp/save/asn1_tcg_asn1.c asn1_tcg_asn1.c differ: byte 31835, line 1229 DIFF: asn1_tcg_asn1.c /tmp/save/asn1_test_asn1.c asn1_test_asn1.c differ: byte 384, line 21 DIFF: asn1_test_asn1.c /tmp/save/test_template_asn1-template.c test_template_asn1-template.c differ: byte 650, line 20 DIFF: test_template_asn1-template.c $ $ cd ../.. $ git checkout $newer_version $ make -j4 && make check $ cd lib/asn1 $ for i in *.[ch]; do [[ $i = asn1parse.? || $i = lex.? || $i = *.h ]] && continue clang-format -i $i $i cmp /tmp/save/$i $i && echo NO DIFFS: $i && continue diff -ubw /tmp/save/$i $i done | $PAGER and one should see deltas such as the following: - a small enhancement to handling of OPTIONAL members: (data)->macData = calloc(1, sizeof(*(data)->macData)); if ((data)->macData == NULL) goto fail; e = decode_PKCS12_MacData(p, len, (data)->macData, &l); - if (e) { + if (e == ASN1_MISSING_FIELD) { free((data)->macData); (data)->macData = NULL; + } else if (e) { + goto fail; } else { p += l; len -= l; ret += l; - more complete handling of DEFAULTed members: e = decode_FWReceiptVersion(p, len, &(data)->version, &l); - if (e) + if (e == ASN1_MISSING_FIELD) { + (data)->version = 1; + } else if (e) { goto fail; - p += l; - len -= l; - ret += l; + } else { + p += l; + len -= l; + ret += l; + } { - replacement of tags with implicit tags (encode side): /* targetUri */ if ((data)->targetUri) { size_t Top_tag_oldret HEIMDAL_UNUSED_ATTRIBUTE = ret; ret = 0; e = encode_URIReference(p, len, (data)->targetUri, &l); if (e) return e; p -= l; len -= l; ret += l; - e = der_put_length_and_tag(p, len, ret, ASN1_C_CONTEXT, PRIM, 4, &l); + e = der_replace_tag(p, len, ASN1_C_CONTEXT, CONS, 4); if (e) return e; p -= l; len -= l; ret += l; ret += Top_tag_oldret; } - replacement of tags with implicit tags (decode side): strengthOfFunction_oldlen = len; if (strengthOfFunction_datalen > len) { e = ASN1_OVERRUN; goto fail; } len = strengthOfFunction_datalen; - e = decode_StrengthOfFunction(p, len, (data)->strengthOfFunction, &l); - if (e) - goto fail; - p += l; - len -= l; - ret += l; + { + unsigned char *pcopy; + pcopy = calloc(1, len); + if (pcopy == 0) { + e = ENOMEM; + goto fail; + } + memcpy(pcopy, p, len); + e = der_replace_tag(pcopy, len, ASN1_C_UNIV, PRIM, 0); + if (e) + goto fail; + e = decode_StrengthOfFunction(p, len, (data)->strengthOfFunction, &l); + if (e) + goto fail; + p += l; + len -= l; + ret += l; + free(pcopy); + } len = strengthOfFunction_oldlen - strengthOfFunction_datalen; } } { size_t profileOid_datalen, profileOid_oldlen; - correct determination of implicit tag constructed vs no for IMPLICT- tagged named primitive types: { size_t profileUri_datalen, profileUri_oldlen; Der_type profileUri_type; e = der_match_tag_and_length(p, len, ASN1_C_CONTEXT, &profileUri_type, 2, &profileUri_datalen, &l); - if (e == 0 && profileUri_type != PRIM) { + if (e == 0 && profileUri_type != CONS) { e = ASN1_BAD_ID; } if (e) { (data)->profileUri = NULL; } else { (data)->profileUri = calloc(1, sizeof(*(data)->profileUri)); if ((data)->profileUri == NULL) { e = ENOMEM; goto fail; } - correct determination of length of IMPLICT-tagged OIDs: if ((data)->profileOid) { size_t Top_tag_oldret = ret; ret = 0; ret += der_length_oid((data)->profileOid); + ret += 1 + der_length_len(ret); ret += Top_tag_oldret; } These deltas should be examined with the corresponding ASN.1 module at hand, cross-referencing the source code to the ASN.1 type definitions and manually applying X.690 rules to double-check the choices of primitive vs. constructed tag, and the choices of when to replace tags and when not.
166 lines
5.5 KiB
C
166 lines
5.5 KiB
C
/*
|
|
* Copyright (c) 1997-2005 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.
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
#ifndef __GEN_LOCL_H__
|
|
#define __GEN_LOCL_H__
|
|
|
|
#include <config.h>
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
#include <ctype.h>
|
|
#include <time.h>
|
|
#include <errno.h>
|
|
#include <err.h>
|
|
#include <roken.h>
|
|
#include <getarg.h>
|
|
#include "hash.h"
|
|
#include "symbol.h"
|
|
#include "asn1-common.h"
|
|
#include "der.h"
|
|
#include "der-private.h"
|
|
|
|
/*
|
|
* XXX We need to move all module state out of globals and into a struct that
|
|
* we pass around when parsing and compiling a module, and also that we keep on
|
|
* a linked list of parsed modules.
|
|
*
|
|
* This is needed to:
|
|
*
|
|
* - implement IMPORTS correctly, because we need to know the type of a symbol
|
|
* in order to emit an extern declaration of it
|
|
* - implement value parsing
|
|
* - implement an ASN.1 library that does value parsing
|
|
*
|
|
* Value parsing, in particular, would be fantastic. We could then have
|
|
* options in hxtool(1) to load arbitrary ASN.1 modules and then parse SAN
|
|
* values given in ASN.1 value syntax on the command-line or in files. Eat
|
|
* your heart out OpenSSL if we do this!
|
|
*
|
|
* As well we'll need a `-I' option to the compiler so it knows where to find
|
|
* modules to IMPORT FROM.
|
|
*/
|
|
typedef struct asn1_module {
|
|
/* Name of ASN.1 module file: */
|
|
const char *orig_filename;
|
|
/* Name of file to always include for common type definitions: */
|
|
const char *type_file_string;
|
|
/* Name of public header file for module: */
|
|
const char *header;
|
|
/* Name of private header file for module: */
|
|
const char *privheader;
|
|
/* Basename of module: */
|
|
const char *headerbase;
|
|
/* Open stdio file handles for output: */
|
|
FILE *privheaderfile;
|
|
FILE *headerfile;
|
|
FILE *oidsfile;
|
|
FILE *codefile;
|
|
FILE *logfile;
|
|
FILE *templatefile;
|
|
/* Module contents: */
|
|
struct sexport *exports;
|
|
struct import *imports;
|
|
Hashtab *htab; /* symbols */
|
|
/* Template state: */
|
|
struct templatehead *template;
|
|
struct tlisthead *tlistmaster;
|
|
/* CLI options and flags needed everywhere: */
|
|
getarg_strings preserve;
|
|
getarg_strings seq;
|
|
const char *enum_prefix;
|
|
unsigned int one_code_file:1;
|
|
unsigned int support_ber:1;
|
|
unsigned int parse_units_flag:1;
|
|
unsigned int prefix_enum:1; /* Should be a getarg_strings of bitrsting types to do this for */
|
|
unsigned int rfc1510_bitstring:1; /* Should be a getarg_strings of bitrsting types to do this for */
|
|
} *asn1_module;
|
|
|
|
void generate_type (const Symbol *);
|
|
void generate_type_header_forwards(const Symbol *);
|
|
void generate_constant (const Symbol *);
|
|
void generate_type_encode (const Symbol *);
|
|
void generate_type_decode (const Symbol *);
|
|
void generate_type_free (const Symbol *);
|
|
void generate_type_length (const Symbol *);
|
|
void generate_type_copy (const Symbol *);
|
|
void generate_type_seq (const Symbol *);
|
|
void generate_glue (const Type *, const char*);
|
|
|
|
const char *classname(Der_class);
|
|
const char *valuename(Der_class, int);
|
|
|
|
void gen_compare_defval(const char *, struct value *);
|
|
void gen_assign_defval(const char *, struct value *);
|
|
|
|
|
|
void init_generate (const char *, const char *);
|
|
const char *get_filename (void);
|
|
void close_generate(void);
|
|
void add_import(const char *);
|
|
void add_export(const char *);
|
|
int is_export(const char *);
|
|
int yyparse(void);
|
|
int is_primitive_type(const Type *);
|
|
|
|
int preserve_type(const char *);
|
|
int seq_type(const char *);
|
|
|
|
void generate_header_of_codefile(const char *);
|
|
void close_codefile(void);
|
|
|
|
int is_template_compat (const Symbol *);
|
|
void generate_template(const Symbol *);
|
|
void generate_template_type_forward(const char *);
|
|
void gen_template_import(const Symbol *);
|
|
|
|
|
|
extern FILE *privheaderfile, *headerfile, *codefile, *logfile, *templatefile;
|
|
extern const char *fuzzer_string;
|
|
extern int support_ber;
|
|
extern int template_flag;
|
|
extern int rfc1510_bitstring;
|
|
extern int one_code_file;
|
|
extern int original_order;
|
|
extern int parse_units_flag;
|
|
extern char *type_file_string;
|
|
|
|
extern int error_flag;
|
|
|
|
#endif /* __GEN_LOCL_H__ */
|