xref: /illumos-gate/usr/src/cmd/picl/plugins/sun4v/lib/snmp/asn1.h (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 /*
37  * ASN.1 values are encoded as octet strings based on the use of a
38  * Type-Length-Value (TLV) structure. The Type indicates the ASN.1
39  * type, the class of the type, and whether the encoding is primitive
40  * or constructed. The Length indicates the length of the actual value
41  * representation and the Value represents the value as a string
42  * of octets.
43  *
44  *              +------------+--------+----------+
45  *              | Identifier | Length | Contents |
46  *              +------------+--------+----------+
47  *
48  * The encoding of the Identifier field is shown below (for tags less than 31):
49  *
50  *              +-------+-----+------------+
51  *              | Class | P/C | Tag number |
52  *              +-------+-----+------------+
53  *          Bit   7   6    5   4  3  2  1  0
54  *
55  * The class field specifies one of four classes, the P/C bit specifies
56  * whether this is a primitive/constructed encoding and the tag number
57  * distinguishes one data type from another within the class.
58  */
59 
60 /*
61  * Identifier classes
62  */
63 #define	ASN_UNIVERSAL		((uchar_t)0x00)
64 #define	ASN_APPLICATION		((uchar_t)0x40)
65 #define	ASN_CONTEXT		((uchar_t)0x80)
66 #define	ASN_PRIVATE		((uchar_t)0xc0)
67 
68 /*
69  * Encoding type
70  */
71 #define	ASN_PRIMITIVE		((uchar_t)0x00)
72 #define	ASN_CONSTRUCTOR		((uchar_t)0x20)
73 
74 /*
75  * Tag numbers for the Universal class of ASN.1 values
76  */
77 #define	ASN_BOOLEAN		((uchar_t)0x01)
78 #define	ASN_INTEGER		((uchar_t)0x02)
79 #define	ASN_BIT_STR		((uchar_t)0x03)
80 #define	ASN_OCTET_STR		((uchar_t)0x04)
81 #define	ASN_NULL		((uchar_t)0x05)
82 #define	ASN_OBJECT_ID		((uchar_t)0x06)
83 #define	ASN_SEQUENCE		((uchar_t)0x10)
84 #define	ASN_SET			((uchar_t)0x11)
85 
86 /*
87  * ASN Extension Tag in the identifier
88  */
89 #define	ASN_EXT_TAG		((uchar_t)0x1f)
90 
91 /*
92  * Application class ASN.1 identifiers
93  */
94 #define	ASN_COUNTER	(ASN_APPLICATION | ASN_PRIMITIVE | (uchar_t)0x01)
95 #define	ASN_TIMETICKS	(ASN_APPLICATION | ASN_PRIMITIVE | (uchar_t)0x03)
96 
97 /*
98  * The Length field in the TLV structure described above is represented
99  * in many ways depending on the value.
100  *
101  * If the length is less than 128, the length field consists of a
102  * single octet beginning with a zero.
103  *
104  *                        +---+-----------+
105  *                        | 0 | Length(L) |
106  *                        +---+-----------+
107  *
108  * If the length is greater than 127, the first octet of the length field
109  * contains a seven-bit integer that specifies the number of additional
110  * length octets and the additional octets specify the actual length.
111  *
112  *              <-- one octet --><----- K octets ----->
113  *              +---------------+---------------------+
114  *              |  1  |    K    |      Length(L)      |
115  *              +---------------+---------------------+
116  *
117  */
118 #define	ASN_LONG_LEN	((uchar_t)0x80)
119 #define	ASN_BIT8	((uchar_t)0x80)
120 
121 /*
122  * Some parts of the code assumes a few things -- big-endian ordering,
123  * sizeof int, etc. to simplify things.
124  */
125 #define	BUILD_INT_SHIFT	23
126 #define	BUILD_INT_MASK	0x1ff
127 
128 /*
129  * Exported ASN.1 encoding related interfaces (only exported within
130  * snmplib, we need to do ld versioning to limit the scope of these to
131  * within snmplib).
132  */
133 uchar_t	*asn_build_sequence(uchar_t *, size_t *, uchar_t, size_t);
134 uchar_t	*asn_build_header(uchar_t *, size_t *, uchar_t, size_t);
135 uchar_t	*asn_build_length(uchar_t *, size_t *, size_t);
136 uchar_t	*asn_build_int(uchar_t *, size_t *, uchar_t, int);
137 uchar_t	*asn_build_string(uchar_t *, size_t *, uchar_t, uchar_t *, size_t);
138 uchar_t	*asn_build_objid(uchar_t *, size_t *, uchar_t, void *, size_t);
139 uchar_t	*asn_build_null(uchar_t *, size_t *, uchar_t);
140 
141 uchar_t	*asn_parse_sequence(uchar_t *, size_t *, uchar_t);
142 uchar_t	*asn_parse_header(uchar_t *, size_t *, uchar_t *);
143 uchar_t	*asn_parse_length(uchar_t *, size_t *);
144 uchar_t	*asn_parse_int(uchar_t *, size_t *, int *);
145 uchar_t *asn_parse_uint(uchar_t *, size_t *, uint_t *);
146 uchar_t	*asn_parse_string(uchar_t *, size_t *, uchar_t **, size_t *);
147 uchar_t	*asn_parse_objid(uchar_t *, size_t *, void *, size_t *);
148 uchar_t	*asn_parse_objval(uchar_t *, size_t *, void *);
149 
150 #ifdef	__cplusplus
151 }
152 #endif
153 
154 #endif	/* _ASN1_H */
155