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