blob: c92721a419f8115a397f25e2dcd5c350846a9688 [file] [log] [blame]
A R Karthick05d9b5f2016-06-08 11:53:54 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# 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 Karthick05d9b5f2016-06-08 11:53:54 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick05d9b5f2016-06-08 11:53:54 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# 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#
A R Karthicka2e53d62016-02-19 17:38:30 -080016import sys, os
A R Karthicka2e53d62016-02-19 17:38:30 -080017from EapolAAA import *
18from enum import *
19import noseTlsAuthHolder as tlsAuthHolder
20from scapy_ssl_tls.ssl_tls import *
A R Karthick22aa0c62016-05-31 11:17:12 -070021from scapy_ssl_tls.ssl_tls_crypto import *
A R Karthick307483c2016-06-06 17:05:19 -070022from tls_cert import Key
A R Karthicka2e53d62016-02-19 17:38:30 -080023from socket import *
ChetanGaonker6138fcd2016-08-18 17:56:39 -070024from CordTestServer import cord_test_radius_restart
A R Karthick307483c2016-06-06 17:05:19 -070025import struct
A R Karthicka2e53d62016-02-19 17:38:30 -080026import scapy
27from nose.tools import *
28from CordTestBase import CordTester
ChetanGaonker6138fcd2016-08-18 17:56:39 -070029from CordContainer import *
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080030import re
ChetanGaonker6138fcd2016-08-18 17:56:39 -070031import time
A R Karthick22aa0c62016-05-31 11:17:12 -070032
Chetan Gaonker5b366302016-03-21 16:18:21 -070033log.setLevel('INFO')
A R Karthick22aa0c62016-05-31 11:17:12 -070034
35def bytes_to_num(data):
A R Karthick2b93d6a2016-09-06 15:19:09 -070036 try:
37 return int(data.encode('hex'), 16)
38 except:
39 print('Exception')
40 return -1
A R Karthick22aa0c62016-05-31 11:17:12 -070041
A R Karthicka2e53d62016-02-19 17:38:30 -080042class TLSAuthTest(EapolPacket, CordTester):
43
44 tlsStateTable = Enumeration("TLSStateTable", ("ST_EAP_SETUP",
45 "ST_EAP_START",
46 "ST_EAP_ID_REQ",
47 "ST_EAP_TLS_HELLO_REQ",
48 "ST_EAP_TLS_CERT_REQ",
Chetan Gaonkerf8f77182016-03-11 15:34:57 -080049 "ST_EAP_TLS_CHANGE_CIPHER_SPEC",
50 "ST_EAP_TLS_FINISHED",
A R Karthicka2e53d62016-02-19 17:38:30 -080051 "ST_EAP_TLS_DONE"
52 )
53 )
54 tlsEventTable = Enumeration("TLSEventTable", ("EVT_EAP_SETUP",
55 "EVT_EAP_START",
56 "EVT_EAP_ID_REQ",
57 "EVT_EAP_TLS_HELLO_REQ",
58 "EVT_EAP_TLS_CERT_REQ",
Chetan Gaonkerf8f77182016-03-11 15:34:57 -080059 "EVT_EAP_TLS_CHANGE_CIPHER_SPEC",
60 "EVT_EAP_TLS_FINISHED",
A R Karthicka2e53d62016-02-19 17:38:30 -080061 "EVT_EAP_TLS_DONE"
62 )
63 )
A R Karthick22aa0c62016-05-31 11:17:12 -070064 server_hello_done_signature = '\x0e\x00\x00\x00'
65 SERVER_HELLO = '\x02'
66 SERVER_CERTIFICATE = '\x0b'
A R Karthick307483c2016-06-06 17:05:19 -070067 CERTIFICATE_REQUEST = '\x0d'
68 SERVER_HELLO_DONE = '\x0e'
A R Karthick22aa0c62016-05-31 11:17:12 -070069 SERVER_UNKNOWN = '\xff'
70 HANDSHAKE = '\x16'
71 CHANGE_CIPHER = '\x14'
72 TLS_OFFSET = 28
73 HDR_IDX = 0
74 DATA_IDX = 1
75 CB_IDX = 2
A R Karthick307483c2016-06-06 17:05:19 -070076
A R Karthickeeac7e12017-03-10 17:35:39 -080077 #this is from client.crt file
A R Karthick22aa0c62016-05-31 11:17:12 -070078 CLIENT_CERT = """-----BEGIN CERTIFICATE-----
A R Karthickeeac7e12017-03-10 17:35:39 -080079MIIDvTCCAqWgAwIBAgIBBjANBgkqhkiG9w0BAQUFADCBizELMAkGA1UEBhMCVVMx
80CzAJBgNVBAgMAkNBMRIwEAYDVQQHDAlTb21ld2hlcmUxEzARBgNVBAoMCkNpZW5h
81IEluYy4xHjAcBgkqhkiG9w0BCQEWD2FkbWluQGNpZW5hLmNvbTEmMCQGA1UEAwwd
82RXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTcwMzExMDA0NzQ0WhcN
83MjIxMDMxMDA0NzQ0WjBnMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExEzARBgNV
84BAoMCkNpZW5hIEluYy4xFzAVBgNVBAMMDnVzZXJAY2llbmEuY29tMR0wGwYJKoZI
A R Karthickaa10a202016-08-15 15:06:21 -070085hvcNAQkBFg51c2VyQGNpZW5hLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
A R Karthickeeac7e12017-03-10 17:35:39 -080086AQoCggEBAOTxi5+TUuaosUh7f/4U9unLV3qHCC7Sf3e0o8F+FP4BVQiIslUTEupw
87gd3VkiVAhxX74Oc3w7XF98msKx7dTetpQaZPZgEgGiDmzGzlAGkwqD4FO5JotJMg
88I0rpcYw+M/z+WDVgJx3I+VGyC6lFKhIJWTlTySheyMYpcgSvOuN4z8keCNg2uaRZ
89IOvIf6aUCTF7fp2HC/468/3T6R/VuYKFNdpsyU1ogbQqCUL4WHM2uWz6G7rzXI0/
90skkKemoGouFw+0I3ydPpGpiWK4NpPKHax4dRGaO1NmTDCtrJOAKPQx8CYYH0HMNp
91BbYvidTnHBxYUBrWNpzOJ/xgopawA8kCAwEAAaNPME0wEwYDVR0lBAwwCgYIKwYB
A R Karthickaa10a202016-08-15 15:06:21 -070092BQUHAwIwNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL3d3dy5leGFtcGxlLmNvbS9l
A R Karthickeeac7e12017-03-10 17:35:39 -080093eGFtcGxlX2NhLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAosa5wV/RA2XO/SVGWx5K
94JkQNBn6zKuLRODdkm1UwpkdSZpYoXw2ds6YPPbxV9gFeitCcPZQUho38Fg1LYrNW
959UsDPC2HAYSxrvCinTLC2NuqLsPj7NSu+41l37hGVG4WTjPXPqet33e0mqz1eYVn
963SsGvZl98sgG6ADD1RqUb0gpEocmoN+yx5W0hIAwZQPBt4nfAWdGH9AcJTL/Gjr7
975m4p3cQYm2AnROp+Bim4AELv02WIl77vYWivlzok1JfZ38GL94+CMLOSKNv9OLjc
98M/uh5Q6PB+4xv8kAjmO/Fq9T6f0KcQNNPY63omZCjrFiMEMvoOD4GIg+WV2SKLsQ
990g==
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800100-----END CERTIFICATE-----"""
A R Karthick22aa0c62016-05-31 11:17:12 -0700101
A R Karthickeeac7e12017-03-10 17:35:39 -0800102 #this is from client.key
A R Karthick307483c2016-06-06 17:05:19 -0700103 CLIENT_PRIV_KEY = """-----BEGIN RSA PRIVATE KEY-----
A R Karthickeeac7e12017-03-10 17:35:39 -0800104MIIEowIBAAKCAQEA5PGLn5NS5qixSHt//hT26ctXeocILtJ/d7SjwX4U/gFVCIiy
105VRMS6nCB3dWSJUCHFfvg5zfDtcX3yawrHt1N62lBpk9mASAaIObMbOUAaTCoPgU7
106kmi0kyAjSulxjD4z/P5YNWAnHcj5UbILqUUqEglZOVPJKF7IxilyBK8643jPyR4I
1072Da5pFkg68h/ppQJMXt+nYcL/jrz/dPpH9W5goU12mzJTWiBtCoJQvhYcza5bPob
108uvNcjT+ySQp6agai4XD7QjfJ0+kamJYrg2k8odrHh1EZo7U2ZMMK2sk4Ao9DHwJh
109gfQcw2kFti+J1OccHFhQGtY2nM4n/GCilrADyQIDAQABAoIBAF+xepvDl3Yj8p4K
110SPBp0N7eCH2FbW0svOzLC1t4GMwmwGUlxex7YX+ucQnJGCIL+6q7skDS9THIQo8A
111MLHg7I3GXBNowokb4u/3qGCnw2k0Vk4+H61NqJSKbVYFh1mIMnC/2xzMSO1RMKzu
112D6O77h7F245zr/P40lDJyAefOq0S6sgZqxmHmoRTHhp0tXV4mkzV7P7RqoJrvAiG
113tGMk5OfdoILnNfoeXNC50nw201UK7xhrrqqlAWZRAaUZJtsb1GxW+jOra6OtsCVg
114AKS/FxzUGMsoqluM5mHLBEN69DAvBBs8g7UVKdmCDZC+feJ31eAnPAoc1gxBHdQU
115pCnA8dECgYEA+Q6X80gnoyG0g66Gh62R7RgDLYPpgkZimLeoX49mwizAUkeSf/Mj
116raVajRmJ8J1n4UklHdQe0PE9Jhuxo4Uo9sP71ZqpQPEvN35/Sw0xxQHcwxD73SWa
117UEVsnWIDJ6QrkoBOhjDMM6tyDSPVDS23352E6sZ9EU45qWvncb5OTdUCgYEA61Np
118Qs/CpEWtPG8IiEPKPEWUEwoO8SS6C4R/UfXNC96GhfIpA4Uy3fQwTUtHEMPL+7lh
119SPFPQDBH90jOTYg30EfHiBMlKW4l21XS+PotTP3ktqZMgx06SnoM2a/+crpzFqkb
120i4eAPCsdTispElbtqleLuUbFO9aG3jHMsK2RtCUCgYB04G9YYL0RJgkTXryNQVvg
121ussK+gOD+kncxxtBtQcRCnU6Z5INb2mH3LgwzRJZk1SjeXLsm5XWkc8Tah2j0iKW
122IwS0if7xlf2Felx8OPXpMOWLuRWpAzN2hg3hkZRPbxBvkLzI5m99s/Ay0GTz6UeH
123reEpV/prO519r0COtTMD/QKBgCdRinbVS8oysh002BIccX/ciD8eIRz9a/BctQE2
124tonTJlre+SdTbApVsHRZrYgJjt2CPdT4LKum5X9VtNKTfe/Y7me3+y+O7dhV4Kgk
1259Mi2ay5xXrtReNnUxqzgkP0OVghlPOr1OuHSulTDNVuRFqitc/UC9BVpZKNfYrnq
126ZjvZAoGBALzgzXajgIdhghCt5PFLkhh3xyOliTXWFstHcMZdQF2wQyeF/uQ2zrC/
1272t1Sa+egV3QDUPYzW9YLQs9eaLh7MS9wCHLY2SMElAqYiNjRfkT4wWdPfeyFx4+E
128Euwtu+lPJ7sEpNu5jX63OS2AeZsQYlsT0Ai+lB4TeyoE6Pj04iC0
A R Karthick307483c2016-06-06 17:05:19 -0700129-----END RSA PRIVATE KEY-----"""
130
A R Karthick22aa0c62016-05-31 11:17:12 -0700131 def handle_server_hello_done(self, server_hello_done):
132 if server_hello_done[-4:] == self.server_hello_done_signature:
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700133 log.info('server hello received')
A R Karthick22aa0c62016-05-31 11:17:12 -0700134 self.server_hello_done_received = True
135
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700136 def __init__(self, intf = 'veth0', client_cert = None, client_priv_key = None,
137 fail_cb = None, src_mac='default', version = "TLS_1_0", session_id = '',
138 session_id_length = None, gmt_unix_time=1234, invalid_content_type = 22,
139 record_fragment_length = None, cipher_suites_length = None,
140 compression_methods_length = None, compression_methods = TLSCompressionMethod.NULL,
141 CipherSuite = True, cipher_suite = 'RSA_WITH_AES_256_CBC_SHA', id_mismatch_in_identifier_response_packet = False,
142 id_mismatch_in_client_hello_packet = False , dont_send_client_certificate = False,
143 dont_send_client_hello = False, restart_radius = False, invalid_client_hello_handshake_type = False,
144 invalid_cert_req_handshake = False, incorrect_tlsrecord_type_cert_req = False,
145 invalid_client_hello_handshake_length = False, clientkeyex_replace_with_serverkeyex = False):
146
A R Karthick22aa0c62016-05-31 11:17:12 -0700147 self.fsmTable = tlsAuthHolder.initTlsAuthHolderFsmTable(self, self.tlsStateTable, self.tlsEventTable)
148 EapolPacket.__init__(self, intf)
149 CordTester.__init__(self, self.fsmTable, self.tlsStateTable.ST_EAP_TLS_DONE)
150 #self.tlsStateTable, self.tlsEventTable)
151 self.currentState = self.tlsStateTable.ST_EAP_SETUP
152 self.currentEvent = self.tlsEventTable.EVT_EAP_SETUP
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700153 self.src_mac = src_mac
154 self.version = version
155 self.session_id_length = session_id_length
156 self.session_id = session_id
157 self.gmt_unix_time = gmt_unix_time
158 self.invalid_content_type = invalid_content_type
159 self.CipherSuite = CipherSuite
160 self.cipher_suites_length = cipher_suites_length
161 self.compression_methods_length = compression_methods_length
162 self.cipher_suite = cipher_suite
163 self.compression_methods_length = compression_methods_length
164 self.compression_methods = compression_methods
165 self.record_fragment_length = record_fragment_length
166 self.invalid_client_hello_handshake_type = invalid_client_hello_handshake_type
167 self.invalid_client_hello_handshake_length = invalid_client_hello_handshake_length
168 self.invalid_cert_req_handshake = invalid_cert_req_handshake
169 self.id_mismatch_in_identifier_response_packet = id_mismatch_in_identifier_response_packet
170 self.id_mismatch_in_client_hello_packet = id_mismatch_in_client_hello_packet
171 self.dont_send_client_certificate = dont_send_client_certificate
172 self.dont_send_client_hello = dont_send_client_hello
173 self.incorrect_tlsrecord_type_cert_req = incorrect_tlsrecord_type_cert_req
174 self.restart_radius = restart_radius
175 self.clientkeyex_replace_with_serverkeyex = clientkeyex_replace_with_serverkeyex
A R Karthick22aa0c62016-05-31 11:17:12 -0700176 self.nextState = None
177 self.nextEvent = None
178 self.pending_bytes = 0 #for TLS fragment reassembly
179 self.server_hello_done_received = False
A R Karthickf028ec92016-08-15 16:58:11 -0700180 self.server_hello_done_eap_id = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700181 self.send_tls_response = True
182 self.server_certs = []
183 self.pkt_last = ''
184 self.pkt_history = []
185 self.pkt_map = { self.SERVER_HELLO: ['', '', lambda pkt: pkt ],
186 self.SERVER_CERTIFICATE: ['', '', lambda pkt: pkt ],
A R Karthick307483c2016-06-06 17:05:19 -0700187 self.CERTIFICATE_REQUEST: ['', '', lambda pkt: pkt ],
A R Karthick22aa0c62016-05-31 11:17:12 -0700188 self.SERVER_HELLO_DONE: ['', '', self.handle_server_hello_done ],
189 self.SERVER_UNKNOWN: ['', '', lambda pkt: pkt ]
190 }
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700191 if self.clientkeyex_replace_with_serverkeyex:
192 self.tls_ctx = TLSSessionCtx(client = False)
193 else:
194 self.tls_ctx = TLSSessionCtx(client = True)
A R Karthick05d9b5f2016-06-08 11:53:54 -0700195 self.client_cert = self.CLIENT_CERT if client_cert is None else client_cert
196 self.client_priv_key = self.CLIENT_PRIV_KEY if client_priv_key is None else client_priv_key
197 self.failTest = False
198 self.fail_cb = fail_cb
A R Karthick22aa0c62016-05-31 11:17:12 -0700199
200 def load_tls_record(self, data, pkt_type = ''):
A R Karthick307483c2016-06-06 17:05:19 -0700201 #if pkt_type not in [ self.SERVER_HELLO_DONE, self.SERVER_UNKNOWN ]:
202 if pkt_type == self.SERVER_HELLO_DONE:
203 data = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/data)
204 elif pkt_type == self.CERTIFICATE_REQUEST:
205 data = str(TLSRecord()/TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/data[9:])
206 data = None #For now ignore this record
207 if data:
A R Karthick22aa0c62016-05-31 11:17:12 -0700208 TLS(data, ctx = self.tls_ctx)
209
210 def pkt_update(self, pkt_type, data, hdr=None, reassembled = False):
211 if not self.pkt_map.has_key(pkt_type):
212 return
213 if hdr is not None:
214 self.pkt_map[pkt_type][self.HDR_IDX] += hdr
215 self.pkt_map[pkt_type][self.DATA_IDX] += data
216 if reassembled is True:
217 self.pkt_map[pkt_type][self.CB_IDX](self.pkt_map[pkt_type][self.DATA_IDX])
218 log.info('Appending packet type %02x to packet history of len %d'
219 %(ord(pkt_type), len(self.pkt_map[pkt_type][self.DATA_IDX])))
220 self.pkt_history.append(self.pkt_map[pkt_type][self.DATA_IDX])
221 data = ''.join(self.pkt_map[pkt_type][:self.DATA_IDX+1])
222 self.load_tls_record(data, pkt_type = pkt_type)
223 self.pkt_map[pkt_type][self.HDR_IDX] = ''
224 self.pkt_map[pkt_type][self.DATA_IDX] = ''
225
A R Karthick05d9b5f2016-06-08 11:53:54 -0700226 def tlsFail(self):
227 ##Force a failure
ChetanGaonker3441faf2016-10-27 12:15:21 -0700228 log.info('entering into testFail function')
A R Karthick05d9b5f2016-06-08 11:53:54 -0700229 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
A.R Karthick7eb2ce02016-06-10 19:00:50 -0700230 self.nextState = self.tlsStateTable.ST_EAP_TLS_FINISHED
A R Karthick05d9b5f2016-06-08 11:53:54 -0700231 self.failTest = True
232
A R Karthick22aa0c62016-05-31 11:17:12 -0700233 def eapol_server_hello_cb(self, pkt):
234 '''Reassemble and send response for server hello/certificate fragments'''
235 r = str(pkt)
236 offset = self.TLS_OFFSET
237 tls_data = r[offset:]
A R Karthick307483c2016-06-06 17:05:19 -0700238 type_hdrlen = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700239 if self.pending_bytes > 0:
240 if len(tls_data) >= self.pending_bytes:
241 self.pkt_update(self.pkt_last, tls_data[:self.pending_bytes], reassembled = True)
242 offset += self.pending_bytes
243 self.pkt_last = ''
244 self.pending_bytes = 0
245 else:
246 self.pkt_update(self.pkt_last, tls_data)
247 self.pending_bytes -= len(tls_data)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700248 print('Offset: %d, pkt : %d, pending %d\n' %(offset, len(pkt), self.pending_bytes))
A R Karthick22aa0c62016-05-31 11:17:12 -0700249 while self.pending_bytes == 0 and offset < len(pkt):
250 tls_data = r[offset:]
A R Karthick2b93d6a2016-09-06 15:19:09 -0700251 hexdump(tls_data)
A R Karthick22aa0c62016-05-31 11:17:12 -0700252 self.pending_bytes = bytes_to_num(tls_data[3:5])
A R Karthick2b93d6a2016-09-06 15:19:09 -0700253 if self.pending_bytes < 0:
254 self.pending_bytes = 0
255 return
A R Karthick22aa0c62016-05-31 11:17:12 -0700256 if tls_data[0] == self.HANDSHAKE:
257 pkt_type = tls_data[5]
A R Karthick307483c2016-06-06 17:05:19 -0700258 if pkt_type in [ self.CERTIFICATE_REQUEST ]:
259 self.pending_bytes = bytes_to_num(tls_data[6:9])
260 type_hdrlen = 4
261 if len(tls_data) - 5 - type_hdrlen >= self.pending_bytes:
262 data_received = tls_data[5: 5 + type_hdrlen + self.pending_bytes ]
263 offset += 5 + type_hdrlen + self.pending_bytes
264 type_hdrlen = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700265 self.pending_bytes = 0
266 self.pkt_update(pkt_type, data_received,
267 hdr = tls_data[:5],
268 reassembled = True)
269 else:
270 self.pkt_update(pkt_type, tls_data[5:],
271 hdr = tls_data[:5],
272 reassembled = False)
A R Karthick307483c2016-06-06 17:05:19 -0700273 self.pending_bytes -= len(tls_data) - 5 - type_hdrlen
A R Karthick22aa0c62016-05-31 11:17:12 -0700274 self.pkt_last = pkt_type
275 log.info('Pending bytes left %d' %(self.pending_bytes))
276 assert self.pending_bytes > 0
A R Karthick307483c2016-06-06 17:05:19 -0700277 elif tls_data[0] == self.SERVER_HELLO_DONE:
A R Karthickf028ec92016-08-15 16:58:11 -0700278 self.server_hello_done_eap_id = pkt[EAP].id
A R Karthick307483c2016-06-06 17:05:19 -0700279 self.pkt_update(tls_data[0], tls_data, reassembled = True)
280 break
A R Karthick22aa0c62016-05-31 11:17:12 -0700281 else:
282 self.pkt_last = self.SERVER_UNKNOWN
283 if len(tls_data) - 5 >= self.pending_bytes:
284 offset += 5 + self.pending_bytes
285 self.pending_bytes = 0
286 self.pkt_last = ''
287
A R Karthickf028ec92016-08-15 16:58:11 -0700288 #send TLS response ack till we receive server hello done
289 if self.server_hello_done_received == False:
A R Karthick22aa0c62016-05-31 11:17:12 -0700290 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, '')
291 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
292
293 def _eapSetup(self):
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700294 #if self.src_mac == 'bcast':self.setup(src_mac='bcast')
295 #if self.src_mac == 'mcast': self.setup(src_mac='mcast')
296 #if self.src_mac == 'zeros': self.setup(src_mac='zeros')
297 #if self.src_mac == 'default': self.setup(src_mac='default')
ChetanGaonkere2b665b2016-12-07 00:50:56 -0800298 log.info('source mac is %s'%self.src_mac)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700299 self.setup(src_mac=self.src_mac)
A R Karthick22aa0c62016-05-31 11:17:12 -0700300 self.nextEvent = self.tlsEventTable.EVT_EAP_START
A R Karthick05d9b5f2016-06-08 11:53:54 -0700301
A R Karthick22aa0c62016-05-31 11:17:12 -0700302 def _eapStart(self):
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700303 log.info('_eapStart method started')
A R Karthick22aa0c62016-05-31 11:17:12 -0700304 self.eapol_start()
305 self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ
306
307 def _eapIdReq(self):
308 log.info( 'Inside EAP ID Req' )
309 def eapol_cb(pkt):
310 log.info('Got EAPOL packet with type id and code request')
311 log.info('Packet code: %d, type: %d, id: %d', pkt[EAP].code, pkt[EAP].type, pkt[EAP].id)
312 log.info("<====== Send EAP Response with identity = %s ================>" % USER)
ChetanGaonkere2b665b2016-12-07 00:50:56 -0800313 if self.id_mismatch_in_identifier_response_packet:
314 log.info('\nSending invalid id field in EAP Identity Response packet')
315 self.eapol_id_req(pkt[EAP].id+10, USER)
316 else:
317 self.eapol_id_req(pkt[EAP].id, USER)
A R Karthick22aa0c62016-05-31 11:17:12 -0700318
A R Karthick05d9b5f2016-06-08 11:53:54 -0700319 r = self.eapol_scapy_recv(cb = eapol_cb,
320 lfilter =
321 lambda pkt: EAP in pkt and pkt[EAP].type == EAP.TYPE_ID and pkt[EAP].code == EAP.REQUEST)
322 if len(r) > 0:
323 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_HELLO_REQ
324 else:
325 self.tlsFail()
326 return r
A R Karthick22aa0c62016-05-31 11:17:12 -0700327
328 def _eapTlsHelloReq(self):
329
330 def eapol_cb(pkt):
331 log.info('Got hello request for id %d', pkt[EAP].id)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700332 self.client_hello = TLSClientHello(version= self.version,
333 gmt_unix_time=self.gmt_unix_time,
A R Karthick22aa0c62016-05-31 11:17:12 -0700334 random_bytes= '\xAB' * 28,
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700335 session_id_length = self.session_id_length,
336 session_id= self.session_id,
337 compression_methods_length = self.compression_methods_length,
338 compression_methods= self.compression_methods,
339 cipher_suites_length = self.cipher_suites_length,
340 cipher_suites=[self.cipher_suite]
A R Karthick22aa0c62016-05-31 11:17:12 -0700341 )
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700342 if self.invalid_client_hello_handshake_type:
343 log.info('sending server_hello instead of client_hello handshape type in client hello packet')
344 client_hello_data = TLSHandshake(type='server_hello')/self.client_hello
345 elif self.invalid_client_hello_handshake_length:
346 log.info('sending TLS Handshake message with zero length field in client hello packet')
347 client_hello_data = TLSHandshake(length=0)/self.client_hello
348 else:
349 client_hello_data = TLSHandshake()/self.client_hello
350 #client_hello_data = TLSHandshake()/self.client_hello
A R Karthick307483c2016-06-06 17:05:19 -0700351 self.pkt_history.append( str(client_hello_data) )
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700352 if self.record_fragment_length:
353 reqdata = TLSRecord(length=self.record_fragment_length)/client_hello_data
354 else:
355 reqdata = TLSRecord()/client_hello_data
A R Karthick22aa0c62016-05-31 11:17:12 -0700356 self.load_tls_record(str(reqdata))
357 log.info("Sending Client Hello TLS payload of len %d, id %d" %(len(reqdata),pkt[EAP].id))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700358 if self.id_mismatch_in_client_hello_packet:
359 log.info('\nsending invalid id field in client hello packet')
360 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id+10, TLS_LENGTH_INCLUDED, str(reqdata))
361 else:
362 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, str(reqdata))
363 if self.dont_send_client_hello:
364 log.info('\nskipping client hello packet sending part')
365 pass
366 else:
367 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
368 if self.restart_radius:
369 cord_test_radius_restart()
A R Karthick22aa0c62016-05-31 11:17:12 -0700370
A R Karthick05d9b5f2016-06-08 11:53:54 -0700371 r = self.eapol_scapy_recv(cb = eapol_cb,
A R Karthick22aa0c62016-05-31 11:17:12 -0700372 lfilter =
373 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST)
A R Karthick05d9b5f2016-06-08 11:53:54 -0700374
375 if len(r) == 0:
376 self.tlsFail()
377 return r
378
A R Karthickf028ec92016-08-15 16:58:11 -0700379 #move to client/server certificate request
A R Karthick22aa0c62016-05-31 11:17:12 -0700380 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ
381
A R Karthick307483c2016-06-06 17:05:19 -0700382 def get_verify_data(self):
A R Karthick22aa0c62016-05-31 11:17:12 -0700383 all_handshake_pkts = ''.join(self.pkt_history)
A R Karthick307483c2016-06-06 17:05:19 -0700384 return self.tls_ctx.get_verify_data(data = all_handshake_pkts)
385
386 def get_verify_signature(self, pem_data):
387 all_handshake_pkts = ''.join(self.pkt_history)
388 k = Key(pem_data)
389 signature = k.sign(all_handshake_pkts, t = 'pkcs', h = 'tls')
390 signature_data = '{}{}'.format(struct.pack('!H', len(signature)), signature)
391 return signature_data
392
393 def get_encrypted_handshake_msg(self, finish_val=''):
A R Karthick22aa0c62016-05-31 11:17:12 -0700394 if not finish_val:
A R Karthick307483c2016-06-06 17:05:19 -0700395 finish_val = self.get_verify_data()
A R Karthick22aa0c62016-05-31 11:17:12 -0700396 msg = str(TLSHandshake(type=TLSHandshakeType.FINISHED)/finish_val)
397 crypto_container = CryptoContainer(self.tls_ctx, data = msg,
398 content_type = TLSContentType.HANDSHAKE)
399 return crypto_container.encrypt()
400
A R Karthick307483c2016-06-06 17:05:19 -0700401 def get_encrypted_application_msg(self, msg = ''):
402 '''Needed with tunneled TLS'''
403 if not msg:
404 msg = 'test data'
405 return to_raw(TLSPlaintext(data = 'GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n'), self.tls_ctx)
406
A R Karthick22aa0c62016-05-31 11:17:12 -0700407 def _eapTlsCertReq(self):
A R Karthickf028ec92016-08-15 16:58:11 -0700408 log.info('Receiving server certificates')
409 while self.server_hello_done_received == False:
410 r = self.eapol_scapy_recv(cb = self.eapol_server_hello_cb,
411 lfilter =
412 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and \
413 pkt[EAP].code == EAP.REQUEST)
414 if len(r) == 0:
415 self.tlsFail()
416 return r
A R Karthickf028ec92016-08-15 16:58:11 -0700417 log.info('Sending client certificate request')
418 rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL)
419 if self.client_cert:
420 der_cert = rex_pem.findall(self.client_cert)[0].decode("base64")
421 client_certificate_list = TLSHandshake()/TLSCertificateList(
422 certificates=[TLSCertificate(data=x509.X509Cert(der_cert))])
A R Karthick05d9b5f2016-06-08 11:53:54 -0700423 else:
A R Karthickf028ec92016-08-15 16:58:11 -0700424 client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[])
425 client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700426 kex_data = self.tls_ctx.get_client_kex_data()
A R Karthickf028ec92016-08-15 16:58:11 -0700427 client_key_ex_data = TLSHandshake()/kex_data
428 client_key_ex = TLSRecord()/client_key_ex_data
429 if self.client_cert:
430 self.load_tls_record(str(client_certificate))
431 self.pkt_history.append(str(client_certificate_list))
432 self.load_tls_record(str(client_key_ex))
433 self.pkt_history.append(str(client_key_ex_data))
434 verify_signature = self.get_verify_signature(self.client_priv_key)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700435 if self.invalid_cert_req_handshake:
436 log.info("sending 'certificate-request' type of handshake message instead of 'certificate-verify' type")
437 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/verify_signature
438 else:
439 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature
440 if self.incorrect_tlsrecord_type_cert_req:
441 log.info("sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet")
442 client_cert_record = TLSRecord(content_type=TLSContentType.ALERT)/client_cert_verify
443 else:
444 client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify
A R Karthickf028ec92016-08-15 16:58:11 -0700445 self.pkt_history.append(str(client_cert_verify))
446 #log.info('TLS ctxt: %s' %self.tls_ctx)
447 client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec()
448 enc_handshake_msg = self.get_encrypted_handshake_msg()
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700449 if self.invalid_content_type:
450 handshake_msg = str(TLSRecord(content_type=self.invalid_content_type)/enc_handshake_msg)
451 else:
452 handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg)
A R Karthickf028ec92016-08-15 16:58:11 -0700453 reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs]))
454 reqdata += handshake_msg
455 log.info("------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700456 if self.dont_send_client_certificate:
457 log.info('\nskipping sending client certificate part')
458 pass
459 else:
460 status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED,
A R Karthickf028ec92016-08-15 16:58:11 -0700461 payload = reqdata, fragsize = 1024)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700462 assert_equal(status, True)
463 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800464
ChetanGaonker3441faf2016-10-27 12:15:21 -0700465 def _eapTlsCertReq_delay(self):
466 self.server_hello_done_received = True
467 log.info('Sending client certificate request')
468 rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL)
469
470 if self.client_cert:
471 der_cert = rex_pem.findall(self.client_cert)[0].decode("base64")
472 client_certificate_list = TLSHandshake()/TLSCertificateList(
473 certificates=[TLSCertificate(data=x509.X509Cert(der_cert))])
474 else:
475 client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[])
476
477 client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list
478 kex_data = self.tls_ctx.get_client_kex_data()
479 client_key_ex_data = TLSHandshake()/kex_data
480 client_key_ex = TLSRecord()/client_key_ex_data
481
482 if self.client_cert:
483 self.load_tls_record(str(client_certificate))
484 self.pkt_history.append(str(client_certificate_list))
485
486 self.load_tls_record(str(client_key_ex))
487 self.pkt_history.append(str(client_key_ex_data))
488 verify_signature = self.get_verify_signature(self.client_priv_key)
489
490 if self.invalid_cert_req_handshake:
491 log.info("Sending 'certificate-request' type of handshake message instead of 'certificate-verify' type")
492 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/verify_signature
493 else:
494 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature
495
496 if self.incorrect_tlsrecord_type_cert_req:
497 log.info("Sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet")
498 client_cert_record = TLSRecord(content_type=TLSContentType.ALERT)/client_cert_verify
499 else:
500 client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify
501
502 self.pkt_history.append(str(client_cert_verify))
503 #log.info('TLS ctxt: %s' %self.tls_ctx)
504 client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec()
505 enc_handshake_msg = self.get_encrypted_handshake_msg()
506
507 if self.invalid_content_type:
508 handshake_msg = str(TLSRecord(content_type=self.invalid_content_type)/enc_handshake_msg)
509 else:
510 handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg)
511 reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs]))
512 reqdata += handshake_msg
513 log.info("------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata))
514
515 if self.dont_send_client_certificate:
516 log.info('\nSkipping sending client certificate part')
517 pass
518 else:
519 status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED,
520 payload = reqdata, fragsize = 1024)
521 assert_equal(status, True)
522 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
523
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800524 def _eapTlsChangeCipherSpec(self):
Chetan Gaonker5b366302016-03-21 16:18:21 -0700525 def eapol_cb(pkt):
A R Karthick22aa0c62016-05-31 11:17:12 -0700526 r = str(pkt)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700527 log.info('data received in change cipher spec function is %s'%pkt.show())
A R Karthick307483c2016-06-06 17:05:19 -0700528 tls_data = r[self.TLS_OFFSET:]
A R Karthick22aa0c62016-05-31 11:17:12 -0700529 log.info('Verifying TLS Change Cipher spec record type %x' %ord(tls_data[0]))
530 assert tls_data[0] == self.CHANGE_CIPHER
A R Karthick307483c2016-06-06 17:05:19 -0700531 log.info('Handshake finished. Sending empty data')
532 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, 0, '')
533 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
Chetan Gaonker5b366302016-03-21 16:18:21 -0700534
A R Karthick05d9b5f2016-06-08 11:53:54 -0700535 r = self.eapol_scapy_recv(cb = eapol_cb,
536 lfilter =
537 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST)
538 if len(r) > 0:
539 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
540 else:
541 self.tlsFail()
542 return r
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800543
544 def _eapTlsFinished(self):
A.R Karthick7eb2ce02016-06-10 19:00:50 -0700545 self.nextEvent = None
Chetan Gaonker5b366302016-03-21 16:18:21 -0700546 def eapol_cb(pkt):
A R Karthick307483c2016-06-06 17:05:19 -0700547 log.info('Server authentication successfull')
Chetan Gaonker5b366302016-03-21 16:18:21 -0700548
A R Karthick05d9b5f2016-06-08 11:53:54 -0700549 timeout = 5
550 if self.failTest is True:
551 if self.fail_cb is not None:
552 self.fail_cb()
553 return
554 timeout = None ##Wait forever on failure and force testcase timeouts
555
Chetan Gaonker5b366302016-03-21 16:18:21 -0700556 self.eapol_scapy_recv(cb = eapol_cb,
A R Karthick22aa0c62016-05-31 11:17:12 -0700557 lfilter =
A R Karthick05d9b5f2016-06-08 11:53:54 -0700558 lambda pkt: EAP in pkt and pkt[EAP].code == EAP.SUCCESS,
559 timeout = timeout)
A R Karthick307483c2016-06-06 17:05:19 -0700560 self.eapol_logoff()