… | |
… | |
76 | level of user-friendlyness. |
76 | level of user-friendlyness. |
77 | |
77 | |
78 | =head2 EXPORT TAGS AND CONSTANTS |
78 | =head2 EXPORT TAGS AND CONSTANTS |
79 | |
79 | |
80 | By default this module doesn't export any symbols, but if you don't want |
80 | By default this module doesn't export any symbols, but if you don't want |
81 | to break your keyboard, editor or eyesigh with extreemly long names, I |
81 | to break your keyboard, editor or eyesight with extremely long names, I |
82 | recommend importing the C<:all> tag. Still, you can selectively import |
82 | recommend importing the C<:all> tag. Still, you can selectively import |
83 | things: |
83 | things. |
84 | |
84 | |
85 | =over |
85 | =over |
86 | |
86 | |
87 | =item :all |
87 | =item C<:all> |
88 | |
88 | |
89 | All of the below. Really. Rcommended for at least first steps, or if you |
89 | All of the below. Really. Recommended for at least first steps, or if you |
90 | don't care about a few kilobytes of wasted memory (and namespace). |
90 | don't care about a few kilobytes of wasted memory (and namespace). |
91 | |
91 | |
92 | =item :const |
92 | =item C<:const> |
93 | |
93 | |
94 | All of the stricly ASN.1-related constants defined by this module, the |
94 | All of the strictly ASN.1-related constants defined by this module, the |
95 | same as C<:const_asn :const_index>. Notably, this does not contain |
95 | same as C<:const_asn :const_index>. Notably, this does not contain |
96 | C<:const_ber_type> and C<:const_snmp>. |
96 | C<:const_ber_type> and C<:const_snmp>. |
97 | |
97 | |
98 | A good set to get everything you need to decode and match BER data would be |
98 | A good set to get everything you need to decode and match BER data would be |
99 | C<:decode :const>. |
99 | C<:decode :const>. |
100 | |
100 | |
101 | =item C<:const_index>> |
101 | =item C<:const_index> |
102 | |
102 | |
103 | The BER tuple array index constants: |
103 | The BER tuple array index constants: |
104 | |
104 | |
105 | BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA |
105 | BER_CLASS BER_TAG BER_CONSTRUCTED BER_DATA |
106 | |
106 | |
107 | =item C<:const_asn> |
107 | =item C<:const_asn> |
108 | |
108 | |
109 | ASN class values (these are C<0>, C<1>, C<2> and C<3>, reespectively - |
109 | ASN class values (these are C<0>, C<1>, C<2> and C<3>, respectively - |
110 | exactly thw two topmost bits from the identifdier octet shifted 6 bits to |
110 | exactly thw two topmost bits from the identifier octet shifted 6 bits to |
111 | the right): |
111 | the right): |
112 | |
112 | |
113 | ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE |
113 | ASN_UNIVERSAL ASN_APPLICATION ASN_CONTEXT ASN_PRIVATE |
114 | |
114 | |
115 | ASN tag values (some of which are aliases, such as C<ASN_OID>). Their |
115 | ASN tag values (some of which are aliases, such as C<ASN_OID>). Their |
… | |
… | |
169 | and also have a flag that says whether a value consists of subvalues (is |
169 | and also have a flag that says whether a value consists of subvalues (is |
170 | "constructed") or not (is "primitive"). |
170 | "constructed") or not (is "primitive"). |
171 | |
171 | |
172 | Tags are simple integers, and ASN.1 defines a somewhat weird assortment of |
172 | Tags are simple integers, and ASN.1 defines a somewhat weird assortment of |
173 | those - for example, you have 32 bit signed integers and 16(!) different |
173 | those - for example, you have 32 bit signed integers and 16(!) different |
174 | string types, but there is no unsigned32 type for example. Different |
174 | string types, but there is no Unsigned32 type for example. Different |
175 | applications work around this in different ways, for example, SNMP defines |
175 | applications work around this in different ways, for example, SNMP defines |
176 | application-specific Gauge32, Counter32 and Unsigned32, which are mapped |
176 | application-specific Gauge32, Counter32 and Unsigned32, which are mapped |
177 | to two different tags: you can distinguish between Counter32 and the |
177 | to two different tags: you can distinguish between Counter32 and the |
178 | others, but not between Gause32 and Unsigned32, without the ASN.1 schema. |
178 | others, but not between Gause32 and Unsigned32, without the ASN.1 schema. |
179 | |
179 | |
… | |
… | |
183 | |
183 | |
184 | This module represents every BER value as a 4-element tuple (actually an |
184 | This module represents every BER value as a 4-element tuple (actually an |
185 | array-reference): |
185 | array-reference): |
186 | |
186 | |
187 | [CLASS, TAG, CONSTRUCTED, DATA] |
187 | [CLASS, TAG, CONSTRUCTED, DATA] |
|
|
188 | |
|
|
189 | For example: |
|
|
190 | |
|
|
191 | [ASN_UNIVERSAL, ASN_INTEGER32, 0, 177] # the integer 177 |
|
|
192 | [ASN_UNIVERSAL, ASN_OCTET_STRING, 0, "john"] # the string "john" |
|
|
193 | [ASN_UNIVERSAL, ASN_OID, 0, "1.3.6.133"] # some OID |
|
|
194 | [ASN_UNIVERSAL, ASN_SEQUENCE, 1, [ [ASN_UNIVERSAL... # a sequence |
188 | |
195 | |
189 | To avoid non-descriptive hardcoded array index numbers, this module |
196 | To avoid non-descriptive hardcoded array index numbers, this module |
190 | defines symbolic constants to access these members: C<BER_CLASS>, |
197 | defines symbolic constants to access these members: C<BER_CLASS>, |
191 | C<BER_TAG>, C<BER_CONSTRUCTED> and C<BER_DATA>. |
198 | C<BER_TAG>, C<BER_CONSTRUCTED> and C<BER_DATA>. |
192 | |
199 | |
… | |
… | |
211 | specific applications (for example, the SNMP C<Unsigned32> type is in this |
218 | specific applications (for example, the SNMP C<Unsigned32> type is in this |
212 | namespace), a special-purpose context namespace (C<ASN_CONTEXT>, used e.g. |
219 | namespace), a special-purpose context namespace (C<ASN_CONTEXT>, used e.g. |
213 | for C<CHOICE>) and a private namespace (C<ASN_PRIVATE>). |
220 | for C<CHOICE>) and a private namespace (C<ASN_PRIVATE>). |
214 | |
221 | |
215 | The meaning of the I<TAG> depends on the namespace, and defines a |
222 | The meaning of the I<TAG> depends on the namespace, and defines a |
216 | (partial) interpretation of the data value. For example, right now, SNMP |
223 | (partial) interpretation of the data value. For example, SNMP defines |
217 | application namespace knowledge ix hardcoded into this module, so it |
224 | extra tags in the C<ASN_APPLICATION> namespace, and to take full advantage |
218 | knows that SNMP C<Unsigned32> values need to be decoded into actual perl |
225 | of these, you need to tell this module how to handle those via profiles. |
219 | integers. |
|
|
220 | |
226 | |
221 | The most common tags in the C<ASN_UNIVERSAL> namespace are |
227 | The most common tags in the C<ASN_UNIVERSAL> namespace are |
222 | C<ASN_INTEGER32>, C<ASN_BIT_STRING>, C<ASN_NULL>, C<ASN_OCTET_STRING>, |
228 | C<ASN_INTEGER32>, C<ASN_BIT_STRING>, C<ASN_NULL>, C<ASN_OCTET_STRING>, |
223 | C<ASN_OBJECT_IDENTIFIER>, C<ASN_SEQUENCE>, C<ASN_SET> and |
229 | C<ASN_OBJECT_IDENTIFIER>, C<ASN_SEQUENCE>, C<ASN_SET> and |
224 | C<ASN_IA5_STRING>. |
230 | C<ASN_IA5_STRING>. |
225 | |
231 | |
226 | The most common tags in SNMP's C<ASN_APPLICATION> namespace |
232 | The most common tags in SNMP's C<ASN_APPLICATION> namespace are |
227 | are C<SNMP_IPADDRESS>, C<SNMP_COUNTER32>, C<SNMP_UNSIGNED32>, |
233 | C<SNMP_COUNTER32>, C<SNMP_UNSIGNED32>, C<SNMP_TIMETICKS> and |
228 | C<SNMP_TIMETICKS>, C<SNMP_OPAQUE> and C<SNMP_COUNTER64>. |
234 | C<SNMP_COUNTER64>. |
229 | |
235 | |
230 | The I<CONSTRUCTED> flag is really just a boolean - if it is false, the |
236 | The I<CONSTRUCTED> flag is really just a boolean - if it is false, |
231 | the value is "primitive" and contains no subvalues, kind of like a |
237 | the value is "primitive" and contains no subvalues, kind of like a |
232 | non-reference perl scalar. IF it is true, then the value is "constructed" |
238 | non-reference perl scalar. If it is true, then the value is "constructed" |
233 | which just means it contains a list of subvalues which this module will |
239 | which just means it contains a list of subvalues which this module will |
234 | en-/decode as BER tuples themselves. |
240 | en-/decode as BER tuples themselves. |
235 | |
241 | |
236 | The I<DATA> value is either a reference to an array of further tuples (if |
242 | The I<DATA> value is either a reference to an array of further tuples (if |
237 | the value is I<CONSTRUCTED>), some decoded representation of the value, |
243 | the value is I<CONSTRUCTED>), some decoded representation of the value, |
… | |
… | |
246 | |
252 | |
247 | =head2 DECODING AND ENCODING |
253 | =head2 DECODING AND ENCODING |
248 | |
254 | |
249 | =over |
255 | =over |
250 | |
256 | |
251 | =item $tuple = ber_decoded $bindata |
257 | =item $tuple = ber_decoded $bindata[, $profile] |
252 | |
258 | |
253 | Decodes binary BER data in C<$bindata> and returns the resulting BER |
259 | Decodes binary BER data in C<$bindata> and returns the resulting BER |
254 | tuple. Croaks on any decoding error, so the returned C<$tuple> is always |
260 | tuple. Croaks on any decoding error, so the returned C<$tuple> is always |
255 | valid. |
261 | valid. |
256 | |
262 | |
|
|
263 | How tags are interpreted is defined by the second argument, which must |
|
|
264 | be a C<Convert::BER::XS::Profile> object. If it is missing, the default |
|
|
265 | profile will be used (C<$Convert::BER::XS::DEFAULT_PROFILE>). |
|
|
266 | |
|
|
267 | In addition to rolling your own, this module provides a |
|
|
268 | C<$Convert::BER::XS::SNMP_PROFILE> that knows about the additional SNMP |
|
|
269 | types. |
|
|
270 | |
257 | =item $bindata = ber_encode $tuple |
271 | =item $bindata = ber_encode $tuple[, $profile] |
258 | |
272 | |
259 | Encodes the BER tuple into a BER/DER data structure. |
273 | Encodes the BER tuple into a BER/DER data structure. AS with |
|
|
274 | Cyber_decode>, an optional profile can be given. |
260 | |
275 | |
261 | =back |
276 | =back |
262 | |
277 | |
263 | =head2 HELPER FUNCTIONS |
278 | =head2 HELPER FUNCTIONS |
264 | |
279 | |
265 | Working with a 4-tuple for every value can be annoying. Or, rather, I<is> |
280 | Working with a 4-tuple for every value can be annoying. Or, rather, I<is> |
266 | annoying. To reduce this a bit, this module defines a number of helper |
281 | annoying. To reduce this a bit, this module defines a number of helper |
267 | functions, both to match BER tuples and to conmstruct BER tuples: |
282 | functions, both to match BER tuples and to construct BER tuples: |
268 | |
283 | |
269 | =head3 MATCH HELPERS |
284 | =head3 MATCH HELPERS |
270 | |
285 | |
271 | Thse functions accept a BER tuple as first argument and either paertially |
286 | These functions accept a BER tuple as first argument and either partially |
272 | or fully match it. They often come in two forms, one which exactly matches |
287 | or fully match it. They often come in two forms, one which exactly matches |
273 | a value, and one which only matches the type and returns the value. |
288 | a value, and one which only matches the type and returns the value. |
274 | |
289 | |
275 | They do check whether valid tuples are passed in and croak otherwise. As |
290 | They do check whether valid tuples are passed in and croak otherwise. As |
276 | a ease-of-use exception, they usually also accept C<undef> instead of a |
291 | a ease-of-use exception, they usually also accept C<undef> instead of a |
277 | tuple reference. in which case they silently fail to match. |
292 | tuple reference, in which case they silently fail to match. |
278 | |
293 | |
279 | =over |
294 | =over |
280 | |
295 | |
281 | =item $bool = ber_is $tuple, $class, $tag, $constructed, $data |
296 | =item $bool = ber_is $tuple, $class, $tag, $constructed, $data |
282 | |
297 | |
283 | This takes a BER C<$tuple> and matches its elements agains the privded |
298 | This takes a BER C<$tuple> and matches its elements against the provided |
284 | values, all of which are optional - values that are either missing or |
299 | values, all of which are optional - values that are either missing or |
285 | C<undef> will be ignored, the others will be matched exactly (e.g. as if |
300 | C<undef> will be ignored, the others will be matched exactly (e.g. as if |
286 | you used C<==> or C<eq> (for C<$data>)). |
301 | you used C<==> or C<eq> (for C<$data>)). |
287 | |
302 | |
288 | Some examples: |
303 | Some examples: |