Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 1 | # |
| 2 | # 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 |
| 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 | # |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 16 | import sys, os |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 17 | from EapolAAA import * |
| 18 | from enum import * |
| 19 | import noseTlsAuthHolder as tlsAuthHolder |
| 20 | from scapy_ssl_tls.ssl_tls import * |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 21 | from scapy_ssl_tls.ssl_tls_crypto import * |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 22 | from tls_cert import Key |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 23 | from socket import * |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 24 | import struct |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 25 | import scapy |
| 26 | from nose.tools import * |
| 27 | from CordTestBase import CordTester |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 28 | import re |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 29 | |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 30 | log.setLevel('INFO') |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 31 | |
| 32 | def bytes_to_num(data): |
| 33 | return int(data.encode('hex'), 16) |
| 34 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 35 | class TLSAuthTest(EapolPacket, CordTester): |
| 36 | |
| 37 | tlsStateTable = Enumeration("TLSStateTable", ("ST_EAP_SETUP", |
| 38 | "ST_EAP_START", |
| 39 | "ST_EAP_ID_REQ", |
| 40 | "ST_EAP_TLS_HELLO_REQ", |
| 41 | "ST_EAP_TLS_CERT_REQ", |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 42 | "ST_EAP_TLS_CHANGE_CIPHER_SPEC", |
| 43 | "ST_EAP_TLS_FINISHED", |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 44 | "ST_EAP_TLS_DONE" |
| 45 | ) |
| 46 | ) |
| 47 | tlsEventTable = Enumeration("TLSEventTable", ("EVT_EAP_SETUP", |
| 48 | "EVT_EAP_START", |
| 49 | "EVT_EAP_ID_REQ", |
| 50 | "EVT_EAP_TLS_HELLO_REQ", |
| 51 | "EVT_EAP_TLS_CERT_REQ", |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 52 | "EVT_EAP_TLS_CHANGE_CIPHER_SPEC", |
| 53 | "EVT_EAP_TLS_FINISHED", |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 54 | "EVT_EAP_TLS_DONE" |
| 55 | ) |
| 56 | ) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 57 | server_hello_done_signature = '\x0e\x00\x00\x00' |
| 58 | SERVER_HELLO = '\x02' |
| 59 | SERVER_CERTIFICATE = '\x0b' |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 60 | CERTIFICATE_REQUEST = '\x0d' |
| 61 | SERVER_HELLO_DONE = '\x0e' |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 62 | SERVER_UNKNOWN = '\xff' |
| 63 | HANDSHAKE = '\x16' |
| 64 | CHANGE_CIPHER = '\x14' |
| 65 | TLS_OFFSET = 28 |
| 66 | HDR_IDX = 0 |
| 67 | DATA_IDX = 1 |
| 68 | CB_IDX = 2 |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 69 | |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 70 | CLIENT_CERT = """-----BEGIN CERTIFICATE----- |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 71 | MIICuDCCAiGgAwIBAgIBAjANBgkqhkiG9w0BAQUFADCBizELMAkGA1UEBhMCVVMx |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 72 | CzAJBgNVBAgTAkNBMRIwEAYDVQQHEwlTb21ld2hlcmUxEzARBgNVBAoTCkNpZW5h |
| 73 | IEluYy4xHjAcBgkqhkiG9w0BCQEWD2FkbWluQGNpZW5hLmNvbTEmMCQGA1UEAxMd |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 74 | RXhhbXBsZSBDZXJ0aWZpY2F0ZSBBdXRob3JpdHkwHhcNMTYwNjA2MjExMjI3WhcN |
| 75 | MTcwNjAxMjExMjI3WjBnMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEzARBgNV |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 76 | BAoTCkNpZW5hIEluYy4xFzAVBgNVBAMUDnVzZXJAY2llbmEuY29tMR0wGwYJKoZI |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 77 | hvcNAQkBFg51c2VyQGNpZW5hLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC |
| 78 | gYEAwvXiSzb9LZ6c7uNziUfKvoHO7wu/uiFC5YUpXbmVGuGZizbVrny0xnR85Dfe |
| 79 | +9R4diansfDhIhzOUl1XjN3YDeSS9OeF5YWNNE8XDhlz2d3rVzaN6hIhdotBkUjg |
| 80 | rUewjTg5OFR31QEyG3v8xR3CLgiE9xQELjZbSA07pD79zuUCAwEAAaNPME0wEwYD |
| 81 | VR0lBAwwCgYIKwYBBQUHAwIwNgYDVR0fBC8wLTAroCmgJ4YlaHR0cDovL3d3dy5l |
| 82 | eGFtcGxlLmNvbS9leGFtcGxlX2NhLmNybDANBgkqhkiG9w0BAQUFAAOBgQDAjkrY |
| 83 | 6tDChmKbvr8w6Du/t8vHjTCoCIocHTN0qzWOeb1YsAGX89+TrWIuO1dFyYd+Z0KC |
| 84 | PDKB5j/ygml9Na+AklSYAVJIjvlzXKZrOaPmhZqDufi+rXWti/utVqY4VMW2+HKC |
| 85 | nXp37qWeuFLGyR1519Y1d6F/5XzqmvbwURuEug== |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 86 | -----END CERTIFICATE-----""" |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 87 | |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 88 | CLIENT_PRIV_KEY = """-----BEGIN RSA PRIVATE KEY----- |
| 89 | MIICXAIBAAKBgQDC9eJLNv0tnpzu43OJR8q+gc7vC7+6IULlhSlduZUa4ZmLNtWu |
| 90 | fLTGdHzkN9771Hh2Jqex8OEiHM5SXVeM3dgN5JL054XlhY00TxcOGXPZ3etXNo3q |
| 91 | EiF2i0GRSOCtR7CNODk4VHfVATIbe/zFHcIuCIT3FAQuNltIDTukPv3O5QIDAQAB |
| 92 | AoGBAJha7NgYhevzqvIov25Fs1QDP0Kh7Ne5DH0u/e+nirUyHfqkBILSI7d+6uay |
| 93 | Hsiv9t9mP+CXvGgbGMVW+oc0CpUbZw4Y64jZhg/vakMuHVhpgUCyPyzjk+7Z7STg |
| 94 | 2B1DEAxILApU8azjrDBIRHM8q0CH6NFwJPpFjg2oi7li6hPhAkEA56e/UT7Mh+57 |
| 95 | qWb2q9CuI+unQcav1tqxRxUtrGHl0YSO5YTWCnaT7vVFUSbemwUhEHJs8h+Qw41L |
| 96 | g4eBu/qXLQJBANdy7puiDBBvV8XxQms14VRAEUUpCwqmzieG3RNmgr7wYRKyXzws |
| 97 | hbgp5HIkGFIM4FOIrFj5jUP6CuF2BfoYaZkCQGRIny75w6s413nfY/u/TBOqyW5V |
| 98 | J/wYElSWW35bpxTLkNzVY5+F88ankUlvTUDIuKaZEobCmXW+bilTeRs6gUUCQGeo |
| 99 | 2Lzw3rUZnTWTus0yg1Ox751C/hkF4LKL5NpsvAN6THpecAvXsA7HuS5hx4HSyCvo |
| 100 | 2mOEzj8ikxGfY4jNLiECQE09wQ39Gw3oGKCzdsTcWy8PXIWjOS44+7N/GjUB52+o |
| 101 | CK7BGBOdZGZUSFc1rVA7eWKzxFDZ+EK264z6DL95mRw= |
| 102 | -----END RSA PRIVATE KEY-----""" |
| 103 | |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 104 | def handle_server_hello_done(self, server_hello_done): |
| 105 | if server_hello_done[-4:] == self.server_hello_done_signature: |
| 106 | self.server_hello_done_received = True |
| 107 | |
| 108 | def __init__(self, intf = 'veth0'): |
| 109 | self.fsmTable = tlsAuthHolder.initTlsAuthHolderFsmTable(self, self.tlsStateTable, self.tlsEventTable) |
| 110 | EapolPacket.__init__(self, intf) |
| 111 | CordTester.__init__(self, self.fsmTable, self.tlsStateTable.ST_EAP_TLS_DONE) |
| 112 | #self.tlsStateTable, self.tlsEventTable) |
| 113 | self.currentState = self.tlsStateTable.ST_EAP_SETUP |
| 114 | self.currentEvent = self.tlsEventTable.EVT_EAP_SETUP |
| 115 | self.nextState = None |
| 116 | self.nextEvent = None |
| 117 | self.pending_bytes = 0 #for TLS fragment reassembly |
| 118 | self.server_hello_done_received = False |
| 119 | self.send_tls_response = True |
| 120 | self.server_certs = [] |
| 121 | self.pkt_last = '' |
| 122 | self.pkt_history = [] |
| 123 | self.pkt_map = { self.SERVER_HELLO: ['', '', lambda pkt: pkt ], |
| 124 | self.SERVER_CERTIFICATE: ['', '', lambda pkt: pkt ], |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 125 | self.CERTIFICATE_REQUEST: ['', '', lambda pkt: pkt ], |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 126 | self.SERVER_HELLO_DONE: ['', '', self.handle_server_hello_done ], |
| 127 | self.SERVER_UNKNOWN: ['', '', lambda pkt: pkt ] |
| 128 | } |
| 129 | self.tls_ctx = TLSSessionCtx(client = True) |
| 130 | |
| 131 | def load_tls_record(self, data, pkt_type = ''): |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 132 | #if pkt_type not in [ self.SERVER_HELLO_DONE, self.SERVER_UNKNOWN ]: |
| 133 | if pkt_type == self.SERVER_HELLO_DONE: |
| 134 | data = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/data) |
| 135 | elif pkt_type == self.CERTIFICATE_REQUEST: |
| 136 | data = str(TLSRecord()/TLSHandshake(type=TLSHandshakeType.CERTIFICATE_REQUEST)/data[9:]) |
| 137 | data = None #For now ignore this record |
| 138 | if data: |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 139 | TLS(data, ctx = self.tls_ctx) |
| 140 | |
| 141 | def pkt_update(self, pkt_type, data, hdr=None, reassembled = False): |
| 142 | if not self.pkt_map.has_key(pkt_type): |
| 143 | return |
| 144 | if hdr is not None: |
| 145 | self.pkt_map[pkt_type][self.HDR_IDX] += hdr |
| 146 | self.pkt_map[pkt_type][self.DATA_IDX] += data |
| 147 | if reassembled is True: |
| 148 | self.pkt_map[pkt_type][self.CB_IDX](self.pkt_map[pkt_type][self.DATA_IDX]) |
| 149 | log.info('Appending packet type %02x to packet history of len %d' |
| 150 | %(ord(pkt_type), len(self.pkt_map[pkt_type][self.DATA_IDX]))) |
| 151 | self.pkt_history.append(self.pkt_map[pkt_type][self.DATA_IDX]) |
| 152 | data = ''.join(self.pkt_map[pkt_type][:self.DATA_IDX+1]) |
| 153 | self.load_tls_record(data, pkt_type = pkt_type) |
| 154 | self.pkt_map[pkt_type][self.HDR_IDX] = '' |
| 155 | self.pkt_map[pkt_type][self.DATA_IDX] = '' |
| 156 | |
| 157 | def eapol_server_hello_cb(self, pkt): |
| 158 | '''Reassemble and send response for server hello/certificate fragments''' |
| 159 | r = str(pkt) |
| 160 | offset = self.TLS_OFFSET |
| 161 | tls_data = r[offset:] |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 162 | type_hdrlen = 0 |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 163 | if self.pending_bytes > 0: |
| 164 | if len(tls_data) >= self.pending_bytes: |
| 165 | self.pkt_update(self.pkt_last, tls_data[:self.pending_bytes], reassembled = True) |
| 166 | offset += self.pending_bytes |
| 167 | self.pkt_last = '' |
| 168 | self.pending_bytes = 0 |
| 169 | else: |
| 170 | self.pkt_update(self.pkt_last, tls_data) |
| 171 | self.pending_bytes -= len(tls_data) |
| 172 | |
| 173 | while self.pending_bytes == 0 and offset < len(pkt): |
| 174 | tls_data = r[offset:] |
| 175 | self.pending_bytes = bytes_to_num(tls_data[3:5]) |
| 176 | if tls_data[0] == self.HANDSHAKE: |
| 177 | pkt_type = tls_data[5] |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 178 | if pkt_type in [ self.CERTIFICATE_REQUEST ]: |
| 179 | self.pending_bytes = bytes_to_num(tls_data[6:9]) |
| 180 | type_hdrlen = 4 |
| 181 | if len(tls_data) - 5 - type_hdrlen >= self.pending_bytes: |
| 182 | data_received = tls_data[5: 5 + type_hdrlen + self.pending_bytes ] |
| 183 | offset += 5 + type_hdrlen + self.pending_bytes |
| 184 | type_hdrlen = 0 |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 185 | self.pending_bytes = 0 |
| 186 | self.pkt_update(pkt_type, data_received, |
| 187 | hdr = tls_data[:5], |
| 188 | reassembled = True) |
| 189 | else: |
| 190 | self.pkt_update(pkt_type, tls_data[5:], |
| 191 | hdr = tls_data[:5], |
| 192 | reassembled = False) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 193 | self.pending_bytes -= len(tls_data) - 5 - type_hdrlen |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 194 | self.pkt_last = pkt_type |
| 195 | log.info('Pending bytes left %d' %(self.pending_bytes)) |
| 196 | assert self.pending_bytes > 0 |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 197 | elif tls_data[0] == self.SERVER_HELLO_DONE: |
| 198 | self.pkt_update(tls_data[0], tls_data, reassembled = True) |
| 199 | break |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 200 | else: |
| 201 | self.pkt_last = self.SERVER_UNKNOWN |
| 202 | if len(tls_data) - 5 >= self.pending_bytes: |
| 203 | offset += 5 + self.pending_bytes |
| 204 | self.pending_bytes = 0 |
| 205 | self.pkt_last = '' |
| 206 | |
| 207 | #send TLS response |
| 208 | if self.send_tls_response: |
| 209 | eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, '') |
| 210 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 211 | |
| 212 | def _eapSetup(self): |
| 213 | self.setup() |
| 214 | self.nextEvent = self.tlsEventTable.EVT_EAP_START |
| 215 | |
| 216 | def _eapStart(self): |
| 217 | self.eapol_start() |
| 218 | self.nextEvent = self.tlsEventTable.EVT_EAP_ID_REQ |
| 219 | |
| 220 | def _eapIdReq(self): |
| 221 | log.info( 'Inside EAP ID Req' ) |
| 222 | def eapol_cb(pkt): |
| 223 | log.info('Got EAPOL packet with type id and code request') |
| 224 | log.info('Packet code: %d, type: %d, id: %d', pkt[EAP].code, pkt[EAP].type, pkt[EAP].id) |
| 225 | log.info("<====== Send EAP Response with identity = %s ================>" % USER) |
| 226 | self.eapol_id_req(pkt[EAP].id, USER) |
| 227 | |
| 228 | self.eapol_scapy_recv(cb = eapol_cb, |
| 229 | lfilter = |
| 230 | lambda pkt: EAP in pkt and pkt[EAP].type == EAP.TYPE_ID and pkt[EAP].code == EAP.REQUEST) |
| 231 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_HELLO_REQ |
| 232 | |
| 233 | def _eapTlsHelloReq(self): |
| 234 | |
| 235 | def eapol_cb(pkt): |
| 236 | log.info('Got hello request for id %d', pkt[EAP].id) |
| 237 | self.client_hello = TLSClientHello(version="TLS_1_0", |
| 238 | gmt_unix_time=1234, |
| 239 | random_bytes= '\xAB' * 28, |
| 240 | session_id='', |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 241 | compression_methods=[TLSCompressionMethod.NULL], |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 242 | cipher_suites=[TLSCipherSuite.RSA_WITH_AES_256_CBC_SHA] |
| 243 | ) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 244 | client_hello_data = TLSHandshake()/self.client_hello |
| 245 | self.pkt_history.append( str(client_hello_data) ) |
| 246 | reqdata = TLSRecord()/client_hello_data |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 247 | self.load_tls_record(str(reqdata)) |
| 248 | log.info("Sending Client Hello TLS payload of len %d, id %d" %(len(reqdata),pkt[EAP].id)) |
| 249 | eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, str(reqdata)) |
| 250 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 251 | |
| 252 | self.eapol_scapy_recv(cb = eapol_cb, |
| 253 | lfilter = |
| 254 | lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST) |
| 255 | |
| 256 | for i in range(2): |
| 257 | self.eapol_scapy_recv(cb = self.eapol_server_hello_cb, |
| 258 | lfilter = |
| 259 | lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST) |
| 260 | ##send cert request when we receive the last server hello fragment |
| 261 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CERT_REQ |
| 262 | |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 263 | def get_verify_data(self): |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 264 | all_handshake_pkts = ''.join(self.pkt_history) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 265 | return self.tls_ctx.get_verify_data(data = all_handshake_pkts) |
| 266 | |
| 267 | def get_verify_signature(self, pem_data): |
| 268 | all_handshake_pkts = ''.join(self.pkt_history) |
| 269 | k = Key(pem_data) |
| 270 | signature = k.sign(all_handshake_pkts, t = 'pkcs', h = 'tls') |
| 271 | signature_data = '{}{}'.format(struct.pack('!H', len(signature)), signature) |
| 272 | return signature_data |
| 273 | |
| 274 | def get_encrypted_handshake_msg(self, finish_val=''): |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 275 | if not finish_val: |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 276 | finish_val = self.get_verify_data() |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 277 | msg = str(TLSHandshake(type=TLSHandshakeType.FINISHED)/finish_val) |
| 278 | crypto_container = CryptoContainer(self.tls_ctx, data = msg, |
| 279 | content_type = TLSContentType.HANDSHAKE) |
| 280 | return crypto_container.encrypt() |
| 281 | |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 282 | def get_encrypted_application_msg(self, msg = ''): |
| 283 | '''Needed with tunneled TLS''' |
| 284 | if not msg: |
| 285 | msg = 'test data' |
| 286 | return to_raw(TLSPlaintext(data = 'GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n'), self.tls_ctx) |
| 287 | |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 288 | def _eapTlsCertReq(self): |
| 289 | |
| 290 | def eapol_cb(pkt): |
| 291 | log.info('Got cert request') |
| 292 | self.send_tls_response = False |
| 293 | self.eapol_server_hello_cb(pkt) |
| 294 | assert self.server_hello_done_received == True |
| 295 | rex_pem = re.compile(r'\-+BEGIN[^\-]+\-+(.*?)\-+END[^\-]+\-+', re.DOTALL) |
| 296 | der_cert = rex_pem.findall(self.CLIENT_CERT)[0].decode("base64") |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 297 | client_certificate_list = TLSHandshake()/TLSCertificateList( |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 298 | certificates=[TLSCertificate(data=x509.X509Cert(der_cert))]) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 299 | client_certificate = TLSRecord(version="TLS_1_0")/client_certificate_list |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 300 | kex_data = self.tls_ctx.get_client_kex_data() |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 301 | client_key_ex_data = TLSHandshake()/kex_data |
| 302 | client_key_ex = TLSRecord()/client_key_ex_data |
| 303 | self.load_tls_record(str(client_certificate)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 304 | self.load_tls_record(str(client_key_ex)) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 305 | self.pkt_history.append(str(client_certificate_list)) |
| 306 | self.pkt_history.append(str(client_key_ex_data)) |
| 307 | verify_signature = self.get_verify_signature(self.CLIENT_PRIV_KEY) |
| 308 | client_cert_verify = TLSHandshake(type=TLSHandshakeType.CERTIFICATE_VERIFY)/verify_signature |
| 309 | client_cert_record = TLSRecord(content_type=TLSContentType.HANDSHAKE)/client_cert_verify |
| 310 | self.pkt_history.append(str(client_cert_verify)) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 311 | #log.info('TLS ctxt: %s' %self.tls_ctx) |
| 312 | client_ccs = TLSRecord(version="TLS_1_0")/TLSChangeCipherSpec() |
| 313 | enc_handshake_msg = self.get_encrypted_handshake_msg() |
| 314 | handshake_msg = str(TLSRecord(content_type=TLSContentType.HANDSHAKE)/enc_handshake_msg) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 315 | reqdata = str(TLS.from_records([client_certificate, client_key_ex, client_cert_record, client_ccs])) |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 316 | reqdata += handshake_msg |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 317 | log.info("------> Sending Client Hello TLS Certificate payload of len %d ----------->" %len(reqdata)) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 318 | eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, TLS_LENGTH_INCLUDED, reqdata) |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 319 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 320 | |
| 321 | self.eapol_scapy_recv(cb = eapol_cb, |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 322 | lfilter = |
| 323 | lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST) |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 324 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_CHANGE_CIPHER_SPEC |
| 325 | |
| 326 | def _eapTlsChangeCipherSpec(self): |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 327 | def eapol_cb(pkt): |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 328 | r = str(pkt) |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 329 | tls_data = r[self.TLS_OFFSET:] |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 330 | log.info('Verifying TLS Change Cipher spec record type %x' %ord(tls_data[0])) |
| 331 | assert tls_data[0] == self.CHANGE_CIPHER |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 332 | log.info('Handshake finished. Sending empty data') |
| 333 | eap_payload = self.eapTLS(EAP_RESPONSE, pkt[EAP].id, 0, '') |
| 334 | self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 335 | |
| 336 | self.eapol_scapy_recv(cb = eapol_cb, |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 337 | lfilter = |
| 338 | lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and pkt[EAP].code == EAP.REQUEST) |
Chetan Gaonker | f8f7718 | 2016-03-11 15:34:57 -0800 | [diff] [blame] | 339 | self.nextEvent = self.tlsEventTable.EVT_EAP_TLS_FINISHED |
| 340 | |
| 341 | def _eapTlsFinished(self): |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 342 | |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 343 | def eapol_cb(pkt): |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 344 | log.info('Server authentication successfull') |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 345 | |
| 346 | self.eapol_scapy_recv(cb = eapol_cb, |
A R Karthick | 22aa0c6 | 2016-05-31 11:17:12 -0700 | [diff] [blame] | 347 | lfilter = |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 348 | lambda pkt: EAP in pkt and pkt[EAP].code == EAP.SUCCESS) |
| 349 | self.eapol_logoff() |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 350 | self.nextEvent = None |