Skip to content

Commit

Permalink
handle MSISDN reading failure and refine the log
Browse files Browse the repository at this point in the history
  • Loading branch information
WingPig99 committed Jun 19, 2022
1 parent 9714466 commit 09f61ee
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 38 deletions.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/WingPig99/usim_go

go 1.18

require (
github.com/free5gc/milenage v1.0.0
github.com/sf1/go-card v1.2.0
github.com/sirupsen/logrus v1.8.1
)

require golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/free5gc/milenage v1.0.0 h1:OMSdAuJpOW71REje4iahCVt0sBJa5FNmJK+iDQtYJzo=
github.com/free5gc/milenage v1.0.0/go.mod h1:QJYEh/7FTg2AXCo80BOrdwbAvm3q8OpVckR3SjwTvw8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sf1/go-card v1.2.0 h1:aOcQd6y+kuqrkMgwZVHeWsq0fmt8luW/INBj//ABBj4=
github.com/sf1/go-card v1.2.0/go.mod h1:cdx/MUznsBaRXw7G3KEw8RAnEdAakPz99+rT+iFDy+I=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion usim.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func InitPcscUSIM(seq int) (u USIM, err error) {
return
} else {
// to do: handle multiple readers choices
logrus.Errorf("multiple readers found, using the %d\n", seq)
logrus.Infof("multiple readers found, using the %d\n", seq)
u.reader = readers[seq]
// return
}
Expand Down
14 changes: 7 additions & 7 deletions usim_pcsc_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func getIMSI(ctx *smartcard.Card) (string, error) {
logrus.Error("Select USIM APP file failed: ", err)
}
// reading IMSI
logrus.Info("SCARD: reading IMSI from (GSM) EF-IMSI")
logrus.Debug("SCARD: reading IMSI from (GSM) EF-IMSI")
if resp, err = _select_file(ctx, SCARD_FILE_GSM_EF_IMSI, cardType, nil); err != nil {
logrus.Debug("reading SCARD_FILE_GSM_EF_IMSI failed: ", err)
return "", errors.New("reading SCARD_FILE_GSM_EF_IMSI failed")
Expand Down Expand Up @@ -217,7 +217,6 @@ func getMSISDN(ctx *smartcard.Card) (msisdn string, err error) {
_select_file(ctx, SCARD_FILE_MF, SCARD_USIM, nil)
_select_file(ctx, 0, SCARD_USIM, aid)
_select_file(ctx, SCARD_FILE_GSM_EF_MSISDN, SCARD_USIM, nil)
logrus.Debug("reading MSISDN failed: ", err)
return "", errors.New("reading MSISDN failed")

}
Expand All @@ -234,8 +233,12 @@ func getMSISDN(ctx *smartcard.Card) (msisdn string, err error) {
}
resp = resp[16:]
swapHex(resp[2:])
dailLen := (resp[0]-2)*2 + 1
msisdn = hex.EncodeToString(resp[2:])[:dailLen]
dailLen := int((resp[0]-2)*2 + 1)
if dailLen <= len(hex.EncodeToString(resp[2:])) {
msisdn = hex.EncodeToString(resp[2:])[:dailLen]
} else {
err = errors.New("reading MSISDN failed")
}
return
}

Expand Down Expand Up @@ -264,14 +267,11 @@ func AKAVerify(ctx *smartcard.Card, simType int, aid []byte, rand, auth []byte)
logrus.Debug("Got response:\n", hex.Dump(resp))
if len(resp) == 2 && resp[0] == 0x98 && resp[1] == 0x62 {
// Authentication error, application specific
errStr := "SCARD: UMTS auth failed - MAC != XMAC"
logrus.Error(errStr)
err = errors.New("SCARD: UMTS auth failed - MAC != XMAC")
return
}
if len(resp) != 2 || resp[0] != 0x61 {
errStr := fmt.Sprintf("SCARD: unexpected response for UMTS auth request (len=%d resp=0x%02X%02X)", len(resp), resp[0], resp[1])
logrus.Error(errStr)
err = errors.New(errStr)
return
}
Expand Down
42 changes: 13 additions & 29 deletions usim_pcsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package usim_go

import (
"encoding/hex"
"fmt"
"testing"

"github.com/sf1/go-card/smartcard"
"github.com/sirupsen/logrus"
)

func init() {
logrus.SetLevel(logrus.DebugLevel)
// logrus.SetLevel(logrus.DebugLevel)
}
func TestISIM_Authentication(t *testing.T) {
u, err := InitPcscUSIM(-1)
u, err := InitPcscUSIM(1)
if err != nil {
t.Fatal(err)
}
Expand All @@ -38,11 +37,8 @@ func TestReadIMSI(t *testing.T) {
if len(readers) == 0 {
t.Error("please insert smart card")
return
} else if len(readers) == 1 {
reader = readers[0]
} else {
// to do: handle multiple readers choices
t.Error("multiple readers not supported")
reader = readers[0]
}
//
card, err := reader.Connect()
Expand All @@ -53,7 +49,7 @@ func TestReadIMSI(t *testing.T) {
if resp, err = getIMSI(card); err != nil {
t.Error(err)
}
fmt.Println(resp)
logrus.Debug(resp)
}
func TestReadICCID(t *testing.T) {
var resp string
Expand All @@ -71,11 +67,8 @@ func TestReadICCID(t *testing.T) {
if len(readers) == 0 {
t.Error("please insert smart card")
return
} else if len(readers) == 1 {
reader = readers[0]
} else {
// to do: handle multiple readers choices
t.Error("multiple readers not supported")
reader = readers[0]
}
//
card, err := reader.Connect()
Expand All @@ -86,7 +79,7 @@ func TestReadICCID(t *testing.T) {
if resp, err = getICCID(card); err != nil {
t.Error(err)
}
fmt.Println(resp)
logrus.Debug(resp)
}

func TestReadMSISDN(t *testing.T) {
Expand All @@ -105,11 +98,8 @@ func TestReadMSISDN(t *testing.T) {
if len(readers) == 0 {
t.Error("please insert smart card")
return
} else if len(readers) == 1 {
reader = readers[0]
} else {
// to do: handle multiple readers choices
t.Error("multiple readers not supported")
reader = readers[0]
}
//
card, err := reader.Connect()
Expand All @@ -120,7 +110,7 @@ func TestReadMSISDN(t *testing.T) {
if resp, err = getMSISDN(card); err != nil {
t.Error(err)
}
fmt.Println(resp)
logrus.Debug(resp)
}
func TestAKAVerify(t *testing.T) {
var resp string
Expand All @@ -138,11 +128,8 @@ func TestAKAVerify(t *testing.T) {
if len(readers) == 0 {
t.Error("please insert smart card")
return
} else if len(readers) == 1 {
reader = readers[0]
} else {
// to do: handle multiple readers choices
t.Error("multiple readers not supported")
reader = readers[0]
}
//
card, err := reader.Connect()
Expand All @@ -157,7 +144,7 @@ func TestAKAVerify(t *testing.T) {
} else {
logrus.Info(res, ik, ck, auts)
}
fmt.Println(resp)
logrus.Debug(resp)
}

func TestParseAKA(t *testing.T) {
Expand Down Expand Up @@ -185,11 +172,8 @@ func TestGSMAlg(t *testing.T) {
if len(readers) == 0 {
t.Error("please insert smart card")
return
} else if len(readers) == 1 {
reader = readers[0]
} else {
// to do: handle multiple readers choices
t.Error("multiple readers not supported")
reader = readers[0]
}
//
card, err := reader.Connect()
Expand All @@ -201,7 +185,7 @@ func TestGSMAlg(t *testing.T) {
if sres, kc, err := GSMAlg(card, SCARD_USIM, rand_enb[:]); err != nil {
t.Error(err)
} else {
logrus.Info("XRES: ", hex.EncodeToString(sres), "KC: ", hex.EncodeToString(kc))
logrus.Debug("XRES: ", hex.EncodeToString(sres), "KC: ", hex.EncodeToString(kc))
}
fmt.Println(resp)
logrus.Debug(resp)
}
2 changes: 1 addition & 1 deletion usim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ AUTN : d7.44.51.9b.25.aa.80.00.84.ba.37.b0.f6.73.4d.d1.
KASME : a8.27.57.5e.ea.1a.10.17.3a.a1.bf.ce.4b.0c.21.85.e0.51.ef.bd.91.7f.fe.f5.1f.74.29.61.f9.03.7a.35.
*/
func Test_gen_auth_res_milenage(t *testing.T) {
u, err := InitSoftUSIM(Milenage, "356092040793011", "208930000000001", "8BAF473F2F8FD09487CCCBD7097C6862", "11111111111111111111111111111111", "",true)
u, err := InitSoftUSIM(Milenage, "356092040793011", "208930000000001", "8BAF473F2F8FD09487CCCBD7097C6862", "11111111111111111111111111111111", "", true)
u.compute_opc()
if err != nil {
t.Error(err)
Expand Down

0 comments on commit 09f61ee

Please sign in to comment.