ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-BER-XS/XS.pm
(Generate patch)

Comparing Convert-BER-XS/XS.pm (file contents):
Revision 1.8 by root, Fri Apr 19 20:42:31 2019 UTC vs.
Revision 1.18 by root, Sat Apr 20 13:48:53 2019 UTC

7 use Convert::BER::XS ':all'; 7 use Convert::BER::XS ':all';
8 8
9 my $ber = ber_decode $buf 9 my $ber = ber_decode $buf
10 or die "unable to decode SNMP message"; 10 or die "unable to decode SNMP message";
11 11
12 # The above results in a data structure consisting of (class, tag, 12 # The above results in a data structure consisting of
13 # (class, tag, # constructed, data)
13 # constructed, data) tuples. Below is such a message, SNMPv1 trap 14 # tuples. Below is such a message, SNMPv1 trap
14 # with a Cisco mac change notification. 15 # with a Cisco mac change notification.
15 # Did you know that Cisco is in the news almost every week because 16 # Did you know that Cisco is in the news almost
17 # every week because # of some backdoor password
16 # of some backdoor password or other extremely stupid security bug? 18 # or other extremely stupid security bug?
17 19
18 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, 20 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1,
19 [ 21 [
20 [ ASN_UNIVERSAL, ASN_INTEGER32, 0, 0 ], # snmp version 1 22 [ ASN_UNIVERSAL, ASN_INTEGER32, 0, 0 ], # snmp version 1
21 [ ASN_UNIVERSAL, 4, 0, "public" ], # community 23 [ ASN_UNIVERSAL, 4, 0, "public" ], # community
71This module implements a I<very> low level BER/DER en-/decoder. 73This module implements a I<very> low level BER/DER en-/decoder.
72 74
73If is tuned for low memory and high speed, while still maintaining some 75If is tuned for low memory and high speed, while still maintaining some
74level of user-friendlyness. 76level of user-friendlyness.
75 77
76Currently, not much is documented, as this is an initial release to
77reserve CPAN namespace, stay tuned for a few days.
78
79=head2 ASN.1/BER/DER/... BASICS 78=head2 ASN.1/BER/DER/... BASICS
80 79
81ASN.1 is a strange language that can be sed to describe protocols and 80ASN.1 is a strange language that can be used to describe protocols and
82data structures. It supports various mappings to JSON, XML, but most 81data structures. It supports various mappings to JSON, XML, but most
83importantly, to a various binary encodings such as BER, that is the topic 82importantly, to a various binary encodings such as BER, that is the topic
84of this module, and is used in SNMP or LDAP for example. 83of this module, and is used in SNMP or LDAP for example.
85 84
86While ASN.1 defines a schema that is useful to interpret encoded data, 85While ASN.1 defines a schema that is useful to interpret encoded data,
87the BER encoding is actually somehat self-describing: you might not know 86the BER encoding is actually somewhat self-describing: you might not know
88whether something is a string or a number or a sequence or something else, 87whether something is a string or a number or a sequence or something else,
89but you can nevertheless decode the overall structure, even if you end up 88but you can nevertheless decode the overall structure, even if you end up
90with just a binary blob for the actual value. 89with just a binary blob for the actual value.
91 90
92This works because BER values are tagged with a type and a namespace, 91This works because BER values are tagged with a type and a namespace,
93and also have a flag that says whther a value consists of subvalues (is 92and also have a flag that says whether a value consists of subvalues (is
94"constructed") or not (is "primitive"). 93"constructed") or not (is "primitive").
95 94
96Tags are simple integers, and ASN.1 defines a somewhat weird assortment of 95Tags are simple integers, and ASN.1 defines a somewhat weird assortment of
97those - for example, you have 32 bit signed integers and 16(!) different 96those - for example, you have 32 bit signed integers and 16(!) different
98string types, but there is no unsigned32 type for example. Different 97string types, but there is no unsigned32 type for example. Different
120I<DATA> member, and you may re-assign the array itself, e.g.: 119I<DATA> member, and you may re-assign the array itself, e.g.:
121 120
122 $ber = ber_decode $binbuf; 121 $ber = ber_decode $binbuf;
123 122
124 # the following is NOT legal: 123 # the following is NOT legal:
125 $ber->[BER_CLASS] = ASN_PRIVATE; # ERROR, readonly(!) 124 $ber->[BER_CLASS] = ASN_PRIVATE; # ERROR, CLASS/TAG/CONSTRUCTED are READ ONLY(!)
126 125
127 # but all of the following are fine: 126 # but all of the following are fine:
128 $ber->[BER_DATA] = "string"; 127 $ber->[BER_DATA] = "string";
129 $ber->[BER_DATA] = [ASN_UNIVERSAL, ASN_INTEGER32, 0, 123]; 128 $ber->[BER_DATA] = [ASN_UNIVERSAL, ASN_INTEGER32, 0, 123];
130 @$ber = (ASN_APPLICATION, SNMP_TIMETICKS, 1000); 129 @$ber = (ASN_APPLICATION, SNMP_TIMETICKS, 0, 1000);
131 130
132I<CLASS> is something like a namespace for I<TAG>s - there is the 131I<CLASS> is something like a namespace for I<TAG>s - there is the
133C<ASN_UNIVERSAL> namespace which defines tags common to all ASN.1 132C<ASN_UNIVERSAL> namespace which defines tags common to all ASN.1
134implementations, the C<ASN_APPLICATION> namespace which defines tags for 133implementations, the C<ASN_APPLICATION> namespace which defines tags for
135specific applications (for example, the SNMP C<Unsigned32> type is in this 134specific applications (for example, the SNMP C<Unsigned32> type is in this
249true. 248true.
250 249
251=item $bool = ber_is_oid $tuple, $oid_string 250=item $bool = ber_is_oid $tuple, $oid_string
252 251
253Returns true if the C<$tuple> represents an ASN_OBJECT_IDENTIFIER 252Returns true if the C<$tuple> represents an ASN_OBJECT_IDENTIFIER
254that exactly matches C$oid_string>. Exmaple: 253that exactly matches C<$oid_string>. Example:
255 254
256 ber_is_oid $tuple, "1.3.6.1.4" 255 ber_is_oid $tuple, "1.3.6.1.4"
257 or die "oid must be 1.3.6.1.4"; 256 or die "oid must be 1.3.6.1.4";
258 257
259=item $oid = ber_is_oid $tuple 258=item $oid = ber_is_oid $tuple
287use common::sense; 286use common::sense;
288 287
289use XSLoader (); 288use XSLoader ();
290use Exporter qw(import); 289use Exporter qw(import);
291 290
292our $VERSION = 0.2; 291our $VERSION;
293 292
293BEGIN {
294 $VERSION = 0.8;
294XSLoader::load __PACKAGE__, $VERSION; 295 XSLoader::load __PACKAGE__, $VERSION;
296}
295 297
296our %EXPORT_TAGS = ( 298our %EXPORT_TAGS = (
297 const => [qw( 299 const => [qw(
298 BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA 300 BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA
299 301
300 ASN_BOOLEAN ASN_INTEGER32 ASN_BIT_STRING ASN_OCTET_STRING ASN_NULL ASN_OBJECT_IDENTIFIER ASN_TAG_BER ASN_TAG_MASK 302 ASN_BOOLEAN ASN_INTEGER32 ASN_BIT_STRING ASN_OCTET_STRING ASN_NULL ASN_OBJECT_IDENTIFIER
301 ASN_CONSTRUCTED ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE ASN_CLASS_MASK ASN_CLASS_SHIFT 303 ASN_OBJECT_DESCRIPTOR ASN_OID ASN_EXTERNAL ASN_REAL ASN_SEQUENCE ASN_ENUMERATED
302 ASN_SEQUENCE 304 ASN_EMBEDDED_PDV ASN_UTF8_STRING ASN_RELATIVE_OID ASN_SET ASN_NUMERIC_STRING
303 305 ASN_PRINTABLE_STRING ASN_TELETEX_STRING ASN_T61_STRING ASN_VIDEOTEX_STRING ASN_IA5_STRING
306 ASN_ASCII_STRING ASN_UTC_TIME ASN_GENERALIZED_TIME ASN_GRAPHIC_STRING ASN_VISIBLE_STRING
307 ASN_ISO646_STRING ASN_GENERAL_STRING ASN_UNIVERSAL_STRING ASN_CHARACTER_STRING ASN_BMP_STRING
308
309 ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE
310
311 BER_TYPE_BYTES BER_TYPE_UTF8 BER_TYPE_UCS2 BER_TYPE_UCS4 BER_TYPE_INT
312 BER_TYPE_OID BER_TYPE_RELOID BER_TYPE_NULL BER_TYPE_BOOL BER_TYPE_REAL
313 BER_TYPE_IPADDRESS BER_TYPE_CROAK
314 )],
315 const_snmp => [qw(
304 SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64 316 SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64
305 )], 317 )],
306 encode => [qw( 318 encode => [qw(
307 ber_decode 319 ber_decode
308 ber_is ber_is_seq ber_is_i32 ber_is_oid 320 ber_is ber_is_seq ber_is_i32 ber_is_oid
309 )], 321 )],
310 decode => [qw( 322 decode => [qw(
311 ber_encode 323 ber_encode
324 ber_i32
312 )], 325 )],
313); 326);
314 327
315our @EXPORT_OK = map @$_, values %EXPORT_TAGS; 328our @EXPORT_OK = map @$_, values %EXPORT_TAGS;
316 329
317$EXPORT_TAGS{all} = \@EXPORT_OK; 330$EXPORT_TAGS{all} = \@EXPORT_OK;
318 331
332=head1 PROFILES
333
334While any BER data can be correctly encoded and decoded out of the box, it
335can be inconvenient to have to manually decode some values into a "better"
336format: for instance, SNMP TimeTicks values are decoded into the raw octet
337strings of their BER representation, which is quite hard to decode. With
338profiles, you can change which class/tag combinations map to which decoder
339function inside C<ber_decode> (and of course also which encoder functions
340are used in C<ber_encode>).
341
342This works by mapping specific class/tag combinations to an internal "ber
343type".
344
345The default profile supports the standard ASN.1 types, but no
346application-specific ones. This means that class/tag combinations not in
347the base set of ASN.1 are decoded into their raw octet strings.
348
349C<Convert::BER::XS> defines two profile variables you can use out of the box:
350
351=over
352
353=item C<$Convert::BER::XS::DEFAULT_PROFILE>
354
355This is the default profile, i.e. the profile that is used when no
356profile is specified for de-/encoding.
357
358You can modify it, but remember that this modifies the defaults for all
359callers that rely on the default profile.
360
361=item C<$Convert::BER::XS::SNMP_PROFILE>
362
363A profile with mappings for SNMP-specific application tags added. This is
364useful when de-/encoding SNMP data.
365
366Example:
367
368 $ber = ber_decode $data, $Convert::BER::XS::SNMP_PROFILE;
369
370=back
371
372=head2 The Convert::BER::XS::Profile class
373
374=over
375
376=item $profile = new Convert::BER::XS::Profile
377
378Create a new profile. The profile will be identical to the default
379profile.
380
381=item $profile->set ($class, $tag, $type)
382
383Sets the mapping for the given C<$class>/C<$tag> combination to C<$type>,
384which must be one of the C<BER_TYPE_*> constants.
385
386Note that currently, the mapping is stored in a flat array, so large
387values of C<$tag> will consume large amounts of memory.
388
389Example:
390
391 $profile = new Convert::BER::XS::Profile;
392 $profile->set (ASN_APPLICATION, SNMP_COUNTER32, BER_TYPE_INT);
393 $ber = ber_decode $data, $profile;
394
395=item $type = $profile->get ($class, $tag)
396
397Returns the BER type mapped to the given C<$class>/C<$tag> combination.
398
399=back
400
401=head2 BER TYPES
402
403This lists the predefined BER types - you can map any C<CLASS>/C<TAG>
404combination to any C<BER_TYPE_*>.
405
406=over
407
408=item C<BER_TYPE_BYTES>
409
410The raw octets of the value. This is the default type for unknown tags and
411de-/encodes the value as if it were an octet string, i.e. by copying the
412raw bytes.
413
414=item C<BER_TYPE_UTF8>
415
416Like C<BER_TYPE_BYTES>, but decodes the value as if it were a UTF-8 string
417(without validation!) and encodes a perl unicode string into a UTF-8 BER
418string.
419
420=item C<BER_TYPE_UCS2>
421
422Similar to C<BER_TYPE_UTF8>, but treats the BER value as UCS-2 encoded
423string.
424
425=item C<BER_TYPE_UCS4>
426
427Similar to C<BER_TYPE_UTF8>, but treats the BER value as UCS-4 encoded
428string.
429
430=item C<BER_TYPE_INT>
431
432Encodes and decodes a BER integer value to a perl integer scalar. This
433should correctly handle 64 bit signed and unsigned values.
434
435=item C<BER_TYPE_OID>
436
437Encodes and decodes an OBJECT IDENTIFIER into dotted form without leading
438dot, e.g. C<1.3.6.1.213>.
439
440=item C<BER_TYPE_RELOID>
441
442Same as C<BER_TYPE_OID> but uses relative object identifier
443encoding: ASN.1 has this hack of encoding the first two OID components
444into a single integer in a weird attempt to save an insignificant amount
445of space in an otherwise wasteful encoding, and relative OIDs are
446basically OIDs without this hack. The practical difference is that the
447second component of an OID can only have the values 1..40, while relative
448OIDs do not have this restriction.
449
450=item C<BER_TYPE_NULL>
451
452Decodes an C<ASN_NULL> value into C<undef>, and always encodes a
453C<ASN_NULL> type, regardless of the perl value.
454
455=item C<BER_TYPE_BOOL>
456
457Decodes an C<ASN_BOOLEAN> value into C<0> or C<1>, and encodes a perl
458boolean value into an C<ASN_BOOLEAN>.
459
460=item C<BER_TYPE_REAL>
461
462Decodes/encodes a BER real value. NOT IMPLEMENTED.
463
464=item C<BER_TYPE_IPADDRESS>
465
466Decodes/encodes a four byte string into an IPv4 dotted-quad address string
467in Perl. Given the obsolete nature of this type, this is a low-effort
468implementation that simply uses C<sprintf> and C<sscanf>-style conversion,
469so it won't handle all string forms supported by C<inet_aton> for example.
470
471=item C<BER_TYPE_CROAK>
472
473Always croaks when encountered during encoding or decoding - the
474default behaviour when encountering an unknown type is to treat it as
475C<BER_TYPE_BYTES>. When you don't want that but instead prefer a hard
476error for some types, then C<BER_TYPE_CROAK> is for you.
477
478=back
479
480=cut
481
482our $DEFAULT_PROFILE = new Convert::BER::XS::Profile;
483our $SNMP_PROFILE = new Convert::BER::XS::Profile;
484
485$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_IPADDRESS , BER_TYPE_IPADDRESS);
486$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER32 , BER_TYPE_INT);
487$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_UNSIGNED32, BER_TYPE_INT);
488$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_TIMETICKS , BER_TYPE_INT);
489$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_OPAQUE , BER_TYPE_IPADDRESS);
490$SNMP_PROFILE->set (ASN_APPLICATION, SNMP_COUNTER64 , BER_TYPE_INT);
491
492$DEFAULT_PROFILE->_set_default;
493
3191; 4941;
320 495
321=head2 BUGS / SHORTCOMINGs 496=head2 LIMITATIONS
322 497
323This module does have a number of SNMPisms hardcoded, such as the SNMP 498This module can only en-/decode 64 bit signed and unsigned integers, and
324tags for Unsigned32 and so on. More configurability is needed, and, if 499only when your perl supports those.
325ever implemented, will come in a form similar to how L<JSON::XS> and 500
326L<CBOR::XS> respresent things, namely with an object-oriented interface. 501OBJECT IDENTIFIEERs cannot have unlimited length, although the limit is
502much larger than e.g. the one imposed by SNMP or other protocols.
503
504REAL values are not supported and will croak.
505
506This module has undergone little to no testing so far.
507
508=head2 ITHREADS SUPPORT
509
510This module is unlikely to work when the (officially discouraged) ithreads
511are in use.
327 512
328=head1 AUTHOR 513=head1 AUTHOR
329 514
330 Marc Lehmann <schmorp@schmorp.de> 515 Marc Lehmann <schmorp@schmorp.de>
331 http://software.schmorp.de/pkg/Convert-BER-XS 516 http://software.schmorp.de/pkg/Convert-BER-XS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines