blob: 2baa1a10fc5dc285ddd3c052b3e7daf0642e183d [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
vlmb02dcc62005-03-10 18:52:02 +000010ber_fetch_length(int _is_constructed, const void *bufptr, size_t size,
vlmfa67ddc2004-06-03 03:38:44 +000011 ber_tlv_len_t *len_r) {
vlmb02dcc62005-03-10 18:52:02 +000012 const uint8_t *buf = (const uint8_t *)bufptr;
vlmfa67ddc2004-06-03 03:38:44 +000013 unsigned oct;
14
15 if(size == 0)
16 return 0; /* Want more */
17
vlmb02dcc62005-03-10 18:52:02 +000018 oct = *(const uint8_t *)buf;
vlmfa67ddc2004-06-03 03:38:44 +000019 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 * Here length may be very close or equal to 2G.
vlm6c593842004-10-26 09:03:31 +000056 * However, the arithmetics used in some decoders
57 * may add some (small) quantities to the length,
vlmeeb5ff92004-09-29 13:25:23 +000058 * to check the resulting value against some limits.
59 * This may result in integer wrap-around.
60 */
vlm735e4612006-07-13 09:22:34 +000061 if((len + 1024) < len - 1024) {
vlmeeb5ff92004-09-29 13:25:23 +000062 /* Too large length value */
63 return -1;
64 }
65
vlmfa67ddc2004-06-03 03:38:44 +000066 *len_r = len;
67 return skipped;
68 }
69
70 return 0; /* Want more */
71 }
72
73}
74
75ssize_t
vlm6130a2f2004-09-29 14:19:14 +000076ber_skip_length(asn_codec_ctx_t *opt_codec_ctx,
vlmb02dcc62005-03-10 18:52:02 +000077 int _is_constructed, const void *ptr, size_t size) {
vlmfa67ddc2004-06-03 03:38:44 +000078 ber_tlv_len_t vlen; /* Length of V in TLV */
79 ssize_t tl; /* Length of L in TLV */
80 ssize_t ll; /* Length of L in TLV */
vlmb42843a2004-06-05 08:17:50 +000081 size_t skip;
vlmfa67ddc2004-06-03 03:38:44 +000082
83 /*
vlm6130a2f2004-09-29 14:19:14 +000084 * Make sure we didn't exceed the maximum stack size.
85 */
vlm4d2ca122005-12-07 05:46:03 +000086 if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx))
87 return -1;
vlm6130a2f2004-09-29 14:19:14 +000088
89 /*
vlmfa67ddc2004-06-03 03:38:44 +000090 * Determine the size of L in TLV.
91 */
92 ll = ber_fetch_length(_is_constructed, ptr, size, &vlen);
93 if(ll <= 0) return ll;
94
95 /*
96 * Definite length.
97 */
98 if(vlen >= 0) {
99 skip = ll + vlen;
100 if(skip > size)
101 return 0; /* Want more */
102 return skip;
103 }
104
105 /*
106 * Indefinite length!
107 */
108 ASN_DEBUG("Skipping indefinite length");
vlmb02dcc62005-03-10 18:52:02 +0000109 for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) {
vlmfa67ddc2004-06-03 03:38:44 +0000110 ber_tlv_tag_t tag;
111
112 /* Fetch the tag */
113 tl = ber_fetch_tag(ptr, size, &tag);
114 if(tl <= 0) return tl;
115
vlm6130a2f2004-09-29 14:19:14 +0000116 ll = ber_skip_length(opt_codec_ctx,
117 BER_TLV_CONSTRUCTED(ptr),
vlmb02dcc62005-03-10 18:52:02 +0000118 ((const char *)ptr) + tl, size - tl);
vlmfa67ddc2004-06-03 03:38:44 +0000119 if(ll <= 0) return ll;
120
121 skip += tl + ll;
122
123 /*
124 * This may be the end of the indefinite length structure,
125 * two consecutive 0 octets.
126 * Check if it is true.
127 */
vlmb02dcc62005-03-10 18:52:02 +0000128 if(((const uint8_t *)ptr)[0] == 0
129 && ((const uint8_t *)ptr)[1] == 0)
vlmfa67ddc2004-06-03 03:38:44 +0000130 return skip;
131
vlmb02dcc62005-03-10 18:52:02 +0000132 ptr = ((const char *)ptr) + tl + ll;
vlmfa67ddc2004-06-03 03:38:44 +0000133 size -= tl + ll;
134 }
135
136 /* UNREACHABLE */
137}
138
vlm3326d452004-09-24 20:59:13 +0000139size_t
vlmfa67ddc2004-06-03 03:38:44 +0000140der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
vlm15f28cb2004-08-19 18:10:27 +0000141 size_t required_size; /* Size of len encoding */
vlmda674682004-08-11 09:07:36 +0000142 uint8_t *buf = (uint8_t *)bufp;
vlmfa67ddc2004-06-03 03:38:44 +0000143 uint8_t *end;
vlm15f28cb2004-08-19 18:10:27 +0000144 size_t i;
vlmfa67ddc2004-06-03 03:38:44 +0000145
146 if(len <= 127) {
147 /* Encoded in 1 octet */
vlm8a09e0f2005-02-25 14:20:30 +0000148 if(size) *buf = (uint8_t)len;
vlmfa67ddc2004-06-03 03:38:44 +0000149 return 1;
150 }
151
152 /*
153 * Compute the size of the subsequent bytes.
154 */
vlm15f28cb2004-08-19 18:10:27 +0000155 for(required_size = 1, i = 8; i < 8 * sizeof(len); i += 8) {
156 if(len >> i)
157 required_size++;
158 else
159 break;
vlmfa67ddc2004-06-03 03:38:44 +0000160 }
161
vlmb1790312006-04-16 08:57:39 +0000162 if(size <= required_size)
vlm15f28cb2004-08-19 18:10:27 +0000163 return required_size + 1;
164
vlm8a09e0f2005-02-25 14:20:30 +0000165 *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */
vlmfa67ddc2004-06-03 03:38:44 +0000166
167 /*
168 * Produce the len encoding, space permitting.
169 */
vlm15f28cb2004-08-19 18:10:27 +0000170 end = buf + required_size;
171 for(i -= 8; buf < end; i -= 8, buf++)
vlm8a09e0f2005-02-25 14:20:30 +0000172 *buf = (uint8_t)(len >> i);
vlmfa67ddc2004-06-03 03:38:44 +0000173
vlm15f28cb2004-08-19 18:10:27 +0000174 return required_size + 1;
vlmfa67ddc2004-06-03 03:38:44 +0000175}
176