blob: 083b0b94961559994707fc7a39afb79ea22be187 [file] [log] [blame]
Neale Ranns3833ffd2019-03-21 14:34:09 +00001diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
Neale Ranns6afaae12019-07-17 15:07:14 +00002index 69e7ae3b..3a1724b2 100644
Neale Ranns3833ffd2019-03-21 14:34:09 +00003--- a/scapy/layers/ipsec.py
4+++ b/scapy/layers/ipsec.py
Neale Ranns6afaae12019-07-17 15:07:14 +00005@@ -344,8 +344,7 @@ class CryptAlgo(object):
Neale Ranns47feb112019-04-11 15:14:07 +00006 encryptor = cipher.encryptor()
7
8 if self.is_aead:
9- aad = struct.pack('!LL', esp.spi, esp.seq)
Neale Ranns6afaae12019-07-17 15:07:14 +000010- encryptor.authenticate_additional_data(aad)
11+ encryptor.authenticate_additional_data(sa.build_aead(esp))
Neale Ranns47feb112019-04-11 15:14:07 +000012 data = encryptor.update(data) + encryptor.finalize()
13 data += encryptor.tag[:self.icv_size]
Neale Ranns6afaae12019-07-17 15:07:14 +000014 else:
15@@ -380,10 +379,7 @@ class CryptAlgo(object):
16
Neale Ranns47feb112019-04-11 15:14:07 +000017 if self.is_aead:
18 # Tag value check is done during the finalize method
Neale Ranns6afaae12019-07-17 15:07:14 +000019- decryptor.authenticate_additional_data(
Neale Ranns47feb112019-04-11 15:14:07 +000020- struct.pack('!LL', esp.spi, esp.seq)
Neale Ranns6afaae12019-07-17 15:07:14 +000021- )
Neale Ranns47feb112019-04-11 15:14:07 +000022-
Neale Ranns6afaae12019-07-17 15:07:14 +000023+ decryptor.authenticate_additional_data(sa.build_aead(esp))
Neale Ranns47feb112019-04-11 15:14:07 +000024 try:
25 data = decryptor.update(data) + decryptor.finalize()
26 except InvalidTag as err:
Neale Ranns6afaae12019-07-17 15:07:14 +000027@@ -518,12 +514,16 @@ class AuthAlgo(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000028 else:
29 return self.mac(key, self.digestmod(), default_backend())
30
31- def sign(self, pkt, key):
32+ def sign(self, pkt, key, trailer=None):
33 """
34 Sign an IPsec (ESP or AH) packet with this algo.
35
36 @param pkt: a packet that contains a valid encrypted ESP or AH layer
37 @param key: the authentication key, a byte string
38+ @param trailer: additional data appended to the packet for ICV
39+ calculation, but not trnasmitted with the packet.
40+ For example, the high order bits of the exteneded
41+ sequence number.
42
43 @return: the signed packet
44 """
Neale Ranns6afaae12019-07-17 15:07:14 +000045@@ -534,16 +534,20 @@ class AuthAlgo(object):
Neale Ranns49e7ef62019-04-10 17:24:29 +000046
47 if pkt.haslayer(ESP):
48 mac.update(raw(pkt[ESP]))
49+ if trailer:
50+ mac.update(trailer)
51 pkt[ESP].data += mac.finalize()[:self.icv_size]
52
Neale Ranns3833ffd2019-03-21 14:34:09 +000053 elif pkt.haslayer(AH):
54 clone = zero_mutable_fields(pkt.copy(), sending=True)
55 mac.update(raw(clone))
56+ if trailer:
57+ mac.update(trailer)
58 pkt[AH].icv = mac.finalize()[:self.icv_size]
59
60 return pkt
61
62- def verify(self, pkt, key):
63+ def verify(self, pkt, key, trailer):
64 """
65 Check that the integrity check value (icv) of a packet is valid.
66
Neale Ranns6afaae12019-07-17 15:07:14 +000067@@ -574,6 +578,8 @@ class AuthAlgo(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000068 clone = zero_mutable_fields(pkt.copy(), sending=False)
69
70 mac.update(raw(clone))
71+ if trailer:
72+ mac.update(trailer) # bytearray(4)) #raw(trailer))
73 computed_icv = mac.finalize()[:self.icv_size]
74
75 # XXX: Cannot use mac.verify because the ICV can be truncated
Neale Ranns6afaae12019-07-17 15:07:14 +000076@@ -757,7 +763,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000077 SUPPORTED_PROTOS = (IP, IPv6)
78
79 def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
80- auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None):
81+ auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None,
82+ use_esn=False):
83 """
84 @param proto: the IPsec proto to use (ESP or AH)
85 @param spi: the Security Parameters Index of this SA
Neale Ranns6afaae12019-07-17 15:07:14 +000086@@ -771,6 +778,7 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000087 to encapsulate the encrypted packets.
88 @param nat_t_header: an instance of a UDP header that will be used
89 for NAT-Traversal.
90+ @param use_esn: Use Extended Sequence Numbers
91 """
92
93 if proto not in (ESP, AH, ESP.name, AH.name):
Neale Ranns6afaae12019-07-17 15:07:14 +000094@@ -782,6 +790,7 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +000095
96 self.spi = spi
97 self.seq_num = seq_num
98+ self.use_esn = use_esn
99
100 if crypt_algo:
101 if crypt_algo not in CRYPT_ALGOS:
Neale Ranns6afaae12019-07-17 15:07:14 +0000102@@ -827,6 +836,23 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000103 raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
104 (pkt.spi, self.spi))
105
Neale Ranns6afaae12019-07-17 15:07:14 +0000106+ def build_aead(self, esp):
107+ if self.use_esn:
108+ return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq))
109+ else:
110+ return (struct.pack('!LL', esp.spi, esp.seq))
111+
Neale Ranns3833ffd2019-03-21 14:34:09 +0000112+ def build_seq_num(self, num):
113+ # only lower order bits are transmitted
114+ # higher order bits are used in the ICV
115+ lower = num & 0xffffffff
116+ upper = num >> 32
117+
118+ if self.use_esn:
Neale Ranns47feb112019-04-11 15:14:07 +0000119+ return lower, struct.pack("!I", upper)
Neale Ranns3833ffd2019-03-21 14:34:09 +0000120+ else:
121+ return lower, None
122+
123 def _encrypt_esp(self, pkt, seq_num=None, iv=None):
124
125 if iv is None:
Neale Ranns6afaae12019-07-17 15:07:14 +0000126@@ -835,7 +861,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000127 if len(iv) != self.crypt_algo.iv_size:
128 raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
129
130- esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv)
131+ low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
132+ esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv)
133
134 if self.tunnel_header:
135 tunnel = self.tunnel_header.copy()
Neale Ranns6afaae12019-07-17 15:07:14 +0000136@@ -857,7 +884,7 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000137 esp = self.crypt_algo.pad(esp)
138 esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
139
140- self.auth_algo.sign(esp, self.auth_key)
141+ self.auth_algo.sign(esp, self.auth_key, high_seq_num)
142
143 if self.nat_t_header:
144 nat_t_header = self.nat_t_header.copy()
Neale Ranns6afaae12019-07-17 15:07:14 +0000145@@ -884,7 +911,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000146
147 def _encrypt_ah(self, pkt, seq_num=None):
148
149- ah = AH(spi=self.spi, seq=seq_num or self.seq_num,
150+ low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num)
151+ ah = AH(spi=self.spi, seq=low_seq_num,
152 icv = b"\x00" * self.auth_algo.icv_size)
153
154 if self.tunnel_header:
Neale Ranns6afaae12019-07-17 15:07:14 +0000155@@ -924,7 +952,8 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000156 else:
157 ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
158
159- signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key)
160+ signed_pkt = self.auth_algo.sign(ip_header / ah / payload,
161+ self.auth_key, high_seq_num)
162
163 # sequence number must always change, unless specified by the user
164 if seq_num is None:
Neale Ranns6afaae12019-07-17 15:07:14 +0000165@@ -955,11 +984,12 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000166
167 def _decrypt_esp(self, pkt, verify=True):
168
169+ low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
170 encrypted = pkt[ESP]
171
172 if verify:
173 self.check_spi(pkt)
174- self.auth_algo.verify(encrypted, self.auth_key)
175+ self.auth_algo.verify(encrypted, self.auth_key, high_seq_num)
176
177 esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
178 self.crypt_algo.icv_size or
Neale Ranns6afaae12019-07-17 15:07:14 +0000179@@ -998,9 +1028,11 @@ class SecurityAssociation(object):
Neale Ranns3833ffd2019-03-21 14:34:09 +0000180
181 def _decrypt_ah(self, pkt, verify=True):
182
183+ low_seq_num, high_seq_num = self.build_seq_num(self.seq_num)
184+
185 if verify:
186 self.check_spi(pkt)
187- self.auth_algo.verify(pkt, self.auth_key)
188+ self.auth_algo.verify(pkt, self.auth_key, high_seq_num)
189
190 ah = pkt[AH]
191 payload = ah.payload