ASN1(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

🌸 NAME 🌸

okay so bestie, this is ASN1(3) from the Library Functions Manual β€” the whole ASN.1 library for SNMP situation. The cast list is long, and I'm not skipping a single one, periodt:

  • 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

and the whole vibe is: ASN.1 library for SNMP. πŸ’…

πŸ’– LIBRARY πŸ’–

It's the Begemot SNMP library (libbsnmp, -lbsnmp). That's the shelf it lives on, bestie. 🫢

✨ SYNOPSIS ✨

okay so first you invite the header file in, like:

#include <bsnmp/asn1.h>

then the prototypes are right here, copied exactly, no edits, no cap:

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 ✨

okay so the ASN.1 library is basically the routine crew that handles ASN.1 encoding for SNMP, bestie. 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.

Here’s the function-by-function tea, no details skipped:

  • 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 an unsigned 32-bit value). All errors in this function are fatal and stop processing.
  • 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.
  • asn_put_temp_header() and asn_commit_header() β€” 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.
  • asn_get_integer_raw() β€” 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.
  • asn_get_integer() β€” decodes a complete 32-bit signed integer including the header. If the tag is wrong ASN_ERR_TAG is returned. asn_put_integer() encodes a 32-bit signed integer.
  • 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). 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. 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).
  • asn_get_null_raw() β€” decodes a null value. len is the length obtained from the header and must be 0. asn_get_null() decodes a null including the header and asn_put_null() encodes a null.
  • asn_put_exception() β€” used to encode an SNMPv2 exception. The exception type is type.
  • asn_get_objid_raw() β€” used to decode an OID value. len must be the value length obtained from the header and oid will receive the decoded OID. asn_get_objid() decodes a complete OID (including the header) and asn_put_objid() encodes a complete OID.
  • asn_get_sequence() β€” decodes a sequence header. The length of the sequence value will be stored in the value pointed to by lenp.
  • 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. asn_get_ipaddress() decodes a complete IP address (including the header) and asn_put_ipaddress() encodes an IP address.
  • 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. asn_put_uint32() encodes an unsigned 32-bit integer value and inserts the tag given in type into the header.
  • 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. asn_put_counter64() encodes a complete unsigned 64-bit value.
  • asn_get_timeticks() β€” decodes an ASN.1 object of type TIMETICKS and asn_put_timeticks() encodes such an object.
  • asn_skip() β€” can be used to skip len bytes in the input buffer.
  • 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.
  • 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.
  • 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.
  • asn_is_suboid() β€” returns 1 if oid1 is equal to the leading part of oid2. It returns 0 otherwise.
  • 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). 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) 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 πŸ‘€


πŸ“œ STANDARDS πŸ“œ

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


πŸ’– AUTHORS πŸ’–

Hartmut Brandt <harti@FreeBSD.org>

okay that’s the whole page, bestie β€” source/version Debian, date October 4, 2005, and it’s the ASN1(3) page. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from
  • freebsd 14.5: base 14.5
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms