blob: 2285677604eb169fb6fd070faa700ba2cefcd025 [file] [log] [blame]
sjana76e88d22020-03-19 13:24:39 -04001/*
2 * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_system.h>
6#include <asn_internal.h>
7#include <per_support.h>
8
9/*
10 * X.691-201508 #10.9 General rules for encoding a length determinant.
11 * Get the optionally constrained length "n" from the stream.
12 */
13ssize_t
14uper_get_length(asn_per_data_t *pd, int ebits, size_t lower_bound,
15 int *repeat) {
16 ssize_t value;
17
18 *repeat = 0;
19
20 /* #11.9.4.1 Encoding if constrained (according to effective bits) */
21 if(ebits >= 0 && ebits <= 16) {
22 value = per_get_few_bits(pd, ebits);
23 if(value >= 0) value += lower_bound;
24 return value;
25 }
26
27 value = per_get_few_bits(pd, 8);
28 if((value & 0x80) == 0) { /* #11.9.3.6 */
29 return (value & 0x7F);
30 } else if((value & 0x40) == 0) { /* #11.9.3.7 */
31 /* bit 8 ... set to 1 and bit 7 ... set to zero */
32 value = ((value & 0x3f) << 8) | per_get_few_bits(pd, 8);
33 return value; /* potential -1 from per_get_few_bits passes through. */
34 } else if(value < 0) {
35 ASN_DEBUG("END of stream reached for PER");
36 return -1;
37 }
38 value &= 0x3f; /* this is "m" from X.691, #11.9.3.8 */
39 if(value < 1 || value > 4) {
40 return -1; /* Prohibited by #11.9.3.8 */
41 }
42 *repeat = 1;
43 return (16384 * value);
44}
45
46/*
47 * Get the normally small length "n".
48 * This procedure used to decode length of extensions bit-maps
49 * for SET and SEQUENCE types.
50 */
51ssize_t
52uper_get_nslength(asn_per_data_t *pd) {
53 ssize_t length;
54
55 ASN_DEBUG("Getting normally small length");
56
57 if(per_get_few_bits(pd, 1) == 0) {
58 length = per_get_few_bits(pd, 6) + 1;
59 if(length <= 0) return -1;
60 ASN_DEBUG("l=%d", (int)length);
61 return length;
62 } else {
63 int repeat;
64 length = uper_get_length(pd, -1, 0, &repeat);
65 if(length >= 0 && !repeat) return length;
66 return -1; /* Error, or do not support >16K extensions */
67 }
68}
69
70/*
71 * Get the normally small non-negative whole number.
72 * X.691, #10.6
73 */
74ssize_t
75uper_get_nsnnwn(asn_per_data_t *pd) {
76 ssize_t value;
77
78 value = per_get_few_bits(pd, 7);
79 if(value & 64) { /* implicit (value < 0) */
80 value &= 63;
81 value <<= 2;
82 value |= per_get_few_bits(pd, 2);
83 if(value & 128) /* implicit (value < 0) */
84 return -1;
85 if(value == 0)
86 return 0;
87 if(value >= 3)
88 return -1;
89 value = per_get_few_bits(pd, 8 * value);
90 return value;
91 }
92
93 return value;
94}
95
96/*
97 * X.691-11/2008, #11.6
98 * Encoding of a normally small non-negative whole number
99 */
100int
101uper_put_nsnnwn(asn_per_outp_t *po, int n) {
102 int bytes;
103
104 if(n <= 63) {
105 if(n < 0) return -1;
106 return per_put_few_bits(po, n, 7);
107 }
108 if(n < 256)
109 bytes = 1;
110 else if(n < 65536)
111 bytes = 2;
112 else if(n < 256 * 65536)
113 bytes = 3;
114 else
115 return -1; /* This is not a "normally small" value */
116 if(per_put_few_bits(po, bytes, 8))
117 return -1;
118
119 return per_put_few_bits(po, n, 8 * bytes);
120}
121
122
123/* X.691-2008/11, #11.5.6 -> #11.3 */
124int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *out_value, int nbits) {
125 unsigned long lhalf; /* Lower half of the number*/
126 long half;
127
128 if(nbits <= 31) {
129 half = per_get_few_bits(pd, nbits);
130 if(half < 0) return -1;
131 *out_value = half;
132 return 0;
133 }
134
135 if((size_t)nbits > 8 * sizeof(*out_value))
136 return -1; /* RANGE */
137
138 half = per_get_few_bits(pd, 31);
139 if(half < 0) return -1;
140
141 if(uper_get_constrained_whole_number(pd, &lhalf, nbits - 31))
142 return -1;
143
144 *out_value = ((unsigned long)half << (nbits - 31)) | lhalf;
145 return 0;
146}
147
148
149/* X.691-2008/11, #11.5.6 -> #11.3 */
150int
151uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v,
152 int nbits) {
153 if(nbits <= 31) {
154 return per_put_few_bits(po, v, nbits);
155 } else {
156 /* Put higher portion first, followed by lower 31-bit */
157 if(uper_put_constrained_whole_number_u(po, v >> 31, nbits - 31))
158 return -1;
159 return per_put_few_bits(po, v, 31);
160 }
161}
162
163/*
164 * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
165 * Put the length "n" (or part of it) into the stream.
166 */
167ssize_t
168uper_put_length(asn_per_outp_t *po, size_t length, int *need_eom) {
169 int dummy = 0;
170 if(!need_eom) need_eom = &dummy;
171
172 if(length <= 127) { /* #11.9.3.6 */
173 *need_eom = 0;
174 return per_put_few_bits(po, length, 8)
175 ? -1 : (ssize_t)length;
176 } else if(length < 16384) { /* #10.9.3.7 */
177 *need_eom = 0;
178 return per_put_few_bits(po, length|0x8000, 16)
179 ? -1 : (ssize_t)length;
180 }
181
182 *need_eom = 0 == (length & 16383);
183 length >>= 14;
184 if(length > 4) {
185 *need_eom = 0;
186 length = 4;
187 }
188
189 return per_put_few_bits(po, 0xC0 | length, 8)
190 ? -1 : (ssize_t)(length << 14);
191
192}
193
194
195/*
196 * Put the normally small length "n" into the stream.
197 * This procedure used to encode length of extensions bit-maps
198 * for SET and SEQUENCE types.
199 */
200int
201uper_put_nslength(asn_per_outp_t *po, size_t length) {
202 if(length <= 64) {
203 /* #11.9.3.4 */
204 if(length == 0) return -1;
205 return per_put_few_bits(po, length - 1, 7) ? -1 : 0;
206 } else {
207 int need_eom = 0;
208 if(uper_put_length(po, length, &need_eom) != (ssize_t)length
209 || need_eom) {
210 /* This might happen in case of >16K extensions */
211 return -1;
212 }
213 }
214
215 return 0;
216}
217
218static int
219per__long_range(long lb, long ub, unsigned long *range_r) {
220 unsigned long bounds_range;
221 if((ub < 0) == (lb < 0)) {
222 bounds_range = ub - lb;
223 } else if(lb < 0) {
224 assert(ub >= 0);
225 bounds_range = 1 + ((unsigned long)ub + (unsigned long)-(lb + 1));
226 } else {
227 assert(!"Unreachable");
228 return -1;
229 }
230 *range_r = bounds_range;
231 return 0;
232}
233
234int
235per_long_range_rebase(long v, long lb, long ub, unsigned long *output) {
236 unsigned long range;
237
238 assert(lb <= ub);
239
240 if(v < lb || v > ub || per__long_range(lb, ub, &range) < 0) {
241 /* Range error. */
242 return -1;
243 }
244
245 /*
246 * Fundamentally what we're doing is returning (v-lb).
247 * However, this triggers undefined behavior when the word width
248 * of signed (v) is the same as the size of unsigned (*output).
249 * In practice, it triggers the UndefinedSanitizer. Therefore we shall
250 * compute the ranges accurately to avoid C's undefined behavior.
251 */
252 if((v < 0) == (lb < 0)) {
253 *output = v-lb;
254 return 0;
255 } else if(v < 0) {
256 unsigned long rebased = 1 + (unsigned long)-(v+1) + (unsigned long)lb;
257 assert(rebased <= range); /* By construction */
258 *output = rebased;
259 return 0;
260 } else if(lb < 0) {
261 unsigned long rebased = 1 + (unsigned long)-(lb+1) + (unsigned long)v;
262 assert(rebased <= range); /* By construction */
263 *output = rebased;
264 return 0;
265 } else {
266 assert(!"Unreachable");
267 return -1;
268 }
269}
270
271int
272per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp) {
273 unsigned long range;
274
275 if(per__long_range(lb, ub, &range) != 0) {
276 return -1;
277 }
278
279 if(inp > range) {
280 /*
281 * We can encode something in the given number of bits that technically
282 * exceeds the range. This is an avenue for security errors,
283 * so we don't allow that.
284 */
285 return -1;
286 }
287
288 if(inp <= LONG_MAX) {
289 *outp = (long)inp + lb;
290 } else {
291 *outp = (lb + LONG_MAX + 1) + (long)((inp - LONG_MAX) - 1);
292 }
293
294 return 0;
295}
296
297int32_t
298aper_get_align(asn_per_data_t *pd) {
299
300 if(pd->nboff & 0x7) {
301 ASN_DEBUG("Aligning %ld bits", 8 - ((unsigned long)pd->nboff & 0x7));
302 return per_get_few_bits(pd, 8 - (pd->nboff & 0x7));
303 }
304 return 0;
305}
306
307ssize_t
308aper_get_length(asn_per_data_t *pd, int range, int ebits, int *repeat) {
309 ssize_t value;
310
311 *repeat = 0;
312
313 if (range <= 65536 && range >= 0)
314 return aper_get_nsnnwn(pd, range);
315
316 if (aper_get_align(pd) < 0)
317 return -1;
318
319 if(ebits >= 0) return per_get_few_bits(pd, ebits);
320
321 value = per_get_few_bits(pd, 8);
322 if(value < 0) return -1;
323 if((value & 128) == 0) /* #10.9.3.6 */
324 return (value & 0x7F);
325 if((value & 64) == 0) { /* #10.9.3.7 */
326 value = ((value & 63) << 8) | per_get_few_bits(pd, 8);
327 if(value < 0) return -1;
328 return value;
329 }
330 value &= 63; /* this is "m" from X.691, #10.9.3.8 */
331 if(value < 1 || value > 4)
332 return -1;
333 *repeat = 1;
334 return (16384 * value);
335}
336
337ssize_t
338aper_get_nslength(asn_per_data_t *pd) {
339 ssize_t length;
340
341 ASN_DEBUG("Getting normally small length");
342
343 if(per_get_few_bits(pd, 1) == 0) {
344 length = per_get_few_bits(pd, 6) + 1;
345 if(length <= 0) return -1;
346 ASN_DEBUG("l=%ld", length);
347 return length;
348 } else {
349 int repeat;
350 length = aper_get_length(pd, -1, -1, &repeat);
351 if(length >= 0 && !repeat) return length;
352 return -1; /* Error, or do not support >16K extensions */
353 }
354}
355
356ssize_t
357aper_get_nsnnwn(asn_per_data_t *pd, int range) {
358 ssize_t value;
359 int bytes = 0;
360
361 ASN_DEBUG("getting nsnnwn with range %d", range);
362
363 if(range <= 255) {
364 int i;
365
366 if (range < 0) return -1;
367 /* 1 -> 8 bits */
368 for (i = 1; i <= 8; i++) {
369 int upper = 1 << i;
370 if (upper >= range)
371 break;
372 }
373 value = per_get_few_bits(pd, i);
374 return value;
375 } else if (range == 256){
376 /* 1 byte */
377 bytes = 1;
378 } else if (range <= 65536) {
379 /* 2 bytes */
380 bytes = 2;
381 } else {
382 return -1;
383 }
384 if (aper_get_align(pd) < 0)
385 return -1;
386 value = per_get_few_bits(pd, 8 * bytes);
387 return value;
388}
389
390int aper_put_align(asn_per_outp_t *po) {
391
392 if(po->nboff & 0x7) {
393 ASN_DEBUG("Aligning %ld bits", 8 - ((unsigned long)po->nboff & 0x7));
394 if(per_put_few_bits(po, 0x00, (8 - (po->nboff & 0x7))))
395 return -1;
396 }
397 return 0;
398}
399
400ssize_t
401aper_put_length(asn_per_outp_t *po, int range, size_t length) {
402
403 ASN_DEBUG("APER put length %zu with range %d", length, range);
404
405 /* 10.9 X.691 Note 2 */
406 if (range <= 65536 && range >= 0)
407 return aper_put_nsnnwn(po, range, length);
408
409 if (aper_put_align(po) < 0)
410 return -1;
411
412 if(length <= 127) /* #10.9.3.6 */{
413 return per_put_few_bits(po, length, 8)
414 ? -1 : (ssize_t)length;
415 }
416 else if(length < 16384) /* #10.9.3.7 */
417 return per_put_few_bits(po, length|0x8000, 16)
418 ? -1 : (ssize_t)length;
419
420 length >>= 14;
421 if(length > 4) length = 4;
422
423 return per_put_few_bits(po, 0xC0 | length, 8)
424 ? -1 : (ssize_t)(length << 14);
425}
426
427
428int
429aper_put_nslength(asn_per_outp_t *po, size_t length) {
430
431 if(length <= 64) {
432 /* #10.9.3.4 */
433 if(length == 0) return -1;
434 return per_put_few_bits(po, length-1, 7) ? -1 : 0;
435 } else {
436 if(aper_put_length(po, -1, length) != (ssize_t)length) {
437 /* This might happen in case of >16K extensions */
438 return -1;
439 }
440 }
441
442 return 0;
443}
444
445int
446aper_put_nsnnwn(asn_per_outp_t *po, int range, int number) {
447 int bytes;
448
449 ASN_DEBUG("aper put nsnnwn %d with range %d", number, range);
450 /* 10.5.7.1 X.691 */
451 if(range < 0) {
452 int i;
453 for (i = 1; ; i++) {
454 int bits = 1 << (8 * i);
455 if (number <= bits)
456 break;
457 }
458 bytes = i;
459 assert(i <= 4);
460 }
461 if(range <= 255) {
462 int i;
463 for (i = 1; i <= 8; i++) {
464 int bits = 1 << i;
465 if (range <= bits)
466 break;
467 }
468 return per_put_few_bits(po, number, i);
469 } else if(range == 256) {
470 bytes = 1;
471 } else if(range <= 65536) {
472 bytes = 2;
473 } else { /* Ranges > 64K */
474 int i;
475 for (i = 1; ; i++) {
476 int bits = 1 << (8 * i);
477 if (range <= bits)
478 break;
479 }
480 assert(i <= 4);
481 bytes = i;
482 }
483 if(aper_put_align(po) < 0) /* Aligning on octet */
484 return -1;
485/* if(per_put_few_bits(po, bytes, 8))
486 return -1;
487*/
488 return per_put_few_bits(po, number, 8 * bytes);
489}