blob: 196b78fe592d39a1ba066e9a31c4a64ad745b6c3 [file] [log] [blame]
snaramre5d4b8912019-12-13 23:39:35 +00001diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +01002index ae057ee1..55d0dd53 100644
snaramre5d4b8912019-12-13 23:39:35 +00003--- a/scapy/layers/ipsec.py
4+++ b/scapy/layers/ipsec.py
Neale Ranns4a58e492020-12-21 13:19:10 +00005@@ -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, \
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010013@@ -359,11 +360,8 @@ class CryptAlgo(object):
snaramre5d4b8912019-12-13 23:39:35 +000014 encryptor = cipher.encryptor()
15
16 if self.is_aead:
17- if esn_en:
18- aad = struct.pack('!LLL', esp.spi, esn, esp.seq)
19- else:
20- aad = struct.pack('!LL', esp.spi, esp.seq)
21- encryptor.authenticate_additional_data(aad)
22+ encryptor.authenticate_additional_data(sa.build_aead(esp))
23+
24 data = encryptor.update(data) + encryptor.finalize()
25 data += encryptor.tag[:self.icv_size]
26 else:
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010027@@ -400,12 +398,7 @@ class CryptAlgo(object):
snaramre5d4b8912019-12-13 23:39:35 +000028
29 if self.is_aead:
30 # Tag value check is done during the finalize method
31- if esn_en:
32- decryptor.authenticate_additional_data(
33- struct.pack('!LLL', esp.spi, esn, esp.seq))
34- else:
35- decryptor.authenticate_additional_data(
36- struct.pack('!LL', esp.spi, esp.seq))
37+ decryptor.authenticate_additional_data(sa.build_aead(esp))
38 try:
39 data = decryptor.update(data) + decryptor.finalize()
40 except InvalidTag as err:
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010041@@ -445,6 +438,7 @@ if algorithms:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -050042 CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR',
43 cipher=algorithms.AES,
44 mode=modes.CTR,
45+ block_size=1,
46 iv_size=8,
47 salt_size=4,
48 format_mode_iv=_aes_ctr_format_mode_iv)
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010049@@ -452,6 +446,7 @@ if algorithms:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -050050 CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM',
51 cipher=algorithms.AES,
52 mode=modes.GCM,
53+ block_size=1,
54 salt_size=4,
55 iv_size=8,
56 icv_size=16,
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010057@@ -460,6 +455,7 @@ if algorithms:
Christian Hoppsfb7e7ed2019-11-03 07:02:15 -050058 CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM',
59 cipher=algorithms.AES,
60 mode=modes.CCM,
61+ block_size=1,
62 iv_size=8,
63 salt_size=3,
64 icv_size=16,
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010065@@ -544,7 +540,7 @@ class AuthAlgo(object):
snaramre5d4b8912019-12-13 23:39:35 +000066 else:
67 return self.mac(key, self.digestmod(), default_backend())
68
69- def sign(self, pkt, key):
70+ def sign(self, pkt, key, trailer=None):
71 """
72 Sign an IPsec (ESP or AH) packet with this algo.
73
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010074@@ -560,16 +556,20 @@ class AuthAlgo(object):
snaramre5d4b8912019-12-13 23:39:35 +000075
76 if pkt.haslayer(ESP):
77 mac.update(raw(pkt[ESP]))
78+ if trailer:
79+ mac.update(trailer)
80 pkt[ESP].data += mac.finalize()[:self.icv_size]
81
82 elif pkt.haslayer(AH):
83 clone = zero_mutable_fields(pkt.copy(), sending=True)
84 mac.update(raw(clone))
85+ if trailer:
86+ mac.update(trailer)
87 pkt[AH].icv = mac.finalize()[:self.icv_size]
88
89 return pkt
90
91- def verify(self, pkt, key):
92+ def verify(self, pkt, key, trailer):
93 """
94 Check that the integrity check value (icv) of a packet is valid.
95
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +010096@@ -600,6 +600,8 @@ class AuthAlgo(object):
snaramre5d4b8912019-12-13 23:39:35 +000097 clone = zero_mutable_fields(pkt.copy(), sending=False)
98
99 mac.update(raw(clone))
100+ if trailer:
101+ mac.update(trailer) # bytearray(4)) #raw(trailer))
102 computed_icv = mac.finalize()[:self.icv_size]
103
104 # XXX: Cannot use mac.verify because the ICV can be truncated
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100105@@ -788,7 +790,7 @@ class SecurityAssociation(object):
Neale Ranns4a58e492020-12-21 13:19:10 +0000106 This class is responsible of "encryption" and "decryption" of IPsec packets. # noqa: E501
107 """
108
109- SUPPORTED_PROTOS = (IP, IPv6)
110+ SUPPORTED_PROTOS = (IP, IPv6, MPLS)
111
112 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
113 auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, esn_en=False, esn=0): # noqa: E501
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100114@@ -862,6 +864,23 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000115 raise TypeError('nat_t_header must be %s' % UDP.name)
116 self.nat_t_header = nat_t_header
117
118+ def build_aead(self, esp):
119+ if self.esn_en:
120+ return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
121+ else:
122+ return (struct.pack('!LL', esp.spi, esp.seq))
123+
124+ def build_seq_num(self, num):
125+ # only lower order bits are transmitted
126+ # higher order bits are used in the ICV
127+ lower = num & 0xffffffff
128+ upper = num >> 32
129+
130+ if self.esn_en:
131+ return lower, struct.pack("!I", upper)
132+ else:
133+ return lower, None
134+
135 def check_spi(self, pkt):
136 if pkt.spi != self.spi:
137 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100138@@ -875,7 +894,8 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000139 if len(iv) != self.crypt_algo.iv_size:
140 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) # noqa: E501
141
142- esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
143+ low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
144+ esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
145
146 if self.tunnel_header:
147 tunnel = self.tunnel_header.copy()
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100148@@ -899,7 +919,7 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000149 esn_en=esn_en or self.esn_en,
150 esn=esn or self.esn)
151
152- self.auth_algo.sign(esp, self.auth_key)
153+ self.auth_algo.sign(esp, self.auth_key, high_seq_num)
154
155 if self.nat_t_header:
156 nat_t_header = self.nat_t_header.copy()
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100157@@ -926,7 +946,8 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000158
159 def _encrypt_ah(self, pkt, seq_num=None):
160
161- ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
162+ low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
163+ ah = AH(spi=self.spi, seq=low_seq_num,
164 icv=b"\x00" * self.auth_algo.icv_size)
165
166 if self.tunnel_header:
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100167@@ -966,7 +987,8 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000168 else:
169 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
170
171- signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key) # noqa: E501
172+ signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
173+ self.auth_key, high_seq_num) # noqa: E501
174
175 # sequence number must always change, unless specified by the user
176 if seq_num is None:
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100177@@ -1003,11 +1025,12 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000178
179 def _decrypt_esp(self, pkt, verify=True, esn_en=None, esn=None):
180
181+ low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
182 encrypted = pkt[ESP]
183
184 if verify:
185 self.check_spi(pkt)
186- self.auth_algo.verify(encrypted, self.auth_key)
187+ self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
188
189 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
190 self.crypt_algo.icv_size or
Arthur de Kerhorce04e3b2022-01-04 15:53:43 +0100191@@ -1048,9 +1071,10 @@ class SecurityAssociation(object):
snaramre5d4b8912019-12-13 23:39:35 +0000192
193 def _decrypt_ah(self, pkt, verify=True):
194
195+ low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
196 if verify:
197 self.check_spi(pkt)
198- self.auth_algo.verify(pkt, self.auth_key)
199+ self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
200
201 ah = pkt[AH]
202 payload = ah.payload