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

@ -312,7 +312,7 @@ export namespace imsd::ipsec {
// What a warm SA in the kernel tells us — enough for a refresh re-REGISTER
// to re-bind without re-authenticating.
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 portPc = 0;
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;
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.
@ -391,6 +407,8 @@ export namespace imsd::ipsec {
// state: (src==LOCAL,dst==P): reqid1->spiPs reqid2->spiPc
// (src==P,dst==LOCAL): reqid1->spiUc reqid2->spiUs
// 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
// that form; PCSCF=/LOCAL= arrive in any spelling), so the spelling in
// /etc/imsd.env can no longer hide a registration's SAs.
@ -403,13 +421,15 @@ export namespace imsd::ipsec {
bool havePc = false;
bool havePortPs = false;
bool havePortPc = false;
std::string_view alg;
std::string_view ealg;
for (std::string_view b : detail::SrcBlocks(stateText)) {
const std::string src = imsd::util::CanonicalAddr(detail::TokenAfter(b, "src "));
const std::string dst = imsd::util::CanonicalAddr(detail::TokenAfter(b, "dst "));
auto spi = detail::ParseHex32(detail::TokenAfter(b, "spi "));
auto reqid = detail::ParseDec(detail::TokenAfter(b, "reqid "));
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 == p && dst == l && *reqid == 1) sa.spiUc = *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; }
}
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;
}