home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / ASN1.pod < prev    next >
Encoding:
Text File  |  2003-04-29  |  9.8 KB  |  445 lines

  1. =head1 NAME
  2.  
  3.   Convert::ASN1 - ASN.1 Encode/Decode library
  4.  
  5. =head1 SYNOPSYS
  6.  
  7.   use Convert::ASN1;
  8.  
  9.   $asn = Convert::ASN1->new;
  10.   $asn->prepare(q<
  11.  
  12.     [APPLICATION 7] SEQUENCE {
  13.       int INTEGER,
  14.       str OCTET STRING
  15.     }
  16.  
  17.   >);
  18.  
  19.   $pdu = $asn->encode( int => 7, str => "string");
  20.  
  21.   $out = $asn->decode($pdu);
  22.   print $out->{int}," ",$out->{str},"\n";
  23.  
  24.   use Convert::ASN1 qw(:io);
  25.  
  26.   $peer   = asn_recv($sock,$buffer,0);
  27.   $nbytes = asn_read($fh, $buffer);
  28.   $nbytes = asn_send($sock, $buffer, $peer);
  29.   $nbytes = asn_send($sock, $buffer);
  30.   $nbytes = asn_write($fh, $buffer);
  31.   $buffer = asn_get($fh);
  32.   $yes    = asn_ready($fh)
  33.  
  34. =head1 DESCRIPTION
  35.  
  36. Convert::ASN1 encodes and decodes ASN.1 data structures using BER/DER
  37. rules.
  38.  
  39. =head1 METHODS
  40.  
  41. =head2 new
  42.  
  43. Contructor, creates a new object.
  44.  
  45. =head2 error
  46.  
  47. Returns the last error.
  48.  
  49. =head2 configure ( OPTIONS )
  50.  
  51. Configure options to control how Convert::ASN1 will perform various tasks.
  52. Options are passed as name-value pairs.
  53.  
  54. =over 4
  55.  
  56. =item encode
  57.  
  58. Reference to a hash which contains various encode options.
  59.  
  60. =item decode
  61.  
  62. Reference to a hash which contains various decode options.
  63.  
  64. =item encoding
  65.  
  66. One of 'BER' or 'DER'. The default is 'BER'
  67.  
  68. =back
  69.  
  70. Encode options
  71.  
  72. =over 4
  73.  
  74. =item real
  75.  
  76. Which encoding to use for real's. One of 'binary', 'nr1', 'nr2', 'nr3'
  77.  
  78. =item time
  79.  
  80. This controls how UTCTime and GeneralizedTime elements are encoded. The default
  81. is C<withzone>.
  82.  
  83. =over 4
  84.  
  85. =item utctime
  86.  
  87. The value passed will be encoded without a zone, ie a UTC value.
  88.  
  89. =item withzone
  90.  
  91. The value will be encoded with a zone. By default it will be encoded
  92. using the local time offset. The offset may be set using the C<timezone>
  93. configure option.
  94.  
  95. =item raw
  96.  
  97. The value passed should already be in the correct format and will be copied
  98. into the PDU as-is.
  99.  
  100. =back
  101.  
  102. =item timezone
  103.  
  104. By default UTCTime and GeneralizedTime will be encoded using the local
  105. time offset from UTC. This will over-ride that. It is an offset from UTC
  106. in seconds.  This option can be overriden by passing a reference to a
  107. list of two values as the time value. The list should contain the time
  108. value and the offset from UTC in seconds.
  109.  
  110. =item bigint
  111.  
  112. If during encoding an value greater than 32 bits is discovered and
  113. is not already a big integer object, then the value will first be
  114. converted into a big integer object. This option controls the big
  115. integer class into which the objects will be blessed. The default
  116. is to use Math::BigInt
  117.  
  118. =back
  119.  
  120. Decode options
  121.  
  122. =over 4
  123.  
  124. =item time
  125.  
  126. This controls how a UTCTime or a GeneralizedTime element will be decoded. The default
  127. is C<utctime>.
  128.  
  129. =over 4
  130.  
  131. =item utctime
  132.  
  133. The value returned will be a time value as returned by the C<time> function.
  134.  
  135. =item withzone
  136.  
  137. The value returned will be a reference to an array of two values. The first is the
  138. same as with C<utctime>, the second is the timezone offset, in seconds, that was
  139. used in the encoding.
  140.  
  141. =item raw
  142.  
  143. The value returned will be the raw encoding as extracted from the PDU.
  144.  
  145. =back
  146.  
  147. =item bigint
  148.  
  149. If during decoding any big integers are discovered (integers greater
  150. than 32 bits), they will be decoded into big integer objects. This option
  151. controls the big integer class into which the objects will be blessed.
  152. The default is to use Math::BigInt.
  153.  
  154. =back
  155.  
  156. =head2 prepare ( ASN )
  157.  
  158. Compile the given ASN.1 descripton which can be passed as a string
  159. or as a filehandle. The syntax used is very close to ASN.1, but has
  160. a few differences. If the ASN decribes only one macro then encode/decode can be
  161. called on this object. If ASN describes more than one ASN.1 macro then C<find>
  162. must be called. The method returns undef on error.
  163.  
  164. =head2 prepare_file ( ASNPATH )
  165.  
  166. Compile the ASN.1 description to be read from the specified pathname.
  167.  
  168. =head2 find ( MACRO )
  169.  
  170. Find a macro from a prepared ASN.1 description. Returns an object which can
  171. be used for encode/decode.
  172.  
  173. =head2 encode ( VARIABLES )
  174.  
  175. Encode a PDU. Top-level variable are passed as name-value pairs, or as a reference
  176. to a hash containing them. Returns the encoded PDU, or undef on error.
  177.  
  178. =head2 decode ( PDU )
  179.  
  180. Decode the PDU, returns a reference to a hash containg the values for the PDU. Returns
  181. undef if there was an error.
  182.  
  183. =head1 EXPORTS
  184.  
  185. As well as providing an object interface for encoding/decoding PDUs Convert::ASN1
  186. also provides the following functions.
  187.  
  188. =head2 IO Functions
  189.  
  190. =over 4
  191.  
  192. =item asn_recv SOCK, BUFFER, FLAGS
  193.  
  194. Will read a single element from the socket SOCK into BUFFER.  FLAGS may
  195. be MSG_PEEK as exported by C<Socket>. Returns the address of the sender,
  196. or undef if there was an error. Some systems do not support the return
  197. of the peer address when the socket is a connected socket, in these
  198. cases the empty string will be returned. This is the same behaviour
  199. as the C<recv> function in perl itself.
  200.  
  201. It is recommended that if the socket is of type SOCK_DGRAM then C<recv>
  202. be called directly instead of calling C<asn_recv>.
  203.  
  204. =item asn_read FH, BUFFER, OFFSET
  205.  
  206. =item asn_read FH, BUFFER
  207.  
  208. Will read a single element from the filehandle FH into BUFFER. Returns the
  209. number of bytes read if a complete element was read, -1 if an incomplete
  210. element was read or undef if there was an error. If OFFSET is specified
  211. then it is assumed that BUFFER already contains an incomplete element
  212. and new data will be appended starting at OFFSET.
  213.  
  214. If FH is a socket the asn_recv is used to read the element, so the same
  215. restiction applies if FH is a socket of type SOCK_DGRAM.
  216.  
  217. =item asn_send SOCK, BUFFER, FLAGS, TO
  218.  
  219. =item asn_send SOCK, BUFFER, FLAGS
  220.  
  221. Identical to calling C<send>, see L<perlfunc>
  222.  
  223. =item asn_write FH, BUFFER
  224.  
  225. Identical to calling C<syswrite> with 2 arguments, see L<perlfunc>
  226.  
  227. =item asn_get FH
  228.  
  229. C<asn_get> provides buffered IO. Because it needs a buffer FH must be a GLOB
  230. or a reference to a GLOB. C<asn_get> will use two entries in the hash element
  231. of the GLOB to use as its buffer:
  232.  
  233.   asn_buffer - input buffer
  234.   asn_need   - number of bytes needed for the next element, if known
  235.  
  236. Returns an element or undef if there was an error.
  237.  
  238. =item asn_ready FH
  239.  
  240. C<asn_ready> works with C<asn_get>. It will return true if C<asn_get> has already
  241. read enough data into the buffer to return a complete element.
  242.  
  243. =back
  244.  
  245. =head2 Encode/Decode Functions
  246.  
  247. =over 4
  248.  
  249. =item asn_tag
  250.  
  251. =item asn_decode_tag
  252.  
  253. =item asn_encode_tag
  254.  
  255. =item asn_decode_length
  256.  
  257. =item asn_encode_length
  258.  
  259. =back
  260.  
  261. =head2 Constants
  262.  
  263. =over 4
  264.  
  265. =item ASN_BIT_STR
  266.  
  267. =item ASN_BOOLEAN
  268.  
  269. =item ASN_ENUMERATED
  270.  
  271. =item ASN_GENERAL_TIME
  272.  
  273. =item ASN_IA5_STR
  274.  
  275. =item ASN_INTEGER
  276.  
  277. =item ASN_NULL
  278.  
  279. =item ASN_OBJECT_ID
  280.  
  281. =item ASN_OCTET_STR
  282.  
  283. =item ASN_PRINT_STR
  284.  
  285. =item ASN_REAL
  286.  
  287. =item ASN_SEQUENCE
  288.  
  289. =item ASN_SET
  290.  
  291. =item ASN_UTC_TIME
  292.  
  293. =item ASN_APPLICATION
  294.  
  295. =item ASN_CONTEXT
  296.  
  297. =item ASN_PRIVATE
  298.  
  299. =item ASN_UNIVERSAL
  300.  
  301. =item ASN_PRIMITIVE
  302.  
  303. =item ASN_CONSTRUCTOR
  304.  
  305. =item ASN_LONG_LEN
  306.  
  307. =item ASN_EXTENSION_ID
  308.  
  309. =item ASN_BIT
  310.  
  311. =back
  312.  
  313. =head2 Debug Functions
  314.  
  315. =over 4
  316.  
  317. =item asn_dump
  318.  
  319. =item asn_hexdump
  320.  
  321. =back
  322.  
  323. =head1 EXPORT TAGS
  324.  
  325. =over 4
  326.  
  327. =item :all
  328.  
  329. All exported functions
  330.  
  331. =item :const
  332.  
  333. ASN_BOOLEAN,     ASN_INTEGER,      ASN_BIT_STR,      ASN_OCTET_STR,
  334. ASN_NULL,        ASN_OBJECT_ID,    ASN_REAL,         ASN_ENUMERATED,
  335. ASN_SEQUENCE,    ASN_SET,          ASN_PRINT_STR,    ASN_IA5_STR,
  336. ASN_UTC_TIME,    ASN_GENERAL_TIME,
  337. ASN_UNIVERSAL,   ASN_APPLICATION,  ASN_CONTEXT,      ASN_PRIVATE,
  338. ASN_PRIMITIVE,   ASN_CONSTRUCTOR,  ASN_LONG_LEN,     ASN_EXTENSION_ID, ASN_BIT
  339.  
  340. =item :debug
  341.  
  342. asn_dump, asn_dumphex
  343.  
  344. =item :io
  345.  
  346. asn_recv, asn_send, asn_read, asn_write, asn_get, asn_ready
  347.  
  348. =item :tag
  349.  
  350. asn_tag, asn_decode_tag, asn_encode_tag, asn_decode_length, asn_encode_length
  351.  
  352. =back
  353.  
  354. =head1 MAPPING ASN.1 TO PERL
  355.  
  356. Every element in the ASN.1 definition has a name, in perl a hash is used
  357. with these names as an index and the element value as the hash value.
  358.  
  359.   # ASN.1
  360.   int INTEGER,
  361.   str OCTET STRING
  362.  
  363.   # Perl
  364.   { int => 5, str => "text" }
  365.  
  366.  
  367. In the case of a SEQUENCE, SET or CHOICE then the value in the namespace will
  368. be a hash reference which will be the namespce for the elements with
  369. that element.
  370.  
  371.   # ASN.1
  372.   int INTEGER,
  373.   seq SEQUENCE {
  374.     str OCTET STRING,
  375.     bool BOOLEAN
  376.   }
  377.  
  378.   # Perl
  379.   { int => 5, seq => { str => "text", bool => 1}}
  380.  
  381. If the element is a SEQUENCE OF, or SET OF, then the value in the namespace
  382. will be an array reference. The elements in the array will be of
  383. the type expected by the type following the OF. For example
  384. with "SEQUENCE OF STRING" the array would contain strings. With
  385. "SEQUENCE OF SEQUENCE { ... }" the array will contain hash references
  386. which will be used as namespaces
  387.  
  388.   # ASN.1
  389.   int INTEGER,
  390.   str SEQUENCE OF OCTET STRING
  391.  
  392.   # Perl
  393.   { int => 5, str => [ "text1", "text2"]}
  394.  
  395.   # ASN.1
  396.   int INTEGER,
  397.   str SEQUENCE OF SEQUENCE {
  398.     type OCTET STRING,
  399.     value INTEGER
  400.   }
  401.  
  402.   # Perl
  403.   { int => 5, str => [
  404.     { type => "abc", value => 4 },
  405.     { type => "def", value => -1 },
  406.   ]}
  407.  
  408. =head2 Exceptions
  409.  
  410. There are some exceptions where Convert::ASN1 does not require an element to be named.
  411. These are SEQUENCE {...}, SET {...} and CHOICE. In each case if the element is not
  412. given a name then the elements inside the {...} will share the same namespace as
  413. the elements outside of the {...}.
  414.  
  415. =head1 TODO
  416.  
  417. =over 4
  418.  
  419. =item *
  420.  
  421. XS implementation.
  422.  
  423. =item *
  424.  
  425. More documentation.
  426.  
  427. =item *
  428.  
  429. More tests.
  430.  
  431. =back
  432.  
  433. =head1 AUTHOR
  434.  
  435. Graham Barr <gbarr@pobox.com>, Report bugs via <bug-Convert-ASN1@rt.cpan.org>
  436.  
  437. =head1 COPYRIGHT
  438.  
  439. Copyright (c) 2000-2002 Graham Barr <gbarr@pobox.com>. All rights reserved.
  440. This program is free software; you can redistribute it and/or
  441. modify it under the same terms as Perl itself.
  442.  
  443. =cut
  444.  
  445.