blob: a34c7491ff5e08230b5de8f4bb687cd0a1ddd33b [file] [log] [blame]
A R Karthicka2e53d62016-02-19 17:38:30 -08001import sys, os
A R Karthicka2e53d62016-02-19 17:38:30 -08002from EapolAAA import *
3from enum import *
4import noseTlsAuthHolder as tlsAuthHolder
5from scapy_ssl_tls.ssl_tls import *
6from socket import *
7from struct import *
8import scapy
9from nose.tools import *
10from CordTestBase import CordTester
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080011import re
A R Karthicka2e53d62016-02-19 17:38:30 -080012
13class TLSAuthTest(EapolPacket, CordTester):
14
15 tlsStateTable = Enumeration("TLSStateTable", ("ST_EAP_SETUP",
16 "ST_EAP_START",
17 "ST_EAP_ID_REQ",
18 "ST_EAP_TLS_HELLO_REQ",
19 "ST_EAP_TLS_CERT_REQ",
20 "ST_EAP_TLS_DONE"
21 )
22 )
23 tlsEventTable = Enumeration("TLSEventTable", ("EVT_EAP_SETUP",
24 "EVT_EAP_START",
25 "EVT_EAP_ID_REQ",
26 "EVT_EAP_TLS_HELLO_REQ",
27 "EVT_EAP_TLS_CERT_REQ",
28 "EVT_EAP_TLS_DONE"
29 )
30 )
31 def __init__(self, intf = 'veth0'):
32 self.fsmTable = tlsAuthHolder.initTlsAuthHolderFsmTable(self, self.tlsStateTable, self.tlsEventTable)
33 EapolPacket.__init__(self, intf)
34 CordTester.__init__(self, self.fsmTable, self.tlsStateTable.ST_EAP_TLS_DONE)
35 #self.tlsStateTable, self.tlsEventTable)
36 self.currentState = self.tlsStateTable.ST_EAP_SETUP
37 self.currentEvent = self.tlsEventTable.EVT_EAP_SETUP
38 self.nextState = None
39 self.nextEvent = None
40
41 def _eapSetup(self):
42 print 'Inside EAP Setup'
43 self.setup()
44 self.nextEvent = self.tlsEventTable.EVT_EAP_START
45
46 def _eapStart(self):
47 print 'Inside EAP Start'
48 self.eapol_start()
49 self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ
50
51 def _eapIdReq(self):
52 print 'Inside EAP ID Req'
53 p = self.eapol_recv()
54 code, pkt_id, eaplen = unpack("!BBH", p[0:4])
55 print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
56 assert_equal(code, EAP_REQUEST)
57 reqtype = unpack("!B", p[4:5])[0]
58 reqdata = p[5:4+eaplen]
59 assert_equal(reqtype, EAP_TYPE_ID)
60 print "<====== Send EAP Response with identity = %s ================>" % USER
61 self.eapol_id_req(pkt_id, USER)
62 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_HELLO_REQ
63
64 def _eapTlsHelloReq(self):
65 print 'Inside EAP TLS Hello Req'
66 p = self.eapol_recv()
67 code, pkt_id, eaplen = unpack("!BBH", p[0:4])
68 print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
69 assert_equal(code, EAP_REQUEST)
70 reqtype = unpack("!B", p[4:5])[0]
71 assert_equal(reqtype, EAP_TYPE_TLS)
72 reqdata = TLSRecord(version="TLS_1_0")/TLSHandshake()/TLSClientHello(version="TLS_1_0",
73 gmt_unix_time=1234,
74 random_bytes="A" * 28,
75 session_id='',
76 compression_methods=(TLSCompressionMethod.NULL),
77 cipher_suites=[TLSCipherSuite.RSA_WITH_AES_128_CBC_SHA]
78 )
79
80 #reqdata.show()
81 print "------> Sending Client Hello TLS payload of len %d ----------->" %len(reqdata)
82 eap_payload = self.eapTLS(EAP_RESPONSE, pkt_id, TLS_LENGTH_INCLUDED, str(reqdata))
83 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
84 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ
85
86 def _eapTlsCertReq(self):
87 print 'Inside EAP TLS Cert Req'
88 p = self.eapol_recv()
89 print 'Got TLS Cert Req with payload len: %d' %len(p)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080090 code, pkt_id, eaplen = unpack("!BBH", p[0:4])
91 print "Code %d, id %d, len %d" %(code, pkt_id, eaplen)
92 assert_equal(code, EAP_REQUEST)
93 reqtype = unpack("!B", p[4:5])[0]
94 assert_equal(reqt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HjtJ/Mjl+dcwmT5UI37N74qh2YUwgcgGA1UdIwSBwDCBvYAU
112HjtJ/Mjl+dcwmT5UI37N74qh2YWhgZmkgZYwgZMxCzAJBgNVBAYTAkZSMQ8wDQYD
113VQQIEwZSYWRpdXMxEjAQBgNVBAcTCVNvbWV3aGVyZTEVMBMGA1UEChMMRXhhbXBs
114ZSBJbmMuMSAwHgYJKoZIhvcNAQkBFhFhZG1pbkBleGFtcGxlLmNvbTEmMCQGA1UE
115AxMdRXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHmCCQDYSU0useAdIjAMBgNV
116HRMEBTADAQH/MDYGA1UdHwQvMC0wK6ApoCeGJWh0dHA6Ly93d3cuZXhhbXBsZS5j
117b20vZXhhbXBsZV9jYS5jcmwwDQYJKoZIhvcNAQELBQADggEBAEbkq17kbT7X/oiy
118E2DOV7g1W8Au+TQR0GKzjXPgmYVGixN8l/9dQZ9WVDmCetBy71UHgTxPp20My2zr
119uA7hy9FYNGtZ2jmu0p019fH+CSCL7RHHgKsY63UsldT1qPYiWiyqbWy5GvJX778N
120GxVo7oN33se1c4KEmMOLVqQqX5dDWjN2r27l0GFh1ssx4RHqOc57G5Txq861i6UT
121KlrN0xpyu7LjcQGMwKbfzCXfwys5i4rrAVX1spILTIihUKpD6FYxp6oj+d4ELZOh
122br3zfhKrkbvPCG0gEziBLnwd11ZJELQfm89IYBhmoOYkk5+ZOszDsXKGWzfV6XSW
123ZRv+LU0=
124-----END CERTIFICATE-----"""
125 self.der_cert = rex_pem.findall(self.pem_cert)[0].decode("base64")
126 self.pem_priv_key = """-----BEGIN RSA PRIVATE KEY-----
127MIIEpAIBAAKCAQEA84TzkjbcskbKZnrlKcXzSSgi07n+4N7kOM7uIhzpkTuU0HIv
128h4VZS2axxfV6hV3CD9MuKVg2zEhroqK1Js5n4ke230nSP/qiELfCl0R+hzRtbfKL
129tFUr1iHeU0uQ6v3q+Tg1K/Tmmg72uxKrhyHDL7z0BriPjhAHJ5XlQ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BmpnAizECgYBdt/y6rerWUx4MhDwwtTnel7JwHyo2MDFS6/5g
147n8qC2Lj6/fMDRE22w+CA2esp7EJNQJGv+b27iFpbJEDh+/Lf5YzIT4MwVskQ5bYB
148JFcmRxUVmf4e09D7o705U/DjCgMH09iCsbLmqQ38ONIRSHZaJtMDtNTHD1yi+jF+
149OT43gQKBgQC/2OHZoko6iRlNOAQ/tMVFNq7fL81GivoQ9F1U0Qr+DH3ZfaH8eIkX
150xT0ToMPJUzWAn8pZv0snA0um6SIgvkCuxO84OkANCVbttzXImIsL7pFzfcwV/ERK
151UM6j0ZuSMFOCr/lGPAoOQU0fskidGEHi1/kW+suSr28TqsyYZpwBDQ==
152-----END RSA PRIVATE KEY-----
153 """
154 self.der_priv_key = rex_pem.findall(self.pem_priv_key)[0].decode("base64")
155 reqdata = TLSRecord(version="TLS_1_0")/TLSHandshake()/TLSCertificateList(
156 certificates=[TLSCertificate(data=x509.X509Cert(self.der_cert))])
157 #reqdata.show()
158 print "------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata)
159 eap_payload = self.eapTLS(EAP_RESPONSE, pkt_id, TLS_LENGTH_INCLUDED, str(reqdata))
160 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
A R Karthicka2e53d62016-02-19 17:38:30 -0800161 self.nextEvent = None