Finally. We're almost at parity for the template compiler.
Now we have a build option to use templating:
`./configure --enable-asn1-templating`
Tests fail if you build `rfc2459.asn1` with `--template`.
TBD: Figure out what differences remain between the two compilers, and
fix the templating compiler accordingly, adding tests along the
way.
Making IMPLICIT tags work in the templating compiler turned out to be a
simple fix: don't attempt to do anything clever about IMPLICIT tags in
the template generator in the compiler other than denoting them --
instead leave all the smarts about IMPLICIT tags to the interpreter.
This might be a very slight pessimization, but also a great
simplification.
The result is very elegant: when the interpreter finds an IMPLICIT
tag it then recurses to find the template for the body of the type
so-tagged, and evaluates that. Much more elegant than the code
generated by the non-template compiler, not least for not needing
any additional temporary memory allocation.
With this we finally have parity in basic testing of the template
compiler. Indeed, for IMPLICIT tags the template compiler and
interpreter might even be better because they support IMPLICIT tags
with BER lengths, whereas the non-template compiler doesn't (mostly
because `der_replace_tag()` needs to be changed to support it.
And, of course, the template compiler is simply superior in that it
produces smaller code and is *much* easier to work with because the
functions to interpret templates are small and simple. Which means we
can add more functions to deal with other encoding rules fairly
trivially. It should be possible to add all of these with very little
work, almost all of it localized to `lib/asn1/template.c`:
- PER Packed Encoding Rules [X.691]
- XER XML Encoding Rules [X.693]
- OER Octet Encoding Rules [X.696] (intended to replace PER)
- JER JSON Encoding Rules [X.697] (doubles as visual representation)
- GSER Generic String E.R.s [RFC3641] (a visual representation)
- XDR External Data Repr. [STD67][RFC4506]
(XDR is *not* an ASN.1 encoding rules specification, but it's a
*lot* like PER/OER but with 4-octet alignment, and is specified
for the syntax equivalent (XDR) of only a subset of ASN.1 syntax
and semantics.)
All we'd have to do is add variants of `_asn1_{length,encode,decode}()`
for each set of rules, then generate per-type stub functions that call
them (as we already do for DER).
We could then have an encoding rule transliteration program that takes a
`TypeName` and some representation of a value encoded by some encoding
rules, and outputs the same thing encoded by a different set of rules.
This would double as a pretty-printer and parser if we do add support
for JER and/or GSER. It would find the template for the given type
using `dlsym()` against some shared object (possibly `libasn1` itself).
Whereas generating source code for C (or whatever language) for
additional ERs requires much more work. Plus, templates are much
smaller, and the interpreter is tiny, which yields much smaller text and
much smaller CPU icache/dcache footprint, which yields better
performance in many cases.
As well, the template system should be much easier to port to other
languages. Though in the cases of, e.g., Rust, it would require use of
`unsafe` in the interpreter, so in fact the inverse might be true: that
it's easier to generate safe Rust code than to implement a template
interpreter in Rust. Similarly for Haskell, OCAML, etc. But wherever
the template interpreter is easy to implement, it's a huge win.
Note that implementing OER and PER using the templates as they are
currently would be a bit of a challenge, as the interpreter would have
to first do a pass of each SEQUENCE/SET to determine the size and
layout of the OER/PER sequence/set preamble by counting the number of
OPTIONAL/DEFAULT members, BOOLEAN members, and extensibility markers
with extensions present. We could always generate more entries to
encode precomputed preamble metadata. We would also need to add a
template entry type for extensibility markers, which currently we do
not.
1286 lines
31 KiB
C
1286 lines
31 KiB
C
/*
|
|
* Copyright (c) 2009 Kungliga Tekniska Högskolan
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
|
* All rights reserved.
|
|
*
|
|
* Portions Copyright (c) 2009 - 2010 Apple Inc. 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 <com_err.h>
|
|
|
|
struct asn1_type_func asn1_template_prim[A1T_NUM_ENTRY] = {
|
|
#define el(name, type) { \
|
|
(asn1_type_encode)der_put_##name, \
|
|
(asn1_type_decode)der_get_##name, \
|
|
(asn1_type_length)der_length_##name, \
|
|
(asn1_type_copy)der_copy_##name, \
|
|
(asn1_type_release)der_free_##name, \
|
|
sizeof(type) \
|
|
}
|
|
#define elber(name, type) { \
|
|
(asn1_type_encode)der_put_##name, \
|
|
(asn1_type_decode)der_get_##name##_ber, \
|
|
(asn1_type_length)der_length_##name, \
|
|
(asn1_type_copy)der_copy_##name, \
|
|
(asn1_type_release)der_free_##name, \
|
|
sizeof(type) \
|
|
}
|
|
el(integer, int),
|
|
el(heim_integer, heim_integer),
|
|
el(integer, int),
|
|
el(integer64, int64_t),
|
|
el(unsigned, unsigned),
|
|
el(unsigned64, uint64_t),
|
|
el(general_string, heim_general_string),
|
|
el(octet_string, heim_octet_string),
|
|
elber(octet_string, heim_octet_string),
|
|
el(ia5_string, heim_ia5_string),
|
|
el(bmp_string, heim_bmp_string),
|
|
el(universal_string, heim_universal_string),
|
|
el(printable_string, heim_printable_string),
|
|
el(visible_string, heim_visible_string),
|
|
el(utf8string, heim_utf8_string),
|
|
el(generalized_time, time_t),
|
|
el(utctime, time_t),
|
|
el(bit_string, heim_bit_string),
|
|
{ (asn1_type_encode)der_put_boolean, (asn1_type_decode)der_get_boolean,
|
|
(asn1_type_length)der_length_boolean, (asn1_type_copy)der_copy_integer,
|
|
(asn1_type_release)der_free_integer, sizeof(int)
|
|
},
|
|
el(oid, heim_oid),
|
|
el(general_string, heim_general_string),
|
|
#undef el
|
|
#undef elber
|
|
};
|
|
|
|
size_t
|
|
_asn1_sizeofType(const struct asn1_template *t)
|
|
{
|
|
return t->offset;
|
|
}
|
|
|
|
/*
|
|
* Here is abstraction to not so well evil fact of bit fields in C,
|
|
* they are endian dependent, so when getting and setting bits in the
|
|
* host local structure we need to know the endianness of the host.
|
|
*
|
|
* Its not the first time in Heimdal this have bitten us, and some day
|
|
* we'll grow up and use #defined constant, but bit fields are still
|
|
* so pretty and shiny.
|
|
*/
|
|
|
|
static void
|
|
_asn1_bmember_get_bit(const unsigned char *p, void *data,
|
|
unsigned int bit, size_t size)
|
|
{
|
|
unsigned int localbit = bit % 8;
|
|
if ((*p >> (7 - localbit)) & 1) {
|
|
#ifdef WORDS_BIGENDIAN
|
|
*(unsigned int *)data |= (1 << ((size * 8) - bit - 1));
|
|
#else
|
|
*(unsigned int *)data |= (1 << bit);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
int
|
|
_asn1_bmember_isset_bit(const void *data, unsigned int bit, size_t size)
|
|
{
|
|
#ifdef WORDS_BIGENDIAN
|
|
if ((*(unsigned int *)data) & (1 << ((size * 8) - bit - 1)))
|
|
return 1;
|
|
return 0;
|
|
#else
|
|
if ((*(unsigned int *)data) & (1 << bit))
|
|
return 1;
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
void
|
|
_asn1_bmember_put_bit(unsigned char *p, const void *data, unsigned int bit,
|
|
size_t size, unsigned int *bitset)
|
|
{
|
|
unsigned int localbit = bit % 8;
|
|
|
|
if (_asn1_bmember_isset_bit(data, bit, size)) {
|
|
*p |= (1 << (7 - localbit));
|
|
if (*bitset == 0)
|
|
*bitset = (7 - localbit) + 1;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Utility function to tell us if the encoding of some type per its template
|
|
* will have an outer tag. This is needed when the caller wants to slap on an
|
|
* IMPLICIT tag: if the inner type has a tag then we need to replace it.
|
|
*/
|
|
static int
|
|
is_tagged(const struct asn1_template *t)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
|
|
t += A1_HEADER_LEN(t);
|
|
if (elements != 1)
|
|
return 0;
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_SEQOF: return 0;
|
|
case A1_OP_SETOF: return 0;
|
|
case A1_OP_BMEMBER: return 0;
|
|
case A1_OP_PARSE: return 0;
|
|
case A1_OP_TAG: return 1;
|
|
case A1_OP_CHOICE: return 1;
|
|
case A1_OP_TYPE: return 1;
|
|
case A1_OP_TYPE_EXTERN: {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
|
|
/*
|
|
* XXX Add a boolean to struct asn1_type_func to tell us if the type is
|
|
* tagged or not. Basically, it's not tagged if it's primitive.
|
|
*/
|
|
if (f->encode == (asn1_type_encode)encode_heim_any)
|
|
return 0;
|
|
abort(); /* XXX */
|
|
}
|
|
default: abort();
|
|
}
|
|
}
|
|
|
|
static size_t
|
|
inner_type_taglen(const struct asn1_template *t)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
|
|
t += A1_HEADER_LEN(t);
|
|
if (elements != 1)
|
|
return 0;
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_SEQOF: return 0;
|
|
case A1_OP_SETOF: return 0;
|
|
case A1_OP_BMEMBER: return 0;
|
|
case A1_OP_PARSE: return 0;
|
|
case A1_OP_CHOICE: return 1;
|
|
case A1_OP_TYPE: return inner_type_taglen(t->ptr);
|
|
case A1_OP_TAG: return der_length_tag(A1_TAG_TAG(t->tt));
|
|
case A1_OP_TYPE_EXTERN: {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
|
|
/*
|
|
* XXX Add a boolean to struct asn1_type_func to tell us if the type is
|
|
* tagged or not. Basically, it's not tagged if it's primitive.
|
|
*/
|
|
if (f->encode == (asn1_type_encode)encode_heim_any)
|
|
return 0;
|
|
abort(); /* XXX */
|
|
}
|
|
default: abort();
|
|
}
|
|
}
|
|
|
|
int
|
|
_asn1_decode(const struct asn1_template *t, unsigned flags,
|
|
const unsigned char *p, size_t len, void *data, size_t *size)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
size_t oldlen = len;
|
|
int ret = 0;
|
|
const unsigned char *startp = NULL;
|
|
unsigned int template_flags = t->tt;
|
|
|
|
/* skip over header */
|
|
t++;
|
|
|
|
if (template_flags & A1_HF_PRESERVE)
|
|
startp = p;
|
|
|
|
while (elements) {
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_TYPE:
|
|
case A1_OP_TYPE_EXTERN: {
|
|
size_t newsize, elsize;
|
|
void *el = DPO(data, t->offset);
|
|
void **pel = (void **)el;
|
|
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
elsize = _asn1_sizeofType(t->ptr);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
elsize = f->size;
|
|
}
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
*pel = calloc(1, elsize);
|
|
if (*pel == NULL)
|
|
return ENOMEM;
|
|
el = *pel;
|
|
}
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
ret = _asn1_decode(t->ptr, flags, p, len, el, &newsize);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
ret = (f->decode)(p, len, el, &newsize);
|
|
}
|
|
if (ret) {
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
free(*pel);
|
|
*pel = NULL;
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
p += newsize; len -= newsize;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_TAG: {
|
|
Der_type dertype;
|
|
size_t newsize;
|
|
size_t datalen, l;
|
|
void *olddata = data;
|
|
int is_indefinite = 0;
|
|
int subflags = flags;
|
|
int replace_tag = (t->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
|
|
|
|
ret = der_match_tag_and_length(p, len, A1_TAG_CLASS(t->tt),
|
|
&dertype, A1_TAG_TAG(t->tt),
|
|
&datalen, &l);
|
|
if (ret) {
|
|
if (t->tt & A1_FLAG_OPTIONAL)
|
|
break;
|
|
return ret;
|
|
}
|
|
|
|
p += l; len -= l;
|
|
|
|
/*
|
|
* Only allow indefinite encoding for OCTET STRING and BER
|
|
* for now. Should handle BIT STRING too.
|
|
*/
|
|
|
|
if (dertype != A1_TAG_TYPE(t->tt) && (flags & A1_PF_ALLOW_BER)) {
|
|
const struct asn1_template *subtype = t->ptr;
|
|
subtype++; /* skip header */
|
|
|
|
if (((subtype->tt & A1_OP_MASK) == A1_OP_PARSE) &&
|
|
A1_PARSE_TYPE(subtype->tt) == A1T_OCTET_STRING)
|
|
subflags |= A1_PF_INDEFINTE;
|
|
}
|
|
|
|
if (datalen == ASN1_INDEFINITE) {
|
|
if ((flags & A1_PF_ALLOW_BER) == 0)
|
|
return ASN1_GOT_BER;
|
|
is_indefinite = 1;
|
|
datalen = len;
|
|
if (datalen < 2)
|
|
return ASN1_OVERRUN;
|
|
/* hide EndOfContent for sub-decoder, catching it below */
|
|
datalen -= 2;
|
|
} else if (datalen > len)
|
|
return ASN1_OVERRUN;
|
|
|
|
data = DPO(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **el = (void **)data;
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
|
|
*el = calloc(1, ellen);
|
|
if (*el == NULL)
|
|
return ENOMEM;
|
|
data = *el;
|
|
}
|
|
|
|
if (replace_tag) {
|
|
const struct asn1_template *subtype = t->ptr;
|
|
int have_tag = 0;
|
|
|
|
/*
|
|
* So, we have an IMPLICIT tag. What we want to do is find the
|
|
* template for the body of the type so-tagged. That's going
|
|
* to be a template that has a tag that isn't itself IMPLICIT.
|
|
*
|
|
* So we chase the pointer in the template until we find such a
|
|
* thing, then decode using that template.
|
|
*/
|
|
while (!have_tag) {
|
|
subtype++;
|
|
if ((subtype->tt & A1_OP_MASK) == A1_OP_TAG)
|
|
replace_tag = (subtype->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
|
|
if (replace_tag) {
|
|
subtype = subtype->ptr;
|
|
continue;
|
|
}
|
|
if ((subtype->tt & A1_OP_MASK) == A1_OP_TAG) {
|
|
ret = _asn1_decode(subtype->ptr, subflags, p, datalen, data, &newsize);
|
|
have_tag = 1;
|
|
} else {
|
|
subtype = subtype->ptr;
|
|
}
|
|
}
|
|
} else {
|
|
ret = _asn1_decode(t->ptr, subflags, p, datalen, data, &newsize);
|
|
}
|
|
if (ret)
|
|
return ret;
|
|
|
|
if (is_indefinite) {
|
|
/* If we use indefinite encoding, the newsize is the datasize. */
|
|
datalen = newsize;
|
|
} else if (newsize != datalen) {
|
|
/* Check for hidden data that might be after the real tag */
|
|
return ASN1_EXTRA_DATA;
|
|
}
|
|
|
|
len -= datalen;
|
|
p += datalen;
|
|
|
|
/*
|
|
* Indefinite encoding needs a trailing EndOfContent,
|
|
* check for that.
|
|
*/
|
|
if (is_indefinite) {
|
|
ret = der_match_tag_and_length(p, len, ASN1_C_UNIV,
|
|
&dertype, UT_EndOfContent,
|
|
&datalen, &l);
|
|
if (ret)
|
|
return ret;
|
|
if (dertype != PRIM)
|
|
return ASN1_BAD_ID;
|
|
if (datalen != 0)
|
|
return ASN1_INDEF_EXTRA_DATA;
|
|
p += l; len -= l;
|
|
}
|
|
data = olddata;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_PARSE: {
|
|
unsigned int type = A1_PARSE_TYPE(t->tt);
|
|
size_t newsize;
|
|
void *el = DPO(data, t->offset);
|
|
|
|
/*
|
|
* INDEFINITE primitive types are one element after the
|
|
* same type but non-INDEFINITE version.
|
|
*/
|
|
if (flags & A1_PF_INDEFINTE)
|
|
type++;
|
|
|
|
if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
|
|
ABORT_ON_ERROR();
|
|
return ASN1_PARSE_ERROR;
|
|
}
|
|
|
|
ret = (asn1_template_prim[type].decode)(p, len, el, &newsize);
|
|
if (ret)
|
|
return ret;
|
|
p += newsize; len -= newsize;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_SETOF:
|
|
case A1_OP_SEQOF: {
|
|
struct template_of *el = DPO(data, t->offset);
|
|
size_t newsize;
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
size_t vallength = 0;
|
|
|
|
while (len > 0) {
|
|
void *tmp;
|
|
size_t newlen = vallength + ellen;
|
|
if (vallength > newlen)
|
|
return ASN1_OVERFLOW;
|
|
|
|
tmp = realloc(el->val, newlen);
|
|
if (tmp == NULL)
|
|
return ENOMEM;
|
|
|
|
memset(DPO(tmp, vallength), 0, ellen);
|
|
el->val = tmp;
|
|
|
|
ret = _asn1_decode(t->ptr, flags & (~A1_PF_INDEFINTE), p, len,
|
|
DPO(el->val, vallength), &newsize);
|
|
if (ret)
|
|
return ret;
|
|
vallength = newlen;
|
|
el->len++;
|
|
p += newsize; len -= newsize;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case A1_OP_BMEMBER: {
|
|
const struct asn1_template *bmember = t->ptr;
|
|
size_t bsize = bmember->offset;
|
|
size_t belements = A1_HEADER_LEN(bmember);
|
|
size_t pos = 0;
|
|
|
|
bmember++;
|
|
|
|
memset(data, 0, bsize);
|
|
|
|
if (len < 1)
|
|
return ASN1_OVERRUN;
|
|
p++; len--;
|
|
|
|
while (belements && len) {
|
|
while (bmember->offset / 8 > pos / 8) {
|
|
if (len < 1)
|
|
break;
|
|
p++; len--;
|
|
pos += 8;
|
|
}
|
|
if (len) {
|
|
_asn1_bmember_get_bit(p, data, bmember->offset, bsize);
|
|
belements--; bmember++;
|
|
}
|
|
}
|
|
len = 0;
|
|
break;
|
|
}
|
|
case A1_OP_CHOICE: {
|
|
const struct asn1_template *choice = t->ptr;
|
|
unsigned int *element = DPO(data, choice->offset);
|
|
size_t datalen;
|
|
unsigned int i;
|
|
|
|
/* provide a saner value as default, we should have a NO element value */
|
|
*element = 1;
|
|
|
|
for (i = 1; i < A1_HEADER_LEN(choice) + 1; i++) {
|
|
/* should match first tag instead, store it in choice.tt */
|
|
ret = _asn1_decode(choice[i].ptr, 0, p, len,
|
|
DPO(data, choice[i].offset), &datalen);
|
|
if (ret == 0) {
|
|
*element = i;
|
|
p += datalen; len -= datalen;
|
|
break;
|
|
} else if (ret != ASN1_BAD_ID && ret != ASN1_MISPLACED_FIELD && ret != ASN1_MISSING_FIELD) {
|
|
return ret;
|
|
}
|
|
}
|
|
if (i >= A1_HEADER_LEN(choice) + 1) {
|
|
if (choice->tt == 0)
|
|
return ASN1_BAD_ID;
|
|
|
|
*element = 0;
|
|
ret = der_get_octet_string(p, len,
|
|
DPO(data, choice->tt), &datalen);
|
|
if (ret)
|
|
return ret;
|
|
p += datalen; len -= datalen;
|
|
}
|
|
|
|
break;
|
|
}
|
|
default:
|
|
ABORT_ON_ERROR();
|
|
return ASN1_PARSE_ERROR;
|
|
}
|
|
t++;
|
|
elements--;
|
|
}
|
|
/* if we are using padding, eat up read of context */
|
|
if (template_flags & A1_HF_ELLIPSIS)
|
|
len = 0;
|
|
|
|
oldlen -= len;
|
|
|
|
if (size)
|
|
*size = oldlen;
|
|
|
|
/*
|
|
* saved the raw bits if asked for it, useful for signature
|
|
* verification.
|
|
*/
|
|
if (startp) {
|
|
heim_octet_string *save = data;
|
|
|
|
save->data = malloc(oldlen);
|
|
if (save->data == NULL)
|
|
return ENOMEM;
|
|
else {
|
|
save->length = oldlen;
|
|
memcpy(save->data, startp, oldlen);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
_asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const void *data, size_t *size)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
int ret = 0;
|
|
size_t oldlen = len;
|
|
|
|
t += A1_HEADER_LEN(t);
|
|
|
|
while (elements) {
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_TYPE:
|
|
case A1_OP_TYPE_EXTERN: {
|
|
size_t newsize;
|
|
const void *el = DPOC(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **pel = (void **)el;
|
|
if (*pel == NULL)
|
|
break;
|
|
el = *pel;
|
|
}
|
|
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
ret = _asn1_encode(t->ptr, p, len, el, &newsize);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
ret = (f->encode)(p, len, el, &newsize);
|
|
}
|
|
|
|
if (ret)
|
|
return ret;
|
|
p -= newsize; len -= newsize;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_TAG: {
|
|
const void *olddata = data;
|
|
size_t l, datalen;
|
|
int replace_tag = 0;
|
|
|
|
data = DPOC(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **el = (void **)data;
|
|
if (*el == NULL) {
|
|
data = olddata;
|
|
break;
|
|
}
|
|
data = *el;
|
|
}
|
|
|
|
replace_tag = (t->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
|
|
|
|
/* IMPLICIT tags need special handling (see gen_encode.c) */
|
|
if (replace_tag) {
|
|
unsigned char *pfree, *psave = p;
|
|
Der_class found_class;
|
|
Der_type found_type;
|
|
unsigned int found_tag;
|
|
size_t lensave = len;
|
|
size_t oldtaglen;
|
|
size_t taglen = der_length_tag(A1_TAG_TAG(t->tt));;
|
|
|
|
/* Allocate a buffer at least as big as we need */
|
|
len = _asn1_length(t->ptr, data) + taglen;
|
|
if ((p = pfree = malloc(len)) == NULL) {
|
|
ret = ENOMEM;
|
|
} else {
|
|
/*
|
|
* Encode into it (with the wrong tag, which we'll replace
|
|
* below).
|
|
*/
|
|
p += len - 1;
|
|
ret = _asn1_encode(t->ptr, p, len, data, &datalen);
|
|
}
|
|
if (ret == 0) {
|
|
/* Get the old tag and, critically, its length */
|
|
len -= datalen; p -= datalen;
|
|
ret = der_get_tag(p + 1, datalen, &found_class, &found_type,
|
|
&found_tag, &oldtaglen);
|
|
}
|
|
if (ret == 0) {
|
|
/* Drop the old tag */
|
|
len += oldtaglen; p += oldtaglen;
|
|
/* Put the new tag */
|
|
ret = der_put_tag(p, len,
|
|
A1_TAG_CLASS(t->tt),
|
|
found_type,
|
|
A1_TAG_TAG(t->tt), &l);
|
|
}
|
|
if (ret == 0) {
|
|
/* Copy the encoding where it belongs */
|
|
len -= l; p -= l;
|
|
psave -= (datalen + l - oldtaglen);
|
|
lensave -= (datalen + l - oldtaglen);
|
|
memcpy(psave + 1, p + 1, datalen + l - oldtaglen);
|
|
p = psave;
|
|
len = lensave;
|
|
}
|
|
free(pfree);
|
|
} else {
|
|
/* Easy case */
|
|
ret = _asn1_encode(t->ptr, p, len, data, &datalen);
|
|
if (ret)
|
|
return ret;
|
|
|
|
len -= datalen; p -= datalen;
|
|
|
|
ret = der_put_length_and_tag(p, len, datalen,
|
|
A1_TAG_CLASS(t->tt),
|
|
A1_TAG_TYPE(t->tt),
|
|
A1_TAG_TAG(t->tt), &l);
|
|
if (ret == 0) {
|
|
p -= l; len -= l;
|
|
}
|
|
}
|
|
if (ret)
|
|
return ret;
|
|
|
|
data = olddata;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_PARSE: {
|
|
unsigned int type = A1_PARSE_TYPE(t->tt);
|
|
size_t newsize;
|
|
const void *el = DPOC(data, t->offset);
|
|
|
|
if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
|
|
ABORT_ON_ERROR();
|
|
return ASN1_PARSE_ERROR;
|
|
}
|
|
|
|
ret = (asn1_template_prim[type].encode)(p, len, el, &newsize);
|
|
if (ret)
|
|
return ret;
|
|
p -= newsize; len -= newsize;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_SETOF: {
|
|
const struct template_of *el = DPOC(data, t->offset);
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
heim_octet_string *val;
|
|
unsigned char *elptr = el->val;
|
|
size_t i, totallen;
|
|
|
|
if (el->len == 0)
|
|
break;
|
|
|
|
if (el->len > UINT_MAX/sizeof(val[0]))
|
|
return ERANGE;
|
|
|
|
val = calloc(el->len, sizeof(val[0]));
|
|
if (val == NULL)
|
|
return ENOMEM;
|
|
|
|
for(totallen = 0, i = 0; i < el->len; i++) {
|
|
unsigned char *next;
|
|
size_t l;
|
|
|
|
val[i].length = _asn1_length(t->ptr, elptr);
|
|
if (val[i].length) {
|
|
val[i].data = malloc(val[i].length);
|
|
if (val[i].data == NULL) {
|
|
ret = ENOMEM;
|
|
break;
|
|
}
|
|
}
|
|
|
|
ret = _asn1_encode(t->ptr, DPO(val[i].data, val[i].length - 1),
|
|
val[i].length, elptr, &l);
|
|
if (ret)
|
|
break;
|
|
|
|
next = elptr + ellen;
|
|
if (next < elptr) {
|
|
ret = ASN1_OVERFLOW;
|
|
break;
|
|
}
|
|
elptr = next;
|
|
totallen += val[i].length;
|
|
}
|
|
if (ret == 0 && totallen > len)
|
|
ret = ASN1_OVERFLOW;
|
|
if (ret) {
|
|
for (i = 0; i < el->len; i++)
|
|
free(val[i].data);
|
|
free(val);
|
|
return ret;
|
|
}
|
|
|
|
len -= totallen;
|
|
|
|
qsort(val, el->len, sizeof(val[0]), _heim_der_set_sort);
|
|
|
|
i = el->len - 1;
|
|
do {
|
|
p -= val[i].length;
|
|
memcpy(p + 1, val[i].data, val[i].length);
|
|
free(val[i].data);
|
|
} while(i-- > 0);
|
|
free(val);
|
|
|
|
break;
|
|
|
|
}
|
|
case A1_OP_SEQOF: {
|
|
struct template_of *el = DPO(data, t->offset);
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
size_t newsize;
|
|
unsigned int i;
|
|
unsigned char *elptr = el->val;
|
|
|
|
if (el->len == 0)
|
|
break;
|
|
|
|
elptr += ellen * (el->len - 1);
|
|
|
|
for (i = 0; i < el->len; i++) {
|
|
ret = _asn1_encode(t->ptr, p, len,
|
|
elptr,
|
|
&newsize);
|
|
if (ret)
|
|
return ret;
|
|
p -= newsize; len -= newsize;
|
|
elptr -= ellen;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case A1_OP_BMEMBER: {
|
|
const struct asn1_template *bmember = t->ptr;
|
|
size_t bsize = bmember->offset;
|
|
size_t belements = A1_HEADER_LEN(bmember);
|
|
size_t pos;
|
|
unsigned char c = 0;
|
|
unsigned int bitset = 0;
|
|
int rfc1510 = (bmember->tt & A1_HBF_RFC1510);
|
|
|
|
bmember += belements;
|
|
|
|
if (rfc1510)
|
|
pos = 31;
|
|
else
|
|
pos = bmember->offset;
|
|
|
|
while (belements && len) {
|
|
while (bmember->offset / 8 < pos / 8) {
|
|
if (rfc1510 || bitset || c) {
|
|
if (len < 1)
|
|
return ASN1_OVERFLOW;
|
|
*p-- = c; len--;
|
|
}
|
|
c = 0;
|
|
pos -= 8;
|
|
}
|
|
_asn1_bmember_put_bit(&c, data, bmember->offset, bsize, &bitset);
|
|
belements--; bmember--;
|
|
}
|
|
if (rfc1510 || bitset) {
|
|
if (len < 1)
|
|
return ASN1_OVERFLOW;
|
|
*p-- = c; len--;
|
|
}
|
|
|
|
if (len < 1)
|
|
return ASN1_OVERFLOW;
|
|
if (rfc1510 || bitset == 0)
|
|
*p-- = 0;
|
|
else
|
|
*p-- = bitset - 1;
|
|
|
|
len--;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_CHOICE: {
|
|
const struct asn1_template *choice = t->ptr;
|
|
const unsigned int *element = DPOC(data, choice->offset);
|
|
size_t datalen;
|
|
const void *el;
|
|
|
|
if (*element > A1_HEADER_LEN(choice)) {
|
|
printf("element: %d\n", *element);
|
|
return ASN1_PARSE_ERROR;
|
|
}
|
|
|
|
if (*element == 0) {
|
|
ret += der_put_octet_string(p, len,
|
|
DPOC(data, choice->tt), &datalen);
|
|
} else {
|
|
choice += *element;
|
|
el = DPOC(data, choice->offset);
|
|
ret = _asn1_encode(choice->ptr, p, len, el, &datalen);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
len -= datalen; p -= datalen;
|
|
|
|
break;
|
|
}
|
|
default:
|
|
ABORT_ON_ERROR();
|
|
}
|
|
t--;
|
|
elements--;
|
|
}
|
|
if (size)
|
|
*size = oldlen - len;
|
|
|
|
return 0;
|
|
}
|
|
|
|
size_t
|
|
_asn1_length(const struct asn1_template *t, const void *data)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
size_t ret = 0;
|
|
|
|
t += A1_HEADER_LEN(t);
|
|
|
|
while (elements) {
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_TYPE:
|
|
case A1_OP_TYPE_EXTERN: {
|
|
const void *el = DPOC(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **pel = (void **)el;
|
|
if (*pel == NULL)
|
|
break;
|
|
el = *pel;
|
|
}
|
|
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
ret += _asn1_length(t->ptr, el);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
ret += (f->length)(el);
|
|
}
|
|
break;
|
|
}
|
|
case A1_OP_TAG: {
|
|
size_t datalen;
|
|
const void *olddata = data;
|
|
size_t oldtaglen = 0;
|
|
|
|
data = DPO(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **el = (void **)data;
|
|
if (*el == NULL) {
|
|
data = olddata;
|
|
break;
|
|
}
|
|
data = *el;
|
|
}
|
|
|
|
if (t->tt & A1_FLAG_IMPLICIT)
|
|
oldtaglen = inner_type_taglen(t->ptr);
|
|
|
|
datalen = _asn1_length(t->ptr, data);
|
|
ret += datalen;
|
|
ret += der_length_tag(A1_TAG_TAG(t->tt));
|
|
ret += oldtaglen ? -oldtaglen : der_length_len(datalen);
|
|
data = olddata;
|
|
break;
|
|
}
|
|
case A1_OP_PARSE: {
|
|
unsigned int type = A1_PARSE_TYPE(t->tt);
|
|
const void *el = DPOC(data, t->offset);
|
|
|
|
if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
|
|
ABORT_ON_ERROR();
|
|
break;
|
|
}
|
|
ret += (asn1_template_prim[type].length)(el);
|
|
break;
|
|
}
|
|
case A1_OP_SETOF:
|
|
case A1_OP_SEQOF: {
|
|
const struct template_of *el = DPOC(data, t->offset);
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
const unsigned char *element = el->val;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < el->len; i++) {
|
|
ret += _asn1_length(t->ptr, element);
|
|
element += ellen;
|
|
}
|
|
|
|
break;
|
|
}
|
|
case A1_OP_BMEMBER: {
|
|
const struct asn1_template *bmember = t->ptr;
|
|
size_t size = bmember->offset;
|
|
size_t belements = A1_HEADER_LEN(bmember);
|
|
int rfc1510 = (bmember->tt & A1_HBF_RFC1510);
|
|
|
|
if (rfc1510) {
|
|
ret += 5;
|
|
} else {
|
|
|
|
ret += 1;
|
|
|
|
bmember += belements;
|
|
|
|
while (belements) {
|
|
if (_asn1_bmember_isset_bit(data, bmember->offset, size)) {
|
|
ret += (bmember->offset / 8) + 1;
|
|
break;
|
|
}
|
|
belements--; bmember--;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case A1_OP_CHOICE: {
|
|
const struct asn1_template *choice = t->ptr;
|
|
const unsigned int *element = DPOC(data, choice->offset);
|
|
|
|
if (*element > A1_HEADER_LEN(choice))
|
|
break;
|
|
|
|
if (*element == 0) {
|
|
ret += der_length_octet_string(DPOC(data, choice->tt));
|
|
} else {
|
|
choice += *element;
|
|
ret += _asn1_length(choice->ptr, DPOC(data, choice->offset));
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
ABORT_ON_ERROR();
|
|
break;
|
|
}
|
|
elements--;
|
|
t--;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
void
|
|
_asn1_free(const struct asn1_template *t, void *data)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
|
|
if (t->tt & A1_HF_PRESERVE)
|
|
der_free_octet_string(data);
|
|
|
|
t++;
|
|
|
|
while (elements) {
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_TYPE:
|
|
case A1_OP_TYPE_EXTERN: {
|
|
void *el = DPO(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **pel = (void **)el;
|
|
if (*pel == NULL)
|
|
break;
|
|
el = *pel;
|
|
}
|
|
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
_asn1_free(t->ptr, el);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
(f->release)(el);
|
|
}
|
|
if (t->tt & A1_FLAG_OPTIONAL)
|
|
free(el);
|
|
|
|
break;
|
|
}
|
|
case A1_OP_PARSE: {
|
|
unsigned int type = A1_PARSE_TYPE(t->tt);
|
|
void *el = DPO(data, t->offset);
|
|
|
|
if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
|
|
ABORT_ON_ERROR();
|
|
break;
|
|
}
|
|
(asn1_template_prim[type].release)(el);
|
|
break;
|
|
}
|
|
case A1_OP_TAG: {
|
|
void *el = DPO(data, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **pel = (void **)el;
|
|
if (*pel == NULL)
|
|
break;
|
|
el = *pel;
|
|
}
|
|
|
|
_asn1_free(t->ptr, el);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL)
|
|
free(el);
|
|
|
|
break;
|
|
}
|
|
case A1_OP_SETOF:
|
|
case A1_OP_SEQOF: {
|
|
struct template_of *el = DPO(data, t->offset);
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
unsigned char *element = el->val;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < el->len; i++) {
|
|
_asn1_free(t->ptr, element);
|
|
element += ellen;
|
|
}
|
|
free(el->val);
|
|
el->val = NULL;
|
|
el->len = 0;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_BMEMBER:
|
|
break;
|
|
case A1_OP_CHOICE: {
|
|
const struct asn1_template *choice = t->ptr;
|
|
const unsigned int *element = DPOC(data, choice->offset);
|
|
|
|
if (*element > A1_HEADER_LEN(choice))
|
|
break;
|
|
|
|
if (*element == 0) {
|
|
der_free_octet_string(DPO(data, choice->tt));
|
|
} else {
|
|
choice += *element;
|
|
_asn1_free(choice->ptr, DPO(data, choice->offset));
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
ABORT_ON_ERROR();
|
|
break;
|
|
}
|
|
t++;
|
|
elements--;
|
|
}
|
|
}
|
|
|
|
int
|
|
_asn1_copy(const struct asn1_template *t, const void *from, void *to)
|
|
{
|
|
size_t elements = A1_HEADER_LEN(t);
|
|
int ret = 0;
|
|
int preserve = (t->tt & A1_HF_PRESERVE);
|
|
|
|
t++;
|
|
|
|
if (preserve) {
|
|
ret = der_copy_octet_string(from, to);
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
|
|
while (elements) {
|
|
switch (t->tt & A1_OP_MASK) {
|
|
case A1_OP_TYPE:
|
|
case A1_OP_TYPE_EXTERN: {
|
|
const void *fel = DPOC(from, t->offset);
|
|
void *tel = DPO(to, t->offset);
|
|
void **ptel = (void **)tel;
|
|
size_t size;
|
|
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
size = _asn1_sizeofType(t->ptr);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
size = f->size;
|
|
}
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **pfel = (void **)fel;
|
|
if (*pfel == NULL)
|
|
break;
|
|
fel = *pfel;
|
|
|
|
tel = *ptel = calloc(1, size);
|
|
if (tel == NULL)
|
|
return ENOMEM;
|
|
}
|
|
|
|
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
|
|
ret = _asn1_copy(t->ptr, fel, tel);
|
|
} else {
|
|
const struct asn1_type_func *f = t->ptr;
|
|
ret = (f->copy)(fel, tel);
|
|
}
|
|
|
|
if (ret) {
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
free(*ptel);
|
|
*ptel = NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
break;
|
|
}
|
|
case A1_OP_PARSE: {
|
|
unsigned int type = A1_PARSE_TYPE(t->tt);
|
|
const void *fel = DPOC(from, t->offset);
|
|
void *tel = DPO(to, t->offset);
|
|
|
|
if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
|
|
ABORT_ON_ERROR();
|
|
return ASN1_PARSE_ERROR;
|
|
}
|
|
ret = (asn1_template_prim[type].copy)(fel, tel);
|
|
if (ret)
|
|
return ret;
|
|
break;
|
|
}
|
|
case A1_OP_TAG: {
|
|
const void *oldfrom = from;
|
|
void *oldto = to;
|
|
void **tel = NULL;
|
|
|
|
from = DPOC(from, t->offset);
|
|
to = DPO(to, t->offset);
|
|
|
|
if (t->tt & A1_FLAG_OPTIONAL) {
|
|
void **fel = (void **)from;
|
|
tel = (void **)to;
|
|
if (*fel == NULL) {
|
|
from = oldfrom;
|
|
to = oldto;
|
|
break;
|
|
}
|
|
from = *fel;
|
|
|
|
to = *tel = calloc(1, _asn1_sizeofType(t->ptr));
|
|
if (to == NULL)
|
|
return ENOMEM;
|
|
}
|
|
|
|
ret = _asn1_copy(t->ptr, from, to);
|
|
if (ret) {
|
|
if (tel) {
|
|
free(*tel);
|
|
*tel = NULL;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
from = oldfrom;
|
|
to = oldto;
|
|
|
|
break;
|
|
}
|
|
case A1_OP_SETOF:
|
|
case A1_OP_SEQOF: {
|
|
const struct template_of *fel = DPOC(from, t->offset);
|
|
struct template_of *tel = DPO(to, t->offset);
|
|
size_t ellen = _asn1_sizeofType(t->ptr);
|
|
unsigned int i;
|
|
|
|
tel->val = calloc(fel->len, ellen);
|
|
if (tel->val == NULL)
|
|
return ENOMEM;
|
|
|
|
tel->len = fel->len;
|
|
|
|
for (i = 0; i < fel->len; i++) {
|
|
ret = _asn1_copy(t->ptr,
|
|
DPOC(fel->val, (i * ellen)),
|
|
DPO(tel->val, (i *ellen)));
|
|
if (ret)
|
|
return ret;
|
|
}
|
|
break;
|
|
}
|
|
case A1_OP_BMEMBER: {
|
|
const struct asn1_template *bmember = t->ptr;
|
|
size_t size = bmember->offset;
|
|
memcpy(to, from, size);
|
|
break;
|
|
}
|
|
case A1_OP_CHOICE: {
|
|
const struct asn1_template *choice = t->ptr;
|
|
const unsigned int *felement = DPOC(from, choice->offset);
|
|
unsigned int *telement = DPO(to, choice->offset);
|
|
|
|
if (*felement > A1_HEADER_LEN(choice))
|
|
return ASN1_PARSE_ERROR;
|
|
|
|
*telement = *felement;
|
|
|
|
if (*felement == 0) {
|
|
ret = der_copy_octet_string(DPOC(from, choice->tt), DPO(to, choice->tt));
|
|
} else {
|
|
choice += *felement;
|
|
ret = _asn1_copy(choice->ptr,
|
|
DPOC(from, choice->offset),
|
|
DPO(to, choice->offset));
|
|
}
|
|
if (ret)
|
|
return ret;
|
|
break;
|
|
}
|
|
default:
|
|
ABORT_ON_ERROR();
|
|
break;
|
|
}
|
|
t++;
|
|
elements--;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
_asn1_decode_top(const struct asn1_template *t, unsigned flags, const unsigned char *p, size_t len, void *data, size_t *size)
|
|
{
|
|
int ret;
|
|
memset(data, 0, t->offset);
|
|
ret = _asn1_decode(t, flags, p, len, data, size);
|
|
if (ret)
|
|
_asn1_free_top(t, data);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int
|
|
_asn1_copy_top(const struct asn1_template *t, const void *from, void *to)
|
|
{
|
|
int ret;
|
|
memset(to, 0, t->offset);
|
|
ret = _asn1_copy(t, from, to);
|
|
if (ret)
|
|
_asn1_free_top(t, to);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void
|
|
_asn1_free_top(const struct asn1_template *t, void *data)
|
|
{
|
|
_asn1_free(t, data);
|
|
memset(data, 0, t->offset);
|
|
}
|