-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestTLS.py
More file actions
70 lines (53 loc) · 2.06 KB
/
testTLS.py
File metadata and controls
70 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!python3
import logging
import socket
import dump_hex
from SslKeyLog import SslKeyLog #keyLog for Wireshark
from HandShakeMsg import HandShakeMsg, CryptoHandShakeMsg, AppMsg
from ClientHello import ClientHello
from ServerHello import ServerHello
from Finished import Finished
from KeySchedule import KeySchedule
from KeyExchange import KeyExchange
logger = logging.getLogger()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
keylog = SslKeyLog("sslkeylog.log") # keyLog for Wireshark
sock = socket.create_connection(("localhost", 11111))
transcript = b""
# Content Types
ALERT = 21
APPLICATION_DATA = 23
# Handshake Types
CLIENT_HELLO = 1
SERVER_HELLO = 2
ENCRYPTED_EXTENTIONS = 8
CERTIFICATE = 11
CERTIFICATE_VERIFY = 15
FINISHED = 20
keySched = KeySchedule(keylog)
keyEx = KeyExchange(keySched)
cl_hello = ClientHello(keylog, keyEx)
sv_hello = ServerHello(keyEx)
finished = Finished(keySched)
hsMsg = HandShakeMsg(sock, keySched)
chsMsg = CryptoHandShakeMsg(sock, keySched)
appMsg = AppMsg(sock, keySched)
# Start hadnshake for a TLS connection
hsMsg.send(CLIENT_HELLO, cl_hello.make()) # Send ClientHello
sv_hello.do(hsMsg.recv(SERVER_HELLO)) # Receive and Parse ServerHello
keyEx.doExchange() # Key Exchange
chsMsg.calc_keys_and_ivs() # set key, IV for crypted handshake message
enc_exts_msg = chsMsg.recv(ENCRYPTED_EXTENTIONS) # Receive Encrypted Server Hello
cert_msg = chsMsg.recv(CERTIFICATE) # Receive Certificate
cert_verify_msg = chsMsg.recv(CERTIFICATE_VERIFY) # Verify Certificate
finished.set_expected_verify_data()
finished.do(chsMsg.recv(FINISHED)) # Server Finished
appMsg.calc_keys_and_ivs()
chsMsg.send(FINISHED, finished.make()) # Client Finished
appMsg.send(APPLICATION_DATA, b"Hello")
print("App Data from Server", appMsg.recv(APPLICATION_DATA))
print("Alert from Server", appMsg.recv(ALERT))
appMsg.send(ALERT, bytes.fromhex("01 00"))