2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: bsnmp/lib/asn1.c,v 1.29 2005/10/04 11:21:31 brandt_h Exp $
33 #include <sys/types.h>
40 #elif defined(HAVE_INTTYPES_H)
46 #if !defined(INT32_MIN)
47 #define INT32_MIN (-0x7fffffff-1)
49 #if !defined(INT32_MAX)
50 #define INT32_MAX (0x7fffffff)
52 #if !defined(UINT32_MAX)
53 #define UINT32_MAX (0xffffffff)
56 static void asn_error_func(const struct asn_buf *, const char *, ...);
58 void (*asn_error)(const struct asn_buf *, const char *, ...) = asn_error_func;
61 * Read the next header. This reads the tag (note, that only single
62 * byte tags are supported for now) and the length field. The length field
63 * is restricted to a 32-bit value.
64 * All errors of this function stop the decoding.
67 asn_get_header(struct asn_buf *b, u_char *type, asn_len_t *len)
71 if (b->asn_len == 0) {
72 asn_error(b, "no identifier for header");
73 return (ASN_ERR_EOBUF);
76 if ((*type & ASN_TYPE_MASK) > 0x30) {
77 asn_error(b, "types > 0x30 not supported (%u)",
78 *type & ASN_TYPE_MASK);
79 return (ASN_ERR_FAILED);
83 if (b->asn_len == 0) {
84 asn_error(b, "no length field");
85 return (ASN_ERR_EOBUF);
87 if (*b->asn_cptr & 0x80) {
88 length = *b->asn_cptr++ & 0x7f;
91 asn_error(b, "indefinite length not supported");
92 return (ASN_ERR_FAILED);
94 if (length > ASN_MAXLENLEN) {
95 asn_error(b, "long length too long (%u)", length);
96 return (ASN_ERR_FAILED);
98 if (length > b->asn_len) {
99 asn_error(b, "long length truncated");
100 return (ASN_ERR_EOBUF);
104 *len = (*len << 8) | *b->asn_cptr++;
108 *len = *b->asn_cptr++;
115 * Write a length field (restricted to values < 2^32-1) and return the
116 * number of bytes this field takes. If ptr is NULL, the length is computed
117 * but nothing is written. If the length would be too large return 0.
120 asn_put_len(u_char *ptr, asn_len_t len)
122 u_int lenlen, lenlen1;
125 if (len > ASN_MAXLEN) {
126 asn_error(NULL, "encoding length too long: (%u)", len);
132 *ptr++ = (u_char)len;
136 /* compute number of bytes for value (is at least 1) */
137 for (tmp = len; tmp != 0; tmp >>= 8)
140 *ptr++ = (u_char)lenlen | 0x80;
142 while (lenlen1-- > 0) {
143 ptr[lenlen1] = len & 0xff;
152 * Write a header (tag and length fields).
153 * Tags are restricted to one byte tags (value <= 0x30) and the
154 * lenght field to 16-bit. All errors stop the encoding.
157 asn_put_header(struct asn_buf *b, u_char type, asn_len_t len)
162 if ((type & ASN_TYPE_MASK) > 0x30) {
163 asn_error(NULL, "types > 0x30 not supported (%u)",
164 type & ASN_TYPE_MASK);
165 return (ASN_ERR_FAILED);
168 return (ASN_ERR_EOBUF);
170 *b->asn_ptr++ = type;
174 if ((lenlen = asn_put_len(NULL, len)) == 0)
175 return (ASN_ERR_FAILED);
176 if (b->asn_len < lenlen)
177 return (ASN_ERR_EOBUF);
179 (void)asn_put_len(b->asn_ptr, len);
180 b->asn_ptr += lenlen;
181 b->asn_len -= lenlen;
187 * This constructs a temporary sequence header with space for the maximum
188 * length field (three byte). Set the pointer that ptr points to to the
189 * start of the encoded header. This is used for a later call to
190 * asn_commit_header which will fix-up the length field and move the
191 * value if needed. All errors should stop the encoding.
193 #define TEMP_LEN (1 + ASN_MAXLENLEN + 1)
195 asn_put_temp_header(struct asn_buf *b, u_char type, u_char **ptr)
199 if (b->asn_len < TEMP_LEN)
200 return (ASN_ERR_EOBUF);
202 if ((ret = asn_put_header(b, type, ASN_MAXLEN)) == ASN_ERR_OK)
203 assert(b->asn_ptr == *ptr + TEMP_LEN);
207 asn_commit_header(struct asn_buf *b, u_char *ptr)
212 /* compute length of encoded value without header */
213 len = b->asn_ptr - (ptr + TEMP_LEN);
215 /* insert length. may not fail. */
216 lenlen = asn_put_len(ptr + 1, len);
217 if (lenlen > TEMP_LEN - 1)
218 return (ASN_ERR_FAILED);
220 if (lenlen < TEMP_LEN - 1) {
221 /* shift value down */
222 shift = (TEMP_LEN - 1) - lenlen;
223 memmove(ptr + 1 + lenlen, ptr + TEMP_LEN, len);
232 * BER integer. This may be used to get a signed 64 bit integer at maximum.
233 * The maximum length should be checked by the caller. This cannot overflow
234 * if the caller ensures that len is at maximum 8.
239 asn_get_real_integer(struct asn_buf *b, asn_len_t len, int64_t *vp)
245 if (b->asn_len < len) {
246 asn_error(b, "truncated integer");
247 return (ASN_ERR_EOBUF);
250 asn_error(b, "zero-length integer");
252 return (ASN_ERR_BADLEN);
258 ((*b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) ||
259 (*b->asn_cptr == 0xff && (b->asn_cptr[1] & 0x80) == 0x80))) {
260 asn_error(b, "non-minimal integer");
261 err = ASN_ERR_BADLEN;
264 if (*b->asn_cptr & 0x80)
269 val |= neg ? (u_char)~*b->asn_cptr : *b->asn_cptr;
274 *vp = -(int64_t)val - 1;
281 * Write a signed integer with the given type. The caller has to ensure
282 * that the actual value is ok for this type.
285 asn_put_real_integer(struct asn_buf *b, u_char type, int64_t ival)
294 /* this may fail if |INT64_MIN| > |INT64_MAX| and
295 * the value is between * INT64_MIN <= ival < -(INT64_MAX+1) */
296 val = (uint64_t)-(ival + 1);
299 val = (uint64_t)ival;
301 /* split the value into octets */
302 for (i = OCTETS - 1; i >= 0; i--) {
308 /* no leading 9 zeroes or ones */
309 for (i = 0; i < OCTETS - 1; i++)
310 if (!((buf[i] == 0xff && (buf[i + 1] & 0x80) != 0) ||
311 (buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0)))
313 if ((ret = asn_put_header(b, type, OCTETS - i)))
315 if (OCTETS - (u_int)i > b->asn_len)
316 return (ASN_ERR_EOBUF);
319 *b->asn_ptr++ = buf[i++];
328 * The same for unsigned 64-bitters. Here we have the problem, that overflow
329 * can happen, because the value maybe 9 bytes long. In this case the
330 * first byte must be 0.
333 asn_get_real_unsigned(struct asn_buf *b, asn_len_t len, uint64_t *vp)
337 if (b->asn_len < len) {
338 asn_error(b, "truncated integer");
339 return (ASN_ERR_EOBUF);
342 asn_error(b, "zero-length integer");
344 return (ASN_ERR_BADLEN);
348 if ((*b->asn_cptr & 0x80) || (len == 9 && *b->asn_cptr != 0)) {
349 /* negative integer or too larger */
350 *vp = 0xffffffffffffffffULL;
352 } else if (len > 1 &&
353 *b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) {
354 asn_error(b, "non-minimal unsigned");
355 err = ASN_ERR_BADLEN;
359 *vp = (*vp << 8) | *b->asn_cptr++;
367 * Values with the msb on need 9 octets.
370 asn_put_real_unsigned(struct asn_buf *b, u_char type, uint64_t val)
377 /* split the value into octets */
378 for (i = OCTETS - 1; i >= 0; i--) {
382 /* no leading 9 zeroes */
383 for (i = 0; i < OCTETS - 1; i++)
384 if (!(buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0))
386 if ((ret = asn_put_header(b, type, OCTETS - i)))
388 if (OCTETS - (u_int)i > b->asn_len)
389 return (ASN_ERR_EOBUF);
392 *b->asn_ptr++ = buf[i++];
400 * The ASN.1 INTEGER type is restricted to 32-bit signed by the SMI.
403 asn_get_integer_raw(struct asn_buf *b, asn_len_t len, int32_t *vp)
408 if ((ret = asn_get_real_integer(b, len, &val)) == ASN_ERR_OK) {
410 ret = ASN_ERR_BADLEN;
411 else if (val > INT32_MAX || val < INT32_MIN)
420 asn_get_integer(struct asn_buf *b, int32_t *vp)
426 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
428 if (type != ASN_TYPE_INTEGER) {
429 asn_error(b, "bad type for integer (%u)", type);
430 return (ASN_ERR_TAG);
433 return (asn_get_integer_raw(b, len, vp));
437 asn_put_integer(struct asn_buf *b, int32_t val)
439 return (asn_put_real_integer(b, ASN_TYPE_INTEGER, val));
445 * <0x04> <len> <data ...>
447 * Get an octetstring. noctets must point to the buffer size and on
448 * return will contain the size of the octetstring, regardless of the
452 asn_get_octetstring_raw(struct asn_buf *b, asn_len_t len, u_char *octets,
455 enum asn_err err = ASN_ERR_OK;
457 if (*noctets < len) {
458 asn_error(b, "octetstring truncated");
461 if (b->asn_len < len) {
462 asn_error(b, "truncatet octetstring");
463 return (ASN_ERR_EOBUF);
466 memcpy(octets, b->asn_cptr, *noctets);
468 memcpy(octets, b->asn_cptr, len);
476 asn_get_octetstring(struct asn_buf *b, u_char *octets, u_int *noctets)
482 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
484 if (type != ASN_TYPE_OCTETSTRING) {
485 asn_error(b, "bad type for octetstring (%u)", type);
486 return (ASN_ERR_TAG);
488 return (asn_get_octetstring_raw(b, len, octets, noctets));
492 asn_put_octetstring(struct asn_buf *b, const u_char *octets, u_int noctets)
496 if ((ret = asn_put_header(b, ASN_TYPE_OCTETSTRING, noctets)) != ASN_ERR_OK)
498 if (b->asn_len < noctets)
499 return (ASN_ERR_EOBUF);
501 memcpy(b->asn_ptr, octets, noctets);
502 b->asn_ptr += noctets;
503 b->asn_len -= noctets;
513 asn_get_null_raw(struct asn_buf *b, asn_len_t len)
516 if (b->asn_len < len) {
517 asn_error(b, "truncated NULL");
518 return (ASN_ERR_EOBUF);
520 asn_error(b, "bad length for NULL (%u)", len);
523 return (ASN_ERR_BADLEN);
529 asn_get_null(struct asn_buf *b)
535 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
537 if (type != ASN_TYPE_NULL) {
538 asn_error(b, "bad type for NULL (%u)", type);
539 return (ASN_ERR_TAG);
541 return (asn_get_null_raw(b, len));
545 asn_put_null(struct asn_buf *b)
547 return (asn_put_header(b, ASN_TYPE_NULL, 0));
551 asn_put_exception(struct asn_buf *b, u_int except)
553 return (asn_put_header(b, ASN_CLASS_CONTEXT | except, 0));
559 * <0x06> <len> <subid...>
562 asn_get_objid_raw(struct asn_buf *b, asn_len_t len, struct asn_oid *oid)
567 if (b->asn_len < len) {
568 asn_error(b, "truncated OBJID");
569 return (ASN_ERR_EOBUF);
573 asn_error(b, "short OBJID");
574 oid->subs[oid->len++] = 0;
575 oid->subs[oid->len++] = 0;
576 return (ASN_ERR_BADLEN);
580 if (oid->len == ASN_MAXOIDLEN) {
581 asn_error(b, "OID too long (%u)", oid->len);
584 return (ASN_ERR_BADLEN);
589 asn_error(b, "unterminated subid");
590 return (ASN_ERR_EOBUF);
592 if (subid > (ASN_MAXID >> 7)) {
593 asn_error(b, "OBID subid too larger");
596 subid = (subid << 7) | (*b->asn_cptr & 0x7f);
599 } while (*b->asn_cptr++ & 0x80);
602 oid->subs[oid->len++] = subid / 40;
603 oid->subs[oid->len++] = subid % 40;
605 oid->subs[oid->len++] = 2;
606 oid->subs[oid->len++] = subid - 80;
609 oid->subs[oid->len++] = subid;
617 asn_get_objid(struct asn_buf *b, struct asn_oid *oid)
623 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
625 if (type != ASN_TYPE_OBJID) {
626 asn_error(b, "bad type for OBJID (%u)", type);
627 return (ASN_ERR_TAG);
629 return (asn_get_objid_raw(b, len, oid));
633 asn_put_objid(struct asn_buf *b, const struct asn_oid *oid)
635 asn_subid_t first, sub;
636 enum asn_err err, err1;
643 asn_error(NULL, "short oid");
647 } else if (oid->len == 1) {
649 asn_error(b, "short oid");
650 if (oid->subs[0] > 2)
651 asn_error(NULL, "oid[0] too large (%u)", oid->subs[0]);
653 first = oid->subs[0] * 40;
656 if (oid->len > ASN_MAXOIDLEN) {
657 asn_error(NULL, "oid too long %u", oid->len);
660 if (oid->subs[0] > 2 ||
661 (oid->subs[0] < 2 && oid->subs[0] >= 40)) {
662 asn_error(NULL, "oid out of range (%u,%u)",
663 oid->subs[0], oid->subs[1]);
666 first = 40 * oid->subs[0] + oid->subs[1];
670 for (i = 1; i < oidlen; i++) {
671 sub = (i == 1) ? first : oid->subs[i];
672 if (sub > ASN_MAXID) {
673 asn_error(NULL, "oid subid too large");
676 len += (sub <= 0x7f) ? 1
677 : (sub <= 0x3fff) ? 2
678 : (sub <= 0x1fffff) ? 3
679 : (sub <= 0xfffffff) ? 4
682 if ((err1 = asn_put_header(b, ASN_TYPE_OBJID, len)) != ASN_ERR_OK)
684 if (b->asn_len < len)
685 return (ASN_ERR_EOBUF);
687 for (i = 1; i < oidlen; i++) {
688 sub = (i == 1) ? first : oid->subs[i];
692 } else if (sub <= 0x3fff) {
693 *b->asn_ptr++ = (sub >> 7) | 0x80;
694 *b->asn_ptr++ = sub & 0x7f;
696 } else if (sub <= 0x1fffff) {
697 *b->asn_ptr++ = (sub >> 14) | 0x80;
698 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
699 *b->asn_ptr++ = sub & 0x7f;
701 } else if (sub <= 0xfffffff) {
702 *b->asn_ptr++ = (sub >> 21) | 0x80;
703 *b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
704 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
705 *b->asn_ptr++ = sub & 0x7f;
708 *b->asn_ptr++ = (sub >> 28) | 0x80;
709 *b->asn_ptr++ = ((sub >> 21) & 0x7f) | 0x80;
710 *b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
711 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
712 *b->asn_ptr++ = sub & 0x7f;
721 * <0x10|0x20> <len> <data...>
724 asn_get_sequence(struct asn_buf *b, asn_len_t *len)
729 if ((err = asn_get_header(b, &type, len)) != ASN_ERR_OK)
731 if (type != (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED)) {
732 asn_error(b, "bad sequence type %u", type);
733 return (ASN_ERR_TAG);
735 if (*len > b->asn_len) {
736 asn_error(b, "truncated sequence");
737 return (ASN_ERR_EOBUF);
745 * 0x40 4 MSB 2MSB 2LSB LSB
748 asn_get_ipaddress_raw(struct asn_buf *b, asn_len_t len, u_char *addr)
752 if (b->asn_len < len) {
753 asn_error(b, "truncated ip-address");
754 return (ASN_ERR_EOBUF);
757 asn_error(b, "short length for ip-Address %u", len);
758 for (i = 0; i < len; i++)
759 *addr++ = *b->asn_cptr++;
763 return (ASN_ERR_BADLEN);
765 for (i = 0; i < 4; i++)
766 *addr++ = *b->asn_cptr++;
767 b->asn_cptr += len - 4;
773 asn_get_ipaddress(struct asn_buf *b, u_char *addr)
779 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
781 if (type != (ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS)) {
782 asn_error(b, "bad type for ip-address %u", type);
783 return (ASN_ERR_TAG);
785 return (asn_get_ipaddress_raw(b, len, addr));
789 asn_put_ipaddress(struct asn_buf *b, const u_char *addr)
793 if ((err = asn_put_header(b, ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS,
797 return (ASN_ERR_EOBUF);
799 memcpy(b->asn_ptr, addr, 4);
809 * 0x42|0x41 <len> ...
812 asn_get_uint32_raw(struct asn_buf *b, asn_len_t len, uint32_t *vp)
817 if ((err = asn_get_real_unsigned(b, len, &v)) == ASN_ERR_OK) {
819 asn_error(b, "uint32 too long %u", len);
820 err = ASN_ERR_BADLEN;
821 } else if (v > UINT32_MAX) {
822 asn_error(b, "uint32 too large %llu", v);
831 asn_put_uint32(struct asn_buf *b, u_char type, uint32_t val)
835 return (asn_put_real_unsigned(b, ASN_CLASS_APPLICATION|type, v));
843 asn_get_counter64_raw(struct asn_buf *b, asn_len_t len, uint64_t *vp)
845 return (asn_get_real_unsigned(b, len, vp));
849 asn_put_counter64(struct asn_buf *b, uint64_t val)
851 return (asn_put_real_unsigned(b,
852 ASN_CLASS_APPLICATION | ASN_APP_COUNTER64, val));
860 asn_get_timeticks(struct asn_buf *b, uint32_t *vp)
866 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
868 if (type != (ASN_CLASS_APPLICATION|ASN_APP_TIMETICKS)) {
869 asn_error(b, "bad type for timeticks %u", type);
870 return (ASN_ERR_TAG);
872 return (asn_get_uint32_raw(b, len, vp));
876 asn_put_timeticks(struct asn_buf *b, uint32_t val)
880 return (asn_put_real_unsigned(b,
881 ASN_CLASS_APPLICATION | ASN_APP_TIMETICKS, v));
885 * Construct a new OID by taking a range of sub ids of the original oid.
888 asn_slice_oid(struct asn_oid *dest, const struct asn_oid *src,
889 u_int from, u_int to)
895 dest->len = to - from;
896 memcpy(dest->subs, &src->subs[from], dest->len * sizeof(dest->subs[0]));
903 asn_append_oid(struct asn_oid *to, const struct asn_oid *from)
905 memcpy(&to->subs[to->len], &from->subs[0],
906 from->len * sizeof(from->subs[0]));
907 to->len += from->len;
914 asn_skip(struct asn_buf *b, asn_len_t len)
916 if (b->asn_len < len)
917 return (ASN_ERR_EOBUF);
931 asn_compare_oid(const struct asn_oid *o1, const struct asn_oid *o2)
935 for (i = 0; i < o1->len && i < o2->len; i++) {
936 if (o1->subs[i] < o2->subs[i])
938 if (o1->subs[i] > o2->subs[i])
941 if (o1->len < o2->len)
943 if (o1->len > o2->len)
949 * Check whether an OID is a sub-string of another OID.
952 asn_is_suboid(const struct asn_oid *o1, const struct asn_oid *o2)
956 for (i = 0; i < o1->len; i++)
957 if (i >= o2->len || o1->subs[i] != o2->subs[i])
963 * Put a string representation of an oid into a user buffer. This buffer
964 * is assumed to be at least ASN_OIDSTRLEN characters long.
966 * sprintf is assumed not to fail here.
969 asn_oid2str_r(const struct asn_oid *oid, char *buf)
974 if ((len = oid->len) > ASN_MAXOIDLEN)
977 for (i = 0, ptr = buf; i < len; i++) {
980 ptr += sprintf(ptr, "%u", oid->subs[i]);
986 * Make a string from an OID in a private buffer.
989 asn_oid2str(const struct asn_oid *oid)
991 static char str[ASN_OIDSTRLEN];
993 return (asn_oid2str_r(oid, str));
998 asn_error_func(const struct asn_buf *b, const char *err, ...)
1003 fprintf(stderr, "ASN.1: ");
1005 vfprintf(stderr, err, ap);
1009 fprintf(stderr, " at");
1010 for (i = 0; b->asn_len > i; i++)
1011 fprintf(stderr, " %02x", b->asn_cptr[i]);
1013 fprintf(stderr, "\n");