blob: cfd467234bf00f4a91104f01b435e6cb7ff55c11 [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
Lev Walkin07a50d82017-09-15 23:26:11 -070043 /* Verify that we won't overflow. */
44 if(!(len >> ((8 * sizeof(len)) - (8+1)))) {
45 len = (len << 8) | *buf;
46 } else {
47 /* Too large length value. */
Lev Walkinf15320b2004-06-03 03:38:44 +000048 return -1;
49 }
50 }
51
52 if(oct == 0) {
Lev Walkin93682632017-09-15 23:32:36 -070053 if(len < 0 || len > RSSIZE_MAX) {
Lev Walkin07a50d82017-09-15 23:26:11 -070054 /* Length value out of sane range. */
Lev Walkinc4e4b682004-09-29 13:25:23 +000055 return -1;
56 }
57
Lev Walkinf15320b2004-06-03 03:38:44 +000058 *len_r = len;
59 return skipped;
60 }
61
62 return 0; /* Want more */
63 }
64
65}
66
67ssize_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070068ber_skip_length(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin8c3b8542005-03-10 18:52:02 +000069 int _is_constructed, const void *ptr, size_t size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000070 ber_tlv_len_t vlen; /* Length of V in TLV */
71 ssize_t tl; /* Length of L in TLV */
72 ssize_t ll; /* Length of L in TLV */
Lev Walkind9bd7752004-06-05 08:17:50 +000073 size_t skip;
Lev Walkinf15320b2004-06-03 03:38:44 +000074
75 /*
Lev Walkinbaaa24f2004-09-29 14:19:14 +000076 * Make sure we didn't exceed the maximum stack size.
77 */
Lev Walkin7c1dc052016-03-14 03:08:15 -070078 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000079 return -1;
Lev Walkinbaaa24f2004-09-29 14:19:14 +000080
81 /*
Lev Walkinf15320b2004-06-03 03:38:44 +000082 * Determine the size of L in TLV.
83 */
84 ll = ber_fetch_length(_is_constructed, ptr, size, &vlen);
85 if(ll <= 0) return ll;
86
87 /*
88 * Definite length.
89 */
90 if(vlen >= 0) {
91 skip = ll + vlen;
92 if(skip > size)
93 return 0; /* Want more */
94 return skip;
95 }
96
97 /*
98 * Indefinite length!
99 */
100 ASN_DEBUG("Skipping indefinite length");
Lev Walkin8c3b8542005-03-10 18:52:02 +0000101 for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 ber_tlv_tag_t tag;
103
104 /* Fetch the tag */
105 tl = ber_fetch_tag(ptr, size, &tag);
106 if(tl <= 0) return tl;
107
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000108 ll = ber_skip_length(opt_codec_ctx,
109 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000110 ((const char *)ptr) + tl, size - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 if(ll <= 0) return ll;
112
113 skip += tl + ll;
114
115 /*
116 * This may be the end of the indefinite length structure,
117 * two consecutive 0 octets.
118 * Check if it is true.
119 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000120 if(((const uint8_t *)ptr)[0] == 0
121 && ((const uint8_t *)ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 return skip;
123
Lev Walkin8c3b8542005-03-10 18:52:02 +0000124 ptr = ((const char *)ptr) + tl + ll;
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 size -= tl + ll;
126 }
127
128 /* UNREACHABLE */
129}
130
Lev Walkin91b29c52004-09-24 20:59:13 +0000131size_t
Lev Walkinf15320b2004-06-03 03:38:44 +0000132der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) {
Lev Walkin619b6aa2004-08-19 18:10:27 +0000133 size_t required_size; /* Size of len encoding */
Lev Walkinc2346572004-08-11 09:07:36 +0000134 uint8_t *buf = (uint8_t *)bufp;
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 uint8_t *end;
Lev Walkin38306e12017-09-17 22:33:45 -0700136 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000137
138 if(len <= 127) {
139 /* Encoded in 1 octet */
Lev Walkin4efbfb72005-02-25 14:20:30 +0000140 if(size) *buf = (uint8_t)len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 return 1;
142 }
143
144 /*
145 * Compute the size of the subsequent bytes.
146 */
Lev Walkin38306e12017-09-17 22:33:45 -0700147 for(required_size = 1, i = 8; i < 8 * (int)sizeof(len); i += 8) {
Lev Walkin619b6aa2004-08-19 18:10:27 +0000148 if(len >> i)
149 required_size++;
150 else
151 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 }
153
Lev Walkin37ec0732006-04-16 08:57:39 +0000154 if(size <= required_size)
Lev Walkin619b6aa2004-08-19 18:10:27 +0000155 return required_size + 1;
156
Lev Walkin4efbfb72005-02-25 14:20:30 +0000157 *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */
Lev Walkinf15320b2004-06-03 03:38:44 +0000158
159 /*
160 * Produce the len encoding, space permitting.
161 */
Lev Walkin619b6aa2004-08-19 18:10:27 +0000162 end = buf + required_size;
163 for(i -= 8; buf < end; i -= 8, buf++)
Lev Walkin4efbfb72005-02-25 14:20:30 +0000164 *buf = (uint8_t)(len >> i);
Lev Walkinf15320b2004-06-03 03:38:44 +0000165
Lev Walkin619b6aa2004-08-19 18:10:27 +0000166 return required_size + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000167}
168