man > Convert::ASN1

๐Ÿ“– NAME

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

๐Ÿš€ Quick Reference

Use CaseCommandDescription
Create a new ASN.1 object$asn = Convert::ASN1->new;Constructor for encoding/decoding
Prepare an ASN.1 schema$asn->prepare(q);Compile ASN.1 description from string
Prepare from file$asn->prepare_file($path);Compile ASN.1 from file
Find a macro$obj = $asn->find('MACRO');Select a specific macro after prepare
Encode a PDU$pdu = $asn->encode( int => 7, str => "string");Convert Perl hash to BER/DER binary
Decode a PDU$out = $asn->decode($pdu);Convert binary to Perl hash
Read ASN.1 element from socketasn_recv($sock, $buffer, 0);Receive a single element
Read ASN.1 element from filehandleasn_read($fh, $buffer);Read a single element
Send ASN.1 element via socketasn_send($sock, $buffer, $flags);Send a single element
Write ASN.1 element to filehandleasn_write($fh, $buffer);Write a single element
Buffered IO$elem = asn_get($fh);Get next element from buffer

๐Ÿ“Œ 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.

Encode options

Decode options

๐Ÿ“„ 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

๐Ÿ”‘ Encode/Decode Functions

๐Ÿงฎ Constants

๐Ÿ› Debug Functions

๐Ÿท๏ธ EXPORT TAGS

๐Ÿ”„ 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 with that element.

# 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 you 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.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-06 16:37 @2600:1f28:365:80b0:b86a:6c6a:14fb:cc81
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^