tsp #
Trusted timestamps for OCaml: the Time-Stamp Protocol of RFC 3161, with the
ESSCertIDv2 of RFC 5816.
A time-stamping authority (TSA) attests that some data existed at a given
time: a requester sends it the hash of the data, and the TSA answers with a
token, a CMS SignedData over a TSTInfo stating the hash, the time and the
TSA's policy. CAdES and PAdES long-term signatures, code signing and
countersignatures in notary formats carry such tokens.
tsp is the I/O-free core: the TimeStampReq and TimeStampResp codecs,
the TSTInfo with its accuracy and ordering, the verification of a token and
a minimal TSA. tsp-eio carries the protocol over HTTP (RFC 3161 section
3.4) and provides the tsp command line.
Contents #
Installation #
$ opam install tsp tsp-eio
Usage #
A request stamps the imprint of the data, with a nonce and asking for the
TSA's certificate:
# let request =
Tsp.Request.v ~nonce:"\x2a" (Tsp.Imprint.of_data "some data");;
val request : Tsp.Request.t = <abstr>
# Tsp.Request.cert_req request;;
- : bool = true
Its DER is POSTed to the TSA as application/timestamp-query
(Tsp_eio.stamp does it). The reply decodes with Tsp.Response.decode_der,
and its token verifies with Tsp.Token.verify against the data, the request
and the trust anchors of the TSA's chain:
let check ~anchors ~now ~data request response =
match Tsp.Response.result response with
| Error (`Rejected status) -> Fmt.epr "refused: %a@." Cmp.Status.pp status
| Ok token -> (
match
Tsp.Token.verify ~request ~time:(fun () -> Some now) ~anchors
(`Data data) token
with
| Ok _tsa ->
Fmt.pr "stamped at %a@." Ptime.pp
(Tsp.Info.gen_time (Tsp.Token.info token))
| Error e -> Fmt.epr "%a@." Tsp.Token.pp_error e)
verify checks that the token stamps the data's imprint with an allowed
hash, carries the request's nonce and policy, has one signer whose CMS
signature and certificate chain verify with an allowed algorithm, that the
signer's certificate has the critical extended key usage id-kp-timeStamping
alone, that the ESS signing-certificate attribute (v1 or v2) names that
certificate, and that the tsa field, if present, names the signer.
Tsp.Info.order orders two tokens as RFC 3161 section 2.4.2 allows: by their
genTime when one TSA issued both with ordering set, and otherwise only when
their genTime intervals, widened by their accuracy, do not overlap.
Tsp.Tsa is a minimal TSA: it signs a token over each request's imprint, and
refuses an unknown extension, hash or policy with the failure RFC 3161 names.
The caller gives each response its time and its unique serial number.
Command line #
$ tsp stamp --anchor ca.pem https://tsa.example/ document.pdf -o document.tsr
$ tsp verify --anchor ca.pem document.pdf document.tsr
$ tsp query document.pdf -o document.tsq
$ tsp reply --key tsa.key --cert tsa.pem --policy 1.2.3.4 document.tsq -o document.tsr
$ tsp serve --key tsa.key --cert tsa.pem --policy 1.2.3.4 --port 8080
--algorithm and --allow-hash, each repeatable, narrow or widen the
signature algorithms and imprint hashes a token may use; the defaults are
RSASSA-PKCS1-v1_5 and ECDSA with SHA-256, SHA-384 or SHA-512, and Ed25519,
and imprints of SHA-256, SHA-384 or SHA-512.
API #
Tsp.Imprint:MessageImprint, a hash and its digest.Tsp.Request:TimeStampReq.Tsp.Info:TSTInfo, withTsp.Accuracyand the order of two tokens.Tsp.Token: the token and its verification.Tsp.Response:TimeStampResp, its status aCmp.Status.t.Tsp.Tsa: a time-stamping authority.Tsp_eio.stampandTsp_eio.route: the HTTP client and server.
Related work #
OpenSSL's ts command and the TSP modules of Bouncy Castle and of Python's
rfc3161ng implement the same protocol; the tests replay OpenSSL's queries
and replies, and record OpenSSL's verdict on the replies tsp writes.
License #
ISC. See LICENSE.md.