1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_ASN1_H
28 #define	_ASN1_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 /*
35  * ASN.1 values are encoded as octet strings based on the use of a
36  * Type-Length-Value (TLV) structure. The Type indicates the ASN.1
37  * type, the class of the type, and whether the encoding is primitive
38  * or constructed. The Length indicates the length of the actual value
39  * representation and the Value represents the value as a string
40  * of octets.
41  *
42  *              +------------+--------+----------+
43  *              | Identifier | Length | Contents |
44  *              +------------+--------+----------+
45  *
46  * The encoding of the Identifier field is shown below (for tags less than 31):
47  *
48  *              +-------+-----+------------+
49  *              | Class | P/C | Tag number |
50  *              +-------+-----+------------+
51  *          Bit   7   6    5   4  3  2  1  0
52  *
53  * The class field specifies one of four classes, the P/C bit specifies
54  * whether this is a primitive/constructed encoding and the tag number
55  * distinguishes one data type from another within the class.
56  */
57 
58 /*
59  * Identifier classes
60  */
61 #define	ASN_UNIVERSAL		((uchar_t)0x00)
62 #define	ASN_APPLICATION		((uchar_t)0x40)
63 #define	ASN_CONTEXT		((uchar_t)0x80)
64 #define	ASN_PRIVATE		((uchar_t)0xc0)
65 
66 /*
67  * Encoding type
68  */
69 #define	ASN_PRIMITIVE		((uchar_t)0x00)
70 #define	ASN_CONSTRUCTOR		((uchar_t)0x20)
71 
72 /*
73  * Tag numbers for the Universal class of ASN.1 values
74  */
75 #define	ASN_BOOLEAN		((uchar_t)0x01)
76 #define	ASN_INTEGER		((uchar_t)0x02)
77 #define	ASN_BIT_STR		((uchar_t)0x03)
78 #define	ASN_OCTET_STR		((uchar_t)0x04)
79 #define	ASN_NULL		((uchar_t)0x05)
80 #define	ASN_OBJECT_ID		((uchar_t)0x06)
81 #define	ASN_SEQUENCE		((uchar_t)0x10)
82 #define	ASN_SET			((uchar_t)0x11)
83 
84 /*
85  * ASN Extension Tag in the identifier
86  */
87 #define	ASN_EXT_TAG		((uchar_t)0x1f)
88 
89 /*
90  * Application class ASN.1 identifiers
91  */
92 #define	ASN_COUNTER	(ASN_APPLICATION | ASN_PRIMITIVE | (uchar_t)0x01)
93 #define	ASN_TIMETICKS	(ASN_APPLICATION | ASN_PRIMITIVE | (uchar_t)0x03)
94 
95 /*
96  * The Length field in the TLV structure described above is represented
97  * in many ways depending on the value.
98  *
99  * If the length is less than 128, the length field consists of a
100  * single octet beginning with a zero.
101  *
102  *                        +---+-----------+
103  *                        | 0 | Length(L) |
104  *                        +---+-----------+
105  *
106  * If the length is greater than 127, the first octet of the length field
107  * contains a seven-bit integer that specifies the number of additional
108  * length octets and the additional octets specify the actual length.
109  *
110  *              <-- one octet --><----- K octets ----->
111  *              +---------------+---------------------+
112  *              |  1  |    K    |      Length(L)      |
113  *              +---------------+---------------------+
114  *
115  */
116 #define	ASN_LONG_LEN	((uchar_t)0x80)
117 #define	ASN_BIT8	((uchar_t)0x80)
118 
119 /*
120  * Some parts of the code assumes a few things -- big-endian ordering,
121  * sizeof int, etc. to simplify things.
122  */
123 #define	BUILD_INT_SHIFT	23
124 #define	BUILD_INT_MASK	0x1ff
125 
126 /*
127  * Exported ASN.1 encoding related interfaces (only exported within
128  * snmplib, we need to do ld versioning to limit the scope of these to
129  * within snmplib).
130  */
131 uchar_t	*asn_build_sequence(uchar_t *, size_t *, uchar_t, size_t);
132 uchar_t	*asn_build_header(uchar_t *, size_t *, uchar_t, size_t);
133 uchar_t	*asn_build_length(uchar_t *, size_t *, size_t);
134 uchar_t	*asn_build_int(uchar_t *, size_t *, uchar_t, int);
135 uchar_t	*asn_build_string(uchar_t *, size_t *, uchar_t, uchar_t *, size_t);
136 uchar_t	*asn_build_objid(uchar_t *, size_t *, uchar_t, void *, size_t);
137 uchar_t	*asn_build_null(uchar_t *, size_t *, uchar_t);
138 
139 uchar_t	*asn_parse_sequence(uchar_t *, size_t *, uchar_t);
140 uchar_t	*asn_parse_header(uchar_t *, size_t *, uchar_t *);
141 uchar_t	*asn_parse_length(uchar_t *, size_t *);
142 uchar_t	*asn_parse_int(uchar_t *, size_t *, int *);
143 uchar_t *asn_parse_uint(uchar_t *, size_t *, uint_t *);
144 uchar_t	*asn_parse_string(uchar_t *, size_t *, uchar_t **, size_t *);
145 uchar_t	*asn_parse_objid(uchar_t *, size_t *, void *, size_t *);
146 uchar_t	*asn_parse_objval(uchar_t *, size_t *, void *);
147 
148 #ifdef	__cplusplus
149 }
150 #endif
151 
152 #endif	/* _ASN1_H */
153