aboutsummaryrefslogtreecommitdiffstats
path: root/test/patches/scapy-2.4.3/ipsec.patch
blob: 12c24e5057f9eadcfc5b9ca942c48005ea2a2e59 (plain)
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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { co
diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
index ae057ee1..d7a21e8b 100644
--- a/scapy/layers/ipsec.py
+++ b/scapy/layers/ipsec.py
@@ -56,6 +56,7 @@ from scapy.fields import ByteEnumField, ByteField, IntField, PacketField, \
     ShortField, StrField, XIntField, XStrField, XStrLenField
 from scapy.packet import Packet, bind_layers, Raw
 from scapy.layers.inet import IP, UDP
+from scapy.contrib.mpls import MPLS
 import scapy.modules.six as six
 from scapy.modules.six.moves import range
 from scapy.layers.inet6 import IPv6, IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, \
@@ -138,6 +139,7 @@ bind_layers(IP, ESP, proto=socket.IPPROTO_ESP)
 bind_layers(IPv6, ESP, nh=socket.IPPROTO_ESP)
 bind_layers(UDP, ESP, dport=4500)  # NAT-Traversal encapsulation
 bind_layers(UDP, ESP, sport=4500)  # NAT-Traversal encapsulation
+bind_layers(UDP, ESP, dport=4545)  # NAT-Traversal encapsulation - random port
 
 ###############################################################################
 
@@ -359,11 +361,8 @@ class CryptAlgo(object):
             encryptor = cipher.encryptor()
 
             if self.is_aead:
-                if esn_en:
-                    aad = struct.pack('!LLL', esp.spi, esn, esp.seq)
-                else:
-                    aad = struct.pack('!LL', esp.spi, esp.seq)
-                encryptor.authenticate_additional_data(aad)
+                encryptor.authenticate_additional_data(sa.build_aead(esp))
+
                 data = encryptor.update(data) + encryptor.finalize()
                 data += encryptor.tag[:self.icv_size]
             else:
@@ -400,12 +399,7 @@ class CryptAlgo(object):
 
             if self.is_aead:
                 # Tag value check is done during the finalize method
-                if esn_en:
-                    decryptor.authenticate_additional_data(
-                        struct.pack('!LLL', esp.spi, esn, esp.seq))
-                else:
-                    decryptor.authenticate_additional_data(
-                        struct.pack('!LL', esp.spi, esp.seq))
+                decryptor.authenticate_additional_data(sa.build_aead(esp))
             try:
                 data = decryptor.update(data) + decryptor.finalize()
             except InvalidTag as err:
@@ -445,6 +439,7 @@ if algorithms:
     CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
                                        cipher=algorithms.AES,
                                        mode=modes.CTR,
+                                       block_size=1,
                                        iv_size=8,
                                        salt_size=4,
                                        format_mode_iv=_aes_ctr_format_mode_iv)
@@ -452,6 +447,7 @@ if algorithms:
     CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM',
                                        cipher=algorithms.AES,
                                        mode=modes.GCM,
+                                       block_size=1,
                                        salt_size=4,
                                        iv_size=8,
                                        icv_size=16,
@@ -460,6 +456,7 @@ if algorithms:
         CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
                                            cipher=algorithms.AES,
                                            mode=modes.CCM,
+                                           block_size=1,
                                            iv_size=8,
                                            salt_size=3,
                                            icv_size=16,
@@ -544,7 +541,7 @@ class AuthAlgo(object):
         else:
             return self.mac(key, self.digestmod(), default_backend())
 
-    def sign(self, pkt, key):
+    def sign(self, pkt, key, trailer=None):
         """
         Sign an IPsec (ESP or AH) packet with this algo.
 
@@ -560,16 +557,20 @@ class AuthAlgo(object):
 
         if pkt.haslayer(ESP):
             mac.update(raw(pkt[ESP]))
+            if trailer:
+                mac.update(trailer)
             pkt[ESP].data += mac.finalize()[:self.icv_size]
 
         elif pkt.haslayer(AH):
             clone = zero_mutable_fields(pkt.copy(), sending=True)
             mac.update(raw(clone))
+            if trailer:
+                mac.update(trailer)
             pkt[AH].icv = mac.finalize()[:self.icv_size]
 
         return pkt
 
-    def verify(self, pkt, key):
+    def verify(self, pkt, key, trailer):
         """
         Check that the integrity check value (icv) of a packet is valid.
 
@@ -600,6 +601,8 @@ class AuthAlgo(object):
             clone = zero_mutable_fields(pkt.copy(), sending=False)
 
         mac.update(raw(clone))
+        if trailer:
+            mac.update(trailer) # bytearray(4)) #raw(trailer))
         computed_icv = mac.finalize()[:self.icv_size]
 
         # XXX: Cannot use mac.verify because the ICV can be truncated
@@ -788,7 +791,7 @@ class SecurityAssociation(object):
     This class is responsible of "encryption" and "decryption" of IPsec packets.  # noqa: E501
     """
 
-    SUPPORTED_PROTOS = (IP, IPv6)
+    SUPPORTED_PROTOS = (IP, IPv6, MPLS)
 
     def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
                  auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, esn_en=False, esn=0):   # noqa: E501
@@ -862,6 +865,23 @@ class SecurityAssociation(object):
                 raise TypeError('nat_t_header must be %s' % UDP.name)
         self.nat_t_header = nat_t_header
 
+    def build_aead(self, esp):
+        if self.esn_en:
+            return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
+        else:
+            return (struct.pack('!LL', esp.spi, esp.seq))
+
+    def build_seq_num(self, num):
+        # only lower order bits are  transmitted
+        # higher order bits are used in the ICV
+        lower = num & 0xffffffff
+        upper = num >> 32
+
+        if self.esn_en:
+            return lower, struct.pack("!I", upper)
+        else:
+            return lower, None
+
     def check_spi(self, pkt):
         if pkt.spi != self.spi:
             raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
@@ -875,7 +895,8 @@ class SecurityAssociation(object):
             if len(iv) != self.crypt_algo.iv_size:
                 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)  # noqa: E501
 
-        esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
+        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
+        esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
 
         if self.tunnel_header:
             tunnel = self.tunnel_header.copy()
@@ -899,7 +920,7 @@ class SecurityAssociation(object):
                                       esn_en=esn_en or self.esn_en,
                                       esn=esn or self.esn)
 
-        self.auth_algo.sign(esp, self.auth_key)
+        self.auth_algo.sign(esp, self.auth_key, high_seq_num)
 
         if self.nat_t_header:
             nat_t_header = self.nat_t_header.copy()
@@ -926,7 +947,8 @@ class SecurityAssociation(object):
 
     def _encrypt_ah(self, pkt, seq_num=None):
 
-        ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
+        low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
+        ah = AH(spi=self.spi, seq=low_seq_num,
                 icv=b"\x00" * self.auth_algo.icv_size)
 
         if self.tunnel_header:
@@ -966,7 +988,8 @@ class SecurityAssociation(object):
         else:
             ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
 
-        signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)  # noqa: E501
+        signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
+                                         self.auth_key, high_seq_num)  # noqa: E501
 
         # sequence number must always change, unless specified by the user
         if seq_num is None:
@@ -1003,11 +1026,12 @@ class SecurityAssociation(object):
 
     def _decrypt_esp(self, pkt, verify=True, esn_en=None, esn=None):
 
+        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
         encrypted = pkt[ESP]
 
         if verify:
             self.check_spi(pkt)
-            self.auth_algo.verify(encrypted, self.auth_key)
+            self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
 
         esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
                                       self.crypt_algo.icv_size or
@@ -1048,9 +1072,10 @@ class SecurityAssociation(object):
 
     def _decrypt_ah(self, pkt, verify=True):
 
+        low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
         if verify:
             self.check_spi(pkt)
-            self.auth_algo.verify(pkt, self.auth_key)
+            self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
 
         ah = pkt[AH]
         payload = ah.payload