-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdiameter_client.py
368 lines (354 loc) · 13.5 KB
/
diameter_client.py
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
##################################################################
# Copyright (c) 2012, Sergej Srepfler <[email protected]>
# February 2012 -
# Version 0.3, Last change on Oct 30, 2012
# This software is distributed under the terms of BSD license.
##################################################################
# EAP-AKA/AKA' client
from libDiameter import *
import eap
import datetime
import time
def create_CER():
# Let's build CER
CER_avps=[]
CER_avps.append(encodeAVP("Origin-Host", ORIGIN_HOST))
CER_avps.append(encodeAVP("Origin-Realm", ORIGIN_REALM))
CER_avps.append(encodeAVP("Vendor-Id", dictVENDORid2code('TGPP')))
CER_avps.append(encodeAVP("Origin-State-Id", ORIGIN_ID))
CER_avps.append(encodeAVP("Supported-Vendor-Id", dictVENDORid2code('TGPP')))
CER_avps.append(encodeAVP("Acct-Application-Id", APPLICATION_ID))
# Create message header (empty)
CER=HDRItem()
# Set command code
CER.cmd=dictCOMMANDname2code("Capabilities-Exchange")
# Set Hop-by-Hop and End-to-End
initializeHops(CER)
# Add AVPs to header and calculate remaining fields
msg=createReq(CER,CER_avps)
# msg now contains CER Request as hex string
return msg
def Payload_Identity():
# Let's build EAP-Payload Identity AVP
# Create EAP-Payload (empty)
EAP=eap.EAPItem()
# Set command code
# Remember - Requests normally starts from AAA-> UE, so
# even when skipped, identity is actually an response
EAP.cmd=eap.EAP_CODE_RESPONSE
# Set id
EAP.id=1
# Set type
EAP.type=eap.EAP_TYPE_IDENTITY
# Add Identity
EAP.msg=eap.addEAPIdentity(IDENTITY)
Payload=eap.encode_EAP(EAP)
return Payload
def Payload_AKA_Identity(ID,ETYPE):
# Let's build EAP-Payload with AT_IDENTITY AVP
# Create EAP-Payload (empty)
EAP=eap.EAPItem()
# Set command code
# Remember - Requests normally starts from AAA-> UE, so
# even when skipped, identity is actually an response
EAP.cmd=eap.EAP_CODE_RESPONSE
# Set id
EAP.id=ID
# Set type
EAP.type=ETYPE
# Set sub-type
EAP.stype=eap.dictEAPSUBname2type("AKA-Identity")
EAP.avps.append(("AT_IDENTITY",IDENTITY))
Payload=eap.encode_EAP(EAP)
# Payload now contains EAP-Payload AVP
return Payload
def Payload_Challenge_Response(ID,RAND,ETYPE):
# Let's build EAP-Payload Challenge-Response AVP
# Create EAP-Payload (empty)
EAP=eap.EAPItem()
# Set command code
EAP.cmd=eap.EAP_CODE_RESPONSE
# Set id
EAP.id=ID
# Set type
EAP.type=ETYPE
# Set sub-type
EAP.stype=eap.dictEAPSUBname2type("AKA-Challenge")
# RAND is copied from Challenge
# These values can be calculated or entered manually
#XRES,CK,IK,AK,AKS=eap.aka_calc_milenage(OP,Ki,RAND)
# Or copy from MAA
# IK=Identity-Key
# CK=Confidentiality-Key
# XRES=SIP-Authorization
IK = "2d346b8c456223bc7519823a0abc94fd";
CK = "07fc3189172095ddce5b4ba2bfb70f7f";
XRES = "e818fbf691ae3b97";
if EAP.type==eap.EAP_TYPE_AKAPRIME:
# For AKA'
KENCR,KAUT,MSK,EMSK,KRE=eap.akap_calc_keys(IDENTITY,CK,IK)
else:
# For AKA
KENCR,KAUT,MSK,EMSK,MK=eap.aka_calc_keys(IDENTITY,CK,IK)
# Add AT_RES
EAP.avps.append(("AT_RES",XRES))
# Add AT_MAC as last
eap.addMAC(EAP,KENCR,'')
# Do not add any AVPs after adding MAC
Payload=eap.encode_EAP(EAP)
# Payload now contains EAP-Payload AVP
return Payload
def create_Identity_Request():
# Let's build Request+Identity-Payload
REQ_avps=[]
REQ_avps.append(encodeAVP('Session-Id', SESSION_ID))
REQ_avps.append(encodeAVP("Origin-Host", ORIGIN_HOST))
REQ_avps.append(encodeAVP("Origin-Realm", ORIGIN_REALM))
REQ_avps.append(encodeAVP("Destination-Realm", DEST_REALM))
REQ_avps.append(encodeAVP("Origin-State-Id", ORIGIN_ID))
REQ_avps.append(encodeAVP('Auth-Application-Id', APPLICATION_ID))
REQ_avps.append(encodeAVP('Auth-Request-Type', 3))
Payload=Payload_Identity()
REQ_avps.append(encodeAVP("EAP-Payload",Payload.decode("hex")))
REQ_avps.append(encodeAVP('Auth-Session-State', 0))
REQ_avps.append(encodeAVP("User-Name", IDENTITY))
REQ_avps.append(encodeAVP("Calling-Station-Id", "313171"))
REQ_avps.append(encodeAVP("RAT-Type", 0))
REQ_avps.append(encodeAVP("ANID", "HRPD"))
# Create message header (empty)
REQ=HDRItem()
# Set command code
REQ.cmd=dictCOMMANDname2code("Diameter-EAP")
# Set Application-Id
REQ.appId=APPLICATION_ID
# Set Hop-by-Hop and End-to-End
initializeHops(REQ)
# Add AVPs to header and calculate remaining fields
msg=createReq(REQ,REQ_avps)
# msg now contains CER Request as hex string
return msg
def create_Identity_Response(ID,ETYPE):
# Let's build Response+EAP-Payload with AT_IDENTITY
REQ_avps=[]
REQ_avps.append(encodeAVP('Session-Id', SESSION_ID))
REQ_avps.append(encodeAVP("Origin-Host", ORIGIN_HOST))
REQ_avps.append(encodeAVP("Origin-Realm", ORIGIN_REALM))
REQ_avps.append(encodeAVP("Destination-Realm", DEST_REALM))
REQ_avps.append(encodeAVP("Origin-State-Id", ORIGIN_ID))
REQ_avps.append(encodeAVP('Auth-Application-Id', APPLICATION_ID))
REQ_avps.append(encodeAVP('Auth-Request-Type', 3))
Payload=Payload_AKA_Identity(ID,ETYPE)
REQ_avps.append(encodeAVP("EAP-Payload",Payload.decode("hex")))
REQ_avps.append(encodeAVP('Auth-Session-State', 0))
REQ_avps.append(encodeAVP("User-Name", IDENTITY))
REQ_avps.append(encodeAVP("Calling-Station-Id", "313171"))
REQ_avps.append(encodeAVP("RAT-Type", 0))
REQ_avps.append(encodeAVP("ANID", "HRPD"))
# Create message header (empty)
REQ=HDRItem()
# Set command code
REQ.cmd=dictCOMMANDname2code("Diameter-EAP")
# Set Application-Id
REQ.appId=APPLICATION_ID
# Set Hop-by-Hop and End-to-End
initializeHops(REQ)
# Add AVPs to header and calculate remaining fields
msg=createReq(REQ,REQ_avps)
# msg now contains CER Request as hex string
return msg
def create_Challenge_Response(ID,RAND,ETYPE):
# Let's build Response+Response-Payload
RES_avps=[]
RES_avps.append(encodeAVP("Session-Id", SESSION_ID))
RES_avps.append(encodeAVP("Origin-Host", ORIGIN_HOST))
RES_avps.append(encodeAVP("Origin-Realm", ORIGIN_REALM))
RES_avps.append(encodeAVP("Destination-Realm", DEST_REALM))
RES_avps.append(encodeAVP('Auth-Application-Id', APPLICATION_ID))
RES_avps.append(encodeAVP('Auth-Request-Type', 3))
Payload=Payload_Challenge_Response(ID,RAND,ETYPE)
RES_avps.append(encodeAVP("EAP-Payload",Payload.decode("hex")))
RES_avps.append(encodeAVP('Auth-Session-State', 0))
RES_avps.append(encodeAVP("User-Name", IDENTITY))
RES_avps.append(encodeAVP("RAT-Type", 0))
RES_avps.append(encodeAVP("ANID", "HRPD"))
RES_avps.append(encodeAVP("Service-Selection", "a1"))
# Create message header (empty)
RES=HDRItem()
# Set Proxyable flag
setFlags(RES,DIAMETER_HDR_PROXIABLE)
# Set command code
RES.cmd=dictCOMMANDname2code("Diameter-EAP")
# Set Application-Id
RES.appId=APPLICATION_ID
# Set Hop-by-Hop and End-to-End
initializeHops(RES)
# Add AVPs to header and calculate remaining fields
msg=createReq(RES,RES_avps)
# msg now contains Response as hex string
return msg
def create_Disconnect():
# Let's build Session-Termination
STR_avps=[]
STR_avps.append(encodeAVP("Session-Id", SESSION_ID))
STR_avps.append(encodeAVP("Origin-Host", ORIGIN_HOST))
STR_avps.append(encodeAVP("Origin-Realm", ORIGIN_REALM))
STR_avps.append(encodeAVP("Destination-Realm", DEST_REALM))
STR_avps.append(encodeAVP("Origin-State-Id", ORIGIN_ID))
STR_avps.append(encodeAVP("Auth-Application-Id", APPLICATION_ID))
STR_avps.append(encodeAVP("User-Name", IDENTITY))
# DIAMETER_LOGOUT=1
STR_avps.append(encodeAVP("Termination-Cause", 1))
# Create message header (empty)
STR=HDRItem()
# Set command code
STR.cmd=dictCOMMANDname2code("Session-Termination")
# Set Application-Id
STR.appId=APPLICATION_ID
# Set Hop-by-Hop and End-to-End
initializeHops(STR)
# Add AVPs to header and calculate remaining fields
msg=createReq(STR,STR_avps)
# msg now contains Session-Termination Request as hex string
return msg
def create_Session_Id():
#The Session-Id MUST be globally and eternally unique
#<DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]
now=datetime.datetime.now()
ret=ORIGIN_HOST+";"
ret=ret+str(now.year)[2:4]+"%02d"%now.month+"%02d"%now.day
ret=ret+"%02d"%now.hour+"%02d"%now.minute+";"
ret=ret+"%02d"%now.second+str(now.microsecond)+";"
ret=ret+IDENTITY[2:16]
return ret
def dump_Payload(avps):
for avp in avps:
(name,value)=decodeAVP(avp)
if name=='EAP-Payload':
print 'Response:',name,'=',value.encode('hex')
E=eap.decode_EAP(value.encode('hex'))
for eavp in E.avps:
(code,data)=eavp
print code,'=',data
else:
print 'Response:',name,'=',value
if __name__ == "__main__":
#logging.basicConfig(level=logging.DEBUG)
LoadDictionary("dictDiameter.xml")
eap.LoadEAPDictionary("dictEAP.xml")
HOST="192.168.2.63"
PORT=3868
ORIGIN_HOST="sta.hsgw.com"
ORIGIN_REALM="hsgw.com"
now=datetime.datetime.now()
ORIGIN_ID=str(now.microsecond)
# 3GPP SWx=16777265 STa=16777250 S6b=16777272
APPLICATION_ID=16777250
#First digit is 0 for AKA, 6 for AKA'
IDENTITY="[email protected]"
#ETYPE=eap.EAP_TYPE_AKA
ETYPE=eap.EAP_TYPE_AKAPRIME
OP="cdc202d5123e20f62b6d676ac72cb318"
Ki="77777777777777777777777777777777"
# Let's assume that my Diameter messages will fit into 4k
MSG_SIZE=4096
###########################################################
# Create unique session ID
SESSION_ID=create_Session_Id()
# Connect to server
Conn=Connect(HOST,PORT)
###########################################################
# Let's build CER
msg=create_CER()
# msg now contains CER Request as hex string
logging.debug("+"*30)
# send data
Conn.send(msg.decode("hex"))
# Receive response
received = Conn.recv(MSG_SIZE)
# split header and AVPs
CEA=HDRItem()
stripHdr(CEA,received.encode("hex"))
# From CEA we needed Destination-Host and Destination-Realm
Capabilities_avps=splitMsgAVPs(CEA.msg)
DEST_HOST=findAVP("Origin-Host",Capabilities_avps)
DEST_REALM=findAVP("Origin-Realm",Capabilities_avps)
###########################################################
# Create Identity Payload
msg=create_Identity_Request()
# msg now contains EAP Request+Identity Payload as hex string
logging.debug("+"*30)
# send data
Conn.send(msg.decode("hex"))
# Receive response
received = Conn.recv(MSG_SIZE)
# Process response
EAPAnyId=HDRItem()
stripHdr(EAPAnyId,received.encode("hex"))
Identity_avps=splitMsgAVPs(EAPAnyId.msg)
Identity_Payload=findAVP("EAP-Payload",Identity_avps)
# Display response for better undestanding
dump_Payload(Identity_avps)
E=eap.decode_EAP(Identity_Payload.encode('hex'))
###########################################################
# If Identity round is not skipped, we need to send AT_IDENTITY
# Create Identity Payload
#msg=create_Identity_Response(E.id,ETYPE)
# msg now contains EAP Request+Identity Payload as hex string
#logging.debug("+"*30)
# send data
#Conn.send(msg.decode("hex"))
# Receive response
#received = Conn.recv(MSG_SIZE)
###########################################################
# Process Challenge
# split header and AVPs
EAPChallenge=HDRItem()
stripHdr(EAPChallenge,received.encode("hex"))
# If you do not want to process full msg, you can stop here without any harm
# We need Payload from EAP-Challenge
Challenge_avps=splitMsgAVPs(EAPChallenge.msg)
# Display response for better undestanding
dump_Payload(Challenge_avps)
DEST_HOST=findAVP("Origin-Host",Challenge_avps)
Challenge_Payload=findAVP("EAP-Payload",Challenge_avps)
print 'DEBUG:',Challenge_Payload.encode('hex')
if Challenge_Payload<>ERROR:
# We need AT_RAND to create response
E=eap.decode_EAP(Challenge_Payload.encode('hex'))
RAND=findAVP("AT_RAND",E.avps)
###########################################################
msg=create_Challenge_Response(E.id,RAND,ETYPE)
# msg now contains EAP Response as hex string
logging.debug("+"*30)
# send data
Conn.send(msg.decode("hex"))
# Receive response
received = Conn.recv(MSG_SIZE)
# split header and AVPs
EAPOK=HDRItem()
stripHdr(EAPOK,received.encode("hex"))
# No decoding is needed.
# Normally - this is the end.
###########################################################
time.sleep(8)
# But to clean things up, let's disconnect
msg=create_Disconnect()
# msg now contains Disconnect Request as hex string
logging.debug("-"*30)
# send data
Conn.send(msg.decode("hex"))
# Receive response
received = Conn.recv(MSG_SIZE)
# split header and AVPs
DIS=HDRItem()
stripHdr(DIS,received.encode("hex"))
# No decoding is needed
###########################################################
# And close the connection
Conn.close()
######################################################
# History
# 0.2.6 - Apr 27, 2012 - First full-working version
# 0.2.7 - May 25, 2012 - Code cleanup. id matching improoved
# 0.3 - Oct 30, 2012 - lib renamed, eap params fix