Ocamlnet 4 Reference Manual : Netasn1

# Module Netasn1

    module Netasn1: sig .. end

ASN.1 support functions

    exception Out_of_range

    exception Parse_error of int

Byte position in string

    module Type_name: sig .. end

    module Value: sig .. end

    val decode_ber : ?pos:int -> ?len:int -> string -> int * Value.value

Decodes a BER-encoded ASN.1 value. Note that DER is a subset of BER, and can also be decoded.

`pos` and `len` may select a substring for the decoder. By default, `pos=0`, and `len` as large as necessary to reach to the end of the string.

The function returns the number of interpreted bytes, and the value. It is not considered as an error if less than `len` bytes are consumed.

The returned value represents implicitly tagged values as `Tagptr(class,tag,pc,pos,len)`. `pos` and `len` denote the substring containting the contents. Use `Netasn1.decode_ber_contents` to further decode the value. You can use `Tag` to put the decoded value back into the tree.

    val decode_ber_contents : ?pos:int ->
           ?len:int ->
           ?indefinite:bool ->
           string ->
           Value.pc -> Type_name.type_name -> int * Value.value

Decodes the BER-encoded contents of a data field. The contents are assumed to have the type denoted by `type_name`.

`pos` and `len` may select a substring for the decoder. By default, `pos=0`, and `len` as large as necessary to reach to the end of the string.

If `indefinite`, the extent of the contents region is considered as indefinite, and the special end marker is required. This is only allowed when `pc = Constructed`.

The function returns the number of interpreted bytes, and the value. It is not considered as an error if less than `len` bytes are consumed.

You need to use this function to recursively decode tagged values. If you get a `Tagptr(class,tag,pc,s,pos,len)` value, it depends on the kind of the tag how to proceed:

- For explicit tags just invoke `Netasn1.decode_ber` again with the given `pos` and `len` parameters.
- For implicit tags you need to know the type of the field. Now call `Netasn1.decode_ber_contents` with the right type name.

The BER encoding doesn't include whether the tag is implicit or explicit, so the decode cannot do by itself the right thing here.

    val decode_ber_length : ?pos:int -> ?len:int -> string -> int

Like `decode_ber`, but returns only the length.

This function skips many consistency checks.

    val decode_ber_header : ?pos:int ->
           ?len:int ->
           ?skip_length_check:bool ->
           string -> int * Value.tag_class * Value.pc * int * int option

`let (hdr_len, tc, pc, tag, len_opt) = decode_ber_header s`: Decodes only the header:

- `hdr_len` will be the length of the header in bytes
- `tc` is the tag class
- `pc` whether primitive or constructed
- `tag` is the numeric tag value
- `len_opt` is the length field, or `None` if the header selects indefinite length

If `skip_length_check` is set, the function does not check whether the string is long enough to hold the whole data part.