This repository has been archived by the owner on Apr 27, 2020. It is now read-only.
/
initContext.go
270 lines (228 loc) · 7.24 KB
/
initContext.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
package n3iwf_util
import (
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net"
"strings"
"github.com/sirupsen/logrus"
"free5gc/lib/path_util"
"free5gc/src/n3iwf/factory"
"free5gc/src/n3iwf/logger"
"free5gc/src/n3iwf/n3iwf_context"
)
var contextLog *logrus.Entry
func init() {
contextLog = logger.ContextLog
}
func InitN3IWFContext() bool {
var ok bool
if factory.N3iwfConfig.Configuration == nil {
contextLog.Error("No N3IWF configuration found")
return false
}
n3iwfContext := n3iwf_context.N3IWFSelf()
n3iwfContext.NFInfo = factory.N3iwfConfig.Configuration.N3IWFInfo
if ok = formatSupportedTAList(&n3iwfContext.NFInfo); !ok {
return false
}
// IKE bind address
if factory.N3iwfConfig.Configuration.IKEBindAddr == "" {
contextLog.Error("IKE bind address is empty")
return false
} else {
n3iwfContext.IKEBindAddress = factory.N3iwfConfig.Configuration.IKEBindAddr
}
// IPSec gateway address
if factory.N3iwfConfig.Configuration.IPSecGatewayAddr == "" {
contextLog.Error("IPSec interface address is empty")
return false
} else {
n3iwfContext.IPSecGatewayAddress = factory.N3iwfConfig.Configuration.IPSecGatewayAddr
}
// GTP bind address
if factory.N3iwfConfig.Configuration.GTPBindAddr == "" {
contextLog.Error("GTP bind address is empty")
return false
} else {
n3iwfContext.GTPBindAddress = factory.N3iwfConfig.Configuration.GTPBindAddr
}
// TCP port
if factory.N3iwfConfig.Configuration.TCPPort == 0 {
contextLog.Error("TCP port is not defined")
return false
} else {
n3iwfContext.TCPPort = factory.N3iwfConfig.Configuration.TCPPort
}
// FQDN
if factory.N3iwfConfig.Configuration.FQDN == "" {
contextLog.Error("FQDN is empty")
return false
} else {
n3iwfContext.FQDN = factory.N3iwfConfig.Configuration.FQDN
}
// Private key
{
var keyPath string
if factory.N3iwfConfig.Configuration.PrivateKey == "" {
contextLog.Warn("No private key file path specified, load default key file...")
keyPath = path_util.Gofree5gcPath("free5gc/support/TLS/n3iwf.key")
} else {
keyPath = factory.N3iwfConfig.Configuration.PrivateKey
}
content, err := ioutil.ReadFile(keyPath)
if err != nil {
contextLog.Errorf("Cannot read private key data from file: %+v", err)
return false
}
block, _ := pem.Decode(content)
if block == nil {
contextLog.Error("Parse pem failed")
return false
}
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
contextLog.Warnf("Parse PKCS8 private key failed: %+v", err)
contextLog.Info("Parse using PKCS1...")
key, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
contextLog.Errorf("Parse PKCS1 pricate key failed: %+v", err)
return false
}
}
rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
contextLog.Error("Private key is not an rsa private key")
return false
}
n3iwfContext.N3IWFPrivateKey = rsaKey
}
// Certificate authority
{
var keyPath string
if factory.N3iwfConfig.Configuration.CertificateAuthority == "" {
contextLog.Warn("No certificate authority file path specified, load default CA certificate...")
keyPath = path_util.Gofree5gcPath("free5gc/support/TLS/n3iwf.pem")
} else {
keyPath = factory.N3iwfConfig.Configuration.CertificateAuthority
}
// Read .pem
content, err := ioutil.ReadFile(keyPath)
if err != nil {
contextLog.Errorf("Cannot read certificate authority data from file: %+v", err)
return false
}
// Decode pem
block, _ := pem.Decode(content)
if block == nil {
contextLog.Error("Parse pem failed")
return false
}
// Parse DER-encoded x509 certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
contextLog.Errorf("Parse certificate authority failed: %+v", err)
return false
}
// Get sha1 hash of subject public key info
sha1Hash := sha1.New()
if _, err := sha1Hash.Write(cert.RawSubjectPublicKeyInfo); err != nil {
contextLog.Errorf("Hash function writing failed: %+v", err)
return false
}
n3iwfContext.CertificateAuthority = sha1Hash.Sum(nil)
}
// Certificate
{
var keyPath string
if factory.N3iwfConfig.Configuration.Certificate == "" {
contextLog.Warn("No certificate file path specified, load default certificate...")
keyPath = path_util.Gofree5gcPath("free5gc/support/TLS/n3iwf.pem")
} else {
keyPath = factory.N3iwfConfig.Configuration.Certificate
}
// Read .pem
content, err := ioutil.ReadFile(keyPath)
if err != nil {
contextLog.Errorf("Cannot read certificate data from file: %+v", err)
return false
}
// Decode pem
block, _ := pem.Decode(content)
if block == nil {
contextLog.Error("Parse pem failed")
return false
}
n3iwfContext.N3IWFCertificate = block.Bytes
}
// UE IP address range
if factory.N3iwfConfig.Configuration.UEIPAddressRange == "" {
contextLog.Error("UE IP address range is empty")
return false
} else {
_, ueIPRange, err := net.ParseCIDR(factory.N3iwfConfig.Configuration.UEIPAddressRange)
if err != nil {
contextLog.Errorf("Parse CIDR failed: %+v", err)
return false
}
n3iwfContext.Subnet = ueIPRange
}
if factory.N3iwfConfig.Configuration.InterfaceMark == 0 {
contextLog.Warn("IPSec interface mark is not defined, set to default value 7")
n3iwfContext.Mark = 7
} else {
n3iwfContext.Mark = factory.N3iwfConfig.Configuration.InterfaceMark
}
return true
}
func formatSupportedTAList(info *n3iwf_context.N3IWFNFInfo) bool {
for taListIndex := range info.SupportedTAList {
supportedTAItem := &info.SupportedTAList[taListIndex]
// Checking TAC
if supportedTAItem.TAC == "" {
contextLog.Error("TAC is mandatory.")
return false
}
if len(supportedTAItem.TAC) < 6 {
contextLog.Trace("Detect configuration TAC length < 6")
supportedTAItem.TAC = strings.Repeat("0", 6-len(supportedTAItem.TAC)) + supportedTAItem.TAC
contextLog.Tracef("Changed to %s", supportedTAItem.TAC)
} else if len(supportedTAItem.TAC) > 6 {
contextLog.Error("Detect configuration TAC length > 6")
return false
}
// Checking SST and SD
for plmnListIndex := range supportedTAItem.BroadcastPLMNList {
broadcastPLMNItem := &supportedTAItem.BroadcastPLMNList[plmnListIndex]
for sliceListIndex := range broadcastPLMNItem.TAISliceSupportList {
sliceSupportItem := &broadcastPLMNItem.TAISliceSupportList[sliceListIndex]
// SST
if sliceSupportItem.SNSSAI.SST == "" {
contextLog.Error("SST is mandatory.")
}
if len(sliceSupportItem.SNSSAI.SST) < 2 {
contextLog.Trace("Detect configuration SST length < 2")
sliceSupportItem.SNSSAI.SST = "0" + sliceSupportItem.SNSSAI.SST
contextLog.Tracef("Change to %s", sliceSupportItem.SNSSAI.SST)
} else if len(sliceSupportItem.SNSSAI.SST) > 2 {
contextLog.Error("Detect configuration SST length > 2")
return false
}
// SD
if sliceSupportItem.SNSSAI.SD != "" {
if len(sliceSupportItem.SNSSAI.SD) < 6 {
contextLog.Trace("Detect configuration SD length < 6")
sliceSupportItem.SNSSAI.SD = strings.Repeat("0", 6-len(sliceSupportItem.SNSSAI.SD)) + sliceSupportItem.SNSSAI.SD
contextLog.Tracef("Change to %s", sliceSupportItem.SNSSAI.SD)
} else if len(sliceSupportItem.SNSSAI.SD) > 6 {
contextLog.Error("Detect configuration SD length > 6")
return false
}
}
}
}
}
return true
}