ber_decoder.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*-
  2. * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
  3. * Redistribution and modifications are permitted subject to BSD license.
  4. */
  5. #include <asn_internal.h>
  6. #undef ADVANCE
  7. #define ADVANCE(num_bytes) do { \
  8. size_t num = num_bytes; \
  9. ptr = ((const char *)ptr) + num; \
  10. size -= num; \
  11. consumed_myself += num; \
  12. } while(0)
  13. #undef RETURN
  14. #define RETURN(_code) do { \
  15. asn_dec_rval_t rval; \
  16. rval.code = _code; \
  17. if(opt_ctx) opt_ctx->step = step; /* Save context */ \
  18. if(_code == RC_OK || opt_ctx) \
  19. rval.consumed = consumed_myself; \
  20. else \
  21. rval.consumed = 0; /* Context-free */ \
  22. return rval; \
  23. } while(0)
  24. /*
  25. * The BER decoder of any type.
  26. */
  27. asn_dec_rval_t
  28. ber_decode(const asn_codec_ctx_t *opt_codec_ctx,
  29. const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,
  30. const void *ptr, size_t size) {
  31. asn_codec_ctx_t s_codec_ctx;
  32. /*
  33. * Stack checker requires that the codec context
  34. * must be allocated on the stack.
  35. */
  36. if(opt_codec_ctx) {
  37. if(opt_codec_ctx->max_stack_size) {
  38. s_codec_ctx = *opt_codec_ctx;
  39. opt_codec_ctx = &s_codec_ctx;
  40. }
  41. } else {
  42. /* If context is not given, be security-conscious anyway */
  43. memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
  44. s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
  45. opt_codec_ctx = &s_codec_ctx;
  46. }
  47. /*
  48. * Invoke type-specific decoder.
  49. */
  50. return type_descriptor->op->ber_decoder(opt_codec_ctx, type_descriptor,
  51. struct_ptr, /* Pointer to the destination structure */
  52. ptr, size, /* Buffer and its size */
  53. 0 /* Default tag mode is 0 */
  54. );
  55. }
  56. /*
  57. * Check the set of <TL<TL<TL...>>> tags matches the definition.
  58. */
  59. asn_dec_rval_t
  60. ber_check_tags(const asn_codec_ctx_t *opt_codec_ctx,
  61. const asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,
  62. const void *ptr, size_t size, int tag_mode, int last_tag_form,
  63. ber_tlv_len_t *last_length, int *opt_tlv_form) {
  64. ssize_t consumed_myself = 0;
  65. ssize_t tag_len;
  66. ssize_t len_len;
  67. ber_tlv_tag_t tlv_tag;
  68. ber_tlv_len_t tlv_len;
  69. ber_tlv_len_t limit_len = -1;
  70. int expect_00_terminators = 0;
  71. int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */
  72. int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */
  73. int tagno;
  74. /*
  75. * Make sure we didn't exceed the maximum stack size.
  76. */
  77. if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
  78. RETURN(RC_FAIL);
  79. /*
  80. * So what does all this implicit skip stuff mean?
  81. * Imagine two types,
  82. * A ::= [5] IMPLICIT T
  83. * B ::= [2] EXPLICIT T
  84. * Where T is defined as
  85. * T ::= [4] IMPLICIT SEQUENCE { ... }
  86. *
  87. * Let's say, we are starting to decode type A, given the
  88. * following TLV stream: <5> <0>. What does this mean?
  89. * It means that the type A contains type T which is,
  90. * in turn, empty.
  91. * Remember though, that we are still in A. We cannot
  92. * just pass control to the type T decoder. Why? Because
  93. * the type T decoder expects <4> <0>, not <5> <0>.
  94. * So, we must make sure we are going to receive <5> while
  95. * still in A, then pass control to the T decoder, indicating
  96. * that the tag <4> was implicitly skipped. The decoder of T
  97. * hence will be prepared to treat <4> as valid tag, and decode
  98. * it appropriately.
  99. */
  100. tagno = step /* Continuing where left previously */
  101. + (tag_mode==1?-1:0)
  102. ;
  103. ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
  104. td->name, (long)size, tag_mode, step, tagno);
  105. /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */
  106. if(tag_mode == 0 && tagno == (int)td->tags_count) {
  107. /*
  108. * This must be the _untagged_ ANY type,
  109. * which outermost tag isn't known in advance.
  110. * Fetch the tag and length separately.
  111. */
  112. tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
  113. switch(tag_len) {
  114. case -1: RETURN(RC_FAIL);
  115. case 0: RETURN(RC_WMORE);
  116. }
  117. tlv_constr = BER_TLV_CONSTRUCTED(ptr);
  118. len_len = ber_fetch_length(tlv_constr,
  119. (const char *)ptr + tag_len, size - tag_len, &tlv_len);
  120. switch(len_len) {
  121. case -1: RETURN(RC_FAIL);
  122. case 0: RETURN(RC_WMORE);
  123. }
  124. ASN_DEBUG("Advancing %ld in ANY case",
  125. (long)(tag_len + len_len));
  126. ADVANCE(tag_len + len_len);
  127. } else {
  128. assert(tagno < (int)td->tags_count); /* At least one loop */
  129. }
  130. for((void)tagno; tagno < (int)td->tags_count; tagno++, step++) {
  131. /*
  132. * Fetch and process T from TLV.
  133. */
  134. tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
  135. ASN_DEBUG("Fetching tag from {%p,%ld}: "
  136. "len %ld, step %d, tagno %d got %s",
  137. ptr, (long)size,
  138. (long)tag_len, step, tagno,
  139. ber_tlv_tag_string(tlv_tag));
  140. switch(tag_len) {
  141. case -1: RETURN(RC_FAIL);
  142. case 0: RETURN(RC_WMORE);
  143. }
  144. tlv_constr = BER_TLV_CONSTRUCTED(ptr);
  145. /*
  146. * If {I}, don't check anything.
  147. * If {I,B,C}, check B and C unless we're at I.
  148. */
  149. if(tag_mode != 0 && step == 0) {
  150. /*
  151. * We don't expect tag to match here.
  152. * It's just because we don't know how the tag
  153. * is supposed to look like.
  154. */
  155. } else {
  156. assert(tagno >= 0); /* Guaranteed by the code above */
  157. if(tlv_tag != td->tags[tagno]) {
  158. /*
  159. * Unexpected tag. Too bad.
  160. */
  161. ASN_DEBUG("Expected: %s, "
  162. "expectation failed (tn=%d, tm=%d)",
  163. ber_tlv_tag_string(td->tags[tagno]),
  164. tagno, tag_mode
  165. );
  166. RETURN(RC_FAIL);
  167. }
  168. }
  169. /*
  170. * Attention: if there are more tags expected,
  171. * ensure that the current tag is presented
  172. * in constructed form (it contains other tags!).
  173. * If this one is the last one, check that the tag form
  174. * matches the one given in descriptor.
  175. */
  176. if(tagno < ((int)td->tags_count - 1)) {
  177. if(tlv_constr == 0) {
  178. ASN_DEBUG("tlv_constr = %d, expfail",
  179. tlv_constr);
  180. RETURN(RC_FAIL);
  181. }
  182. } else {
  183. if(last_tag_form != tlv_constr
  184. && last_tag_form != -1) {
  185. ASN_DEBUG("last_tag_form %d != %d",
  186. last_tag_form, tlv_constr);
  187. RETURN(RC_FAIL);
  188. }
  189. }
  190. /*
  191. * Fetch and process L from TLV.
  192. */
  193. len_len = ber_fetch_length(tlv_constr,
  194. (const char *)ptr + tag_len, size - tag_len, &tlv_len);
  195. ASN_DEBUG("Fetching len = %ld", (long)len_len);
  196. switch(len_len) {
  197. case -1: RETURN(RC_FAIL);
  198. case 0: RETURN(RC_WMORE);
  199. }
  200. /*
  201. * FIXME
  202. * As of today, the chain of tags
  203. * must either contain several indefinite length TLVs,
  204. * or several definite length ones.
  205. * No mixing is allowed.
  206. */
  207. if(tlv_len == -1) {
  208. /*
  209. * Indefinite length.
  210. */
  211. if(limit_len == -1) {
  212. expect_00_terminators++;
  213. } else {
  214. ASN_DEBUG("Unexpected indefinite length "
  215. "in a chain of definite lengths");
  216. RETURN(RC_FAIL);
  217. }
  218. ADVANCE(tag_len + len_len);
  219. continue;
  220. } else {
  221. if(expect_00_terminators) {
  222. ASN_DEBUG("Unexpected definite length "
  223. "in a chain of indefinite lengths");
  224. RETURN(RC_FAIL);
  225. }
  226. }
  227. /*
  228. * Check that multiple TLVs specify ever decreasing length,
  229. * which is consistent.
  230. */
  231. if(limit_len == -1) {
  232. limit_len = tlv_len + tag_len + len_len;
  233. if(limit_len < 0) {
  234. /* Too great tlv_len value? */
  235. RETURN(RC_FAIL);
  236. }
  237. } else if(limit_len != tlv_len + tag_len + len_len) {
  238. /*
  239. * Inner TLV specifies length which is inconsistent
  240. * with the outer TLV's length value.
  241. */
  242. ASN_DEBUG("Outer TLV is %ld and inner is %ld",
  243. (long)limit_len, (long)tlv_len);
  244. RETURN(RC_FAIL);
  245. }
  246. ADVANCE(tag_len + len_len);
  247. limit_len -= (tag_len + len_len);
  248. if((ssize_t)size > limit_len) {
  249. /*
  250. * Make sure that we won't consume more bytes
  251. * from the parent frame than the inferred limit.
  252. */
  253. size = limit_len;
  254. }
  255. }
  256. if(opt_tlv_form)
  257. *opt_tlv_form = tlv_constr;
  258. if(expect_00_terminators)
  259. *last_length = -expect_00_terminators;
  260. else
  261. *last_length = tlv_len;
  262. RETURN(RC_OK);
  263. }