xref: /freebsd/contrib/bsnmp/lib/asn1.3 (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1.\"
2.\" Copyright (c) 2001-2003
3.\"	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4.\"	All rights reserved.
5.\"
6.\" Author: Harti Brandt <harti@freebsd.org>
7.\"
8.\" Redistribution of this software and documentation and use in source and
9.\" binary forms, with or without modification, are permitted provided that
10.\" the following conditions are met:
11.\"
12.\" 1. Redistributions of source code or documentation must retain the above
13.\"    copyright notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\" 3. Neither the name of the Institute nor the names of its contributors
18.\"    may be used to endorse or promote products derived from this software
19.\"    without specific prior written permission.
20.\"
21.\" THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
22.\" AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24.\" FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
25.\" FRAUNHOFER FOKUS OR ITS CONTRIBUTORS  BE LIABLE FOR ANY DIRECT, INDIRECT,
26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28.\" OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29.\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32.\"
33.\" $Begemot: bsnmp/lib/asn1.3,v 1.4 2004/04/13 17:01:31 novo Exp $
34.\"
35.Dd August 15, 2002
36.Dt asn1 3
37.Os
38.Sh NAME
39.Nm asn_get_header ,
40.Nm asn_put_header ,
41.Nm asn_put_temp_header ,
42.Nm asn_commit_header ,
43.Nm asn_get_integer_raw ,
44.Nm asn_get_integer ,
45.Nm asn_put_integer ,
46.Nm asn_get_octetstring_raw ,
47.Nm asn_get_octetstring ,
48.Nm asn_put_octetstring ,
49.Nm asn_get_null_raw ,
50.Nm asn_get_null ,
51.Nm asn_put_null ,
52.Nm asn_put_exception ,
53.Nm asn_get_objid_raw ,
54.Nm asn_get_objid ,
55.Nm asn_put_objid ,
56.Nm asn_get_sequence ,
57.Nm asn_get_ipaddress_raw ,
58.Nm asn_get_ipaddress ,
59.Nm asn_put_ipaddress ,
60.Nm asn_get_uint32_raw ,
61.Nm asn_put_uint32 ,
62.Nm asn_get_counter64_raw ,
63.Nm asn_put_counter64 ,
64.Nm asn_get_timeticks ,
65.Nm asn_put_timeticks ,
66.Nm asn_skip ,
67.Nm asn_slice_oid ,
68.Nm asn_append_oid ,
69.Nm asn_compare_oid ,
70.Nm asn_is_suboid ,
71.Nm asn_oid2str_r ,
72.Nm asn_oid2str
73.Nd "ASN.1 library for SNMP"
74.Sh LIBRARY
75Begemot SNMP library
76.Pq libbsnmp, -lbsnmp
77.Sh SYNOPSIS
78.In bsnmp/asn1.h
79.Ft enum asn_err
80.Fn asn_get_header "struct asn_buf *buf" "u_char *type" "asn_len_t *lenp"
81.Ft enum asn_err
82.Fn asn_put_header "struct asn_buf *buf" "u_char type" "asn_len_t len"
83.Ft enum asn_err
84.Fn asn_put_temp_header "struct asn_buf *buf" "u_char type" "u_char **ptr"
85.Ft enum asn_err
86.Fn asn_commit_header "struct asn_buf *buf" "u_char *ptr"
87.Ft enum asn_err
88.Fn asn_get_integer_raw "struct asn_buf *buf" "asn_len_t len" "int32_t *res"
89.Ft enum asn_err
90.Fn asn_get_integer "struct asn_buf *buf" "int32_t *res"
91.Ft enum asn_err
92.Fn asn_put_integer "struct asn_buf *buf" "int32_t arg"
93.Ft enum asn_err
94.Fn asn_get_octetstring_raw "struct asn_buf *buf" "asn_len_t len" "u_char *out" "u_int *outsize"
95.Ft enum asn_err
96.Fn asn_get_octetstring "struct asn_buf *buf" "u_char *out" "u_int *outsize"
97.Ft enum asn_err
98.Fn asn_put_octetstring "struct asn_buf *buf" "const u_char *str" "u_int strsize"
99.Ft enum asn_err
100.Fn asn_get_null_raw "struct asn_buf *buf" "asn_len_t len"
101.Ft enum asn_err
102.Fn asn_get_null "struct asn_buf *buf"
103.Ft enum asn_err
104.Fn asn_put_null "struct asn_buf *buf"
105.Ft enum asn_err
106.Fn asn_put_exception "struct asn_buf *buf" "u_int type"
107.Ft enum asn_err
108.Fn asn_get_objid_raw "struct asn_buf *buf" "asn_len_t len" "struct asn_oid *oid"
109.Ft enum asn_err
110.Fn asn_get_objid "struct asn_buf *buf" "struct asn_oid *oid"
111.Ft enum asn_err
112.Fn asn_put_objid "struct asn_buf *buf" "const struct asn_oid *oid"
113.Ft enum asn_err
114.Fn asn_get_sequence "struct asn_buf *buf" "asn_len_t *lenp"
115.Ft enum asn_err
116.Fn asn_get_ipaddress_raw "struct asn_buf *buf" "asn_len_t len" "u_char *ipa"
117.Ft enum asn_err
118.Fn asn_get_ipaddress "struct asn_buf *buf" "u_char *ipa"
119.Ft enum asn_err
120.Fn asn_put_ipaddress "struct asn_buf *buf" "const u_char *ipa"
121.Ft enum asn_err
122.Fn asn_get_uint32_raw "struct asn_buf *buf" "asn_len_t len" "u_int32_t *res"
123.Ft enum asn_err
124.Fn asn_put_uint32 "struct asn_buf *buf" "u_char type" "u_int32_t val"
125.Ft enum asn_err
126.Fn asn_get_counter64_raw "struct asn_buf *buf" "asn_len_t len" "u_int64_t *res"
127.Ft enum asn_err
128.Fn asn_put_counter64 "struct asn_buf *buf" "u_int64_t val"
129.Ft enum asn_err
130.Fn asn_get_timeticks "struct asn_buf *buf" "u_int32_t *valp"
131.Ft enum asn_err
132.Fn asn_put_timeticks "struct asn_buf *buf" "u_int32_t val"
133.Ft enum asn_err
134.Fn asn_skip "struct asn_buf *buf" "asn_len_t len"
135.Ft void
136.Fn asn_slice_oid "struct asn_oid *dest" "const struct asn_oid *src" "u_int from" "u_int to"
137.Ft void
138.Fn asn_append_oid "struct asn_oid *to" "const struct asn_oid *from"
139.Ft int
140.Fn asn_compare_oid "const struct asn_oid *oid1" "const struct asn_oid *oid2"
141.Ft int
142.Fn asn_is_suboid "const struct asn_oid *oid1" "const struct asn_oid *oid2"
143.Ft char *
144.Fn asn_oid2str_r "const struct asn_oid *oid" "char *buf"
145.Ft char *
146.Fn asn_oid2str "const struct asn_oid *oid"
147.Sh DESCRIPTION
148The ASN.1 library contains routines to handle ASN.1 encoding for SNMP.
149It supports only the restricted form of ASN.1 as required by SNMP. There
150are two basic structures used throughout the library:
151.Bd -literal -offset indent
152/* these restrictions are in the SMI */
153#define ASN_MAXID	0xffffffff
154#define ASN_MAXOIDLEN	128
155
156/* type of subidentifiers */
157typedef u_int32_t asn_subid_t;
158
159struct asn_oid {
160	u_int	len;
161	asn_subid_t subs[ASN_MAXOIDLEN];
162};
163.Ed
164.Pp
165This structure represents an OID with the restrictions defined in the SNMP
166SMI.
167.Fa len
168holds the current length of the OID and
169.Fa subs
170holds the elements of the OID.
171.Bd -literal -offset indent
172struct asn_buf {
173	union {
174		u_char	*ptr;
175		const u_char *cptr;
176	}	asn_u;
177	size_t	asn_len;
178};
179#define asn_cptr	asn_u.cptr
180#define asn_ptr	asn_u.ptr
181.Ed
182.Pp
183This structure is used to encode and decode ASN.1. It describes the output
184buffer for encoding routines and the input buffer for decoding routines.
185For encoding
186.Fa asn_len
187holds the number of remaining free octets in the buffer. The first free byte
188is pointed to by
189.Fa asn_ptr .
190For decoding
191.Fa asn_len
192holds the number of remaining bytes to decode. The next byte to decode is pointed
193to by
194.Fa asn_cptr .
195.Pp
196Most of the functions return an error code
197.Fa "enum asn_error" :
198.Bd -literal -offset indent
199enum asn_err {
200	/* conversion was ok */
201	ASN_ERR_OK	= 0,
202	/* conversion failed and stopped */
203	ASN_ERR_FAILED	= 1 | 0x1000,
204	/* length field bad, value skipped */
205	ASN_ERR_BADLEN	= 2,
206	/* out of buffer, stopped */
207	ASN_ERR_EOBUF	= 3 | 0x1000,
208	/* length ok, but value is out of range */
209	ASN_ERR_RANGE	= 4,
210	/* not the expected tag, stopped */
211	ASN_ERR_TAG	= 5 | 0x1000,
212};
213#define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
214.Ed
215.Pp
216If
217.Fn ASN_ERR_STOPPED
218returns true, the error was fatal and processing has stopped at the point
219of error.
220.Pp
221The function
222.Fn asn_get_header
223reads the next header from the input octet stream. It returns the tag
224in the variable pointed to by
225.Fa type
226(note that only single byte tags are supported) and the decoded length field
227in the value pointed to by
228.Fa lenp
229(this is restricted to a unsigned 32-bit value). All errors in this function
230are fatal and stop processing.
231.Pp
232The function
233.Fn asn_put_header
234writes an ASN.1 header.
235.Fa type
236is the tag to write and is restricted to one byte tags (i.e. tags
237lesser or equal than 0x30).
238.Fa len
239is the length of the value and is restricted to 16-bit.
240.Pp
241The functions
242.Fn asn_put_temp_header
243and
244.Fn asn_commit_header
245are used to write a header when the length of the value is not known in
246advance, for example, for sequences.
247.Fn asn_put_temp_header
248writes a header with the given tag
249.Fa type
250and space for the maximum supported length field and sets the pointer pointed
251to by
252.Fa ptr
253to the begin of this length field. This pointer must then be fed into
254.Fn asn_commit_header
255directly after writing the value to the buffer. The function will compute the
256length, insert it into the right place and shift the value if the resulting
257length field is shorter than the estimated one.
258.Pp
259The function
260.Fn asn_get_integer_raw
261is used to decode a signed integer value (32-bit). It assumes, that the
262header of the integer has been decoded already.
263.Fa len
264is the length obtained from the ASN.1 header and the integer will be returned
265in the value pointed to by
266.Fa res .
267.Pp
268The function
269.Fn asn_get_integer
270decodes a complete 32-bit signed integer including the header. If the
271tag is wrong
272.Li ASN_ERR_TAG
273is returned.
274The function
275.Fn asn_put_integer
276encodes a 32-bit signed integer.
277.Pp
278The function
279.Fn asn_get_octetstring_raw
280decodes the value field of an ASN.1 octet string. The length obtained from the
281header must be fed into the
282.Fa len
283argument and
284.Fa out
285must point to a buffer to receive the octet string. On entry to the function
286.Fa outsize
287must point to the size of the buffer. On exit
288.Fa outsize
289will point to the number of octets decoded (if no error occurs this will be
290equal to
291.Fa len ).
292The function
293.Fn asn_get_octetstring
294decodes an octetstring including the header.
295.Fa out
296must point to a buffer to receive the string,
297.Fa outsize
298must point to the size of the buffer. On exit of the function
299.Fa outsize
300will point to the number of octets decoded.
301The function
302.Fn asn_put_octetstring
303encodes an octetstring (including the header).
304.Fa str
305points to the string to encode and
306.Fa strsize
307is the length of the string (the string may contain embedded
308.Li NUL Ns s).
309.Pp
310The function
311.Fn asn_get_null_raw
312decodes a null value.
313.Fa len
314is the length obtained from the header and must be 0.
315The function
316.Fn asn_get_null
317decodes a null including the header and the function
318.Fn asn_put_null
319encodes a null.
320.Pp
321The function
322.Fn asn_put_exception
323is used to encode an SNMPv2 exception. The exception type is
324.Fa type .
325.Pp
326The function
327.Fn asn_get_objid_raw
328is used to decode an OID value.
329.Fa len
330must be the value length obtained from the header and
331.Fa oid
332will receive the decoded OID.
333The function
334.Fn asn_get_objid
335decodes a complete OID (including the header) and the function
336.Fn asn_put_objid
337encodes a complete OID.
338.Pp
339The function
340.Fn asn_get_sequence
341decodes a sequence header.
342The length of the sequence value will be stored in the value pointed to by
343.Fa lenp .
344.Pp
345The function
346.Fn asn_get_ipaddress_raw
347decodes an IP address value.
348.Fa len
349is the length from the header and must be 4.
350.Fa ipa
351will receive the decoded IP address and must point to a buffer of at least
352four bytes.
353The function
354.Fn asn_get_ipaddress
355decodes a complete IP address (including the header) and
356.Fn asn_put_ipaddress
357encodes an IP address.
358.Pp
359The function
360.Fn asn_get_uint32_raw
361decodes an unsigned 32-bit integer value.
362.Fa len
363is the length from the header and
364.Fa res
365will get the decoded value.
366The function
367.Fn asn_put_uint32
368encodes an unsigned 32-bit integer value and inserts the tag given in
369.Fa type
370into the header.
371.Pp
372The function
373.Fn asn_get_counter64_raw
374decodes an unsigned 64-bit integer value.
375.Fa len
376must be the value length from the header. The resulting value is
377stored into the variable pointed to by
378.Fa res .
379The function
380.Fn asn_put_counter64
381encodes a complete unsigned 64-bit value.
382.Pp
383The function
384.Fn asn_get_timeticks
385decodes an ASN.1 object of type
386.Li TIMETICKS
387and the function
388.Fn asn_put_timeticks
389encodes such an object.
390.Pp
391The function
392.Fn asn_skip
393can be used to skip
394.Fa len
395bytes in the input buffer.
396.Pp
397The function
398.Fn asn_slice_oid
399splits a part out from an OID. It takes all the subids from the OID
400pointed to by
401.Fa src
402starting with the subid at position
403.Fa from
404(the first subid beeing subid 0) up to, but not including, subid
405.Fa to
406and generates a new OID in
407.Fa dest .
408If
409.Fa to
410is less or equal to
411.Fa from
412the resulting OID will have a length of zero.
413.Pp
414The function
415.Fn asn_append_oid
416appends the OID
417.Fa from
418to the OID
419.Fa to
420given that the resulting OID is not too long. If the maximum length is exceeded
421the result is undefined.
422.Pp
423The function
424.Fn asn_compare_oid
425compares two oids and returns the values
426.Li -1 ,
427.Li 0 or
428.Li +1
429when
430.Fa oid1
431is lesser than, equal, or larger than
432.Fa oid2
433resp.
434.Pp
435The function
436.Fn asn_is_suboid
437returns 1 if
438.Fa oid1
439is equal to the leading part of
440.Fa oid2 .
441It returns 0 otherwise.
442.Pp
443The function
444.Fn asn_oid2str_r
445makes a printable string from
446.Fa oid .
447The buffer pointed to by
448.Fa str
449must be large enough to hold the result. The constant
450.Li ASN_OIDSTRLEN
451is defined to be the length of the maximum string generated by this function
452(including the trailing NUL).
453The function
454.Fn asn_oid2str
455makes a printable string from
456.Fa oid
457into a private buffer that is overwritten by each call.
458.Sh DIAGNOSTICS
459When an error occures in any of the function the function pointed to
460by the global pointer
461.Bd -literal -offset indent
462extern void (*asn_error)(const struct asn_buf *, const char *, ...);
463.Ed
464.Pp
465is called with the current buffer (this may be
466.Li NULL )
467and a
468.Xr printf 3
469style format string.
470There is a default error handler in the library that prints a message
471starting with
472.Sq ASN.1:
473followed by the error message and an optional dump of the buffer.
474.Sh SEE ALSO
475.Xr snmpd 1 ,
476.Xr gensnmptree 1 ,
477.Xr bsnmplib 3
478.Xr bsnmpclient 3 ,
479.Xr bsnmpagent 3
480.Sh STANDARDS
481This implementation conforms to the applicable IETF RFCs and ITU-T
482recommendations.
483.Sh AUTHORS
484.An Hartmut Brandt Aq harti@freebsd.org
485