Repository: secdev/scapy

Issue:
TLS dissection error
Occurred during a sniff on master.

```
Z:\Coding\github\scapy\scapy\layers\tls\cert.py in __call__(cls, cert_path)
    574             cert = X509_Cert(obj.der)
    575         except Exception:
--> 576             raise Exception("Unable to import certificate")
    577         obj.import_from_asn1pkt(cert)
    578         return obj

Exception: Unable to import certificate
```

To reproduce, from `debug.crashed_on`:
```
pkt = Ether(b'\xd0P\x99V\xdd\xf9\x14\x0cv\x8f\xfe(\x86\xdd`\x0f\xb1\xbe\x04\xd8\x06{*\x00\x14P@\x07\x08\x16\x00\x00\x00\x00\x00\x00 \x04*\x01\x0e5/\x17\xfe`\x18\xb5\x9b\xfc \xc8\xc5\xa9\x01\xbb\xd7o{\xc0\x89\n\x91\xec\xf2\x9eP\x10\x00\x8d\xb3\x1b\x00\x00\xc00\x04J<L\xe3\xe7la\x8f\xf8\xb8\xaeC\xaa\xfe\x98\xd9\xb3b\xa3\xc3\x04\x10\xa0\xb3\x19U\xfaX\xd3\x86\x92D\xd1Ti\x07\x00N\x9e\x0b8~\x1d\xfa\xdc\xfc\x8dA\xaf\x12b\xbe\x19\x8a\xd8,\xe4nb2x\xca\x0bk\xd7~\x07\x11\x19\xe1\xc5x\xbf\xa9C\xa7[\x10\xd2\xbe\xd7\xdf\x18xh\xcc6\x06V\xfb\x0e\xf3\xdd\xb3\x11\xde,\xfd\xc6\x16\x04\xf0MB\xac\x05\xb4f\xa39*eD\xdd\xaa\t\x8f\x99\x16h\xa33\xc71\xc8\xeb\xf7W\xd1\xec\xe6\x13\x96XHq~\x8f\xf3\xfcR\xfc\x06\xb9\r\xe9\xe6\xfa=J\xb1\x81\xfc\xd2\xc8\xec\xdc\x93\x9b%\x86\xa7\\\xe3G\x8d\x0f\xab\xf5b\x97\xac\xe6\xa6\xcb\xdf.e\x00\x0cQ\xf7\x9c\xd8e\xc6\x06\x8b\x9e\xdbm\xd8N\x00\xc4\x1f\x1f\xeb@\x12\xbb`z\xbcB\xf5\x1e\xf0rJQ\x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d9aP\xd4<8p\x92\xc2\x13Eo&\x85\xcfu\xe3\'D\t\xfaM`\x01x\xe06\x18T\xa4\xd9\x858\xbe\xa0w\xa2wq\x1c\xf6\xd43a\x00\xbd\xa0o\xfd\x06\xd90U\x8e\xb2V\x14\x08 <\xff\xa9y:\xde\xd1p\xc9}#\x0e\xa5\xfbA"7\x89\xd5\xd1\xaf\xd6\x82\xbe \x8a\x92E\x01\xb0T\xea:U\xa4t\xbcm><j\xc3\xb0\'\x19g\x95S\x83\x93\x12\x8b\x1eF;TCJ\x95n\x01\xfdt\xe8\x84CM\xc5\x01\x9a\x8c\xdf\x84{\x96e\xee\x83\xd3>7\xb3\x9af\xc6aQN]\xf3\x93b\x95%q\xf9\xe653\x96I\xe9FO\x92\xa7\xec\xb1w\xbd\x01.v\xdb\x03\xad\xa2\xba\xb3\xea\xfa\xaf\xd7L\xaa\xb7\x0eZ\xba\x97\xb8s\x922\xf2\xd8[B\x8e\xd5\xdd$\xa2\xac\xec\xf4\xca\xe0\xf3\x18\xff\x8c\x17\xb4\xe2K\xb08)\xc3\xcfW\xc1te\xb31\x05,\xc8\x03i%{')
```

Another one triggered by an [AppVeyor build](https://ci.appveyor.com/project/secdev/scapy/builds/23837282/job/t0k08m71ledk5e32) & thanks to UTscapy analysis:

```
Ether(hex_bytes('00155dfb587a00155dfb58430800450005dc54d3400070065564400410d40a00000d01bb044e8b86744e16063ac45010faf06ba9000016030317c30200005503035cb336a067d53a5d2cedbdfec666ac740afbd0637ddd13eddeab768c3c63abee20981a0000d245f1c905b329323ad67127cd4b907a49f775c331d0794149aca7cdc02800000d0005000000170000ff010001000b000ec6000ec300090530820901308206e9a00302010202132000036e72aded906765595fae000000036e72300d06092a864886f70d01010b050030818b310b3009060355040613025553311330110603550408130a57617368696e67746f6e3110300e060355040713075265646d6f6e64311e301c060355040a13154d6963726f736f667420436f72706f726174696f6e31153013060355040b130c4d6963726f736f6674204954311e301c060355040313154d6963726f736f667420495420544c532043412032301e170d3138303632393139303635355a170d3230303632393139303635355a301b3119301706035504030c102a2e6c6f67696e2e6c6976652e636f6d30820122300d06092a864886f70d01010105000382010f003082010a0282010100bd1d4afe8064b0b1296b6de1b1983ed1a9468770757eea26ff0a2ecde5c789a745becfdc9d07318c113cf8840f57c9379be097563dfbda7d124a4b3c2aea139ab5d9c281e19f74288aa0b33a078bc67ae70fa02083addf4d0a49e36764238d396b718d227cc5965d5f5e22aa40e0d8c6a45595f0ac0b173af5559fc83abf31d16433f29b91b61e5dabad5c8c92110a80209796937cf2836c35815d9d6b5527972a94608e03a24faf2dacc9f17a177e34061e3ef321602bf92809e473bf08ae5a97faae517f7f78e5908aa4c0a278b2ebfb3e15c544d8aa7e12d5e655f9d9bdc02e0b2adcdb3c641d94d04c32ffe10f569bf5ca48cda8b7da14bd7dea95089a210203010001a38204cb308204c7308201f3060a2b06010401d679020402048201e3048201df01dd007500a4b90990b418581487bb13a2cc67700a3c359804f91bdfb8e377cd0ec80ddc10000001644cfa2b4f00000403004630440220638bcbfa1bf0c762296edac11109c8fc53c08b04c5c7e33065467b09dbb1ffb602207c9f0ea8006081f217d8fd1d81ea62589f5a464aa04c9b0bb0c2d67706d3a57d007500ddeb1d2b7a0d4fa6208b81ad8168707e2e8e9d01d55c888d3d11c4cdb6ecbecc000001644cfa2d91000004030046304402200c24fb3b1f4ac6776924568e15d8f57492f7822af4a6cb1a2fce3361634c946802202a24cf48ec44b189700713d95a9237b7b64f067306df29e686eb0e14b285451c0075005ea773f9df56c0e7b536487dd049e0327a919a0c84a112128418759681714558000001644cfa30c90000040300463044022036b8efe060ff0e40d343953e2dcfc5d78126cbc8fa30c6e7c4a5bf3335da29c002207d6002e4f618df32c583af2669da4e16e05b590b622bf89a0a3d3dfdf615e50e007600bc78e1dfc5f63c684649334da10fa15f0979692009c081b4f3f6917f3ed9b8a5000001644cfa2c630000040300473045022027f9b69d95d18a378830b5c2a1b1df0e84d89bf7da52fffe9f7157d624733638022100d65fba5f0e74d63579be1cd5c7a2bce6b4195400539297329911384d7918c72b302706092b060104018237150a041a3018300a06082b06010505070302300a06082b06010505070301303e06092b06010401823715070431302f06272b060104018237150887da867583eed90182c9851b81b59e6185f4eb60815d84d2df4282e7937a02016402011d30818506082b0601050507010104793077305106082b060105050730028645687474703a2f2f7777772e6d6963726f736f66742e636f6d2f706b692f6d73636f72702f4d6963726f736f66742532304954253230544c532532304341253230322e637274302206082b060105050730018616687474703a2f2f6f6373702e6d736f6373702e636f6d301d0603551d0e041604141154417dbc650ff6a11bb27d015e559bce915a6b300b0603551d0f0404030204b030730603551d11046c306a822070'))
```


Discussion:
Does wireshark manage to parse the TLS message that include the certificate?

Resolving patch:
diff --git a/scapy/layers/tls/extensions.py b/scapy/layers/tls/extensions.py
--- a/scapy/layers/tls/extensions.py
+++ b/scapy/layers/tls/extensions.py
@@ -684,7 +684,7 @@ def i2m(self, pkt, i):
 
     def m2i(self, pkt, m):
         res = []
-        while m:
+        while len(m) > 4:
             t = struct.unpack("!H", m[:2])[0]
             tmp_len = struct.unpack("!H", m[2:4])[0]
             cls = _tls_ext_cls.get(t, TLS_Ext_Unknown)


Test patch:
diff --git a/test/tls.uts b/test/tls.uts
--- a/test/tls.uts
+++ b/test/tls.uts
@@ -1121,6 +1121,11 @@ def test_tls_without_cryptography(get_algs_from_ciphersuite_name_mock):
 
 test_tls_without_cryptography()
 
+= Truncated TCP segment
+
+pkt = Ether(hex_bytes('00155dfb587a00155dfb58430800450005dc54d3400070065564400410d40a00000d01bb044e8b86744e16063ac45010faf06ba9000016030317c30200005503035cb336a067d53a5d2cedbdfec666ac740afbd0637ddd13eddeab768c3c63abee20981a0000d245f1c905b329323ad67127cd4b907a49f775c331d0794149aca7cdc02800000d0005000000170000ff010001000b000ec6000ec300090530820901308206e9a00302010202132000036e72aded906765595fae000000036e72300d06092a864886f70d01010b050030818b310b30090603550406130255533113'))
+assert TLSServerHello in pkt
+
 ###############################################################################
 ########################### TLS Misc tests ####################################
 ###############################################################################