ikev2 #
IKEv2 with ML-KEM key exchange, and ESP, in OCaml.
IKEv2 (RFC 7296) negotiates the keys of an IPsec VPN with one
Diffie-Hellman exchange. A quantum computer would break that exchange, and
with it the keys of traffic recorded today. RFC 9370 lets two
peers run up to seven more key exchanges and mix every shared secret into
the keys. With ML-KEM (FIPS 203) among them, the keys hold as long as
either the classical or the post-quantum exchange does.
The ikev2 package implements both roles of IKEv2 with those additional
key exchanges, and ESP (RFC 4303) for the traffic. It does no
I/O: it turns the datagrams it is given into the datagrams to send and the
events to report. The ikev2-eio package runs it over Eio UDP sockets.
Installation #
Both packages need OCaml 5.1 or later. They are not in the public
opam-repository; add the overlay repository, then install them:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install ikev2 ikev2-eio
Usage #
Two peers that share a secret establish an IKE SA. Each has a configuration
with its identity, its credential and how it checks its peer:
open Ikev2
let config name =
Result.get_ok
(Config.v
~id:Message.{ id_type = Fqdn; data = name }
~credential:(Psk "a shared secret")
~trust:(Peer_psk "a shared secret") ())
let g = Crypto_rng.v ~seed:"README" (module Crypto_rng.Fortuna)
let now = Mtime.of_uint64_ns 0L
Initiator.v returns the initiator and its first datagrams. incoming
gives a datagram to a peer, and returns the datagrams it answers with and
the events of the step. run carries the datagrams between the two peers
until neither has more to send, and counts them:
let rec run i r count events = function
| [] -> (count, List.rev events)
| (`To_responder, d) :: rest ->
let r, out, ev = Result.get_ok (Responder.incoming ~g r ~now d) in
let out = List.map (fun d -> (`To_initiator, d)) out in
run i r (count + 1) (List.rev_append ev events) (rest @ out)
| (`To_initiator, d) :: rest ->
let i, out, ev = Result.get_ok (Initiator.incoming ~g i ~now d) in
let out = List.map (fun d -> (`To_responder, d)) out in
run i r (count + 1) (List.rev_append ev events) (rest @ out)
let handshake () =
let i, first = Initiator.v ~g ~now (config "initiator.example") in
let r = Responder.v (config "responder.example") in
run i r 0 [] (List.map (fun d -> (`To_responder, d)) first)
The default profile requires an ML-KEM exchange after the Diffie-Hellman
one, so the handshake takes six datagrams. They are the request and the
response of IKE_SA_INIT, of IKE_INTERMEDIATE and of IKE_AUTH. Each peer then reports the
algorithms negotiated and the identity it authenticated:
let count, events = handshake ()
# count;;
- : int = 6
# List.map (Fmt.str "%a" Event.pp) events;;
- : string list =
["IKE SA established with ENCR_AES_GCM_16_256, PRF_HMAC_SHA2_256, Curve25519, ML-KEM-768";
"IKE SA established with ENCR_AES_GCM_16_256, PRF_HMAC_SHA2_256, Curve25519, ML-KEM-768"]
# List.filter_map
(function
| Event.Established { peer; _ } -> Some peer.data
| _ -> None)
events;;
- : string list = ["initiator.example"; "responder.example"]
Over the network, Ikev2_eio.initiate starts an IKE SA with a peer and
Ikev2_eio.serve answers IKE SAs on an address. Both call a function with
each event.
Features #
- IKEv2 (RFC 7296): IKE_SA_INIT, IKE_AUTH, CREATE_CHILD_SA and
INFORMATIONAL exchanges, rekeying, and the stateless cookies that bound
half-open IKE SAs. - Additional key exchanges (RFC 9370) over IKE_INTERMEDIATE
(RFC 9242), with ML-KEM-768 and ML-KEM-1024
(draft-ietf-ipsecme-ikev2-mlkem). ML-KEM-512 is not offered. - Fragmentation (RFC 7383). ML-KEM key exchange data is over a
kilobyte, which most paths do not carry in one datagram. - Post-quantum preshared keys (RFC 8784).
- Authentication with certificates, by the Digital Signature method
(RFC 7427) with ECDSA, Ed25519 or ML-DSA (RFC 9881),
or with preshared keys. - ESP (RFC 4303) with its anti-replay window and extended
sequence numbers. - The algorithms RFC 8247 and RFC 8221 require, and the CNSA 2.0 suite
(ML-KEM-1024, ML-DSA-87, AES-256-GCM, SHA-384) asProfile.cnsa2.
API #
lib/ikev2.mli is the entry point of ikev2.
The modules an application uses:
InitiatorandResponderare the two roles of RFC 7296 section 1.2.
Each hasv,incoming,timer,next_timeout,requestandclose.Listeneris the responder side of one address. It keeps oneResponder
per IKE SA, found by the SPIs of each datagram.Configholds an identity, a credential, the checks on the peer, a
Profileand thelimits.Profilelists, for each kind of algorithm, the ones a peer proposes and
accepts, most preferred first.Requestis what an application asks of an established IKE SA: create,
rekey or delete a Child SA, rekey the IKE SA, or check that the peer is
alive.Eventis what an IKE SA reports, andErrorwhy one failed.Child_sais an ESP Child SA, andTsits traffic selectors.Espis one direction of an ESP SA.
The modules they are built of:
Messageis the codec of RFC 7296 section 3: the IKE header and the
chain of payloads.Packetencrypts and decrypts the Encrypted payload (RFC 7296
section 3.14), with an AEAD (RFC 5282) or with a cipher and a MAC, and
the fragments of RFC 7383.Kexruns one key exchange as one round trip, whether Diffie-Hellman
(Curve25519, ECP-256, ECP-384, ECP-521) or ML-KEM.Keysderives the keys: SKEYSEED and prf+ (RFC 7296 sections 2.13 and
2.14), the update after each additional key exchange (RFC 9370
section 2.2.2), the preshared key of RFC 8784, rekeying, and the keys of
a Child SA.Authbuilds and checks the AUTH payload (RFC 7296 section 2.15,
RFC 9242 section 3.3.2).Ike_saandSessionare an IKE SA after IKE_SA_INIT and after
IKE_AUTH.Retransmittimes the request in flight (RFC 7296 section 2.1), and
Cookiemakes and checks cookies (section 2.6).
lib/eio/ikev2_eio.mli is the entry point of
ikev2-eio: Ikev2_eio.initiate, Ikev2_eio.request, Ikev2_eio.close
and Ikev2_eio.serve.
Design #
Time and randomness are arguments #
The library never opens a socket, reads a clock or draws random bytes on
its own. Every function that depends on time takes ~now:Mtime.t.
Retransmission is the pair timer and next_timeout, separate from
incoming. Random bytes come from a Crypto_rng.g the caller may pass.
The same code therefore runs under Eio, in a unikernel, in a fuzzer and in
a test with a fixed seed.
Each role describes its phases and the events each phase accepts in
control, a value of the fsm package. incoming, timer, request and
close consult it before they act.
Algorithms and peers are allowlists #
A Profile accepts only the algorithms it lists. Profile.default follows
RFC 8247 and RFC 8221, and requires one ML-KEM additional key exchange.
Profile.cnsa2 is the CNSA 2.0 suite.
A peer is authenticated by an Auth.trust. It is a preshared key, or an
X509.Authenticator.t with the signature algorithms the peer may use. By
default those are ECDSA, Ed25519 and ML-DSA (Auth.default_schemes).
Messages #
Message reads every payload into a typed value, whatever the phase of the
exchange. At that layer an Encrypted payload and an Encrypted Fragment
payload are opaque bytes. Packet opens them, and verifies each fragment
before it joins a reassembly.
The descriptions Message is built from also generate C validators with
EverParse, one per structure, under c/. A test runs the same inputs
through them and through the OCaml decoder and compares the verdicts.
The AUTH payload covers the octets of RFC 7296 section 2.15, extended with
the IntAuth chain of RFC 9242 section 3.3.2 over each IKE_INTERMEDIATE
message. The chain is computed over Packet.content, which fragmentation
does not change. A signature names its algorithm with the DER
AlgorithmIdentifier of RFC 7427 section 3, and the identity a peer claims
must be one its certificate holds (RFC 4945 section 3.1).
Established IKE SAs #
Session runs the CREATE_CHILD_SA exchanges that create and rekey Child
SAs and that rekey the IKE SA (RFC 7296 sections 1.3 and 2.18). Each is
followed by one IKE_FOLLOWUP_KE exchange per additional key exchange
(RFC 9370 section 2.2.4). INFORMATIONAL exchanges delete SAs. Session
keeps the Message IDs, a window of one request, and the last response to
send again (RFC 7296 sections 2.1 and 2.3).
A rekeyed IKE SA keeps the one it replaced until the Delete that retires it
is answered. When both peers start an exchange at once, Session follows
RFC 7296 section 2.25. It answers TEMPORARY_FAILURE to a request that
cannot complete while its own exchange runs. Of two rekeys of one SA, it
closes the one whose exchange holds the lowest nonce (sections 2.8.1 and
2.8.2).
IKE_AUTH creates the first Child SA when the configuration names its
traffic, and none otherwise (RFC 6023). The keys are mixed with the
configuration's preshared key of RFC 8784 when both peers sent USE_PPK.
Bounds on the work a peer can cause #
Every bound is a field of Config.limits.
- Once a listener holds
half_openhalf-open IKE SAs, it keeps no state
for a new IKE_SA_INIT request and answers with a cookie (RFC 7296
section 2.6).Cookie.gatedecides before aResponderis made. - A half-open
Respondergives up once the initiator's retransmissions
would have run out: 31.5 s with the default limits
(Retransmit.lifetime). - A cookie proves the address of the peer. The half-open IKE SAs admitted
with a cookie are counted per IPv4 address and per IPv6 /64 prefix, with
an IPv4-mapped address counted as its IPv4 address. A request from an
address that holdshalf_open_per_prefixof them is dropped (RFC 8019
section 4.2), and the other addresses are still served. - A datagram costs the listener one header decode and lookups in balanced
maps of its IKE SAs. Only an IKE_SA_INIT request that names no IKE SA is
decoded in full. A test checks that a datagram allocates the same number
of words with 100 IKE SAs as with 1000. - A message, whole or reassembled, is at most
max_messageoctets in at
mostmax_fragmentsfragments. A fragment joins a reassembly only after
its integrity check passed, so an attacker who cannot read the traffic
cannot fill one. - An IKE SA accepts at most
max_intermediateIKE_INTERMEDIATE exchanges. - An established IKE SA holds the state of at most one series of
IKE_FOLLOWUP_KE exchanges of the peer. It drops that state once the next
request is later than the peer's retransmissions would be (RFC 9370
section 2.2.4). - An AUTH payload is checked against the key of the peer's certificate
before the certificate chain is validated, so a forged one costs one
signature verification. - Decoding is linear in the input. The length of each payload is checked
against what remains before its body is read.
Interoperability #
The tests replay IKE SAs recorded against strongSwan and Libreswan, in both
roles. Each test runs this library with the seed of the recording and
checks that it sends the same octets. REGEN=1 dune build @traces records
them again, and needs podman.
test/interop/strongswan, against strongSwan 6.1.0: X25519 then
ML-KEM-768 in fragmented IKE_INTERMEDIATE exchanges, ECP-384 then
ML-KEM-1024 with ECDSA certificates, a preshared key of RFC 8784,
CREATE_CHILD_SA with IKE_FOLLOWUP_KE, and the rekeying of Child SAs and
of IKE SAs.- The same directory holds ESP packets exchanged between
Espand the
xfrm of the Linux kernel, keyed by the Child SA strongSwan installed
there. The ciphers are AES-GCM, with and without extended sequence
numbers, and ChaCha20-Poly1305. The packets also exercise the anti-replay
window. test/interop/libreswan, against Libreswan 5.4.2: the same key exchanges
in fragmented IKE_INTERMEDIATE exchanges and in the rekeying of IKE SAs,
ECDSA certificates, a preshared key of RFC 8784, and an IKE_AUTH with no
Child SA. Child SAs are created and rekeyed in CREATE_CHILD_SA without
additional key exchanges, which Libreswan 5.4.2 does not complete.
Related work #
- strongSwan and
Libreswan are the C implementations this library
is tested against. nox-crypto-pqprovides ML-KEM and ML-DSA,nox-x509validates
certificates, andfsmholds the phase descriptions.
References #
- RFC 7296, Internet Key Exchange Protocol Version 2 (IKEv2)
- RFC 9370, Multiple Key Exchanges in IKEv2
- RFC 9242, Intermediate Exchange in IKEv2
- RFC 7383, IKEv2 Message Fragmentation
- RFC 8784, Mixing Preshared Keys in IKEv2 for Post-quantum
Security - RFC 7427, Signature Authentication in IKEv2
- RFC 9881, Algorithm Identifiers for ML-DSA in X.509
- RFC 4303, IP Encapsulating Security Payload (ESP)
License #
ISC. See LICENSE.md.