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.1 by root, Fri Apr 19 16:19:36 2019 UTC vs.
Revision 1.6 by root, Fri Apr 19 20:38:38 2019 UTC

1=head1 NAME 1=head1 NAME
2 2
3Convert::BER::XS - I<very> low level BER decoding 3Convert::BER::XS - I<very> low level BER en-/decoding
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
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 v1/v2c Message"; 10 or die "unable to decode SNMP message";
11
12 # The above results in a data structure consisting of (class, tag,
13 # constructed, data) tuples. Below is such a message, SNMPv1 trap
14 # with a Cisco mac change notification.
15 # Did you know that Cisco is in the news almost every week because
16 # of some backdoor password or other extremely stupid security bug?
17
18 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1,
19 [
20 [ ASN_UNIVERSAL, ASN_INTEGER32, 0, 0 ], # snmp version 1
21 [ ASN_UNIVERSAL, 4, 0, "public" ], # community
22 [ ASN_CONTEXT, 4, 1, # CHOICE, constructed - trap PDU
23 [
24 [ ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, 0, "1.3.6.1.4.1.9.9.215.2" ], # enterprise oid
25 [ ASN_APPLICATION, 0, 0, "\x0a\x00\x00\x01" ], # SNMP IpAddress, 10.0.0.1
26 [ ASN_UNIVERSAL, ASN_INTEGER32, 0, 6 ], # generic trap
27 [ ASN_UNIVERSAL, ASN_INTEGER32, 0, 1 ], # specific trap
28 [ ASN_APPLICATION, ASN_TIMETICKS, 0, 1817903850 ], # SNMP TimeTicks
29 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, # the varbindlist
30 [
31 [ ASN_UNIVERSAL, ASN_SEQUENCE, 1, # a single varbind, "key value" pair
32 [
33 [ ASN_UNIVERSAL, ASN_OBJECT_IDENTIFIER, 0, "1.3.6.1.4.1.9.9.215.1.1.8.1.2.1" ], # the oid
34 [ ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "...data..." # the value
35 ]
36 ]
37 ],
38 ...
39
40 # let's decode it a bit with some helper functions
11 41
12 my $msg = ber_is_seq $ber 42 my $msg = ber_is_seq $ber
13 or die "SNMP message does not start with a sequence"; 43 or die "SNMP message does not start with a sequence";
14 44
15 ber_is $msg->[0], ASN_UNIVERSAL, ASN_INTEGER32, 0 45 ber_is $msg->[0], ASN_UNIVERSAL, ASN_INTEGER32, 0
16 or die "SNMP message does not start with snmp version\n"; 46 or die "SNMP message does not start with snmp version\n";
17 47
48 # message is SNMP v1 or v2c?
18 if ($msg->[0][BER_DATA] == 0 || $msg->[0][BER_DATA] == 1) { 49 if ($msg->[0][BER_DATA] == 0 || $msg->[0][BER_DATA] == 1) {
50
19 # message is SNMP v1 or v2c 51 # message is v1 trap?
20
21 if (ber_is $msg->[2], ASN_CONTEXT, 4, 1) { 52 if (ber_is $msg->[2], ASN_CONTEXT, 4, 1) {
22 # message is v1 trap
23 my $trap = $msg->[2][BER_DATA]; 53 my $trap = $msg->[2][BER_DATA];
24 54
25 # check whether trap is a cisco mac notification mac changed message 55 # check whether trap is a cisco mac notification mac changed message
26 if ( 56 if (
27 (ber_is_oid $trap->[0], "1.3.6.1.4.1.9.9.215.2") # cmnInterfaceObjects 57 (ber_is_oid $trap->[0], "1.3.6.1.4.1.9.9.215.2") # cmnInterfaceObjects
28 and (ber_is_i32 $trap->[2], 6) 58 and (ber_is_i32 $trap->[2], 6)
29 and (ber_is_i32 $trap->[3], 1) # mac changed msg 59 and (ber_is_i32 $trap->[3], 1) # mac changed msg
30 ) { 60 ) {
31 ... and so on 61 ... and so on
32 62
63 # finally, let's encode it again and hope it results in the same bit pattern
64
65 my $buf = ber_encode $ber;
66
33=head1 DESCRIPTION 67=head1 DESCRIPTION
34 68
35This module implements a I<very> low level BER/DER decoder, and in the 69This module implements a I<very> low level BER/DER en-/decoder.
36future, probably also an encoder (tell me if you want an encoder, this
37might speed up the process of getting one).
38 70
39If is tuned for low memory and high speed, while still maintaining some 71If is tuned for low memory and high speed, while still maintaining some
40level of user-friendlyness. 72level of user-friendlyness.
41 73
42Currently, not much is documented, as this is an initial release to 74Currently, not much is documented, as this is an initial release to
43reserve CPAN namespace, stay tuned for a few days. 75reserve CPAN namespace, stay tuned for a few days.
44 76
77=head2 ASN.1/BER/DER/... BASICS
78
79ASN.1 is a strange language that can be sed to describe protocols and
80data structures. It supports various mappings to JSON, XML, but most
81importantly, to a various binary encodings such as BER, that is the topic
82of this module, and is used in SNMP or LDAP for example.
83
84While ASN.1 defines a schema that is useful to interpret encoded data,
85the BER encoding is actually somehat self-describing: you might not know
86whether 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
88with just a binary blob for the actual value.
89
90This 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
92"constructed") or not (is "primitive").
93
94Tags are simple integers, and ASN.1 defines a somewhat weird assortment of
95those - for example, you have 32 bit signed integers and 16(!) different
96string types, but there is no unsigned32 type for example. Different
97applications work around this in different ways, for example, SNMP defines
98application-specific Gauge32, Counter32 and Unsigned32, which are mapped
99to two different tags: you can distinguish between Counter32 and the
100others, but not between Gause32 and Unsigned32, without the ASN.1 schema.
101
102Ugh.
103
104=head2 DECODED BER REPRESENTATION
105
106This module represents every BER value as a 4-element tuple (actually an
107array-reference):
108
109 [CLASS, TAG, CONSTRUCTED, DATA]
110
111To avoid non-descriptive hardcoded array index numbers, this module
112defines symbolic constants to access these members: C<BER_CLASS>,
113C<BER_TAG>, C<BER_CONSTRUCTED> and C<BER_DATA>.
114
115Also, the first three members are integers with a little caveat: for
116performance reasons, these are readonly and shared, so you must not modify
117them (increment, assign to them etc.) in any way. You may modify the
118I<DATA> member, and you may re-assign the array itself, e.g.:
119
120 $ber = ber_decode $binbuf;
121
122 # the following is NOT legal:
123 $ber->[BER_CLASS] = ASN_PRIVATE; # ERROR, readonly(!)
124
125 # but all of the following are fine:
126 $ber->[BER_DATA] = "string";
127 $ber->[BER_DATA] = [ASN_UNIVERSAL, ASN_INTEGER32, 0, 123];
128 @$ber = (ASN_APPLICATION, SNMP_TIMETICKS, 1000);
129
130I<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
132implementations, the C<ASN_APPLICATION> namespace which defines tags for
133specific applications (for example, the SNMP C<Unsigned32> type is in this
134namespace), a special-purpose context namespace (C<ASN_CONTEXT>, used e.g.
135for C<CHOICE>) and a private namespace (C<ASN_PRIVATE>).
136
137The meaning of the I<TAG> depends on the namespace, and defines a
138(partial) interpretation of the data value. For example, right now, SNMP
139application namespace knowledge ix hardcoded into this module, so it
140knows that SNMP C<Unsigned32> values need to be decoded into actual perl
141integers.
142
143The most common tags in the C<ASN_UNIVERSAL> namespace are
144C<ASN_INTEGER32>, C<ASN_BIT_STRING>, C<ASN_NULL>, C<ASN_OCTET_STRING>,
145C<ASN_OBJECT_IDENTIFIER>, C<ASN_SEQUENCE>, C<ASN_SET> and
146C<ASN_IA5_STRING>.
147
148The most common tags in SNMP's C<ASN_APPLICATION> namespace
149are C<SNMP_IPADDRESS>, C<SNMP_COUNTER32>, C<SNMP_UNSIGNED32>,
150C<SNMP_TIMETICKS>, C<SNMP_OPAQUE> and C<SNMP_COUNTER64>.
151
152The I<CONSTRUCTED> flag is really just a boolean - if it is false, the
153the value is "primitive" and contains no subvalues, kind of like a
154non-reference perl scalar. IF it is true, then the value is "constructed"
155which just means it contains a list of subvalues which this module will
156en-/decode as BER tuples themselves.
157
158The I<DATA> value is either a reference to an array of further tuples (if
159the value is I<CONSTRUCTED>), some decoded representation of the value,
160if this module knows how to decode it (e.g. for the integer types above)
161or a binary string with the raw octets if this module doesn't know how to
162interpret the namespace/tag.
163
164Thus, you can always decode a BER data structure and at worst you get a
165string in place of some nice decoded value.
166
167See the SYNOPSIS for an example of such an encoded tuple representation.
168
169=head2 HELPER FUNCTIONS
170
171Working 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
173functions, both to match BER tuples and to conmstruct BER tuples:
174
175=head3 MATCH HELPERS
176
177Thse functions accept a BER tuple as first argument and either paertially
178or fully match it. They often come in two forms, one which exactly matches
179a value, and one which only matches the type and returns the value.
180
181They do check whether valid tuples are passed in and croak otherwise. As
182a ease-of-use exception, they usually also accept C<undef> instead of a
183tuple reference. in which case they silently fail to match.
184
185=over
186
187=item $bool = ber_is $tuple, $class, $tag, $constructed, $data
188
189This takes a BER C<$tuple> and matches its elements agains the privded
190values, all of which are optional - values that are either missing or
191C<undef> will be ignored, the others will be matched exactly (e.g. as if
192you used C<==> or C<eq> (for C<$data>)).
193
194Some examples:
195
196 ber_is $tuple, ASN_UNIVERSAL, ASN_SEQUENCE, 1
197 orf die "tuple is not an ASN SEQUENCE";
198
199 ber_is $tuple, ASN_UNIVERSAL, ASN_NULL
200 or die "tuple is not an ASN NULL value";
201
202 ber_is $tuple, ASN_UNIVERSAL, ASN_INTEGER32, 0, 50
203 or die "BER integer must be 50";
204
205=item $seq = ber_is_seq $tuple
206
207Returns the sequence members (the array of subvalues) if the C<$tuple> is
208an ASN SEQUENCE, i.e. the C<BER_DATA> member. If the C<$tuple> is not a
209sequence it returns C<undef>. For example, SNMP version 1/2c/3 packets all
210consist of an outer SEQUENCE value:
211
212 my $ber = ber_decode $snmp_data;
213
214 my $snmp = ber_is_seq $ber
215 or die "SNMP packet invalid: does not start with SEQUENCE";
216
217 # now we know $snmp is a sequence, so decode the SNMP version
218
219 my $version = ber_is_i32 $snmp->[0]
220 or die "SNMP packet invalid: does not start with version number";
221
222=item $bool = ber_is_i32 $tuple, $i32
223
224Returns a true value if the C<$tuple> represents an ASN INTEGER32 with
225the value C<$i32>.
226
227=item $i32 = ber_is_i32 $tuple
228
229Returns true (and extracts the integer value) if the C<$tuple> is an ASN
230INTEGER32. For C<0>, this function returns a special value that is 0 but
231true.
232
233=item $bool = ber_is_oid $tuple, $oid_string
234
235Returns true if the C<$tuple> represents an ASN_OBJECT_IDENTIFIER
236that exactly matches C$oid_string>. Exmaple:
237
238 ber_is_oid $tuple, "1.3.6.1.4"
239 or die "oid must be 1.3.6.1.4";
240
241=item $oid = ber_is_oid $tuple
242
243Returns true (and extracts the OID string) if the C<$tuple> is an ASN
244OBJECT IDENTIFIER. Otherwise, it returns C<undef>.
245
246=back
247
248=head3 CONSTRUCTION HELPERS
249
250=over
251
252=item $tuple = ber_i32 $value
253
254Constructs a new C<ASN_INTEGER32> tuple.
255
256=back
257
258=head2 RELATIONSHIP TO L<Convert::BER> and L<Convert::ASN1>
259
260This module is I<not> the XS version of L<Convert::BER>, but a different
261take at doing the same thing. I imagine this module would be a good base
262for speeding up either of these, or write a similar module, or write your
263own LDAP or SNMP module for example.
264
45=cut 265=cut
46 266
47package Convert::BER::XS; 267package Convert::BER::XS;
48 268
49use common::sense; 269use common::sense;
50 270
51use XSLoader (); 271use XSLoader ();
52use Exporter qw(import); 272use Exporter qw(import);
53 273
54our $VERSION = '0.0'; 274our $VERSION = 0.1;
55 275
56XSLoader::load __PACKAGE__, $VERSION; 276XSLoader::load __PACKAGE__, $VERSION;
57 277
58our %EXPORT_TAGS = ( 278our %EXPORT_TAGS = (
59 all => [qw( 279 const => [qw(
280 BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA
281
282 ASN_BOOLEAN ASN_INTEGER32 ASN_BIT_STRING ASN_OCTET_STRING ASN_NULL ASN_OBJECT_IDENTIFIER ASN_TAG_BER ASN_TAG_MASK
283 ASN_CONSTRUCTED ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE ASN_CLASS_MASK ASN_CLASS_SHIFT
284 ASN_SEQUENCE
285
286 SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64
287 )],
288 encode => [qw(
60 ber_decode 289 ber_decode
61 ber_is ber_is_seq ber_is_i32 ber_is_oid 290 ber_is ber_is_seq ber_is_i32 ber_is_oid
62 BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA 291 )],
63 ASN_BOOLEAN ASN_INTEGER32 ASN_BIT_STRING ASN_OCTET_STRING ASN_NULL ASN_OBJECT_IDENTIFIER ASN_TAG_BER ASN_TAG_MASK 292 decode => [qw(
64 ASN_CONSTRUCTED ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE ASN_CLASS_MASK ASN_CLASS_SHIFT 293 ber_encode
65 ASN_SEQUENCE ASN_IPADDRESS ASN_COUNTER32 ASN_UNSIGNED32 ASN_TIMETICKS ASN_OPAQUE ASN_COUNTER64
66 )], 294 )],
67); 295);
68 296
69our @EXPORT_OK = map @$_, values %EXPORT_TAGS; 297our @EXPORT_OK = map @$_, values %EXPORT_TAGS;
70 298
299$EXPORT_TAGS{all} = \@EXPORT_OK;
300
711; 3011;
302
303=head2 BUGS / SHORTCOMINGs
304
305This module does have a number of SNMPisms hardcoded, such as the SNMP
306tags for Unsigned32 and so on. More configurability is needed, and, if
307ever implemented, will come in a form similar to how L<JSON::XS> and
308L<CBOR::XS> respresent things, namely with an object-oriented interface.
72 309
73=head1 AUTHOR 310=head1 AUTHOR
74 311
75 Marc Lehmann <schmorp@schmorp.de> 312 Marc Lehmann <schmorp@schmorp.de>
76 http://software.schmorp.de/pkg/Convert-BER-XS 313 http://software.schmorp.de/pkg/Convert-BER-XS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines