blob: 4f1ea88eccc33bbb33f08b1c71eea08c0080f4f2 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <ber_tlv_length.h>
7#include <ber_tlv_tag.h>
8
9ssize_t
10ber_fetch_length(int _is_constructed, void *bufptr, size_t size,
11 ber_tlv_len_t *len_r) {
vlmda674682004-08-11 09:07:36 +000012 uint8_t *buf = (uint8_t *)bufptr;
vlmfa67ddc2004-06-03 03:38:44 +000013 unsigned oct;
14
15 if(size == 0)
16 return 0; /* Want more */
17
18 oct = *(uint8_t *)buf;
19 if((oct & 0x80) == 0) {
20 /*
21 * Short definite length.
22 */
vlm15f28cb2004-08-19 18:10:27 +000023 *len_r = oct; /* & 0x7F */
vlmfa67ddc2004-06-03 03:38:44 +000024 return 1;
25 } else {
26 ber_tlv_len_t len;
vlmb42843a2004-06-05 08:17:50 +000027 size_t skipped;
vlmfa67ddc2004-06-03 03:38:44 +000028
29 if(_is_constructed && oct == 0x80) {
30 *len_r = -1; /* Indefinite length */
31 return 1;
32 }
33
34 if(oct == 0xff) {
35 /* Reserved in standard for future use. */
36 return -1;
37 }
38
39 oct &= 0x7F; /* Leave only the 7 LS bits */
40 for(len = 0, buf++, skipped = 1;
vlm15f28cb2004-08-19 18:10:27 +000041 oct && (++skipped <= size); buf++, oct--) {
vlmfa67ddc2004-06-03 03:38:44 +000042
43 len = (len << 8) | *buf;
44 if(len < 0
45 || (len >> ((8 * sizeof(len)) - 8) && oct > 1)) {
46 /*
47 * Too large length value.
48 */
49 return -1;
50 }
51 }
52
53 if(oct == 0) {
vlmeeb5ff92004-09-29 13:25:23 +000054
55 /*
56 * Here length may be very close or equal to 2G.
vlm6c593842004-10-26 09:03:31 +000057 * However, the arithmetics used in some decoders
58 * may add some (small) quantities to the length,
vlmeeb5ff92004-09-29 13:25:23 +000059 * to check the resulting value against some limits.
60 * This may result in integer wrap-around.
61 */
62 if((len + 1024) < 0) {
63 /* Too large length value */
64 return -1;
65 }
66
vlmfa67ddc2004-06-03 03:38:44 +000067 *len_r = len;
68 return skipped;
69 }
70
71 return 0; /* Want more */
72 }
73
74}
75
76ssize_t
vlm6130a2f2004-09-29 14:19:14 +000077ber_skip_length(asn_codec_ctx_t *opt_codec_ctx,
78 int _is_constructed, void *ptr, size_t size) {
vlmfa67ddc2004-06-03 03:38:44 +000079 ber_tlv_len_t vlen; /* Length of V in TLV */
80 ssize_t tl; /* Length of L in TLV */
81 ssize_t ll; /* Length of L in TLV */
vlmb42843a2004-06-05 08:17:50 +000082 size_t skip;
vlmfa67ddc2004-06-03 03:38:44 +000083
84 /*
vlm6130a2f2004-09-29 14:19:14 +000085 * Make sure we didn't exceed the maximum stack size.
86 */
87 if(opt_codec_ctx && opt_codec_ctx->max_stack_size) {
88 ptrdiff_t usedstack = ((char *)opt_codec_ctx - (char *)&size);
89 /* double negative is required to avoid int wrap-around */
90 if(usedstack > 0) usedstack = -usedstack;
91 ASN_DEBUG("Current stack size %ld", -(long)usedstack);
92 if(usedstack < -(ptrdiff_t)opt_codec_ctx->max_stack_size) {
93 ASN_DEBUG("Stack limit %ld reached",
94 (long)opt_codec_ctx->max_stack_size);
95 return -1;
96 }
97 }
98
99 /*
vlmfa67ddc2004-06-03 03:38:44 +0000100 * Determine the size of L in TLV.
101 */
102 ll = ber_fetch_length(_is_constructed, ptr, size, &vlen);
103 if(ll <= 0) return ll;
104
105 /*
106 * Definite length.
107 */
108 if(vlen >= 0) {
109 skip = ll + vlen;
110 if(skip > size)
111 return 0; /* Want more */
112 return skip;
113 }
114
115 /*
116 * Indefinite length!
117 */
118 ASN_DEBUG("Skipping indefinite length");
vlmd86c9252004-08-25 01:34:11 +0000119 for(skip = ll, ptr = ((char *)ptr) + ll, size -= ll;;) {
vlmfa67ddc2004-06-03 03:38:44 +0000120 ber_tlv_tag_t tag;
121
122 /* Fetch the tag */
123 tl = ber_fetch_tag(ptr, size, &tag);
124 if(tl <= 0) return tl;
125
vlm6130a2f2004-09-29 14:19:14 +0000126 ll = ber_skip_length(opt_codec_ctx,
127 BER_TLV_CONSTRUCTED(ptr),
vlmd86c9252004-08-25 01:34:11 +0000128 ((char *)ptr) + tl, size - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000129 if(ll <= 0) return ll;
130
131 skip += tl + ll;
132
133 /*
134 * This may be the end of the indefinite length structure,
135 * two consecutive 0 octets.
136 * Check if it is true.
137 */
138 if(((uint8_t *)ptr)[0] == 0
139 && ((uint8_t *)ptr)[1] == 0)
140 return skip;
141
vlmd86c9252004-08-25 01:34:11 +0000142 ptr = ((char *)ptr) + tl + ll;
vlmfa67ddc2004-06-03 03:38:44 +0000143 size -= tl + ll;
144 }
145
146 /* UNREACHABLE */
147}
148
vlm3326d452004-09-24 20:59:13 +0000149size_t
vlmfa67ddc2004-06-03 03:38:44 +0000150der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
vlm15f28cb2004-08-19 18:10:27 +0000151 size_t required_size; /* Size of len encoding */
vlmda674682004-08-11 09:07:36 +0000152 uint8_t *buf = (uint8_t *)bufp;
vlmfa67ddc2004-06-03 03:38:44 +0000153 uint8_t *end;
vlm15f28cb2004-08-19 18:10:27 +0000154 size_t i;
vlmfa67ddc2004-06-03 03:38:44 +0000155
156 if(len <= 127) {
157 /* Encoded in 1 octet */
vlm8a09e0f2005-02-25 14:20:30 +0000158 if(size) *buf = (uint8_t)len;
vlmfa67ddc2004-06-03 03:38:44 +0000159 return 1;
160 }
161
162 /*
163 * Compute the size of the subsequent bytes.
164 */
vlm15f28cb2004-08-19 18:10:27 +0000165 for(required_size = 1, i = 8; i < 8 * sizeof(len); i += 8) {
166 if(len >> i)
167 required_size++;
168 else
169 break;
vlmfa67ddc2004-06-03 03:38:44 +0000170 }
171
vlm15f28cb2004-08-19 18:10:27 +0000172 if(size < required_size)
173 return required_size + 1;
174
vlm8a09e0f2005-02-25 14:20:30 +0000175 *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */
vlmfa67ddc2004-06-03 03:38:44 +0000176
177 /*
178 * Produce the len encoding, space permitting.
179 */
vlm15f28cb2004-08-19 18:10:27 +0000180 end = buf + required_size;
181 for(i -= 8; buf < end; i -= 8, buf++)
vlm8a09e0f2005-02-25 14:20:30 +0000182 *buf = (uint8_t)(len >> i);
vlmfa67ddc2004-06-03 03:38:44 +0000183
vlm15f28cb2004-08-19 18:10:27 +0000184 return required_size + 1;
vlmfa67ddc2004-06-03 03:38:44 +0000185}
186