… | |
… | |
73 | This module implements a I<very> low level BER/DER en-/decoder. |
73 | This module implements a I<very> low level BER/DER en-/decoder. |
74 | |
74 | |
75 | If is tuned for low memory and high speed, while still maintaining some |
75 | If is tuned for low memory and high speed, while still maintaining some |
76 | level of user-friendlyness. |
76 | level of user-friendlyness. |
77 | |
77 | |
78 | Currently, not much is documented, as this is an initial release to |
|
|
79 | reserve CPAN namespace, stay tuned for a few days. |
|
|
80 | |
|
|
81 | =head2 ASN.1/BER/DER/... BASICS |
78 | =head2 ASN.1/BER/DER/... BASICS |
82 | |
79 | |
83 | ASN.1 is a strange language that can be sed to describe protocols and |
80 | ASN.1 is a strange language that can be used to describe protocols and |
84 | data structures. It supports various mappings to JSON, XML, but most |
81 | data structures. It supports various mappings to JSON, XML, but most |
85 | importantly, to a various binary encodings such as BER, that is the topic |
82 | importantly, to a various binary encodings such as BER, that is the topic |
86 | of this module, and is used in SNMP or LDAP for example. |
83 | of this module, and is used in SNMP or LDAP for example. |
87 | |
84 | |
88 | While ASN.1 defines a schema that is useful to interpret encoded data, |
85 | While ASN.1 defines a schema that is useful to interpret encoded data, |
… | |
… | |
90 | whether something is a string or a number or a sequence or something else, |
87 | whether something is a string or a number or a sequence or something else, |
91 | but you can nevertheless decode the overall structure, even if you end up |
88 | but you can nevertheless decode the overall structure, even if you end up |
92 | with just a binary blob for the actual value. |
89 | with just a binary blob for the actual value. |
93 | |
90 | |
94 | This works because BER values are tagged with a type and a namespace, |
91 | This works because BER values are tagged with a type and a namespace, |
95 | and also have a flag that says whther a value consists of subvalues (is |
92 | and also have a flag that says whether a value consists of subvalues (is |
96 | "constructed") or not (is "primitive"). |
93 | "constructed") or not (is "primitive"). |
97 | |
94 | |
98 | Tags are simple integers, and ASN.1 defines a somewhat weird assortment of |
95 | Tags are simple integers, and ASN.1 defines a somewhat weird assortment of |
99 | those - for example, you have 32 bit signed integers and 16(!) different |
96 | those - for example, you have 32 bit signed integers and 16(!) different |
100 | string types, but there is no unsigned32 type for example. Different |
97 | string types, but there is no unsigned32 type for example. Different |
… | |
… | |
347 | |
344 | |
348 | The default profile supports the standard ASN.1 types, but no |
345 | The default profile supports the standard ASN.1 types, but no |
349 | application-specific ones. This means that class/tag combinations not in |
346 | application-specific ones. This means that class/tag combinations not in |
350 | the base set of ASN.1 are decoded into their raw octet strings. |
347 | the base set of ASN.1 are decoded into their raw octet strings. |
351 | |
348 | |
352 | C<Convert::BER::XS> defines two profile variables you cna use out of the box: |
349 | C<Convert::BER::XS> defines two profile variables you can use out of the box: |
353 | |
350 | |
354 | =over |
351 | =over |
355 | |
352 | |
356 | =item C<$Convert::BER::XS::DEFAULT_PROFILE> |
353 | =item C<$Convert::BER::XS::DEFAULT_PROFILE> |
357 | |
354 | |
358 | This is the default profile, i.e. the profile that is used when no |
355 | This is the default profile, i.e. the profile that is used when no |
359 | profile is specified for de-/encoding. |
356 | profile is specified for de-/encoding. |
360 | |
357 | |
361 | You cna modify it, but remember that this modifies the defaults for all |
358 | You can modify it, but remember that this modifies the defaults for all |
362 | callers that rely on the defauit profile. |
359 | callers that rely on the default profile. |
363 | |
360 | |
364 | =item C<$Convert::BER::XS::SNMP_PROFILE> |
361 | =item C<$Convert::BER::XS::SNMP_PROFILE> |
365 | |
362 | |
366 | A profile with mappings for SNMP-specific application tags added. This is |
363 | A profile with mappings for SNMP-specific application tags added. This is |
367 | useful when de-/encoding SNMP data. |
364 | useful when de-/encoding SNMP data. |
368 | |
365 | |
369 | Example: |
366 | Example: |
|
|
367 | |
370 | $ber = ber_decode $data, $Convert::BER::XS::SNMP_PROFILE; |
368 | $ber = ber_decode $data, $Convert::BER::XS::SNMP_PROFILE; |
371 | |
369 | |
372 | =back |
370 | =back |
373 | |
371 | |
374 | =head2 The Convert::BER::XS::Profile class |
372 | =head2 The Convert::BER::XS::Profile class |
… | |
… | |
387 | |
385 | |
388 | Note that currently, the mapping is stored in a flat array, so large |
386 | Note that currently, the mapping is stored in a flat array, so large |
389 | values of C<$tag> will consume large amounts of memory. |
387 | values of C<$tag> will consume large amounts of memory. |
390 | |
388 | |
391 | Example: |
389 | Example: |
|
|
390 | |
392 | $profile = new Convert::BER::XS::Profile; |
391 | $profile = new Convert::BER::XS::Profile; |
393 | $profile->set (ASN_APPLICATION, SNMP_COUNTER32, BER_TYPE_INT); |
392 | $profile->set (ASN_APPLICATION, SNMP_COUNTER32, BER_TYPE_INT); |
394 | $ber = ber_decode $data, $profile; |
393 | $ber = ber_decode $data, $profile; |
395 | |
394 | |
396 | =item $type = $profile->get ($class, $tag) |
395 | =item $type = $profile->get ($class, $tag) |
… | |
… | |
438 | Encodes and decodes an OBJECT IDENTIFIER into dotted form without leading |
437 | Encodes and decodes an OBJECT IDENTIFIER into dotted form without leading |
439 | dot, e.g. C<1.3.6.1.213>. |
438 | dot, e.g. C<1.3.6.1.213>. |
440 | |
439 | |
441 | =item C<BER_TYPE_RELOID> |
440 | =item C<BER_TYPE_RELOID> |
442 | |
441 | |
443 | Same as C<BER_TYPE_OID> but uses relative OID encoding: ASN.1 has this |
442 | Same as C<BER_TYPE_OID> but uses relative object identifier |
444 | hack of encoding the first two OID components into a single integer in a |
443 | encoding: ASN.1 has this hack of encoding the first two OID components |
445 | weird attempt to save an insignificant amount of space in an otherwise |
444 | into a single integer in a weird attempt to save an insignificant amount |
446 | wasteful encoding, and relative OIDs are basically OIDs without this |
445 | of space in an otherwise wasteful encoding, and relative OIDs are |
447 | hack. The practical difference is that the second component of an OID |
446 | basically OIDs without this hack. The practical difference is that the |
448 | can only have the values 1..40, while relative OIDs do not have this |
447 | second component of an OID can only have the values 1..40, while relative |
449 | restriction. |
448 | OIDs do not have this restriction. |
450 | |
449 | |
451 | =item C<BER_TYPE_NULL> |
450 | =item C<BER_TYPE_NULL> |
452 | |
451 | |
453 | Decodes an C<ASN_NULL> value into C<undef>, and always encodes a |
452 | Decodes an C<ASN_NULL> value into C<undef>, and always encodes a |
454 | C<ASN_NULL> type, regardless of the perl value. |
453 | C<ASN_NULL> type, regardless of the perl value. |
… | |
… | |
462 | |
461 | |
463 | Decodes/encodes a BER real value. NOT IMPLEMENTED. |
462 | Decodes/encodes a BER real value. NOT IMPLEMENTED. |
464 | |
463 | |
465 | =item C<BER_TYPE_IPADDRESS> |
464 | =item C<BER_TYPE_IPADDRESS> |
466 | |
465 | |
467 | Decodes/encodes a four byte string into an IOv4 dotted-quad address string |
466 | Decodes/encodes a four byte string into an IPv4 dotted-quad address string |
468 | in perl. Given ther obsolete nature of this type, this is a low-effort |
467 | in Perl. Given the obsolete nature of this type, this is a low-effort |
469 | implementation that simply uses C<sprintf> and C<sscanf>-style conversion, |
468 | implementation that simply uses C<sprintf> and C<sscanf>-style conversion, |
470 | so it won't handle all string forms supported by C<inet_aton>. |
469 | so it won't handle all string forms supported by C<inet_aton> for example. |
471 | |
470 | |
472 | =item C<BER_TYPE_CROAK> |
471 | =item C<BER_TYPE_CROAK> |
473 | |
472 | |
474 | Always croaks when encountered during encoding or decoding - the |
473 | Always croaks when encountered during encoding or decoding - the |
475 | default behaviour when encountering an unknown type is to treat it as |
474 | default behaviour when encountering an unknown type is to treat it as |