I’m working on a VoWifi connection to a new provider. StrongSWAN is able to connect IPsec to their ePDG.
When my code tries to connect an inner IPsec tunnel to the P-CSCF the AUTHENTICATE fails with an incorrect MAC:
pySIM-shell (00:MF/ADF.USIM)> authenticate 65ac...308a 71ab...ac85
EXCEPTION of type 'SwMatchError' occurred with message: SW match failed!
Expected 9000 and got 9862: Security management - Authentication error, incorrect MAC
This would usually mean I’m using the wrong SIM card, like some other provider’s SIM card so the key material is wrong, but this is the only SIM in the system. I removed all other cards.
If I copy the Nonce from the StrongSWAN connection and try to re-run the authenticate in pySIM-shell, it all goes as expected: the MAC passes but the authentication fails due to the sequence number and returns an AUTS.
pySIM-shell (00:MF/ADF.USIM)> authenticate c2e7...f20f de29...fff4
{
"synchronisation_failure": {
"auts": "7020...5f50"
}
}
What makes me really confused is a pcap from an iPhone using this same SIM card and successfully connecting to the provider’s P-CSCF. I see the iPhone send a REGISTER, I see the P-CSCF send back a 401 with a Nonce, I see the iPhone successfully establish a new IPsec tunnel — so the iPhone’s authenticate must have worked.
If I try to use the Nonce from the iPhone pcap with pySim-shell AUTHENTICATE I’d expect to get an AUTS since the sequence number will be wrong. But I don’t, I get incorrect MAC. That makes no sense, I don’t understand how the iPhone manages to authenticate.
pySIM-shell (00:MF/ADF.USIM)> authenticate 753b...1583 0c7e...d79b
EXCEPTION of type 'SwMatchError' occurred with message: SW match failed!
Expected 9000 and got 9862: Security management - Authentication error, incorrect MAC
I read in OPc vs OP in SIM keys | Nick vs Networking that recent SIM cards sometimes no longer have the same Operator key across all SIM cards, that they instead make Operator be just more random bits which are unique to each SIM. So every SIM card has completely unique keying material, and they have to look up both K and OP for each SIM.
It occurs to me that if I’ve gotten my IMSI wrong, and the provider is sending a Nonce which is intended some other IMSI, that this would fail with an Incorrect MAC because the OP key wouldn’t work.
I’ve checked and double-checked the IMSI and don’t see a problem, but I’m going to keep looking as this seems likely. I’ve also looked at the contents of the iPhone’s REGISTER, and I’m sending the same Authorization in mine with the same username and realm and other fields.
Are there other ways which I’m not thinking of where the symptom of the failure would be an incorrect MAC?
There is an odd thing about this Alcatel-Lucent P-CSCF: it includes a very long opaque field in the WWW-Authenticate of its 401, where the value starts with ALU:
WWW-Authenticate: Digest realm="one.att.net",
nonce="ZazW...7rIU=",
opaque="ALU:QbkR...AZGg__",
algorithm=AKAv1-MD5,
qop="auth"
I assume I’m not supposed to use opaque as part of the authenticate operation somehow. It looks like something I’m just supposed to send back at some point.