A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 2 | # Copyright 2016-present Ciena Corporation |
| 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 |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 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 | # |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 16 | import sys, os |
| 17 | from EapolAAA import * |
| 18 | from enum import * |
| 19 | import noseMd5AuthHolder as md5AuthHolder |
| 20 | from socket import * |
| 21 | from struct import * |
| 22 | from md5 import md5 |
| 23 | from scapy.all import * |
| 24 | from nose.tools import * |
| 25 | from CordTestBase import CordTester |
| 26 | |
| 27 | class MD5AuthTest(EapolPacket, CordTester): |
| 28 | |
| 29 | md5StateTable = Enumeration("MD5StateTable", ("ST_EAP_SETUP", |
| 30 | "ST_EAP_START", |
| 31 | "ST_EAP_ID_REQ", |
| 32 | "ST_EAP_MD5_CHALLENGE", |
| 33 | "ST_EAP_STATUS", |
| 34 | "ST_EAP_MD5_DONE" |
| 35 | ) |
| 36 | ) |
| 37 | md5EventTable = Enumeration("MD5EventTable", ("EVT_EAP_SETUP", |
| 38 | "EVT_EAP_START", |
| 39 | "EVT_EAP_ID_REQ", |
| 40 | "EVT_EAP_MD5_CHALLENGE", |
| 41 | "EVT_EAP_STATUS", |
| 42 | "EVT_EAP_MD5_DONE" |
| 43 | ) |
| 44 | ) |
| 45 | def __init__(self, intf = 'veth0', password = "password", required_status = "EAP_SUCCESS"): |
| 46 | self.passwd = password |
| 47 | self.req_status = required_status |
| 48 | self.fsmTable = md5AuthHolder.initMd5AuthHolderFsmTable(self, self.md5StateTable, self.md5EventTable) |
| 49 | EapolPacket.__init__(self, intf) |
| 50 | CordTester.__init__(self, self.fsmTable, self.md5StateTable.ST_EAP_MD5_DONE) |
| 51 | self.currentState = self.md5StateTable.ST_EAP_SETUP |
| 52 | self.currentEvent = self.md5EventTable.EVT_EAP_SETUP |
| 53 | self.nextState = None |
| 54 | self.nextEvent = None |
| 55 | |
| 56 | def _eapSetup(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 57 | print('Inside EAP Setup') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 58 | self.setup() |
| 59 | self.nextEvent = self.md5EventTable.EVT_EAP_START |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 60 | |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 61 | def _eapStart(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 62 | print('Inside EAP Start') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 63 | self.eapol_start() |
| 64 | self.nextEvent = self.md5EventTable.EVT_EAP_ID_REQ |
| 65 | |
| 66 | def _eapIdReq(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 67 | print('Inside EAP ID Req') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 68 | p = self.eapol_recv() |
| 69 | code, pkt_id, eaplen = unpack("!BBH", p[0:4]) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 70 | print("Code %d, id %d, len %d" %(code, pkt_id, eaplen)) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 71 | assert_equal(code, EAP_REQUEST) |
| 72 | reqtype = unpack("!B", p[4:5])[0] |
| 73 | reqdata = p[5:4+eaplen] |
| 74 | assert_equal(reqtype, EAP_TYPE_ID) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 75 | print("<====== Send EAP Response with identity = %s ================>" % USER) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 76 | self.eapol_id_req(pkt_id, USER) |
| 77 | self.nextEvent = self.md5EventTable.EVT_EAP_MD5_CHALLENGE |
| 78 | |
| 79 | def _eapMd5Challenge(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 80 | print('Inside EAP MD5 Challenge Exchange') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 81 | challenge,pkt_id =self.eap_md5_challenge_recv(self.passwd) |
| 82 | resp=md5(challenge).digest() |
| 83 | resp=chr(len(resp))+resp |
| 84 | length= 5+len(resp) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 85 | print("Generated MD5 challenge is %s Length : %d" % (resp,length)) |
| 86 | print("--> Send EAP response with MD5 challenge") |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 87 | eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_MD5, str(resp)) |
| 88 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 89 | self.nextEvent = self.md5EventTable.EVT_EAP_STATUS |
| 90 | |
| 91 | def _eapStatus(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 92 | print('Inside EAP Status -- Sucess/Failure') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 93 | if self.req_status == "EAP_SUCCESS": |
| 94 | status=self.eap_Status() |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 95 | print("<============EAP code received is = %d ====================>" % status) |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 96 | assert_equal(status, EAP_SUCCESS) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 97 | print("Received EAP SUCCESS") |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 98 | else: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 99 | print('Inside EAP Status -- Sucess/Failure ===> SUCCESS should not be received , Since Negative Testcase') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 100 | self.s.settimeout(10) |
| 101 | assert_equal(self.s.gettimeout(), 10) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 102 | print("Check if the socket timed out ====> Since negative testcase socket should timeout because ONOS is not sending the EAP FAILURE Message") |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 103 | assert_raises(socket.error, self.s.recv, 1024) |
| 104 | self.nextEvent = self.md5EventTable.EVT_EAP_MD5_DONE |
| 105 | |
| 106 | def _wrong_password(self): |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 107 | print('Start Testcase for EAP-MD5 Wrong Password') |
Chetan Gaonker | b424ff8 | 2016-03-08 12:11:12 -0800 | [diff] [blame] | 108 | #self._eap_md5_states() |
| 109 | self.__init__(intf = 'veth0', password = "wrong_password", required_status = "EAP_FAILURE") |