1 diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
2 index ae057ee1..d7a21e8b 100644
3 --- a/scapy/layers/ipsec.py
4 +++ b/scapy/layers/ipsec.py
5 @@ -56,6 +56,7 @@ from scapy.fields import ByteEnumField, ByteField, IntField, PacketField, \
6 ShortField, StrField, XIntField, XStrField, XStrLenField
7 from scapy.packet import Packet, bind_layers, Raw
8 from scapy.layers.inet import IP, UDP
9 +from scapy.contrib.mpls import MPLS
10 import scapy.modules.six as six
11 from scapy.modules.six.moves import range
12 from scapy.layers.inet6 import IPv6, IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, \
13 @@ -138,6 +139,7 @@ bind_layers(IP, ESP, proto=socket.IPPROTO_ESP)
14 bind_layers(IPv6, ESP, nh=socket.IPPROTO_ESP)
15 bind_layers(UDP, ESP, dport=4500) # NAT-Traversal encapsulation
16 bind_layers(UDP, ESP, sport=4500) # NAT-Traversal encapsulation
17 +bind_layers(UDP, ESP, dport=4545) # NAT-Traversal encapsulation - random port
19 ###############################################################################
21 @@ -359,11 +361,8 @@ class CryptAlgo(object):
22 encryptor = cipher.encryptor()
26 - aad = struct.pack('!LLL', esp.spi, esn, esp.seq)
28 - aad = struct.pack('!LL', esp.spi, esp.seq)
29 - encryptor.authenticate_additional_data(aad)
30 + encryptor.authenticate_additional_data(sa.build_aead(esp))
32 data = encryptor.update(data) + encryptor.finalize()
33 data += encryptor.tag[:self.icv_size]
35 @@ -400,12 +399,7 @@ class CryptAlgo(object):
38 # Tag value check is done during the finalize method
40 - decryptor.authenticate_additional_data(
41 - struct.pack('!LLL', esp.spi, esn, esp.seq))
43 - decryptor.authenticate_additional_data(
44 - struct.pack('!LL', esp.spi, esp.seq))
45 + decryptor.authenticate_additional_data(sa.build_aead(esp))
47 data = decryptor.update(data) + decryptor.finalize()
48 except InvalidTag as err:
49 @@ -445,6 +439,7 @@ if algorithms:
50 CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
51 cipher=algorithms.AES,
56 format_mode_iv=_aes_ctr_format_mode_iv)
57 @@ -452,6 +447,7 @@ if algorithms:
58 CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM',
59 cipher=algorithms.AES,
65 @@ -460,6 +456,7 @@ if algorithms:
66 CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
67 cipher=algorithms.AES,
73 @@ -544,7 +541,7 @@ class AuthAlgo(object):
75 return self.mac(key, self.digestmod(), default_backend())
77 - def sign(self, pkt, key):
78 + def sign(self, pkt, key, trailer=None):
80 Sign an IPsec (ESP or AH) packet with this algo.
82 @@ -560,16 +557,20 @@ class AuthAlgo(object):
85 mac.update(raw(pkt[ESP]))
88 pkt[ESP].data += mac.finalize()[:self.icv_size]
90 elif pkt.haslayer(AH):
91 clone = zero_mutable_fields(pkt.copy(), sending=True)
92 mac.update(raw(clone))
95 pkt[AH].icv = mac.finalize()[:self.icv_size]
99 - def verify(self, pkt, key):
100 + def verify(self, pkt, key, trailer):
102 Check that the integrity check value (icv) of a packet is valid.
104 @@ -600,6 +601,8 @@ class AuthAlgo(object):
105 clone = zero_mutable_fields(pkt.copy(), sending=False)
107 mac.update(raw(clone))
109 + mac.update(trailer) # bytearray(4)) #raw(trailer))
110 computed_icv = mac.finalize()[:self.icv_size]
112 # XXX: Cannot use mac.verify because the ICV can be truncated
113 @@ -788,7 +791,7 @@ class SecurityAssociation(object):
114 This class is responsible of "encryption" and "decryption" of IPsec packets. # noqa: E501
117 - SUPPORTED_PROTOS = (IP, IPv6)
118 + SUPPORTED_PROTOS = (IP, IPv6, MPLS)
120 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
121 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, esn_en=False, esn=0): # noqa: E501
122 @@ -862,6 +865,23 @@ class SecurityAssociation(object):
123 raise TypeError('nat_t_header must be %s' % UDP.name)
124 self.nat_t_header = nat_t_header
126 + def build_aead(self, esp):
128 + return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
130 + return (struct.pack('!LL', esp.spi, esp.seq))
132 + def build_seq_num(self, num):
133 + # only lower order bits are transmitted
134 + # higher order bits are used in the ICV
135 + lower = num & 0xffffffff
139 + return lower, struct.pack("!I", upper)
143 def check_spi(self, pkt):
144 if pkt.spi != self.spi:
145 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
146 @@ -875,7 +895,8 @@ class SecurityAssociation(object):
147 if len(iv) != self.crypt_algo.iv_size:
148 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) # noqa: E501
150 - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
151 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
152 + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
154 if self.tunnel_header:
155 tunnel = self.tunnel_header.copy()
156 @@ -899,7 +920,7 @@ class SecurityAssociation(object):
157 esn_en=esn_en or self.esn_en,
160 - self.auth_algo.sign(esp, self.auth_key)
161 + self.auth_algo.sign(esp, self.auth_key, high_seq_num)
163 if self.nat_t_header:
164 nat_t_header = self.nat_t_header.copy()
165 @@ -926,7 +947,8 @@ class SecurityAssociation(object):
167 def _encrypt_ah(self, pkt, seq_num=None):
169 - ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
170 + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
171 + ah = AH(spi=self.spi, seq=low_seq_num,
172 icv=b"\x00" * self.auth_algo.icv_size)
174 if self.tunnel_header:
175 @@ -966,7 +988,8 @@ class SecurityAssociation(object):
177 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
179 - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key) # noqa: E501
180 + signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
181 + self.auth_key, high_seq_num) # noqa: E501
183 # sequence number must always change, unless specified by the user
185 @@ -1003,11 +1026,12 @@ class SecurityAssociation(object):
187 def _decrypt_esp(self, pkt, verify=True, esn_en=None, esn=None):
189 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
194 - self.auth_algo.verify(encrypted, self.auth_key)
195 + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
197 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
198 self.crypt_algo.icv_size or
199 @@ -1048,9 +1072,10 @@ class SecurityAssociation(object):
201 def _decrypt_ah(self, pkt, verify=True):
203 + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
206 - self.auth_algo.verify(pkt, self.auth_key)
207 + self.auth_algo.verify(pkt, self.auth_key, high_seq_num)