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, size_t *moved)
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);
226 * BER integer. This may be used to get a signed 64 bit integer at maximum.
227 * The maximum length should be checked by the caller. This cannot overflow
228 * if the caller ensures that len is at maximum 8.
233 asn_get_real_integer(struct asn_buf *b, asn_len_t len, int64_t *vp)
239 if (b->asn_len < len) {
240 asn_error(b, "truncated integer");
241 return (ASN_ERR_EOBUF);
244 asn_error(b, "zero-length integer");
246 return (ASN_ERR_BADLEN);
252 ((*b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) ||
253 (*b->asn_cptr == 0xff && (b->asn_cptr[1] & 0x80) == 0x80))) {
254 asn_error(b, "non-minimal integer");
255 err = ASN_ERR_BADLEN;
258 if (*b->asn_cptr & 0x80)
263 val |= neg ? (u_char)~*b->asn_cptr : *b->asn_cptr;
268 *vp = -(int64_t)val - 1;
275 * Write a signed integer with the given type. The caller has to ensure
276 * that the actual value is ok for this type.
279 asn_put_real_integer(struct asn_buf *b, u_char type, int64_t ival)
288 /* this may fail if |INT64_MIN| > |INT64_MAX| and
289 * the value is between * INT64_MIN <= ival < -(INT64_MAX+1) */
290 val = (uint64_t)-(ival + 1);
293 val = (uint64_t)ival;
295 /* split the value into octets */
296 for (i = OCTETS - 1; i >= 0; i--) {
302 /* no leading 9 zeroes or ones */
303 for (i = 0; i < OCTETS - 1; i++)
304 if (!((buf[i] == 0xff && (buf[i + 1] & 0x80) != 0) ||
305 (buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0)))
307 if ((ret = asn_put_header(b, type, OCTETS - i)))
309 if (OCTETS - (u_int)i > b->asn_len)
310 return (ASN_ERR_EOBUF);
313 *b->asn_ptr++ = buf[i++];
322 * The same for unsigned 64-bitters. Here we have the problem, that overflow
323 * can happen, because the value maybe 9 bytes long. In this case the
324 * first byte must be 0.
327 asn_get_real_unsigned(struct asn_buf *b, asn_len_t len, uint64_t *vp)
331 if (b->asn_len < len) {
332 asn_error(b, "truncated integer");
333 return (ASN_ERR_EOBUF);
336 asn_error(b, "zero-length integer");
338 return (ASN_ERR_BADLEN);
342 if ((*b->asn_cptr & 0x80) || (len == 9 && *b->asn_cptr != 0)) {
343 /* negative integer or too larger */
344 *vp = 0xffffffffffffffffULL;
346 } else if (len > 1 &&
347 *b->asn_cptr == 0x00 && (b->asn_cptr[1] & 0x80) == 0) {
348 asn_error(b, "non-minimal unsigned");
349 err = ASN_ERR_BADLEN;
353 *vp = (*vp << 8) | *b->asn_cptr++;
361 * Values with the msb on need 9 octets.
364 asn_put_real_unsigned(struct asn_buf *b, u_char type, uint64_t val)
371 /* split the value into octets */
372 for (i = OCTETS - 1; i >= 0; i--) {
376 /* no leading 9 zeroes */
377 for (i = 0; i < OCTETS - 1; i++)
378 if (!(buf[i] == 0x00 && (buf[i + 1] & 0x80) == 0))
380 if ((ret = asn_put_header(b, type, OCTETS - i)))
382 if (OCTETS - (u_int)i > b->asn_len)
383 return (ASN_ERR_EOBUF);
386 *b->asn_ptr++ = buf[i++];
394 * The ASN.1 INTEGER type is restricted to 32-bit signed by the SMI.
397 asn_get_integer_raw(struct asn_buf *b, asn_len_t len, int32_t *vp)
402 if ((ret = asn_get_real_integer(b, len, &val)) == ASN_ERR_OK) {
404 ret = ASN_ERR_BADLEN;
405 else if (val > INT32_MAX || val < INT32_MIN)
414 asn_get_integer(struct asn_buf *b, int32_t *vp)
420 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
422 if (type != ASN_TYPE_INTEGER) {
423 asn_error(b, "bad type for integer (%u)", type);
424 return (ASN_ERR_TAG);
427 return (asn_get_integer_raw(b, len, vp));
431 asn_put_integer(struct asn_buf *b, int32_t val)
433 return (asn_put_real_integer(b, ASN_TYPE_INTEGER, val));
439 * <0x04> <len> <data ...>
441 * Get an octetstring. noctets must point to the buffer size and on
442 * return will contain the size of the octetstring, regardless of the
446 asn_get_octetstring_raw(struct asn_buf *b, asn_len_t len, u_char *octets,
449 enum asn_err err = ASN_ERR_OK;
451 if (*noctets < len) {
452 asn_error(b, "octetstring truncated");
455 if (b->asn_len < len) {
456 asn_error(b, "truncatet octetstring");
457 return (ASN_ERR_EOBUF);
460 memcpy(octets, b->asn_cptr, *noctets);
462 memcpy(octets, b->asn_cptr, len);
470 asn_get_octetstring(struct asn_buf *b, u_char *octets, u_int *noctets)
476 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
478 if (type != ASN_TYPE_OCTETSTRING) {
479 asn_error(b, "bad type for octetstring (%u)", type);
480 return (ASN_ERR_TAG);
482 return (asn_get_octetstring_raw(b, len, octets, noctets));
486 asn_put_octetstring(struct asn_buf *b, const u_char *octets, u_int noctets)
490 if ((ret = asn_put_header(b, ASN_TYPE_OCTETSTRING, noctets)) != ASN_ERR_OK)
492 if (b->asn_len < noctets)
493 return (ASN_ERR_EOBUF);
495 memcpy(b->asn_ptr, octets, noctets);
496 b->asn_ptr += noctets;
497 b->asn_len -= noctets;
507 asn_get_null_raw(struct asn_buf *b, asn_len_t len)
510 if (b->asn_len < len) {
511 asn_error(b, "truncated NULL");
512 return (ASN_ERR_EOBUF);
514 asn_error(b, "bad length for NULL (%u)", len);
517 return (ASN_ERR_BADLEN);
523 asn_get_null(struct asn_buf *b)
529 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
531 if (type != ASN_TYPE_NULL) {
532 asn_error(b, "bad type for NULL (%u)", type);
533 return (ASN_ERR_TAG);
535 return (asn_get_null_raw(b, len));
539 asn_put_null(struct asn_buf *b)
541 return (asn_put_header(b, ASN_TYPE_NULL, 0));
545 asn_put_exception(struct asn_buf *b, u_int except)
547 return (asn_put_header(b, ASN_CLASS_CONTEXT | except, 0));
553 * <0x06> <len> <subid...>
556 asn_get_objid_raw(struct asn_buf *b, asn_len_t len, struct asn_oid *oid)
561 if (b->asn_len < len) {
562 asn_error(b, "truncated OBJID");
563 return (ASN_ERR_EOBUF);
567 asn_error(b, "short OBJID");
568 oid->subs[oid->len++] = 0;
569 oid->subs[oid->len++] = 0;
570 return (ASN_ERR_BADLEN);
574 if (oid->len == ASN_MAXOIDLEN) {
575 asn_error(b, "OID too long (%u)", oid->len);
578 return (ASN_ERR_BADLEN);
583 asn_error(b, "unterminated subid");
584 return (ASN_ERR_EOBUF);
586 if (subid > (ASN_MAXID >> 7)) {
587 asn_error(b, "OBID subid too larger");
590 subid = (subid << 7) | (*b->asn_cptr & 0x7f);
593 } while (*b->asn_cptr++ & 0x80);
596 oid->subs[oid->len++] = subid / 40;
597 oid->subs[oid->len++] = subid % 40;
599 oid->subs[oid->len++] = 2;
600 oid->subs[oid->len++] = subid - 80;
603 oid->subs[oid->len++] = subid;
611 asn_get_objid(struct asn_buf *b, struct asn_oid *oid)
617 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
619 if (type != ASN_TYPE_OBJID) {
620 asn_error(b, "bad type for OBJID (%u)", type);
621 return (ASN_ERR_TAG);
623 return (asn_get_objid_raw(b, len, oid));
627 asn_put_objid(struct asn_buf *b, const struct asn_oid *oid)
629 asn_subid_t first, sub;
630 enum asn_err err, err1;
637 asn_error(NULL, "short oid");
641 } else if (oid->len == 1) {
643 asn_error(b, "short oid");
644 if (oid->subs[0] > 2)
645 asn_error(NULL, "oid[0] too large (%u)", oid->subs[0]);
647 first = oid->subs[0] * 40;
650 if (oid->len > ASN_MAXOIDLEN) {
651 asn_error(NULL, "oid too long %u", oid->len);
654 if (oid->subs[0] > 2 ||
655 (oid->subs[0] < 2 && oid->subs[0] >= 40)) {
656 asn_error(NULL, "oid out of range (%u,%u)",
657 oid->subs[0], oid->subs[1]);
660 first = 40 * oid->subs[0] + oid->subs[1];
664 for (i = 1; i < oidlen; i++) {
665 sub = (i == 1) ? first : oid->subs[i];
666 if (sub > ASN_MAXID) {
667 asn_error(NULL, "oid subid too large");
670 len += (sub <= 0x7f) ? 1
671 : (sub <= 0x3fff) ? 2
672 : (sub <= 0x1fffff) ? 3
673 : (sub <= 0xfffffff) ? 4
676 if ((err1 = asn_put_header(b, ASN_TYPE_OBJID, len)) != ASN_ERR_OK)
678 if (b->asn_len < len)
679 return (ASN_ERR_EOBUF);
681 for (i = 1; i < oidlen; i++) {
682 sub = (i == 1) ? first : oid->subs[i];
686 } else if (sub <= 0x3fff) {
687 *b->asn_ptr++ = (sub >> 7) | 0x80;
688 *b->asn_ptr++ = sub & 0x7f;
690 } else if (sub <= 0x1fffff) {
691 *b->asn_ptr++ = (sub >> 14) | 0x80;
692 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
693 *b->asn_ptr++ = sub & 0x7f;
695 } else if (sub <= 0xfffffff) {
696 *b->asn_ptr++ = (sub >> 21) | 0x80;
697 *b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
698 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
699 *b->asn_ptr++ = sub & 0x7f;
702 *b->asn_ptr++ = (sub >> 28) | 0x80;
703 *b->asn_ptr++ = ((sub >> 21) & 0x7f) | 0x80;
704 *b->asn_ptr++ = ((sub >> 14) & 0x7f) | 0x80;
705 *b->asn_ptr++ = ((sub >> 7) & 0x7f) | 0x80;
706 *b->asn_ptr++ = sub & 0x7f;
715 * <0x10|0x20> <len> <data...>
718 asn_get_sequence(struct asn_buf *b, asn_len_t *len)
723 if ((err = asn_get_header(b, &type, len)) != ASN_ERR_OK)
725 if (type != (ASN_TYPE_SEQUENCE|ASN_TYPE_CONSTRUCTED)) {
726 asn_error(b, "bad sequence type %u", type);
727 return (ASN_ERR_TAG);
729 if (*len > b->asn_len) {
730 asn_error(b, "truncated sequence");
731 return (ASN_ERR_EOBUF);
739 * 0x40 4 MSB 2MSB 2LSB LSB
742 asn_get_ipaddress_raw(struct asn_buf *b, asn_len_t len, u_char *addr)
746 if (b->asn_len < len) {
747 asn_error(b, "truncated ip-address");
748 return (ASN_ERR_EOBUF);
751 asn_error(b, "short length for ip-Address %u", len);
752 for (i = 0; i < len; i++)
753 *addr++ = *b->asn_cptr++;
757 return (ASN_ERR_BADLEN);
759 for (i = 0; i < 4; i++)
760 *addr++ = *b->asn_cptr++;
761 b->asn_cptr += len - 4;
767 asn_get_ipaddress(struct asn_buf *b, u_char *addr)
773 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
775 if (type != (ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS)) {
776 asn_error(b, "bad type for ip-address %u", type);
777 return (ASN_ERR_TAG);
779 return (asn_get_ipaddress_raw(b, len, addr));
783 asn_put_ipaddress(struct asn_buf *b, const u_char *addr)
787 if ((err = asn_put_header(b, ASN_CLASS_APPLICATION|ASN_APP_IPADDRESS,
791 return (ASN_ERR_EOBUF);
793 memcpy(b->asn_ptr, addr, 4);
803 * 0x42|0x41 <len> ...
806 asn_get_uint32_raw(struct asn_buf *b, asn_len_t len, uint32_t *vp)
811 if ((err = asn_get_real_unsigned(b, len, &v)) == ASN_ERR_OK) {
813 asn_error(b, "uint32 too long %u", len);
814 err = ASN_ERR_BADLEN;
815 } else if (v > UINT32_MAX) {
816 asn_error(b, "uint32 too large %llu", v);
825 asn_put_uint32(struct asn_buf *b, u_char type, uint32_t val)
829 return (asn_put_real_unsigned(b, ASN_CLASS_APPLICATION|type, v));
837 asn_get_counter64_raw(struct asn_buf *b, asn_len_t len, uint64_t *vp)
839 return (asn_get_real_unsigned(b, len, vp));
843 asn_put_counter64(struct asn_buf *b, uint64_t val)
845 return (asn_put_real_unsigned(b,
846 ASN_CLASS_APPLICATION | ASN_APP_COUNTER64, val));
854 asn_get_timeticks(struct asn_buf *b, uint32_t *vp)
860 if ((err = asn_get_header(b, &type, &len)) != ASN_ERR_OK)
862 if (type != (ASN_CLASS_APPLICATION|ASN_APP_TIMETICKS)) {
863 asn_error(b, "bad type for timeticks %u", type);
864 return (ASN_ERR_TAG);
866 return (asn_get_uint32_raw(b, len, vp));
870 asn_put_timeticks(struct asn_buf *b, uint32_t val)
874 return (asn_put_real_unsigned(b,
875 ASN_CLASS_APPLICATION | ASN_APP_TIMETICKS, v));
879 * Construct a new OID by taking a range of sub ids of the original oid.
882 asn_slice_oid(struct asn_oid *dest, const struct asn_oid *src,
883 u_int from, u_int to)
889 dest->len = to - from;
890 memcpy(dest->subs, &src->subs[from], dest->len * sizeof(dest->subs[0]));
897 asn_append_oid(struct asn_oid *to, const struct asn_oid *from)
899 memcpy(&to->subs[to->len], &from->subs[0],
900 from->len * sizeof(from->subs[0]));
901 to->len += from->len;
908 asn_skip(struct asn_buf *b, asn_len_t len)
910 if (b->asn_len < len)
911 return (ASN_ERR_EOBUF);
921 asn_pad(struct asn_buf *b, asn_len_t len)
923 if (b->asn_len < len)
924 return (ASN_ERR_EOBUF);
939 asn_compare_oid(const struct asn_oid *o1, const struct asn_oid *o2)
943 for (i = 0; i < o1->len && i < o2->len; i++) {
944 if (o1->subs[i] < o2->subs[i])
946 if (o1->subs[i] > o2->subs[i])
949 if (o1->len < o2->len)
951 if (o1->len > o2->len)
957 * Check whether an OID is a sub-string of another OID.
960 asn_is_suboid(const struct asn_oid *o1, const struct asn_oid *o2)
964 for (i = 0; i < o1->len; i++)
965 if (i >= o2->len || o1->subs[i] != o2->subs[i])
971 * Put a string representation of an oid into a user buffer. This buffer
972 * is assumed to be at least ASN_OIDSTRLEN characters long.
974 * sprintf is assumed not to fail here.
977 asn_oid2str_r(const struct asn_oid *oid, char *buf)
982 if ((len = oid->len) > ASN_MAXOIDLEN)
985 for (i = 0, ptr = buf; i < len; i++) {
988 ptr += sprintf(ptr, "%u", oid->subs[i]);
994 * Make a string from an OID in a private buffer.
997 asn_oid2str(const struct asn_oid *oid)
999 static char str[ASN_OIDSTRLEN];
1001 return (asn_oid2str_r(oid, str));
1006 asn_error_func(const struct asn_buf *b, const char *err, ...)
1011 fprintf(stderr, "ASN.1: ");
1013 vfprintf(stderr, err, ap);
1017 fprintf(stderr, " at");
1018 for (i = 0; b->asn_len > i; i++)
1019 fprintf(stderr, " %02x", b->asn_cptr[i]);
1021 fprintf(stderr, "\n");