3 ASN.1 Encoding and Decoding
This library supports the Basic Encoding Rules (BER) for ASN.1 as well as its restricted form, the Distinguished Encoding Rules (DER).
A BER encoding is logically a tag, length, value (TLV)
triple. The tag indicates to what type the value belongs (or at least
disambiguates; the tag is often context-specific). The tag also
indicates whether the encoding is primitive or
constructed—
procedure
(write-asn1 type value [out #:rules rules]) → void?
type : asn1-type? value : any/c out : output-port? = (current-output-port) rules : (or/c 'BER 'DER) = 'BER
procedure
type : asn1-type? in : input-port? = (current-input-port) rules : (or/c 'BER 'DER) = 'BER
procedure
(asn1->bytes type value [#:rules rules]) → bytes?
type : asn1-type? value : any/c rules : (or/c 'BER 'DER) = 'BER
procedure
(bytes->asn1 type bstr [#:rules rules]) → any/c
type : asn1-type? bstr : bytes? rules : (or/c 'BER 'DER) = 'BER
procedure
(write-asn1/DER type value [out]) → void?
type : asn1-type? value : any/c out : output-port? = (current-output-port)
procedure
(read-asn1/DER type [in]) → any/c
type : asn1-type? in : input-port? = (current-input-port)
procedure
(asn1->bytes/DER type value) → bytes?
type : asn1-type? value : any/c
procedure
(bytes->asn1/DER type bstr) → any/c
type : asn1-type? bstr : bytes?
Equivalent to calling the corresponding procedure with #:rules 'DER.
> (define Integers (SEQUENCE-OF INTEGER)) > (asn1->bytes Integers '(1 2 3 -1000)) #"0\200\2\1\1\2\1\2\2\1\3\2\2\374\30\0\0"
> (asn1->bytes Integers '(1 2 3 -1000) #:rules 'DER) #"0\r\2\1\1\2\1\2\2\1\3\2\2\374\30"
> (asn1->bytes/DER Integers '(1 2 3 -1000)) #"0\r\2\1\1\2\1\2\2\1\3\2\2\374\30"
3.1 BER Utilities
(require asn1/ber) | package: asn1-lib |
struct
tag-class : (or/c 'universal 'private 'application 'context-sensitive) tag-number : exact-nonnegative-integer? value : (or/c bytes? (listof (or/c bytes? BER-frame?)))
procedure
(BER-encode type v [#:der? der?]) → BER-frame?
type : asn1-type? v : any/c der? : boolean? = #f
procedure
(BER-decode type b [#:der? der?]) → any/c
type : asn1-type? b : BER-frame? der? : boolean? = #f
> (BER-encode INTEGER 5) (BER-frame 'universal 2 #"\5")
> (BER-decode INTEGER (BER-frame 'universal 2 #"\5")) 5
> (BER-encode (SEQUENCE-OF INTEGER) '(1 2 3 -1000))
(BER-frame
'universal 16
(list
(BER-frame 'universal 2 #"\1")
(BER-frame 'universal 2 #"\2")
(BER-frame 'universal 2 #"\3")
(BER-frame 'universal 2 #"\374\30")))
> (BER-encode (SEQUENCE [a IA5String] [b INTEGER]) (hasheq 'a "Jean" 'b 24601))
(BER-frame
'universal 16
(list (BER-frame 'universal 22 #"Jean") (BER-frame 'universal 2 #"`\31")))
procedure
(write-BER-frame frame [out #:rules rules]) → void?
frame : BER-frame? out : output-port? = (current-output-port) rules : (or/c 'BER 'DER) = 'BER
procedure
(read-BER-frame [in #:rules rules]) → BER-frame?
in : input-port? = (current-input-port) rules : (or/c 'BER 'DER) = 'BER