nox-asn1-combinators #
Warning. This is a work-in-progress experimental fork of asn1-combinators. Do not use it in production; use asn1-combinators instead.
ASN.1 types written as OCaml values, with their BER and DER codecs.
An ASN.1 type such as SEQUENCE { id INTEGER, name UTF8String } is written
with the combinators of Asn.S, and its OCaml type is inferred: here
(int * string) Asn.t. Asn.codec turns that value into an encoder and a
decoder. The encodings are the Basic Encoding Rules (BER) and the
Distinguished Encoding Rules (DER) of ITU-T X.690. The library parses no
ASN.1 notation and generates no code.
nox-asn1-combinators is a fork of
mirleft/ocaml-asn1-combinators,
whose API it keeps. The fork differs in two ways:
- The parser of a recursive type (
Asn.S.fix) is built with its codec, so a codec shared between OCaml 5 domains forces noLazyvalue at its first decode. - A decoder reads nested values in place, and refuses an input nested deeper
than the
max_depthofAsn.codec, 64 by default.
Installation #
The package needs OCaml 4.13 or later. It is not in the public
opam-repository; add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install nox-asn1-combinators
Usage #
A sequence of a required integer and a required string, encoded with DER
and decoded again. Asn.decode returns the value and the bytes left after
it:
# let person = Asn.S.(sequence2 (required ~label:"id" int) (required ~label:"name" utf8_string));;
val person : (int * string) Asn.t = <abstr>
# let codec = Asn.codec Asn.der person;;
val codec : (int * string) Asn.codec = <abstr>
# let der = Asn.encode codec (42, "Ada");;
val der : string = "0\b\002\001*\012\003Ada"
# let hex s = String.concat " " (List.map (Printf.sprintf "%02x") (List.map Char.code (List.of_seq (String.to_seq s))));;
val hex : string -> string = <fun>
# hex der;;
- : string = "30 08 02 01 2a 0c 03 41 64 61"
# Asn.decode codec der;;
- : ((int * string) * string, Asn.error) result = Ok ((42, "Ada"), "")
An input that does not fit the type is an error:
# Result.is_error (Asn.decode codec "\x30\x03\x01\x01\xff");;
- : bool = true
Optional elements, CHOICE and tags have their own combinators, and
Asn.S.map converts a decoded tuple to a record and back:
# type version = V1 | V2;;
type version = V1 | V2
# let version =
Asn.S.(
map
(function None -> V1 | Some _ -> V2)
(function V1 -> None | V2 -> Some 1)
(sequence (single (optional ~label:"version" (explicit 0 int)))));;
val version : version Asn.t = <abstr>
# hex (Asn.encode (Asn.codec Asn.der version) V2);;
- : string = "30 05 a0 03 02 01 01"
API #
lib/asn.mli documents the library:
Asn.Sholds the ASN.1 types: the primitive ones (bool,integer,octet_string,oid,utc_time, the string types),sequence,set,sequence_of,set_of,choice2tochoice6, theimplicitandexplicittags,mapandfix.Asn.OIDbuilds, compares and prints object identifiers.Asn.codec,Asn.encode,Asn.encode_intoandAsn.decodeencode and decode withAsn.berorAsn.der.Asn.randomdraws a random value of a type, for tests.
Credits #
This is a fork of mirleft/ocaml-asn1-combinators
by David Kaloper Meršinjak and its contributors, under the ISC licence. David
Kaloper Meršinjak wrote the library and most of its history; Hannes Mehnert
maintains it and wrote much of the later work, and Reynir Björnsson, Anil
Madhavapeddy, Daniel Bünzli, David Sheets and others contributed fixes. The
fork keeps its API and its modules. It adds the parser of a recursive type
built with its codec, so a codec shared between OCaml 5 domains forces no
Lazy value, and a bound on the nesting depth of a decoded input.
License #
ISC. See LICENSE.md.