1122394Sharti/*
2122394Sharti * Copyright (c) 2001-2003
3122394Sharti *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4122394Sharti *	All rights reserved.
5122394Sharti *
6122394Sharti * Author: Harti Brandt <harti@freebsd.org>
7310901Sngie *
8133211Sharti * Redistribution and use in source and binary forms, with or without
9133211Sharti * modification, are permitted provided that the following conditions
10133211Sharti * are met:
11133211Sharti * 1. Redistributions of source code must retain the above copyright
12133211Sharti *    notice, this list of conditions and the following disclaimer.
13122394Sharti * 2. Redistributions in binary form must reproduce the above copyright
14122394Sharti *    notice, this list of conditions and the following disclaimer in the
15122394Sharti *    documentation and/or other materials provided with the distribution.
16310901Sngie *
17133211Sharti * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18133211Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19133211Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20133211Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21133211Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22133211Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23133211Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24133211Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25133211Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26133211Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27133211Sharti * SUCH DAMAGE.
28122394Sharti *
29156066Sharti * $Begemot: bsnmp/lib/asn1.h,v 1.20 2005/10/05 16:43:11 brandt_h Exp $
30122394Sharti *
31122394Sharti * ASN.1 for SNMP
32122394Sharti */
33122394Sharti#ifndef asn1_h_
34122394Sharti#define asn1_h_
35122394Sharti
36122394Sharti#include <sys/types.h>
37122394Sharti
38122394Shartistruct asn_buf {
39122394Sharti	union {
40122394Sharti		u_char	*ptr;
41122394Sharti		const u_char *cptr;
42122394Sharti	}	asn_u;
43122394Sharti	size_t	asn_len;
44122394Sharti};
45122394Sharti#define asn_cptr	asn_u.cptr
46122394Sharti#define asn_ptr	asn_u.ptr
47122394Sharti
48122394Sharti/* these restrictions are in the SMI */
49122394Sharti#define ASN_MAXID	0xffffffff
50122394Sharti#define ASN_MAXOIDLEN	128
51122394Sharti
52122394Sharti/* the string needed for this (with trailing zero) */
53122394Sharti#define ASN_OIDSTRLEN	(ASN_MAXOIDLEN * (10 + 1) - 1 + 1)
54122394Sharti
55122394Sharti/* type of subidentifiers */
56133211Shartitypedef uint32_t asn_subid_t;
57122394Sharti
58122394Shartistruct asn_oid {
59122394Sharti	u_int	len;
60122394Sharti	asn_subid_t subs[ASN_MAXOIDLEN];
61122394Sharti};
62122394Sharti
63122394Shartienum asn_err {
64122394Sharti	/* conversion was ok */
65122394Sharti	ASN_ERR_OK	= 0,
66122394Sharti	/* conversion failed and stopped */
67122394Sharti	ASN_ERR_FAILED	= 1 | 0x1000,
68122394Sharti	/* length field bad, value skipped */
69122394Sharti	ASN_ERR_BADLEN	= 2,
70122394Sharti	/* out of buffer, stopped */
71122394Sharti	ASN_ERR_EOBUF	= 3 | 0x1000,
72122394Sharti	/* length ok, but value is out of range */
73122394Sharti	ASN_ERR_RANGE	= 4,
74122394Sharti	/* not the expected tag, stopped */
75122394Sharti	ASN_ERR_TAG	= 5 | 0x1000,
76122394Sharti};
77122394Sharti#define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
78122394Sharti
79122394Sharti/* type for the length field of encoded values. The length is restricted
80133211Sharti * to 65535, but using uint16_t would give conversion warnings on gcc */
81133211Shartitypedef uint32_t asn_len_t;	/* could be also uint16_t */
82122394Sharti
83122394Sharti/* maximal length of a long length field without the length of the length */
84122394Sharti#define ASN_MAXLEN	65535
85122394Sharti#define ASN_MAXLENLEN	2	/* number of bytes in a length */
86122394Sharti
87122394Sharti/* maximum size of an octet string as per SMIv2 */
88122394Sharti#define ASN_MAXOCTETSTRING 65535
89122394Sharti
90122394Shartiextern void (*asn_error)(const struct asn_buf *, const char *, ...);
91122394Sharti
92122394Shartienum asn_err asn_get_header(struct asn_buf *, u_char *, asn_len_t *);
93122394Shartienum asn_err asn_put_header(struct asn_buf *, u_char, asn_len_t);
94122394Sharti
95122394Shartienum asn_err asn_put_temp_header(struct asn_buf *, u_char, u_char **);
96216294Ssyrinxenum asn_err asn_commit_header(struct asn_buf *, u_char *, size_t *);
97122394Sharti
98122394Shartienum asn_err asn_get_integer_raw(struct asn_buf *, asn_len_t, int32_t *);
99122394Shartienum asn_err asn_get_integer(struct asn_buf *, int32_t *);
100122394Shartienum asn_err asn_put_integer(struct asn_buf *, int32_t);
101122394Sharti
102122394Shartienum asn_err asn_get_octetstring_raw(struct asn_buf *, asn_len_t, u_char *, u_int *);
103122394Shartienum asn_err asn_get_octetstring(struct asn_buf *, u_char *, u_int *);
104122394Shartienum asn_err asn_put_octetstring(struct asn_buf *, const u_char *, u_int);
105122394Sharti
106122394Shartienum asn_err asn_get_null_raw(struct asn_buf *b, asn_len_t);
107122394Shartienum asn_err asn_get_null(struct asn_buf *);
108122394Shartienum asn_err asn_put_null(struct asn_buf *);
109122394Sharti
110122394Shartienum asn_err asn_put_exception(struct asn_buf *, u_int);
111122394Sharti
112122394Shartienum asn_err asn_get_objid_raw(struct asn_buf *, asn_len_t, struct asn_oid *);
113122394Shartienum asn_err asn_get_objid(struct asn_buf *, struct asn_oid *);
114122394Shartienum asn_err asn_put_objid(struct asn_buf *, const struct asn_oid *);
115122394Sharti
116122394Shartienum asn_err asn_get_sequence(struct asn_buf *, asn_len_t *);
117122394Sharti
118122394Shartienum asn_err asn_get_ipaddress_raw(struct asn_buf *, asn_len_t, u_char *);
119122394Shartienum asn_err asn_get_ipaddress(struct asn_buf *, u_char *);
120122394Shartienum asn_err asn_put_ipaddress(struct asn_buf *, const u_char *);
121122394Sharti
122133211Shartienum asn_err asn_get_uint32_raw(struct asn_buf *, asn_len_t, uint32_t *);
123133211Shartienum asn_err asn_put_uint32(struct asn_buf *, u_char, uint32_t);
124122394Sharti
125133211Shartienum asn_err asn_get_counter64_raw(struct asn_buf *, asn_len_t, uint64_t *);
126133211Shartienum asn_err asn_put_counter64(struct asn_buf *, uint64_t);
127122394Sharti
128133211Shartienum asn_err asn_get_timeticks(struct asn_buf *, uint32_t *);
129133211Shartienum asn_err asn_put_timeticks(struct asn_buf *, uint32_t);
130122394Sharti
131122394Shartienum asn_err asn_skip(struct asn_buf *, asn_len_t);
132216294Ssyrinxenum asn_err asn_pad(struct asn_buf *, asn_len_t);
133122394Sharti
134122394Sharti/*
135122394Sharti * Utility functions for OIDs
136122394Sharti */
137122394Sharti/* get a sub-OID from the middle of another OID */
138122394Shartivoid asn_slice_oid(struct asn_oid *, const struct asn_oid *, u_int, u_int);
139122394Sharti
140122394Sharti/* append an OID to another one */
141122394Shartivoid asn_append_oid(struct asn_oid *, const struct asn_oid *);
142122394Sharti
143122394Sharti/* compare two OIDs */
144122394Shartiint asn_compare_oid(const struct asn_oid *, const struct asn_oid *);
145122394Sharti
146122394Sharti/* check whether the first is a suboid of the second one */
147122394Shartiint asn_is_suboid(const struct asn_oid *, const struct asn_oid *);
148122394Sharti
149122394Sharti/* format an OID into a user buffer of size ASN_OIDSTRLEN */
150122394Shartichar *asn_oid2str_r(const struct asn_oid *, char *);
151122394Sharti
152122394Sharti/* format an OID into a private static buffer */
153122394Shartichar *asn_oid2str(const struct asn_oid *);
154122394Sharti
155122394Shartienum {
156122394Sharti	ASN_TYPE_BOOLEAN	= 0x01,
157122394Sharti	ASN_TYPE_INTEGER	= 0x02,
158122394Sharti	ASN_TYPE_BITSTRING	= 0x03,
159122394Sharti	ASN_TYPE_OCTETSTRING	= 0x04,
160122394Sharti	ASN_TYPE_NULL		= 0x05,
161122394Sharti	ASN_TYPE_OBJID		= 0x06,
162122394Sharti	ASN_TYPE_SEQUENCE	= 0x10,
163122394Sharti
164122394Sharti	ASN_TYPE_CONSTRUCTED	= 0x20,
165122394Sharti	ASN_CLASS_UNIVERSAL	= 0x00,
166122394Sharti	ASN_CLASS_APPLICATION	= 0x40,
167122394Sharti	ASN_CLASS_CONTEXT	= 0x80,
168122394Sharti	ASN_CLASS_PRIVATE	= 0xc0,
169122394Sharti	ASN_TYPE_MASK		= 0x1f,
170122394Sharti
171122394Sharti	ASN_APP_IPADDRESS	= 0x00,
172122394Sharti	ASN_APP_COUNTER		= 0x01,
173122394Sharti	ASN_APP_GAUGE		= 0x02,
174122394Sharti	ASN_APP_TIMETICKS	= 0x03,
175122394Sharti	ASN_APP_OPAQUE		= 0x04,	/* not implemented */
176122394Sharti	ASN_APP_COUNTER64	= 0x06,
177122394Sharti
178122394Sharti	ASN_EXCEPT_NOSUCHOBJECT	= 0x00,
179122394Sharti	ASN_EXCEPT_NOSUCHINSTANCE = 0x01,
180122394Sharti	ASN_EXCEPT_ENDOFMIBVIEW	= 0x02,
181122394Sharti};
182122394Sharti
183122394Sharti#endif
184