blob: b3f58347eacca83cdb2e94d863bc4bca6a08070f [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A R Karthick76a497a2017-04-12 10:59:39 -070017#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070018# Copyright 2016-present Ciena Corporation
19#
20# Licensed under the Apache License, Version 2.0 (the "License");
21# you may not use this file except in compliance with the License.
22# You may obtain a copy of the License at
A R Karthick76a497a2017-04-12 10:59:39 -070023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick76a497a2017-04-12 10:59:39 -070025#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070026# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS,
28# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
31#
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080032import sys, os
33from EapolAAA import *
A R Karthick74d00312017-04-18 14:26:01 -070034from Enum import *
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080035import noseMd5AuthHolder as md5AuthHolder
36from socket import *
37from struct import *
38from md5 import md5
39from scapy.all import *
40from nose.tools import *
41from CordTestBase import CordTester
42
43class MD5AuthTest(EapolPacket, CordTester):
44
45 md5StateTable = Enumeration("MD5StateTable", ("ST_EAP_SETUP",
46 "ST_EAP_START",
47 "ST_EAP_ID_REQ",
48 "ST_EAP_MD5_CHALLENGE",
49 "ST_EAP_STATUS",
50 "ST_EAP_MD5_DONE"
51 )
52 )
53 md5EventTable = Enumeration("MD5EventTable", ("EVT_EAP_SETUP",
54 "EVT_EAP_START",
55 "EVT_EAP_ID_REQ",
56 "EVT_EAP_MD5_CHALLENGE",
57 "EVT_EAP_STATUS",
58 "EVT_EAP_MD5_DONE"
59 )
60 )
61 def __init__(self, intf = 'veth0', password = "password", required_status = "EAP_SUCCESS"):
62 self.passwd = password
63 self.req_status = required_status
64 self.fsmTable = md5AuthHolder.initMd5AuthHolderFsmTable(self, self.md5StateTable, self.md5EventTable)
65 EapolPacket.__init__(self, intf)
66 CordTester.__init__(self, self.fsmTable, self.md5StateTable.ST_EAP_MD5_DONE)
67 self.currentState = self.md5StateTable.ST_EAP_SETUP
68 self.currentEvent = self.md5EventTable.EVT_EAP_SETUP
69 self.nextState = None
70 self.nextEvent = None
71
72 def _eapSetup(self):
A R Karthick76a497a2017-04-12 10:59:39 -070073 print('Inside EAP Setup')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080074 self.setup()
75 self.nextEvent = self.md5EventTable.EVT_EAP_START
A R Karthick76a497a2017-04-12 10:59:39 -070076
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080077 def _eapStart(self):
A R Karthick76a497a2017-04-12 10:59:39 -070078 print('Inside EAP Start')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080079 self.eapol_start()
80 self.nextEvent = self.md5EventTable.EVT_EAP_ID_REQ
81
82 def _eapIdReq(self):
A R Karthick76a497a2017-04-12 10:59:39 -070083 print('Inside EAP ID Req')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080084 p = self.eapol_recv()
85 code, pkt_id, eaplen = unpack("!BBH", p[0:4])
A R Karthick76a497a2017-04-12 10:59:39 -070086 print("Code %d, id %d, len %d" %(code, pkt_id, eaplen))
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080087 assert_equal(code, EAP_REQUEST)
88 reqtype = unpack("!B", p[4:5])[0]
89 reqdata = p[5:4+eaplen]
90 assert_equal(reqtype, EAP_TYPE_ID)
A R Karthick76a497a2017-04-12 10:59:39 -070091 print("<====== Send EAP Response with identity = %s ================>" % USER)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080092 self.eapol_id_req(pkt_id, USER)
93 self.nextEvent = self.md5EventTable.EVT_EAP_MD5_CHALLENGE
94
95 def _eapMd5Challenge(self):
A R Karthick76a497a2017-04-12 10:59:39 -070096 print('Inside EAP MD5 Challenge Exchange')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -080097 challenge,pkt_id =self.eap_md5_challenge_recv(self.passwd)
98 resp=md5(challenge).digest()
99 resp=chr(len(resp))+resp
100 length= 5+len(resp)
A R Karthick76a497a2017-04-12 10:59:39 -0700101 print("Generated MD5 challenge is %s Length : %d" % (resp,length))
102 print("--> Send EAP response with MD5 challenge")
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800103 eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_MD5, str(resp))
104 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
105 self.nextEvent = self.md5EventTable.EVT_EAP_STATUS
106
107 def _eapStatus(self):
A R Karthick76a497a2017-04-12 10:59:39 -0700108 print('Inside EAP Status -- Sucess/Failure')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800109 if self.req_status == "EAP_SUCCESS":
110 status=self.eap_Status()
A R Karthick76a497a2017-04-12 10:59:39 -0700111 print("<============EAP code received is = %d ====================>" % status)
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800112 assert_equal(status, EAP_SUCCESS)
A R Karthick76a497a2017-04-12 10:59:39 -0700113 print("Received EAP SUCCESS")
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800114 else:
A R Karthick76a497a2017-04-12 10:59:39 -0700115 print('Inside EAP Status -- Sucess/Failure ===> SUCCESS should not be received , Since Negative Testcase')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800116 self.s.settimeout(10)
117 assert_equal(self.s.gettimeout(), 10)
A R Karthick76a497a2017-04-12 10:59:39 -0700118 print("Check if the socket timed out ====> Since negative testcase socket should timeout because ONOS is not sending the EAP FAILURE Message")
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800119 assert_raises(socket.error, self.s.recv, 1024)
120 self.nextEvent = self.md5EventTable.EVT_EAP_MD5_DONE
121
122 def _wrong_password(self):
A R Karthick76a497a2017-04-12 10:59:39 -0700123 print('Start Testcase for EAP-MD5 Wrong Password')
Chetan Gaonkerb424ff82016-03-08 12:11:12 -0800124 #self._eap_md5_states()
125 self.__init__(intf = 'veth0', password = "wrong_password", required_status = "EAP_FAILURE")