diff --git a/AGENTS.md b/AGENTS.md index d5365ae..b3ba586 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,7 +45,7 @@ PipeWire. The verified device/carrier combinations are in the README. | `interfaces/Imsd-Sdp.cppm` | `imsd::sdp` — SDP offer/answer: the AMR-WB + AMR + telephone-event offer (optional IR.92 preconditions; `OfferedTelephoneEventPt` names its telephone-event numbers), the parser the media plane needs (the codec, its payload type and mode, and the telephone-event payload type the m-line lists at that codec's clock), `BuildAnswer` for the terminating side, the `CODECS` override. | | `interfaces/Imsd-Sms.cppm` | `imsd::sms` — the short message codecs SMS over IP rides on (TS 24.341 carries the bytes a 2G/3G phone puts in an RP-DATA, in a SIP MESSAGE body): the GSM 7-bit default alphabet with its extension table and the septet packing, UCS-2 (TS 23.038); the SM-TL PDUs (TS 23.040: SMS-SUBMIT built, one per segment with the concatenation header when the text needs more than one; SMS-DELIVER, SMS-STATUS-REPORT and SMS-SUBMIT-REPORT parsed; the SMS-DELIVER-REPORT that acknowledges a delivery; the user-data header — concatenation, application ports — and the `Reassembler` of a concatenated message); the SM-RL messages (TS 24.011: RP-DATA, RP-ACK, RP-ERROR in both directions, the cause values) and `RpCauseIsTemporary`, the causes of a submission after which imsd sends the segment again; EF_SMSP (TS 31.102 §4.2.27), where the (U)SIM names the service centre, and qmicli's hex dump of it. Byte-level only: no SIP, no clock, no I/O. | | `interfaces/Imsd-Aka.cppm` | `imsd::aka` — USIM AKA glue: the AKAv1-MD5 digest (RFC 3310), IMPI/IMPU/realm from IMSI + MCC/MNC, parsers for `qmicli --uim-get-card-status` and `mmcli` output, the AUTHENTICATE APDU and its response. | -| `interfaces/Imsd-Ipsec.cppm` | `imsd::ipsec` — the two ESP SA pairs (TS 33.203) as `ip xfrm` argv lists, the Security-Server mechanism selection, and the reader that reconstructs SPIs/ports from `ip xfrm state`/`policy` text for a warm resume, and (1.0.0) `SaPeer`, the P-CSCF the kernel's pair is to, read from the `dir out` policies that carry imsd's fingerprint, `OwnXfrmDeleteCommands`, the deletes by exact key of what two rules select — the policies carrying imsd's fingerprint and the reqid-1/2 transport-mode ESP states in their direction or on the UE's ims-PDN address, whoever installed them (§12), and `FileNamesAnotherPair`, whether a state file's `spi_uc`/`spi_us` name the kernel's pair (unknown on either side decides nothing). | +| `interfaces/Imsd-Ipsec.cppm` | `imsd::ipsec` — the two ESP SA pairs (TS 33.203) as `ip xfrm` argv lists, the ESP ciphers offered (`ParseEalgOffer`, `DefaultEalgOffer`), the Security-Server mechanism selection, and the reader that reconstructs SPIs/ports from `ip xfrm state`/`policy` text for a warm resume, and (1.0.0) `SaPeer`, the P-CSCF the kernel's pair is to, read from the `dir out` policies that carry imsd's fingerprint, `OwnXfrmDeleteCommands`, the deletes by exact key of what two rules select — the policies carrying imsd's fingerprint and the reqid-1/2 transport-mode ESP states in their direction or on the UE's ims-PDN address, whoever installed them (§12), and `FileNamesAnotherPair`, whether a state file's `spi_uc`/`spi_us` name the kernel's pair (unknown on either side decides nothing). | | `interfaces/Imsd-Messages.cppm` | `imsd::msg` — every SIP request and response imsd sends, built from an explicit `Context` and per-request variables so the builders are pure functions; `tests/Messages` is their suite. Also `IsEmergency`. Since 1.2.0 the SIP MESSAGE of SMS over IP (`BuildMessageSms`: an RP-DATA to the service centre's PSI, or an RP-ACK/RP-ERROR to the IP-SM-GW with In-Reply-To — TS 24.341 5.3.1.2 and 5.3.2.4), the `+g.3gpp.smsip` Contact tag behind `Context::smsOverIp`, `BuildResponse`, the final for an inbound MESSAGE outside a call's dialog (the 200, the 480, the 488): each Via row echoed in order and the To echoed with a tag added unless it already holds `tag=` (RFC 3261 8.2.6.2); since 1.2.1 `BuildBadRequest`, the 400 to a request cut short of the Content-Length read from it — any but an ACK or a CANCEL, in a call's dialog or not —, in that shape, a To tag it adds made from the request's header section and a secret of the run (RFC 3261 8.2.7); of requests that came whole, in-dialog requests, a MESSAGE among them, and the shell's answers to any other request outside a call — a NOTIFY, an OPTIONS, a method it does not handle; not an INVITE, whose 486 while a call exists is `BuildInviteResponse`'s — keep `BuildResponse200`'s hop-by-hop shape; and `ScPsi`, the service centre's PSI from its international number — never a dialled-number guess. | | `interfaces/Imsd-Engine.cppm` | `imsd::engine` — the decisions as pure reducers: `CallMachine` (one call's dialog, returning `Action`s for the shell), `RegRecovery` (the registration recovery policy, §7.4) and, since 1.2.0, SMS over IP (§7.9): `SmsOutbound` (one short message of ours on its way to the service centre, segment by segment, since 1.2.1 a segment that gets no answer, or an RP-ERROR with cause 41, sent once more with TP-RD set — TS 23.040 9.2.3.6 —, returning `SmsAction`s) and `OnSmsMessage` (what an inbound MESSAGE outside a call's dialog leads to: its final, the message or status report it delivered, the delivery report to send), and `RecentTransactions` (an inbound request's retransmission — the same Call-ID, CSeq and Via branch within 32 s (`TimerJ`), the branch being the value of the first `branch=` of its first Via row and a request with none or an empty one never matched — answered with the same final and not handled again) and, since 1.2.1, `RecentSubmissions` (the Call-IDs of the MESSAGEs our submissions went out in, an hour each, by which a submit report for one that is no longer in flight is known for ours — TS 24.341 5.3.1.2); `SameRecipient` and `StatusReportBook`, a status report's match to the message that asked for one — TP-MR and recipient — and its word for the whole message, with `SmsWordOrder`, the order of a message's words: the submission's first, a report's held until then (§7.9); `RunToken`, `SentSmsId` and `ReceivedSmsId`, the ids (§8.4); `StoredSms` with `FormatStoredSms`/`ParseStoredSms`, the record codec of the received-message store, and `StoredSmsBefore`, `List`'s order. | | `interfaces/Imsd-Qmi.cppm` | `imsd::qmi` — the QMI WDS request bytes `imsd-pcscf` sends (Bind Mux Data Port, Set IP Family, Get Packet Service Status, Get Current Settings, Get Profile Settings), the message/TLV parser, the P-CSCF list readers (0x22 flag, 0x23 IPv4, 0x24 FQDN, 0x2E IPv6 — the last one libqmi does not model). | @@ -254,8 +254,11 @@ What the suites pin: text, what is refused), the address-list parser, hex/base64, MD5. - `Aka` — the AKAv1-MD5 digest, identity derivation, the `qmicli`/`mmcli` text parsers, the AUTHENTICATE APDU and its response parser. -- `Ipsec` — the exact `ip xfrm` argv lists for the SA pairs, the - Security-Server mechanism selection, the SA reader (the entry it +- `Ipsec` — the exact `ip xfrm` argv lists for the SA pairs, the `EALG` + setting, the Security-Server mechanism selection against one offered + cipher and against two (KPN's entry and Odido's, O2's six with the SPIs + on the null entry and on the aes-cbc one, a list whose every entry + carries them), 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 @@ -283,7 +286,8 @@ What the suites pin: TLVs) and the error names (OutOfCall, NoEffect, an unknown code as `?`). - `Messages` — the outgoing SIP messages: the initial and the protected REGISTER, the reg-event SUBSCRIBE and the INVITEs byte-for-byte, their - variants, the in-dialog requests, the ACKs, the CANCEL and + variants (a Security-Client for another offer among them), the in-dialog + requests, the ACKs, the CANCEL and `BuildResponse200` by their lines, `BuildInviteResponse` by the lines of one 180 and by a 486 without a Record-Route (`tests/Engine` checks the responses `CallMachine` builds with it); the @@ -485,7 +489,11 @@ Rules: 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 + network); a list that marks one of two offered ciphers (pinned in + `tests/Ipsec` with two of O2's recorded lists, each its answer to an + offer of one cipher, and run against a stand-in P-CSCF that marks the + lower-q of the two as far as the `SA:` line; no capture holds O2's + answer to imsd offering two); a Security-Server list `PersistState` writes empty (one with 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, 2026-10-11); a state write of a standing registration after a fresh @@ -1549,7 +1557,8 @@ account `user`, its old default. Lines the operator (and the scripts) look for, all prefixed `imsd: `: -- `LOCAL=… P-CSCF=…`, `SIP transport: …`, `SIM slot=… IMSI=… domain=…` at start. +- `LOCAL=… P-CSCF=…`, `SIP transport: …`, `ESP ciphers offered: …`, `SIM slot=… IMSI=… domain=…` at start; + `EALG entry '…' is not a cipher imsd can install; ignored` for a name in `EALG` that imsd cannot install. - `RESUME via existing SA (cseq N)`, `REGISTERED (resumed + true-refreshed)`, `FRESH registration`, `Security-Server: …`, `REGISTERED (fresh)`. - `bring-up deferred: …; next: fresh registration in N s` — one line; `in N diff --git a/README.md b/README.md index 19d1822..9d49eb2 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Configuration is environment variables in `/etc/imsd.env`, none are required. | `HOLDS_FILE` | `/run/imsd.holds` (root) / `$XDG_RUNTIME_DIR/imsd.holds` (`/tmp/imsd.holds` without a runtime dir) | the waits the network imposed (a `Retry-After`, the refusal schedule), where that schedule stands whether the old registration is gone (`old_gone`) and the refusal count (`refusals`: a resume refused without a Retry-After at a start with no fresh target is one, the restart's the second), kept across a restart of imsd for the PDN they were set on — not across a reboot; `/dev/null` switches the file off (only a regular file is ever removed; anything else is emptied). `imsd-handover.sh` honours it too | | `RESUME` | `auto` | `0` forces a fresh registration (ignores a warm SA) where there is a fresh target; with none the untried pair is removed and the bring-up ends as AGENTS §7.2 (the daemon's own bring-up) step 1 says | | `REFRESH_INTERVAL` | *(auto)* | keepalive re-REGISTER period in s; default = half the granted expiry, clamped to [120, 1800] | -| `EALG` | `aes-cbc` | offered ESP cipher (`aes-cbc`, `des-ede3-cbc`, `null`). From the 401's `Security-Server` list imsd selects the highest-preference `ipsec-3gpp` mechanism that matches this offer (RFC 3329 §2.3.1) and takes the SPIs/ports from the entry carrying them (some cores list every mechanism they support and put the SPIs on the one they applied); when no entry matches, the P-CSCF's SPI-carrying entry is installed as listed (`hmac-md5-96`, an absent `ealg` = null) and the journal says so. The line, the selection and the SA parameters are logged at every fresh registration | +| `EALG` | `aes-cbc` | the ESP ciphers offered, in order: a comma-separated list of `aes-cbc`, `null` (no encryption) and `des-ede3-cbc`; a name imsd cannot install is ignored with a journal line. From the 401's `Security-Server` list imsd selects the `ipsec-3gpp` mechanism the P-CSCF put the SPIs on when it is the only such entry and one of the offer, else the mechanism of the offer with the highest preference (the `q` of RFC 3329 §2.3.1), and takes the SPIs/ports from the entry carrying them (some cores list every mechanism they support and put the SPIs on the one they applied); when no entry matches, the P-CSCF's SPI-carrying entry is taken as listed (`hmac-md5-96`, an absent `ealg` = null) where imsd can install it, and the journal says so. The offer is logged at bring-up beside the SIP transport, the line, the selection and the SA parameters at every fresh registration | | `SIP_TRANSPORT` | `auto` | transport of the protected leg (the second REGISTER and everything after it): `tcp`, `udp`, or `auto` = TCP, falling back to a fresh registration over UDP when the P-CSCF leaves two TCP connects unanswered — but only on a phone that has never registered over TCP: once the state file records a TCP registration, a silent connect is treated as an outage, not a policy (delete the file or set `udp` to override, e.g. after a SIM change). The challenge (first REGISTER) is always UDP. Over UDP a REGISTER or INVITE larger than ~1.2 KB leaves as IPv6 fragments at the ims PDN's 1280-byte MTU (the journal says so once per flow); there are no SIP retransmission timers over UDP yet, so a lost datagram costs a timeout. The SAs, the listener and the firewall rule cover both transports | | `SMS_OVER_IMS` | `0` | `1` takes short messages over IMS (AGENTS.md §7.9): the REGISTER Contact advertises `+g.3gpp.smsip` and `net.catcrafts.IMS1.Messaging.Send` submits one; off, imsd does not advertise it and refuses `Send` (AGENTS.md §7.9 says how imsd answers a text the network sends anyway) | | `SMSC` | *(EF_SMSP)* | the service centre a submission is addressed to, in international form (`+31653131313`; spaces, dashes and dots dropped; a national or `00`-prefixed number, or a `*` or `#` in it, is refused at bring-up); unset, imsd reads the (U)SIM's EF_SMSP record 1. Without either, `Send` fails with the reason | diff --git a/implementations/main.cpp b/implementations/main.cpp index 721c9f3..35d8b32 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -1380,7 +1380,15 @@ public: outDir_ = EnvOr("OUT_DIR", StateDir()); rtpPort_ = std::atoi(EnvOr("RTP_PORT", "50004").c_str()); precond_ = EnvOr("PRECOND", "0") == "1"; - ealgOffer_ = EnvOr("EALG", "aes-cbc"); + // EALG: the ESP ciphers Security-Client offers, in the order + // written (imsd::ipsec::ParseEalgOffer: with no cipher named, the + // default list). A name imsd cannot install is passed over with a + // line. + { + imsd::ipsec::EalgSetting ealg = imsd::ipsec::ParseEalgOffer(EnvOr("EALG", "")); + for (const std::string& u : ealg.unknown) Log(std::format("EALG entry '{}' is not a cipher imsd can install; ignored", u)); + ealgOffer_ = std::move(ealg.ciphers); + } // SIP_TRANSPORT: the protected leg's transport. auto (default) = TCP, // and when the P-CSCF never answers the TCP connect, a fresh // registration over UDP; tcp / udp force one. The challenge (first @@ -1650,7 +1658,8 @@ private: } // config - std::string pcscf_, dev_, outDir_, local_, ealgOffer_, mediaBin_; + std::string pcscf_, dev_, outDir_, local_, mediaBin_; + std::vector ealgOffer_; // the ESP ciphers offered, in order // The P-CSCF a fresh registration goes to (PCSCF=, else the first of // PCSCF_DISCOVERED=) and the whole discovered list. pcscf_ is the one in // use: the same, or the kernel's SA peer while a resumed registration @@ -1795,6 +1804,11 @@ private: ctx_.ealgOffer = ealgOffer_; SetTransport(transport_); Log(std::format("SIP transport: {} ({})", TransportName(transport_), sipPolicy_)); + { + std::string list; + for (const std::string& e : ealgOffer_) list += (list.empty() ? "" : ", ") + e; + Log(std::format("ESP ciphers offered: {}", list)); + } // USER_AGENT overrides (some networks fingerprint UAs — setting the // stock firmware's build string gives oracle parity, journal/ims.md // s53); USER_AGENT= (empty) omits the header. @@ -2095,8 +2109,8 @@ private: ss_ += line; } Log(std::format("Security-Server: {}", ss_)); - // Algorithms from the mechanism we select against our offer (RFC - // 3329 §2.3.1), SPIs/ports from the entry carrying them (O2 lists + // Algorithms from the mechanism ParseSecurityServer selects against + // our offer, SPIs/ports from the entry carrying them (O2 lists // six and marks one; ims ledger F11). An answer we cannot use is a // deferral on the throttle schedule, not a fatal: a fatal restarts // imsd every RestartSec, an initial REGISTER each time. diff --git a/interfaces/Imsd-Ipsec.cppm b/interfaces/Imsd-Ipsec.cppm index 3c93239..25f6128 100644 --- a/interfaces/Imsd-Ipsec.cppm +++ b/interfaces/Imsd-Ipsec.cppm @@ -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 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 ciphers; // the offer + std::vector 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(std::tolower(static_cast(c))); + if (n.empty()) continue; // not NormalizeEalg's absent-means-null: an empty element names nothing + n = NormalizeEalg(n); + std::vector& 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 ... `, 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 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 ParseSecurityServer(std::string_view value, std::string_view offeredAlg, const std::vector& 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 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 marked; + if (std::ranges::count_if(ss.mechanisms, carries) == 1) marked = static_cast(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(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 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& 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) { diff --git a/interfaces/Imsd-Messages.cppm b/interfaces/Imsd-Messages.cppm index 68ce25f..958dd29 100644 --- a/interfaces/Imsd-Messages.cppm +++ b/interfaces/Imsd-Messages.cppm @@ -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 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); diff --git a/tests/Ipsec/main.cpp b/tests/Ipsec/main.cpp index 2f2439c..ea43a4b 100644 --- a/tests/Ipsec/main.cpp +++ b/tests/Ipsec/main.cpp @@ -7,7 +7,8 @@ // 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 -// six-mechanism Mavenir shape verbatim, KPN's single mechanism). +// six-mechanism Mavenir shape verbatim, under a null and under an aes-cbc +// offer; KPN's single mechanism; Odido's), and the EALG list. import std; import Imsd; @@ -287,7 +288,7 @@ int main() { "ipsec-3gpp;q=0.98;alg=hmac-sha-1-96;mod=trans;ealg=aes-cbc"; { // alyx's offer (EALG=null): the entry O2 marked is also the only one matching the offer - auto ss = ParseSecurityServer(O2, "hmac-sha-1-96", "null"); + auto ss = ParseSecurityServer(O2, "hmac-sha-1-96", {"null"}); Check(ss.has_value() && ss->mechanisms.size() == 6, "O2/null: six mechanisms"); Check(ss && ss->selected == 3 && ss->carrier == 3 && ss->offerMatched, "O2/null: the fourth mechanism (sha1, no ealg, the SPIs) selected, offer matched"); if (ss) { @@ -302,43 +303,107 @@ int main() { Check(ss->mechanisms[1].ealg == "des-ede3-cbc" && !ss->mechanisms[1].spiPs, "O2/null: the 3DES entry is parsed but carries no SPIs"); } // the default offer (EALG=aes-cbc): the spec picks sha1+aes (q=0.98); the SPIs stay with the marked entry - auto sa = ParseSecurityServer(O2, "hmac-sha-1-96", "aes-cbc"); + auto sa = ParseSecurityServer(O2, "hmac-sha-1-96", {"aes-cbc"}); Check(sa && sa->selected == 5 && sa->offerMatched, "O2/aes: sha1+aes-cbc (q=0.98) selected for the aes offer"); Check(sa && sa->carrier == 3 && sa->Carrier().spiPs == 133937015u && sa->Carrier().portPs == 6060, "O2/aes: SPIs/ports from the entry carrying them"); Check(sa && sa->Selected().ealg == "aes-cbc" && sa->Selected().alg == "hmac-sha-1-96", "O2/aes: algorithms from the selected entry"); // an offer O2 does not list (3des alias -> des-ede3-cbc): sha1+3des (q=0.96) - auto s3 = ParseSecurityServer(O2, "hmac-sha-1-96", "3des"); + auto s3 = ParseSecurityServer(O2, "hmac-sha-1-96", {"3des"}); Check(s3 && s3->selected == 4 && s3->offerMatched && s3->carrier == 3, "O2/3des: the alias matches des-ede3-cbc (q=0.96), SPIs from the marked entry"); } // ---- KPN: one mechanism, spaces after the separators, explicit ealg { - auto ss = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=49889723; spi-s=49889722; port-c=33142; port-s=6000", "hmac-sha-1-96", "aes-cbc"); + auto ss = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=49889723; spi-s=49889722; port-c=33142; port-s=6000", "hmac-sha-1-96", {"aes-cbc"}); Check(ss && ss->mechanisms.size() == 1 && ss->selected == 0 && ss->carrier == 0 && ss->offerMatched, "KPN: single mechanism selected, offer matched"); Check(ss && ss->Selected().alg == "hmac-sha-1-96" && ss->Selected().ealg == "aes-cbc", "KPN: alg + ealg"); Check(ss && ss->Selected().spiPc == 49889723u && ss->Selected().spiPs == 49889722u && ss->Selected().portPc == 33142 && ss->Selected().portPs == 6000, "KPN: SPIs + ports"); Check(ss && ss->Selected().qMilli == 100, "KPN: q=0.1"); - auto nul = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null; spi-c=33998503; spi-s=33998502; port-c=33021; port-s=6000", "hmac-sha-1-96", "null"); + auto nul = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null; spi-c=33998503; spi-s=33998502; port-c=33021; port-s=6000", "hmac-sha-1-96", {"null"}); Check(nul && nul->Selected().ealg == "null" && nul->offerMatched, "KPN: explicit ealg=null matches the null offer"); // the P-CSCF answers a cipher we did not offer: still the only entry, installed as answered, flagged - auto other = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null; spi-c=33998503; spi-s=33998502; port-c=33021; port-s=6000", "hmac-sha-1-96", "aes-cbc"); + auto other = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null; spi-c=33998503; spi-s=33998502; port-c=33021; port-s=6000", "hmac-sha-1-96", {"aes-cbc"}); Check(other && other->selected == 0 && other->carrier == 0 && !other->offerMatched, "single entry not matching the offer: selected via the spi-s fallback, offerMatched=false"); // the fresh-SA throttle answer: spi-s present and zero - auto thr = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=1; spi-s=0; port-c=33102; port-s=6000", "hmac-sha-1-96", "aes-cbc"); + auto thr = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=1; spi-s=0; port-c=33102; port-s=6000", "hmac-sha-1-96", {"aes-cbc"}); Check(thr && thr->Carrier().spiPs.has_value() && *thr->Carrier().spiPs == 0, "KPN throttle: spi-s=0 is present, not absent"); // no SPIs anywhere: parsed, nothing to build from - auto bare = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null;", "hmac-sha-1-96", "null"); + auto bare = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null;", "hmac-sha-1-96", {"null"}); Check(bare && bare->mechanisms.size() == 1 && bare->offerMatched && !bare->Carrier().spiPs && !bare->Carrier().portPs, "no spi-s: absent, not zero"); } + // ---- the offer: EALG as a list, the default when it names nothing + { + using L = std::vector; + Check(DefaultEalgOffer() == L{"aes-cbc"}, "default offer: aes-cbc"); + Check(ParseEalgOffer("null").ciphers == L{"null"} && ParseEalgOffer("aes-cbc").ciphers == L{"aes-cbc"}, "EALG: one name is a list of one"); + Check(ParseEalgOffer("null,aes-cbc").ciphers == L{"null", "aes-cbc"}, "EALG: the order written is the order offered"); + Check(ParseEalgOffer(" AES-CBC ,\tNull ").ciphers == L{"aes-cbc", "null"}, "EALG: case, spaces and tabs ignored"); + Check(ParseEalgOffer("3des").ciphers == L{"des-ede3-cbc"} && ParseEalgOffer("des-ede3-cbc,3DES").ciphers == L{"des-ede3-cbc"}, "EALG: 3des is des-ede3-cbc, a repeat dropped"); + auto mixed = ParseEalgOffer("aes-gcm,null, HMAC-SHA-1-96,aes-gcm"); + Check(mixed.ciphers == L{"null"} && mixed.unknown == L{"aes-gcm", "hmac-sha-1-96"}, "EALG: a name imsd cannot install goes to unknown, once, in lower case without spaces"); + Check(ParseEalgOffer("").ciphers == DefaultEalgOffer() && ParseEalgOffer("").unknown.empty() && ParseEalgOffer(" , ,").ciphers == DefaultEalgOffer() && ParseEalgOffer(" , ,").unknown.empty(), "EALG: empty or blank elements name nothing: the default offer"); + Check(ParseEalgOffer("aes-gcm").ciphers == DefaultEalgOffer() && ParseEalgOffer("aes-gcm").unknown == L{"aes-gcm"}, "EALG: no known name -> the default offer, the name in unknown"); + Check(ParseEalgOffer("aes-cbc,").ciphers == L{"aes-cbc"} && ParseEalgOffer("aes-cbc,").unknown.empty(), "EALG: an empty element is no name, not the null cipher"); + } + // ---- selection against more than one offered cipher + { + const std::vector both = {"aes-cbc", "null"}; + // KPN's answer: one entry + auto kpn = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=49889723; spi-s=49889722; port-c=33142; port-s=6000", "hmac-sha-1-96", both); + Check(kpn && kpn->selected == 0 && kpn->offerMatched && kpn->Selected().ealg == "aes-cbc", "both offered, KPN's aes-cbc entry: selected, offer matched"); + // Odido's answer to an offer of the null cipher (2026-07-13), verbatim + auto odido = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=null; spi-c=30890423; spi-s=30890422; port-c=32997; port-s=6000", "hmac-sha-1-96", both); + Check(odido && odido->selected == 0 && odido->offerMatched && NormalizeEalg(odido->Selected().ealg) == "null" && odido->Carrier().spiPs == 30890422u, "both offered, Odido's null entry: selected, offer matched"); + // O2's list as it answered a null offer: the SPIs on sha1/null (q=0.94). With + // both ciphers offered that marked entry is taken, not sha1/aes-cbc (q=0.98) + auto o2null = ParseSecurityServer(O2, "hmac-sha-1-96", both); + Check(o2null && o2null->selected == 3 && o2null->carrier == 3 && o2null->offerMatched, "both offered, O2's list marking sha1/null: the marked entry, not the higher-q sha1/aes-cbc"); + Check(o2null && NormalizeEalg(o2null->Selected().ealg) == "null" && o2null->Selected().alg == "hmac-sha-1-96", "both offered, O2 marking sha1/null: the selected entry is sha1 with the null cipher"); + // O2's list as it answered an aes-cbc offer (alyx report 14, 2026-09-20), verbatim: the SPIs on sha1/aes-cbc + constexpr std::string_view O2Aes = + "ipsec-3gpp;q=0.88;alg=hmac-md5-96;mod=trans," + "ipsec-3gpp;q=0.9;alg=hmac-md5-96;mod=trans;ealg=des-ede3-cbc," + "ipsec-3gpp;q=0.92;alg=hmac-md5-96;mod=trans;ealg=aes-cbc," + "ipsec-3gpp;q=0.94;alg=hmac-sha-1-96;mod=trans," + "ipsec-3gpp;q=0.96;alg=hmac-sha-1-96;mod=trans;ealg=des-ede3-cbc," + "ipsec-3gpp;q=0.98;alg=hmac-sha-1-96;mod=trans;ealg=aes-cbc;spi-c=53871876;spi-s=58749351;port-c=6058;port-s=6060"; + auto o2aes = ParseSecurityServer(O2Aes, "hmac-sha-1-96", both); + Check(o2aes && o2aes->selected == 5 && o2aes->carrier == 5 && o2aes->offerMatched && o2aes->Selected().ealg == "aes-cbc", "both offered, O2's list marking sha1/aes-cbc: that entry"); + Check(ParseSecurityServer(O2Aes, "hmac-sha-1-96", {"aes-cbc"})->selected == 5 && ParseSecurityServer(O2Aes, "hmac-sha-1-96", {"null"})->selected == 3, "one cipher offered: the entry of that cipher, marked or not"); + // the marked entry is one we did not offer (md5): the highest-q offered entry, its SPIs + auto md5marked = ParseSecurityServer("ipsec-3gpp;q=0.5;alg=hmac-sha-1-96;ealg=aes-cbc,ipsec-3gpp;q=0.4;alg=hmac-sha-1-96,ipsec-3gpp;q=0.9;alg=hmac-md5-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", both); + Check(md5marked && md5marked->selected == 0 && md5marked->carrier == 2 && md5marked->offerMatched, "both offered, the marked entry not offered: the highest-q offered entry, SPIs from the marked one"); + // every entry carries the SPIs: nothing is marked — the highest q, without q the first + auto listq = ParseSecurityServer("ipsec-3gpp;q=0.4;alg=hmac-sha-1-96;ealg=null;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.5;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", both); + Check(listq && listq->selected == 1 && listq->carrier == 1 && listq->offerMatched, "both offered, both entries carry the SPIs: the highest q"); + auto listfirst = ParseSecurityServer("ipsec-3gpp;alg=hmac-sha-1-96;ealg=null;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", both); + Check(listfirst && listfirst->selected == 0 && listfirst->offerMatched, "both offered, both entries carry the SPIs, no q: the first on the list"); + // neither offered cipher listed: the P-CSCF's entry, flagged + auto neither = ParseSecurityServer("ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=des-ede3-cbc; spi-c=1; spi-s=2; port-c=3; port-s=4", "hmac-sha-1-96", both); + Check(neither && neither->selected == 0 && !neither->offerMatched, "both offered, a 3DES answer: taken via the spi-s fallback, offerMatched=false"); + // the marked entry over a higher-q offered one also under one offered cipher (same algorithms, the entry's own mod) + auto onecipher = ParseSecurityServer("ipsec-3gpp;q=0.9;alg=hmac-sha-1-96;ealg=aes-cbc;mod=tun,ipsec-3gpp;q=0.5;alg=hmac-sha-1-96;ealg=aes-cbc;mod=trans;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", {"aes-cbc"}); + Check(onecipher && onecipher->selected == 1 && onecipher->Selected().mod == "trans", "one cipher offered, two entries of it: the marked one"); + // the resume's choice under the same offer: O2's stored list, and a list whose two entries carry SPIs of their own + ExistingSa sa; + sa.spiPc = 129427852u; sa.spiPs = 133937015u; sa.portPc = 6051; sa.portPs = 6060; + sa.securityServer = "ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=129427852; spi-s=133937015; port-c=6051; port-s=6060"; + auto rsv = ChooseResumeSecurityVerify(O2, sa, both); + Check(rsv.source == ResumeSecurityVerify::Source::Stored && rsv.value == O2 && rsv.mechanisms == 6, "resume, both offered: O2's stored list for the kernel's pair"); + constexpr std::string_view TwoPairs = "ipsec-3gpp;q=0.9;alg=hmac-sha-1-96;ealg=null;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.5;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8"; + ExistingSa aesPair; + aesPair.spiPc = 5; aesPair.spiPs = 6; aesPair.portPc = 7; aesPair.portPs = 8; + Check(ChooseResumeSecurityVerify(TwoPairs, aesPair, {"aes-cbc"}).source == ResumeSecurityVerify::Source::Stored && ChooseResumeSecurityVerify(TwoPairs, aesPair, both).source == ResumeSecurityVerify::Source::OtherSa, "resume: the offer handed in decides which entry's SPIs are held against the kernel's pair"); + } // ---- the resume's Security-Verify: the stored list only for the SA pair the kernel holds { using Source = ResumeSecurityVerify::Source; ExistingSa sa; sa.spiPc = 129427852u; sa.spiPs = 133937015u; sa.portPc = 6051; sa.portPs = 6060; sa.securityServer = "ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=129427852; spi-s=133937015; port-c=6051; port-s=6060"; - auto ok = ChooseResumeSecurityVerify(O2, sa, "null"); + auto ok = ChooseResumeSecurityVerify(O2, sa, {"null"}); Check(ok.source == Source::Stored && ok.value == O2 && ok.mechanisms == 6, "resume: O2's six-entry list whose carrier is the kernel's pair -> the list verbatim, six mechanisms"); - auto okAes = ChooseResumeSecurityVerify(O2, sa, "aes-cbc"); - Check(okAes.source == Source::Stored && okAes.value == O2, "resume: the same under the aes offer (the carrier does not depend on the offer)"); + auto okAes = ChooseResumeSecurityVerify(O2, sa, {"aes-cbc"}); + Check(okAes.source == Source::Stored && okAes.value == O2, "resume: the same under the aes offer (O2's list has one carrier)"); constexpr std::pair off[] = { {"spi-c", "ipsec-3gpp;q=0.94;alg=hmac-sha-1-96;mod=trans;spi-c=129427853;spi-s=133937015;port-c=6051;port-s=6060"}, {"spi-s", "ipsec-3gpp;q=0.94;alg=hmac-sha-1-96;mod=trans;spi-c=129427852;spi-s=133937016;port-c=6051;port-s=6060"}, @@ -346,19 +411,19 @@ int main() { {"port-s", "ipsec-3gpp;q=0.94;alg=hmac-sha-1-96;mod=trans;spi-c=129427852;spi-s=133937015;port-c=6051;port-s=6061"}, }; for (const auto& [field, text] : off) { - auto r = ChooseResumeSecurityVerify(text, sa, "null"); + auto r = ChooseResumeSecurityVerify(text, sa, {"null"}); Check(r.source == Source::OtherSa && r.value == sa.securityServer, std::format("resume: {} off by one -> the rebuilt entry", field)); } - auto offSpiS = ChooseResumeSecurityVerify(off[1].second, sa, "null"); + auto offSpiS = ChooseResumeSecurityVerify(off[1].second, sa, {"null"}); Check(offSpiS.storedSpiPs && *offSpiS.storedSpiPs == 133937016u, "resume: the stored spi-s is reported for the log line"); - auto none = ChooseResumeSecurityVerify("", sa, "null"); + auto none = ChooseResumeSecurityVerify("", sa, {"null"}); Check(none.source == Source::NoneStored && none.value == sa.securityServer, "resume: nothing stored -> the rebuilt entry"); - auto junk = ChooseResumeSecurityVerify("not a list", sa, "null"); + auto junk = ChooseResumeSecurityVerify("not a list", sa, {"null"}); Check(junk.source == Source::OtherSa && junk.value == sa.securityServer && !junk.storedSpiPs, "resume: an unparseable value -> the rebuilt entry, no spi-s to report"); ExistingSa kpn; kpn.spiPc = 49889723u; kpn.spiPs = 49889722u; kpn.portPc = 33142; kpn.portPs = 6000; kpn.securityServer = "ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=49889723; spi-s=49889722; port-c=33142; port-s=6000"; - auto k = ChooseResumeSecurityVerify(kpn.securityServer, kpn, "aes-cbc"); + auto k = ChooseResumeSecurityVerify(kpn.securityServer, kpn, {"aes-cbc"}); Check(k.source == Source::Stored && k.value == kpn.securityServer && k.mechanisms == 1, "resume: KPN's one-entry list for its pair -> the list, one mechanism"); } // ---- a state file another attempt left behind: the file's UE SPIs against the kernel pair's @@ -373,41 +438,41 @@ int main() { // ---- selection rules and syntax corners { // spec-shaped list: every entry carries the SPI set, md5 preferred by q — we offered sha1, so sha1 wins (audit A1) - auto a1 = ParseSecurityServer("ipsec-3gpp;q=0.1;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=11;spi-s=12;port-c=13;port-s=14,ipsec-3gpp;q=0.2;alg=hmac-md5-96;ealg=aes-cbc;spi-c=11;spi-s=12;port-c=13;port-s=14", "hmac-sha-1-96", "aes-cbc"); + auto a1 = ParseSecurityServer("ipsec-3gpp;q=0.1;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=11;spi-s=12;port-c=13;port-s=14,ipsec-3gpp;q=0.2;alg=hmac-md5-96;ealg=aes-cbc;spi-c=11;spi-s=12;port-c=13;port-s=14", "hmac-sha-1-96", {"aes-cbc"}); Check(a1 && a1->selected == 0 && a1->offerMatched && a1->Selected().alg == "hmac-sha-1-96", "every entry carries SPIs, md5 preferred: the offered sha1 is selected, not the highest q"); // two sha1 entries with different ciphers, both carrying SPIs: the offered cipher wins over q (audit A3) - auto a3 = ParseSecurityServer("ipsec-3gpp;q=0.9;alg=hmac-sha-1-96;ealg=des-ede3-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.8;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", "aes-cbc"); + auto a3 = ParseSecurityServer("ipsec-3gpp;q=0.9;alg=hmac-sha-1-96;ealg=des-ede3-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.8;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", {"aes-cbc"}); Check(a3 && a3->selected == 1 && a3->Carrier().spiPs == 6u, "offered cipher wins over a higher-q cipher we did not offer"); // two matching entries: the higher q - auto two = ParseSecurityServer("ipsec-3gpp;q=0.5;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.9;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", "aes-cbc"); + auto two = ParseSecurityServer("ipsec-3gpp;q=0.5;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.9;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", {"aes-cbc"}); Check(two && two->selected == 1 && two->Selected().spiPs == 6u, "two matching entries: highest q selected"); // nothing matches the offer (md5-only list): fallback = the highest-q carrier, flagged - auto md5 = ParseSecurityServer("ipsec-3gpp;q=0.5;alg=hmac-md5-96;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.9;alg=hmac-md5-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", "aes-cbc"); + auto md5 = ParseSecurityServer("ipsec-3gpp;q=0.5;alg=hmac-md5-96;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;q=0.9;alg=hmac-md5-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", {"aes-cbc"}); Check(md5 && md5->selected == 1 && !md5->offerMatched && md5->carrier == 1, "no match: highest-q carrier, offerMatched=false"); // no q at all, nothing matching: the first carrier - auto noq = ParseSecurityServer("ipsec-3gpp;alg=hmac-md5-96;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;alg=hmac-md5-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", "aes-cbc"); + auto noq = ParseSecurityServer("ipsec-3gpp;alg=hmac-md5-96;spi-c=1;spi-s=2;port-c=3;port-s=4,ipsec-3gpp;alg=hmac-md5-96;ealg=aes-cbc;spi-c=5;spi-s=6;port-c=7;port-s=8", "hmac-sha-1-96", {"aes-cbc"}); Check(noq && noq->selected == 0 && !noq->offerMatched, "no q, no match: first carrier"); // absent alg on the entry counts as the offered one - auto noalg = ParseSecurityServer("ipsec-3gpp;ealg=aes-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", "aes-cbc"); + auto noalg = ParseSecurityServer("ipsec-3gpp;ealg=aes-cbc;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", {"aes-cbc"}); Check(noalg && noalg->offerMatched, "absent alg matches the offer"); // only non-ipsec mechanisms: nothing for us - Check(!ParseSecurityServer("tls;q=0.2,digest;d-alg=MD5", "hmac-sha-1-96", "aes-cbc").has_value(), "tls/digest only -> nullopt"); + Check(!ParseSecurityServer("tls;q=0.2,digest;d-alg=MD5", "hmac-sha-1-96", {"aes-cbc"}).has_value(), "tls/digest only -> nullopt"); // a quoted comma must not split the list; tokens are case-insensitive - auto q = ParseSecurityServer("digest;d-alg=MD5;d-qop=\"auth,auth-int\",IPSEC-3GPP; ALG=HMAC-SHA-1-96; EALG=AES-CBC; SPI-C=11; SPI-S=12; PORT-C=13; PORT-S=14", "HMAC-SHA-1-96", "AES-CBC"); + auto q = ParseSecurityServer("digest;d-alg=MD5;d-qop=\"auth,auth-int\",IPSEC-3GPP; ALG=HMAC-SHA-1-96; EALG=AES-CBC; SPI-C=11; SPI-S=12; PORT-C=13; PORT-S=14", "HMAC-SHA-1-96", {"AES-CBC"}); Check(q && q->mechanisms.size() == 2 && q->mechanisms[0].name == "digest", "quoted comma stays inside the digest mechanism"); Check(q && q->selected == 1 && q->offerMatched && q->Selected().name == "ipsec-3gpp" && q->Selected().alg == "hmac-sha-1-96" && q->Selected().ealg == "aes-cbc" && q->Selected().spiPs == 12u && q->Selected().portPs == 14, "case-insensitive names and values, lower-cased; offer compared case-insensitively"); // prot/mod parsed for the caller to refuse - auto tun = ParseSecurityServer("ipsec-3gpp;alg=hmac-sha-1-96;ealg=aes-cbc;prot=ah;mod=tun;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", "aes-cbc"); + auto tun = ParseSecurityServer("ipsec-3gpp;alg=hmac-sha-1-96;ealg=aes-cbc;prot=ah;mod=tun;spi-c=1;spi-s=2;port-c=3;port-s=4", "hmac-sha-1-96", {"aes-cbc"}); Check(tun && tun->Selected().prot == "ah" && tun->Selected().mod == "tun", "prot/mod parsed"); // spi-s beyond 32 bits, a port beyond 16 bits or zero: absent, not garbage - auto bad = ParseSecurityServer("ipsec-3gpp;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=1;spi-s=4294967296;port-c=0;port-s=70000", "hmac-sha-1-96", "aes-cbc"); + auto bad = ParseSecurityServer("ipsec-3gpp;alg=hmac-sha-1-96;ealg=aes-cbc;spi-c=1;spi-s=4294967296;port-c=0;port-s=70000", "hmac-sha-1-96", {"aes-cbc"}); Check(bad && !bad->Selected().spiPs && !bad->Selected().portPs && !bad->Selected().portPc && bad->Selected().spiPc == 1u, "out-of-range spi-s/port-s and port 0 are absent"); // q syntax - auto q1 = ParseSecurityServer("ipsec-3gpp;q=1;spi-s=1", "hmac-sha-1-96", "null"); + auto q1 = ParseSecurityServer("ipsec-3gpp;q=1;spi-s=1", "hmac-sha-1-96", {"null"}); Check(q1 && q1->Selected().qMilli == 1000, "q=1 -> 1000"); - auto qx = ParseSecurityServer("ipsec-3gpp;q=x;spi-s=1", "hmac-sha-1-96", "null"); + auto qx = ParseSecurityServer("ipsec-3gpp;q=x;spi-s=1", "hmac-sha-1-96", {"null"}); Check(qx && !qx->Selected().qMilli, "q=x -> no q"); - Check(!ParseSecurityServer("", "hmac-sha-1-96", "null").has_value() && !ParseSecurityServer(" , ", "hmac-sha-1-96", "null").has_value(), "empty value -> nullopt"); + Check(!ParseSecurityServer("", "hmac-sha-1-96", {"null"}).has_value() && !ParseSecurityServer(" , ", "hmac-sha-1-96", {"null"}).has_value(), "empty value -> nullopt"); Check(NormalizeEalg("") == "null" && NormalizeEalg("3des") == "des-ede3-cbc" && NormalizeEalg("aes-cbc") == "aes-cbc", "NormalizeEalg"); } diff --git a/tests/Messages/main.cpp b/tests/Messages/main.cpp index e8f6c6f..e08c4da 100644 --- a/tests/Messages/main.cpp +++ b/tests/Messages/main.cpp @@ -212,6 +212,24 @@ int main() { Check(r3same.contains("port-c=45061; port-s=45062\r\n"), "without a client port the in-use one is offered (the initial registration's second REGISTER)"); } + // ---- the offer: one mechanism per offered cipher, in the list's order, + // every one with the REGISTER's SPIs and ports + { + Check(c.ealgOffer == imsd::ipsec::DefaultEalgOffer(), "the default context offers the default list"); + Context two = c; + two.ealgOffer = {"aes-cbc", "null"}; + std::string r1 = BuildRegisterInitial(two, "REGCALLID@2001:db8::db43", "FTAG5678", "REGBRANCH123456", 7449812u, 176466735u); + Check(r1.contains("\r\nSecurity-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062\r\n"), "two ciphers offered: two mechanisms in one Security-Client"); + std::string auth = AuthAka(two, "NONCEXYZ", "CNONCE0123456789", "RESP0000"); + std::string r2 = BuildRegisterProtected(two, "REGCALLID@2001:db8::db43", "FTAG5678", "REGBRANCH123456", 2, 7449812u, 176466735u, auth); + Check(r2.contains("\r\nSecurity-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062\r\n"), "the protected REGISTER repeats the initial one's Security-Client (TS 33.203 Annex H)"); + std::string r3 = BuildRegisterProtected(two, "REGCALLID@2001:db8::db43", "FTAG5678", "REGBRANCH123456", 3, 55555u, 66666u, auth, 45063); + Check(r3.contains("\r\nSecurity-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=55555; spi-s=66666; port-c=45063; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=55555; spi-s=66666; port-c=45063; port-s=45062\r\n"), "a reregistration's new SPIs and client port in every mechanism"); + two.ealgOffer = {"null", "des-ede3-cbc"}; + std::string r4 = BuildRegisterInitial(two, "REGCALLID@2001:db8::db43", "FTAG5678", "REGBRANCH123456", 7449812u, 176466735u); + Check(r4.contains("\r\nSecurity-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=des-ede3-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062\r\n"), "the list's order is the header's order"); + } + // ---- protected-leg transport follows Context.transport (SIP_TRANSPORT=udp, // or the auto fallback): every protected request's Via says UDP, the // challenge's Via stays UDP either way, and TCP is the untouched default.