2 .\" Copyright (c) 2004-2005
4 .\" All rights reserved.
5 .\" Copyright (c) 2001-2003
6 .\" Fraunhofer Institute for Open Communication Systems (FhG Fokus).
7 .\" All rights reserved.
9 .\" Author: Harti Brandt <harti@FreeBSD.org>
11 .\" Redistribution and use in source and binary forms, with or without
12 .\" modification, are permitted provided that the following conditions
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\" notice, this list of conditions and the following disclaimer.
16 .\" 2. Redistributions in binary form must reproduce the above copyright
17 .\" notice, this list of conditions and the following disclaimer in the
18 .\" documentation and/or other materials provided with the distribution.
20 .\" THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" $Begemot: bsnmp/lib/asn1.3,v 1.9 2005/10/04 08:46:49 brandt_h Exp $
40 .Nm asn_put_temp_header ,
41 .Nm asn_commit_header ,
42 .Nm asn_get_integer_raw ,
45 .Nm asn_get_octetstring_raw ,
46 .Nm asn_get_octetstring ,
47 .Nm asn_put_octetstring ,
48 .Nm asn_get_null_raw ,
51 .Nm asn_put_exception ,
52 .Nm asn_get_objid_raw ,
55 .Nm asn_get_sequence ,
56 .Nm asn_get_ipaddress_raw ,
57 .Nm asn_get_ipaddress ,
58 .Nm asn_put_ipaddress ,
59 .Nm asn_get_uint32_raw ,
61 .Nm asn_get_counter64_raw ,
62 .Nm asn_put_counter64 ,
63 .Nm asn_get_timeticks ,
64 .Nm asn_put_timeticks ,
72 .Nd "ASN.1 library for SNMP"
79 .Fn asn_get_header "struct asn_buf *buf" "u_char *type" "asn_len_t *lenp"
81 .Fn asn_put_header "struct asn_buf *buf" "u_char type" "asn_len_t len"
83 .Fn asn_put_temp_header "struct asn_buf *buf" "u_char type" "u_char **ptr"
85 .Fn asn_commit_header "struct asn_buf *buf" "u_char *ptr"
87 .Fn asn_get_integer_raw "struct asn_buf *buf" "asn_len_t len" "int32_t *res"
89 .Fn asn_get_integer "struct asn_buf *buf" "int32_t *res"
91 .Fn asn_put_integer "struct asn_buf *buf" "int32_t arg"
93 .Fn asn_get_octetstring_raw "struct asn_buf *buf" "asn_len_t len" "u_char *out" "u_int *outsize"
95 .Fn asn_get_octetstring "struct asn_buf *buf" "u_char *out" "u_int *outsize"
97 .Fn asn_put_octetstring "struct asn_buf *buf" "const u_char *str" "u_int strsize"
99 .Fn asn_get_null_raw "struct asn_buf *buf" "asn_len_t len"
101 .Fn asn_get_null "struct asn_buf *buf"
103 .Fn asn_put_null "struct asn_buf *buf"
105 .Fn asn_put_exception "struct asn_buf *buf" "u_int type"
107 .Fn asn_get_objid_raw "struct asn_buf *buf" "asn_len_t len" "struct asn_oid *oid"
109 .Fn asn_get_objid "struct asn_buf *buf" "struct asn_oid *oid"
111 .Fn asn_put_objid "struct asn_buf *buf" "const struct asn_oid *oid"
113 .Fn asn_get_sequence "struct asn_buf *buf" "asn_len_t *lenp"
115 .Fn asn_get_ipaddress_raw "struct asn_buf *buf" "asn_len_t len" "u_char *ipa"
117 .Fn asn_get_ipaddress "struct asn_buf *buf" "u_char *ipa"
119 .Fn asn_put_ipaddress "struct asn_buf *buf" "const u_char *ipa"
121 .Fn asn_get_uint32_raw "struct asn_buf *buf" "asn_len_t len" "u_int32_t *res"
123 .Fn asn_put_uint32 "struct asn_buf *buf" "u_char type" "u_int32_t val"
125 .Fn asn_get_counter64_raw "struct asn_buf *buf" "asn_len_t len" "u_int64_t *res"
127 .Fn asn_put_counter64 "struct asn_buf *buf" "u_int64_t val"
129 .Fn asn_get_timeticks "struct asn_buf *buf" "u_int32_t *valp"
131 .Fn asn_put_timeticks "struct asn_buf *buf" "u_int32_t val"
133 .Fn asn_skip "struct asn_buf *buf" "asn_len_t len"
135 .Fn asn_slice_oid "struct asn_oid *dest" "const struct asn_oid *src" "u_int from" "u_int to"
137 .Fn asn_append_oid "struct asn_oid *to" "const struct asn_oid *from"
139 .Fn asn_compare_oid "const struct asn_oid *oid1" "const struct asn_oid *oid2"
141 .Fn asn_is_suboid "const struct asn_oid *oid1" "const struct asn_oid *oid2"
143 .Fn asn_oid2str_r "const struct asn_oid *oid" "char *buf"
145 .Fn asn_oid2str "const struct asn_oid *oid"
147 The ASN.1 library contains routines to handle ASN.1 encoding for SNMP.
148 It supports only the restricted form of ASN.1 as required by SNMP.
149 There are two basic structures used throughout the library:
150 .Bd -literal -offset indent
151 /* these restrictions are in the SMI */
152 #define ASN_MAXID 0xffffffff
153 #define ASN_MAXOIDLEN 128
155 /* type of subidentifiers */
156 typedef u_int32_t asn_subid_t;
160 asn_subid_t subs[ASN_MAXOIDLEN];
164 This structure represents an OID with the restrictions defined in the SNMP
167 holds the current length of the OID and
169 holds the elements of the OID.
170 .Bd -literal -offset indent
178 #define asn_cptr asn_u.cptr
179 #define asn_ptr asn_u.ptr
182 This structure is used to encode and decode ASN.1.
183 It describes the output
184 buffer for encoding routines and the input buffer for decoding routines.
187 holds the number of remaining free octets in the buffer.
188 The first free byte is pointed to by
192 holds the number of remaining bytes to decode.
193 The next byte to decode is pointed to by
196 Most of the functions return an error code
197 .Fa "enum asn_error" :
198 .Bd -literal -offset indent
200 /* conversion was ok */
202 /* conversion failed and stopped */
203 ASN_ERR_FAILED = 1 | 0x1000,
204 /* length field bad, value skipped */
206 /* out of buffer, stopped */
207 ASN_ERR_EOBUF = 3 | 0x1000,
208 /* length ok, but value is out of range */
210 /* not the expected tag, stopped */
211 ASN_ERR_TAG = 5 | 0x1000,
213 #define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
218 returns true, the error was fatal and processing has stopped at the point
223 reads the next header from the input octet stream.
224 It returns the tag in the variable pointed to by
226 (note that only single byte tags are supported) and the decoded length field
227 in the value pointed to by
229 (this is restricted to a unsigned 32-bit value).
230 All errors in this function are fatal and stop processing.
234 writes an ASN.1 header.
236 is the tag to write and is restricted to one byte tags (i.e., tags
237 lesser or equal than 0x30).
239 is the length of the value and is restricted to 16-bit.
242 .Fn asn_put_temp_header
244 .Fn asn_commit_header
245 are used to write a header when the length of the value is not known in
246 advance, for example, for sequences.
247 .Fn asn_put_temp_header
248 writes a header with the given tag
250 and space for the maximum supported length field and sets the pointer pointed
253 to the begin of this length field.
254 This pointer must then be fed into
255 .Fn asn_commit_header
256 directly after writing the value to the buffer.
257 The function will compute the
258 length, insert it into the right place and shift the value if the resulting
259 length field is shorter than the estimated one.
262 .Fn asn_get_integer_raw
263 is used to decode a signed integer value (32-bit).
265 header of the integer has been decoded already.
267 is the length obtained from the ASN.1 header and the integer will be returned
268 in the value pointed to by
273 decodes a complete 32-bit signed integer including the header.
279 encodes a 32-bit signed integer.
282 .Fn asn_get_octetstring_raw
283 decodes the value field of an ASN.1 octet string.
284 The length obtained from the header must be fed into the
288 must point to a buffer to receive the octet string.
289 On entry to the function
291 must point to the size of the buffer.
294 will point to the number of octets decoded (if no error occurs this will be
298 .Fn asn_get_octetstring
299 decodes an octetstring including the header.
301 must point to a buffer to receive the string,
303 must point to the size of the buffer.
304 On exit of the function
306 will point to the number of octets decoded.
308 .Fn asn_put_octetstring
309 encodes an octetstring (including the header).
311 points to the string to encode and
313 is the length of the string (the string may contain embedded
318 decodes a null value.
320 is the length obtained from the header and must be 0.
323 decodes a null including the header and the function
328 .Fn asn_put_exception
329 is used to encode an SNMPv2 exception.
330 The exception type is
334 .Fn asn_get_objid_raw
335 is used to decode an OID value.
337 must be the value length obtained from the header and
339 will receive the decoded OID.
342 decodes a complete OID (including the header) and the function
344 encodes a complete OID.
348 decodes a sequence header.
349 The length of the sequence value will be stored in the value pointed to by
353 .Fn asn_get_ipaddress_raw
354 decodes an IP address value.
356 is the length from the header and must be 4.
358 will receive the decoded IP address and must point to a buffer of at least
361 .Fn asn_get_ipaddress
362 decodes a complete IP address (including the header) and
363 .Fn asn_put_ipaddress
364 encodes an IP address.
367 .Fn asn_get_uint32_raw
368 decodes an unsigned 32-bit integer value.
370 is the length from the header and
372 will get the decoded value.
375 encodes an unsigned 32-bit integer value and inserts the tag given in
380 .Fn asn_get_counter64_raw
381 decodes an unsigned 64-bit integer value.
383 must be the value length from the header.
384 The resulting value is stored into the variable pointed to by
387 .Fn asn_put_counter64
388 encodes a complete unsigned 64-bit value.
391 .Fn asn_get_timeticks
392 decodes an ASN.1 object of type
395 .Fn asn_put_timeticks
396 encodes such an object.
402 bytes in the input buffer.
406 splits a part out from an OID.
407 It takes all the subids from the OID pointed to by
409 starting with the subid at position
411 (the first subid being subid 0) up to, but not including, subid
413 and generates a new OID in
419 the resulting OID will have a length of zero.
427 given that the resulting OID is not too long.
428 If the maximum length is exceeded the result is undefined.
432 compares two oids and returns the values
438 is lesser than, equal, or larger than
446 is equal to the leading part of
448 It returns 0 otherwise.
452 makes a printable string from
454 The buffer pointed to by
456 must be large enough to hold the result.
459 is defined to be the length of the maximum string generated by this function
460 (including the trailing NUL).
463 makes a printable string from
465 into a private buffer that is overwritten by each call.
467 When an error occurs in any of the function the function pointed to
468 by the global pointer
469 .Bd -literal -offset indent
470 extern void (*asn_error)(const struct asn_buf *, const char *, ...);
473 is called with the current buffer (this may be
478 There is a default error handler in the library that prints a message
481 followed by the error message and an optional dump of the buffer.
489 This implementation conforms to the applicable IETF RFCs and ITU-T
492 .An Hartmut Brandt Aq harti@FreeBSD.org