-
Notifications
You must be signed in to change notification settings - Fork 37
/
NAS_MobileIdentity5GS.go
335 lines (285 loc) · 8 KB
/
NAS_MobileIdentity5GS.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package nasType
import (
"encoding/hex"
"errors"
"fmt"
"math/bits"
"strconv"
"strings"
)
// MobileIdentity5GS 9.11.3.4
// MobileIdentity5GSContents Row, sBit, len = [0, 0], 8 , INF
type MobileIdentity5GS struct {
Iei uint8
Len uint16
Buffer []uint8
}
// const
const (
noIdentity uint8 = iota
suci
fiveGGUTI
imei
fiveGSTMSI
imeisv
)
// const
const (
high5BitMask uint8 = 0x07
low4BitMask uint8 = 0xf0
high4BitMask uint8 = 0x0f
Bit4 uint8 = 0x08
)
func NewMobileIdentity5GS(iei uint8) (mobileIdentity5GS *MobileIdentity5GS) {
mobileIdentity5GS = &MobileIdentity5GS{}
mobileIdentity5GS.SetIei(iei)
return mobileIdentity5GS
}
// MobileIdentity5GS 9.11.3.4
// Iei Row, sBit, len = [], 8, 8
func (a *MobileIdentity5GS) GetIei() (iei uint8) {
return a.Iei
}
// MobileIdentity5GS 9.11.3.4
// Iei Row, sBit, len = [], 8, 8
func (a *MobileIdentity5GS) SetIei(iei uint8) {
a.Iei = iei
}
// MobileIdentity5GS 9.11.3.4
// Len Row, sBit, len = [], 8, 16
func (a *MobileIdentity5GS) GetLen() (len uint16) {
return a.Len
}
// MobileIdentity5GS 9.11.3.4
// Len Row, sBit, len = [], 8, 16
func (a *MobileIdentity5GS) SetLen(len uint16) {
a.Len = len
a.Buffer = make([]uint8, a.Len)
}
// MobileIdentity5GS 9.11.3.4
// MobileIdentity5GSContents Row, sBit, len = [0, 0], 8 , INF
func (a *MobileIdentity5GS) GetMobileIdentity5GSContents() (mobileIdentity5GSContents []uint8) {
mobileIdentity5GSContents = make([]uint8, len(a.Buffer))
copy(mobileIdentity5GSContents, a.Buffer)
return mobileIdentity5GSContents
}
// MobileIdentity5GS 9.11.3.4
// MobileIdentity5GSContents Row, sBit, len = [0, 0], 8 , INF
func (a *MobileIdentity5GS) SetMobileIdentity5GSContents(mobileIdentity5GSContents []uint8) {
copy(a.Buffer, mobileIdentity5GSContents)
}
// TS 24.501 9.11.3.3 Table 9.11.3.3.1 identity type information element
// All other values are unused and shall be interpreted
// as "SUCI", if received by the UE
func (a *MobileIdentity5GS) GetTypeOfIdentity() (string, error) {
idType := a.Buffer[0] & high5BitMask
switch idType {
case noIdentity:
return "", errors.New("no identity")
case suci:
return "SUCI", nil
case fiveGGUTI:
return "5G-GUTI", nil
case imei:
return "IMEI", nil
case fiveGSTMSI:
return "5G-S-TMSI", nil
case imeisv:
return "IMEISV", nil
default:
return "SUCI", nil
}
}
// GetMobileIdentity
func (a *MobileIdentity5GS) GetMobileIdentity() (string, string, error) {
idType, err := a.GetTypeOfIdentity()
if err != nil {
return "", idType, err
}
switch idType {
case "SUCI":
return a.GetSUCI(), idType, nil
case "5G-GUTI":
return a.Get5GGUTI(), idType, nil
case "IMEI":
return a.GetIMEI(), idType, nil
case "5G-S-TMSI":
return a.Get5GTMSI(), idType, nil
case "IMEISV":
return a.GetIMEISV(), idType, nil
default:
return a.GetSUCI(), "SUCI", nil
}
}
// GetSUCI
func (a *MobileIdentity5GS) GetSUCI() string {
idType, err := a.GetTypeOfIdentity()
if idType == "SUCI" && err == nil {
var schemeOutput string
// Encode buf to SUCI in supi format "IMSI"
supiFormat := (a.Buffer[0] & low4BitMask) >> 4
if supiFormat == suci {
return naiToString(a.Buffer)
}
mcc := a.GetMCC()
mnc := a.GetMNC()
var routingIndBytes []byte
routingIndBytes = append(routingIndBytes, bits.RotateLeft8(a.Buffer[4], 4))
routingIndBytes = append(routingIndBytes, bits.RotateLeft8(a.Buffer[5], 4))
routingInd := hex.EncodeToString(routingIndBytes)
if idx := strings.Index(routingInd, "f"); idx != -1 {
routingInd = routingInd[0:idx]
}
// Protection Scheme
protectionScheme := fmt.Sprintf("%x", a.Buffer[6]) // convert byte to hex string without leading 0s
// Home Network Public Key Indentifier
homeNetworkPublicKeyIdentifier := fmt.Sprintf("%d", a.Buffer[7])
// Scheme output
// TS 24.501 9.11.3.4
if protectionScheme == "0" {
// MSIN
var msinBytes []byte
for i := 8; i < len(a.Buffer); i++ {
msinBytes = append(msinBytes, bits.RotateLeft8(a.Buffer[i], 4))
}
schemeOutput = hex.EncodeToString(msinBytes)
if schemeOutput[len(schemeOutput)-1] == 'f' {
schemeOutput = schemeOutput[:len(schemeOutput)-1]
}
} else {
schemeOutput = hex.EncodeToString(a.Buffer[8:])
}
// "suci-0-208-93-0-0-0-00007487"
suci := strings.Join([]string{
"suci", "0", mcc, mnc, routingInd, protectionScheme, homeNetworkPublicKeyIdentifier,
schemeOutput,
}, "-")
return suci
}
return ""
}
// GetPlmnID
func (a *MobileIdentity5GS) GetPlmnID() string {
plmnId := a.GetMCC() + a.GetMNC()
return plmnId
}
// GetMCC
func (a *MobileIdentity5GS) GetMCC() string {
mccDigit3 := (a.Buffer[2] & high4BitMask)
tmpBytes := []byte{bits.RotateLeft8(a.Buffer[1], 4), (mccDigit3 << 4)}
mcc := hex.EncodeToString(tmpBytes)
mcc = mcc[:3] // remove rear 0
return mcc
}
// GetMNC
func (a *MobileIdentity5GS) GetMNC() string {
mncDigit3 := (a.Buffer[2] & low4BitMask) >> 4
tmpBytes := []byte{bits.RotateLeft8(a.Buffer[3], 4), mncDigit3 << 4}
mnc := hex.EncodeToString(tmpBytes)
if mnc[2] == 'f' {
mnc = mnc[:2] // mnc is 2 digit -> remove 'f'
} else {
mnc = mnc[:3] // mnc is 3 digit -> remove rear 0
}
return mnc
}
// Get5GGUTI
func (a *MobileIdentity5GS) Get5GGUTI() string {
return a.GetMCC() + a.GetMNC() + a.GetAmfID() + a.Get5GTMSI()
}
// GetAmfID
func (a *MobileIdentity5GS) GetAmfID() string {
return hex.EncodeToString(a.Buffer[4:7])
}
// GetAmfRegionID
func (a *MobileIdentity5GS) GetAmfRegionID() string {
return hex.EncodeToString(a.Buffer[4:5])
}
// GetAmfSetID
func (a *MobileIdentity5GS) GetAmfSetID() string {
var amfSetStartPoint int
idType, err := a.GetTypeOfIdentity()
if idType == "5G-GUTI" && err == nil {
amfSetStartPoint = 5
}
if idType == "5G-S-TMSI" && err == nil {
amfSetStartPoint = 1
}
amfSetID := (uint16(a.Buffer[amfSetStartPoint])<<2 + uint16((a.Buffer[amfSetStartPoint+1])&GetBitMask(8, 2))>>6)
amfSetID_string := strconv.FormatUint(uint64(amfSetID), 10)
return amfSetID_string
}
// GetAmfPointer
func (a *MobileIdentity5GS) GetAmfPointer() string {
var amfPointerStartPoint int
idType, err := a.GetTypeOfIdentity()
if idType == "5G-GUTI" && err == nil {
amfPointerStartPoint = 6
}
if idType == "5G-S-TMSI" && err == nil {
amfPointerStartPoint = 2
}
AMFPointer := (a.Buffer[amfPointerStartPoint]) & GetBitMask(6, 0)
AMFPointer_string := strconv.FormatUint(uint64(AMFPointer), 10)
return AMFPointer_string
}
// Get5GTMSI
func (a *MobileIdentity5GS) Get5GTMSI() string {
idType, err := a.GetTypeOfIdentity()
if idType == "5G-GUTI" && err == nil {
tmsi5G_string := hex.EncodeToString(a.Buffer[7:])
return tmsi5G_string
} else if idType == "5G-S-TMSI" && err == nil {
tmsi5G := a.Buffer[3:7]
tmsi5G_string := hex.EncodeToString(tmsi5G[0:])
return tmsi5G_string
} else {
return ""
}
}
// GetIMEI
func (a *MobileIdentity5GS) GetIMEI() string {
idType, err := a.GetTypeOfIdentity()
if idType == "IMEI" && err == nil {
return "imei-" + peiToString(a.Buffer)
}
return ""
}
// GetIMEISV
func (a *MobileIdentity5GS) GetIMEISV() string {
idType, err := a.GetTypeOfIdentity()
if idType == "IMEISV" && err == nil {
return "imeisv-" + peiToString(a.Buffer)
}
return ""
}
func (a *MobileIdentity5GS) Get5GSTMSI() (tMSI5GS string, mobileIdType string, err error) {
partOfAmfId := hex.EncodeToString(a.Buffer[1:3])
tmsi5g := a.Get5GTMSI()
tMSI5GS = partOfAmfId + tmsi5g
return tMSI5GS, "5G-S-TMSI", nil
}
func naiToString(buf []byte) string {
prefix := "nai"
naiBytes := buf[1:]
naiStr := hex.EncodeToString(naiBytes)
nai := strings.Join([]string{prefix, "1", naiStr}, "-")
return nai
}
func peiToString(buf []byte) string {
oddIndication := (buf[0] & Bit4) >> 3
digit1 := (buf[0] & low4BitMask)
tmpBytes := []byte{digit1}
for _, octet := range buf[1:] {
digitP := octet & high4BitMask
digitP1 := octet & low4BitMask
tmpBytes[len(tmpBytes)-1] += digitP
tmpBytes = append(tmpBytes, digitP1)
}
digitStr := hex.EncodeToString(tmpBytes)
digitStr = digitStr[:len(digitStr)-1] // remove the last digit
if oddIndication == 0 { // even digits
digitStr = digitStr[:len(digitStr)-1] // remove the last digit
}
return digitStr
}