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