vm: fix a null dereference on out-of-memory
[minix.git] / lib / liblwip / core / snmp / asn1_dec.c
blob1d5658207a15293e8447dec0b01f3ce0ad7a33cd
1 /**
2 * @file
3 * Abstract Syntax Notation One (ISO 8824, 8825) decoding
5 * @todo not optimised (yet), favor correctness over speed, favor speed over size
6 */
8 /*
9 * Copyright (c) 2006 Axon Digital Design B.V., The Netherlands.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without modification,
13 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
26 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
34 * Author: Christiaan Simons <christiaan.simons@axon.tv>
37 #include "lwip/opt.h"
39 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
41 #include "lwip/snmp_asn1.h"
43 /**
44 * Retrieves type field from incoming pbuf chain.
46 * @param p points to a pbuf holding an ASN1 coded type field
47 * @param ofs points to the offset within the pbuf chain of the ASN1 coded type field
48 * @param type return ASN1 type
49 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
51 err_t
52 snmp_asn1_dec_type(struct pbuf *p, u16_t ofs, u8_t *type)
54 u16_t plen, base;
55 u8_t *msg_ptr;
57 plen = 0;
58 while (p != NULL)
60 base = plen;
61 plen += p->len;
62 if (ofs < plen)
64 msg_ptr = (u8_t*)p->payload;
65 msg_ptr += ofs - base;
66 *type = *msg_ptr;
67 return ERR_OK;
69 p = p->next;
71 /* p == NULL, ofs >= plen */
72 return ERR_ARG;
75 /**
76 * Decodes length field from incoming pbuf chain into host length.
78 * @param p points to a pbuf holding an ASN1 coded length
79 * @param ofs points to the offset within the pbuf chain of the ASN1 coded length
80 * @param octets_used returns number of octets used by the length code
81 * @param length return host order length, upto 64k
82 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
84 err_t
85 snmp_asn1_dec_length(struct pbuf *p, u16_t ofs, u8_t *octets_used, u16_t *length)
87 u16_t plen, base;
88 u8_t *msg_ptr;
90 plen = 0;
91 while (p != NULL)
93 base = plen;
94 plen += p->len;
95 if (ofs < plen)
97 msg_ptr = (u8_t*)p->payload;
98 msg_ptr += ofs - base;
100 if (*msg_ptr < 0x80)
102 /* primitive definite length format */
103 *octets_used = 1;
104 *length = *msg_ptr;
105 return ERR_OK;
107 else if (*msg_ptr == 0x80)
109 /* constructed indefinite length format, termination with two zero octets */
110 u8_t zeros;
111 u8_t i;
113 *length = 0;
114 zeros = 0;
115 while (zeros != 2)
117 i = 2;
118 while (i > 0)
120 i--;
121 (*length) += 1;
122 ofs += 1;
123 if (ofs >= plen)
125 /* next octet in next pbuf */
126 p = p->next;
127 if (p == NULL) { return ERR_ARG; }
128 msg_ptr = (u8_t*)p->payload;
129 plen += p->len;
131 else
133 /* next octet in same pbuf */
134 msg_ptr++;
136 if (*msg_ptr == 0)
138 zeros++;
139 if (zeros == 2)
141 /* stop while (i > 0) */
142 i = 0;
145 else
147 zeros = 0;
151 *octets_used = 1;
152 return ERR_OK;
154 else if (*msg_ptr == 0x81)
156 /* constructed definite length format, one octet */
157 ofs += 1;
158 if (ofs >= plen)
160 /* next octet in next pbuf */
161 p = p->next;
162 if (p == NULL) { return ERR_ARG; }
163 msg_ptr = (u8_t*)p->payload;
165 else
167 /* next octet in same pbuf */
168 msg_ptr++;
170 *length = *msg_ptr;
171 *octets_used = 2;
172 return ERR_OK;
174 else if (*msg_ptr == 0x82)
176 u8_t i;
178 /* constructed definite length format, two octets */
179 i = 2;
180 while (i > 0)
182 i--;
183 ofs += 1;
184 if (ofs >= plen)
186 /* next octet in next pbuf */
187 p = p->next;
188 if (p == NULL) { return ERR_ARG; }
189 msg_ptr = (u8_t*)p->payload;
190 plen += p->len;
192 else
194 /* next octet in same pbuf */
195 msg_ptr++;
197 if (i == 0)
199 /* least significant length octet */
200 *length |= *msg_ptr;
202 else
204 /* most significant length octet */
205 *length = (*msg_ptr) << 8;
208 *octets_used = 3;
209 return ERR_OK;
211 else
213 /* constructed definite length format 3..127 octets, this is too big (>64k) */
214 /** @todo: do we need to accept inefficient codings with many leading zero's? */
215 *octets_used = 1 + ((*msg_ptr) & 0x7f);
216 return ERR_ARG;
219 p = p->next;
222 /* p == NULL, ofs >= plen */
223 return ERR_ARG;
227 * Decodes positive integer (counter, gauge, timeticks) into u32_t.
229 * @param p points to a pbuf holding an ASN1 coded integer
230 * @param ofs points to the offset within the pbuf chain of the ASN1 coded integer
231 * @param len length of the coded integer field
232 * @param value return host order integer
233 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
235 * @note ASN coded integers are _always_ signed. E.g. +0xFFFF is coded
236 * as 0x00,0xFF,0xFF. Note the leading sign octet. A positive value
237 * of 0xFFFFFFFF is preceded with 0x00 and the length is 5 octets!!
239 err_t
240 snmp_asn1_dec_u32t(struct pbuf *p, u16_t ofs, u16_t len, u32_t *value)
242 u16_t plen, base;
243 u8_t *msg_ptr;
245 plen = 0;
246 while (p != NULL)
248 base = plen;
249 plen += p->len;
250 if (ofs < plen)
252 msg_ptr = (u8_t*)p->payload;
253 msg_ptr += ofs - base;
254 if ((len > 0) && (len < 6))
256 /* start from zero */
257 *value = 0;
258 if (*msg_ptr & 0x80)
260 /* negative, expecting zero sign bit! */
261 return ERR_ARG;
263 else
265 /* positive */
266 if ((len > 1) && (*msg_ptr == 0))
268 /* skip leading "sign byte" octet 0x00 */
269 len--;
270 ofs += 1;
271 if (ofs >= plen)
273 /* next octet in next pbuf */
274 p = p->next;
275 if (p == NULL) { return ERR_ARG; }
276 msg_ptr = (u8_t*)p->payload;
277 plen += p->len;
279 else
281 /* next octet in same pbuf */
282 msg_ptr++;
286 /* OR octets with value */
287 while (len > 1)
289 len--;
290 *value |= *msg_ptr;
291 *value <<= 8;
292 ofs += 1;
293 if (ofs >= plen)
295 /* next octet in next pbuf */
296 p = p->next;
297 if (p == NULL) { return ERR_ARG; }
298 msg_ptr = (u8_t*)p->payload;
299 plen += p->len;
301 else
303 /* next octet in same pbuf */
304 msg_ptr++;
307 *value |= *msg_ptr;
308 return ERR_OK;
310 else
312 return ERR_ARG;
315 p = p->next;
317 /* p == NULL, ofs >= plen */
318 return ERR_ARG;
322 * Decodes integer into s32_t.
324 * @param p points to a pbuf holding an ASN1 coded integer
325 * @param ofs points to the offset within the pbuf chain of the ASN1 coded integer
326 * @param len length of the coded integer field
327 * @param value return host order integer
328 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
330 * @note ASN coded integers are _always_ signed!
332 err_t
333 snmp_asn1_dec_s32t(struct pbuf *p, u16_t ofs, u16_t len, s32_t *value)
335 u16_t plen, base;
336 u8_t *msg_ptr;
337 #if BYTE_ORDER == LITTLE_ENDIAN
338 u8_t *lsb_ptr = (u8_t*)value;
339 #endif
340 #if BYTE_ORDER == BIG_ENDIAN
341 u8_t *lsb_ptr = (u8_t*)value + sizeof(s32_t) - 1;
342 #endif
343 u8_t sign;
345 plen = 0;
346 while (p != NULL)
348 base = plen;
349 plen += p->len;
350 if (ofs < plen)
352 msg_ptr = (u8_t*)p->payload;
353 msg_ptr += ofs - base;
354 if ((len > 0) && (len < 5))
356 if (*msg_ptr & 0x80)
358 /* negative, start from -1 */
359 *value = -1;
360 sign = 1;
362 else
364 /* positive, start from 0 */
365 *value = 0;
366 sign = 0;
368 /* OR/AND octets with value */
369 while (len > 1)
371 len--;
372 if (sign)
374 *lsb_ptr &= *msg_ptr;
375 *value <<= 8;
376 *lsb_ptr |= 255;
378 else
380 *lsb_ptr |= *msg_ptr;
381 *value <<= 8;
383 ofs += 1;
384 if (ofs >= plen)
386 /* next octet in next pbuf */
387 p = p->next;
388 if (p == NULL) { return ERR_ARG; }
389 msg_ptr = (u8_t*)p->payload;
390 plen += p->len;
392 else
394 /* next octet in same pbuf */
395 msg_ptr++;
398 if (sign)
400 *lsb_ptr &= *msg_ptr;
402 else
404 *lsb_ptr |= *msg_ptr;
406 return ERR_OK;
408 else
410 return ERR_ARG;
413 p = p->next;
415 /* p == NULL, ofs >= plen */
416 return ERR_ARG;
420 * Decodes object identifier from incoming message into array of s32_t.
422 * @param p points to a pbuf holding an ASN1 coded object identifier
423 * @param ofs points to the offset within the pbuf chain of the ASN1 coded object identifier
424 * @param len length of the coded object identifier
425 * @param oid return object identifier struct
426 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
428 err_t
429 snmp_asn1_dec_oid(struct pbuf *p, u16_t ofs, u16_t len, struct snmp_obj_id *oid)
431 u16_t plen, base;
432 u8_t *msg_ptr;
433 s32_t *oid_ptr;
435 plen = 0;
436 while (p != NULL)
438 base = plen;
439 plen += p->len;
440 if (ofs < plen)
442 msg_ptr = (u8_t*)p->payload;
443 msg_ptr += ofs - base;
445 oid->len = 0;
446 oid_ptr = &oid->id[0];
447 if (len > 0)
449 /* first compressed octet */
450 if (*msg_ptr == 0x2B)
452 /* (most) common case 1.3 (iso.org) */
453 *oid_ptr = 1;
454 oid_ptr++;
455 *oid_ptr = 3;
456 oid_ptr++;
458 else if (*msg_ptr < 40)
460 *oid_ptr = 0;
461 oid_ptr++;
462 *oid_ptr = *msg_ptr;
463 oid_ptr++;
465 else if (*msg_ptr < 80)
467 *oid_ptr = 1;
468 oid_ptr++;
469 *oid_ptr = (*msg_ptr) - 40;
470 oid_ptr++;
472 else
474 *oid_ptr = 2;
475 oid_ptr++;
476 *oid_ptr = (*msg_ptr) - 80;
477 oid_ptr++;
479 oid->len = 2;
481 else
483 /* accepting zero length identifiers e.g. for
484 getnext operation. uncommon but valid */
485 return ERR_OK;
487 len--;
488 if (len > 0)
490 ofs += 1;
491 if (ofs >= plen)
493 /* next octet in next pbuf */
494 p = p->next;
495 if (p == NULL) { return ERR_ARG; }
496 msg_ptr = (u8_t*)p->payload;
497 plen += p->len;
499 else
501 /* next octet in same pbuf */
502 msg_ptr++;
505 while ((len > 0) && (oid->len < LWIP_SNMP_OBJ_ID_LEN))
507 /* sub-identifier uses multiple octets */
508 if (*msg_ptr & 0x80)
510 s32_t sub_id = 0;
512 while ((*msg_ptr & 0x80) && (len > 1))
514 len--;
515 sub_id = (sub_id << 7) + (*msg_ptr & ~0x80);
516 ofs += 1;
517 if (ofs >= plen)
519 /* next octet in next pbuf */
520 p = p->next;
521 if (p == NULL) { return ERR_ARG; }
522 msg_ptr = (u8_t*)p->payload;
523 plen += p->len;
525 else
527 /* next octet in same pbuf */
528 msg_ptr++;
531 if (!(*msg_ptr & 0x80) && (len > 0))
533 /* last octet sub-identifier */
534 len--;
535 sub_id = (sub_id << 7) + *msg_ptr;
536 *oid_ptr = sub_id;
539 else
541 /* !(*msg_ptr & 0x80) sub-identifier uses single octet */
542 len--;
543 *oid_ptr = *msg_ptr;
545 if (len > 0)
547 /* remaining oid bytes available ... */
548 ofs += 1;
549 if (ofs >= plen)
551 /* next octet in next pbuf */
552 p = p->next;
553 if (p == NULL) { return ERR_ARG; }
554 msg_ptr = (u8_t*)p->payload;
555 plen += p->len;
557 else
559 /* next octet in same pbuf */
560 msg_ptr++;
563 oid_ptr++;
564 oid->len++;
566 if (len == 0)
568 /* len == 0, end of oid */
569 return ERR_OK;
571 else
573 /* len > 0, oid->len == LWIP_SNMP_OBJ_ID_LEN or malformed encoding */
574 return ERR_ARG;
578 p = p->next;
580 /* p == NULL, ofs >= plen */
581 return ERR_ARG;
585 * Decodes (copies) raw data (ip-addresses, octet strings, opaque encoding)
586 * from incoming message into array.
588 * @param p points to a pbuf holding an ASN1 coded raw data
589 * @param ofs points to the offset within the pbuf chain of the ASN1 coded raw data
590 * @param len length of the coded raw data (zero is valid, e.g. empty string!)
591 * @param raw_len length of the raw return value
592 * @param raw return raw bytes
593 * @return ERR_OK if successfull, ERR_ARG if we can't (or won't) decode
595 err_t
596 snmp_asn1_dec_raw(struct pbuf *p, u16_t ofs, u16_t len, u16_t raw_len, u8_t *raw)
598 u16_t plen, base;
599 u8_t *msg_ptr;
601 if (len > 0)
603 plen = 0;
604 while (p != NULL)
606 base = plen;
607 plen += p->len;
608 if (ofs < plen)
610 msg_ptr = (u8_t*)p->payload;
611 msg_ptr += ofs - base;
612 if (raw_len >= len)
614 while (len > 1)
616 /* copy len - 1 octets */
617 len--;
618 *raw = *msg_ptr;
619 raw++;
620 ofs += 1;
621 if (ofs >= plen)
623 /* next octet in next pbuf */
624 p = p->next;
625 if (p == NULL) { return ERR_ARG; }
626 msg_ptr = (u8_t*)p->payload;
627 plen += p->len;
629 else
631 /* next octet in same pbuf */
632 msg_ptr++;
635 /* copy last octet */
636 *raw = *msg_ptr;
637 return ERR_OK;
639 else
641 /* raw_len < len, not enough dst space */
642 return ERR_ARG;
645 p = p->next;
647 /* p == NULL, ofs >= plen */
648 return ERR_ARG;
650 else
652 /* len == 0, empty string */
653 return ERR_OK;
657 #endif /* LWIP_SNMP */