UPDATE: Nevermind, this was my doing, I’d broken it and messed up switching back to the branch which had previously worked. After correcting that, it is working again and able to authenticate for the P-CSCF connection.
I got a ck and ik back from the SIM card which are used to set up the inner IPsec tunnel to the P-CSCF, which works because I can communicate with it.
I then send another REGISTER, this time with the Response field populated in the Authorization.
Sending: REGISTER sip:msg.pc.t-mobile.com SIP/2.0
Via: SIP/2.0/TCP [2607:fc20:c777:d725:ac39:9052:7d40:d28d]:65529;branch=z9hG4bK-c7dede348e465e4a;rport
Security-Verify: ipsec-3gpp;alg=hmac-sha-1-96;ealg=aes-cbc;mod=trans;port-c=60653;port-s=65529;prot=esp;q=0.5;spi-c=496345;spi-s=3717969603
Contact: <sip:[2607:fc20:c777:d725:ac39:9052:7d40:d28d]:65529>;+g.3gpp.icsi-ref="urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel";+g.3gpp.smsip;+g.3gpp.srvcc-alerting;+sip.instance="<urn:gsma:imei:12345678-123456-0>";text
From: <sip:123456789012345@ims.mnc240.mcc310.3gppnetwork.org>;tag=77166
To: <sip:123456789012345@ims.mnc240.mcc310.3gppnetwork.org>
CSeq: 2 REGISTER
Max-Forwards: 70
Authorization: Digest uri="sip:msg.pc.t-mobile.com",username="123456789012345@msg.pc.t-mobile.com",response="25873fad344b63ebd42079501cecd3c1",realm="msg.pc.t-mobile.com",nonce="yWn9XkOBFJ0YEEjMvo/7XVj/TP32fwAAf/fH4m7VjTM=",algorithm=AKAv1-MD5
Expires: 600000
Require: sec-agree
Proxy-Require: sec-agree
k: path,sec-agree
Allow: INVITE,BYE,CANCEL,ACK,NOTIFY,UPDATE,PRACK,INFO,MESSAGE,OPTIONS
User-Agent: T-Mobile VoLTE-ePDG-RTT-ussd AAA
Call-ID: kZhehmGjrInqZEiOLYMuuvkA
Session-ID: gNqMxV0d14psDvq7Jtzv8JlA86Q3O5NX
Content-Length: 0
The Response was calculated using:
imsi_name = "123456789012345@msg.pc.t-mobile.com"
realm = "msg.pc.t-mobile.com"
sres = ...returned from card.USIM's authenticate method...
nonceb64 = ... the b64-encoded Nonce from T/Mobiles 401 Unauthorized...
str1 = hashlib.md5(f"{imsi_name}:{realm}:{sres}".encode('ascii')).hexdigest()
str2 = hashlib.md5(f"REGISTER:sip:{realm}".encode('ascii')).hexdigest()
response = hashlib.md5(f"{str1}:{nonceb64}:{str2}".encode('ascii')).hexdigest()
T/Mobile doesn’t like this and sends back “494 Security Agreement Required.”
I’ll keep working on it.
1 Like
Still stuck, after working on this for a few days now. I can:
-
Connect strongswan-epdg to the T/Mobile ePDG using my Tello SIM.
-
Send a SIP REGISTER, get back a 401 Unauthenticated.
-
Extract the RAND and AUTN from the Nonce in the Unauthenticated response, call the USIM authenticate() method to get back an sres, ck, and ik.
-
Use the ck and ik to establish another IPsec tunnel to the P-CSCF, inside of the IPsec tunnel to the ePDG.
-
Connect to the T/Mobile P-CSCF again. Running tcpdump on the strongswan-epdg tun23 device, I can see the packets change from a TCP connection containing the first REGISTER to ESP packets for the new IPsec SAs I just set up.
-
Send another REGISTER, this one with a Response populated in the Authentication field: Authorization: Digest uri="sip:msg.pc.t-mobile.com",username="012345678901234@msg.pc.t-mobile.com",response="602e492ca59d6bf87cf4c3e9c2df5254",realm="msg.pc.t-mobile.com",nonce="ow52pdMghON/7QudwknvCANStG/TjwAAzvXOe3YPzto=",algorithm=AKAv1-MD5 and with a Security-Verify where I copied the Security-Server from the 401 Unauthenticated response.
-
The T/Mobile P-CSCF sends back 494 Security Agreement Required
I’ve tried a few variations on how to include the sres in the Response, including bytes(vector[0]) and bytes(vector[0]).hex() and bytes(vector[0]).hex().upper().
vector = u.authenticate(RAND=..., AUTN=..., ctx='3G')
sres = bytes(vector[0])
...
imsi_name = "012345678901234@msg.pc.t-mobile.com"
realm = "msg.pc.t-mobile.com"
nonceb64 = ...extracted from the 401 Unauthenticated message, still base64 encoded exactly as it was sent to me...
str1 = hashlib.md5(f"{imsi_name}:{realm}:".encode('ascii') + sres).hexdigest()
str2 = hashlib.md5(f"REGISTER:sip:{realm}".encode('ascii')).hexdigest()
response = hashlib.md5(f"{str1}:{nonceb64}:{str2}".encode('ascii')).hexdigest()
Honestly I don’t even know that the problem is with the sres specifically, maybe I have one of the other components differently from how it is supposed to be done. I based the username and realm values on what I see in a packet trace on an iPhone using the rvi0 debugging interface it supplies (which is wonderful).
I’ve also tried a just completely random Response value and get the same 494 back from T/Mobile, to try to confirm that the issue is with how I’m computing the Response value.
It is frustrating that I don’t think there is a better way to debug this than to try things until it works.
1 Like
Still stuck in this same state.
If I remove the Security-Verify the response changes to 400 Bad Request. Similarly if I remove the Require or Proxy-Require the response changes to a 400. So I think those are correct, allowing the P-CSCF to get to the point where it decides to send a 494.
If I deliberately corrupt the hex value of the Response within the Authentication header, I still get a 494. So it is plausible that an incorrect Response is leading to the 494.
The Response is a digest computer over the Username, Realm, and Nonce, plus the sres from the SIM Card’s Authenticate. I’m using the same Username and Realm which I can see in an iPhone packet trace from rvi0 which succeeded. I’ve checked and double-checked that the Nonce is exactly the same as I received it from the P-CSCF.
I think that only leaves the sres. I just can’t see what I’m doing wrong.
I tried using osmo-sim-auth.py to run the authentication and parse its output, in case I was somehow doing it incorrectly, but it would still get a 494 back from T/Mobile.
I figured it out. There was a problem with calculation of the Authentication Response, but that wasn’t the reason for the 494. The description of the 494 talked about copying the Security-Server value into Security-Verify, which I’d checked and re-checked, but the actual problem was with Security-Client.
In the very first REGISTER before setting up the inner IPsec tunnel to the P-CSCF one makes up a port-s and spi-s to include in the Security-Client. When the other end sent back its 401, I was updating its spi-s in a way which changed the Security-Client sent in the next REGISTER after setting up IPsec. The 494 was complaining that the Security-Client didn’t match what I’d sent before.
After fixing that, it would get a 403 Forbidden with Reason: SIP;cause=403;text="CC_IMS_AUTH_RESP_MISMATCH"
Corrected code to calculate the Response:
sres = bytes(vector[0])
...
str1 = hashlib.md5(f"{imsi}@{realm}:{realm}:".encode("latin1") + sres).hexdigest()
str2 = hashlib.md5(f"REGISTER:sip:{realm}".encode("latin1")).hexdigest()
response = hashlib.md5(f"{str1}:{nonceb64}:{str2}".encode("latin1")).hexdigest()