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.31 2005/10/06 07:14:58 brandt_h Exp $
33 #include <sys/types.h>
40 #elif defined(HAVE_INTTYPES_H)
48 static void asn_error_func(const struct asn_buf *, const char *, ...);
50 void (*asn_error)(const struct asn_buf *, const char *, ...) = asn_error_func;
53 * Read the next header. This reads the tag (note, that only single
54 * byte tags are supported for now) and the length field. The length field
55 * is restricted to a 32-bit value.
56 * All errors of this function stop the decoding.
59 asn_get_header(struct asn_buf *b, u_char *type, asn_len_t *len)
63 if (b->asn_len == 0) {
64 asn_error(b, "no identifier for header");
65 return (ASN_ERR_EOBUF);
68 if ((*type & ASN_TYPE_MASK) > 0x30) {
69 asn_error(b, "types > 0x30 not supported (%u)",
70 *type & ASN_TYPE_MASK);
71 return (ASN_ERR_FAILED);
75 if (b->asn_len == 0) {
76 asn_error(b, "no length field");
77 return (ASN_ERR_EOBUF);
79 if (*b->asn_cptr & 0x80) {
80 length = *b->asn_cptr++ & 0x7f;
83 asn_error(b, "indefinite length not supported");
84 return (ASN_ERR_FAILED);
86 if (length > ASN_MAXLENLEN) {
87 asn_error(b, "long length too long (%u)", length);
88 return (ASN_ERR_FAILED);
90 if (length > b->asn_len) {
91 asn_error(b, "long length truncated");
92 return (ASN_ERR_EOBUF);
96 *len = (*len << 8) | *b->asn_cptr++;
100 *len = *b->asn_cptr++;
107 * Write a length field (restricted to values < 2^32-1) and return the
108 * number of bytes this field takes. If ptr is NULL, the length is computed
109 * but nothing is written. If the length would be too large return 0.
112 asn_put_len(u_char *ptr, asn_len_t len)
114 u_int lenlen, lenlen1;
117 if (len > ASN_MAXLEN) {
118 asn_error(NULL, "encoding length too long: (%u)", len);
124 *ptr++ = (u_char)len;
128 /* compute number of bytes for value (is at least 1) */
129 for (tmp = len; tmp != 0; tmp >>= 8)
132 *ptr++ = (u_char)lenlen | 0x80;
134 while (lenlen1-- > 0) {
135 ptr[lenlen1] = len & 0xff;
144 * Write a header (tag and length fields).
145 * Tags are restricted to one byte tags (value <= 0x30) and the
146 * lenght field to 16-bit. All errors stop the encoding.
149 asn_put_header(struct asn_buf *b, u_char type, asn_len_t len)
154 if ((type & ASN_TYPE_MASK) > 0x30) {
155 asn_error(NULL, "types > 0x30 not supported (%u)",
156 type & ASN_TYPE_MASK);
157 return (ASN_ERR_FAILED);
160 return (ASN_ERR_EOBUF);
162 *b->asn_ptr++ = type;
166 if ((lenlen = asn_put_len(NULL, len)) == 0)
167 return (ASN_ERR_FAILED);
168 if (b->asn_len < lenlen)
169 return (ASN_ERR_EOBUF);
171 (void)asn_put_len(b->asn_ptr, len);
172 b->asn_ptr += lenlen;
173 b->asn_len -= lenlen;
179 * This constructs a temporary sequence header with space for the maximum
180 * length field (three byte). Set the pointer that ptr points to to the
181 * start of the encoded header. This is used for a later call to
182 * asn_commit_header which will fix-up the length field and move the
183 * value if needed. All errors should stop the encoding.
185 #define TEMP_LEN (1 + ASN_MAXLENLEN + 1)
187 asn_put_temp_header(struct asn_buf *b, u_char type, u_char **ptr)
191 if (b->asn_len < TEMP_LEN)
192 return (ASN_ERR_EOBUF);
194 if ((ret = asn_put_header(b, type, ASN_MAXLEN)) == ASN_ERR_OK)
195 assert(b->asn_ptr == *ptr + TEMP_LEN);
199 asn_commit_header(struct asn_buf *b, u_char *ptr)
204 /* compute length of encoded value without header */
205 len = b->asn_ptr - (ptr + TEMP_LEN);
207 /* insert length. may not fail. */
208 lenlen = asn_put_len(ptr + 1, len);
209 if (lenlen > TEMP_LEN - 1)
210 return (ASN_ERR_FAILED);
212 if (lenlen < TEMP_LEN - 1) {
213 /* shift value down */
214 shift = (TEMP_LEN - 1) - lenlen;
215 memmove(ptr + 1 + lenlen, ptr + TEMP_LEN, len);
224 * BER integer. This may be used to get a signed 64 bit integer at maximum.
225 * The maximum length should be checked by the caller. This cannot overflow
226 * if the caller ensures that len is at maximum 8.
231 asn_get_real_integer(struct asn_buf *b, asn_len_t len, int64_t *vp)
237 if (b->asn_len < len) {
238 asn_error(b, "truncated integer");
239 return (ASN_ERR_EOBUF);
242 asn_error(b, "zero-length integer");
244 return (ASN_ERR_BADLEN);
250 ((*b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) ||
251 (*b->asn_cptr == 0xff && (b->asn_cptr[1] & 0x80) == 0x80))) {
252 asn_error(b, "non-minimal integer");
253 err = ASN_ERR_BADLEN;
256 if (*b->asn_cptr & 0x80)
261 val |= neg ? (u_char)~*b->asn_cptr : *b->asn_cptr;
266 *vp = -(int64_t)val - 1;
273 * Write a signed integer with the given type. The caller has to ensure
274 * that the actual value is ok for this type.
277 asn_put_real_integer(struct asn_buf *b, u_char type, int64_t ival)
286 /* this may fail if |INT64_MIN| > |INT64_MAX| and
287 * the value is between * INT64_MIN <= ival < -(INT64_MAX+1) */
288 val = (uint64_t)-(ival + 1);
291 val = (uint64_t)ival;
293 /* split the value into octets */
294 for (i = OCTETS - 1; i >= 0; i--) {
300 /* no leading 9 zeroes or ones */
301 for (i = 0; i < OCTETS - 1; i++)
302 if (!((buf[i] == 0xff && (buf[i + 1] & 0x80) != 0) ||
303 (buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0)))
305 if ((ret = asn_put_header(b, type, OCTETS - i)))
307 if (OCTETS - (u_int)i > b->asn_len)
308 return (ASN_ERR_EOBUF);
311 *b->asn_ptr++ = buf[i++];
320 * The same for unsigned 64-bitters. Here we have the problem, that overflow
321 * can happen, because the value maybe 9 bytes long. In this case the
322 * first byte must be 0.
325 asn_get_real_unsigned(struct asn_buf *b, asn_len_t len, uint64_t *vp)
329 if (b->asn_len < len) {
330 asn_error(b, "truncated integer");
331 return (ASN_ERR_EOBUF);
334 asn_error(b, "zero-length integer");
336 return (ASN_ERR_BADLEN);
340 if ((*b->asn_cptr & 0x80) || (len == 9 && *b->asn_cptr != 0)) {
341 /* negative integer or too larger */
342 *vp = 0xffffffffffffffffULL;
344 } else if (len > 1 &&
345 *b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) {
346 asn_error(b, "non-minimal unsigned");
347 err = ASN_ERR_BADLEN;
351 *vp = (*vp << 8) | *b->asn_cptr++;
359 * Values with the msb on need 9 octets.
362 asn_put_real_unsigned(struct asn_buf *b, u_char type, uint64_t val)
369 /* split the value into octets */
370 for (i = OCTETS - 1; i >= 0; i--) {
374 /* no leading 9 zeroes */
375 for (i = 0; i < OCTETS - 1; i++)
376 if (!(buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0))
378 if ((ret = asn_put_header(b, type, OCTETS - i)))
380 if (OCTETS - (u_int)i > b->asn_len)
381 return (ASN_ERR_EOBUF);
384 *b->asn_ptr++ = buf[i++];
392 * The ASN.1 INTEGER type is restricted to 32-bit signed by the SMI.
395 asn_get_integer_raw(struct asn_buf *b, asn_len_t len, int32_t *vp)
400 if ((ret = asn_get_real_integer(b, len, &val)) == ASN_ERR_OK) {
402 ret = ASN_ERR_BADLEN;
403 else if (val > INT32_MAX || val < INT32_MIN)
412 asn_get_integer(struct asn_buf *b, int32_t *vp)
418 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
420 if (type != ASN_TYPE_INTEGER) {
421 asn_error(b, "bad type for integer (%u)", type);
422 return (ASN_ERR_TAG);
425 return (asn_get_integer_raw(b, len, vp));
429 asn_put_integer(struct asn_buf *b, int32_t val)
431 return (asn_put_real_integer(b, ASN_TYPE_INTEGER, val));
437 * <0x04> <len> <data ...>
439 * Get an octetstring. noctets must point to the buffer size and on
440 * return will contain the size of the octetstring, regardless of the
444 asn_get_octetstring_raw(struct asn_buf *b, asn_len_t len, u_char *octets,
447 enum asn_err err = ASN_ERR_OK;
449 if (*noctets < len) {
450 asn_error(b, "octetstring truncated");
453 if (b->asn_len < len) {
454 asn_error(b, "truncatet octetstring");
455 return (ASN_ERR_EOBUF);
458 memcpy(octets, b->asn_cptr, *noctets);
460 memcpy(octets, b->asn_cptr, len);
468 asn_get_octetstring(struct asn_buf *b, u_char *octets, u_int *noctets)
474 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
476 if (type != ASN_TYPE_OCTETSTRING) {
477 asn_error(b, "bad type for octetstring (%u)", type);
478 return (ASN_ERR_TAG);
480 return (asn_get_octetstring_raw(b, len, octets, noctets));
484 asn_put_octetstring(struct asn_buf *b, const u_char *octets, u_int noctets)
488 if ((ret = asn_put_header(b, ASN_TYPE_OCTETSTRING, noctets)) != ASN_ERR_OK)
490 if (b->asn_len < noctets)
491 return (ASN_ERR_EOBUF);
493 memcpy(b->asn_ptr, octets, noctets);
494 b->asn_ptr += noctets;
495 b->asn_len -= noctets;
505 asn_get_null_raw(struct asn_buf *b, asn_len_t len)
508 if (b->asn_len < len) {
509 asn_error(b, "truncated NULL");
510 return (ASN_ERR_EOBUF);
512 asn_error(b, "bad length for NULL (%u)", len);
515 return (ASN_ERR_BADLEN);
521 asn_get_null(struct asn_buf *b)
527 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
529 if (type != ASN_TYPE_NULL) {
530 asn_error(b, "bad type for NULL (%u)", type);
531 return (ASN_ERR_TAG);
533 return (asn_get_null_raw(b, len));
537 asn_put_null(struct asn_buf *b)
539 return (asn_put_header(b, ASN_TYPE_NULL, 0));
543 asn_put_exception(struct asn_buf *b, u_int except)
545 return (asn_put_header(b, ASN_CLASS_CONTEXT | except, 0));
551 * <0x06> <len> <subid...>
554 asn_get_objid_raw(struct asn_buf *b, asn_len_t len, struct asn_oid *oid)
559 if (b->asn_len < len) {
560 asn_error(b, "truncated OBJID");
561 return (ASN_ERR_EOBUF);
565 asn_error(b, "short OBJID");
566 oid->subs[oid->len++] = 0;
567 oid->subs[oid->len++] = 0;
568 return (ASN_ERR_BADLEN);
572 if (oid->len == ASN_MAXOIDLEN) {
573 asn_error(b, "OID too long (%u)", oid->len);
576 return (ASN_ERR_BADLEN);
581 asn_error(b, "unterminated subid");
582 return (ASN_ERR_EOBUF);
584 if (subid > (ASN_MAXID >> 7)) {
585 asn_error(b, "OBID subid too larger");
588 subid = (subid << 7) | (*b->asn_cptr & 0x7f);
591 } while (*b->asn_cptr++ & 0x80);
594 oid->subs[oid->len++] = subid / 40;
595 oid->subs[oid->len++] = subid % 40;
597 oid->subs[oid->len++] = 2;
598 oid->subs[oid->len++] = subid - 80;
601 oid->subs[oid->len++] = subid;
609 asn_get_objid(struct asn_buf *b, struct asn_oid *oid)
615 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
617 if (type != ASN_TYPE_OBJID) {
618 asn_error(b, "bad type for OBJID (%u)", type);
619 return (ASN_ERR_TAG);
621 return (asn_get_objid_raw(b, len, oid));
625 asn_put_objid(struct asn_buf *b, const struct asn_oid *oid)
627 asn_subid_t first, sub;
628 enum asn_err err, err1;
635 asn_error(NULL, "short oid");
639 } else if (oid->len == 1) {
641 asn_error(b, "short oid");
642 if (oid->subs[0] > 2)
643 asn_error(NULL, "oid[0] too large (%u)", oid->subs[0]);
645 first = oid->subs[0] * 40;
648 if (oid->len > ASN_MAXOIDLEN) {
649 asn_error(NULL, "oid too long %u", oid->len);
652 if (oid->subs[0] > 2 ||
653 (oid->subs[0] < 2 && oid->subs[0] >= 40)) {
654 asn_error(NULL, "oid out of range (%u,%u)",
655 oid->subs[0], oid->subs[1]);
658 first = 40 * oid->subs[0] + oid->subs[1];
662 for (i = 1; i < oidlen; i++) {
663 sub = (i == 1) ? first : oid->subs[i];
664 if (sub > ASN_MAXID) {
665 asn_error(NULL, "oid subid too large");
668 len += (sub <= 0x7f) ? 1
669 : (sub <= 0x3fff) ? 2
670 : (sub <= 0x1fffff) ? 3
671 : (sub <= 0xfffffff) ? 4
674 if ((err1 = asn_put_header(b, ASN_TYPE_OBJID, len)) != ASN_ERR_OK)
676 if (b->asn_len < len)
677 return (ASN_ERR_EOBUF);
679 for (i = 1; i < oidlen; i++) {
680 sub = (i == 1) ? first : oid->subs[i];
684 } else if (sub <= 0x3fff) {
685 *b->asn_ptr++ = (sub >> 7) | 0x80;
686 *b->asn_ptr++ = sub & 0x7f;
688 } else if (sub <= 0x1fffff) {
689 *b->asn_ptr++ = (sub >> 14) | 0x80;
690 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
691 *b->asn_ptr++ = sub & 0x7f;
693 } else if (sub <= 0xfffffff) {
694 *b->asn_ptr++ = (sub >> 21) | 0x80;
695 *b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
696 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
697 *b->asn_ptr++ = sub & 0x7f;
700 *b->asn_ptr++ = (sub >> 28) | 0x80;
701 *b->asn_ptr++ = ((sub >> 21) & 0x7f) | 0x80;
702 *b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
703 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
704 *b->asn_ptr++ = sub & 0x7f;
713 * <0x10|0x20> <len> <data...>
716 asn_get_sequence(struct asn_buf *b, asn_len_t *len)
721 if ((err = asn_get_header(b, &type, len)) != ASN_ERR_OK)
723 if (type != (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED)) {
724 asn_error(b, "bad sequence type %u", type);
725 return (ASN_ERR_TAG);
727 if (*len > b->asn_len) {
728 asn_error(b, "truncated sequence");
729 return (ASN_ERR_EOBUF);
737 * 0x40 4 MSB 2MSB 2LSB LSB
740 asn_get_ipaddress_raw(struct asn_buf *b, asn_len_t len, u_char *addr)
744 if (b->asn_len < len) {
745 asn_error(b, "truncated ip-address");
746 return (ASN_ERR_EOBUF);
749 asn_error(b, "short length for ip-Address %u", len);
750 for (i = 0; i < len; i++)
751 *addr++ = *b->asn_cptr++;
755 return (ASN_ERR_BADLEN);
757 for (i = 0; i < 4; i++)
758 *addr++ = *b->asn_cptr++;
759 b->asn_cptr += len - 4;
765 asn_get_ipaddress(struct asn_buf *b, u_char *addr)
771 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
773 if (type != (ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS)) {
774 asn_error(b, "bad type for ip-address %u", type);
775 return (ASN_ERR_TAG);
777 return (asn_get_ipaddress_raw(b, len, addr));
781 asn_put_ipaddress(struct asn_buf *b, const u_char *addr)
785 if ((err = asn_put_header(b, ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS,
789 return (ASN_ERR_EOBUF);
791 memcpy(b->asn_ptr, addr, 4);
801 * 0x42|0x41 <len> ...
804 asn_get_uint32_raw(struct asn_buf *b, asn_len_t len, uint32_t *vp)
809 if ((err = asn_get_real_unsigned(b, len, &v)) == ASN_ERR_OK) {
811 asn_error(b, "uint32 too long %u", len);
812 err = ASN_ERR_BADLEN;
813 } else if (v > UINT32_MAX) {
814 asn_error(b, "uint32 too large %llu", v);
823 asn_put_uint32(struct asn_buf *b, u_char type, uint32_t val)
827 return (asn_put_real_unsigned(b, ASN_CLASS_APPLICATION|type, v));
835 asn_get_counter64_raw(struct asn_buf *b, asn_len_t len, uint64_t *vp)
837 return (asn_get_real_unsigned(b, len, vp));
841 asn_put_counter64(struct asn_buf *b, uint64_t val)
843 return (asn_put_real_unsigned(b,
844 ASN_CLASS_APPLICATION | ASN_APP_COUNTER64, val));
852 asn_get_timeticks(struct asn_buf *b, uint32_t *vp)
858 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
860 if (type != (ASN_CLASS_APPLICATION|ASN_APP_TIMETICKS)) {
861 asn_error(b, "bad type for timeticks %u", type);
862 return (ASN_ERR_TAG);
864 return (asn_get_uint32_raw(b, len, vp));
868 asn_put_timeticks(struct asn_buf *b, uint32_t val)
872 return (asn_put_real_unsigned(b,
873 ASN_CLASS_APPLICATION | ASN_APP_TIMETICKS, v));
877 * Construct a new OID by taking a range of sub ids of the original oid.
880 asn_slice_oid(struct asn_oid *dest, const struct asn_oid *src,
881 u_int from, u_int to)
887 dest->len = to - from;
888 memcpy(dest->subs, &src->subs[from], dest->len * sizeof(dest->subs[0]));
895 asn_append_oid(struct asn_oid *to, const struct asn_oid *from)
897 memcpy(&to->subs[to->len], &from->subs[0],
898 from->len * sizeof(from->subs[0]));
899 to->len += from->len;
906 asn_skip(struct asn_buf *b, asn_len_t len)
908 if (b->asn_len < len)
909 return (ASN_ERR_EOBUF);
923 asn_compare_oid(const struct asn_oid *o1, const struct asn_oid *o2)
927 for (i = 0; i < o1->len && i < o2->len; i++) {
928 if (o1->subs[i] < o2->subs[i])
930 if (o1->subs[i] > o2->subs[i])
933 if (o1->len < o2->len)
935 if (o1->len > o2->len)
941 * Check whether an OID is a sub-string of another OID.
944 asn_is_suboid(const struct asn_oid *o1, const struct asn_oid *o2)
948 for (i = 0; i < o1->len; i++)
949 if (i >= o2->len || o1->subs[i] != o2->subs[i])
955 * Put a string representation of an oid into a user buffer. This buffer
956 * is assumed to be at least ASN_OIDSTRLEN characters long.
958 * sprintf is assumed not to fail here.
961 asn_oid2str_r(const struct asn_oid *oid, char *buf)
966 if ((len = oid->len) > ASN_MAXOIDLEN)
969 for (i = 0, ptr = buf; i < len; i++) {
972 ptr += sprintf(ptr, "%u", oid->subs[i]);
978 * Make a string from an OID in a private buffer.
981 asn_oid2str(const struct asn_oid *oid)
983 static char str[ASN_OIDSTRLEN];
985 return (asn_oid2str_r(oid, str));
990 asn_error_func(const struct asn_buf *b, const char *err, ...)
995 fprintf(stderr, "ASN.1: ");
997 vfprintf(stderr, err, ap);
1001 fprintf(stderr, " at");
1002 for (i = 0; b->asn_len > i; i++)
1003 fprintf(stderr, " %02x", b->asn_cptr[i]);
1005 fprintf(stderr, "\n");