Certificate Management Protocol (RFC 9810, RFC 9483), I/O-free core
README.md

cmp #

A client for the Certificate Management Protocol (CMP,
RFC 9810) in OCaml, as the Lightweight CMP Profile
(RFC 9483) defines it.

A device that joins a private PKI needs a certificate from the PKI's
certification authority (CA), and must renew it before it expires, with
nobody at the keyboard. CMP carries those requests in PKI messages that
protect themselves. Each message is signed by a certificate the sender
already holds, or carries a MAC under a secret the sender shares with the
CA, so any transport will do; RFC 9811 sends them over HTTP.

cmp has the codecs of those messages and the code that protects and
verifies them. Its client runs one enrolment and opens no socket: it
returns the HTTP request to send or the time to wait, and the caller tells
it what came back.

Installation #

cmp needs OCaml 5.1 or later. The package 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 cmp

Usage #

first_request builds the first message of an enrolment under a shared
secret. It signs a certificate request for key and starts an
initialization request (ir). Its result is the path the caller must POST
the message to:

let first_request ~now ~rng key =
  let template =
    Cmp.Crmf.template
      ~subject:
        X509.Distinguished_name.
          [ Relative_distinguished_name.singleton (CN "device") ]
      ~public_key:(X509.Private_key.public key) ()
  in
  let credentials =
    Cmp.Protection.Shared_secret
      {
        reference = "device-1";
        secret = "shared with the CA";
        algorithm =
          Cmp.Algorithm.Password_based_mac
            { salt = rng 16; owf = `SHA256; iterations = 10_000; mac = `SHA256 };
      }
  in
  match
    ( Cmp.Crmf.sign key { id = 0; template; controls = [] },
      Cmp.Client.v ~rng ~credentials (Cmp.Protection.secret "shared with the CA")
    )
  with
  | Ok request, Ok client -> (
      match Cmp.Client.enroll client ~now (Ir request) with
      | Ok (_, _, Cmp.Client.Send { path; _ }) -> Some path
      | _ -> None)
  | _ -> None
# let key = X509.Private_key.generate ~seed:"a seed for this example" `ED25519;;
val key : X509.Private_key.t = `ED25519 <abstr>
# first_request ~now:Ptime.epoch ~rng:(fun n -> String.make n 'x') key;;
- : string option = Some "/.well-known/cmp/initialization"

The caller posts body there with the media type
Cmp.Transfer.media_type, and hands the HTTP response to Cmp.Client.incoming. incoming returns the next
action and the events of the step. An enrolment ends with the event
Enrolled, which carries the certificate, or with Failed.

API #

lib/cmp.mli is the entry point:

  • Cmp.Client runs one enrolment at a time: v, enroll and incoming.
  • Cmp.Message, Cmp.Header and Cmp.Body are the codecs of PKIMessage,
    PKIHeader and PKIBody. A body is one of ir, cr, kur, p10cr or
    their responses, or certConf, pkiconf, pollReq, pollRep, genm,
    genp or error.
  • Cmp.Crmf is the certificate request message of CRMF
    (RFC 4211), with its templates, its controls and the signature
    that proves possession of the key.
  • Cmp.Status, Cmp.Info and Cmp.Name are PKIStatusInfo,
    InfoTypeAndValue and the general names of headers.
  • Cmp.Algorithm and Cmp.Protection sign a message and verify its
    signature, or compute its MAC with PasswordBasedMac or PBMAC1
    (RFC 9481).
  • Cmp.Transfer holds the media type and the paths under
    /.well-known/cmp, with the CA label and the operation labels.
  • Cmp.Error lists the ways an operation fails.

Limitations #

The library is the client only. It does not implement central key
generation (RFC 9483 section 4.1.6), revocation (section 4.2), the support
messages of section 4.3 as client operations, nested messages or KEM-based
protection. The codecs read the general messages of section 4.3 and refuse
the other choices.

Design #

The library does no I/O. It takes the time and the random octets as
arguments, and Cmp.Client.enroll and Cmp.Client.incoming return an
action for the caller to perform, which is Send, Sleep or Idle.
Cmp.Client.control describes the client's phases and the inputs each one
accepts, as a value of the fsm package.

Messages keep the bytes they were received as #

A PKI message's protection covers the DER of its header and body
(RFC 9810 section 5.1.3), and a header may carry information the codec has
no type for. The codecs therefore read a message element by element with
X509.Der from nox-x509, and keep the header and the body as the bytes
they arrived in. Cmp.Message.protected_part is those bytes, whatever the
sender put in them. A CRMF request is kept the same way, because its proof
of possession signs its encoding. CRMF is part of this package because CMP
is its only user.

Checks on a response #

The client checks each response as RFC 9483 section 3.5 lists:

  • its DER, its pvno and its transactionID;
  • a senderNonce of at least 128 bits;
  • a recipNonce equal to the senderNonce the client sent, or to the one
    of its request for a delayed response;
  • its protection;
  • its body type, against the phase the client is in.

Only an error message may come unprotected (section 3.6.4), and it ends the
operation as a rejection.

A signed response is verified with a certificate of its extraCerts: the
one whose subject is the sender and whose subject key identifier is the
senderKID. That certificate must validate to the caller's trust anchors
and allow digitalSignature. The client keeps it for the later responses
of the transaction that omit extraCerts (section 3.3).

The client asks for implicit confirmation by default, and sends a certConf
when the CA does not grant it. It rejects a certificate that does not
certify the requested key.

Bounds on the work a server can cause #

  • A response is decoded once, element by element. Decoding allocates about
    eight bytes per byte of the response. A test holds it under twelve, on a
    freeText of 1 KiB and of 10 KiB.
  • A MAC key is derived once per response. The caller bounds the iteration
    count, the salt length and the hashes, and the client checks them before
    it derives anything. Cmp.Protection.secret allows 100000 iterations,
    salts of 64 octets and SHA-2 by default.
  • The certificates of a signed response are validated against the trust
    anchors by nox-x509, which bounds its path search.
  • A CA that delays its response makes the client poll at most max_polls
    times, and wait at most max_wait each time.

Interoperability #

The tests in test/interop/openssl replay exchanges between the
openssl cmp client of OpenSSL 3.0 and its mock server, recorded over HTTP.
The exchanges are an ir under PasswordBasedMac with implicit and with
explicit confirmation, a signed cr, a kur signed by the certificate it
renews, a p10cr, an ir answered after polling, and a rejection.

Each recorded message decodes, encodes again to OpenSSL's bytes, and
verifies. Cmp.Client is also run with OpenSSL's transaction identifier
and nonces as its random octets. Its request body and its certConf are then
the bytes OpenSSL sent, and it accepts each recorded response.
REGEN=1 dune build @traces records the exchanges again, and needs Docker.

OpenSSL differs from RFC 9483 in two places. Under a MAC, its sender is the
requested subject, where section 4.1.5 asks for the common name of the
secret's reference; Cmp.Client sends the latter. After the first message,
OpenSSL keeps the first recipient, where section 3.1 asks for the sender of
the previous message. OpenSSL's PasswordBasedMac also defaults to
HMAC-SHA1, which a caller of Cmp.Protection.secret must allow explicitly.

  • OpenSSL (Apache-2.0) has a CMP client and a
    mock server, openssl cmp, from version 3.0.
  • XiPKI (Apache-2.0),
    EJBCA and the Bouncy Castle libraries implement
    CMP CAs and clients.
  • est implements EST (RFC 7030), the other enrolment protocol over HTTPS.
    EST relies on TLS and CMS, where CMP protects its own messages.

References #

Licence #

ISC. See LICENSE.md.