Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 17 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 18 | # 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 Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 23 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 25 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 26 | # 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 Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 32 | import sys, os |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 33 | from EapolAAA import * |
A R Karthick | 74d0031 | 2017-04-18 14:26:01 -0700 | [diff] [blame] | 34 | from Enum import * |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 35 | import noseTlsAuthHolder as tlsAuthHolder |
| 36 | from scapy_ssl_tls.ssl_tls import * |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 37 | from scapy_ssl_tls.ssl_tls_crypto import * |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 38 | from tls_cert import Key |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 39 | from socket import * |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 40 | from CordTestServer import cord_test_radius_restart |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 41 | import struct |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 42 | import scapy |
| 43 | from nose.tools import * |
| 44 | from CordTestBase import CordTester |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 45 | from CordContainer import * |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 46 | from CordTestUtils import log_test |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 47 | import re |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 48 | import time |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 49 | |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 50 | log_test.setLevel('INFO') |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 51 | |
| 52 | def bytes_to_num(data): |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 53 | try: |
| 54 | return int(data.encode('hex'), 16) |
| 55 | except: |
| 56 | print('Exception') |
| 57 | return -1 |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 58 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 59 | class 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 Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 66 | "ST_EAP_TLS_CHANGE_CIPHER_SPEC", |
| 67 | "ST_EAP_TLS_FINISHED", |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 68 | "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 Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 76 | "EVT_EAP_TLS_CHANGE_CIPHER_SPEC", |
| 77 | "EVT_EAP_TLS_FINISHED", |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 78 | "EVT_EAP_TLS_DONE" |
| 79 | ) |
| 80 | ) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 81 | server_hello_done_signature = '\x0e\x00\x00\x00' |
| 82 | SERVER_HELLO = '\x02' |
| 83 | SERVER_CERTIFICATE = '\x0b' |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 84 | CERTIFICATE_REQUEST = '\x0d' |
| 85 | SERVER_HELLO_DONE = '\x0e' |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 86 | 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 Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 93 | |
A R Karthick | eeac7e1 | 2017-03-10 17:35:39 -0800 | [diff] [blame] | 94 | #this is from client.crt file |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 95 | CLIENT_CERT = """-----BEGIN CERTIFICATE----- |
A R Karthick | eeac7e1 | 2017-03-10 17:35:39 -0800 | [diff] [blame] | 96 | MIIDvTCCAqWgAwIBAgIBBjANBgkqhkiG9w0BAQUFADCBizELMAkGA1UEBhMCVVMx |
| 97 | CzAJBgNVBAgMAkNBMRIwEAYDVQQHDAlTb21ld2hlcmUxEzARBgNVBAoMCkNpZW5h |
| 98 | IEluYy4xHjAcBgkqhkiG9w0BCQEWD2FkbWluQGNpZW5hLmNvbTEmMCQGA1UEAwwd |
| 99 | RXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTcwMzExMDA0NzQ0WhcN |
| 100 | MjIxMDMxMDA0NzQ0WjBnMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExEzARBgNV |
| 101 | BAoMCkNpZW5hIEluYy4xFzAVBgNVBAMMDnVzZXJAY2llbmEuY29tMR0wGwYJKoZI |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 102 | hvcNAQkBFg51c2VyQGNpZW5hLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC |
A R Karthick | eeac7e1 | 2017-03-10 17:35:39 -0800 | [diff] [blame] | 103 | AQoCggEBAOTxi5+TUuaosUh7f/4U9unLV3qHCC7Sf3e0o8F+FP4BVQiIslUTEupw |
| 104 | gd3VkiVAhxX74Oc3w7XF98msKx7dTetpQaZPZgEgGiDmzGzlAGkwqD4FO5JotJMg |
| 105 | I0rpcYw+M/z+WDVgJx3I+VGyC6lFKhIJWTlTySheyMYpcgSvOuN4z8keCNg2uaRZ |
| 106 | IOvIf6aUCTF7fp2HC/468/3T6R/VuYKFNdpsyU1ogbQqCUL4WHM2uWz6G7rzXI0/ |
| 107 | skkKemoGouFw+0I3ydPpGpiWK4NpPKHax4dRGaO1NmTDCtrJOAKPQx8CYYH0HMNp |
| 108 | BbYvidTnHBxYUBrWNpzOJ/xgopawA8kCAwEAAaNPME0wEwYDVR0lBAwwCgYIKwYB |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 109 | BQUHAwIwNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL3d3dy5leGFtcGxlLmNvbS9l |
A R Karthick | eeac7e1 | 2017-03-10 17:35:39 -0800 | [diff] [blame] | 110 | eGFtcGxlX2NhLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAosa5wV/RA2XO/SVGWx5K |
| 111 | JkQNBn6zKuLRODdkm1UwpkdSZpYoXw2ds6YPPbxV9gFeitCcPZQUho38Fg1LYrNW |
| 112 | 9UsDPC2HAYSxrvCinTLC2NuqLsPj7NSu+41l37hGVG4WTjPXPqet33e0mqz1eYVn |
| 113 | 3SsGvZl98sgG6ADD1RqUb0gpEocmoN+yx5W0hIAwZQPBt4nfAWdGH9AcJTL/Gjr7 |
| 114 | 5m4p3cQYm2AnROp+Bim4AELv02WIl77vYWivlzok1JfZ38GL94+CMLOSKNv9OLjc |
| 115 | M/uh5Q6PB+4xv8kAjmO/Fq9T6f0KcQNNPY63omZCjrFiMEMvoOD4GIg+WV2SKLsQ |
| 116 | 0g== |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 117 | -----END CERTIFICATE-----""" |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 118 | |
A R Karthick | eeac7e1 | 2017-03-10 17:35:39 -0800 | [diff] [blame] | 119 | #this is from client.key |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 120 | CLIENT_PRIV_KEY = """-----BEGIN RSA PRIVATE KEY----- |
A R Karthick | eeac7e1 | 2017-03-10 17:35:39 -0800 | [diff] [blame] | 121 | MIIEowIBAAKCAQEA5PGLn5NS5qixSHt//hT26ctXeocILtJ/d7SjwX4U/gFVCIiy |
| 122 | VRMS6nCB3dWSJUCHFfvg5zfDtcX3yawrHt1N62lBpk9mASAaIObMbOUAaTCoPgU7 |
| 123 | kmi0kyAjSulxjD4z/P5YNWAnHcj5UbILqUUqEglZOVPJKF7IxilyBK8643jPyR4I |
| 124 | 2Da5pFkg68h/ppQJMXt+nYcL/jrz/dPpH9W5goU12mzJTWiBtCoJQvhYcza5bPob |
| 125 | uvNcjT+ySQp6agai4XD7QjfJ0+kamJYrg2k8odrHh1EZo7U2ZMMK2sk4Ao9DHwJh |
| 126 | gfQcw2kFti+J1OccHFhQGtY2nM4n/GCilrADyQIDAQABAoIBAF+xepvDl3Yj8p4K |
| 127 | SPBp0N7eCH2FbW0svOzLC1t4GMwmwGUlxex7YX+ucQnJGCIL+6q7skDS9THIQo8A |
| 128 | MLHg7I3GXBNowokb4u/3qGCnw2k0Vk4+H61NqJSKbVYFh1mIMnC/2xzMSO1RMKzu |
| 129 | D6O77h7F245zr/P40lDJyAefOq0S6sgZqxmHmoRTHhp0tXV4mkzV7P7RqoJrvAiG |
| 130 | tGMk5OfdoILnNfoeXNC50nw201UK7xhrrqqlAWZRAaUZJtsb1GxW+jOra6OtsCVg |
| 131 | AKS/FxzUGMsoqluM5mHLBEN69DAvBBs8g7UVKdmCDZC+feJ31eAnPAoc1gxBHdQU |
| 132 | pCnA8dECgYEA+Q6X80gnoyG0g66Gh62R7RgDLYPpgkZimLeoX49mwizAUkeSf/Mj |
| 133 | raVajRmJ8J1n4UklHdQe0PE9Jhuxo4Uo9sP71ZqpQPEvN35/Sw0xxQHcwxD73SWa |
| 134 | UEVsnWIDJ6QrkoBOhjDMM6tyDSPVDS23352E6sZ9EU45qWvncb5OTdUCgYEA61Np |
| 135 | Qs/CpEWtPG8IiEPKPEWUEwoO8SS6C4R/UfXNC96GhfIpA4Uy3fQwTUtHEMPL+7lh |
| 136 | SPFPQDBH90jOTYg30EfHiBMlKW4l21XS+PotTP3ktqZMgx06SnoM2a/+crpzFqkb |
| 137 | i4eAPCsdTispElbtqleLuUbFO9aG3jHMsK2RtCUCgYB04G9YYL0RJgkTXryNQVvg |
| 138 | ussK+gOD+kncxxtBtQcRCnU6Z5INb2mH3LgwzRJZk1SjeXLsm5XWkc8Tah2j0iKW |
| 139 | IwS0if7xlf2Felx8OPXpMOWLuRWpAzN2hg3hkZRPbxBvkLzI5m99s/Ay0GTz6UeH |
| 140 | reEpV/prO519r0COtTMD/QKBgCdRinbVS8oysh002BIccX/ciD8eIRz9a/BctQE2 |
| 141 | tonTJlre+SdTbApVsHRZrYgJjt2CPdT4LKum5X9VtNKTfe/Y7me3+y+O7dhV4Kgk |
| 142 | 9Mi2ay5xXrtReNnUxqzgkP0OVghlPOr1OuHSulTDNVuRFqitc/UC9BVpZKNfYrnq |
| 143 | ZjvZAoGBALzgzXajgIdhghCt5PFLkhh3xyOliTXWFstHcMZdQF2wQyeF/uQ2zrC/ |
| 144 | 2t1Sa+egV3QDUPYzW9YLQs9eaLh7MS9wCHLY2SMElAqYiNjRfkT4wWdPfeyFx4+E |
| 145 | Euwtu+lPJ7sEpNu5jX63OS2AeZsQYlsT0Ai+lB4TeyoE6Pj04iC0 |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 146 | -----END RSA PRIVATE KEY-----""" |
| 147 | |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 148 | def handle_server_hello_done(self, server_hello_done): |
| 149 | if server_hello_done[-4:] == self.server_hello_done_signature: |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 150 | log_test.info('server hello received over interface %s' %self.intf) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 151 | self.server_hello_done_received = True |
| 152 | |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 153 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 164 | 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 |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 170 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 193 | 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 Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 197 | self.server_hello_done_eap_id = 0 |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 198 | 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 Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 204 | self.CERTIFICATE_REQUEST: ['', '', lambda pkt: pkt ], |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 205 | self.SERVER_HELLO_DONE: ['', '', self.handle_server_hello_done ], |
| 206 | self.SERVER_UNKNOWN: ['', '', lambda pkt: pkt ] |
| 207 | } |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 208 | if self.clientkeyex_replace_with_serverkeyex: |
| 209 | self.tls_ctx = TLSSessionCtx(client = False) |
| 210 | else: |
| 211 | self.tls_ctx = TLSSessionCtx(client = True) |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 212 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 216 | |
| 217 | def load_tls_record(self, data, pkt_type = ''): |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 218 | #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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 225 | 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 Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 235 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 237 | 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 Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 243 | def tlsFail(self): |
| 244 | ##Force a failure |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 245 | log_test.info('entering into testFail function for interface %s' %self.intf) |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 246 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED |
A.R Karthick | 7eb2ce0 | 2016-06-10 19:00:50 -0700 | [diff] [blame] | 247 | self.nextState = self.tlsStateTable.ST_EAP_TLS_FINISHED |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 248 | self.failTest = True |
| 249 | |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 250 | 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 Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 255 | type_hdrlen = 0 |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 256 | 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 Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 265 | print('Interface: %s, Offset: %d, pkt : %d, pending %d\n' %(self.intf, offset, len(pkt), self.pending_bytes)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 266 | while self.pending_bytes == 0 and offset < len(pkt): |
| 267 | tls_data = r[offset:] |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 268 | hexdump(tls_data) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 269 | self.pending_bytes = bytes_to_num(tls_data[3:5]) |
A R Karthick | 2b93d6a | 2016-09-06 15:19:09 -0700 | [diff] [blame] | 270 | if self.pending_bytes < 0: |
| 271 | self.pending_bytes = 0 |
| 272 | return |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 273 | if tls_data[0] == self.HANDSHAKE: |
| 274 | pkt_type = tls_data[5] |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 275 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 282 | 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 Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 290 | self.pending_bytes -= len(tls_data) - 5 - type_hdrlen |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 291 | self.pkt_last = pkt_type |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 292 | log_test.info('Interface: %s, Pending bytes left %d' %(self.intf, self.pending_bytes)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 293 | assert self.pending_bytes > 0 |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 294 | elif tls_data[0] == self.SERVER_HELLO_DONE: |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 295 | self.server_hello_done_eap_id = pkt[EAP].id |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 296 | self.pkt_update(tls_data[0], tls_data, reassembled = True) |
| 297 | break |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 298 | 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 Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 305 | #send TLS response ack till we receive server hello done |
| 306 | if self.server_hello_done_received == False: |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 307 | 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): |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 311 | #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 Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 315 | #log_test.info('Interface: %s, Source mac is %s' %(self.intf, self.src_mac)) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 316 | self.setup(src_mac=self.src_mac) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 317 | self.nextEvent = self.tlsEventTable.EVT_EAP_START |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 318 | |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 319 | def _eapStart(self): |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 320 | log_test.info('_eapStart method started over interface %s' %(self.intf)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 321 | self.eapol_start() |
| 322 | self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ |
| 323 | |
| 324 | def _eapIdReq(self): |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 325 | log_test.info('Inside EAP ID Req for interface %s' %(self.intf)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 326 | def eapol_cb(pkt): |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 327 | 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)) |
ChetanGaonker | e2b665b | 2016-12-07 00:50:56 -0800 | [diff] [blame] | 330 | if self.id_mismatch_in_identifier_response_packet: |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 331 | log_test.info('\nSending invalid id field in EAP Identity Response packet over interface %s' %(self.intf)) |
ChetanGaonker | e2b665b | 2016-12-07 00:50:56 -0800 | [diff] [blame] | 332 | self.eapol_id_req(pkt[EAP].id+10, USER) |
| 333 | else: |
| 334 | self.eapol_id_req(pkt[EAP].id, USER) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 335 | |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 336 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 344 | |
| 345 | def _eapTlsHelloReq(self): |
| 346 | |
| 347 | def eapol_cb(pkt): |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 348 | log_test.info('Got hello request for id %d over interface %s', pkt[EAP].id, self.intf) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 349 | self.client_hello = TLSClientHello(version= self.version, |
| 350 | gmt_unix_time=self.gmt_unix_time, |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 351 | random_bytes= '\xAB' * 28, |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 352 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 358 | ) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 359 | if self.invalid_client_hello_handshake_type: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 360 | log_test.info('sending server_hello instead of client_hello handshape type in client hello packet') |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 361 | client_hello_data = TLSHandshake(type='server_hello')/self.client_hello |
| 362 | elif self.invalid_client_hello_handshake_length: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 363 | log_test.info('sending TLS Handshake message with zero length field in client hello packet') |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 364 | 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 Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 368 | self.pkt_history.append( str(client_hello_data) ) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 369 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 373 | self.load_tls_record(str(reqdata)) |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 374 | log_test.info("Sending Client Hello TLS payload of len %d, id %d over interface %s" %(len(reqdata),pkt[EAP].id, self.intf)) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 375 | if self.id_mismatch_in_client_hello_packet: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 376 | log_test.info('\nsending invalid id field in client hello packet') |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 377 | 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 Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 381 | log_test.info('\nskipping client hello packet sending part') |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 382 | pass |
| 383 | else: |
| 384 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 385 | if self.restart_radius: |
| 386 | cord_test_radius_restart() |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 387 | |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 388 | r = self.eapol_scapy_recv(cb = eapol_cb, |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 389 | lfilter = |
| 390 | lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST) |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 391 | |
| 392 | if len(r) == 0: |
| 393 | self.tlsFail() |
| 394 | return r |
| 395 | |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 396 | #move to client/server certificate request |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 397 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ |
| 398 | |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 399 | def get_verify_data(self): |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 400 | all_handshake_pkts = ''.join(self.pkt_history) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 401 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 411 | if not finish_val: |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 412 | finish_val = self.get_verify_data() |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 413 | 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 Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 418 | 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 Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 424 | def _eapTlsCertReq(self): |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 425 | log_test.info('Receiving server certificates over interface %s', self.intf) |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 426 | 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 Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 434 | log_test.info('Sending client certificate request over interface %s', self.intf) |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 435 | 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 Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 440 | else: |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 441 | client_certificate_list = TLSHandshake()/TLSCertificateList(certificates=[]) |
| 442 | client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 443 | kex_data = self.tls_ctx.get_client_kex_data() |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 444 | 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) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 452 | if self.invalid_cert_req_handshake: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 453 | log_test.info("sending 'certificate-request' type of handshake message instead of 'certificate-verify' type") |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 454 | 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 Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 458 | log_test.info("sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet") |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 459 | 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 Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 462 | self.pkt_history.append(str(client_cert_verify)) |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 463 | #log_test.info('Interface: %s, TLS ctxt: %s' %(self.intf, self.tls_ctx)) |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 464 | client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec() |
| 465 | enc_handshake_msg = self.get_encrypted_handshake_msg() |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 466 | 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 Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 470 | reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs])) |
| 471 | reqdata += handshake_msg |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 472 | log_test.info("Sending Client Hello TLS Certificate payload of len %d over interface %s" %(len(reqdata), self.intf)) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 473 | if self.dont_send_client_certificate: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 474 | log_test.info('\nskipping sending client certificate part') |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 475 | pass |
| 476 | else: |
| 477 | status = self.eapFragmentSend(EAP_RESPONSE, self.server_hello_done_eap_id, TLS_LENGTH_INCLUDED, |
A R Karthick | f028ec9 | 2016-08-15 16:58:11 -0700 | [diff] [blame] | 478 | payload = reqdata, fragsize = 1024) |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 479 | assert_equal(status, True) |
| 480 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 481 | |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 482 | def _eapTlsCertReq_delay(self): |
| 483 | self.server_hello_done_received = True |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 484 | log_test.info('Sending client certificate request over interface %s', self.intf) |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 485 | 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 Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 508 | log_test.info("Sending 'certificate-request' type of handshake message instead of 'certificate-verify' type") |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 509 | 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 Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 514 | log_test.info("Sending TLS Record type as ALERT instead of HANDSHAKE in certificate request packet") |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 515 | 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 Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 520 | #log_test.info('TLS ctxt: %s' %self.tls_ctx) |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 521 | 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 Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 530 | log_test.info("Sending Client Hello TLS Certificate payload of len %d over interface %s" %(len(reqdata), self.intf)) |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 531 | |
| 532 | if self.dont_send_client_certificate: |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 533 | log_test.info('\nSkipping sending client certificate part') |
ChetanGaonker | 3441faf | 2016-10-27 12:15:21 -0700 | [diff] [blame] | 534 | 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 Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 541 | def _eapTlsChangeCipherSpec(self): |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 542 | def eapol_cb(pkt): |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 543 | r = str(pkt) |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 544 | log_test.info('Interface %s. Received data in change cipher spec function is %s'%(self.intf, pkt.show())) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 545 | tls_data = r[self.TLS_OFFSET:] |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 546 | log_test.info('Verifying TLS Change Cipher spec record type %x over interface %s' %(ord(tls_data[0]), self.intf)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 547 | assert tls_data[0] == self.CHANGE_CIPHER |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 548 | log_test.info('Handshake finished. Sending empty data over interface %s' %self.intf) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 549 | eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, 0, '') |
| 550 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 551 | |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 552 | 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 Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 560 | |
| 561 | def _eapTlsFinished(self): |
A.R Karthick | 7eb2ce0 | 2016-06-10 19:00:50 -0700 | [diff] [blame] | 562 | self.nextEvent = None |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 563 | def eapol_cb(pkt): |
A R Karthick | 43613a7 | 2017-06-13 11:05:15 -0700 | [diff] [blame] | 564 | log_test.info('Server authentication successfull over interface %s' %self.intf) |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 565 | |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 566 | 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 Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 573 | self.eapol_scapy_recv(cb = eapol_cb, |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 574 | lfilter = |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 575 | lambda pkt: EAP in pkt and pkt[EAP].code == EAP.SUCCESS, |
| 576 | timeout = timeout) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 577 | self.eapol_logoff() |