1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Convert::BER::XS - I<very> low level BER decoding |
3 | Convert::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 | |
… | |
… | |
56 | and (ber_is_i32 $trap->[2], 6) |
56 | and (ber_is_i32 $trap->[2], 6) |
57 | and (ber_is_i32 $trap->[3], 1) # mac changed msg |
57 | and (ber_is_i32 $trap->[3], 1) # mac changed msg |
58 | ) { |
58 | ) { |
59 | ... and so on |
59 | ... and so on |
60 | |
60 | |
|
|
61 | # finally, let's encode it again and hope it results in the same bit pattern |
|
|
62 | |
|
|
63 | my $buf = ber_encode $ber; |
|
|
64 | |
61 | =head1 DESCRIPTION |
65 | =head1 DESCRIPTION |
62 | |
66 | |
63 | This module implements a I<very> low level BER/DER decoder, and in the |
67 | This module implements a I<very> low level BER/DER en-/decoder. |
64 | future, probably also an encoder (tell me if you want an encoder, this |
|
|
65 | might speed up the process of getting one). |
|
|
66 | |
68 | |
67 | If is tuned for low memory and high speed, while still maintaining some |
69 | If is tuned for low memory and high speed, while still maintaining some |
68 | level of user-friendlyness. |
70 | level of user-friendlyness. |
69 | |
71 | |
70 | Currently, not much is documented, as this is an initial release to |
72 | Currently, not much is documented, as this is an initial release to |
71 | reserve CPAN namespace, stay tuned for a few days. |
73 | reserve CPAN namespace, stay tuned for a few days. |
72 | |
74 | |
|
|
75 | =head2 ASN.1/BER/DER/... BASICS |
|
|
76 | |
|
|
77 | ASN.1 is a strange language that can be sed to describe protocols and |
|
|
78 | data structures. It supports various mappings to JSON, XML, but most |
|
|
79 | importantly, to a various binary encodings such as BER, that is the topic |
|
|
80 | of this module, and is used in SNMP or LDAP for example. |
|
|
81 | |
|
|
82 | While ASN.1 defines a schema that is useful to interpret encoded data, |
|
|
83 | the BER encoding is actually somehat self-describing: you might not know |
|
|
84 | whether something is a string or a number or a sequence or something else, |
|
|
85 | but you can nevertheless decode the overall structure, even if you end up |
|
|
86 | with just a binary blob for the actual value. |
|
|
87 | |
|
|
88 | This works because BER values are tagged with a type and a namespace, |
|
|
89 | and also have a flag that says whther a value consists of subvalues (is |
|
|
90 | "constructed") or not (is "primitive"). |
|
|
91 | |
|
|
92 | Tags are simple integers, and ASN.1 defines a somewhat weird assortment of |
|
|
93 | those - for example, you have 32 bit signed integers and 16(!) different |
|
|
94 | string types, but there is no unsigned32 type for example. Different |
|
|
95 | applications work around this in different ways, for example, SNMP defines |
|
|
96 | application-specific Gauge32, Counter32 and Unsigned32, which are mapped |
|
|
97 | to two different tags: you can distinguish between Counter32 and the |
|
|
98 | others, but not between Gause32 and Unsigned32, without the ASN.1 schema. |
|
|
99 | |
|
|
100 | Ugh. |
|
|
101 | |
|
|
102 | =head2 DECODED BER REPRESENTATION |
|
|
103 | |
|
|
104 | This module represents every BER value as a 4-element tuple (actually an |
|
|
105 | array-reference): |
|
|
106 | |
|
|
107 | [CLASS, TAG, CONSTRUCTED, DATA] |
|
|
108 | |
|
|
109 | I<CLASS> is something like a namespace for I<TAG>s - there is the |
|
|
110 | C<ASN_UNIVERSAL> namespace which defines tags common to all ASN.1 |
|
|
111 | implementations, the C<ASN_APPLICATION> namespace which defines tags for |
|
|
112 | specific applications (for example, the SNMP C<Unsigned32> type is in this |
|
|
113 | namespace), a special-purpose context namespace (C<ASN_CONTEXT>, used e.g. |
|
|
114 | for C<CHOICE>) and a private namespace (C<ASN_PRIVATE>). |
|
|
115 | |
|
|
116 | The meaning of the I<TAG> depends on the namespace, and defines a |
|
|
117 | (partial) interpretation of the data value. For example, right now, SNMP |
|
|
118 | application namespace knowledge ix hardcoded into this module, so it |
|
|
119 | knows that SNMP C<Unsigned32> values need to be decoded into actual perl |
|
|
120 | integers. |
|
|
121 | |
|
|
122 | The most common tags in the C<ASN_UNIVERSAL> namespace are |
|
|
123 | C<ASN_INTEGER32>, C<ASN_BIT_STRING>, C<ASN_NULL>, C<ASN_OCTET_STRING>, |
|
|
124 | C<ASN_OBJECT_IDENTIFIER>, C<ASN_SEQUENCE>, C<ASN_SET> and |
|
|
125 | C<ASN_IA5_STRING>. |
|
|
126 | |
|
|
127 | The most common tags in SNMP's C<ASN_APPLICATION> namespace |
|
|
128 | are C<SNMP_IPADDRESS>, C<SNMP_COUNTER32>, C<SNMP_UNSIGNED32>, |
|
|
129 | C<SNMP_TIMETICKS>, C<SNMP_OPAQUE> and C<SNMP_COUNTER64>. |
|
|
130 | |
|
|
131 | The I<CONSTRUCTED> flag is really just a boolean - if it is false, the |
|
|
132 | the value is "primitive" and contains no subvalues, kind of like a |
|
|
133 | non-reference perl scalar. IF it is true, then the value is "constructed" |
|
|
134 | which just means it contains a list of subvalues which this module will |
|
|
135 | en-/decode as BER tuples themselves. |
|
|
136 | |
|
|
137 | The I<DATA> value is either a reference to an array of further tuples (if |
|
|
138 | the value is I<CONSTRUCTED>), some decoded representation of the value, |
|
|
139 | if this module knows how to decode it (e.g. for the integer types above) |
|
|
140 | or a binary string with the raw octets if this module doesn't know how to |
|
|
141 | interpret the namespace/tag. |
|
|
142 | |
|
|
143 | Thus, you can always decode a BER data structure and at worst you get a |
|
|
144 | string in place of some nice decoded value. |
|
|
145 | |
|
|
146 | See the SYNOPSIS for an example of such an encoded tuple representation. |
|
|
147 | |
73 | =head2 RELATIONSHIP TO L<Convert::BER> and L<Convert::ASN1> |
148 | =head2 RELATIONSHIP TO L<Convert::BER> and L<Convert::ASN1> |
74 | |
149 | |
75 | This module is I<not> the XS version of L<Convert::BER>, but a different |
150 | This module is I<not> the XS version of L<Convert::BER>, but a different |
76 | take at doing the same thing. I imagine this module would be a good base |
151 | take at doing the same thing. I imagine this module would be a good base |
77 | for speeding up either fo these, or write a similar module, or write your |
152 | for speeding up either of these, or write a similar module, or write your |
78 | own LDAP or SNMP module for example. |
153 | own LDAP or SNMP module for example. |
79 | |
154 | |
80 | =cut |
155 | =cut |
81 | |
156 | |
82 | package Convert::BER::XS; |
157 | package Convert::BER::XS; |
… | |
… | |
89 | our $VERSION = '0.0'; |
164 | our $VERSION = '0.0'; |
90 | |
165 | |
91 | XSLoader::load __PACKAGE__, $VERSION; |
166 | XSLoader::load __PACKAGE__, $VERSION; |
92 | |
167 | |
93 | our %EXPORT_TAGS = ( |
168 | our %EXPORT_TAGS = ( |
94 | all => [qw( |
169 | const => [qw( |
|
|
170 | BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA |
|
|
171 | |
|
|
172 | ASN_BOOLEAN ASN_INTEGER32 ASN_BIT_STRING ASN_OCTET_STRING ASN_NULL ASN_OBJECT_IDENTIFIER ASN_TAG_BER ASN_TAG_MASK |
|
|
173 | ASN_CONSTRUCTED ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE ASN_CLASS_MASK ASN_CLASS_SHIFT |
|
|
174 | ASN_SEQUENCE |
|
|
175 | |
|
|
176 | SNMP_IPADDRESS SNMP_COUNTER32 SNMP_UNSIGNED32 SNMP_TIMETICKS SNMP_OPAQUE SNMP_COUNTER64 |
|
|
177 | )], |
|
|
178 | encode => [qw( |
95 | ber_decode |
179 | ber_decode |
96 | ber_is ber_is_seq ber_is_i32 ber_is_oid |
180 | ber_is ber_is_seq ber_is_i32 ber_is_oid |
97 | BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA |
181 | )], |
98 | ASN_BOOLEAN ASN_INTEGER32 ASN_BIT_STRING ASN_OCTET_STRING ASN_NULL ASN_OBJECT_IDENTIFIER ASN_TAG_BER ASN_TAG_MASK |
182 | decode => [qw( |
99 | ASN_CONSTRUCTED ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE ASN_CLASS_MASK ASN_CLASS_SHIFT |
183 | ber_encode |
100 | ASN_SEQUENCE ASN_IPADDRESS ASN_COUNTER32 ASN_UNSIGNED32 ASN_TIMETICKS ASN_OPAQUE ASN_COUNTER64 |
|
|
101 | )], |
184 | )], |
102 | ); |
185 | ); |
103 | |
186 | |
104 | our @EXPORT_OK = map @$_, values %EXPORT_TAGS; |
187 | our @EXPORT_OK = map @$_, values %EXPORT_TAGS; |
105 | |
188 | |
|
|
189 | $EXPORT_TAGS{all} = \@EXPORT_OK; |
|
|
190 | |
106 | 1; |
191 | 1; |
|
|
192 | |
|
|
193 | =head2 BUGS / SHORTCOMINGs |
|
|
194 | |
|
|
195 | This module does have a number of SNMPisms hardcoded, such as the SNMP |
|
|
196 | tags for Unsigned32 and so on. More configurability is needed, and, if |
|
|
197 | ever implemented, will come in a form similar to how L<JSON::XS> and |
|
|
198 | L<CBOR::XS> respresent things, namely with an object-oriented interface. |
107 | |
199 | |
108 | =head1 AUTHOR |
200 | =head1 AUTHOR |
109 | |
201 | |
110 | Marc Lehmann <schmorp@schmorp.de> |
202 | Marc Lehmann <schmorp@schmorp.de> |
111 | http://software.schmorp.de/pkg/Convert-BER-XS |
203 | http://software.schmorp.de/pkg/Convert-BER-XS |