ipsec, messages, imsd: the offered ESP ciphers are a list

imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.

EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).

An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.

Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
This commit is contained in:
Jorijn van der Graaf 2026-11-23 10:08:04 +01:00
commit ed2a009425
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
7 changed files with 233 additions and 68 deletions

View file

@ -82,6 +82,35 @@ export namespace imsd::ipsec {
return ealg == "null" || ealg == "aes-cbc" || ealg == "des-ede3-cbc";
}
// The ESP ciphers offered in Security-Client when EALG names none, in
// the order offered.
std::vector<std::string> DefaultEalgOffer() { return {"aes-cbc"}; }
// An EALG setting ("aes-cbc, null") read as the ciphers to offer, in the
// order written: case-insensitive, spaces and tabs ignored, "3des" for
// des-ede3-cbc, a repeat dropped. An element that names no cipher
// KnownEalg() has goes to `unknown`, in lower case without its spaces;
// an empty element is passed over. A setting that names no cipher gives
// DefaultEalgOffer().
struct EalgSetting {
std::vector<std::string> ciphers; // the offer
std::vector<std::string> unknown; // the names passed over
};
EalgSetting ParseEalgOffer(std::string_view spec) {
EalgSetting out;
for (auto part : std::views::split(spec, ',')) {
std::string n;
for (char c : std::string_view(part))
if (c != ' ' && c != '\t') n += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
if (n.empty()) continue; // not NormalizeEalg's absent-means-null: an empty element names nothing
n = NormalizeEalg(n);
std::vector<std::string>& to = KnownEalg(n) ? out.ciphers : out.unknown;
if (std::ranges::find(to, n) == to.end()) to.push_back(n);
}
if (out.ciphers.empty()) out.ciphers = DefaultEalgOffer();
return out;
}
namespace detail {
// ESP cipher argv triple for `ip xfrm state add ... <enc>`, matching
// imscall.setup_sa: aes-cbc keyed with CK; 3DES uses CK||CK[:8] to
@ -150,12 +179,20 @@ export namespace imsd::ipsec {
//
// The P-CSCF answers the challenge with the mechanisms it supports, each
// with a preference, plus the SPIs and ports it assigned to this
// registration. The UE takes the highest-preference mechanism that is
// in that list AND that it offered (RFC 3329 §2.3.1, TS 24.229
// §5.1.1.5.1, TS 33.203 §7.2); imsd offers exactly one combination
// (hmac-sha-1-96 + EALG). The SPIs/ports are per registration, not per
// mechanism: they come from whichever entry carries them (O2 UK's
// Mavenir core lists six entries and puts them on the one it applied).
// registration. RFC 3329 §2.3.1 and TS 24.229 §5.1.1.5.1 have the UE
// take the most preferred mechanism it supports; TS 33.203 §7.2 has it
// take the first on the P-CSCF's list, the same entry where the list
// runs from its highest q down or carries none. imsd looks among the
// entries it offered: hmac-sha-1-96 with each cipher of its list (EALG,
// DefaultEalgOffer). The SPIs/ports are per registration, not per
// mechanism: they come from whichever entry carries them. O2 UK's
// Mavenir core lists six entries, in rising q, and puts them on the one
// it applied: the sha1 entry without a cipher for a null offer
// (2026-09-17), the sha1 entry with aes-cbc, its highest q, for an
// aes-cbc offer (2026-09-20). So an entry that alone carries them, when
// it is one imsd offered, is taken before the highest q is looked at:
// with more than one cipher offered the two can name different
// algorithms, with one a different mod or prot.
// imsd ≤ 0.3.5 took the first `ealg=` anywhere in the header and
// installed 3DES against a P-CSCF expecting the null cipher: every
// protected packet was undecryptable to it (alyx, 2026-09-17; journal/ims
@ -255,15 +292,21 @@ export namespace imsd::ipsec {
// Parse a Security-Server value and select against our offer. Names,
// parameter names and alg/ealg/prot/mod values are tokens
// (case-insensitive) and come back lower-case. `selected` = the
// highest-q ipsec-3gpp mechanism whose alg (absent = the offered one)
// and ealg (absent = null) equal what we offered; when none matches,
// the highest-q ipsec-3gpp entry carrying spi-s, else port-s, else the
// first ipsec-3gpp entry, with offerMatched = false so the caller can
// say so. `carrier` = selected when it carries spi-s, else the highest-q
// ipsec-3gpp entry that does. nullopt when the value holds no ipsec-3gpp
// mechanism at all.
std::optional<SecurityServer> ParseSecurityServer(std::string_view value, std::string_view offeredAlg, std::string_view offeredEalg) {
// (case-insensitive) and come back lower-case. An entry is one we
// offered when it is ipsec-3gpp, its alg (absent = the offered one) is
// the offered one and its ealg (absent = null) is among the offered
// ciphers. `selected` = the ipsec-3gpp entry carrying spi-s when one
// entry alone carries it and it is one we offered (the entry the P-CSCF
// marked), else the highest-q entry we offered (ties: the earlier one);
// when none matches, the highest-q ipsec-3gpp entry carrying spi-s, else
// port-s, else the first ipsec-3gpp entry, with offerMatched = false so
// the caller can say so. With one cipher offered the marked entry and
// the highest-q offered one name the same algorithms; their mod and prot
// can differ, and the marked entry's are the ones taken. `carrier` =
// selected when it carries spi-s, else the highest-q ipsec-3gpp entry
// that does. nullopt when the value holds no ipsec-3gpp mechanism at
// all.
std::optional<SecurityServer> ParseSecurityServer(std::string_view value, std::string_view offeredAlg, const std::vector<std::string>& offeredEalgs) {
SecurityServer ss;
for (std::string_view part : detail::SplitUnquoted(value, ',')) {
part = detail::TrimWs(part);
@ -294,13 +337,17 @@ export namespace imsd::ipsec {
auto ipsec = [](const SecurityMechanism& m) { return m.name == "ipsec-3gpp"; };
auto carries = [&](const SecurityMechanism& m) { return ipsec(m) && m.spiPs.has_value(); };
std::string wantAlg = detail::Lower(offeredAlg);
std::string wantEalg = NormalizeEalg(detail::Lower(offeredEalg));
std::vector<std::string> wantEalgs;
for (const std::string& e : offeredEalgs) wantEalgs.push_back(NormalizeEalg(detail::Lower(e)));
auto offered = [&](const SecurityMechanism& m) {
return ipsec(m) && (m.alg.empty() || m.alg == wantAlg) && NormalizeEalg(m.ealg) == wantEalg;
return ipsec(m) && (m.alg.empty() || m.alg == wantAlg) && std::ranges::find(wantEalgs, NormalizeEalg(m.ealg)) != wantEalgs.end();
};
auto firstIpsec = std::ranges::find_if(ss.mechanisms, ipsec);
if (firstIpsec == ss.mechanisms.end()) return std::nullopt;
if (auto i = detail::Best(ss.mechanisms, offered)) { ss.selected = *i; ss.offerMatched = true; }
std::optional<std::size_t> marked;
if (std::ranges::count_if(ss.mechanisms, carries) == 1) marked = static_cast<std::size_t>(std::ranges::find_if(ss.mechanisms, carries) - ss.mechanisms.begin());
if (marked && offered(ss.mechanisms[*marked])) { ss.selected = *marked; ss.offerMatched = true; }
else if (auto i = detail::Best(ss.mechanisms, offered)) { ss.selected = *i; ss.offerMatched = true; }
else if (auto i = detail::Best(ss.mechanisms, carries)) ss.selected = *i;
else if (auto i = detail::Best(ss.mechanisms, [&](const SecurityMechanism& m) { return ipsec(m) && m.portPs.has_value(); })) ss.selected = *i;
else ss.selected = static_cast<std::size_t>(firstIpsec - ss.mechanisms.begin());
@ -458,7 +505,10 @@ export namespace imsd::ipsec {
// file's list when the entry carrying its SPIs names the SA pair the
// kernel holds (a file an attempt the process did not live to record
// left behind names another pair), else the one-mechanism entry rebuilt
// from the SA — for an empty or unparseable value too.
// from the SA — for an empty or unparseable value too. The entry
// carrying the SPIs is ParseSecurityServer's carrier under the offer
// handed in: the offer decides only where several entries carry SPIs
// of their own.
struct ResumeSecurityVerify {
enum class Source { Stored, NoneStored, OtherSa };
Source source = Source::NoneStored;
@ -466,12 +516,12 @@ export namespace imsd::ipsec {
std::size_t mechanisms = 0; // of the stored list, when it is used
std::optional<std::uint32_t> storedSpiPs; // the stored list's spi-s (OtherSa); nullopt = none parsed
};
ResumeSecurityVerify ChooseResumeSecurityVerify(std::string_view stored, const ExistingSa& sa, std::string_view offeredEalg) {
ResumeSecurityVerify ChooseResumeSecurityVerify(std::string_view stored, const ExistingSa& sa, const std::vector<std::string>& offeredEalgs) {
ResumeSecurityVerify r;
r.value = sa.securityServer;
if (stored.empty()) return r;
r.source = ResumeSecurityVerify::Source::OtherSa;
if (auto parsed = ParseSecurityServer(stored, "hmac-sha-1-96", offeredEalg)) {
if (auto parsed = ParseSecurityServer(stored, "hmac-sha-1-96", offeredEalgs)) {
const SecurityMechanism& c = parsed->Carrier();
r.storedSpiPs = c.spiPs;
if (c.spiPc == sa.spiPc && c.spiPs == sa.spiPs && c.portPc == sa.portPc && c.portPs == sa.portPs) {

View file

@ -29,6 +29,7 @@ export module Imsd:Messages;
import std;
import :Util;
import :Aka;
import :Ipsec;
import :Sip;
export namespace imsd::msg {
@ -47,7 +48,9 @@ export namespace imsd::msg {
// of every request we send over it. TCP unless the engine fell back
// to, or was told to use, UDP.
std::string transport = "TCP";
std::string ealgOffer = "aes-cbc";
// The ESP ciphers Security-Client offers, in order (canonical names:
// imsd::ipsec::ParseEalgOffer).
std::vector<std::string> ealgOffer = imsd::ipsec::DefaultEalgOffer();
// RFC 7254 IMEI URN for +sip.instance (e.g. urn:gsma:imei:35999999-
// 000000-1); empty omits the parameter.
std::string instanceId;
@ -101,12 +104,18 @@ export namespace imsd::msg {
return std::format("Via: SIP/2.0/{} {};branch=z9hG4bK{};rport", transport, imsd::util::HostPort(c.local, port), branch);
}
// Security-Client value: the SPIs and protected ports offered for the SA
// pairs this REGISTER may set up — the in-use ones on an initial
// registration, new SPIs and a new client port on a reregistration
// (TS 24.229 5.1.1.4.2 d; TS 33.203 7.4.0: the server port stays).
// Security-Client value: one mechanism per offered cipher, each with the
// SPIs and protected ports offered for the SA pairs this REGISTER may
// set up — the in-use ones on an initial registration, new SPIs and a
// new client port on a reregistration (TS 24.229 5.1.1.4.2 d; TS 33.203
// 7.4.0: the server port stays). One pair of SPIs and one of ports for
// the whole list (TS 33.203 7.2 NOTE 1, NOTE 2), the mechanisms
// comma-separated in one header field (Annex H).
inline std::string SecurityClient(const Context& c, std::uint32_t spiUc, std::uint32_t spiUs, int portUc, int portUs) {
return std::format("Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg={}; " "spi-c={}; spi-s={}; port-c={}; port-s={}", c.ealgOffer, spiUc, spiUs, portUc, portUs);
std::string v;
for (const std::string& ealg : c.ealgOffer)
v += std::format("{}ipsec-3gpp; alg=hmac-sha-1-96; ealg={}; " "spi-c={}; spi-s={}; port-c={}; port-s={}", v.empty() ? "" : ", ", ealg, spiUc, spiUs, portUc, portUs);
return std::format("Security-Client: {}", v);
}
inline std::string SecurityClient(const Context& c, std::uint32_t spiUc, std::uint32_t spiUs) {
return SecurityClient(c, spiUc, spiUs, c.portUc, c.portUs);