12 * Definitions for Abstract Syntax Notation One, ASN.1
13 * As defined in ISO/IS 8824 and ISO/IS 8825
17 /***********************************************************
18 Copyright 1988, 1989 by Carnegie Mellon University
22 Permission to use, copy, modify, and distribute this software and its
23 documentation for any purpose and without fee is hereby granted,
24 provided that the above copyright notice appear in all copies and that
25 both that copyright notice and this permission notice appear in
26 supporting documentation, and that the name of CMU not be
27 used in advertising or publicity pertaining to distribution of the
28 software without specific, written prior permission.
30 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
31 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
32 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
33 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
34 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
35 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
37 ******************************************************************/
40 #ifndef MAX_SUBID /* temporary - duplicate definition protection */
41 #ifndef EIGHTBIT_SUBIDS
43 #define MAX_SUBID 0xFFFFFFFF
46 #define MAX_SUBID 0xFF
51 #define MAX_OID_LEN 128 /* max subid's in an oid */
52 #ifndef MAX_NAME_LEN /* conflicts with some libraries */
53 #define MAX_NAME_LEN MAX_OID_LEN /* obsolete. use MAX_OID_LEN */
56 #define OID_LENGTH(x) (sizeof(x)/sizeof(oid))
59 #define ASN_BOOLEAN ((u_char)0x01)
60 #define ASN_INTEGER ((u_char)0x02)
61 #define ASN_BIT_STR ((u_char)0x03)
62 #define ASN_OCTET_STR ((u_char)0x04)
63 #define ASN_NULL ((u_char)0x05)
64 #define ASN_OBJECT_ID ((u_char)0x06)
65 #define ASN_SEQUENCE ((u_char)0x10)
66 #define ASN_SET ((u_char)0x11)
68 #define ASN_UNIVERSAL ((u_char)0x00)
69 #define ASN_APPLICATION ((u_char)0x40)
70 #define ASN_CONTEXT ((u_char)0x80)
71 #define ASN_PRIVATE ((u_char)0xC0)
73 #define ASN_PRIMITIVE ((u_char)0x00)
74 #define ASN_CONSTRUCTOR ((u_char)0x20)
76 #define ASN_LONG_LEN (0x80)
77 #define ASN_EXTENSION_ID (0x1F)
78 #define ASN_BIT8 (0x80)
80 #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
81 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
88 #ifdef OPAQUE_SPECIAL_TYPES
89 typedef struct counter64 integer64;
90 typedef struct counter64 unsigned64;
93 * The BER inside an OPAQUE is an context specific with a value of 48 (0x30)
94 * plus the "normal" tag. For a Counter64, the tag is 0x46 (i.e., an
95 * applications specific tag with value 6). So the value for a 64 bit
96 * counter is 0x46 + 0x30, or 0x76 (118 base 10). However, values
97 * greater than 30 can not be encoded in one octet. So the first octet
98 * has the class, in this case context specific (ASN_CONTEXT), and
99 * the special value (i.e., 31) to indicate that the real value follows
100 * in one or more octets. The high order bit of each following octet
101 * indicates if the value is encoded in additional octets. A high order
102 * bit of zero, indicates the last. For this "hack", only one octet
103 * will be used for the value.
107 * first octet of the tag
109 #define ASN_OPAQUE_TAG1 (ASN_CONTEXT | ASN_EXTENSION_ID)
111 * base value for the second octet of the tag - the
112 * second octet was the value for the tag
114 #define ASN_OPAQUE_TAG2 ((u_char)0x30)
116 #define ASN_OPAQUE_TAG2U ((u_char)0x2f) /* second octet of tag for union */
119 * All the ASN.1 types for SNMP "should have been" defined in this file,
120 * but they were not. (They are defined in snmp_impl.h) Thus, the tag for
121 * Opaque and Counter64 is defined, again, here with a different names.
123 #define ASN_APP_OPAQUE (ASN_APPLICATION | 4)
124 #define ASN_APP_COUNTER64 (ASN_APPLICATION | 6)
125 #define ASN_APP_FLOAT (ASN_APPLICATION | 8)
126 #define ASN_APP_DOUBLE (ASN_APPLICATION | 9)
127 #define ASN_APP_I64 (ASN_APPLICATION | 10)
128 #define ASN_APP_U64 (ASN_APPLICATION | 11)
129 #define ASN_APP_UNION (ASN_PRIVATE | 1) /* or ASN_PRIV_UNION ? */
132 * value for Counter64
134 #define ASN_OPAQUE_COUNTER64 (ASN_OPAQUE_TAG2 + ASN_APP_COUNTER64)
136 * max size of BER encoding of Counter64
138 #define ASN_OPAQUE_COUNTER64_MX_BER_LEN 12
143 #define ASN_OPAQUE_FLOAT (ASN_OPAQUE_TAG2 + ASN_APP_FLOAT)
145 * size of BER encoding of Float
147 #define ASN_OPAQUE_FLOAT_BER_LEN 7
152 #define ASN_OPAQUE_DOUBLE (ASN_OPAQUE_TAG2 + ASN_APP_DOUBLE)
154 * size of BER encoding of Double
156 #define ASN_OPAQUE_DOUBLE_BER_LEN 11
159 * value for Integer64
161 #define ASN_OPAQUE_I64 (ASN_OPAQUE_TAG2 + ASN_APP_I64)
163 * max size of BER encoding of Integer64
165 #define ASN_OPAQUE_I64_MX_BER_LEN 11
168 * value for Unsigned64
170 #define ASN_OPAQUE_U64 (ASN_OPAQUE_TAG2 + ASN_APP_U64)
172 * max size of BER encoding of Unsigned64
174 #define ASN_OPAQUE_U64_MX_BER_LEN 12
176 #endif /* OPAQUE_SPECIAL_TYPES */
179 #define ASN_PRIV_INCL_RANGE (ASN_PRIVATE | 2)
180 #define ASN_PRIV_EXCL_RANGE (ASN_PRIVATE | 3)
181 #define ASN_PRIV_DELEGATED (ASN_PRIVATE | 5)
182 #define ASN_PRIV_IMPLIED_OCTET_STR (ASN_PRIVATE | ASN_OCTET_STR) /* 4 */
183 #define ASN_PRIV_IMPLIED_OBJECT_ID (ASN_PRIVATE | ASN_OBJECT_ID) /* 6 */
184 #define ASN_PRIV_RETRY (ASN_PRIVATE | 7)
185 #define IS_DELEGATED(x) ((x) == ASN_PRIV_DELEGATED)
188 int asn_check_packet(u_char *, size_t);
189 u_char *asn_parse_int(u_char *, size_t *, u_char *, long *,
191 u_char *asn_build_int(u_char *, size_t *, u_char, long *,
193 u_char *asn_parse_unsigned_int(u_char *, size_t *, u_char *,
195 u_char *asn_build_unsigned_int(u_char *, size_t *, u_char,
197 u_char *asn_parse_string(u_char *, size_t *, u_char *,
199 u_char *asn_build_string(u_char *, size_t *, u_char,
200 const u_char *, size_t);
201 u_char *asn_parse_header(u_char *, size_t *, u_char *);
202 u_char *asn_parse_sequence(u_char *, size_t *, u_char *, u_char expected_type, /* must be this type */
203 const char *estr); /* error message prefix */
204 u_char *asn_build_header(u_char *, size_t *, u_char, size_t);
205 u_char *asn_build_sequence(u_char *, size_t *, u_char, size_t);
206 u_char *asn_parse_length(u_char *, u_long *);
207 u_char *asn_build_length(u_char *, size_t *, size_t);
208 u_char *asn_parse_objid(u_char *, size_t *, u_char *, oid *,
210 u_char *asn_build_objid(u_char *, size_t *, u_char, oid *,
212 u_char *asn_parse_null(u_char *, size_t *, u_char *);
213 u_char *asn_build_null(u_char *, size_t *, u_char);
214 u_char *asn_parse_bitstring(u_char *, size_t *, u_char *,
216 u_char *asn_build_bitstring(u_char *, size_t *, u_char,
218 u_char *asn_parse_unsigned_int64(u_char *, size_t *, u_char *,
219 struct counter64 *, size_t);
220 u_char *asn_build_unsigned_int64(u_char *, size_t *, u_char,
221 struct counter64 *, size_t);
222 u_char *asn_parse_signed_int64(u_char *, size_t *, u_char *,
223 struct counter64 *, size_t);
224 u_char *asn_build_signed_int64(u_char *, size_t *, u_char,
225 struct counter64 *, size_t);
226 u_char *asn_build_float(u_char *, size_t *, u_char, float *,
228 u_char *asn_parse_float(u_char *, size_t *, u_char *, float *,
230 u_char *asn_build_double(u_char *, size_t *, u_char, double *,
232 u_char *asn_parse_double(u_char *, size_t *, u_char *,
235 #ifdef USE_REVERSE_ASNENCODING
238 * Re-allocator function for below.
241 int asn_realloc(u_char **, size_t *);
244 * Re-allocating reverse ASN.1 encoder functions. Synopsis:
246 * u_char *buf = (u_char*)malloc(100);
247 * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
248 * size_t buf_len = 100, offset = 0;
250 * int allow_realloc = 1;
252 * if (asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
253 * type, &data, sizeof(long)) == 0) {
257 * NOTE WELL: after calling one of these functions with allow_realloc
258 * non-zero, buf might have moved, buf_len might have grown and
259 * offset will have increased by the size of the encoded data.
260 * You should **NEVER** do something like this:
262 * u_char *buf = (u_char *)malloc(100), *ptr;
263 * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
264 * size_t buf_len = 100, offset = 0;
265 * long data1 = 1234, data2 = 5678;
266 * int rc = 0, allow_realloc = 1;
268 * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
269 * type, &data1, sizeof(long));
270 * ptr = buf[buf_len - offset]; / * points at encoding of data1 * /
274 * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
275 * type, &data2, sizeof(long));
276 * make use of ptr here;
279 * ptr is **INVALID** at this point. In general, you should store the
280 * offset value and compute pointers when you need them:
284 * u_char *buf = (u_char *)malloc(100), *ptr;
285 * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
286 * size_t buf_len = 100, offset = 0, ptr_offset;
287 * long data1 = 1234, data2 = 5678;
288 * int rc = 0, allow_realloc = 1;
290 * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
291 * type, &data1, sizeof(long));
292 * ptr_offset = offset;
296 * rc = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
297 * type, &data2, sizeof(long));
298 * ptr = buf + buf_len - ptr_offset
299 * make use of ptr here;
303 * Here, you can see that ptr will be a valid pointer even if the block of
304 * memory has been moved, as it may well have been. Plenty of examples of
305 * usage all over asn1.c, snmp_api.c, snmpusm.c.
307 * The other thing you should **NEVER** do is to pass a pointer to a buffer
308 * on the stack as the first argument when allow_realloc is non-zero, unless
309 * you really know what you are doing and your machine/compiler allows you to
310 * free non-heap memory. There are rumours that such things exist, but many
311 * consider them no more than the wild tales of a fool.
313 * Of course, you can pass allow_realloc as zero, to indicate that you do not
314 * wish the packet buffer to be reallocated for some reason; perhaps because
315 * it is on the stack. This may be useful to emulate the functionality of
318 * u_char my_static_buffer[100], *cp = NULL;
319 * size_t my_static_buffer_len = 100;
320 * float my_pi = (float)22/(float)7;
322 * cp = asn_rbuild_float(my_static_buffer, &my_static_buffer_len,
323 * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
332 * u_char my_static_buffer[100];
333 * size_t my_static_buffer_len = 100, my_offset = 0;
334 * float my_pi = (float)22/(float)7;
337 * rc = asn_realloc_rbuild_float(&my_static_buffer, &my_static_buffer_len,
339 * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
348 int asn_realloc_rbuild_int(u_char ** pkt, size_t * pkt_len,
350 int allow_realloc, u_char type,
351 long *data, size_t data_size);
353 int asn_realloc_rbuild_string(u_char ** pkt,
361 int asn_realloc_rbuild_unsigned_int(u_char ** pkt,
369 int asn_realloc_rbuild_header(u_char ** pkt,
376 int asn_realloc_rbuild_sequence(u_char ** pkt,
383 int asn_realloc_rbuild_length(u_char ** pkt,
389 int asn_realloc_rbuild_objid(u_char ** pkt,
393 u_char type, const oid *,
396 int asn_realloc_rbuild_null(u_char ** pkt,
402 int asn_realloc_rbuild_bitstring(u_char ** pkt,
410 int asn_realloc_rbuild_unsigned_int64(u_char ** pkt,
418 int asn_realloc_rbuild_signed_int64(u_char ** pkt,
423 struct counter64 *data,
426 int asn_realloc_rbuild_float(u_char ** pkt,
430 u_char type, float *data,
433 int asn_realloc_rbuild_double(u_char ** pkt,
437 u_char type, double *data,