|
Packit Service |
466431 |
/*-
|
|
Packit Service |
466431 |
* Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
|
|
Packit Service |
466431 |
* Redistribution and modifications are permitted subject to BSD license.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
#include <asn_internal.h>
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
#undef ADVANCE
|
|
Packit Service |
466431 |
#define ADVANCE(num_bytes) do { \
|
|
Packit Service |
466431 |
size_t num = num_bytes; \
|
|
Packit Service |
466431 |
ptr = ((const char *)ptr) + num; \
|
|
Packit Service |
466431 |
size -= num; \
|
|
Packit Service |
466431 |
consumed_myself += num; \
|
|
Packit Service |
466431 |
} while(0)
|
|
Packit Service |
466431 |
#undef RETURN
|
|
Packit Service |
466431 |
#define RETURN(_code) do { \
|
|
Packit Service |
466431 |
asn_dec_rval_t rval; \
|
|
Packit Service |
466431 |
rval.code = _code; \
|
|
Packit Service |
466431 |
if(opt_ctx) opt_ctx->step = step; /* Save context */ \
|
|
Packit Service |
466431 |
if(_code == RC_OK || opt_ctx) \
|
|
Packit Service |
466431 |
rval.consumed = consumed_myself; \
|
|
Packit Service |
466431 |
else \
|
|
Packit Service |
466431 |
rval.consumed = 0; /* Context-free */ \
|
|
Packit Service |
466431 |
return rval; \
|
|
Packit Service |
466431 |
} while(0)
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* The BER decoder of any type.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
asn_dec_rval_t
|
|
Packit Service |
466431 |
ber_decode(asn_codec_ctx_t *opt_codec_ctx,
|
|
Packit Service |
466431 |
asn_TYPE_descriptor_t *type_descriptor,
|
|
Packit Service |
466431 |
void **struct_ptr, const void *ptr, size_t size) {
|
|
Packit Service |
466431 |
asn_codec_ctx_t s_codec_ctx;
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Stack checker requires that the codec context
|
|
Packit Service |
466431 |
* must be allocated on the stack.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(opt_codec_ctx) {
|
|
Packit Service |
466431 |
if(opt_codec_ctx->max_stack_size) {
|
|
Packit Service |
466431 |
s_codec_ctx = *opt_codec_ctx;
|
|
Packit Service |
466431 |
opt_codec_ctx = &s_codec_ctx;
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
} else {
|
|
Packit Service |
466431 |
/* If context is not given, be security-conscious anyway */
|
|
Packit Service |
466431 |
memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
|
|
Packit Service |
466431 |
s_codec_ctx.max_stack_size = _ASN_DEFAULT_STACK_MAX;
|
|
Packit Service |
466431 |
opt_codec_ctx = &s_codec_ctx;
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Invoke type-specific decoder.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
return type_descriptor->ber_decoder(opt_codec_ctx, type_descriptor,
|
|
Packit Service |
466431 |
struct_ptr, /* Pointer to the destination structure */
|
|
Packit Service |
466431 |
ptr, size, /* Buffer and its size */
|
|
Packit Service |
466431 |
0 /* Default tag mode is 0 */
|
|
Packit Service |
466431 |
);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Check the set of <TL<TL<TL...>>> tags matches the definition.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
asn_dec_rval_t
|
|
Packit Service |
466431 |
ber_check_tags(asn_codec_ctx_t *opt_codec_ctx,
|
|
Packit Service |
466431 |
asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,
|
|
Packit Service |
466431 |
const void *ptr, size_t size, int tag_mode, int last_tag_form,
|
|
Packit Service |
466431 |
ber_tlv_len_t *last_length, int *opt_tlv_form) {
|
|
Packit Service |
466431 |
ssize_t consumed_myself = 0;
|
|
Packit Service |
466431 |
ssize_t tag_len;
|
|
Packit Service |
466431 |
ssize_t len_len;
|
|
Packit Service |
466431 |
ber_tlv_tag_t tlv_tag;
|
|
Packit Service |
466431 |
ber_tlv_len_t tlv_len;
|
|
Packit Service |
466431 |
ber_tlv_len_t limit_len = -1;
|
|
Packit Service |
466431 |
int expect_00_terminators = 0;
|
|
Packit Service |
466431 |
int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */
|
|
Packit Service |
466431 |
int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */
|
|
Packit Service |
466431 |
int tagno;
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Make sure we didn't exceed the maximum stack size.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* So what does all this implicit skip stuff mean?
|
|
Packit Service |
466431 |
* Imagine two types,
|
|
Packit Service |
466431 |
* A ::= [5] IMPLICIT T
|
|
Packit Service |
466431 |
* B ::= [2] EXPLICIT T
|
|
Packit Service |
466431 |
* Where T is defined as
|
|
Packit Service |
466431 |
* T ::= [4] IMPLICIT SEQUENCE { ... }
|
|
Packit Service |
466431 |
*
|
|
Packit Service |
466431 |
* Let's say, we are starting to decode type A, given the
|
|
Packit Service |
466431 |
* following TLV stream: <5> <0>. What does this mean?
|
|
Packit Service |
466431 |
* It means that the type A contains type T which is,
|
|
Packit Service |
466431 |
* in turn, empty.
|
|
Packit Service |
466431 |
* Remember though, that we are still in A. We cannot
|
|
Packit Service |
466431 |
* just pass control to the type T decoder. Why? Because
|
|
Packit Service |
466431 |
* the type T decoder expects <4> <0>, not <5> <0>.
|
|
Packit Service |
466431 |
* So, we must make sure we are going to receive <5> while
|
|
Packit Service |
466431 |
* still in A, then pass control to the T decoder, indicating
|
|
Packit Service |
466431 |
* that the tag <4> was implicitly skipped. The decoder of T
|
|
Packit Service |
466431 |
* hence will be prepared to treat <4> as valid tag, and decode
|
|
Packit Service |
466431 |
* it appropriately.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
tagno = step /* Continuing where left previously */
|
|
Packit Service |
466431 |
+ (tag_mode==1?-1:0)
|
|
Packit Service |
466431 |
;
|
|
Packit Service |
466431 |
ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
|
|
Packit Service |
466431 |
td->name, (long)size, tag_mode, step, tagno);
|
|
Packit Service |
466431 |
/* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
if(tag_mode == 0 && tagno == td->tags_count) {
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* This must be the _untagged_ ANY type,
|
|
Packit Service |
466431 |
* which outermost tag isn't known in advance.
|
|
Packit Service |
466431 |
* Fetch the tag and length separately.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
|
|
Packit Service |
466431 |
switch(tag_len) {
|
|
Packit Service |
466431 |
case -1: RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
case 0: RETURN(RC_WMORE);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
tlv_constr = BER_TLV_CONSTRUCTED(ptr);
|
|
Packit Service |
466431 |
len_len = ber_fetch_length(tlv_constr,
|
|
Packit Service |
466431 |
(const char *)ptr + tag_len, size - tag_len, &tlv_len);
|
|
Packit Service |
466431 |
switch(len_len) {
|
|
Packit Service |
466431 |
case -1: RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
case 0: RETURN(RC_WMORE);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
ASN_DEBUG("Advancing %ld in ANY case",
|
|
Packit Service |
466431 |
(long)(tag_len + len_len));
|
|
Packit Service |
466431 |
ADVANCE(tag_len + len_len);
|
|
Packit Service |
466431 |
} else {
|
|
Packit Service |
466431 |
assert(tagno < td->tags_count); /* At least one loop */
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
for((void)tagno; tagno < td->tags_count; tagno++, step++) {
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Fetch and process T from TLV.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
|
|
Packit Service |
466431 |
ASN_DEBUG("Fetching tag from {%p,%ld}: "
|
|
Packit Service |
466431 |
"len %ld, step %d, tagno %d got %s",
|
|
Packit Service |
466431 |
ptr, (long)size,
|
|
Packit Service |
466431 |
(long)tag_len, step, tagno,
|
|
Packit Service |
466431 |
ber_tlv_tag_string(tlv_tag));
|
|
Packit Service |
466431 |
switch(tag_len) {
|
|
Packit Service |
466431 |
case -1: RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
case 0: RETURN(RC_WMORE);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
tlv_constr = BER_TLV_CONSTRUCTED(ptr);
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* If {I}, don't check anything.
|
|
Packit Service |
466431 |
* If {I,B,C}, check B and C unless we're at I.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(tag_mode != 0 && step == 0) {
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* We don't expect tag to match here.
|
|
Packit Service |
466431 |
* It's just because we don't know how the tag
|
|
Packit Service |
466431 |
* is supposed to look like.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
} else {
|
|
Packit Service |
466431 |
assert(tagno >= 0); /* Guaranteed by the code above */
|
|
Packit Service |
466431 |
if(tlv_tag != td->tags[tagno]) {
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Unexpected tag. Too bad.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
ASN_DEBUG("Expected: %s, "
|
|
Packit Service |
466431 |
"expectation failed (tn=%d, tm=%d)",
|
|
Packit Service |
466431 |
ber_tlv_tag_string(td->tags[tagno]),
|
|
Packit Service |
466431 |
tagno, tag_mode
|
|
Packit Service |
466431 |
);
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Attention: if there are more tags expected,
|
|
Packit Service |
466431 |
* ensure that the current tag is presented
|
|
Packit Service |
466431 |
* in constructed form (it contains other tags!).
|
|
Packit Service |
466431 |
* If this one is the last one, check that the tag form
|
|
Packit Service |
466431 |
* matches the one given in descriptor.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(tagno < (td->tags_count - 1)) {
|
|
Packit Service |
466431 |
if(tlv_constr == 0) {
|
|
Packit Service |
466431 |
ASN_DEBUG("tlv_constr = %d, expfail",
|
|
Packit Service |
466431 |
tlv_constr);
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
} else {
|
|
Packit Service |
466431 |
if(last_tag_form != tlv_constr
|
|
Packit Service |
466431 |
&& last_tag_form != -1) {
|
|
Packit Service |
466431 |
ASN_DEBUG("last_tag_form %d != %d",
|
|
Packit Service |
466431 |
last_tag_form, tlv_constr);
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Fetch and process L from TLV.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
len_len = ber_fetch_length(tlv_constr,
|
|
Packit Service |
466431 |
(const char *)ptr + tag_len, size - tag_len, &tlv_len);
|
|
Packit Service |
466431 |
ASN_DEBUG("Fetching len = %ld", (long)len_len);
|
|
Packit Service |
466431 |
switch(len_len) {
|
|
Packit Service |
466431 |
case -1: RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
case 0: RETURN(RC_WMORE);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* FIXME
|
|
Packit Service |
466431 |
* As of today, the chain of tags
|
|
Packit Service |
466431 |
* must either contain several indefinite length TLVs,
|
|
Packit Service |
466431 |
* or several definite length ones.
|
|
Packit Service |
466431 |
* No mixing is allowed.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(tlv_len == -1) {
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Indefinite length.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(limit_len == -1) {
|
|
Packit Service |
466431 |
expect_00_terminators++;
|
|
Packit Service |
466431 |
} else {
|
|
Packit Service |
466431 |
ASN_DEBUG("Unexpected indefinite length "
|
|
Packit Service |
466431 |
"in a chain of definite lengths");
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
ADVANCE(tag_len + len_len);
|
|
Packit Service |
466431 |
continue;
|
|
Packit Service |
466431 |
} else {
|
|
Packit Service |
466431 |
if(expect_00_terminators) {
|
|
Packit Service |
466431 |
ASN_DEBUG("Unexpected definite length "
|
|
Packit Service |
466431 |
"in a chain of indefinite lengths");
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Check that multiple TLVs specify ever decreasing length,
|
|
Packit Service |
466431 |
* which is consistent.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
if(limit_len == -1) {
|
|
Packit Service |
466431 |
limit_len = tlv_len + tag_len + len_len;
|
|
Packit Service |
466431 |
if(limit_len < 0) {
|
|
Packit Service |
466431 |
/* Too great tlv_len value? */
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
} else if(limit_len != tlv_len + tag_len + len_len) {
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Inner TLV specifies length which is inconsistent
|
|
Packit Service |
466431 |
* with the outer TLV's length value.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
ASN_DEBUG("Outer TLV is %ld and inner is %ld",
|
|
Packit Service |
466431 |
(long)limit_len, (long)tlv_len);
|
|
Packit Service |
466431 |
RETURN(RC_FAIL);
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
ADVANCE(tag_len + len_len);
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
limit_len -= (tag_len + len_len);
|
|
Packit Service |
466431 |
if((ssize_t)size > limit_len) {
|
|
Packit Service |
466431 |
/*
|
|
Packit Service |
466431 |
* Make sure that we won't consume more bytes
|
|
Packit Service |
466431 |
* from the parent frame than the inferred limit.
|
|
Packit Service |
466431 |
*/
|
|
Packit Service |
466431 |
size = limit_len;
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
}
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
if(opt_tlv_form)
|
|
Packit Service |
466431 |
*opt_tlv_form = tlv_constr;
|
|
Packit Service |
466431 |
if(expect_00_terminators)
|
|
Packit Service |
466431 |
*last_length = -expect_00_terminators;
|
|
Packit Service |
466431 |
else
|
|
Packit Service |
466431 |
*last_length = tlv_len;
|
|
Packit Service |
466431 |
|
|
Packit Service |
466431 |
RETURN(RC_OK);
|
|
Packit Service |
466431 |
}
|