blob: 265572245f8447b88b34a784fb6c6516fbf3716f [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 Karthick05d9b5f2016-06-08 11:53:54 -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 Karthick05d9b5f2016-06-08 11:53:54 -070023#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070024# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick05d9b5f2016-06-08 11:53:54 -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#
A R Karthicka2e53d62016-02-19 17:38:30 -080032import sys, os
A R Karthicka2e53d62016-02-19 17:38:30 -080033from EapolAAA import *
A R Karthick74d00312017-04-18 14:26:01 -070034from Enum import *
A R Karthicka2e53d62016-02-19 17:38:30 -080035import noseTlsAuthHolder as tlsAuthHolder
36from scapy_ssl_tls.ssl_tls import *
A R Karthick22aa0c62016-05-31 11:17:12 -070037from scapy_ssl_tls.ssl_tls_crypto import *
A R Karthick307483c2016-06-06 17:05:19 -070038from tls_cert import Key
A R Karthicka2e53d62016-02-19 17:38:30 -080039from socket import *
ChetanGaonker6138fcd2016-08-18 17:56:39 -070040from CordTestServer import cord_test_radius_restart
A R Karthick307483c2016-06-06 17:05:19 -070041import struct
A R Karthicka2e53d62016-02-19 17:38:30 -080042import scapy
43from nose.tools import *
44from CordTestBase import CordTester
ChetanGaonker6138fcd2016-08-18 17:56:39 -070045from CordContainer import *
A R Karthick76a497a2017-04-12 10:59:39 -070046from CordTestUtils import log_test
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080047import re
ChetanGaonker6138fcd2016-08-18 17:56:39 -070048import time
A R Karthick22aa0c62016-05-31 11:17:12 -070049
A R Karthick76a497a2017-04-12 10:59:39 -070050log_test.setLevel('INFO')
A R Karthick22aa0c62016-05-31 11:17:12 -070051
52def bytes_to_num(data):
A R Karthick2b93d6a2016-09-06 15:19:09 -070053 try:
54 return int(data.encode('hex'), 16)
55 except:
56 print('Exception')
57 return -1
A R Karthick22aa0c62016-05-31 11:17:12 -070058
A R Karthicka2e53d62016-02-19 17:38:30 -080059class TLSAuthTest(EapolPacket, CordTester):
60
61 tlsStateTable = Enumeration("TLSStateTable", ("ST_EAP_SETUP",
62 "ST_EAP_START",
63 "ST_EAP_ID_REQ",
64 "ST_EAP_TLS_HELLO_REQ",
65 "ST_EAP_TLS_CERT_REQ",
Chetan Gaonkerf8f77182016-03-11 15:34:57 -080066 "ST_EAP_TLS_CHANGE_CIPHER_SPEC",
67 "ST_EAP_TLS_FINISHED",
A R Karthicka2e53d62016-02-19 17:38:30 -080068 "ST_EAP_TLS_DONE"
69 )
70 )
71 tlsEventTable = Enumeration("TLSEventTable", ("EVT_EAP_SETUP",
72 "EVT_EAP_START",
73 "EVT_EAP_ID_REQ",
74 "EVT_EAP_TLS_HELLO_REQ",
75 "EVT_EAP_TLS_CERT_REQ",
Chetan Gaonkerf8f77182016-03-11 15:34:57 -080076 "EVT_EAP_TLS_CHANGE_CIPHER_SPEC",
77 "EVT_EAP_TLS_FINISHED",
A R Karthicka2e53d62016-02-19 17:38:30 -080078 "EVT_EAP_TLS_DONE"
79 )
80 )
A R Karthick22aa0c62016-05-31 11:17:12 -070081 server_hello_done_signature = '\x0e\x00\x00\x00'
82 SERVER_HELLO = '\x02'
83 SERVER_CERTIFICATE = '\x0b'
A R Karthick307483c2016-06-06 17:05:19 -070084 CERTIFICATE_REQUEST = '\x0d'
85 SERVER_HELLO_DONE = '\x0e'
A R Karthick22aa0c62016-05-31 11:17:12 -070086 SERVER_UNKNOWN = '\xff'
87 HANDSHAKE = '\x16'
88 CHANGE_CIPHER = '\x14'
89 TLS_OFFSET = 28
90 HDR_IDX = 0
91 DATA_IDX = 1
92 CB_IDX = 2
A R Karthick307483c2016-06-06 17:05:19 -070093
A R Karthickeeac7e12017-03-10 17:35:39 -080094 #this is from client.crt file
A R Karthick22aa0c62016-05-31 11:17:12 -070095 CLIENT_CERT = """-----BEGIN CERTIFICATE-----
A R Karthickeeac7e12017-03-10 17:35:39 -080096MIIDvTCCAqWgAwIBAgIBBjANBgkqhkiG9w0BAQUFADCBizELMAkGA1UEBhMCVVMx
97CzAJBgNVBAgMAkNBMRIwEAYDVQQHDAlTb21ld2hlcmUxEzARBgNVBAoMCkNpZW5h
98IEluYy4xHjAcBgkqhkiG9w0BCQEWD2FkbWluQGNpZW5hLmNvbTEmMCQGA1UEAwwd
99RXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTcwMzExMDA0NzQ0WhcN
100MjIxMDMxMDA0NzQ0WjBnMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExEzARBgNV
101BAoMCkNpZW5hIEluYy4xFzAVBgNVBAMMDnVzZXJAY2llbmEuY29tMR0wGwYJKoZI
A R Karthickaa10a202016-08-15 15:06:21 -0700102hvcNAQkBFg51c2VyQGNpZW5hLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
A R Karthickeeac7e12017-03-10 17:35:39 -0800103AQoCggEBAOTxi5+TUuaosUh7f/4U9unLV3qHCC7Sf3e0o8F+FP4BVQiIslUTEupw
104gd3VkiVAhxX74Oc3w7XF98msKx7dTetpQaZPZgEgGiDmzGzlAGkwqD4FO5JotJMg
105I0rpcYw+M/z+WDVgJx3I+VGyC6lFKhIJWTlTySheyMYpcgSvOuN4z8keCNg2uaRZ
106IOvIf6aUCTF7fp2HC/468/3T6R/VuYKFNdpsyU1ogbQqCUL4WHM2uWz6G7rzXI0/
107skkKemoGouFw+0I3ydPpGpiWK4NpPKHax4dRGaO1NmTDCtrJOAKPQx8CYYH0HMNp
108BbYvidTnHBxYUBrWNpzOJ/xgopawA8kCAwEAAaNPME0wEwYDVR0lBAwwCgYIKwYB
A R Karthickaa10a202016-08-15 15:06:21 -0700109BQUHAwIwNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL3d3dy5leGFtcGxlLmNvbS9l
A R Karthickeeac7e12017-03-10 17:35:39 -0800110eGFtcGxlX2NhLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAosa5wV/RA2XO/SVGWx5K
111JkQNBn6zKuLRODdkm1UwpkdSZpYoXw2ds6YPPbxV9gFeitCcPZQUho38Fg1LYrNW
1129UsDPC2HAYSxrvCinTLC2NuqLsPj7NSu+41l37hGVG4WTjPXPqet33e0mqz1eYVn
1133SsGvZl98sgG6ADD1RqUb0gpEocmoN+yx5W0hIAwZQPBt4nfAWdGH9AcJTL/Gjr7
1145m4p3cQYm2AnROp+Bim4AELv02WIl77vYWivlzok1JfZ38GL94+CMLOSKNv9OLjc
115M/uh5Q6PB+4xv8kAjmO/Fq9T6f0KcQNNPY63omZCjrFiMEMvoOD4GIg+WV2SKLsQ
1160g==
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800117-----END CERTIFICATE-----"""
A R Karthick22aa0c62016-05-31 11:17:12 -0700118
A R Karthickeeac7e12017-03-10 17:35:39 -0800119 #this is from client.key
A R Karthick307483c2016-06-06 17:05:19 -0700120 CLIENT_PRIV_KEY = """-----BEGIN RSA PRIVATE KEY-----
A R Karthickeeac7e12017-03-10 17:35:39 -0800121MIIEowIBAAKCAQEA5PGLn5NS5qixSHt//hT26ctXeocILtJ/d7SjwX4U/gFVCIiy
122VRMS6nCB3dWSJUCHFfvg5zfDtcX3yawrHt1N62lBpk9mASAaIObMbOUAaTCoPgU7
123kmi0kyAjSulxjD4z/P5YNWAnHcj5UbILqUUqEglZOVPJKF7IxilyBK8643jPyR4I
1242Da5pFkg68h/ppQJMXt+nYcL/jrz/dPpH9W5goU12mzJTWiBtCoJQvhYcza5bPob
125uvNcjT+ySQp6agai4XD7QjfJ0+kamJYrg2k8odrHh1EZo7U2ZMMK2sk4Ao9DHwJh
126gfQcw2kFti+J1OccHFhQGtY2nM4n/GCilrADyQIDAQABAoIBAF+xepvDl3Yj8p4K
127SPBp0N7eCH2FbW0svOzLC1t4GMwmwGUlxex7YX+ucQnJGCIL+6q7skDS9THIQo8A
128MLHg7I3GXBNowokb4u/3qGCnw2k0Vk4+H61NqJSKbVYFh1mIMnC/2xzMSO1RMKzu
129D6O77h7F245zr/P40lDJyAefOq0S6sgZqxmHmoRTHhp0tXV4mkzV7P7RqoJrvAiG
130tGMk5OfdoILnNfoeXNC50nw201UK7xhrrqqlAWZRAaUZJtsb1GxW+jOra6OtsCVg
131AKS/FxzUGMsoqluM5mHLBEN69DAvBBs8g7UVKdmCDZC+feJ31eAnPAoc1gxBHdQU
132pCnA8dECgYEA+Q6X80gnoyG0g66Gh62R7RgDLYPpgkZimLeoX49mwizAUkeSf/Mj
133raVajRmJ8J1n4UklHdQe0PE9Jhuxo4Uo9sP71ZqpQPEvN35/Sw0xxQHcwxD73SWa
134UEVsnWIDJ6QrkoBOhjDMM6tyDSPVDS23352E6sZ9EU45qWvncb5OTdUCgYEA61Np
135Qs/CpEWtPG8IiEPKPEWUEwoO8SS6C4R/UfXNC96GhfIpA4Uy3fQwTUtHEMPL+7lh
136SPFPQDBH90jOTYg30EfHiBMlKW4l21XS+PotTP3ktqZMgx06SnoM2a/+crpzFqkb
137i4eAPCsdTispElbtqleLuUbFO9aG3jHMsK2RtCUCgYB04G9YYL0RJgkTXryNQVvg
138ussK+gOD+kncxxtBtQcRCnU6Z5INb2mH3LgwzRJZk1SjeXLsm5XWkc8Tah2j0iKW
139IwS0if7xlf2Felx8OPXpMOWLuRWpAzN2hg3hkZRPbxBvkLzI5m99s/Ay0GTz6UeH
140reEpV/prO519r0COtTMD/QKBgCdRinbVS8oysh002BIccX/ciD8eIRz9a/BctQE2
141tonTJlre+SdTbApVsHRZrYgJjt2CPdT4LKum5X9VtNKTfe/Y7me3+y+O7dhV4Kgk
1429Mi2ay5xXrtReNnUxqzgkP0OVghlPOr1OuHSulTDNVuRFqitc/UC9BVpZKNfYrnq
143ZjvZAoGBALzgzXajgIdhghCt5PFLkhh3xyOliTXWFstHcMZdQF2wQyeF/uQ2zrC/
1442t1Sa+egV3QDUPYzW9YLQs9eaLh7MS9wCHLY2SMElAqYiNjRfkT4wWdPfeyFx4+E
145Euwtu+lPJ7sEpNu5jX63OS2AeZsQYlsT0Ai+lB4TeyoE6Pj04iC0
A R Karthick307483c2016-06-06 17:05:19 -0700146-----END RSA PRIVATE KEY-----"""
147
A R Karthick22aa0c62016-05-31 11:17:12 -0700148 def handle_server_hello_done(self, server_hello_done):
149 if server_hello_done[-4:] == self.server_hello_done_signature:
A R Karthick43613a72017-06-13 11:05:15 -0700150 log_test.info('server hello received over interface %s' %self.intf)
A R Karthick22aa0c62016-05-31 11:17:12 -0700151 self.server_hello_done_received = True
152
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700153 def __init__(self, intf = 'veth0', client_cert = None, client_priv_key = None,
154 fail_cb = None, src_mac='default', version = "TLS_1_0", session_id = '',
155 session_id_length = None, gmt_unix_time=1234, invalid_content_type = 22,
156 record_fragment_length = None, cipher_suites_length = None,
157 compression_methods_length = None, compression_methods = TLSCompressionMethod.NULL,
158 CipherSuite = True, cipher_suite = 'RSA_WITH_AES_256_CBC_SHA', id_mismatch_in_identifier_response_packet = False,
159 id_mismatch_in_client_hello_packet = False , dont_send_client_certificate = False,
160 dont_send_client_hello = False, restart_radius = False, invalid_client_hello_handshake_type = False,
161 invalid_cert_req_handshake = False, incorrect_tlsrecord_type_cert_req = False,
162 invalid_client_hello_handshake_length = False, clientkeyex_replace_with_serverkeyex = False):
163
A R Karthick22aa0c62016-05-31 11:17:12 -0700164 self.fsmTable = tlsAuthHolder.initTlsAuthHolderFsmTable(self, self.tlsStateTable, self.tlsEventTable)
165 EapolPacket.__init__(self, intf)
166 CordTester.__init__(self, self.fsmTable, self.tlsStateTable.ST_EAP_TLS_DONE)
167 #self.tlsStateTable, self.tlsEventTable)
168 self.currentState = self.tlsStateTable.ST_EAP_SETUP
169 self.currentEvent = self.tlsEventTable.EVT_EAP_SETUP
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700170 self.src_mac = src_mac
171 self.version = version
172 self.session_id_length = session_id_length
173 self.session_id = session_id
174 self.gmt_unix_time = gmt_unix_time
175 self.invalid_content_type = invalid_content_type
176 self.CipherSuite = CipherSuite
177 self.cipher_suites_length = cipher_suites_length
178 self.compression_methods_length = compression_methods_length
179 self.cipher_suite = cipher_suite
180 self.compression_methods_length = compression_methods_length
181 self.compression_methods = compression_methods
182 self.record_fragment_length = record_fragment_length
183 self.invalid_client_hello_handshake_type = invalid_client_hello_handshake_type
184 self.invalid_client_hello_handshake_length = invalid_client_hello_handshake_length
185 self.invalid_cert_req_handshake = invalid_cert_req_handshake
186 self.id_mismatch_in_identifier_response_packet = id_mismatch_in_identifier_response_packet
187 self.id_mismatch_in_client_hello_packet = id_mismatch_in_client_hello_packet
188 self.dont_send_client_certificate = dont_send_client_certificate
189 self.dont_send_client_hello = dont_send_client_hello
190 self.incorrect_tlsrecord_type_cert_req = incorrect_tlsrecord_type_cert_req
191 self.restart_radius = restart_radius
192 self.clientkeyex_replace_with_serverkeyex = clientkeyex_replace_with_serverkeyex
A R Karthick22aa0c62016-05-31 11:17:12 -0700193 self.nextState = None
194 self.nextEvent = None
195 self.pending_bytes = 0 #for TLS fragment reassembly
196 self.server_hello_done_received = False
A R Karthickf028ec92016-08-15 16:58:11 -0700197 self.server_hello_done_eap_id = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700198 self.send_tls_response = True
199 self.server_certs = []
200 self.pkt_last = ''
201 self.pkt_history = []
202 self.pkt_map = { self.SERVER_HELLO: ['', '', lambda pkt: pkt ],
203 self.SERVER_CERTIFICATE: ['', '', lambda pkt: pkt ],
A R Karthick307483c2016-06-06 17:05:19 -0700204 self.CERTIFICATE_REQUEST: ['', '', lambda pkt: pkt ],
A R Karthick22aa0c62016-05-31 11:17:12 -0700205 self.SERVER_HELLO_DONE: ['', '', self.handle_server_hello_done ],
206 self.SERVER_UNKNOWN: ['', '', lambda pkt: pkt ]
207 }
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700208 if self.clientkeyex_replace_with_serverkeyex:
209 self.tls_ctx = TLSSessionCtx(client = False)
210 else:
211 self.tls_ctx = TLSSessionCtx(client = True)
A R Karthick05d9b5f2016-06-08 11:53:54 -0700212 self.client_cert = self.CLIENT_CERT if client_cert is None else client_cert
213 self.client_priv_key = self.CLIENT_PRIV_KEY if client_priv_key is None else client_priv_key
214 self.failTest = False
215 self.fail_cb = fail_cb
A R Karthick22aa0c62016-05-31 11:17:12 -0700216
217 def load_tls_record(self, data, pkt_type = ''):
A R Karthick307483c2016-06-06 17:05:19 -0700218 #if pkt_type not in [ self.SERVER_HELLO_DONE, self.SERVER_UNKNOWN ]:
219 if pkt_type == self.SERVER_HELLO_DONE:
220 data = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/data)
221 elif pkt_type == self.CERTIFICATE_REQUEST:
222 data = str(TLSRecord()/TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/data[9:])
223 data = None #For now ignore this record
224 if data:
A R Karthick22aa0c62016-05-31 11:17:12 -0700225 TLS(data, ctx = self.tls_ctx)
226
227 def pkt_update(self, pkt_type, data, hdr=None, reassembled = False):
228 if not self.pkt_map.has_key(pkt_type):
229 return
230 if hdr is not None:
231 self.pkt_map[pkt_type][self.HDR_IDX] += hdr
232 self.pkt_map[pkt_type][self.DATA_IDX] += data
233 if reassembled is True:
234 self.pkt_map[pkt_type][self.CB_IDX](self.pkt_map[pkt_type][self.DATA_IDX])
A R Karthick43613a72017-06-13 11:05:15 -0700235 log_test.info('Interface %s, Appending packet type %02x to packet history of len %d'
236 %(self.intf, ord(pkt_type), len(self.pkt_map[pkt_type][self.DATA_IDX])))
A R Karthick22aa0c62016-05-31 11:17:12 -0700237 self.pkt_history.append(self.pkt_map[pkt_type][self.DATA_IDX])
238 data = ''.join(self.pkt_map[pkt_type][:self.DATA_IDX+1])
239 self.load_tls_record(data, pkt_type = pkt_type)
240 self.pkt_map[pkt_type][self.HDR_IDX] = ''
241 self.pkt_map[pkt_type][self.DATA_IDX] = ''
242
A R Karthick05d9b5f2016-06-08 11:53:54 -0700243 def tlsFail(self):
244 ##Force a failure
A R Karthick43613a72017-06-13 11:05:15 -0700245 log_test.info('entering into testFail function for interface %s' %self.intf)
A R Karthick05d9b5f2016-06-08 11:53:54 -0700246 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
A.R Karthick7eb2ce02016-06-10 19:00:50 -0700247 self.nextState = self.tlsStateTable.ST_EAP_TLS_FINISHED
A R Karthick05d9b5f2016-06-08 11:53:54 -0700248 self.failTest = True
249
A R Karthick22aa0c62016-05-31 11:17:12 -0700250 def eapol_server_hello_cb(self, pkt):
251 '''Reassemble and send response for server hello/certificate fragments'''
252 r = str(pkt)
253 offset = self.TLS_OFFSET
254 tls_data = r[offset:]
A R Karthick307483c2016-06-06 17:05:19 -0700255 type_hdrlen = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700256 if self.pending_bytes > 0:
257 if len(tls_data) >= self.pending_bytes:
258 self.pkt_update(self.pkt_last, tls_data[:self.pending_bytes], reassembled = True)
259 offset += self.pending_bytes
260 self.pkt_last = ''
261 self.pending_bytes = 0
262 else:
263 self.pkt_update(self.pkt_last, tls_data)
264 self.pending_bytes -= len(tls_data)
A R Karthick43613a72017-06-13 11:05:15 -0700265 print('Interface: %s, Offset: %d, pkt : %d, pending %d\n' %(self.intf, offset, len(pkt), self.pending_bytes))
A R Karthick22aa0c62016-05-31 11:17:12 -0700266 while self.pending_bytes == 0 and offset < len(pkt):
267 tls_data = r[offset:]
A R Karthick2b93d6a2016-09-06 15:19:09 -0700268 hexdump(tls_data)
A R Karthick22aa0c62016-05-31 11:17:12 -0700269 self.pending_bytes = bytes_to_num(tls_data[3:5])
A R Karthick2b93d6a2016-09-06 15:19:09 -0700270 if self.pending_bytes < 0:
271 self.pending_bytes = 0
272 return
A R Karthick22aa0c62016-05-31 11:17:12 -0700273 if tls_data[0] == self.HANDSHAKE:
274 pkt_type = tls_data[5]
A R Karthick307483c2016-06-06 17:05:19 -0700275 if pkt_type in [ self.CERTIFICATE_REQUEST ]:
276 self.pending_bytes = bytes_to_num(tls_data[6:9])
277 type_hdrlen = 4
278 if len(tls_data) - 5 - type_hdrlen >= self.pending_bytes:
279 data_received = tls_data[5: 5 + type_hdrlen + self.pending_bytes ]
280 offset += 5 + type_hdrlen + self.pending_bytes
281 type_hdrlen = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700282 self.pending_bytes = 0
283 self.pkt_update(pkt_type, data_received,
284 hdr = tls_data[:5],
285 reassembled = True)
286 else:
287 self.pkt_update(pkt_type, tls_data[5:],
288 hdr = tls_data[:5],
289 reassembled = False)
A R Karthick307483c2016-06-06 17:05:19 -0700290 self.pending_bytes -= len(tls_data) - 5 - type_hdrlen
A R Karthick22aa0c62016-05-31 11:17:12 -0700291 self.pkt_last = pkt_type
A R Karthick43613a72017-06-13 11:05:15 -0700292 log_test.info('Interface: %s, Pending bytes left %d' %(self.intf, self.pending_bytes))
A R Karthick22aa0c62016-05-31 11:17:12 -0700293 assert self.pending_bytes > 0
A R Karthick307483c2016-06-06 17:05:19 -0700294 elif tls_data[0] == self.SERVER_HELLO_DONE:
A R Karthickf028ec92016-08-15 16:58:11 -0700295 self.server_hello_done_eap_id = pkt[EAP].id
A R Karthick307483c2016-06-06 17:05:19 -0700296 self.pkt_update(tls_data[0], tls_data, reassembled = True)
297 break
A R Karthick22aa0c62016-05-31 11:17:12 -0700298 else:
299 self.pkt_last = self.SERVER_UNKNOWN
300 if len(tls_data) - 5 >= self.pending_bytes:
301 offset += 5 + self.pending_bytes
302 self.pending_bytes = 0
303 self.pkt_last = ''
304
A R Karthickf028ec92016-08-15 16:58:11 -0700305 #send TLS response ack till we receive server hello done
306 if self.server_hello_done_received == False:
A R Karthick22aa0c62016-05-31 11:17:12 -0700307 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, '')
308 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
309
310 def _eapSetup(self):
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700311 #if self.src_mac == 'bcast':self.setup(src_mac='bcast')
312 #if self.src_mac == 'mcast': self.setup(src_mac='mcast')
313 #if self.src_mac == 'zeros': self.setup(src_mac='zeros')
314 #if self.src_mac == 'default': self.setup(src_mac='default')
A R Karthick43613a72017-06-13 11:05:15 -0700315 #log_test.info('Interface: %s, Source mac is %s' %(self.intf, self.src_mac))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700316 self.setup(src_mac=self.src_mac)
A R Karthick22aa0c62016-05-31 11:17:12 -0700317 self.nextEvent = self.tlsEventTable.EVT_EAP_START
A R Karthick05d9b5f2016-06-08 11:53:54 -0700318
A R Karthick22aa0c62016-05-31 11:17:12 -0700319 def _eapStart(self):
A R Karthick43613a72017-06-13 11:05:15 -0700320 log_test.info('_eapStart method started over interface %s' %(self.intf))
A R Karthick22aa0c62016-05-31 11:17:12 -0700321 self.eapol_start()
322 self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ
323
324 def _eapIdReq(self):
A R Karthick43613a72017-06-13 11:05:15 -0700325 log_test.info('Inside EAP ID Req for interface %s' %(self.intf))
A R Karthick22aa0c62016-05-31 11:17:12 -0700326 def eapol_cb(pkt):
A R Karthick43613a72017-06-13 11:05:15 -0700327 log_test.info('Got EAPOL packet with type id and code request for interface %s' %(self.intf))
328 log_test.info('Interface: %s, Packet code: %d, type: %d, id: %d' %(self.intf, pkt[EAP].code, pkt[EAP].type, pkt[EAP].id))
329 log_test.info("Send EAP Response with identity %s over interface %s" % (USER, self.intf))
ChetanGaonkere2b665b2016-12-07 00:50:56 -0800330 if self.id_mismatch_in_identifier_response_packet:
A R Karthick43613a72017-06-13 11:05:15 -0700331 log_test.info('\nSending invalid id field in EAP Identity Response packet over interface %s' %(self.intf))
ChetanGaonkere2b665b2016-12-07 00:50:56 -0800332 self.eapol_id_req(pkt[EAP].id+10, USER)
333 else:
334 self.eapol_id_req(pkt[EAP].id, USER)
A R Karthick22aa0c62016-05-31 11:17:12 -0700335
A R Karthick05d9b5f2016-06-08 11:53:54 -0700336 r = self.eapol_scapy_recv(cb = eapol_cb,
337 lfilter =
338 lambda pkt: EAP in pkt and pkt[EAP].type == EAP.TYPE_ID and pkt[EAP].code == EAP.REQUEST)
339 if len(r) > 0:
340 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_HELLO_REQ
341 else:
342 self.tlsFail()
343 return r
A R Karthick22aa0c62016-05-31 11:17:12 -0700344
345 def _eapTlsHelloReq(self):
346
347 def eapol_cb(pkt):
A R Karthick43613a72017-06-13 11:05:15 -0700348 log_test.info('Got hello request for id %d over interface %s', pkt[EAP].id, self.intf)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700349 self.client_hello = TLSClientHello(version= self.version,
350 gmt_unix_time=self.gmt_unix_time,
A R Karthick22aa0c62016-05-31 11:17:12 -0700351 random_bytes= '\xAB' * 28,
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700352 session_id_length = self.session_id_length,
353 session_id= self.session_id,
354 compression_methods_length = self.compression_methods_length,
355 compression_methods= self.compression_methods,
356 cipher_suites_length = self.cipher_suites_length,
357 cipher_suites=[self.cipher_suite]
A R Karthick22aa0c62016-05-31 11:17:12 -0700358 )
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700359 if self.invalid_client_hello_handshake_type:
A R Karthick76a497a2017-04-12 10:59:39 -0700360 log_test.info('sending server_hello instead of client_hello handshape type in client hello packet')
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700361 client_hello_data = TLSHandshake(type='server_hello')/self.client_hello
362 elif self.invalid_client_hello_handshake_length:
A R Karthick76a497a2017-04-12 10:59:39 -0700363 log_test.info('sending TLS Handshake message with zero length field in client hello packet')
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700364 client_hello_data = TLSHandshake(length=0)/self.client_hello
365 else:
366 client_hello_data = TLSHandshake()/self.client_hello
367 #client_hello_data = TLSHandshake()/self.client_hello
A R Karthick307483c2016-06-06 17:05:19 -0700368 self.pkt_history.append( str(client_hello_data) )
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700369 if self.record_fragment_length:
370 reqdata = TLSRecord(length=self.record_fragment_length)/client_hello_data
371 else:
372 reqdata = TLSRecord()/client_hello_data
A R Karthick22aa0c62016-05-31 11:17:12 -0700373 self.load_tls_record(str(reqdata))
A R Karthick43613a72017-06-13 11:05:15 -0700374 log_test.info("Sending Client Hello TLS payload of len %d, id %d over interface %s" %(len(reqdata),pkt[EAP].id, self.intf))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700375 if self.id_mismatch_in_client_hello_packet:
A R Karthick76a497a2017-04-12 10:59:39 -0700376 log_test.info('\nsending invalid id field in client hello packet')
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700377 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id+10, TLS_LENGTH_INCLUDED, str(reqdata))
378 else:
379 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, str(reqdata))
380 if self.dont_send_client_hello:
A R Karthick76a497a2017-04-12 10:59:39 -0700381 log_test.info('\nskipping client hello packet sending part')
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700382 pass
383 else:
384 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
385 if self.restart_radius:
386 cord_test_radius_restart()
A R Karthick22aa0c62016-05-31 11:17:12 -0700387
A R Karthick05d9b5f2016-06-08 11:53:54 -0700388 r = self.eapol_scapy_recv(cb = eapol_cb,
A R Karthick22aa0c62016-05-31 11:17:12 -0700389 lfilter =
390 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 -0700391
392 if len(r) == 0:
393 self.tlsFail()
394 return r
395
A R Karthickf028ec92016-08-15 16:58:11 -0700396 #move to client/server certificate request
A R Karthick22aa0c62016-05-31 11:17:12 -0700397 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ
398
A R Karthick307483c2016-06-06 17:05:19 -0700399 def get_verify_data(self):
A R Karthick22aa0c62016-05-31 11:17:12 -0700400 all_handshake_pkts = ''.join(self.pkt_history)
A R Karthick307483c2016-06-06 17:05:19 -0700401 return self.tls_ctx.get_verify_data(data = all_handshake_pkts)
402
403 def get_verify_signature(self, pem_data):
404 all_handshake_pkts = ''.join(self.pkt_history)
405 k = Key(pem_data)
406 signature = k.sign(all_handshake_pkts, t = 'pkcs', h = 'tls')
407 signature_data = '{}{}'.format(struct.pack('!H', len(signature)), signature)
408 return signature_data
409
410 def get_encrypted_handshake_msg(self, finish_val=''):
A R Karthick22aa0c62016-05-31 11:17:12 -0700411 if not finish_val:
A R Karthick307483c2016-06-06 17:05:19 -0700412 finish_val = self.get_verify_data()
A R Karthick22aa0c62016-05-31 11:17:12 -0700413 msg = str(TLSHandshake(type=TLSHandshakeType.FINISHED)/finish_val)
414 crypto_container = CryptoContainer(self.tls_ctx, data = msg,
415 content_type = TLSContentType.HANDSHAKE)
416 return crypto_container.encrypt()
417
A R Karthick307483c2016-06-06 17:05:19 -0700418 def get_encrypted_application_msg(self, msg = ''):
419 '''Needed with tunneled TLS'''
420 if not msg:
421 msg = 'test data'
422 return to_raw(TLSPlaintext(data = 'GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n'), self.tls_ctx)
423
A R Karthick22aa0c62016-05-31 11:17:12 -0700424 def _eapTlsCertReq(self):
A R Karthick43613a72017-06-13 11:05:15 -0700425 log_test.info('Receiving server certificates over interface %s', self.intf)
A R Karthickf028ec92016-08-15 16:58:11 -0700426 while self.server_hello_done_received == False:
427 r = self.eapol_scapy_recv(cb = self.eapol_server_hello_cb,
428 lfilter =
429 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and \
430 pkt[EAP].code == EAP.REQUEST)
431 if len(r) == 0:
432 self.tlsFail()
433 return r
A R Karthick43613a72017-06-13 11:05:15 -0700434 log_test.info('Sending client certificate request over interface %s', self.intf)
A R Karthickf028ec92016-08-15 16:58:11 -0700435 rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL)
436 if self.client_cert:
437 der_cert = rex_pem.findall(self.client_cert)[0].decode("base64")
438 client_certificate_list = TLSHandshake()/TLSCertificateList(
439 certificates=[TLSCertificate(data=x509.X509Cert(der_cert))])
A R Karthick05d9b5f2016-06-08 11:53:54 -0700440 else:
A R Karthickf028ec92016-08-15 16:58:11 -0700441 client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[])
442 client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700443 kex_data = self.tls_ctx.get_client_kex_data()
A R Karthickf028ec92016-08-15 16:58:11 -0700444 client_key_ex_data = TLSHandshake()/kex_data
445 client_key_ex = TLSRecord()/client_key_ex_data
446 if self.client_cert:
447 self.load_tls_record(str(client_certificate))
448 self.pkt_history.append(str(client_certificate_list))
449 self.load_tls_record(str(client_key_ex))
450 self.pkt_history.append(str(client_key_ex_data))
451 verify_signature = self.get_verify_signature(self.client_priv_key)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700452 if self.invalid_cert_req_handshake:
A R Karthick76a497a2017-04-12 10:59:39 -0700453 log_test.info("sending 'certificate-request' type of handshake message instead of 'certificate-verify' type")
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700454 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/verify_signature
455 else:
456 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature
457 if self.incorrect_tlsrecord_type_cert_req:
A R Karthick76a497a2017-04-12 10:59:39 -0700458 log_test.info("sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet")
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700459 client_cert_record = TLSRecord(content_type=TLSContentType.ALERT)/client_cert_verify
460 else:
461 client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify
A R Karthickf028ec92016-08-15 16:58:11 -0700462 self.pkt_history.append(str(client_cert_verify))
A R Karthick43613a72017-06-13 11:05:15 -0700463 #log_test.info('Interface: %s, TLS ctxt: %s' %(self.intf, self.tls_ctx))
A R Karthickf028ec92016-08-15 16:58:11 -0700464 client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec()
465 enc_handshake_msg = self.get_encrypted_handshake_msg()
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700466 if self.invalid_content_type:
467 handshake_msg = str(TLSRecord(content_type=self.invalid_content_type)/enc_handshake_msg)
468 else:
469 handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg)
A R Karthickf028ec92016-08-15 16:58:11 -0700470 reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs]))
471 reqdata += handshake_msg
A R Karthick43613a72017-06-13 11:05:15 -0700472 log_test.info("Sending Client Hello TLS Certificate payload of len %d over interface %s" %(len(reqdata), self.intf))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700473 if self.dont_send_client_certificate:
A R Karthick76a497a2017-04-12 10:59:39 -0700474 log_test.info('\nskipping sending client certificate part')
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700475 pass
476 else:
477 status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED,
A R Karthickf028ec92016-08-15 16:58:11 -0700478 payload = reqdata, fragsize = 1024)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700479 assert_equal(status, True)
480 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800481
ChetanGaonker3441faf2016-10-27 12:15:21 -0700482 def _eapTlsCertReq_delay(self):
483 self.server_hello_done_received = True
A R Karthick43613a72017-06-13 11:05:15 -0700484 log_test.info('Sending client certificate request over interface %s', self.intf)
ChetanGaonker3441faf2016-10-27 12:15:21 -0700485 rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL)
486
487 if self.client_cert:
488 der_cert = rex_pem.findall(self.client_cert)[0].decode("base64")
489 client_certificate_list = TLSHandshake()/TLSCertificateList(
490 certificates=[TLSCertificate(data=x509.X509Cert(der_cert))])
491 else:
492 client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[])
493
494 client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list
495 kex_data = self.tls_ctx.get_client_kex_data()
496 client_key_ex_data = TLSHandshake()/kex_data
497 client_key_ex = TLSRecord()/client_key_ex_data
498
499 if self.client_cert:
500 self.load_tls_record(str(client_certificate))
501 self.pkt_history.append(str(client_certificate_list))
502
503 self.load_tls_record(str(client_key_ex))
504 self.pkt_history.append(str(client_key_ex_data))
505 verify_signature = self.get_verify_signature(self.client_priv_key)
506
507 if self.invalid_cert_req_handshake:
A R Karthick76a497a2017-04-12 10:59:39 -0700508 log_test.info("Sending 'certificate-request' type of handshake message instead of 'certificate-verify' type")
ChetanGaonker3441faf2016-10-27 12:15:21 -0700509 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/verify_signature
510 else:
511 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature
512
513 if self.incorrect_tlsrecord_type_cert_req:
A R Karthick76a497a2017-04-12 10:59:39 -0700514 log_test.info("Sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet")
ChetanGaonker3441faf2016-10-27 12:15:21 -0700515 client_cert_record = TLSRecord(content_type=TLSContentType.ALERT)/client_cert_verify
516 else:
517 client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify
518
519 self.pkt_history.append(str(client_cert_verify))
A R Karthick76a497a2017-04-12 10:59:39 -0700520 #log_test.info('TLS ctxt: %s' %self.tls_ctx)
ChetanGaonker3441faf2016-10-27 12:15:21 -0700521 client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec()
522 enc_handshake_msg = self.get_encrypted_handshake_msg()
523
524 if self.invalid_content_type:
525 handshake_msg = str(TLSRecord(content_type=self.invalid_content_type)/enc_handshake_msg)
526 else:
527 handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg)
528 reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs]))
529 reqdata += handshake_msg
A R Karthick43613a72017-06-13 11:05:15 -0700530 log_test.info("Sending Client Hello TLS Certificate payload of len %d over interface %s" %(len(reqdata), self.intf))
ChetanGaonker3441faf2016-10-27 12:15:21 -0700531
532 if self.dont_send_client_certificate:
A R Karthick76a497a2017-04-12 10:59:39 -0700533 log_test.info('\nSkipping sending client certificate part')
ChetanGaonker3441faf2016-10-27 12:15:21 -0700534 pass
535 else:
536 status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED,
537 payload = reqdata, fragsize = 1024)
538 assert_equal(status, True)
539 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
540
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800541 def _eapTlsChangeCipherSpec(self):
Chetan Gaonker5b366302016-03-21 16:18:21 -0700542 def eapol_cb(pkt):
A R Karthick22aa0c62016-05-31 11:17:12 -0700543 r = str(pkt)
A R Karthick43613a72017-06-13 11:05:15 -0700544 log_test.info('Interface %s. Received data in change cipher spec function is %s'%(self.intf, pkt.show()))
A R Karthick307483c2016-06-06 17:05:19 -0700545 tls_data = r[self.TLS_OFFSET:]
A R Karthick43613a72017-06-13 11:05:15 -0700546 log_test.info('Verifying TLS Change Cipher spec record type %x over interface %s' %(ord(tls_data[0]), self.intf))
A R Karthick22aa0c62016-05-31 11:17:12 -0700547 assert tls_data[0] == self.CHANGE_CIPHER
A R Karthick43613a72017-06-13 11:05:15 -0700548 log_test.info('Handshake finished. Sending empty data over interface %s' %self.intf)
A R Karthick307483c2016-06-06 17:05:19 -0700549 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, 0, '')
550 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
Chetan Gaonker5b366302016-03-21 16:18:21 -0700551
A R Karthick05d9b5f2016-06-08 11:53:54 -0700552 r = self.eapol_scapy_recv(cb = eapol_cb,
553 lfilter =
554 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST)
555 if len(r) > 0:
556 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
557 else:
558 self.tlsFail()
559 return r
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800560
561 def _eapTlsFinished(self):
A.R Karthick7eb2ce02016-06-10 19:00:50 -0700562 self.nextEvent = None
Chetan Gaonker5b366302016-03-21 16:18:21 -0700563 def eapol_cb(pkt):
A R Karthick43613a72017-06-13 11:05:15 -0700564 log_test.info('Server authentication successfull over interface %s' %self.intf)
Chetan Gaonker5b366302016-03-21 16:18:21 -0700565
A R Karthick05d9b5f2016-06-08 11:53:54 -0700566 timeout = 5
567 if self.failTest is True:
568 if self.fail_cb is not None:
569 self.fail_cb()
570 return
571 timeout = None ##Wait forever on failure and force testcase timeouts
572
Chetan Gaonker5b366302016-03-21 16:18:21 -0700573 self.eapol_scapy_recv(cb = eapol_cb,
A R Karthick22aa0c62016-05-31 11:17:12 -0700574 lfilter =
A R Karthick05d9b5f2016-06-08 11:53:54 -0700575 lambda pkt: EAP in pkt and pkt[EAP].code == EAP.SUCCESS,
576 timeout = timeout)
A R Karthick307483c2016-06-06 17:05:19 -0700577 self.eapol_logoff()