ec20_adapter_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. package vowifi
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/hex"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "sync"
  10. "testing"
  11. "vocat/internal/modem"
  12. )
  13. type ec20TranscriptStep struct {
  14. command string
  15. sensitive bool
  16. lines []string
  17. final string
  18. err error
  19. }
  20. type ec20Transcript struct {
  21. t *testing.T
  22. mu sync.Mutex
  23. steps []ec20TranscriptStep
  24. next int
  25. }
  26. func TestICCIDIdentifierStripsBCDPadding(t *testing.T) {
  27. for _, test := range []struct {
  28. wire string
  29. want string
  30. }{
  31. {wire: "8944110069353447454F", want: "8944110069353447454"},
  32. {wire: "894921007608519523FF", want: "894921007608519523"},
  33. } {
  34. response := modem.Response{Lines: []string{"+QCCID: " + test.wire}}
  35. if got := iccidIdentifier(response, []string{"+CCID:", "+QCCID:"}, 18, 22); got != test.want {
  36. t.Fatalf("iccidIdentifier(%q) = %q, want %q", test.wire, got, test.want)
  37. }
  38. }
  39. }
  40. func (transcript *ec20Transcript) ExecuteAT(
  41. _ context.Context,
  42. _ string,
  43. command string,
  44. ) (modem.Response, error) {
  45. return transcript.execute(command, false)
  46. }
  47. func (transcript *ec20Transcript) ExecuteSensitiveAT(
  48. _ context.Context,
  49. _ string,
  50. command string,
  51. ) (modem.Response, error) {
  52. return transcript.execute(command, true)
  53. }
  54. func (transcript *ec20Transcript) execute(
  55. command string,
  56. sensitive bool,
  57. ) (modem.Response, error) {
  58. transcript.t.Helper()
  59. transcript.mu.Lock()
  60. defer transcript.mu.Unlock()
  61. if transcript.next >= len(transcript.steps) {
  62. transcript.t.Fatalf("unexpected EC20 command %q", command)
  63. }
  64. step := transcript.steps[transcript.next]
  65. transcript.next++
  66. if command != step.command {
  67. transcript.t.Fatalf(
  68. "EC20 command %d = %q, want %q",
  69. transcript.next,
  70. command,
  71. step.command,
  72. )
  73. }
  74. if sensitive != step.sensitive {
  75. transcript.t.Fatalf(
  76. "EC20 command %q sensitive=%v, want %v",
  77. command,
  78. sensitive,
  79. step.sensitive,
  80. )
  81. }
  82. final := step.final
  83. if final == "" && step.err == nil {
  84. final = "OK"
  85. }
  86. return modem.Response{
  87. Command: command,
  88. Lines: append([]string(nil), step.lines...),
  89. Final: final,
  90. }, step.err
  91. }
  92. func (transcript *ec20Transcript) assertDone() {
  93. transcript.t.Helper()
  94. transcript.mu.Lock()
  95. defer transcript.mu.Unlock()
  96. if transcript.next != len(transcript.steps) {
  97. transcript.t.Fatalf(
  98. "consumed %d/%d EC20 transcript steps",
  99. transcript.next,
  100. len(transcript.steps),
  101. )
  102. }
  103. }
  104. func TestEC20AdapterCSIMFallbackSupportsSuccessAndSynchronizationFailure(
  105. t *testing.T,
  106. ) {
  107. t.Parallel()
  108. tests := []struct {
  109. name string
  110. apdu []byte
  111. wantErr error
  112. assert func(*testing.T, AKAResult)
  113. }{
  114. {
  115. name: "success",
  116. apdu: successfulUSIMResponse(),
  117. assert: func(t *testing.T, result AKAResult) {
  118. t.Helper()
  119. if result.SynchronizationFailure {
  120. t.Fatal("successful result was marked as synchronization failure")
  121. }
  122. if !bytes.Equal(result.RES, []byte{1, 2, 3, 4, 5, 6, 7, 8}) {
  123. t.Fatalf("RES = %x", result.RES)
  124. }
  125. if len(result.CK) != 16 || len(result.IK) != 16 {
  126. t.Fatalf("CK/IK lengths = %d/%d", len(result.CK), len(result.IK))
  127. }
  128. },
  129. },
  130. {
  131. name: "synchronization_failure",
  132. apdu: synchronizationFailureUSIMResponse(),
  133. assert: func(t *testing.T, result AKAResult) {
  134. t.Helper()
  135. if !result.SynchronizationFailure {
  136. t.Fatal("AUTS result was not marked as synchronization failure")
  137. }
  138. if len(result.AUTS) != 14 {
  139. t.Fatalf("AUTS length = %d", len(result.AUTS))
  140. }
  141. if len(result.RES) != 0 || len(result.CK) != 0 || len(result.IK) != 0 {
  142. t.Fatal("synchronization failure exposed a success vector")
  143. }
  144. },
  145. },
  146. {
  147. name: "mac_failure_9862",
  148. apdu: []byte{0x98, 0x62},
  149. wantErr: ErrEC20AKAMACFailure,
  150. },
  151. }
  152. for _, test := range tests {
  153. test := test
  154. t.Run(test.name, func(t *testing.T) {
  155. t.Parallel()
  156. var challenge AKAChallenge
  157. for index := range challenge.RAND {
  158. challenge.RAND[index] = byte(index)
  159. challenge.AUTN[index] = byte(0xf0 + index)
  160. }
  161. authAPDU := buildUSIMAuthenticateAPDU(challenge)
  162. authCommand := fmt.Sprintf(
  163. `AT+CSIM=%d,"%s"`,
  164. len(authAPDU)*2,
  165. strings.ToUpper(hex.EncodeToString(authAPDU)),
  166. )
  167. encodedResponse := strings.ToUpper(hex.EncodeToString(test.apdu))
  168. transcript := &ec20Transcript{
  169. t: t,
  170. steps: append(
  171. identityTranscriptSteps("234150123456789"),
  172. ec20TranscriptStep{
  173. command: "AT+CCID",
  174. lines: []string{"+CCID: 8944101234567890123"},
  175. },
  176. ec20TranscriptStep{
  177. command: "AT+CUAD",
  178. lines: []string{
  179. `+CUAD: 22,"61094F07A0000000871002"`,
  180. },
  181. },
  182. ec20TranscriptStep{
  183. command: `AT+CCHO="A0000000871002"`,
  184. err: errors.New("unsupported"),
  185. final: "ERROR",
  186. },
  187. // The SELECT response requests GET RESPONSE. This is the
  188. // behavior observed on EC20 basic-channel firmware.
  189. ec20TranscriptStep{
  190. command: `AT+CSIM=24,"00A4040407A0000000871002"`,
  191. lines: []string{`+CSIM: 4,"613A"`},
  192. },
  193. ec20TranscriptStep{
  194. command: `AT+CSIM=10,"00C000003A"`,
  195. lines: []string{`+CSIM: 4,"9000"`},
  196. },
  197. ec20TranscriptStep{
  198. command: "AT+CCID",
  199. lines: []string{"+CCID: 8944101234567890123"},
  200. },
  201. ec20TranscriptStep{
  202. command: `AT+CSIM=24,"00A4040407A0000000871002"`,
  203. lines: []string{`+CSIM: 4,"9000"`},
  204. },
  205. ec20TranscriptStep{
  206. command: authCommand,
  207. sensitive: true,
  208. lines: []string{fmt.Sprintf(
  209. `+CSIM: %d,"%s"`,
  210. len(encodedResponse),
  211. encodedResponse,
  212. )},
  213. },
  214. ),
  215. }
  216. adapter, err := NewEC20Adapter(transcript, EC20AdapterOptions{})
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. identity, err := adapter.ReadIdentity(context.Background(), "ec20-1")
  221. if err != nil {
  222. t.Fatalf("ReadIdentity: %v", err)
  223. }
  224. evidence, err := adapter.CheckReady(context.Background(), identity)
  225. if err != nil {
  226. t.Fatalf("CheckReady: %v", err)
  227. }
  228. if !evidence.Ready || evidence.Application != "USIM" {
  229. t.Fatalf("AKA evidence = %#v", evidence)
  230. }
  231. result, err := adapter.Authenticate(
  232. context.Background(),
  233. identity,
  234. challenge,
  235. )
  236. if test.wantErr != nil {
  237. if !errors.Is(err, test.wantErr) {
  238. t.Fatalf("Authenticate error = %v, want %v", err, test.wantErr)
  239. }
  240. transcript.assertDone()
  241. return
  242. }
  243. if err != nil {
  244. t.Fatalf("Authenticate: %v", err)
  245. }
  246. test.assert(t, result)
  247. transcript.assertDone()
  248. })
  249. }
  250. }
  251. func TestEC20AdapterLogicalChannelAuthenticateFollowsGetResponse(
  252. t *testing.T,
  253. ) {
  254. t.Parallel()
  255. var challenge AKAChallenge
  256. for index := range challenge.RAND {
  257. challenge.RAND[index] = byte(index)
  258. challenge.AUTN[index] = byte(0xf0 + index)
  259. }
  260. authAPDU := buildUSIMAuthenticateAPDU(challenge)
  261. authCommand := fmt.Sprintf(
  262. `AT+CGLA=1,%d,"%s"`,
  263. len(authAPDU)*2,
  264. strings.ToUpper(hex.EncodeToString(authAPDU)),
  265. )
  266. chainedResponse := logicalChainedUSIMResponse()
  267. if len(chainedResponse)-2 != 0x35 {
  268. t.Fatalf(
  269. "test response body length = %d, want 0x35",
  270. len(chainedResponse)-2,
  271. )
  272. }
  273. encodedResponse := strings.ToUpper(hex.EncodeToString(chainedResponse))
  274. transcript := &ec20Transcript{
  275. t: t,
  276. steps: append(
  277. identityTranscriptSteps("234150123456789"),
  278. ec20TranscriptStep{
  279. command: "AT+CCID",
  280. lines: []string{"+CCID: 8944101234567890123"},
  281. },
  282. ec20TranscriptStep{
  283. command: "AT+CUAD",
  284. lines: []string{
  285. `+CUAD: 22,"61094F07A0000000871002"`,
  286. },
  287. },
  288. ec20TranscriptStep{
  289. command: `AT+CCHO="A0000000871002"`,
  290. lines: []string{"+CCHO: 1"},
  291. },
  292. ec20TranscriptStep{command: "AT+CCHC=1"},
  293. ec20TranscriptStep{
  294. command: "AT+CCID",
  295. lines: []string{"+CCID: 8944101234567890123"},
  296. },
  297. ec20TranscriptStep{
  298. command: `AT+CCHO="A0000000871002"`,
  299. lines: []string{"+CCHO: 1"},
  300. },
  301. ec20TranscriptStep{
  302. command: authCommand,
  303. sensitive: true,
  304. lines: []string{`+CGLA: 4,"6135"`},
  305. },
  306. ec20TranscriptStep{
  307. command: `AT+CGLA=1,10,"00C0000035"`,
  308. sensitive: true,
  309. lines: []string{fmt.Sprintf(
  310. `+CGLA: %d,"%s"`,
  311. len(encodedResponse),
  312. encodedResponse,
  313. )},
  314. },
  315. ec20TranscriptStep{command: "AT+CCHC=1"},
  316. ),
  317. }
  318. adapter, err := NewEC20Adapter(transcript, EC20AdapterOptions{})
  319. if err != nil {
  320. t.Fatal(err)
  321. }
  322. identity, err := adapter.ReadIdentity(context.Background(), "ec20-1")
  323. if err != nil {
  324. t.Fatalf("ReadIdentity: %v", err)
  325. }
  326. if _, err := adapter.CheckReady(context.Background(), identity); err != nil {
  327. t.Fatalf("CheckReady: %v", err)
  328. }
  329. result, err := adapter.Authenticate(
  330. context.Background(),
  331. identity,
  332. challenge,
  333. )
  334. if err != nil {
  335. t.Fatalf("Authenticate: %v", err)
  336. }
  337. if !bytes.Equal(result.RES, []byte{1, 2, 3, 4, 5, 6, 7, 8}) ||
  338. len(result.CK) != 16 ||
  339. len(result.IK) != 16 {
  340. t.Fatalf(
  341. "AKA result RES=%x CK=%d IK=%d",
  342. result.RES,
  343. len(result.CK),
  344. len(result.IK),
  345. )
  346. }
  347. transcript.assertDone()
  348. }
  349. func TestEC20AdapterReadsExplicitHomePLMNAndKnownAssignmentFallback(
  350. t *testing.T,
  351. ) {
  352. t.Parallel()
  353. tests := []struct {
  354. name string
  355. steps []ec20TranscriptStep
  356. }{
  357. {
  358. name: "EF_AD",
  359. steps: identityTranscriptSteps("234150123456789"),
  360. },
  361. {
  362. name: "assigned HPLMN when EF_AD omits MNC length",
  363. steps: append(
  364. identityTranscriptStepsWithoutEFAD("234150123456789"),
  365. ec20TranscriptStep{
  366. command: "AT+CRSM=176,28589,0,0,4",
  367. err: errors.New("not available"),
  368. final: "ERROR",
  369. },
  370. ec20TranscriptStep{
  371. command: "AT+CRSM=176,28589,0,0,0",
  372. err: errors.New("not available"),
  373. final: "ERROR",
  374. },
  375. ),
  376. },
  377. }
  378. for _, test := range tests {
  379. test := test
  380. t.Run(test.name, func(t *testing.T) {
  381. t.Parallel()
  382. transcript := &ec20Transcript{t: t, steps: test.steps}
  383. adapter, err := NewEC20Adapter(transcript, EC20AdapterOptions{})
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. identity, err := adapter.ReadIdentity(context.Background(), "ec20-1")
  388. if err != nil {
  389. t.Fatalf("ReadIdentity: %v", err)
  390. }
  391. if identity.HomeMCC != "234" || identity.HomeMNC != "15" {
  392. t.Fatalf(
  393. "home PLMN = %s/%s, want 234/15",
  394. identity.HomeMCC,
  395. identity.HomeMNC,
  396. )
  397. }
  398. transcript.assertDone()
  399. })
  400. }
  401. }
  402. func TestEC20AdapterRadioTransactionRestoresCFUNAndPDPContexts(
  403. t *testing.T,
  404. ) {
  405. t.Parallel()
  406. transcript := &ec20Transcript{
  407. t: t,
  408. steps: []ec20TranscriptStep{
  409. {command: "AT+CFUN?", lines: []string{"+CFUN: 1"}},
  410. {
  411. command: "AT+CGACT?",
  412. lines: []string{"+CGACT: 1,1", "+CGACT: 2,0"},
  413. },
  414. {command: "AT+CFUN?", lines: []string{"+CFUN: 1"}},
  415. {command: "AT+CFUN=4"},
  416. {command: "AT+CFUN?", lines: []string{"+CFUN: 4"}},
  417. {
  418. command: "AT+CGACT?",
  419. lines: []string{"+CGACT: 1,0", "+CGACT: 2,0"},
  420. },
  421. {
  422. command: "AT+CGACT?",
  423. lines: []string{"+CGACT: 1,0", "+CGACT: 2,0"},
  424. },
  425. {command: "AT+CFUN?", lines: []string{"+CFUN: 4"}},
  426. {command: "AT+CFUN=1"},
  427. {command: "AT+CFUN?", lines: []string{"+CFUN: 1"}},
  428. {
  429. command: "AT+CGACT?",
  430. lines: []string{"+CGACT: 1,0", "+CGACT: 2,0"},
  431. },
  432. {command: "AT+CGACT=1,1"},
  433. {
  434. command: "AT+CGACT?",
  435. lines: []string{"+CGACT: 1,1", "+CGACT: 2,0"},
  436. },
  437. },
  438. }
  439. adapter, err := NewEC20Adapter(transcript, EC20AdapterOptions{
  440. PureAirplanePolicy: func(string) bool { return true },
  441. RestoreCellularData: true,
  442. })
  443. if err != nil {
  444. t.Fatal(err)
  445. }
  446. snapshot, err := adapter.Snapshot(context.Background(), "ec20-1")
  447. if err != nil {
  448. t.Fatalf("Snapshot: %v", err)
  449. }
  450. if !snapshot.CellularDataEnabled ||
  451. snapshot.OperatingMode != 1 ||
  452. !snapshot.PureAirplanePolicy {
  453. t.Fatalf("snapshot = %#v", snapshot)
  454. }
  455. if err := adapter.EnterVoWiFiRFOff(
  456. context.Background(),
  457. "ec20-1",
  458. ); err != nil {
  459. t.Fatalf("EnterVoWiFiRFOff: %v", err)
  460. }
  461. if err := adapter.StopCellularData(
  462. context.Background(),
  463. "ec20-1",
  464. ); err != nil {
  465. t.Fatalf("StopCellularData: %v", err)
  466. }
  467. if err := adapter.Restore(
  468. context.Background(),
  469. "ec20-1",
  470. snapshot,
  471. ); err != nil {
  472. t.Fatalf("Restore: %v", err)
  473. }
  474. transcript.assertDone()
  475. }
  476. func TestEC20AdapterNeverStartsCellularDataByDefault(t *testing.T) {
  477. t.Parallel()
  478. transcript := &ec20Transcript{
  479. t: t,
  480. steps: []ec20TranscriptStep{
  481. {command: "AT+CFUN?", lines: []string{"+CFUN: 1"}},
  482. {command: "AT+CGACT?", lines: []string{"+CGACT: 1,1"}},
  483. {command: "AT+CFUN?", lines: []string{"+CFUN: 1"}},
  484. {command: "AT+CFUN?", lines: []string{"+CFUN: 1"}},
  485. {command: "AT+CGACT?", lines: []string{"+CGACT: 1,1"}},
  486. {command: "AT+CGACT=0,1"},
  487. {command: "AT+CGACT?", lines: []string{"+CGACT: 1,0"}},
  488. },
  489. }
  490. adapter, err := NewEC20Adapter(transcript, EC20AdapterOptions{})
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. snapshot, err := adapter.Snapshot(context.Background(), "ec20-1")
  495. if err != nil {
  496. t.Fatalf("Snapshot: %v", err)
  497. }
  498. if err := adapter.Restore(context.Background(), "ec20-1", snapshot); err != nil {
  499. t.Fatalf("Restore: %v", err)
  500. }
  501. transcript.assertDone()
  502. }
  503. func identityTranscriptSteps(imsi string) []ec20TranscriptStep {
  504. return append(
  505. identityTranscriptStepsWithoutEFAD(imsi),
  506. ec20TranscriptStep{
  507. command: "AT+CRSM=176,28589,0,0,4",
  508. lines: []string{`+CRSM: 144,0,"00000002"`},
  509. },
  510. )
  511. }
  512. func identityTranscriptStepsWithoutEFAD(imsi string) []ec20TranscriptStep {
  513. return []ec20TranscriptStep{
  514. {command: "AT+CPIN?", lines: []string{"+CPIN: READY"}},
  515. {command: "AT+CIMI", lines: []string{imsi}},
  516. {
  517. command: "AT+CCID",
  518. lines: []string{"+CCID: 8944101234567890123"},
  519. },
  520. {command: "AT+CGSN", lines: []string{"867530912345678"}},
  521. }
  522. }
  523. func successfulUSIMResponse() []byte {
  524. res := []byte{1, 2, 3, 4, 5, 6, 7, 8}
  525. ck := bytes.Repeat([]byte{0x11}, 16)
  526. ik := bytes.Repeat([]byte{0x22}, 16)
  527. kc := bytes.Repeat([]byte{0x33}, 8)
  528. value := []byte{byte(len(res))}
  529. value = append(value, res...)
  530. value = append(value, byte(len(ck)))
  531. value = append(value, ck...)
  532. value = append(value, byte(len(ik)))
  533. value = append(value, ik...)
  534. value = append(value, byte(len(kc)))
  535. value = append(value, kc...)
  536. raw := []byte{0xdb}
  537. raw = append(raw, value...)
  538. return append(raw, 0x90, 0x00)
  539. }
  540. func logicalChainedUSIMResponse() []byte {
  541. res := []byte{1, 2, 3, 4, 5, 6, 7, 8}
  542. ck := bytes.Repeat([]byte{0x11}, 16)
  543. ik := bytes.Repeat([]byte{0x22}, 16)
  544. kc := bytes.Repeat([]byte{0x33}, 8)
  545. value := []byte{byte(len(res))}
  546. value = append(value, res...)
  547. value = append(value, byte(len(ck)))
  548. value = append(value, ck...)
  549. value = append(value, byte(len(ik)))
  550. value = append(value, ik...)
  551. value = append(value, byte(len(kc)))
  552. value = append(value, kc...)
  553. raw := []byte{0xdb}
  554. raw = append(raw, value...)
  555. return append(raw, 0x90, 0x00)
  556. }
  557. func synchronizationFailureUSIMResponse() []byte {
  558. auts := make([]byte, 14)
  559. for index := range auts {
  560. auts[index] = byte(0xa0 + index)
  561. }
  562. raw := []byte{0xdc, byte(len(auts))}
  563. raw = append(raw, auts...)
  564. return append(raw, 0x90, 0x00)
  565. }