Blame src/asn1c/ber_decoder.c

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