xref: /freebsd/contrib/bsnmp/lib/asn1.h (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/lib/asn1.h,v 1.20 2005/10/05 16:43:11 brandt_h Exp $
30  *
31  * ASN.1 for SNMP
32  */
33 #ifndef asn1_h_
34 #define asn1_h_
35 
36 #include <sys/types.h>
37 
38 struct asn_buf {
39 	union {
40 		u_char	*ptr;
41 		const u_char *cptr;
42 	}	asn_u;
43 	size_t	asn_len;
44 };
45 #define asn_cptr	asn_u.cptr
46 #define asn_ptr	asn_u.ptr
47 
48 /* these restrictions are in the SMI */
49 #define ASN_MAXID	0xffffffff
50 #define ASN_MAXOIDLEN	128
51 
52 /* the string needed for this (with trailing zero) */
53 #define ASN_OIDSTRLEN	(ASN_MAXOIDLEN * (10 + 1) - 1 + 1)
54 
55 /* type of subidentifiers */
56 typedef uint32_t asn_subid_t;
57 
58 struct asn_oid {
59 	u_int	len;
60 	asn_subid_t subs[ASN_MAXOIDLEN];
61 };
62 
63 enum asn_err {
64 	/* conversion was ok */
65 	ASN_ERR_OK	= 0,
66 	/* conversion failed and stopped */
67 	ASN_ERR_FAILED	= 1 | 0x1000,
68 	/* length field bad, value skipped */
69 	ASN_ERR_BADLEN	= 2,
70 	/* out of buffer, stopped */
71 	ASN_ERR_EOBUF	= 3 | 0x1000,
72 	/* length ok, but value is out of range */
73 	ASN_ERR_RANGE	= 4,
74 	/* not the expected tag, stopped */
75 	ASN_ERR_TAG	= 5 | 0x1000,
76 };
77 #define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
78 
79 /* type for the length field of encoded values. The length is restricted
80  * to 65535, but using uint16_t would give conversion warnings on gcc */
81 typedef uint32_t asn_len_t;	/* could be also uint16_t */
82 
83 /* maximal length of a long length field without the length of the length */
84 #define ASN_MAXLEN	65535
85 #define ASN_MAXLENLEN	2	/* number of bytes in a length */
86 
87 /* maximum size of an octet string as per SMIv2 */
88 #define ASN_MAXOCTETSTRING 65535
89 
90 extern void (*asn_error)(const struct asn_buf *, const char *, ...);
91 
92 enum asn_err asn_get_header(struct asn_buf *, u_char *, asn_len_t *);
93 enum asn_err asn_put_header(struct asn_buf *, u_char, asn_len_t);
94 
95 enum asn_err asn_put_temp_header(struct asn_buf *, u_char, u_char **);
96 enum asn_err asn_commit_header(struct asn_buf *, u_char *, size_t *);
97 
98 enum asn_err asn_get_integer_raw(struct asn_buf *, asn_len_t, int32_t *);
99 enum asn_err asn_get_integer(struct asn_buf *, int32_t *);
100 enum asn_err asn_put_integer(struct asn_buf *, int32_t);
101 
102 enum asn_err asn_get_octetstring_raw(struct asn_buf *, asn_len_t, u_char *, u_int *);
103 enum asn_err asn_get_octetstring(struct asn_buf *, u_char *, u_int *);
104 enum asn_err asn_put_octetstring(struct asn_buf *, const u_char *, u_int);
105 
106 enum asn_err asn_get_null_raw(struct asn_buf *b, asn_len_t);
107 enum asn_err asn_get_null(struct asn_buf *);
108 enum asn_err asn_put_null(struct asn_buf *);
109 
110 enum asn_err asn_put_exception(struct asn_buf *, u_int);
111 
112 enum asn_err asn_get_objid_raw(struct asn_buf *, asn_len_t, struct asn_oid *);
113 enum asn_err asn_get_objid(struct asn_buf *, struct asn_oid *);
114 enum asn_err asn_put_objid(struct asn_buf *, const struct asn_oid *);
115 
116 enum asn_err asn_get_sequence(struct asn_buf *, asn_len_t *);
117 
118 enum asn_err asn_get_ipaddress_raw(struct asn_buf *, asn_len_t, u_char *);
119 enum asn_err asn_get_ipaddress(struct asn_buf *, u_char *);
120 enum asn_err asn_put_ipaddress(struct asn_buf *, const u_char *);
121 
122 enum asn_err asn_get_uint32_raw(struct asn_buf *, asn_len_t, uint32_t *);
123 enum asn_err asn_put_uint32(struct asn_buf *, u_char, uint32_t);
124 
125 enum asn_err asn_get_counter64_raw(struct asn_buf *, asn_len_t, uint64_t *);
126 enum asn_err asn_put_counter64(struct asn_buf *, uint64_t);
127 
128 enum asn_err asn_get_timeticks(struct asn_buf *, uint32_t *);
129 enum asn_err asn_put_timeticks(struct asn_buf *, uint32_t);
130 
131 enum asn_err asn_skip(struct asn_buf *, asn_len_t);
132 enum asn_err asn_pad(struct asn_buf *, asn_len_t);
133 
134 /*
135  * Utility functions for OIDs
136  */
137 /* get a sub-OID from the middle of another OID */
138 void asn_slice_oid(struct asn_oid *, const struct asn_oid *, u_int, u_int);
139 
140 /* append an OID to another one */
141 void asn_append_oid(struct asn_oid *, const struct asn_oid *);
142 
143 /* compare two OIDs */
144 int asn_compare_oid(const struct asn_oid *, const struct asn_oid *);
145 
146 /* check whether the first is a suboid of the second one */
147 int asn_is_suboid(const struct asn_oid *, const struct asn_oid *);
148 
149 /* format an OID into a user buffer of size ASN_OIDSTRLEN */
150 char *asn_oid2str_r(const struct asn_oid *, char *);
151 
152 /* format an OID into a private static buffer */
153 char *asn_oid2str(const struct asn_oid *);
154 
155 enum {
156 	ASN_TYPE_BOOLEAN	= 0x01,
157 	ASN_TYPE_INTEGER	= 0x02,
158 	ASN_TYPE_BITSTRING	= 0x03,
159 	ASN_TYPE_OCTETSTRING	= 0x04,
160 	ASN_TYPE_NULL		= 0x05,
161 	ASN_TYPE_OBJID		= 0x06,
162 	ASN_TYPE_SEQUENCE	= 0x10,
163 
164 	ASN_TYPE_CONSTRUCTED	= 0x20,
165 	ASN_CLASS_UNIVERSAL	= 0x00,
166 	ASN_CLASS_APPLICATION	= 0x40,
167 	ASN_CLASS_CONTEXT	= 0x80,
168 	ASN_CLASS_PRIVATE	= 0xc0,
169 	ASN_TYPE_MASK		= 0x1f,
170 
171 	ASN_APP_IPADDRESS	= 0x00,
172 	ASN_APP_COUNTER		= 0x01,
173 	ASN_APP_GAUGE		= 0x02,
174 	ASN_APP_TIMETICKS	= 0x03,
175 	ASN_APP_OPAQUE		= 0x04,	/* not implemented */
176 	ASN_APP_COUNTER64	= 0x06,
177 
178 	ASN_EXCEPT_NOSUCHOBJECT	= 0x00,
179 	ASN_EXCEPT_NOSUCHINSTANCE = 0x01,
180 	ASN_EXCEPT_ENDOFMIBVIEW	= 0x02,
181 };
182 
183 #endif
184