/
security.go
316 lines (282 loc) · 8.52 KB
/
security.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
package test
import (
"crypto/aes"
"crypto/cipher"
"fmt"
"free5gc/lib/nas"
"github.com/aead/cmac"
"reflect"
)
// TS 33501 Annex A.8 Algorithm distinguisher For Knas_int Knas_enc
const (
N_NAS_ENC_ALG uint8 = 0x01
N_NAS_INT_ALG uint8 = 0x02
N_RRC_ENC_ALG uint8 = 0x03
N_RRC_INT_ALG uint8 = 0x04
N_UP_ENC_alg uint8 = 0x05
N_UP_INT_alg uint8 = 0x06
)
// TS 33501 Annex D Algorithm identifier values For Knas_int
const (
ALG_INTEGRITY_128_NIA0 uint8 = 0x00 // NULL
ALG_INTEGRITY_128_NIA1 uint8 = 0x01 // 128-Snow3G
ALG_INTEGRITY_128_NIA2 uint8 = 0x02 // 128-AES
ALG_INTEGRITY_128_NIA3 uint8 = 0x03 // 128-ZUC
)
// TS 33501 Annex D Algorithm identifier values For Knas_enc
const (
ALG_CIPHERING_128_NEA0 uint8 = 0x00 // NULL
ALG_CIPHERING_128_NEA1 uint8 = 0x01 // 128-Snow3G
ALG_CIPHERING_128_NEA2 uint8 = 0x02 // 128-AES
ALG_CIPHERING_128_NEA3 uint8 = 0x03 // 128-ZUC
)
// 1bit
const (
SECURITY_DIRECTION_UPLINK uint8 = 0x00
SECURITY_DIRECTION_DOWNLINK uint8 = 0x01
)
// 5bits
const (
SECURITY_ONLY_ONE_BEARER uint8 = 0x00
SECURITY_BEARER_3GPP uint8 = 0x01
SECURITY_BEARER_NON_3GPP uint8 = 0x02
)
// TS 33501 Annex A.0 Access type distinguisher For Kgnb Kn3iwf
const (
ACCESS_TYPE_3GPP uint8 = 0x01
ACCESS_TYPE_NON_3GPP uint8 = 0x02
)
func NASEncode(ue *RanUeContext, msg *nas.Message) (payload []byte, err error) {
integrityProtected := false
newSecurityContext := false
ciphering := false
if ue == nil {
err = fmt.Errorf("amfUe is nil")
return
}
if msg == nil {
err = fmt.Errorf("Nas Message is empty")
return
}
switch msg.SecurityHeader.SecurityHeaderType {
case nas.SecurityHeaderTypePlainNas:
fmt.Println("NasPdu Security: Plain Nas")
return msg.PlainNasEncode()
case nas.SecurityHeaderTypeIntegrityProtected:
fmt.Println("NasPdu Security: Integrity Protected")
integrityProtected = true
case nas.SecurityHeaderTypeIntegrityProtectedAndCiphered:
fmt.Println("NasPdu Security: Integrity Protected And Ciphered")
integrityProtected = true
ciphering = true
case nas.SecurityHeaderTypeIntegrityProtectedWithNew5gNasSecurityContext:
fmt.Println("NasPdu Security: Integrity Protected With New 5gNasSecurityContext")
integrityProtected = true
newSecurityContext = true
case nas.SecurityHeaderTypeIntegrityProtectedAndCipheredWithNew5gNasSecurityContext:
fmt.Println("NasPdu Security: Integrity Protected And Ciphered WithNew 5gNasSecurityContext")
integrityProtected = true
ciphering = true
newSecurityContext = true
default:
return nil, fmt.Errorf("Security Type[%d] is not be implemented", msg.SecurityHeader.SecurityHeaderType)
}
if newSecurityContext {
ue.ULCount = 0
ue.DLOverflow = 0
ue.DLCountSQN = 0
}
if ue.CipheringAlg == ALG_CIPHERING_128_NEA0 {
ciphering = false
}
if ue.IntegrityAlg == ALG_INTEGRITY_128_NIA0 {
integrityProtected = false
}
if ciphering || integrityProtected {
securityHeader := []byte{msg.SecurityHeader.ProtocolDiscriminator, msg.SecurityHeaderType}
sequenceNumber := uint8(ue.ULCount & 0xff)
payload, err = msg.PlainNasEncode()
if err != nil {
return
}
if ciphering {
// TODO: Support for ue has nas connection in both accessType
if err = NasEncrypt(ue.CipheringAlg, ue.KnasEnc, ue.GetSecurityULCount(), SECURITY_BEARER_3GPP,
SECURITY_DIRECTION_UPLINK, payload); err != nil {
return
}
}
// add sequece number
payload = append([]byte{sequenceNumber}, payload[:]...)
mac32 := make([]byte, 4)
if integrityProtected {
mac32, err = NasMacCalculate(ue.IntegrityAlg, ue.KnasInt, ue.GetSecurityULCount(), SECURITY_BEARER_3GPP, SECURITY_DIRECTION_UPLINK, payload)
if err != nil {
return
}
}
// Add mac value
payload = append(mac32, payload[:]...)
// Add EPD and Security Type
payload = append(securityHeader, payload[:]...)
// Increase UL Count
ue.ULCount = (ue.ULCount + 1) & 0xffffff
} else {
return msg.PlainNasEncode()
}
return
}
func NASDecode(ue *RanUeContext, securityHeaderType uint8, payload []byte) (msg *nas.Message, err error) {
integrityProtected := false
newSecurityContext := false
ciphering := false
if ue == nil {
err = fmt.Errorf("amfUe is nil")
return
}
if payload == nil {
err = fmt.Errorf("Nas payload is empty")
return
}
switch securityHeaderType {
case nas.SecurityHeaderTypePlainNas:
case nas.SecurityHeaderTypeIntegrityProtected:
integrityProtected = true
case nas.SecurityHeaderTypeIntegrityProtectedAndCiphered:
integrityProtected = true
ciphering = true
case nas.SecurityHeaderTypeIntegrityProtectedWithNew5gNasSecurityContext:
integrityProtected = true
newSecurityContext = true
case nas.SecurityHeaderTypeIntegrityProtectedAndCipheredWithNew5gNasSecurityContext:
integrityProtected = true
ciphering = true
newSecurityContext = true
default:
return nil, fmt.Errorf("Security Type[%d] is not be implemented", securityHeaderType)
}
msg = new(nas.Message)
if newSecurityContext {
ue.DLOverflow = 0
ue.DLCountSQN = 0
}
if ue.CipheringAlg == ALG_CIPHERING_128_NEA0 {
ciphering = false
}
if ue.IntegrityAlg == ALG_INTEGRITY_128_NIA0 {
integrityProtected = false
}
if ciphering || integrityProtected {
securityHeader := payload[0:6]
sequenceNumber := payload[6]
receivedMac32 := securityHeader[2:]
// remove security Header except for sequece Number
payload = payload[6:]
// Caculate ul count
if ue.DLCountSQN > sequenceNumber {
ue.DLOverflow++
}
ue.DLCountSQN = sequenceNumber
if integrityProtected {
// ToDo: use real mac calculate
mac32, err := NasMacCalculate(ue.IntegrityAlg, ue.KnasInt, ue.GetSecurityDLCount(), SECURITY_BEARER_3GPP,
SECURITY_DIRECTION_DOWNLINK, payload)
if err != nil {
return nil, err
}
if !reflect.DeepEqual(mac32, receivedMac32) {
return nil, fmt.Errorf("NAS MAC verification failed(0x%x != 0x%x)", mac32, receivedMac32)
}
}
// remove sequece Number
payload = payload[1:]
if ciphering {
// TODO: Support for ue has nas connection in both accessType
if err = NasEncrypt(ue.CipheringAlg, ue.KnasEnc, ue.GetSecurityDLCount(), SECURITY_BEARER_3GPP,
SECURITY_DIRECTION_DOWNLINK, payload); err != nil {
return
}
}
}
err = msg.PlainNasDecode(&payload)
return
}
func NasEncrypt(AlgoID uint8, KnasEnc []byte, Count []byte, Bearer uint8, Direction uint8, plainText []byte) error {
if len(KnasEnc) != 16 {
return fmt.Errorf("Size of KnasEnc[%d] != 16 bytes)", len(KnasEnc))
}
if Bearer > 0x1f {
return fmt.Errorf("Bearer is beyond 5 bits")
}
if Direction > 1 {
return fmt.Errorf("Direction is beyond 1 bits")
}
if plainText == nil {
return fmt.Errorf("Nas Payload is nil")
}
switch AlgoID {
case ALG_CIPHERING_128_NEA1:
return fmt.Errorf("NEA3 not implement yet.")
case ALG_CIPHERING_128_NEA2:
// Couter[0..32] | BEARER[0..4] | DIRECTION[0] | 0^26 | 0^64
CouterBlk := make([]byte, 16)
//First 32 bits are count
copy(CouterBlk, Count)
//Put Bearer and direction together
CouterBlk[4] = (Bearer << 3) | (Direction << 2)
block, err := aes.NewCipher(KnasEnc)
if err != nil {
return err
}
ciphertext := make([]byte, len(plainText))
stream := cipher.NewCTR(block, CouterBlk)
stream.XORKeyStream(ciphertext, plainText)
// override plainText with cipherText
copy(plainText, ciphertext)
return nil
case ALG_CIPHERING_128_NEA3:
return fmt.Errorf("NEA3 not implement yet.")
default:
return fmt.Errorf("Unknown Algorithm Identity[%d]", AlgoID)
}
}
func NasMacCalculate(AlgoID uint8, KnasInt []byte, Count []byte, Bearer uint8, Direction uint8, msg []byte) ([]byte, error) {
if len(KnasInt) != 16 {
return nil, fmt.Errorf("Size of KnasEnc[%d] != 16 bytes)", len(KnasInt))
}
if Bearer > 0x1f {
return nil, fmt.Errorf("Bearer is beyond 5 bits")
}
if Direction > 1 {
return nil, fmt.Errorf("Direction is beyond 1 bits")
}
if msg == nil {
return nil, fmt.Errorf("Nas Payload is nil")
}
switch AlgoID {
case ALG_INTEGRITY_128_NIA1:
return nil, fmt.Errorf("NEA3 not implement yet.")
case ALG_INTEGRITY_128_NIA2:
// Couter[0..32] | BEARER[0..4] | DIRECTION[0] | 0^26
m := make([]byte, len(msg)+8)
//First 32 bits are count
copy(m, Count)
//Put Bearer and direction together
m[4] = (Bearer << 3) | (Direction << 2)
block, err := aes.NewCipher(KnasInt)
if err != nil {
return nil, err
}
copy(m[8:], msg)
cmac, err := cmac.Sum(m, block, 16)
if err != nil {
return nil, err
}
// only get the most significant 32 bits to be mac value
return cmac[:4], nil
case ALG_INTEGRITY_128_NIA3:
return nil, fmt.Errorf("NEA3 not implement yet.")
default:
return nil, fmt.Errorf("Unknown Algorithm Identity[%d]", AlgoID)
}
}