ipsec: the Security-Verify rebuilt from the kernel's SA names the SA's algorithms

A resume sends the 401's Security-Server list from the state file as its
Security-Verify, and where the file holds none for the pair in the
kernel, one mechanism rebuilt from that pair: its SPIs and ports, with
alg=hmac-sha-1-96 and ealg=aes-cbc written in whatever the pair was
installed with. Over a pair with the null cipher, 3DES or hmac-md5-96
that entry named an algorithm the pair does not use, and RFC 3329 2.3.1
has a server answer a Security-Verify that differs from its own list
with a 494.

ParseExistingSa now reads the two names from the state it takes spi-s
from: `ip xfrm state` prints them on its auth-trunc and enc lines, in
the kernel's names for what imsd installs. A state that prints neither
line, or another name, reads as hmac-sha-1-96 and aes-cbc, as before.

Tested: tests/Ipsec, on the lines `ip xfrm state` prints for the SAs
imsd installs (Linux 7.2, iproute2 7.2).
This commit is contained in:
Jorijn van der Graaf 2026-11-23 10:07:59 +01:00
commit 6b0d6341d3
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 73 additions and 9 deletions

View file

@ -255,7 +255,9 @@ What the suites pin:
- `Aka` — the AKAv1-MD5 digest, identity derivation, the `qmicli`/`mmcli` - `Aka` — the AKAv1-MD5 digest, identity derivation, the `qmicli`/`mmcli`
text parsers, the AUTHENTICATE APDU and its response parser. text parsers, the AUTHENTICATE APDU and its response parser.
- `Ipsec` — the exact `ip xfrm` argv lists for the SA pairs, the - `Ipsec` — the exact `ip xfrm` argv lists for the SA pairs, the
Security-Server mechanism selection, the SA reader, the SA peer Security-Server mechanism selection, the SA reader (the entry it
rebuilds names the pair's integrity algorithm and cipher, read from the
lines the kernel prints for them), the SA peer
(`SaPeer`: the one address a reqid-1 and a reqid-2 `dir out` transport (`SaPeer`: the one address a reqid-1 and a reqid-2 `dir out` transport
policy both name with imsd's protected port as the selector's sport — the policy both name with imsd's protected port as the selector's sport — the
fingerprint —, the pair kept beside a VPN's policies; fingerprint —, the pair kept beside a VPN's policies;
@ -480,7 +482,10 @@ Rules:
list; KPN's has one mechanism, so on the bench the stored value and the list; KPN's has one mechanism, so on the bench the stored value and the
rebuilt one are the same bytes; O2's six-entry list has not resumed on rebuilt one are the same bytes; O2's six-entry list has not resumed on
this code, and the state-file round trip is shell code no test this code, and the state-file round trip is shell code no test
reaches); a Security-Server list `PersistState` writes empty (one with reaches); a resume whose rebuilt entry names another algorithm than
`hmac-sha-1-96` or another cipher than `aes-cbc` (pinned in `tests/Ipsec`
on the lines `ip xfrm state` prints; no such pair has been resumed on a
network); a Security-Server list `PersistState` writes empty (one with
a quote, a backslash or a newline — neither KPN's nor O2's list has a quote, a backslash or a newline — neither KPN's nor O2's list has
one; the resume of such a file ran with a hand-edited empty key, one; the resume of such a file ran with a hand-edited empty key,
2026-10-11); a state write of a standing registration after a fresh 2026-10-11); a state write of a standing registration after a fresh
@ -1074,7 +1079,7 @@ The daemon's own bring-up (`BringUp()` in `main.cpp`):
`spi_uc`/`spi_us` are not the pair's, step 3), it sends — `spi_uc`/`spi_us` are not the pair's, step 3), it sends —
on the resume's re-REGISTER, the keepalive re-REGISTERs, the reg-event on the resume's re-REGISTER, the keepalive re-REGISTERs, the reg-event
SUBSCRIBE and INVITEs, until its next fresh registration — the one SUBSCRIBE and INVITEs, until its next fresh registration — the one
entry rebuilt from the SA entry rebuilt from the SA, with the algorithms the kernel prints for it
(`Imsd:Ipsec`, which makes the choice): KPN's one-entry answer to the (`Imsd:Ipsec`, which makes the choice): KPN's one-entry answer to the
default offer in the same bytes, not a list such as O2's six, which RFC 3329 §2.3.1 has the server answer with a default offer in the same bytes, not a list such as O2's six, which RFC 3329 §2.3.1 has the server answer with a
494 when it does not mirror its own; a refused resume registers fresh (with 494 when it does not mirror its own; a refused resume registers fresh (with

View file

@ -1968,8 +1968,9 @@ private:
// SUBSCRIBE and the INVITEs echo: the 401's whole Security-Server // SUBSCRIBE and the INVITEs echo: the 401's whole Security-Server
// list (RFC 3329 §2.3.1), from the state file since 0.3.12, or the // list (RFC 3329 §2.3.1), from the state file since 0.3.12, or the
// one-mechanism entry rebuilt from the SA — Imsd:Ipsec chooses // one-mechanism entry rebuilt from the SA — Imsd:Ipsec chooses
// (ChooseResumeSecurityVerify). The rebuilt entry is KPN's answer to // (ChooseResumeSecurityVerify). The rebuilt entry names the pair's
// the default offer in the same bytes, not a list such as O2's six, // own algorithms: KPN's answer to the default offer in the same
// bytes, not a list such as O2's six,
// which the RFC has the server answer with a 494; a refused resume // which the RFC has the server answer with a 494; a refused resume
// registers fresh where there is a fresh target (§7.2 step 1 otherwise). // registers fresh where there is a fresh target (§7.2 step 1 otherwise).
auto rsv = imsd::ipsec::ChooseResumeSecurityVerify(ps->securityServer.value_or(""), *sa, ealgOffer_); auto rsv = imsd::ipsec::ChooseResumeSecurityVerify(ps->securityServer.value_or(""), *sa, ealgOffer_);

View file

@ -312,7 +312,7 @@ export namespace imsd::ipsec {
// What a warm SA in the kernel tells us — enough for a refresh re-REGISTER // What a warm SA in the kernel tells us — enough for a refresh re-REGISTER
// to re-bind without re-authenticating. // to re-bind without re-authenticating.
struct ExistingSa { struct ExistingSa {
std::string securityServer; // one-mechanism Security-Server value: the resume's Security-Verify when the state file holds no list for this pair std::string securityServer; // one-mechanism Security-Server value, its alg and ealg the pair's own: the resume's Security-Verify when the state file holds no list for this pair
int portPs = 0; int portPs = 0;
int portPc = 0; int portPc = 0;
std::uint32_t spiUc = 0; // our inbound client SPI (a reregistration offers a different one) std::uint32_t spiUc = 0; // our inbound client SPI (a reregistration offers a different one)
@ -384,6 +384,22 @@ export namespace imsd::ipsec {
if (b.find("dir in") != std::string_view::npos) return ParseDec(TokenAfter(b, "dport ")) == port; if (b.find("dir in") != std::string_view::npos) return ParseDec(TokenAfter(b, "dport ")) == port;
return false; return false;
} }
// The sec-agree names of a state's algorithms, from the names `ip
// xfrm state` prints on its `auth-trunc` and `enc` lines (the
// kernel's for what KernelAuth and EncArgs install; the null cipher,
// added as cipher_null, prints as ecb(cipher_null)). A state that
// prints no such line, or another name, reads as hmac-sha-1-96 and
// aes-cbc.
inline std::string_view AlgOfState(std::string_view b) {
return TokenAfter(b, "auth-trunc ") == "hmac(md5)" ? "hmac-md5-96" : "hmac-sha-1-96";
}
inline std::string_view EalgOfState(std::string_view b) {
std::string_view enc = TokenAfter(b, "enc ");
if (enc == "ecb(cipher_null)") return "null";
if (enc == "cbc(des3_ede)") return "des-ede3-cbc";
return "aes-cbc";
}
} }
// Reconstruct SA facts from `ip xfrm state` + `ip xfrm policy` text. // Reconstruct SA facts from `ip xfrm state` + `ip xfrm policy` text.
@ -391,6 +407,8 @@ export namespace imsd::ipsec {
// state: (src==LOCAL,dst==P): reqid1->spiPs reqid2->spiPc // state: (src==LOCAL,dst==P): reqid1->spiPs reqid2->spiPc
// (src==P,dst==LOCAL): reqid1->spiUc reqid2->spiUs // (src==P,dst==LOCAL): reqid1->spiUc reqid2->spiUs
// policy: dir out, reqid1->portPs=dport, reqid2->portPc=dport // policy: dir out, reqid1->portPs=dport, reqid2->portPc=dport
// The state that gives spiPs also gives the algorithms the rebuilt
// Security-Server entry names (detail::AlgOfState, EalgOfState).
// Addresses are compared in RFC 5952 form on both sides (the kernel prints // Addresses are compared in RFC 5952 form on both sides (the kernel prints
// that form; PCSCF=/LOCAL= arrive in any spelling), so the spelling in // that form; PCSCF=/LOCAL= arrive in any spelling), so the spelling in
// /etc/imsd.env can no longer hide a registration's SAs. // /etc/imsd.env can no longer hide a registration's SAs.
@ -403,13 +421,15 @@ export namespace imsd::ipsec {
bool havePc = false; bool havePc = false;
bool havePortPs = false; bool havePortPs = false;
bool havePortPc = false; bool havePortPc = false;
std::string_view alg;
std::string_view ealg;
for (std::string_view b : detail::SrcBlocks(stateText)) { for (std::string_view b : detail::SrcBlocks(stateText)) {
const std::string src = imsd::util::CanonicalAddr(detail::TokenAfter(b, "src ")); const std::string src = imsd::util::CanonicalAddr(detail::TokenAfter(b, "src "));
const std::string dst = imsd::util::CanonicalAddr(detail::TokenAfter(b, "dst ")); const std::string dst = imsd::util::CanonicalAddr(detail::TokenAfter(b, "dst "));
auto spi = detail::ParseHex32(detail::TokenAfter(b, "spi ")); auto spi = detail::ParseHex32(detail::TokenAfter(b, "spi "));
auto reqid = detail::ParseDec(detail::TokenAfter(b, "reqid ")); auto reqid = detail::ParseDec(detail::TokenAfter(b, "reqid "));
if (!spi || !reqid) continue; if (!spi || !reqid) continue;
if (src == l && dst == p && *reqid == 1) { sa.spiPs = *spi; havePs = true; } if (src == l && dst == p && *reqid == 1) { sa.spiPs = *spi; havePs = true; alg = detail::AlgOfState(b); ealg = detail::EalgOfState(b); }
if (src == l && dst == p && *reqid == 2) { sa.spiPc = *spi; havePc = true; } if (src == l && dst == p && *reqid == 2) { sa.spiPc = *spi; havePc = true; }
if (src == p && dst == l && *reqid == 1) sa.spiUc = *spi; if (src == p && dst == l && *reqid == 1) sa.spiUc = *spi;
if (src == p && dst == l && *reqid == 2) sa.spiUs = *spi; if (src == p && dst == l && *reqid == 2) sa.spiUs = *spi;
@ -430,7 +450,7 @@ export namespace imsd::ipsec {
else if (*reqid == 2) { sa.portPc = *dport; havePortPc = true; } else if (*reqid == 2) { sa.portPc = *dport; havePortPc = true; }
} }
if (!(havePs && havePc && havePortPs && havePortPc)) return std::nullopt; if (!(havePs && havePc && havePortPs && havePortPc)) return std::nullopt;
sa.securityServer = std::format("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; " "spi-c={}; spi-s={}; port-c={}; port-s={}", sa.spiPc, sa.spiPs, sa.portPc, sa.portPs); sa.securityServer = std::format("ipsec-3gpp; q=0.1; alg={}; ealg={}; " "spi-c={}; spi-s={}; port-c={}; port-s={}", alg, ealg, sa.spiPc, sa.spiPs, sa.portPc, sa.portPs);
return sa; return sa;
} }

View file

@ -3,7 +3,9 @@
// Imsd:Ipsec unit tests — the `ip xfrm` setup command sequence (byte-exact // Imsd:Ipsec unit tests — the `ip xfrm` setup command sequence (byte-exact
// argv), the warm-SA reader, fed `ip xfrm state`/`policy` output in the // argv), the warm-SA reader, fed `ip xfrm state`/`policy` output in the
// exact shape captured from a registered FP6 (addresses/keys synthetic), and // exact shape captured from a registered FP6 (addresses/keys synthetic) and,
// for the entry it rebuilds, states in the shape a Linux 7.2 `ip xfrm state`
// prints, with its line for each algorithm imsd installs, and
// the Security-Server parser, fed the operator lines as captured (O2 UK's // the Security-Server parser, fed the operator lines as captured (O2 UK's
// six-mechanism Mavenir shape verbatim, KPN's single mechanism). // six-mechanism Mavenir shape verbatim, KPN's single mechanism).
import std; import std;
@ -77,6 +79,42 @@ int main() {
Check(sa && sa->spiPc == 0x0526f2c1u, "spi_pc (UE->P reqid2)"); Check(sa && sa->spiPc == 0x0526f2c1u, "spi_pc (UE->P reqid2)");
Check(sa && sa->securityServer == "ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; " "spi-c=86438593; spi-s=86438592; port-c=33421; port-s=6000", "reconstructed Security-Server value"); Check(sa && sa->securityServer == "ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; " "spi-c=86438593; spi-s=86438592; port-c=33421; port-s=6000", "reconstructed Security-Server value");
} }
// the rebuilt entry names the pair's own algorithms: the state towards
// the P-CSCF's server port as `ip xfrm state` prints it for each
// algorithm imsd installs (the null cipher has no key: a trailing space)
{
constexpr std::string_view Sha1 = "\tauth-trunc hmac(sha1) 0x00112233445566778899aabbccddeeff 96\n";
constexpr std::string_view Md5 = "\tauth-trunc hmac(md5) 0x00112233445566778899aabbccddeeff 96\n";
constexpr std::string_view Aes = "\tenc cbc(aes) 0xffeeddccbbaa99887766554433221100\n";
constexpr std::string_view Null = "\tenc ecb(cipher_null) \n";
constexpr std::string_view Des3 = "\tenc cbc(des3_ede) 0xffeeddccbbaa99887766554433221100ffeeddccbbaa9988\n";
// `first` is the reqid-1 state's two lines (the state that gives spi-s),
// `second` the reqid-2 state's
auto rebuilt = [](std::string_view first, std::string_view second, bool reqid1Listed1st = false) {
auto block = [](std::string_view spi, int reqid, std::string_view lines) {
return std::format(
"src 2001:db8:1111:2222:3333:4444:5555:db43 dst 2001:db8::105\n"
"\tproto esp spi {} reqid {} mode transport\n"
"\treplay-window 0 \n"
"{}"
"\tanti-replay context: seq 0x0, oseq 0x0, bitmap 0x00000000\n"
"\tsel src ::/0 dst ::/0 \n", spi, reqid, lines);
};
const std::string one = block("0x0526f2c0", 1, first);
const std::string two = block("0x0526f2c1", 2, second);
auto sa = ParseExistingSa(reqid1Listed1st ? one + two : two + one, Policy, "2001:db8::105", "2001:db8:1111:2222:3333:4444:5555:db43");
return sa ? sa->securityServer : std::string();
};
auto entry = [](std::string_view alg, std::string_view ealg) { return std::format("ipsec-3gpp; q=0.1; alg={}; ealg={}; spi-c=86438593; spi-s=86438592; port-c=33421; port-s=6000", alg, ealg); };
auto both = [](std::string_view a, std::string_view b) { return std::string(a) + std::string(b); };
Check(rebuilt(both(Sha1, Aes), both(Sha1, Aes)) == entry("hmac-sha-1-96", "aes-cbc"), "rebuilt entry: a sha1 and aes-cbc pair");
Check(rebuilt(both(Sha1, Null), both(Sha1, Null)) == entry("hmac-sha-1-96", "null"), "rebuilt entry: a null pair names ealg=null");
Check(rebuilt(both(Sha1, Des3), both(Sha1, Des3)) == entry("hmac-sha-1-96", "des-ede3-cbc"), "rebuilt entry: a 3DES pair names des-ede3-cbc");
Check(rebuilt(both(Md5, Aes), both(Md5, Aes)) == entry("hmac-md5-96", "aes-cbc"), "rebuilt entry: an md5 pair names hmac-md5-96");
Check(rebuilt(both(Md5, Null), both(Sha1, Des3)) == entry("hmac-md5-96", "null") && rebuilt(both(Md5, Null), both(Sha1, Des3), true) == entry("hmac-md5-96", "null"), "rebuilt entry: the algorithms are those of the state that gives spi-s, listed first or last");
Check(rebuilt("", both(Md5, Null)) == entry("hmac-sha-1-96", "aes-cbc"), "rebuilt entry: a state that prints no algorithm reads as sha1 and aes-cbc");
Check(rebuilt("\tauth-trunc hmac(sha256) 0x00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff 96\n\tenc cbc(camellia) 0xffeeddccbbaa99887766554433221100\n", "") == entry("hmac-sha-1-96", "aes-cbc"), "rebuilt entry: names imsd does not install read as sha1 and aes-cbc");
}
// the environment's spelling must not matter: upper case, uncompressed, // the environment's spelling must not matter: upper case, uncompressed,
// bracketed — the kernel prints RFC 5952 form and both sides are // bracketed — the kernel prints RFC 5952 form and both sides are
// canonicalised before the compare // canonicalised before the compare