blob: 49343d37afac948c132b440c64916fd9daff866b [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <ber_tlv_length.h>
7#include <ber_tlv_tag.h>
8
9ssize_t
Lev Walkin8c3b8542005-03-10 18:52:02 +000010ber_fetch_length(int _is_constructed, const void *bufptr, size_t size,
Lev Walkinf15320b2004-06-03 03:38:44 +000011 ber_tlv_len_t *len_r) {
Lev Walkin8c3b8542005-03-10 18:52:02 +000012 const uint8_t *buf = (const uint8_t *)bufptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000013 unsigned oct;
14
15 if(size == 0)
16 return 0; /* Want more */
17
Lev Walkin8c3b8542005-03-10 18:52:02 +000018 oct = *(const uint8_t *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +000019 if((oct & 0x80) == 0) {
20 /*
21 * Short definite length.
22 */
Lev Walkin619b6aa2004-08-19 18:10:27 +000023 *len_r = oct; /* & 0x7F */
Lev Walkinf15320b2004-06-03 03:38:44 +000024 return 1;
25 } else {
26 ber_tlv_len_t len;
Lev Walkind9bd7752004-06-05 08:17:50 +000027 size_t skipped;
Lev Walkinf15320b2004-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;
Lev Walkin619b6aa2004-08-19 18:10:27 +000041 oct && (++skipped <= size); buf++, oct--) {
Lev Walkinf15320b2004-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) {
Lev Walkinc4e4b682004-09-29 13:25:23 +000054
55 /*
56 * Here length may be very close or equal to 2G.
Lev Walkin33700162004-10-26 09:03:31 +000057 * However, the arithmetics used in some decoders
58 * may add some (small) quantities to the length,
Lev Walkinc4e4b682004-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
Lev Walkinf15320b2004-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
Lev Walkinbaaa24f2004-09-29 14:19:14 +000077ber_skip_length(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin8c3b8542005-03-10 18:52:02 +000078 int _is_constructed, const void *ptr, size_t size) {
Lev Walkinf15320b2004-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 */
Lev Walkind9bd7752004-06-05 08:17:50 +000082 size_t skip;
Lev Walkinf15320b2004-06-03 03:38:44 +000083
84 /*
Lev Walkinbaaa24f2004-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 /*
Lev Walkinf15320b2004-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");
Lev Walkin8c3b8542005-03-10 18:52:02 +0000119 for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) {
Lev Walkinf15320b2004-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
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000126 ll = ber_skip_length(opt_codec_ctx,
127 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000128 ((const char *)ptr) + tl, size - tl);
Lev Walkinf15320b2004-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 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000138 if(((const uint8_t *)ptr)[0] == 0
139 && ((const uint8_t *)ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 return skip;
141
Lev Walkin8c3b8542005-03-10 18:52:02 +0000142 ptr = ((const char *)ptr) + tl + ll;
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 size -= tl + ll;
144 }
145
146 /* UNREACHABLE */
147}
148
Lev Walkin91b29c52004-09-24 20:59:13 +0000149size_t
Lev Walkinf15320b2004-06-03 03:38:44 +0000150der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
Lev Walkin619b6aa2004-08-19 18:10:27 +0000151 size_t required_size; /* Size of len encoding */
Lev Walkinc2346572004-08-11 09:07:36 +0000152 uint8_t *buf = (uint8_t *)bufp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 uint8_t *end;
Lev Walkin619b6aa2004-08-19 18:10:27 +0000154 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000155
156 if(len <= 127) {
157 /* Encoded in 1 octet */
Lev Walkin4efbfb72005-02-25 14:20:30 +0000158 if(size) *buf = (uint8_t)len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 return 1;
160 }
161
162 /*
163 * Compute the size of the subsequent bytes.
164 */
Lev Walkin619b6aa2004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 }
171
Lev Walkin619b6aa2004-08-19 18:10:27 +0000172 if(size < required_size)
173 return required_size + 1;
174
Lev Walkin4efbfb72005-02-25 14:20:30 +0000175 *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */
Lev Walkinf15320b2004-06-03 03:38:44 +0000176
177 /*
178 * Produce the len encoding, space permitting.
179 */
Lev Walkin619b6aa2004-08-19 18:10:27 +0000180 end = buf + required_size;
181 for(i -= 8; buf < end; i -= 8, buf++)
Lev Walkin4efbfb72005-02-25 14:20:30 +0000182 *buf = (uint8_t)(len >> i);
Lev Walkinf15320b2004-06-03 03:38:44 +0000183
Lev Walkin619b6aa2004-08-19 18:10:27 +0000184 return required_size + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000185}
186