info > Convert::ASN1

πŸ“› NAME

Convert::ASN1 - ASN.1 Encode/Decode library

πŸš€ Quick Reference

Use CaseCommandDescription
πŸ†• Create convertermy $asn = Convert::ASN1->new;Instantiate a new ASN.1 converter
πŸ“¦ Prepare schema$asn->prepare(q<...>);Compile an ASN.1 definition
⬆️ Encode PDU$pdu = $asn->encode(int => 5, str => "text");Encode a data structure into BER/DER
⬇️ Decode PDU$data = $asn->decode($pdu);Decode a PDU into a Perl hash ref
πŸ“₯ Read from socketasn_recv($sock, $buffer, 0);Receive a single ASN.1 element
πŸ“€ Write to filehandleasn_write($fh, $buffer);Write a raw element to a handle
πŸ” Find macro$macro = $asn->find('MACRO');Retrieve a compiled macro from a schema
πŸ”§ Configure options$asn->configure(encoding => 'DER');Set encoding mode (BER/DER)

πŸ”’ VERSION

version 0.33

πŸ“ SYNOPSIS

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.

πŸ”§ 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.

encodeReference to a hash which contains various encode options. decodeReference to a hash which contains various decode options. encodingOne of 'BER' or 'DER'. The default is 'BER' tagdefaultOne 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

realWhich encoding to use for real’s. One of 'binary', 'nr1', 'nr2', 'nr3' timeThis controls how UTCTime and GeneralizedTime elements are encoded. The default is "withzone". utctimeThe value passed will be encoded without a zone, i.e., a UTC value. withzoneThe 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. rawThe value passed should already be in the correct format and will be copied into the PDU as-is. timezoneBy default UTCTime and GeneralizedTime will be encoded using the local time offset from UTC. This will override 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. bigintIf during encoding a value greater than 32 bits is discovered and is not already a big integer object, the value will be first converted into a big integer object. This option controls the big integer class into which the objects will be blessed. The default is to use Math::BigInt

🧾 Decode options

timeControls how a UTCTime or GeneralizedTime element will be decoded. The default is "utctime". utctimeThe value returned is a time value as returned by the time function. withzoneThe 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. rawThe value returned is the raw encoding as extracted from the PDU. bigintIf 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 use Math::BigInt. nullThe value to decode ASN.1 NULL types into. If not set, it defaults to 1.

πŸ“₯ 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 variables 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 socket SOCK into BUFFER. FLAGS may be MSG_PEEK as exported by Socket. Returns the address of the sender, or undef if 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 the recv function in perl itself. It is recommended that if the socket is of type SOCK_DGRAM then recv be called directly instead of calling asn_recv. asn_read ( FH, BUFFER, OFFSET )
asn_read ( FH, BUFFER ) Will read a single element from the filehandle FH into BUFFER. Returns the number of bytes read if a complete element was read, -1 if an incomplete element was read or undef if there was an error. If OFFSET is specified then it is assumed that BUFFER already contains an incomplete element and new data will be appended starting at OFFSET. If FH is a socket, asn_recv is used to read the element, so the same restriction applies if FH is a socket of type SOCK_DGRAM. asn_send ( SOCK, BUFFER, FLAGS, TO )
asn_send ( SOCK, BUFFER, FLAGS ) Identical to calling send, see perlfunc. asn_write ( FH, BUFFER ) Identical to calling syswrite with 2 arguments, see perlfunc. asn_get ( FH ) asn_get provides buffered IO. Because it needs a buffer FH must be a GLOB or a reference to a GLOB. asn_get will use two entries in the hash element of the GLOB 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_ready works with asn_get. It will return true if asn_get has already read enough data into the buffer to return a complete element.

πŸ” Encode/Decode Functions

asn_tag ( CLASS, VALUE ) Given CLASS and a VALUE, calculate an integer which when encoded will become the tag. asn_decode_tag ( TAG ) Decode the given ASN.1 encoded TAG. asn_encode_tag ( TAG ) Encode TAG value for encoding. We assume that the tag has been correctly generated with asn_tag ( CLASS, VALUE ). asn_decode_length ( LEN ) Decode the given ASN.1 decoded LEN. asn_encode_length ( LEN ) Encode the given LEN to its ASN.1 encoding.

πŸ“Œ Constants

🐞 Debug Functions

asn_dump ( [FH,] BUFFER ) Try to decode the given buffer as ASN.1 structure and dump it to the given file handle, or STDERR if the handle is not given. asn_hexdump ( FH, BUFFER ) Dump a hex representation of the buffer.

🏷️ EXPORT TAGS

:allAll exported functions :constASN_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 :debugasn_dump, asn_hexdump :ioasn_recv, asn_send, asn_read, asn_write, asn_get, asn_ready :tagasn_tag, asn_decode_tag, asn_encode_tag, asn_decode_length, asn_encode_length

πŸ—ΊοΈ MAPPING ASN.1 TO PERL

Every element in the ASN.1 definition has a name. In perl a hash is used with these names as an index and the element value as the hash value.

# ASN.1
int INTEGER,
str OCTET STRING

# Perl
{ int => 5, str => "text" }

In the case of a SEQUENCE, SET or CHOICE then the value in the namespace will be a hash reference which will be the namespace for the elements inside.

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

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

# 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 {...}.

πŸ“‹ TODO

πŸ‘€ AUTHOR

Graham Barr <gbarr AT 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 your work on a fresh branch created from master and submit a pull request.

©️ COPYRIGHT

Copyright (c) 2000-2012 Graham Barr <gbarr AT 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.

Convert::ASN1
πŸ“› NAME πŸš€ Quick Reference πŸ”’ VERSION πŸ“ SYNOPSIS πŸ“– DESCRIPTION πŸ”§ METHODS
πŸ†• new ( [OPTIONS] ) ❗ error () βš™οΈ configure ( OPTIONS ) πŸ“₯ prepare ( ASN ) πŸ“‚ prepare_file ( ASNPATH ) πŸ” find ( MACRO ) ⬆️ encode ( VARIABLES ) ⬇️ decode ( PDU ) πŸ“‡ registeroid ( OID, HANDLER ) 🏷️ registertype ( NAME, OID, HANDLER )
πŸ“€ EXPORTS
🌐 IO Functions πŸ” Encode/Decode Functions πŸ“Œ Constants 🐞 Debug Functions
🏷️ EXPORT TAGS πŸ—ΊοΈ MAPPING ASN.1 TO PERL
⚠️ Exceptions
πŸ“‹ TODO πŸ‘€ AUTHOR πŸ†˜ SUPPORT ©️ COPYRIGHT

Generated by phpman v4.9.25-8-g32d6339 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-08 02:34 @216.73.217.93
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-pro / taotoken.net / www.chedong.com - original format

^_top_^