blob: 58e9d8dea07429719b1f9b8641795dc9f9680701 [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 Karthick22aa0c62016-05-31 11:17:12 -070077 CLIENT_CERT = """-----BEGIN CERTIFICATE-----
A R Karthickaa10a202016-08-15 15:06:21 -070078MIIDvTCCAqWgAwIBAgIBAjANBgkqhkiG9w0BAQUFADCBizELMAkGA1UEBhMCVVMx
Chetan Gaonkerf8f77182016-03-11 15:34:57 -080079CzAJBgNVBAgTAkNBMRIwEAYDVQQHEwlTb21ld2hlcmUxEzARBgNVBAoTCkNpZW5h
80IEluYy4xHjAcBgkqhkiG9w0BCQEWD2FkbWluQGNpZW5hLmNvbTEmMCQGA1UEAxMd
A R Karthickaa10a202016-08-15 15:06:21 -070081RXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTYwMzExMTg1MzM2WhcN
82MTcwMzA2MTg1MzM2WjBnMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEzARBgNV
Chetan Gaonkerf8f77182016-03-11 15:34:57 -080083BAoTCkNpZW5hIEluYy4xFzAVBgNVBAMUDnVzZXJAY2llbmEuY29tMR0wGwYJKoZI
A R Karthickaa10a202016-08-15 15:06:21 -070084hvcNAQkBFg51c2VyQGNpZW5hLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
85AQoCggEBAOxemcBsPn9tZsCa5o2JA6sQDC7A6JgCNXXl2VFzKLNNvB9PS6D7ZBsQ
865An0zEDMNzi51q7lnrYg1XyiE4S8FzMGAFr94RlGMQJUbRD9V/oqszMX4k++iAOK
87tIA1gr3x7Zi+0tkjVSVzXTmgNnhChAamdMsjYUG5+CY9WAicXyy+VEV3zTphZZDR
88OjcjEp4m/TSXVPYPgYDXI40YZKX5BdvqykWtT/tIgZb48RS1NPyN/XkCYzl3bv21
89qx7Mc0fcEbsJBIIRYTUkfxnsilcnmLxSYO+p+DZ9uBLBzcQt+4Rd5pLSfi21WM39
902Z2oOi3vs/OYAPAqgmi2JWOv3mePa/8CAwEAAaNPME0wEwYDVR0lBAwwCgYIKwYB
91BQUHAwIwNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL3d3dy5leGFtcGxlLmNvbS9l
92eGFtcGxlX2NhLmNybDANBgkqhkiG9w0BAQUFAAOCAQEALBzMPDTIB6sLyPl0T6JV
93MjOkyldAVhXWiQsTjaGQGJUUe1cmUJyZbUZEc13MygXMPOM4x7z6VpXGuq1c/Vxn
94VzQ2fNnbJcIAHi/7G8W5/SQfPesIVDsHTEc4ZspPi5jlS/MVX3HOC+BDbOjdbwqP
95RX0JEr+uOyhjO+lRxG8ilMRACoBUbw1eDuVDoEBgErSUC44pq5ioDw2xelc+Y6hQ
96dmtYwfY0DbvwxHtA495frLyPcastDiT/zre7NL51MyUDPjjYjghNQEwvu66IKbQ3
97T1tJBrgI7/WI+dqhKBFolKGKTDWIHsZXQvZ1snGu/FRYzg1l+R/jT8cRB9BDwhUt
98yg==
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080099-----END CERTIFICATE-----"""
A R Karthick22aa0c62016-05-31 11:17:12 -0700100
A R Karthick307483c2016-06-06 17:05:19 -0700101 CLIENT_PRIV_KEY = """-----BEGIN RSA PRIVATE KEY-----
A R Karthickaa10a202016-08-15 15:06:21 -0700102MIIEpAIBAAKCAQEA7F6ZwGw+f21mwJrmjYkDqxAMLsDomAI1deXZUXMos028H09L
103oPtkGxDkCfTMQMw3OLnWruWetiDVfKIThLwXMwYAWv3hGUYxAlRtEP1X+iqzMxfi
104T76IA4q0gDWCvfHtmL7S2SNVJXNdOaA2eEKEBqZ0yyNhQbn4Jj1YCJxfLL5URXfN
105OmFlkNE6NyMSnib9NJdU9g+BgNcjjRhkpfkF2+rKRa1P+0iBlvjxFLU0/I39eQJj
106OXdu/bWrHsxzR9wRuwkEghFhNSR/GeyKVyeYvFJg76n4Nn24EsHNxC37hF3mktJ+
107LbVYzf3Znag6Le+z85gA8CqCaLYlY6/eZ49r/wIDAQABAoIBAQC9Oha4l2+JMBoc
108g2WjVVcccWJvY3xRhSMrWXEa5ahlswuxvRd8rwS7LlCBL/r8vQBQZ2ZY6fafM7X1
109awNZNgMUk+9g6PJ1+11s0g3mlgsCeYCwnKRO1ueofjh2k2AxlCZ0LAA8WS7nJm4x
110nfM9X3K2qDfEEHTh23Gvm1iIvDbtZ3+kXnjsdAuYduiaDDPNSyNOSCe2eCt2d9vt
111twV5pEf0PXcuLJ29i2LkRKdPwz/1J/AAE0dTJS9lrlLKE9qWXO2my4eUQI2FXVzW
112RpxhjGoFNXa59okobZ555rRrp4LHe8HPx5aowLSS5HGGrXHpiyYpFR9uciQxMD6q
113BQgmim5RAoGBAP09mWJS2gyiB9xqNY8MYyTrldXUIfujJ5OZch088rmbdS2p3TvG
114Oy6K0rOufBMCl95Ncv6YQ7cjQKpq8Q7fTfPkRI3/994DZu5D+vwyqIZpBnHDAnTi
115R9kf1Ep1QHmJPPE1GcijTnksaXP5g9+me2fTi4cCcl+An8GPv06z/KKjAoGBAO7x
1168MH1Dy2zAJhvlPbXdQLa/6b5aQeEVqObnJUvEaEy4M3c0QakULTDVQjsu/+ONNNV
1170Z5ZsBIWe/LaXxenub9lRJpD4KZOdz5bYIIq+Oa8L4bqTvyB/pVcZOE5a4ANvGiC
1184rVdAenPu734skgDFQPNZWKi/T6OZyJYgNzHG4L1AoGAEugvdHzVFM5gId+4Ifb9
119y/re0/kjlGMJCGcTcwVi5eKqa/9bqiPRtVbeBlZHoy+1YP6NUF7T5291W4PifYbE
120jioDyEpNGkFMxQtESOILXQWoWoQBwfJHBPnwYqLAbpKFf0jEpQs0R62+Lc96Pg9y
1219TyBFVJkcabrxorR8LFVclsCgYB8+eJ5MBneRy/aixIZAZxb//uTdAQxQFCohi2i
122Adpwu9HFGufhV3Q296u0XU3/XnvWxZ47+qES9Nujq//suXd32hnFrhcEJSpNXTHf
123I2bIGEmrgUYK4Fst+ANzobrOYWDYMQ0u2xSzHEoQFNH6xFHriTSsIJ/gZk8fMbdE
124wodrOQKBgQCOsFLo97vhlv6abA4v0T6bXuq4pzedIEh3bkqC/8rpLxqG98VoymHM
125bZIYf0U0KK3aNVfyXkIjGBaqA9/A0ttx/guOacf8M3yXbl3uEqlKevJTjhWlbUjp
126fM2med+fZ0+bh4DZ3O8BUJ1+6dxHngF/86GlwxTK4iSRkLIv6n3YSA==
A R Karthick307483c2016-06-06 17:05:19 -0700127-----END RSA PRIVATE KEY-----"""
128
A R Karthick22aa0c62016-05-31 11:17:12 -0700129 def handle_server_hello_done(self, server_hello_done):
130 if server_hello_done[-4:] == self.server_hello_done_signature:
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700131 log.info('server hello received')
A R Karthick22aa0c62016-05-31 11:17:12 -0700132 self.server_hello_done_received = True
133
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700134 def __init__(self, intf = 'veth0', client_cert = None, client_priv_key = None,
135 fail_cb = None, src_mac='default', version = "TLS_1_0", session_id = '',
136 session_id_length = None, gmt_unix_time=1234, invalid_content_type = 22,
137 record_fragment_length = None, cipher_suites_length = None,
138 compression_methods_length = None, compression_methods = TLSCompressionMethod.NULL,
139 CipherSuite = True, cipher_suite = 'RSA_WITH_AES_256_CBC_SHA', id_mismatch_in_identifier_response_packet = False,
140 id_mismatch_in_client_hello_packet = False , dont_send_client_certificate = False,
141 dont_send_client_hello = False, restart_radius = False, invalid_client_hello_handshake_type = False,
142 invalid_cert_req_handshake = False, incorrect_tlsrecord_type_cert_req = False,
143 invalid_client_hello_handshake_length = False, clientkeyex_replace_with_serverkeyex = False):
144
A R Karthick22aa0c62016-05-31 11:17:12 -0700145 self.fsmTable = tlsAuthHolder.initTlsAuthHolderFsmTable(self, self.tlsStateTable, self.tlsEventTable)
146 EapolPacket.__init__(self, intf)
147 CordTester.__init__(self, self.fsmTable, self.tlsStateTable.ST_EAP_TLS_DONE)
148 #self.tlsStateTable, self.tlsEventTable)
149 self.currentState = self.tlsStateTable.ST_EAP_SETUP
150 self.currentEvent = self.tlsEventTable.EVT_EAP_SETUP
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700151 self.src_mac = src_mac
152 self.version = version
153 self.session_id_length = session_id_length
154 self.session_id = session_id
155 self.gmt_unix_time = gmt_unix_time
156 self.invalid_content_type = invalid_content_type
157 self.CipherSuite = CipherSuite
158 self.cipher_suites_length = cipher_suites_length
159 self.compression_methods_length = compression_methods_length
160 self.cipher_suite = cipher_suite
161 self.compression_methods_length = compression_methods_length
162 self.compression_methods = compression_methods
163 self.record_fragment_length = record_fragment_length
164 self.invalid_client_hello_handshake_type = invalid_client_hello_handshake_type
165 self.invalid_client_hello_handshake_length = invalid_client_hello_handshake_length
166 self.invalid_cert_req_handshake = invalid_cert_req_handshake
167 self.id_mismatch_in_identifier_response_packet = id_mismatch_in_identifier_response_packet
168 self.id_mismatch_in_client_hello_packet = id_mismatch_in_client_hello_packet
169 self.dont_send_client_certificate = dont_send_client_certificate
170 self.dont_send_client_hello = dont_send_client_hello
171 self.incorrect_tlsrecord_type_cert_req = incorrect_tlsrecord_type_cert_req
172 self.restart_radius = restart_radius
173 self.clientkeyex_replace_with_serverkeyex = clientkeyex_replace_with_serverkeyex
A R Karthick22aa0c62016-05-31 11:17:12 -0700174 self.nextState = None
175 self.nextEvent = None
176 self.pending_bytes = 0 #for TLS fragment reassembly
177 self.server_hello_done_received = False
A R Karthickf028ec92016-08-15 16:58:11 -0700178 self.server_hello_done_eap_id = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700179 self.send_tls_response = True
180 self.server_certs = []
181 self.pkt_last = ''
182 self.pkt_history = []
183 self.pkt_map = { self.SERVER_HELLO: ['', '', lambda pkt: pkt ],
184 self.SERVER_CERTIFICATE: ['', '', lambda pkt: pkt ],
A R Karthick307483c2016-06-06 17:05:19 -0700185 self.CERTIFICATE_REQUEST: ['', '', lambda pkt: pkt ],
A R Karthick22aa0c62016-05-31 11:17:12 -0700186 self.SERVER_HELLO_DONE: ['', '', self.handle_server_hello_done ],
187 self.SERVER_UNKNOWN: ['', '', lambda pkt: pkt ]
188 }
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700189 if self.clientkeyex_replace_with_serverkeyex:
190 self.tls_ctx = TLSSessionCtx(client = False)
191 else:
192 self.tls_ctx = TLSSessionCtx(client = True)
A R Karthick05d9b5f2016-06-08 11:53:54 -0700193 self.client_cert = self.CLIENT_CERT if client_cert is None else client_cert
194 self.client_priv_key = self.CLIENT_PRIV_KEY if client_priv_key is None else client_priv_key
195 self.failTest = False
196 self.fail_cb = fail_cb
A R Karthick22aa0c62016-05-31 11:17:12 -0700197
198 def load_tls_record(self, data, pkt_type = ''):
A R Karthick307483c2016-06-06 17:05:19 -0700199 #if pkt_type not in [ self.SERVER_HELLO_DONE, self.SERVER_UNKNOWN ]:
200 if pkt_type == self.SERVER_HELLO_DONE:
201 data = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/data)
202 elif pkt_type == self.CERTIFICATE_REQUEST:
203 data = str(TLSRecord()/TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/data[9:])
204 data = None #For now ignore this record
205 if data:
A R Karthick22aa0c62016-05-31 11:17:12 -0700206 TLS(data, ctx = self.tls_ctx)
207
208 def pkt_update(self, pkt_type, data, hdr=None, reassembled = False):
209 if not self.pkt_map.has_key(pkt_type):
210 return
211 if hdr is not None:
212 self.pkt_map[pkt_type][self.HDR_IDX] += hdr
213 self.pkt_map[pkt_type][self.DATA_IDX] += data
214 if reassembled is True:
215 self.pkt_map[pkt_type][self.CB_IDX](self.pkt_map[pkt_type][self.DATA_IDX])
216 log.info('Appending packet type %02x to packet history of len %d'
217 %(ord(pkt_type), len(self.pkt_map[pkt_type][self.DATA_IDX])))
218 self.pkt_history.append(self.pkt_map[pkt_type][self.DATA_IDX])
219 data = ''.join(self.pkt_map[pkt_type][:self.DATA_IDX+1])
220 self.load_tls_record(data, pkt_type = pkt_type)
221 self.pkt_map[pkt_type][self.HDR_IDX] = ''
222 self.pkt_map[pkt_type][self.DATA_IDX] = ''
223
A R Karthick05d9b5f2016-06-08 11:53:54 -0700224 def tlsFail(self):
225 ##Force a failure
ChetanGaonker3441faf2016-10-27 12:15:21 -0700226 log.info('entering into testFail function')
A R Karthick05d9b5f2016-06-08 11:53:54 -0700227 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
A.R Karthick7eb2ce02016-06-10 19:00:50 -0700228 self.nextState = self.tlsStateTable.ST_EAP_TLS_FINISHED
A R Karthick05d9b5f2016-06-08 11:53:54 -0700229 self.failTest = True
230
A R Karthick22aa0c62016-05-31 11:17:12 -0700231 def eapol_server_hello_cb(self, pkt):
232 '''Reassemble and send response for server hello/certificate fragments'''
233 r = str(pkt)
234 offset = self.TLS_OFFSET
235 tls_data = r[offset:]
A R Karthick307483c2016-06-06 17:05:19 -0700236 type_hdrlen = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700237 if self.pending_bytes > 0:
238 if len(tls_data) >= self.pending_bytes:
239 self.pkt_update(self.pkt_last, tls_data[:self.pending_bytes], reassembled = True)
240 offset += self.pending_bytes
241 self.pkt_last = ''
242 self.pending_bytes = 0
243 else:
244 self.pkt_update(self.pkt_last, tls_data)
245 self.pending_bytes -= len(tls_data)
A R Karthick2b93d6a2016-09-06 15:19:09 -0700246 print('Offset: %d, pkt : %d, pending %d\n' %(offset, len(pkt), self.pending_bytes))
A R Karthick22aa0c62016-05-31 11:17:12 -0700247 while self.pending_bytes == 0 and offset < len(pkt):
248 tls_data = r[offset:]
A R Karthick2b93d6a2016-09-06 15:19:09 -0700249 hexdump(tls_data)
A R Karthick22aa0c62016-05-31 11:17:12 -0700250 self.pending_bytes = bytes_to_num(tls_data[3:5])
A R Karthick2b93d6a2016-09-06 15:19:09 -0700251 if self.pending_bytes < 0:
252 self.pending_bytes = 0
253 return
A R Karthick22aa0c62016-05-31 11:17:12 -0700254 if tls_data[0] == self.HANDSHAKE:
255 pkt_type = tls_data[5]
A R Karthick307483c2016-06-06 17:05:19 -0700256 if pkt_type in [ self.CERTIFICATE_REQUEST ]:
257 self.pending_bytes = bytes_to_num(tls_data[6:9])
258 type_hdrlen = 4
259 if len(tls_data) - 5 - type_hdrlen >= self.pending_bytes:
260 data_received = tls_data[5: 5 + type_hdrlen + self.pending_bytes ]
261 offset += 5 + type_hdrlen + self.pending_bytes
262 type_hdrlen = 0
A R Karthick22aa0c62016-05-31 11:17:12 -0700263 self.pending_bytes = 0
264 self.pkt_update(pkt_type, data_received,
265 hdr = tls_data[:5],
266 reassembled = True)
267 else:
268 self.pkt_update(pkt_type, tls_data[5:],
269 hdr = tls_data[:5],
270 reassembled = False)
A R Karthick307483c2016-06-06 17:05:19 -0700271 self.pending_bytes -= len(tls_data) - 5 - type_hdrlen
A R Karthick22aa0c62016-05-31 11:17:12 -0700272 self.pkt_last = pkt_type
273 log.info('Pending bytes left %d' %(self.pending_bytes))
274 assert self.pending_bytes > 0
A R Karthick307483c2016-06-06 17:05:19 -0700275 elif tls_data[0] == self.SERVER_HELLO_DONE:
A R Karthickf028ec92016-08-15 16:58:11 -0700276 self.server_hello_done_eap_id = pkt[EAP].id
A R Karthick307483c2016-06-06 17:05:19 -0700277 self.pkt_update(tls_data[0], tls_data, reassembled = True)
278 break
A R Karthick22aa0c62016-05-31 11:17:12 -0700279 else:
280 self.pkt_last = self.SERVER_UNKNOWN
281 if len(tls_data) - 5 >= self.pending_bytes:
282 offset += 5 + self.pending_bytes
283 self.pending_bytes = 0
284 self.pkt_last = ''
285
A R Karthickf028ec92016-08-15 16:58:11 -0700286 #send TLS response ack till we receive server hello done
287 if self.server_hello_done_received == False:
A R Karthick22aa0c62016-05-31 11:17:12 -0700288 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, '')
289 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
290
291 def _eapSetup(self):
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700292 #if self.src_mac == 'bcast':self.setup(src_mac='bcast')
293 #if self.src_mac == 'mcast': self.setup(src_mac='mcast')
294 #if self.src_mac == 'zeros': self.setup(src_mac='zeros')
295 #if self.src_mac == 'default': self.setup(src_mac='default')
ChetanGaonkere2b665b2016-12-07 00:50:56 -0800296 log.info('source mac is %s'%self.src_mac)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700297 self.setup(src_mac=self.src_mac)
A R Karthick22aa0c62016-05-31 11:17:12 -0700298 self.nextEvent = self.tlsEventTable.EVT_EAP_START
A R Karthick05d9b5f2016-06-08 11:53:54 -0700299
A R Karthick22aa0c62016-05-31 11:17:12 -0700300 def _eapStart(self):
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700301 log.info('_eapStart method started')
A R Karthick22aa0c62016-05-31 11:17:12 -0700302 self.eapol_start()
303 self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ
304
305 def _eapIdReq(self):
306 log.info( 'Inside EAP ID Req' )
307 def eapol_cb(pkt):
308 log.info('Got EAPOL packet with type id and code request')
309 log.info('Packet code: %d, type: %d, id: %d', pkt[EAP].code, pkt[EAP].type, pkt[EAP].id)
310 log.info("<====== Send EAP Response with identity = %s ================>" % USER)
ChetanGaonkere2b665b2016-12-07 00:50:56 -0800311 if self.id_mismatch_in_identifier_response_packet:
312 log.info('\nSending invalid id field in EAP Identity Response packet')
313 self.eapol_id_req(pkt[EAP].id+10, USER)
314 else:
315 self.eapol_id_req(pkt[EAP].id, USER)
A R Karthick22aa0c62016-05-31 11:17:12 -0700316
A R Karthick05d9b5f2016-06-08 11:53:54 -0700317 r = self.eapol_scapy_recv(cb = eapol_cb,
318 lfilter =
319 lambda pkt: EAP in pkt and pkt[EAP].type == EAP.TYPE_ID and pkt[EAP].code == EAP.REQUEST)
320 if len(r) > 0:
321 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_HELLO_REQ
322 else:
323 self.tlsFail()
324 return r
A R Karthick22aa0c62016-05-31 11:17:12 -0700325
326 def _eapTlsHelloReq(self):
327
328 def eapol_cb(pkt):
329 log.info('Got hello request for id %d', pkt[EAP].id)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700330 self.client_hello = TLSClientHello(version= self.version,
331 gmt_unix_time=self.gmt_unix_time,
A R Karthick22aa0c62016-05-31 11:17:12 -0700332 random_bytes= '\xAB' * 28,
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700333 session_id_length = self.session_id_length,
334 session_id= self.session_id,
335 compression_methods_length = self.compression_methods_length,
336 compression_methods= self.compression_methods,
337 cipher_suites_length = self.cipher_suites_length,
338 cipher_suites=[self.cipher_suite]
A R Karthick22aa0c62016-05-31 11:17:12 -0700339 )
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700340 if self.invalid_client_hello_handshake_type:
341 log.info('sending server_hello instead of client_hello handshape type in client hello packet')
342 client_hello_data = TLSHandshake(type='server_hello')/self.client_hello
343 elif self.invalid_client_hello_handshake_length:
344 log.info('sending TLS Handshake message with zero length field in client hello packet')
345 client_hello_data = TLSHandshake(length=0)/self.client_hello
346 else:
347 client_hello_data = TLSHandshake()/self.client_hello
348 #client_hello_data = TLSHandshake()/self.client_hello
A R Karthick307483c2016-06-06 17:05:19 -0700349 self.pkt_history.append( str(client_hello_data) )
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700350 if self.record_fragment_length:
351 reqdata = TLSRecord(length=self.record_fragment_length)/client_hello_data
352 else:
353 reqdata = TLSRecord()/client_hello_data
A R Karthick22aa0c62016-05-31 11:17:12 -0700354 self.load_tls_record(str(reqdata))
355 log.info("Sending Client Hello TLS payload of len %d, id %d" %(len(reqdata),pkt[EAP].id))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700356 if self.id_mismatch_in_client_hello_packet:
357 log.info('\nsending invalid id field in client hello packet')
358 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id+10, TLS_LENGTH_INCLUDED, str(reqdata))
359 else:
360 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, str(reqdata))
361 if self.dont_send_client_hello:
362 log.info('\nskipping client hello packet sending part')
363 pass
364 else:
365 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
366 if self.restart_radius:
367 cord_test_radius_restart()
A R Karthick22aa0c62016-05-31 11:17:12 -0700368
A R Karthick05d9b5f2016-06-08 11:53:54 -0700369 r = self.eapol_scapy_recv(cb = eapol_cb,
A R Karthick22aa0c62016-05-31 11:17:12 -0700370 lfilter =
371 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 -0700372
373 if len(r) == 0:
374 self.tlsFail()
375 return r
376
A R Karthickf028ec92016-08-15 16:58:11 -0700377 #move to client/server certificate request
A R Karthick22aa0c62016-05-31 11:17:12 -0700378 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ
379
A R Karthick307483c2016-06-06 17:05:19 -0700380 def get_verify_data(self):
A R Karthick22aa0c62016-05-31 11:17:12 -0700381 all_handshake_pkts = ''.join(self.pkt_history)
A R Karthick307483c2016-06-06 17:05:19 -0700382 return self.tls_ctx.get_verify_data(data = all_handshake_pkts)
383
384 def get_verify_signature(self, pem_data):
385 all_handshake_pkts = ''.join(self.pkt_history)
386 k = Key(pem_data)
387 signature = k.sign(all_handshake_pkts, t = 'pkcs', h = 'tls')
388 signature_data = '{}{}'.format(struct.pack('!H', len(signature)), signature)
389 return signature_data
390
391 def get_encrypted_handshake_msg(self, finish_val=''):
A R Karthick22aa0c62016-05-31 11:17:12 -0700392 if not finish_val:
A R Karthick307483c2016-06-06 17:05:19 -0700393 finish_val = self.get_verify_data()
A R Karthick22aa0c62016-05-31 11:17:12 -0700394 msg = str(TLSHandshake(type=TLSHandshakeType.FINISHED)/finish_val)
395 crypto_container = CryptoContainer(self.tls_ctx, data = msg,
396 content_type = TLSContentType.HANDSHAKE)
397 return crypto_container.encrypt()
398
A R Karthick307483c2016-06-06 17:05:19 -0700399 def get_encrypted_application_msg(self, msg = ''):
400 '''Needed with tunneled TLS'''
401 if not msg:
402 msg = 'test data'
403 return to_raw(TLSPlaintext(data = 'GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n'), self.tls_ctx)
404
A R Karthick22aa0c62016-05-31 11:17:12 -0700405 def _eapTlsCertReq(self):
A R Karthickf028ec92016-08-15 16:58:11 -0700406 log.info('Receiving server certificates')
407 while self.server_hello_done_received == False:
408 r = self.eapol_scapy_recv(cb = self.eapol_server_hello_cb,
409 lfilter =
410 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and \
411 pkt[EAP].code == EAP.REQUEST)
412 if len(r) == 0:
413 self.tlsFail()
414 return r
A R Karthickf028ec92016-08-15 16:58:11 -0700415 log.info('Sending client certificate request')
416 rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL)
417 if self.client_cert:
418 der_cert = rex_pem.findall(self.client_cert)[0].decode("base64")
419 client_certificate_list = TLSHandshake()/TLSCertificateList(
420 certificates=[TLSCertificate(data=x509.X509Cert(der_cert))])
A R Karthick05d9b5f2016-06-08 11:53:54 -0700421 else:
A R Karthickf028ec92016-08-15 16:58:11 -0700422 client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[])
423 client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700424 kex_data = self.tls_ctx.get_client_kex_data()
A R Karthickf028ec92016-08-15 16:58:11 -0700425 client_key_ex_data = TLSHandshake()/kex_data
426 client_key_ex = TLSRecord()/client_key_ex_data
427 if self.client_cert:
428 self.load_tls_record(str(client_certificate))
429 self.pkt_history.append(str(client_certificate_list))
430 self.load_tls_record(str(client_key_ex))
431 self.pkt_history.append(str(client_key_ex_data))
432 verify_signature = self.get_verify_signature(self.client_priv_key)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700433 if self.invalid_cert_req_handshake:
434 log.info("sending 'certificate-request' type of handshake message instead of 'certificate-verify' type")
435 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/verify_signature
436 else:
437 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature
438 if self.incorrect_tlsrecord_type_cert_req:
439 log.info("sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet")
440 client_cert_record = TLSRecord(content_type=TLSContentType.ALERT)/client_cert_verify
441 else:
442 client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify
A R Karthickf028ec92016-08-15 16:58:11 -0700443 self.pkt_history.append(str(client_cert_verify))
444 #log.info('TLS ctxt: %s' %self.tls_ctx)
445 client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec()
446 enc_handshake_msg = self.get_encrypted_handshake_msg()
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700447 if self.invalid_content_type:
448 handshake_msg = str(TLSRecord(content_type=self.invalid_content_type)/enc_handshake_msg)
449 else:
450 handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg)
A R Karthickf028ec92016-08-15 16:58:11 -0700451 reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs]))
452 reqdata += handshake_msg
453 log.info("------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata))
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700454 if self.dont_send_client_certificate:
455 log.info('\nskipping sending client certificate part')
456 pass
457 else:
458 status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED,
A R Karthickf028ec92016-08-15 16:58:11 -0700459 payload = reqdata, fragsize = 1024)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700460 assert_equal(status, True)
461 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800462
ChetanGaonker3441faf2016-10-27 12:15:21 -0700463 def _eapTlsCertReq_delay(self):
464 self.server_hello_done_received = True
465 log.info('Sending client certificate request')
466 rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL)
467
468 if self.client_cert:
469 der_cert = rex_pem.findall(self.client_cert)[0].decode("base64")
470 client_certificate_list = TLSHandshake()/TLSCertificateList(
471 certificates=[TLSCertificate(data=x509.X509Cert(der_cert))])
472 else:
473 client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[])
474
475 client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list
476 kex_data = self.tls_ctx.get_client_kex_data()
477 client_key_ex_data = TLSHandshake()/kex_data
478 client_key_ex = TLSRecord()/client_key_ex_data
479
480 if self.client_cert:
481 self.load_tls_record(str(client_certificate))
482 self.pkt_history.append(str(client_certificate_list))
483
484 self.load_tls_record(str(client_key_ex))
485 self.pkt_history.append(str(client_key_ex_data))
486 verify_signature = self.get_verify_signature(self.client_priv_key)
487
488 if self.invalid_cert_req_handshake:
489 log.info("Sending 'certificate-request' type of handshake message instead of 'certificate-verify' type")
490 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/verify_signature
491 else:
492 client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature
493
494 if self.incorrect_tlsrecord_type_cert_req:
495 log.info("Sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet")
496 client_cert_record = TLSRecord(content_type=TLSContentType.ALERT)/client_cert_verify
497 else:
498 client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify
499
500 self.pkt_history.append(str(client_cert_verify))
501 #log.info('TLS ctxt: %s' %self.tls_ctx)
502 client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec()
503 enc_handshake_msg = self.get_encrypted_handshake_msg()
504
505 if self.invalid_content_type:
506 handshake_msg = str(TLSRecord(content_type=self.invalid_content_type)/enc_handshake_msg)
507 else:
508 handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg)
509 reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs]))
510 reqdata += handshake_msg
511 log.info("------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata))
512
513 if self.dont_send_client_certificate:
514 log.info('\nSkipping sending client certificate part')
515 pass
516 else:
517 status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED,
518 payload = reqdata, fragsize = 1024)
519 assert_equal(status, True)
520 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC
521
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800522 def _eapTlsChangeCipherSpec(self):
Chetan Gaonker5b366302016-03-21 16:18:21 -0700523 def eapol_cb(pkt):
A R Karthick22aa0c62016-05-31 11:17:12 -0700524 r = str(pkt)
ChetanGaonker6138fcd2016-08-18 17:56:39 -0700525 log.info('data received in change cipher spec function is %s'%pkt.show())
A R Karthick307483c2016-06-06 17:05:19 -0700526 tls_data = r[self.TLS_OFFSET:]
A R Karthick22aa0c62016-05-31 11:17:12 -0700527 log.info('Verifying TLS Change Cipher spec record type %x' %ord(tls_data[0]))
528 assert tls_data[0] == self.CHANGE_CIPHER
A R Karthick307483c2016-06-06 17:05:19 -0700529 log.info('Handshake finished. Sending empty data')
530 eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, 0, '')
531 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
Chetan Gaonker5b366302016-03-21 16:18:21 -0700532
A R Karthick05d9b5f2016-06-08 11:53:54 -0700533 r = self.eapol_scapy_recv(cb = eapol_cb,
534 lfilter =
535 lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST)
536 if len(r) > 0:
537 self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED
538 else:
539 self.tlsFail()
540 return r
Chetan Gaonkerf8f77182016-03-11 15:34:57 -0800541
542 def _eapTlsFinished(self):
A.R Karthick7eb2ce02016-06-10 19:00:50 -0700543 self.nextEvent = None
Chetan Gaonker5b366302016-03-21 16:18:21 -0700544 def eapol_cb(pkt):
A R Karthick307483c2016-06-06 17:05:19 -0700545 log.info('Server authentication successfull')
Chetan Gaonker5b366302016-03-21 16:18:21 -0700546
A R Karthick05d9b5f2016-06-08 11:53:54 -0700547 timeout = 5
548 if self.failTest is True:
549 if self.fail_cb is not None:
550 self.fail_cb()
551 return
552 timeout = None ##Wait forever on failure and force testcase timeouts
553
Chetan Gaonker5b366302016-03-21 16:18:21 -0700554 self.eapol_scapy_recv(cb = eapol_cb,
A R Karthick22aa0c62016-05-31 11:17:12 -0700555 lfilter =
A R Karthick05d9b5f2016-06-08 11:53:54 -0700556 lambda pkt: EAP in pkt and pkt[EAP].code == EAP.SUCCESS,
557 timeout = timeout)
A R Karthick307483c2016-06-06 17:05:19 -0700558 self.eapol_logoff()