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