% Tests for TLS module
#
# Try me with :
# bash test/run_tests -t test/tls.uts -F

~ crypto

###############################################################################
################# Reading SSLv2 vulnerable test session #######################
###############################################################################

# These packets come from a session between an s_server and an s_client.
# We assume the server's private key has been retrieved. Because the cipher
# suite does not provide PFS, we are able to break the data confidentiality.
# With openssl version being 0.9.8v, these are exactly the commands used:
# openssl s_server -cert test/tls/pki/srv_cert.pem -key test/tls/pki/srv_key.pem -Verify 2 -cipher EXP-RC4-MD5
# openssl s_client -ssl2 -cert test/tls/pki/cli_cert.pem -key test/tls/pki/cli_key.pem

+ Read a vulnerable SSLv2 session

= Reading SSLv2 session - Loading unparsed SSLv2 records
ch = b'\x80.\x01\x00\x02\x00\x15\x00\x00\x00\x10\x07\x00\xc0\x05\x00\x80\x03\x00\x80\x01\x00\x80\x06\x00@\x04\x00\x80\x02\x00\x80\x1a\xfb/\x9c\xa3\xd1)4T\xa3\x15(!\xff\xd1\x0c'
sh = b'\x83\xc0\x04\x00\x01\x00\x02\x03\xa2\x00\x03\x00\x100\x82\x03\x9e0\x82\x02\x86\xa0\x03\x02\x01\x02\x02\t\x00\xfe\x04W\r\xc7\'\xe9\xf60\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x000T1\x0b0\t\x06\x03U\x04\x06\x13\x02MN1\x140\x12\x06\x03U\x04\x07\x0c\x0bUlaanbaatar1\x170\x15\x06\x03U\x04\x0b\x0c\x0eScapy Test PKI1\x160\x14\x06\x03U\x04\x03\x0c\rScapy Test CA0\x1e\x17\r160916102811Z\x17\r260915102811Z0X1\x0b0\t\x06\x03U\x04\x06\x13\x02MN1\x140\x12\x06\x03U\x04\x07\x0c\x0bUlaanbaatar1\x170\x15\x06\x03U\x04\x0b\x0c\x0eScapy Test PKI1\x1a0\x18\x06\x03U\x04\x03\x0c\x11Scapy Test Server0\x82\x01"0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x82\x01\x0f\x000\x82\x01\n\x02\x82\x01\x01\x00\xcc\xf1\xf1\x9b`-`\xae\xf2\x98\r\')\xd9\xc0\tYL\x0fJ0\xa8R\xdf\xe5\xb1!\x9fO\xc3=V\x93\xdd_\xc6\xf7\xb3\xf6U\x8b\xe7\x92\xe2\xde\xf2\x85I\xb4\xa1,\xf4\xfdv\xa8g\xca\x04 `\x11\x18\xa6\xf2\xa9\xb6\xa6\x1d\xd9\xaa\xe5\xd9\xdb\xaf\xe6\xafUW\x9f\xffR\x89e\xe6\x80b\x80!\x94\xbc\xcf\x81\x1b\xcbg\xc2\x9d\xb5\x05w\x04\xa6\xc7\x88\x18\x80xh\x956\xde\x97\x1b\xb6a\x87B\x1au\x98E\x82\xeb>2\x11\xc8\x9b\x86B9\x8dM\x12\xb7X\x1b\x19\xf3\x9d+\xa1\x98\x82\xca\xd7;$\xfb\t9\xb0\xbc\xc2\x95\xcf\x82)u\x16)?B \x17+M@\x8cVl\xad\xba\x0f4\x85\xb1\x7f@yqx\xb7\xa5\x04\xbb\x94\xf7\xb5A\x95\xee|\xeb\x8d\x0cyhY\xef\xcb\xb3\xfa>x\x1e\xeegLz\xdd\xe0\x99\xef\xda\xe7\xef\xb2\t]\xbe\x80 !\x05\x83,D\xdb]*v)\xa5\xb0#\x88t\x07T"\xd6)z\x92\xf5o-\x9e\xe7\xf8&+\x9cXe\x02\x03\x01\x00\x01\xa3o0m0\t\x06\x03U\x1d\x13\x04\x020\x000\x0b\x06\x03U\x1d\x0f\x04\x04\x03\x02\x05\xe00\x1d\x06\x03U\x1d\x0e\x04\x16\x04\x14\xa1+ p\xd2k\x80\xe5e\xbc\xeb\x03\x0f\x88\x9ft\xad\xdd\xf6\x130\x1f\x06\x03U\x1d#\x04\x180\x16\x80\x14fS\x94\xf4\x15\xd1\xbdgh\xb0Q725\xe1\xa4\xaa\xde\x07|0\x13\x06\x03U\x1d%\x04\x0c0\n\x06\x08+\x06\x01\x05\x05\x07\x03\x010\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x00\x03\x82\x01\x01\x00\x81\x88\x92sk\x93\xe7\x95\xd6\xddA\xee\x8e\x1e\xbd\xa3HX\xa7A5?{}\xd07\x98\x0e\xb8,\x94w\xc8Q6@\xadY\t(\xc8V\xd6\xea[\xac\xb4\xd8?h\xb7f\xca\xe1V7\xa9\x00e\xeaQ\xc9\xec\xb2iI]\xf9\xe3\xc0\xedaT\xc9\x12\x9f\xc6\xb0\nsU\xe8U5`\xef\x1c6\xf0\xda\xd1\x90wV\x04\xb8\xab8\xee\xf7\t\xc5\xa5\x98\x90#\xea\x1f\xdb\x15\x7f2(\x81\xab\x9b\x85\x02K\x95\xe77Q{\x1bH.\xfb>R\xa3\r\xb4F\xa9\x92:\x1c\x1f\xd7\n\x1eXJ\xfa.Q\x8f)\xc6\x1e\xb8\x0e1\x0es\xf1\'\x88\x17\xca\xc8i\x0c\xfa\x83\xcd\xb3y\x0e\x14\xb0\xb8\x9b/:-\t\xe3\xfc\x06\xf0:n\xfd6;+\x1a\t*\xe8\xab_\x8c@\xe4\x81\xb2\xbc\xf7\x83g\x11nN\x93\xea"\xaf\xff\xa3\x9awWv\xd0\x0b8\xac\xf8\x8a\x945\x8e\xd7\xd4a\xcc\x01\xff$\xb4\x8fa#\xba\x88\xd7Y\xe4\xe9\xba*N\xb5\x15\x0f\x9c\xd0\xea\x06\x91\xd9\xde\xab\x02\x00\x809\x19R\xa4\xab)\x93\x94\xcd\x8a,^\x03\xb9\xf1\x80'
mk = b"\x81\x15\x02\x02\x00\x80\x00\x0b\x01\x00\x00\x00~\xc2\xf9\x9e\x94i\x02\xfe\xed\xc4\xdc\x9b\xe0\xe7 \xa8\xcct'\xf7\xc3\x05\xfc\xa5^\x13\xdc\xaa\xf6\xfa\x88\x9f\xf9\x89\xc5@\x86\xfe\xe3\xe0\x88\xd8\x02B%5\x8e\xeaV\xd9\x08\xd2In\x9aY\xca\x87\x86k\xdf\xc3W\x13x\xff\x98\xb9!bU9\x07R.i\xce\x19\xf0\xa0\xc0\x1af\x87\\M&4\x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b\xe9\xea\xa7F\x83n\xaaP\xc0\x88\x8a\x03\xce"9p\xbcW\xb1r\xac\xcc\xb1\xaa\x08\x85\xb4\xe2'
d1 = b'\x80C\x99\x83z\xc0\xdc\xe7\xf0I\x80\x8c\x8e\x1c\xc7bx\x98\xd3\x84\xd6\x06\xc8r\x9b\x197\xd2\xe9\x08\xc53s\x88 y\x8e)\x9f\xdcE*e\xb2r\xa2$<h\x1c/\x89\x9e\xc0\xc2D9KWB7\xddSi\xb6\xaeE\x13\xe2'
d2 = b"\x80%\xee'\xc9\xb5(\xe3`\xcf\xaf\x1b\xa3 \x81\xad\x8b0\xc2Y\x8eg\xaaV\x90\x92\x02\xfe\xd1\xd0\t\xa3fE\x88\x97\x85\x08\xf5"
import binascii

= Reading SSLv2 session - SSLv2 parsing does not raise any error
t = SSLv2(ch)

= Reading SSLv2 session - Record with cleartext
assert(t.len == 46)
assert(not t.padlen)
assert(not t.mac)
assert(not t.pad)
len(t.msg) == 1

= Reading SSLv2 session - Record __getitem__
SSLv2ClientHello in t

= Reading SSLv2 session - ClientHello
ch = t.msg[0]
assert(isinstance(ch, SSLv2ClientHello))
assert(ch.msgtype == 1)
assert(ch.version == 0x0002)
assert(ch.cipherslen == 21)
assert(not ch.sid)
assert(ch.challengelen == 16)
assert(ch.ciphers == [0x0700c0, 0x050080, 0x030080, 0x010080, 0x060040, 0x040080, 0x020080])
ch.challenge == binascii.unhexlify('1afb2f9ca3d1293454a3152821ffd10c')

= Reading SSLv2 session - ServerHello
t = SSLv2(sh, tls_session=t.tls_session.mirror())
sh = t.msg[0]
assert(isinstance(sh, SSLv2ServerHello))
assert(sh.msgtype == 4)
assert(sh.sid_hit == 0)
assert(sh.certtype == 1)
assert(sh.version == 0x0002)
assert(sh.certlen == 930)
assert(sh.cipherslen == 3)
assert(sh.connection_idlen == 16)
assert(isinstance(sh.cert, Cert))
assert(len(sh.cert.der) == 930)
assert(sh.cert.subject_str == '/C=MN/L=Ulaanbaatar/OU=Scapy Test PKI/CN=Scapy Test Server')
assert(sh.ciphers == [0x020080])
sh.connection_id == binascii.unhexlify('391952a4ab299394cd8a2c5e03b9f180')

= Reading SSLv2 session - ClientMasterKey with unknown server key
t_enc = SSLv2(mk)
mk_enc = t_enc.msg[0]
assert(mk_enc.clearkeylen == 11)
assert(mk_enc.encryptedkeylen == 256)
assert(mk_enc.clearkey == binascii.unhexlify('7ec2f99e946902feedc4dc'))
assert(mk_enc.encryptedkey[:3] == b"\x9b\xe0\xe7" and mk_enc.encryptedkey[-3:] == b"\xea\x57\x2e")
assert(t_enc.tls_session.pwcs.tls_version == 0x0002)
assert(t_enc.tls_session.prcs.tls_version == 0x0002)
mk_enc.decryptedkey is None

= Reading SSLv2 session - Importing server compromised key
import os
filename = scapy_path("/test/tls/pki/srv_key.pem")
rsa_key = PrivKeyRSA(filename)
t.tls_session.server_rsa_key = rsa_key

= Reading SSLv2 session - ClientMasterKey with compromised server key
t = SSLv2(mk, tls_session=t.tls_session.mirror())
assert(t.len == 277 and not t.padlen and not t.mac and not t.pad)
mk = t.msg[0]
assert(isinstance(mk, SSLv2ClientMasterKey))
assert(mk.msgtype == 2)
assert(mk.cipher == 0x020080)
assert(mk.clearkeylen == 11)
assert(mk.encryptedkeylen == 256)
assert(mk.keyarglen == 0)
assert(mk.clearkey == binascii.unhexlify('7ec2f99e946902feedc4dc'))
assert(mk.decryptedkey == binascii.unhexlify('e2d430fc04'))
not mk.keyarg

= Reading SSLv2 session - Checking session secrets
s = t.tls_session
assert(s.sslv2_common_cs == [0x020080])
assert(s.sslv2_challenge == ch.challenge)
assert(not s.pre_master_secret)
assert(s.master_secret == b'~\xc2\xf9\x9e\x94i\x02\xfe\xed\xc4\xdc\xe2\xd40\xfc\x04')
assert(s.sslv2_key_material == b'\xf4\xae\x00\x03kB>\x06\xba[\xd7\xea,\x08\xc2\xae\xba\xf3\x10\xbf\xea\x08\x8flV\x11D\xc5L\xad3\xf9')
assert(s.rcs.cipher.key == b'\xba\xf3\x10\xbf\xea\x08\x8flV\x11D\xc5L\xad3\xf9')
s.wcs.cipher.key == b'\xf4\xae\x00\x03kB>\x06\xba[\xd7\xea,\x08\xc2\xae'

= Reading SSLv2 session - Record with ciphertext
t = SSLv2(sv, tls_session=t.tls_session.mirror())
assert(t.len == 33)
assert(not t.padlen)
assert(t.mac == b'?:\xf3vE\xf3\xe83\x1a\xd0\xab\xba\xb6\x86\xe6\x89')
not t.pad

= Reading SSLv2 session - ServerVerify
sv = t.msg[0]
assert(isinstance(sv, SSLv2ServerVerify))
assert(sv.msgtype == 5)
sv.challenge == ch.challenge

= Reading SSLv2 session - ClientFinished
t = SSLv2(cf, tls_session=t.tls_session.mirror())
cf = t.msg[0]
assert(isinstance(cf, SSLv2ClientFinished))
assert(cf.msgtype == 3)
cf.connection_id == sh.connection_id

= Reading SSLv2 session - RequestCertificate
t = SSLv2(rc, tls_session=t.tls_session.mirror())
rc = t.msg[0]
assert(isinstance(rc, SSLv2RequestCertificate))
assert(rc.msgtype == 7)
assert(rc.authtype == 1)
rc.challenge == binascii.unhexlify('19619ddf7384d68e7a614ae1989ab41e')

= Reading SSLv2 session - ClientCertificate
t = SSLv2(cc, tls_session=t.tls_session.mirror())
cc = t.msg[0]
assert(isinstance(cc, SSLv2ClientCertificate))
assert(cc.msgtype == 8)
assert(cc.certtype == 1)
assert(cc.certlen == 930)
assert(cc.responselen == 256)
assert(isinstance(cc.certdata, Cert))
assert(len(cc.certdata.der) == 930)
assert(cc.certdata.subject_str == '/C=MN/L=Ulaanbaatar/OU=Scapy Test PKI/CN=Scapy Test Client')
assert(len(cc.responsedata.sig_val) == 256)
cc.responsedata.sig_val[:4] == b"\x81#\x95\xb5" and cc.responsedata.sig_val[-4:] == b"RM6\xd3"

= Reading SSLv2 session - ServerFinished
t = SSLv2(sf, tls_session=t.tls_session.mirror())
sf = t.msg[0]
assert(isinstance(sf, SSLv2ServerFinished))
assert(sf.msgtype == 6)
sf.sid == binascii.unhexlify('11c1e8070b2cf249ad3d85caf8854bc8')

= Reading SSLv2 session - Application data
t1 = SSLv2(d1, tls_session=t.tls_session.mirror())
data1 = t1.msg[0]
assert(isinstance(data1, Raw))
assert(data1.load == b'These are horrendous parameters for a TLS session.\n')
t2 = SSLv2(d2, tls_session=t1.tls_session)
data2 = t2.msg[0]
assert(isinstance(data2, Raw))
data2.load ==  b'*nothing to do here*\n'

= Reading SSLv2 session - Checking final sequence numbers
t2.tls_session.rcs.seq_num == 6 and t2.tls_session.wcs.seq_num == 4


###############################################################################
####################### TLS-related scapy internals ###########################
###############################################################################

+ Check TLS-related scapy internals

= Check TLS-related scapy internals - Checking raw() harmlessness (with RC4)
t1.show()
assert(t1.msg[0].load == b'These are horrendous parameters for a TLS session.\n')
assert(t1.tls_session.rcs.seq_num == 6 and t1.tls_session.wcs.seq_num == 4)
d1 == raw(t1)

= Check TLS-related scapy internals - Checking show2() harmlessness (with RC4)
t2.show2()
assert(t2.msg[0].load == b'*nothing to do here*\n')
assert(t2.tls_session.rcs.seq_num == 6 and t2.tls_session.wcs.seq_num == 4)
d2 == raw(t2)

= Check TLS-related scapy internals - Checking show2() harmlessness (with 3DES)
# These are also taken from a s_client/s_server session.
ch = b'\x80.\x01\x00\x02\x00\x15\x00\x00\x00\x10\x07\x00\xc0\x05\x00\x80\x03\x00\x80\x01\x00\x80\x06\x00@\x04\x00\x80\x02\x00\x80!2bc\x97^\xa3\r9\x14*\xe4\xd6\xd4\n\x1e'
sh = b'\x83\xd2\x04\x00\x01\x00\x02\x03\xa2\x00\x15\x00\x100\x82\x03\x9e0\x82\x02\x86\xa0\x03\x02\x01\x02\x02\t\x00\xfe\x04W\r\xc7\'\xe9\xf60\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x000T1\x0b0\t\x06\x03U\x04\x06\x13\x02MN1\x140\x12\x06\x03U\x04\x07\x0c\x0bUlaanbaatar1\x170\x15\x06\x03U\x04\x0b\x0c\x0eScapy Test PKI1\x160\x14\x06\x03U\x04\x03\x0c\rScapy Test CA0\x1e\x17\r160916102811Z\x17\r260915102811Z0X1\x0b0\t\x06\x03U\x04\x06\x13\x02MN1\x140\x12\x06\x03U\x04\x07\x0c\x0bUlaanbaatar1\x170\x15\x06\x03U\x04\x0b\x0c\x0eScapy Test PKI1\x1a0\x18\x06\x03U\x04\x03\x0c\x11Scapy Test Server0\x82\x01"0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x82\x01\x0f\x000\x82\x01\n\x02\x82\x01\x01\x00\xcc\xf1\xf1\x9b`-`\xae\xf2\x98\r\')\xd9\xc0\tYL\x0fJ0\xa8R\xdf\xe5\xb1!\x9fO\xc3=V\x93\xdd_\xc6\xf7\xb3\xf6U\x8b\xe7\x92\xe2\xde\xf2\x85I\xb4\xa1,\xf4\xfdv\xa8g\xca\x04 `\x11\x18\xa6\xf2\xa9\xb6\xa6\x1d\xd9\xaa\xe5\xd9\xdb\xaf\xe6\xafUW\x9f\xffR\x89e\xe6\x80b\x80!\x94\xbc\xcf\x81\x1b\xcbg\xc2\x9d\xb5\x05w\x04\xa6\xc7\x88\x18\x80xh\x956\xde\x97\x1b\xb6a\x87B\x1au\x98E\x82\xeb>2\x11\xc8\x9b\x86B9\x8dM\x12\xb7X\x1b\x19\xf3\x9d+\xa1\x98\x82\xca\xd7;$\xfb\t9\xb0\xbc\xc2\x95\xcf\x82)u\x16)?B \x17+M@\x8cVl\xad\xba\x0f4\x85\xb1\x7f@yqx\xb7\xa5\x04\xbb\x94\xf7\xb5A\x95\xee|\xeb\x8d\x0cyhY\xef\xcb\xb3\xfa>x\x1e\xeegLz\xdd\xe0\x99\xef\xda\xe7\xef\xb2\t]\xbe\x80 !\x05\x83,D\xdb]*v)\xa5\xb0#\x88t\x07T"\xd6)z\x92\xf5o-\x9e\xe7\xf8&+\x9cXe\x02\x03\x01\x00\x01\xa3o0m0\t\x06\x03U\x1d\x13\x04\x020\x000\x0b\x06\x03U\x1d\x0f\x04\x04\x03\x02\x05\xe00\x1d\x06\x03U\x1d\x0e\x04\x16\x04\x14\xa1+ p\xd2k\x80\xe5e\xbc\xeb\x03\x0f\x88\x9ft\xad\xdd\xf6\x130\x1f\x06\x03U\x1d#\x04\x180\x16\x80\x14fS\x94\xf4\x15\xd1\xbdgh\xb0Q725\xe1\xa4\xaa\xde\x07|0\x13\x06\x03U\x1d%\x04\x0c0\n\x06\x08+\x06\x01\x05\x05\x07\x03\x010\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x00\x03\x82\x01\x01\x00\x81\x88\x92sk\x93\xe7\x95\xd6\xddA\xee\x8e\x1e\xbd\xa3HX\xa7A5?{}\xd07\x98\x0e\xb8,\x94w\xc8Q6@\xadY\t(\xc8V\xd6\xea[\xac\xb4\xd8?h\xb7f\xca\xe1V7\xa9\x00e\xeaQ\xc9\xec\xb2iI]\xf9\xe3\xc0\xedaT\xc9\x12\x9f\xc6\xb0\nsU\xe8U5`\xef\x1c6\xf0\xda\xd1\x90wV\x04\xb8\xab8\xee\xf7\t\xc5\xa5\x98\x90#\xea\x1f\xdb\x15\x7f2(\x81\xab\x9b\x85\x02K\x95\xe77Q{\x1bH.\xfb>R\xa3\r\xb4F\xa9\x92:\x1c\x1f\xd7\n\x1eXJ\xfa.Q\x8f)\xc6\x1e\xb8\x0e1\x0es\xf1\'\x88\x17\xca\xc8i\x0c\xfa\x83\xcd\xb3y\x0e\x14\xb0\xb8\x9b/:-\t\xe3\xfc\x06\xf0:n\xfd6;+\x1a\t*\xe8\xab_\x8c@\xe4\x81\xb2\xbc\xf7\x83g\x11nN\x93\xea"\xaf\xff\xa3\x9awWv\xd0\x0b8\xac\xf8\x8a\x945\x8e\xd7\xd4a\xcc\x01\xff$\xb4\x8fa#\xba\x88\xd7Y\xe4\xe9\xba*N\xb5\x15\x0f\x9c\xd0\xea\x06\x91\xd9\xde\xab\x07\x00\xc0\x05\x00\x80\x03\x00\x80\x01\x00\x80\x06\x00@\x04\x00\x80\x02\x00\x80\x03\xea\xd5\x88T&\xe7\\\xc9wM\x05\x1fo\xbf\xec'
mk = b'\x81\x12\x02\x07\x00\xc0\x00\x00\x01\x00\x00\x08(\xc98#b\xc1\x94\x9e\xf1|\xd3\x98/\x84\xa6\xfb\x1e}b7\xf1\xfa9\xc3\xb4A\xe4\xb7\x97\xcd\x0c\xd4\x81\x82\xee\x8b\x80f]_\xbc\xe5\xeb\xe4}b\x82 \xa1S\xd5\xbe\xb3\xf7\xbb\x1c]zm\xe6\xc5\x95\xe3Y\x9d\x84b\xf2\x89\x08i\xf9"\x8d\xf7\xb9\xe3\xb5\xcb\x90\xc2V\xcel\x14\xb0\xd4-\xb3\xd3\xfe\x83\x8a(\x025\xd0Y\x9b4M\xde\xc6\x99{H\x89u.\xfa\x17\x13|\xd39\xf6sc\xaat\x9fy\xf69s9\xbfM\xdc\xcdT\x8d~U"\xdd\xce\xab\xfa\x0e\xa9\x90$s&\x0c8\xe4\x13b\x15\xc7\xc2\r#\xc96\x04{\x14\xd0\x19\xef\x13ql\x07\xbf\'\xfb\xdc\x14t\xf6I\xe6\x0b\xe7\xf2\xd6e\'%2H\x81\x16\x0bT;0\x95G%E\xc559p\x85S\x07\xbf\x03\xb0^\x06\xf0\xdcL\xd4\xf2\x9b\xf7\xc6T\xdb%MS\x8ch\xb5\x91H\xffF\xf0\xafCw\x1a:7\x1f\xf8\x05\x944\xc1p\xb6\x8e\x12.#,a\xd4s\xc7\x9f\xe5\xbf\xcb\xee\x1aS\xa71\x15\xf5pE'
sv = b'\x00(\x07q\x8c\x08\xdb\xa8\xaa\x8d\x87\x0b\xe8\x01^\xed\x87\x8e\xb2\xc0\xa9\x84\x11v)~\xf8\xd9,\xea\xe2\x05\x86\x1d\x01n\xe1\xe6\xccE[\xcej'
t = SSLv2(ch)
t.show2()
challenge = t.msg[0].challenge
t = SSLv2(sh, tls_session=t.tls_session.mirror())
t.show2()
t.tls_session.server_rsa_key = rsa_key
t = SSLv2(mk, tls_session=t.tls_session.mirror())
t.show2()
t.show2()
t = SSLv2(sv, tls_session=t.tls_session.mirror())
t.show2()
t.show2()
assert(t.padlen == 7)
assert(t.mac == b'\xe7\xe4\x08\x0e\x86\xc4\x93\t\x80l/\x80\xdaQ\xa0z')
assert(t.tls_session.rcs.seq_num == 2)
assert(t.tls_session.wcs.seq_num == 2)
t.msg[0].challenge == challenge

= Check TLS-related scapy internals - Checking tls_session freeze during show2()
l = len(t.tls_session.handshake_messages)
t.show2()
l == len(t.tls_session.handshake_messages)

= Check TLS-related scapy internals - Checking SSLv2 cast from TLS class
t = TLS(ch)
assert(isinstance(t, SSLv2))
t.msg[0].challenge == challenge


###############################################################################
######################### Building SSLv2 packets ##############################
###############################################################################

+ Build SSLv2 packets

= Building SSLv2 packets - Various default messages
raw(SSLv2())
raw(SSLv2Error())
raw(SSLv2ClientHello())
raw(SSLv2ServerHello())
raw(SSLv2ClientMasterKey())
raw(SSLv2ServerVerify())
raw(SSLv2ClientFinished())
raw(SSLv2RequestCertificate())
raw(SSLv2ClientCertificate())
raw(SSLv2ServerFinished())

= Building SSLv2 packets - Error within clear record
t = SSLv2(msg=SSLv2Error(code='no_cipher'))
raw(t) == b'\x80\x03\x00\x00\x01'

= Building SSLv2 packets - ClientHello with automatic length computation
ch_pkt = SSLv2ClientHello()
ch_pkt.msgtype = 'client_hello'
ch_pkt.version = 0x0002
ch_pkt.ciphers = [SSL_CK_DES_192_EDE3_CBC_WITH_MD5, SSL_CK_IDEA_128_CBC_WITH_MD5, SSL_CK_RC2_128_CBC_WITH_MD5, SSL_CK_RC4_128_WITH_MD5, SSL_CK_DES_64_CBC_WITH_MD5, SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5, SSL_CK_RC4_128_EXPORT40_WITH_MD5]
ch_pkt.challenge = b'!2bc\x97^\xa3\r9\x14*\xe4\xd6\xd4\n\x1e'
t = SSLv2(msg=ch_pkt)
raw(t) == ch

= Building SSLv2 packets - ClientMasterKey context linking
mk = SSLv2ClientMasterKey(cipher='SSL_CK_DES_192_EDE3_CBC_WITH_MD5', clearkey=b'\xff'*19, encryptedkey=b'\0'*256, decryptedkey=b'\xaa'*5, keyarg=b'\x01'*8)
t = SSLv2(msg=mk)
t.tls_session.sslv2_connection_id = b'\xba'*16
t.tls_session.sslv2_challenge = b'\x42'*16
t.raw_stateful()
s = t.tls_session
assert(s.master_secret == b'\xff'*19 + b'\xaa'*5)
assert(isinstance(s.rcs.ciphersuite, SSL_CK_DES_192_EDE3_CBC_WITH_MD5))
assert(isinstance(s.wcs.ciphersuite, SSL_CK_DES_192_EDE3_CBC_WITH_MD5))
s.rcs.cipher.iv == b'\x01'*8
s.wcs.cipher.iv == b'\x01'*8


###############################################################################
############################ Automaton behaviour ##############################
###############################################################################

# see test/tls/tests_tls_netaccess.uts

