Convert::ASN1(3)
π Convert::ASN1 π β manual page Convert::ASN1(3), bestie π
okay so this is the Convert::ASN1 page, and itβs giving ASN.1 encode/decode realness. Every option, every default, every little detail stays β just with the group-chat energy it deserves. π«Ά
πΈ NAME πΈ
Convert::ASN1 β ASN.1 Encode/Decode library.
β¨ VERSION β¨
version 0.34
π¦ SYNOPSIS π¦
bestie, hereβs the whole demo vibe, no cap:
use Convert::ASN1;
$asn = Convert::ASN1->new;
$asn->prepare(q<
[APPLICATION 7] SEQUENCE {
int INTEGER,
str OCTET STRING
}
>);
$pdu = $asn->encode( int => 7, str => "string");
$out = $asn->decode($pdu);
print $out->{int}," ",$out->{str},"\n";
use Convert::ASN1 qw(:io);
$peer = asn_recv($sock,$buffer,0);
$nbytes = asn_read($fh, $buffer);
$nbytes = asn_send($sock, $buffer, $peer);
$nbytes = asn_send($sock, $buffer);
$nbytes = asn_write($fh, $buffer);
$buffer = asn_get($fh);
$yes = asn_ready($fh)
π DESCRIPTION π
Convert::ASN1 encodes and decodes ASN.1 data structures using BER/DER rules. Thatβs the whole main-character job, periodt. π
πΈ METHODS πΈ
π new ( [OPTIONS] ) π
Constructor, creates a new object. β¨
If given, OPTIONS are the same ones as for "configure ( OPTIONS )" below.
π« error () π«
Returns the last error. π
π configure ( OPTIONS ) π
Configure options to control how Convert::ASN1 will perform various tasks. Options are passed as name-value pairs. π«Ά
encodeβ Reference to a hash which contains various encode options.decodeβ Reference to a hash which contains various decode options.encodingβ One of'BER'or'DER'. The default is'BER'.tagdefaultβ One of'EXPLICIT'or'IMPLICIT'. Default tagging conventions are normally given in the ASN.1 module definition (not supported by the parser). The ASN.1 spec states EXPLICIT tagging is the default, but this option has IMPLICIT tagging default for backward compatibility reasons.
π Encode options π
realβ Which encoding to use forreal's. One of'binary','nr1','nr2','nr3'.timeβ This controls howUTCTimeandGeneralizedTimeelements are encoded. The default is"withzone".utctimeβ The value passed will be encoded without a zone, ie a UTC value.withzoneβ The value will be encoded with a zone. By default it will be encoded using the local time offset. The offset may be set using the"timezone"configure option.rawβ The value passed should already be in the correct format and will be copied into the PDU as-is.
timezoneβ By defaultUTCTimeandGeneralizedTimewill be encoded using the local time offset from UTC. This will over-ride that. It is an offset from UTC in seconds. This option can be overridden by passing a reference to a list of two values as the time value. The list should contain the time value and the offset from UTC in seconds.bigintβ If during encoding an value greater than 32 bits is discovered and is not already a big integer object, then the value will first be converted into a big integer object. This option controls the big integer class into which the objects will be blessed. The default is to useMath::BigInt.
π¦ Decode options π¦
timeβ This controls how aUTCTimeor aGeneralizedTimeelement will be decoded. The default is"utctime".utctimeβ The value returned will be a time value as returned by the"time"function.withzoneβ The value returned will be a reference to an array of two values. The first is the same as with"utctime", the second is the timezone offset, in seconds, that was used in the encoding.rawβ The value returned will be the raw encoding as extracted from the PDU.
bigintβ If during decoding any big integers are discovered (integers greater than 32 bits), they will be decoded into big integer objects. This option controls the big integer class into which the objects will be blessed. The default is to useMath::BigInt.nullβ The value to decode ASN.1 NULL types into. If not set, it defaults to1.
π§ prepare ( ASN ) π§
Compile the given ASN.1 description which can be passed as a string or as a filehandle. The syntax used is very close to ASN.1, but has a few differences. If the ASN describes only one macro then encode/decode can be called on this object. If ASN describes more than one ASN.1 macro then find must be called. The method returns undef on error. π«
π prepare_file ( ASNPATH ) π
Compile the ASN.1 description to be read from the specified pathname.
π find ( MACRO ) π
Find a macro from a prepared ASN.1 description. Returns an object which can be used for encode/decode. π«
π encode ( VARIABLES ) π
Encode a PDU. Top-level variable are passed as name-value pairs, or as a reference to a hash containing them. Returns the encoded PDU, or undef on error.
β decode ( PDU ) β
Decode the PDU, returns a reference to a hash containing the values for the PDU. Returns undef if there was an error.
π₯ registeroid ( OID, HANDLER ) π₯
Register a handler for all ASN.1 elements that are "DEFINED BY" the given OID.
HANDLER must be a Convert::ASN1 object, e.g. as returned by find ( MACRO ).
π registertype ( NAME, OID, HANDLER ) π
Register a handler for all ASN.1 elements named "NAME", that are "DEFINED BY" the given OID.
HANDLER must be a Convert::ASN1 object, e.g. as returned by find ( MACRO ).
π EXPORTS π
As well as providing an object interface for encoding/decoding PDUs Convert::ASN1 also provides the following functions.
π IO Functions π
asn_recv ( SOCK, BUFFER, FLAGS )β Will read a single element from the socketSOCKintoBUFFER.FLAGSmay beMSG_PEEKas exported bySocket. Returns the address of the sender, orundefif there was an error. Some systems do not support the return of the peer address when the socket is a connected socket, in these cases the empty string will be returned. This is the same behaviour as therecvfunction in perl itself.
It is recommended that if the socket is of typeSOCK_DGRAMthenrecvbe called directly instead of callingasn_recv.
asn_read ( FH, BUFFER, OFFSET )/asn_read ( FH, BUFFER )β Will read a single element from the filehandleFHintoBUFFER. Returns the number of bytes read if a complete element was read,-1if an incomplete element was read orundefif there was an error. IfOFFSETis specified then it is assumed thatBUFFERalready contains an incomplete element and new data will be appended starting atOFFSET. IfFHis a socket theasn_recvis used to read the element, so the same restriction applies ifFHis a socket of typeSOCK_DGRAM.asn_send ( SOCK, BUFFER, FLAGS, TO )/asn_send ( SOCK, BUFFER, FLAGS )β Identical to callingsend, see perlfunc.asn_write ( FH, BUFFER )β Identical to callingsyswritewith 2 arguments, see perlfunc.asn_get ( FH )βasn_getprovides buffered IO. Because it needs a bufferFHmust be aGLOBor a reference to aGLOB.asn_getwill use two entries in the hash element of theGLOBto use as its buffer:
asn_buffer - input buffer
asn_need - number of bytes needed for the next element, if known
Returns an element or undef if there was an error.
asn_ready ( FH )βasn_readyworks withasn_get. It will return true ifasn_gethas already read enough data into the buffer to return a complete element.
π¦ Encode/Decode Functions π¦
asn_tag ( CLASS, VALUE )β GivenCLASSand aVALUE, calculate an integer which when encoded will become the tag.asn_decode_tag ( TAG )β Decode the given ASN.1 encodedTAG.asn_encode_tag ( TAG )β EncodeTAGvalue for encoding. We assume that the tag has been correctly generated withasn_tag ( CLASS, VALUE ).asn_decode_length ( LEN )β Decode the given ASN.1 decodedLEN.asn_encode_length ( LEN )β Encode the givenLENto its ASN.1 encoding.
π Constants π
ASN_BIT_STR
ASN_BOOLEAN
ASN_ENUMERATED
ASN_GENERAL_TIME
ASN_IA5_STR
ASN_INTEGER
ASN_NULL
ASN_OBJECT_ID
ASN_OCTET_STR
ASN_PRINT_STR
ASN_REAL
ASN_SEQUENCE
ASN_SET
ASN_UTC_TIME
ASN_APPLICATION
ASN_CONTEXT
ASN_PRIVATE
ASN_UNIVERSAL
ASN_PRIMITIVE
ASN_CONSTRUCTOR
ASN_LONG_LEN
ASN_EXTENSION_ID
ASN_BIT
π§ Debug Functions π§
asn_dump ( [FH,] BUFFER )β Try to decode the given buffer as ASN.1 structure and dump it to the given file handle, orSTDERRif the handle is not given.asn_hexdump ( FH, BUFFER )β signature only, bestie; thatβs what the page gives us.
π EXPORT TAGS π
π« :all π«
All exported functions.
πΈ :const πΈ
ASN_BOOLEAN, ASN_INTEGER, ASN_BIT_STR, ASN_OCTET_STR, ASN_NULL, ASN_OBJECT_ID, ASN_REAL, ASN_ENUMERATED, ASN_SEQUENCE, ASN_SET, ASN_PRINT_STR, ASN_IA5_STR, ASN_UTC_TIME, ASN_GENERAL_TIME, ASN_UNIVERSAL, ASN_APPLICATION, ASN_CONTEXT, ASN_PRIVATE, ASN_PRIMITIVE, ASN_CONSTRUCTOR, ASN_LONG_LEN, ASN_EXTENSION_ID, ASN_BIT
π¦ :debug π¦
asn_dump, asn_hexdump
π :io π
asn_recv, asn_send, asn_read, asn_write, asn_get, asn_ready
β :tag β
asn_tag, asn_decode_tag, asn_encode_tag, asn_decode_length, asn_encode_length
πΊοΈ MAPPING ASN.1 TO PERL πΊοΈ
Okay bestie, so every element in the ASN.1 definition has a name, and in Perl a hash is used with these names as an index and the element value as the hash value. Itβs giving βnames in, values outβ and honestly it slays π
# ASN.1
int INTEGER,
str OCTET STRING
# Perl
{ int => 5, str => "text" }
When itβs a SEQUENCE, SET or CHOICE, the value in the namespace will be a hash reference, and that hash reference will be the namespace for the elements with that element. Nested-hash main character energy π«Ά
# ASN.1
int INTEGER,
seq SEQUENCE {
str OCTET STRING,
bool BOOLEAN
}
# Perl
{ int => 5, seq => { str => "text", bool => 1}}
If the element is a SEQUENCE OF, or SET OF, then the value in the namespace will be an array reference. The elements in the array will be of the type expected by the type following the OF. For example, with SEQUENCE OF STRING the array would contain strings. With SEQUENCE OF SEQUENCE { ... } the array will contain hash references which will be used as namespaces. Arrays of little namespaces? Obsessed actually β¨
# ASN.1
int INTEGER,
str SEQUENCE OF OCTET STRING
# Perl
{ int => 5, str => [ "text1", "text2"]}
# ASN.1
int INTEGER,
str SEQUENCE OF SEQUENCE {
type OCTET STRING,
value INTEGER
}
# Perl
{ int => 5, str => [
{ type => "abc", value => 4 },
{ type => "def", value => -1 },
]}
Finally, if you wish to pre-parse ASN.1 and hold it to include inline in your PDU, you can coerce it into the ASN.1 spec by defining the value as ANY in the schema, and then pass the pre encoded value inline. Pre-encoded glitter, basically π
# ASN.1
int INTEGER,
str OCTET STRING,
pre ANY
# Perl
{ int => 5, str => "text", pre=>"\x03\x03\x00\x0a\x05" }
passes a pre-encoded BIT STRING instance as hex text. -But it could be a previous run of $obj->encode() from another run held in some variable.
π¨ Exceptions π¨
There are some exceptions where Convert::ASN1 does not require an element to be named. These are SEQUENCE {...}, SET {...} and CHOICE. In each case if the element is not given a name then the elements inside the {...} will share the same namespace as the elements outside of the {...}. No extra name tag needed, bestie β theyβre all sharing the vibe check π
π TODO π
- XS implementation.
- More documentation.
- More tests.
π AUTHOR π
Graham Barr <gbarr@cpan.org>
π«Ά SUPPORT π«Ά
Report issues via github at https://github.com/gbarr/perl-Convert-ASN1/issues
To contribute I encourage you to create a git fork of the repository at https://github.com/gbarr/perl-Convert-ASN1 do you work on a fresh branch created from master and submit a pull request
πΈ COPYRIGHT πΈ
Copyright (c) 2000-2012 Graham Barr <gbarr@cpan.org>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Thatβs the whole page, bestie β source/version perl v5.42.0, dated 2024-03-08, and itβs Convert::ASN1(3). Go forth and encode iconically πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Convert-ASN1 0.340.0-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms