summaryrefslogtreecommitdiffstats
path: root/contrib/bsnmp/lib/asn1.3
blob: 419a929aada166ad65155bb8e28e1d82c39c3d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
.\"
.\" Copyright (c) 2004-2005
.\"	Hartmut Brandt.
.\"	All rights reserved.
.\" Copyright (c) 2001-2003
.\"	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
.\"	All rights reserved.
.\" 
.\" Author: Harti Brandt <harti@FreeBSD.org>
.\" 
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 
.\" THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Begemot: bsnmp/lib/asn1.3,v 1.9 2005/10/04 08:46:49 brandt_h Exp $
.\"
.Dd October 4, 2005
.Dt ASN1 3
.Os
.Sh NAME
.Nm asn_get_header ,
.Nm asn_put_header ,
.Nm asn_put_temp_header ,
.Nm asn_commit_header ,
.Nm asn_get_integer_raw ,
.Nm asn_get_integer ,
.Nm asn_put_integer ,
.Nm asn_get_octetstring_raw ,
.Nm asn_get_octetstring ,
.Nm asn_put_octetstring ,
.Nm asn_get_null_raw ,
.Nm asn_get_null ,
.Nm asn_put_null ,
.Nm asn_put_exception ,
.Nm asn_get_objid_raw ,
.Nm asn_get_objid ,
.Nm asn_put_objid ,
.Nm asn_get_sequence ,
.Nm asn_get_ipaddress_raw ,
.Nm asn_get_ipaddress ,
.Nm asn_put_ipaddress ,
.Nm asn_get_uint32_raw ,
.Nm asn_put_uint32 ,
.Nm asn_get_counter64_raw ,
.Nm asn_put_counter64 ,
.Nm asn_get_timeticks ,
.Nm asn_put_timeticks ,
.Nm asn_skip ,
.Nm asn_slice_oid ,
.Nm asn_append_oid ,
.Nm asn_compare_oid ,
.Nm asn_is_suboid ,
.Nm asn_oid2str_r ,
.Nm asn_oid2str
.Nd "ASN.1 library for SNMP"
.Sh LIBRARY
Begemot SNMP library
.Pq libbsnmp, -lbsnmp
.Sh SYNOPSIS
.In bsnmp/asn1.h
.Ft enum asn_err
.Fn asn_get_header "struct asn_buf *buf" "u_char *type" "asn_len_t *lenp"
.Ft enum asn_err
.Fn asn_put_header "struct asn_buf *buf" "u_char type" "asn_len_t len"
.Ft enum asn_err
.Fn asn_put_temp_header "struct asn_buf *buf" "u_char type" "u_char **ptr"
.Ft enum asn_err
.Fn asn_commit_header "struct asn_buf *buf" "u_char *ptr"
.Ft enum asn_err
.Fn asn_get_integer_raw "struct asn_buf *buf" "asn_len_t len" "int32_t *res"
.Ft enum asn_err
.Fn asn_get_integer "struct asn_buf *buf" "int32_t *res"
.Ft enum asn_err
.Fn asn_put_integer "struct asn_buf *buf" "int32_t arg"
.Ft enum asn_err
.Fn asn_get_octetstring_raw "struct asn_buf *buf" "asn_len_t len" "u_char *out" "u_int *outsize"
.Ft enum asn_err
.Fn asn_get_octetstring "struct asn_buf *buf" "u_char *out" "u_int *outsize"
.Ft enum asn_err
.Fn asn_put_octetstring "struct asn_buf *buf" "const u_char *str" "u_int strsize"
.Ft enum asn_err
.Fn asn_get_null_raw "struct asn_buf *buf" "asn_len_t len"
.Ft enum asn_err
.Fn asn_get_null "struct asn_buf *buf"
.Ft enum asn_err
.Fn asn_put_null "struct asn_buf *buf"
.Ft enum asn_err
.Fn asn_put_exception "struct asn_buf *buf" "u_int type"
.Ft enum asn_err
.Fn asn_get_objid_raw "struct asn_buf *buf" "asn_len_t len" "struct asn_oid *oid"
.Ft enum asn_err
.Fn asn_get_objid "struct asn_buf *buf" "struct asn_oid *oid"
.Ft enum asn_err
.Fn asn_put_objid "struct asn_buf *buf" "const struct asn_oid *oid"
.Ft enum asn_err
.Fn asn_get_sequence "struct asn_buf *buf" "asn_len_t *lenp"
.Ft enum asn_err
.Fn asn_get_ipaddress_raw "struct asn_buf *buf" "asn_len_t len" "u_char *ipa"
.Ft enum asn_err
.Fn asn_get_ipaddress "struct asn_buf *buf" "u_char *ipa"
.Ft enum asn_err
.Fn asn_put_ipaddress "struct asn_buf *buf" "const u_char *ipa"
.Ft enum asn_err
.Fn asn_get_uint32_raw "struct asn_buf *buf" "asn_len_t len" "u_int32_t *res"
.Ft enum asn_err
.Fn asn_put_uint32 "struct asn_buf *buf" "u_char type" "u_int32_t val"
.Ft enum asn_err
.Fn asn_get_counter64_raw "struct asn_buf *buf" "asn_len_t len" "u_int64_t *res"
.Ft enum asn_err
.Fn asn_put_counter64 "struct asn_buf *buf" "u_int64_t val"
.Ft enum asn_err
.Fn asn_get_timeticks "struct asn_buf *buf" "u_int32_t *valp"
.Ft enum asn_err
.Fn asn_put_timeticks "struct asn_buf *buf" "u_int32_t val"
.Ft enum asn_err
.Fn asn_skip "struct asn_buf *buf" "asn_len_t len"
.Ft void
.Fn asn_slice_oid "struct asn_oid *dest" "const struct asn_oid *src" "u_int from" "u_int to"
.Ft void
.Fn asn_append_oid "struct asn_oid *to" "const struct asn_oid *from"
.Ft int
.Fn asn_compare_oid "const struct asn_oid *oid1" "const struct asn_oid *oid2"
.Ft int
.Fn asn_is_suboid "const struct asn_oid *oid1" "const struct asn_oid *oid2"
.Ft char *
.Fn asn_oid2str_r "const struct asn_oid *oid" "char *buf"
.Ft char *
.Fn asn_oid2str "const struct asn_oid *oid"
.Sh DESCRIPTION
The ASN.1 library contains routines to handle ASN.1 encoding for SNMP.
It supports only the restricted form of ASN.1 as required by SNMP.
There are two basic structures used throughout the library:
.Bd -literal -offset indent
/* these restrictions are in the SMI */
#define ASN_MAXID	0xffffffff
#define ASN_MAXOIDLEN	128

/* type of subidentifiers */
typedef u_int32_t asn_subid_t;

struct asn_oid {
	u_int	len;
	asn_subid_t subs[ASN_MAXOIDLEN];
};
.Ed
.Pp
This structure represents an OID with the restrictions defined in the SNMP
SMI.
.Fa len
holds the current length of the OID and
.Fa subs
holds the elements of the OID.
.Bd -literal -offset indent
struct asn_buf {
	union {
		u_char	*ptr;
		const u_char *cptr;
	}	asn_u;
	size_t	asn_len;
};
#define asn_cptr	asn_u.cptr
#define asn_ptr	asn_u.ptr
.Ed
.Pp
This structure is used to encode and decode ASN.1.
It describes the output
buffer for encoding routines and the input buffer for decoding routines.
For encoding
.Fa asn_len
holds the number of remaining free octets in the buffer.
The first free byte is pointed to by
.Fa asn_ptr .
For decoding
.Fa asn_len
holds the number of remaining bytes to decode.
The next byte to decode is pointed to by
.Fa asn_cptr .
.Pp
Most of the functions return an error code
.Fa "enum asn_error" :
.Bd -literal -offset indent
enum asn_err {
	/* conversion was ok */
	ASN_ERR_OK	= 0,
	/* conversion failed and stopped */
	ASN_ERR_FAILED	= 1 | 0x1000,
	/* length field bad, value skipped */
	ASN_ERR_BADLEN	= 2,
	/* out of buffer, stopped */
	ASN_ERR_EOBUF	= 3 | 0x1000,
	/* length ok, but value is out of range */
	ASN_ERR_RANGE	= 4,
	/* not the expected tag, stopped */
	ASN_ERR_TAG	= 5 | 0x1000,
};
#define ASN_ERR_STOPPED(E) (((E) & 0x1000) != 0)
.Ed
.Pp
If
.Fn ASN_ERR_STOPPED
returns true, the error was fatal and processing has stopped at the point
of error.
.Pp
The function
.Fn asn_get_header
reads the next header from the input octet stream.
It returns the tag in the variable pointed to by
.Fa type
(note that only single byte tags are supported) and the decoded length field
in the value pointed to by
.Fa lenp
(this is restricted to a unsigned 32-bit value).
All errors in this function are fatal and stop processing.
.Pp
The function
.Fn asn_put_header
writes an ASN.1 header.
.Fa type
is the tag to write and is restricted to one byte tags (i.e., tags
lesser or equal than 0x30).
.Fa len
is the length of the value and is restricted to 16-bit.
.Pp
The functions
.Fn asn_put_temp_header
and
.Fn asn_commit_header
are used to write a header when the length of the value is not known in
advance, for example, for sequences.
.Fn asn_put_temp_header
writes a header with the given tag
.Fa type
and space for the maximum supported length field and sets the pointer pointed
to by
.Fa ptr
to the begin of this length field.
This pointer must then be fed into
.Fn asn_commit_header
directly after writing the value to the buffer.
The function will compute the
length, insert it into the right place and shift the value if the resulting
length field is shorter than the estimated one.
.Pp
The function
.Fn asn_get_integer_raw
is used to decode a signed integer value (32-bit).
It assumes, that the
header of the integer has been decoded already.
.Fa len
is the length obtained from the ASN.1 header and the integer will be returned
in the value pointed to by
.Fa res .
.Pp
The function
.Fn asn_get_integer
decodes a complete 32-bit signed integer including the header.
If the tag is wrong
.Li ASN_ERR_TAG
is returned.
The function
.Fn asn_put_integer
encodes a 32-bit signed integer.
.Pp
The function
.Fn asn_get_octetstring_raw
decodes the value field of an ASN.1 octet string.
The length obtained from the header must be fed into the
.Fa len
argument and
.Fa out
must point to a buffer to receive the octet string.
On entry to the function
.Fa outsize
must point to the size of the buffer.
On exit
.Fa outsize
will point to the number of octets decoded (if no error occurs this will be
equal to
.Fa len ).
The function
.Fn asn_get_octetstring
decodes an octetstring including the header.
.Fa out
must point to a buffer to receive the string,
.Fa outsize
must point to the size of the buffer.
On exit of the function
.Fa outsize
will point to the number of octets decoded.
The function
.Fn asn_put_octetstring
encodes an octetstring (including the header).
.Fa str
points to the string to encode and
.Fa strsize
is the length of the string (the string may contain embedded
.Li NUL Ns s).
.Pp
The function
.Fn asn_get_null_raw
decodes a null value.
.Fa len
is the length obtained from the header and must be 0.
The function
.Fn asn_get_null
decodes a null including the header and the function
.Fn asn_put_null
encodes a null.
.Pp
The function
.Fn asn_put_exception
is used to encode an SNMPv2 exception.
The exception type is
.Fa type .
.Pp
The function
.Fn asn_get_objid_raw
is used to decode an OID value.
.Fa len
must be the value length obtained from the header and
.Fa oid
will receive the decoded OID.
The function
.Fn asn_get_objid
decodes a complete OID (including the header) and the function
.Fn asn_put_objid
encodes a complete OID.
.Pp
The function
.Fn asn_get_sequence
decodes a sequence header.
The length of the sequence value will be stored in the value pointed to by
.Fa lenp .
.Pp
The function
.Fn asn_get_ipaddress_raw
decodes an IP address value.
.Fa len
is the length from the header and must be 4.
.Fa ipa
will receive the decoded IP address and must point to a buffer of at least
four bytes.
The function
.Fn asn_get_ipaddress
decodes a complete IP address (including the header) and
.Fn asn_put_ipaddress
encodes an IP address.
.Pp
The function
.Fn asn_get_uint32_raw
decodes an unsigned 32-bit integer value.
.Fa len
is the length from the header and
.Fa res
will get the decoded value.
The function
.Fn asn_put_uint32
encodes an unsigned 32-bit integer value and inserts the tag given in
.Fa type
into the header.
.Pp
The function
.Fn asn_get_counter64_raw
decodes an unsigned 64-bit integer value.
.Fa len
must be the value length from the header.
The resulting value is stored into the variable pointed to by
.Fa res .
The function
.Fn asn_put_counter64
encodes a complete unsigned 64-bit value.
.Pp
The function
.Fn asn_get_timeticks
decodes an ASN.1 object of type
.Li TIMETICKS
and the function
.Fn asn_put_timeticks
encodes such an object.
.Pp
The function
.Fn asn_skip
can be used to skip
.Fa len
bytes in the input buffer.
.Pp
The function
.Fn asn_slice_oid
splits a part out from an OID.
It takes all the subids from the OID pointed to by
.Fa src
starting with the subid at position
.Fa from
(the first subid being subid 0) up to, but not including, subid
.Fa to
and generates a new OID in
.Fa dest .
If
.Fa to
is less or equal to
.Fa from
the resulting OID will have a length of zero.
.Pp
The function
.Fn asn_append_oid
appends the OID
.Fa from
to the OID
.Fa to
given that the resulting OID is not too long.
If the maximum length is exceeded the result is undefined.
.Pp
The function
.Fn asn_compare_oid
compares two oids and returns the values
.Li -1 ,
.Li 0 or
.Li +1
when
.Fa oid1
is lesser than, equal, or larger than
.Fa oid2
resp.
.Pp
The function
.Fn asn_is_suboid
returns 1 if
.Fa oid1
is equal to the leading part of
.Fa oid2 .
It returns 0 otherwise.
.Pp
The function
.Fn asn_oid2str_r
makes a printable string from
.Fa oid .
The buffer pointed to by
.Fa str
must be large enough to hold the result.
The constant
.Li ASN_OIDSTRLEN
is defined to be the length of the maximum string generated by this function
(including the trailing NUL).
The function
.Fn asn_oid2str
makes a printable string from
.Fa oid
into a private buffer that is overwritten by each call.
.Sh DIAGNOSTICS
When an error occurs in any of the function the function pointed to
by the global pointer
.Bd -literal -offset indent
extern void (*asn_error)(const struct asn_buf *, const char *, ...);
.Ed
.Pp
is called with the current buffer (this may be
.Li NULL )
and a
.Xr printf 3
style format string.
There is a default error handler in the library that prints a message
starting with
.Sq ASN.1:
followed by the error message and an optional dump of the buffer.
.Sh SEE ALSO
.Xr gensnmptree 1 ,
.Xr bsnmpd 1 ,
.Xr bsnmpagent 3 ,
.Xr bsnmpclient 3 ,
.Xr bsnmplib 3
.Sh STANDARDS
This implementation conforms to the applicable IETF RFCs and ITU-T
recommendations.
.Sh AUTHORS
.An Hartmut Brandt Aq harti@FreeBSD.org
OpenPOWER on IntegriCloud