1 /* 2 * Copyright (c) 2001-2003 3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 4 * All rights reserved. 5 * 6 * Author: Harti Brandt <harti@freebsd.org> 7 * 8 * Redistribution of this software and documentation and use in source and 9 * binary forms, with or without modification, are permitted provided that 10 * the following conditions are met: 11 * 12 * 1. Redistributions of source code or documentation must retain the above 13 * copyright notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS 22 * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 25 * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 28 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * $Begemot: bsnmp/lib/asn1.h,v 1.16 2002/02/11 10:19:57 hbb Exp $ 34 * 35 * ASN.1 for SNMP 36 */ 37 #ifndef asn1_h_ 38 #define asn1_h_ 39 40 #include <sys/types.h> 41 42 struct asn_buf { 43 union { 44 u_char *ptr; 45 const u_char *cptr; 46 } asn_u; 47 size_t asn_len; 48 }; 49 #define asn_cptr asn_u.cptr 50 #define asn_ptr asn_u.ptr 51 52 /* these restrictions are in the SMI */ 53 #define ASN_MAXID 0xffffffff 54 #define ASN_MAXOIDLEN 128 55 56 /* the string needed for this (with trailing zero) */ 57 #define ASN_OIDSTRLEN (ASN_MAXOIDLEN * (10 + 1) - 1 + 1) 58 59 /* type of subidentifiers */ 60 typedef u_int32_t asn_subid_t; 61 62 struct asn_oid { 63 u_int len; 64 asn_subid_t subs[ASN_MAXOIDLEN]; 65 }; 66 67 enum asn_err { 68 /* conversion was ok */ 69 ASN_ERR_OK = 0, 70 /* conversion failed and stopped */ 71 ASN_ERR_FAILED = 1 | 0x1000, 72 /* length field bad, value skipped */ 73 ASN_ERR_BADLEN = 2, 74 /* out of buffer, stopped */ 75 ASN_ERR_EOBUF = 3 | 0x1000, 76 /* length ok, but value is out of range */ 77 ASN_ERR_RANGE = 4, 78 /* not the expected tag, stopped */ 79 ASN_ERR_TAG = 5 | 0x1000, 80 }; 81 #define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0) 82 83 /* type for the length field of encoded values. The length is restricted 84 * to 65535, but using u_int16_t would give conversion warnings on gcc */ 85 typedef u_int32_t asn_len_t; /* could be also u_int16_t */ 86 87 /* maximal length of a long length field without the length of the length */ 88 #define ASN_MAXLEN 65535 89 #define ASN_MAXLENLEN 2 /* number of bytes in a length */ 90 91 /* maximum size of an octet string as per SMIv2 */ 92 #define ASN_MAXOCTETSTRING 65535 93 94 extern void (*asn_error)(const struct asn_buf *, const char *, ...); 95 96 enum asn_err asn_get_header(struct asn_buf *, u_char *, asn_len_t *); 97 enum asn_err asn_put_header(struct asn_buf *, u_char, asn_len_t); 98 99 enum asn_err asn_put_temp_header(struct asn_buf *, u_char, u_char **); 100 enum asn_err asn_commit_header(struct asn_buf *, u_char *); 101 102 enum asn_err asn_get_integer_raw(struct asn_buf *, asn_len_t, int32_t *); 103 enum asn_err asn_get_integer(struct asn_buf *, int32_t *); 104 enum asn_err asn_put_integer(struct asn_buf *, int32_t); 105 106 enum asn_err asn_get_octetstring_raw(struct asn_buf *, asn_len_t, u_char *, u_int *); 107 enum asn_err asn_get_octetstring(struct asn_buf *, u_char *, u_int *); 108 enum asn_err asn_put_octetstring(struct asn_buf *, const u_char *, u_int); 109 110 enum asn_err asn_get_null_raw(struct asn_buf *b, asn_len_t); 111 enum asn_err asn_get_null(struct asn_buf *); 112 enum asn_err asn_put_null(struct asn_buf *); 113 114 enum asn_err asn_put_exception(struct asn_buf *, u_int); 115 116 enum asn_err asn_get_objid_raw(struct asn_buf *, asn_len_t, struct asn_oid *); 117 enum asn_err asn_get_objid(struct asn_buf *, struct asn_oid *); 118 enum asn_err asn_put_objid(struct asn_buf *, const struct asn_oid *); 119 120 enum asn_err asn_get_sequence(struct asn_buf *, asn_len_t *); 121 122 enum asn_err asn_get_ipaddress_raw(struct asn_buf *, asn_len_t, u_char *); 123 enum asn_err asn_get_ipaddress(struct asn_buf *, u_char *); 124 enum asn_err asn_put_ipaddress(struct asn_buf *, const u_char *); 125 126 enum asn_err asn_get_uint32_raw(struct asn_buf *, asn_len_t, u_int32_t *); 127 enum asn_err asn_put_uint32(struct asn_buf *, u_char, u_int32_t); 128 129 enum asn_err asn_get_counter64_raw(struct asn_buf *, asn_len_t, u_int64_t *); 130 enum asn_err asn_put_counter64(struct asn_buf *, u_int64_t); 131 132 enum asn_err asn_get_timeticks(struct asn_buf *, u_int32_t *); 133 enum asn_err asn_put_timeticks(struct asn_buf *, u_int32_t); 134 135 enum asn_err asn_skip(struct asn_buf *, asn_len_t); 136 137 /* 138 * Utility functions for OIDs 139 */ 140 /* get a sub-OID from the middle of another OID */ 141 void asn_slice_oid(struct asn_oid *, const struct asn_oid *, u_int, u_int); 142 143 /* append an OID to another one */ 144 void asn_append_oid(struct asn_oid *, const struct asn_oid *); 145 146 /* compare two OIDs */ 147 int asn_compare_oid(const struct asn_oid *, const struct asn_oid *); 148 149 /* check whether the first is a suboid of the second one */ 150 int asn_is_suboid(const struct asn_oid *, const struct asn_oid *); 151 152 /* format an OID into a user buffer of size ASN_OIDSTRLEN */ 153 char *asn_oid2str_r(const struct asn_oid *, char *); 154 155 /* format an OID into a private static buffer */ 156 char *asn_oid2str(const struct asn_oid *); 157 158 enum { 159 ASN_TYPE_BOOLEAN = 0x01, 160 ASN_TYPE_INTEGER = 0x02, 161 ASN_TYPE_BITSTRING = 0x03, 162 ASN_TYPE_OCTETSTRING = 0x04, 163 ASN_TYPE_NULL = 0x05, 164 ASN_TYPE_OBJID = 0x06, 165 ASN_TYPE_SEQUENCE = 0x10, 166 167 ASN_TYPE_CONSTRUCTED = 0x20, 168 ASN_CLASS_UNIVERSAL = 0x00, 169 ASN_CLASS_APPLICATION = 0x40, 170 ASN_CLASS_CONTEXT = 0x80, 171 ASN_CLASS_PRIVATE = 0xc0, 172 ASN_TYPE_MASK = 0x1f, 173 174 ASN_APP_IPADDRESS = 0x00, 175 ASN_APP_COUNTER = 0x01, 176 ASN_APP_GAUGE = 0x02, 177 ASN_APP_TIMETICKS = 0x03, 178 ASN_APP_OPAQUE = 0x04, /* not implemented */ 179 ASN_APP_COUNTER64 = 0x06, 180 181 ASN_EXCEPT_NOSUCHOBJECT = 0x00, 182 ASN_EXCEPT_NOSUCHINSTANCE = 0x01, 183 ASN_EXCEPT_ENDOFMIBVIEW = 0x02, 184 }; 185 186 #endif 187