---
name: "asn1"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/asn1"
---

# asn1(3)

ASN.1 library for SNMP

## NAME

**asn\_get\_header**, **asn\_put\_header**, **asn\_put\_temp\_header**, **asn\_commit\_header**, **asn\_get\_integer\_raw**, **asn\_get\_integer**, **asn\_put\_integer**, **asn\_get\_octetstring\_raw**, **asn\_get\_octetstring**, **asn\_put\_octetstring**, **asn\_get\_null\_raw**, **asn\_get\_null**, **asn\_put\_null**, **asn\_put\_exception**, **asn\_get\_objid\_raw**, **asn\_get\_objid**, **asn\_put\_objid**, **asn\_get\_sequence**, **asn\_get\_ipaddress\_raw**, **asn\_get\_ipaddress**, **asn\_put\_ipaddress**, **asn\_get\_uint32\_raw**, **asn\_put\_uint32**, **asn\_get\_counter64\_raw**, **asn\_put\_counter64**, **asn\_get\_timeticks**, **asn\_put\_timeticks**, **asn\_skip**, **asn\_slice\_oid**, **asn\_append\_oid**, **asn\_compare\_oid**, **asn\_is\_suboid**, **asn\_oid2str\_r**, **asn\_oid2str** – ASN.1 library for SNMP

## LIBRARY

Begemot SNMP library (libbsnmp, -lbsnmp)

## SYNOPSIS

**#include \<bsnmp/asn1.h\>**

*enum* *asn\_err*\

**asn\_get\_header**(*struct asn\_buf \*buf*, *u\_char \*type*, *asn\_len\_t \*lenp*);

*enum* *asn\_err*\

**asn\_put\_header**(*struct asn\_buf \*buf*, *u\_char type*, *asn\_len\_t len*);

*enum* *asn\_err*\

**asn\_put\_temp\_header**(*struct asn\_buf \*buf*, *u\_char type*, *u\_char \*\*ptr*);

*enum* *asn\_err*\

**asn\_commit\_header**(*struct asn\_buf \*buf*, *u\_char \*ptr*);

*enum* *asn\_err*\

**asn\_get\_integer\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*, *int32\_t \*res*);

*enum* *asn\_err*\

**asn\_get\_integer**(*struct asn\_buf \*buf*, *int32\_t \*res*);

*enum* *asn\_err*\

**asn\_put\_integer**(*struct asn\_buf \*buf*, *int32\_t arg*);

*enum* *asn\_err*\

**asn\_get\_octetstring\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*, *u\_char \*out*, *u\_int \*outsize*);

*enum* *asn\_err*\

**asn\_get\_octetstring**(*struct asn\_buf \*buf*, *u\_char \*out*, *u\_int \*outsize*);

*enum* *asn\_err*\

**asn\_put\_octetstring**(*struct asn\_buf \*buf*, *const u\_char \*str*, *u\_int strsize*);

*enum* *asn\_err*\

**asn\_get\_null\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*);

*enum* *asn\_err*\

**asn\_get\_null**(*struct asn\_buf \*buf*);

*enum* *asn\_err*\

**asn\_put\_null**(*struct asn\_buf \*buf*);

*enum* *asn\_err*\

**asn\_put\_exception**(*struct asn\_buf \*buf*, *u\_int type*);

*enum* *asn\_err*\

**asn\_get\_objid\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*, *struct asn\_oid \*oid*);

*enum* *asn\_err*\

**asn\_get\_objid**(*struct asn\_buf \*buf*, *struct asn\_oid \*oid*);

*enum* *asn\_err*\

**asn\_put\_objid**(*struct asn\_buf \*buf*, *const struct asn\_oid \*oid*);

*enum* *asn\_err*\

**asn\_get\_sequence**(*struct asn\_buf \*buf*, *asn\_len\_t \*lenp*);

*enum* *asn\_err*\

**asn\_get\_ipaddress\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*, *u\_char \*ipa*);

*enum* *asn\_err*\

**asn\_get\_ipaddress**(*struct asn\_buf \*buf*, *u\_char \*ipa*);

*enum* *asn\_err*\

**asn\_put\_ipaddress**(*struct asn\_buf \*buf*, *const u\_char \*ipa*);

*enum* *asn\_err*\

**asn\_get\_uint32\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*, *u\_int32\_t \*res*);

*enum* *asn\_err*\

**asn\_put\_uint32**(*struct asn\_buf \*buf*, *u\_char type*, *u\_int32\_t val*);

*enum* *asn\_err*\

**asn\_get\_counter64\_raw**(*struct asn\_buf \*buf*, *asn\_len\_t len*, *u\_int64\_t \*res*);

*enum* *asn\_err*\

**asn\_put\_counter64**(*struct asn\_buf \*buf*, *u\_int64\_t val*);

*enum* *asn\_err*\

**asn\_get\_timeticks**(*struct asn\_buf \*buf*, *u\_int32\_t \*valp*);

*enum* *asn\_err*\

**asn\_put\_timeticks**(*struct asn\_buf \*buf*, *u\_int32\_t val*);

*enum* *asn\_err*\

**asn\_skip**(*struct asn\_buf \*buf*, *asn\_len\_t len*);

*void*\

**asn\_slice\_oid**(*struct asn\_oid \*dest*, *const struct asn\_oid \*src*, *u\_int from*, *u\_int to*);

*void*\

**asn\_append\_oid**(*struct asn\_oid \*to*, *const struct asn\_oid \*from*);

*int*\

**asn\_compare\_oid**(*const struct asn\_oid \*oid1*, *const struct asn\_oid \*oid2*);

*int*\

**asn\_is\_suboid**(*const struct asn\_oid \*oid1*, *const struct asn\_oid \*oid2*);

*char* *\**\

**asn\_oid2str\_r**(*const struct asn\_oid \*oid*, *char \*buf*);

*char* *\**\

**asn\_oid2str**(*const struct asn\_oid \*oid*);

## 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:

```
/* 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];
};
```

This structure represents an OID with the restrictions defined in the SNMP SMI. *len* holds the current length of the OID and *subs* holds the elements of the OID.

```
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
```

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 *asn\_len* holds the number of remaining free octets in the buffer. The first free byte is pointed to by *asn\_ptr*. For decoding *asn\_len* holds the number of remaining bytes to decode. The next byte to decode is pointed to by *asn\_cptr*.

Most of the functions return an error code *enum asn\_error*:

```
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)
```

If **ASN\_ERR\_STOPPED**() returns true, the error was fatal and processing has stopped at the point of error.

The function **asn\_get\_header**() reads the next header from the input octet stream. It returns the tag in the variable pointed to by *type* (note that only single byte tags are supported) and the decoded length field in the value pointed to by *lenp* (this is restricted to a unsigned 32-bit value). All errors in this function are fatal and stop processing.

The function **asn\_put\_header**() writes an ASN.1 header. *type* is the tag to write and is restricted to one byte tags (i.e., tags lesser or equal than 0x30). *len* is the length of the value and is restricted to 16-bit.

The functions **asn\_put\_temp\_header**() and **asn\_commit\_header**() are used to write a header when the length of the value is not known in advance, for example, for sequences. **asn\_put\_temp\_header**() writes a header with the given tag *type* and space for the maximum supported length field and sets the pointer pointed to by *ptr* to the begin of this length field. This pointer must then be fed into **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.

The function **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. *len* is the length obtained from the ASN.1 header and the integer will be returned in the value pointed to by *res*.

The function **asn\_get\_integer**() decodes a complete 32-bit signed integer including the header. If the tag is wrong ASN\_ERR\_TAG is returned. The function **asn\_put\_integer**() encodes a 32-bit signed integer.

The function **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 *len* argument and *out* must point to a buffer to receive the octet string. On entry to the function *outsize* must point to the size of the buffer. On exit *outsize* will point to the number of octets decoded (if no error occurs this will be equal to *len*, *).* The function **asn\_get\_octetstring**() decodes an octetstring including the header. *out* must point to a buffer to receive the string, *outsize* must point to the size of the buffer. On exit of the function *outsize* will point to the number of octets decoded. The function **asn\_put\_octetstring**() encodes an octetstring (including the header). *str* points to the string to encode and *strsize* is the length of the string (the string may contain embedded NULs).

The function **asn\_get\_null\_raw**() decodes a null value. *len* is the length obtained from the header and must be 0. The function **asn\_get\_null**() decodes a null including the header and the function **asn\_put\_null**() encodes a null.

The function **asn\_put\_exception**() is used to encode an SNMPv2 exception. The exception type is *type*.

The function **asn\_get\_objid\_raw**() is used to decode an OID value. *len* must be the value length obtained from the header and *oid* will receive the decoded OID. The function **asn\_get\_objid**() decodes a complete OID (including the header) and the function **asn\_put\_objid**() encodes a complete OID.

The function **asn\_get\_sequence**() decodes a sequence header. The length of the sequence value will be stored in the value pointed to by *lenp*.

The function **asn\_get\_ipaddress\_raw**() decodes an IP address value. *len* is the length from the header and must be 4. *ipa* will receive the decoded IP address and must point to a buffer of at least four bytes. The function **asn\_get\_ipaddress**() decodes a complete IP address (including the header) and **asn\_put\_ipaddress**() encodes an IP address.

The function **asn\_get\_uint32\_raw**() decodes an unsigned 32-bit integer value. *len* is the length from the header and *res* will get the decoded value. The function **asn\_put\_uint32**() encodes an unsigned 32-bit integer value and inserts the tag given in *type* into the header.

The function **asn\_get\_counter64\_raw**() decodes an unsigned 64-bit integer value. *len* must be the value length from the header. The resulting value is stored into the variable pointed to by *res*. The function **asn\_put\_counter64**() encodes a complete unsigned 64-bit value.

The function **asn\_get\_timeticks**() decodes an ASN.1 object of type TIMETICKS and the function **asn\_put\_timeticks**() encodes such an object.

The function **asn\_skip**() can be used to skip *len* bytes in the input buffer.

The function **asn\_slice\_oid**() splits a part out from an OID. It takes all the subids from the OID pointed to by *src* starting with the subid at position *from* (the first subid being subid 0) up to, but not including, subid *to* and generates a new OID in *dest*. If *to* is less or equal to *from* the resulting OID will have a length of zero.

The function **asn\_append\_oid**() appends the OID *from* to the OID *to* given that the resulting OID is not too long. If the maximum length is exceeded the result is undefined.

The function **asn\_compare\_oid**() compares two oids and returns the values -1, 0 or +1 when *oid1* is lesser than, equal, or larger than *oid2* resp.

The function **asn\_is\_suboid**() returns 1 if *oid1* is equal to the leading part of *oid2*. It returns 0 otherwise.

The function **asn\_oid2str\_r**() makes a printable string from *oid*. The buffer pointed to by *str* must be large enough to hold the result. The constant ASN\_OIDSTRLEN is defined to be the length of the maximum string generated by this function (including the trailing NUL). The function **asn\_oid2str**() makes a printable string from *oid* into a private buffer that is overwritten by each call.

## DIAGNOSTICS

When an error occurs in any of the function the function pointed to by the global pointer

```
extern void (*asn_error)(const struct asn_buf *, const char *, ...);
```

is called with the current buffer (this may be NULL) and a [printf(3)](/printf/3.md) style format string. There is a default error handler in the library that prints a message starting with ‘ASN.1:’ followed by the error message and an optional dump of the buffer.

## SEE ALSO

[gensnmptree(1)](/gensnmptree/1.md), [bsnmpd(1)](/bsnmpd/1.md), [bsnmpagent(3)](/bsnmpagent/3.md), [bsnmpclient(3)](/bsnmpclient/3.md), [bsnmplib(3)](/bsnmplib/3.md)

## STANDARDS

This implementation conforms to the applicable IETF RFCs and ITU-T recommendations.

## AUTHORS

Hartmut Brandt ⟨harti@FreeBSD.org⟩
