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.6 by root, Fri Apr 19 20:38:38 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
28 [ ASN_APPLICATION, ASN_TIMETICKS, 0, 1817903850 ], # SNMP TimeTicks 30 [ ASN_APPLICATION, ASN_TIMETICKS, 0, 1817903850 ], # SNMP TimeTicks
29 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, # the varbindlist 31 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, # the varbindlist
30 [ 32 [
31 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, # a single varbind, "key value" pair 33 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, # a single varbind, "key value" pair
32 [ 34 [
33 [ ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, 0, "1.3.6.1.4.1.9.9.215.1.1.8.1.2.1" ], # the oid 35 [ ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, 0, "1.3.6.1.4.1.9.9.215.1.1.8.1.2.1" ],
34 [ ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "...data..." # the value 36 [ ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "...data..." # the value
35 ] 37 ]
36 ] 38 ]
37 ], 39 ],
38 ... 40 ...
64 66
65 my $buf = ber_encode $ber; 67 my $buf = ber_encode $ber;
66 68
67=head1 DESCRIPTION 69=head1 DESCRIPTION
68 70
71WARNING: Before release 1.0, the API is not considered stable in any way.
72
69This module implements a I<very> low level BER/DER en-/decoder. 73This module implements a I<very> low level BER/DER en-/decoder.
70 74
71If 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
72level of user-friendlyness. 76level of user-friendlyness.
73 77
74Currently, not much is documented, as this is an initial release to
75reserve CPAN namespace, stay tuned for a few days.
76
77=head2 ASN.1/BER/DER/... BASICS 78=head2 ASN.1/BER/DER/... BASICS
78 79
79ASN.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
80data structures. It supports various mappings to JSON, XML, but most 81data structures. It supports various mappings to JSON, XML, but most
81importantly, 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
82of this module, and is used in SNMP or LDAP for example. 83of this module, and is used in SNMP or LDAP for example.
83 84
84While 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,
85the BER encoding is actually somehat self-describing: you might not know 86the BER encoding is actually somewhat self-describing: you might not know
86whether 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,
87but 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
88with just a binary blob for the actual value. 89with just a binary blob for the actual value.
89 90
90This 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,
91and 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
92"constructed") or not (is "primitive"). 93"constructed") or not (is "primitive").
93 94
94Tags 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
95those - for example, you have 32 bit signed integers and 16(!) different 96those - for example, you have 32 bit signed integers and 16(!) different
96string types, but there is no unsigned32 type for example. Different 97string types, but there is no unsigned32 type for example. Different
118I<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.:
119 120
120 $ber = ber_decode $binbuf; 121 $ber = ber_decode $binbuf;
121 122
122 # the following is NOT legal: 123 # the following is NOT legal:
123 $ber->[BER_CLASS] = ASN_PRIVATE; # ERROR, readonly(!) 124 $ber->[BER_CLASS] = ASN_PRIVATE; # ERROR, CLASS/TAG/CONSTRUCTED are READ ONLY(!)
124 125
125 # but all of the following are fine: 126 # but all of the following are fine:
126 $ber->[BER_DATA] = "string"; 127 $ber->[BER_DATA] = "string";
127 $ber->[BER_DATA] = [ASN_UNIVERSAL, ASN_INTEGER32, 0, 123]; 128 $ber->[BER_DATA] = [ASN_UNIVERSAL, ASN_INTEGER32, 0, 123];
128 @$ber = (ASN_APPLICATION, SNMP_TIMETICKS, 1000); 129 @$ber = (ASN_APPLICATION, SNMP_TIMETICKS, 0, 1000);
129 130
130I<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
131C<ASN_UNIVERSAL> namespace which defines tags common to all ASN.1 132C<ASN_UNIVERSAL> namespace which defines tags common to all ASN.1
132implementations, the C<ASN_APPLICATION> namespace which defines tags for 133implementations, the C<ASN_APPLICATION> namespace which defines tags for
133specific applications (for example, the SNMP C<Unsigned32> type is in this 134specific applications (for example, the SNMP C<Unsigned32> type is in this
164Thus, you can always decode a BER data structure and at worst you get a 165Thus, you can always decode a BER data structure and at worst you get a
165string in place of some nice decoded value. 166string in place of some nice decoded value.
166 167
167See the SYNOPSIS for an example of such an encoded tuple representation. 168See the SYNOPSIS for an example of such an encoded tuple representation.
168 169
170=head2 DECODING AND ENCODING
171
172=over
173
174=item $tuple = ber_decoded $bindata
175
176Decodes binary BER data in C<$bindata> and returns the resulting BER
177tuple. Croaks on any decoding error, so the returned C<$tuple> is always
178valid.
179
180=item $bindata = ber_encode $tuple
181
182Encodes the BER tuple into a BER/DER data structure.
183
184=back
185
169=head2 HELPER FUNCTIONS 186=head2 HELPER FUNCTIONS
170 187
171Working with a 4-tuple for every value can be annoying. Or, rather, I<is> 188Working with a 4-tuple for every value can be annoying. Or, rather, I<is>
172annoying. To reduce this a bit, this module defines a number of helper 189annoying. To reduce this a bit, this module defines a number of helper
173functions, both to match BER tuples and to conmstruct BER tuples: 190functions, both to match BER tuples and to conmstruct BER tuples:
231true. 248true.
232 249
233=item $bool = ber_is_oid $tuple, $oid_string 250=item $bool = ber_is_oid $tuple, $oid_string
234 251
235Returns true if the C<$tuple> represents an ASN_OBJECT_IDENTIFIER 252Returns true if the C<$tuple> represents an ASN_OBJECT_IDENTIFIER
236that exactly matches C$oid_string>. Exmaple: 253that exactly matches C<$oid_string>. Example:
237 254
238 ber_is_oid $tuple, "1.3.6.1.4" 255 ber_is_oid $tuple, "1.3.6.1.4"
239 or die "oid must be 1.3.6.1.4"; 256 or die "oid must be 1.3.6.1.4";
240 257
241=item $oid = ber_is_oid $tuple 258=item $oid = ber_is_oid $tuple
269use common::sense; 286use common::sense;
270 287
271use XSLoader (); 288use XSLoader ();
272use Exporter qw(import); 289use Exporter qw(import);
273 290
274our $VERSION = 0.1; 291our $VERSION;
275 292
293BEGIN {
294 $VERSION = 0.8;
276XSLoader::load __PACKAGE__, $VERSION; 295 XSLoader::load __PACKAGE__, $VERSION;
296}
277 297
278our %EXPORT_TAGS = ( 298our %EXPORT_TAGS = (
279 const => [qw( 299 const => [qw(
280 BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA 300 BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA
281 301
282 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
283 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
284 ASN_SEQUENCE 304 ASN_EMBEDDED_PDV ASN_UTF8_STRING ASN_RELATIVE_OID ASN_SET ASN_NUMERIC_STRING
285 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(
286 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
287 )], 317 )],
288 encode => [qw( 318 encode => [qw(
289 ber_decode 319 ber_decode
290 ber_is ber_is_seq ber_is_i32 ber_is_oid 320 ber_is ber_is_seq ber_is_i32 ber_is_oid
291 )], 321 )],
292 decode => [qw( 322 decode => [qw(
293 ber_encode 323 ber_encode
324 ber_i32
294 )], 325 )],
295); 326);
296 327
297our @EXPORT_OK = map @$_, values %EXPORT_TAGS; 328our @EXPORT_OK = map @$_, values %EXPORT_TAGS;
298 329
299$EXPORT_TAGS{all} = \@EXPORT_OK; 330$EXPORT_TAGS{all} = \@EXPORT_OK;
300 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
3011; 4941;
302 495
303=head2 BUGS / SHORTCOMINGs 496=head2 LIMITATIONS
304 497
305This 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
306tags for Unsigned32 and so on. More configurability is needed, and, if 499only when your perl supports those.
307ever implemented, will come in a form similar to how L<JSON::XS> and 500
308L<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.
309 512
310=head1 AUTHOR 513=head1 AUTHOR
311 514
312 Marc Lehmann <schmorp@schmorp.de> 515 Marc Lehmann <schmorp@schmorp.de>
313 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