Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 1 | diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 2 | index 69e7ae3b..3a1724b2 100644 |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 3 | --- a/scapy/layers/ipsec.py |
| 4 | +++ b/scapy/layers/ipsec.py |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 5 | @@ -344,8 +344,7 @@ class CryptAlgo(object): |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 6 | encryptor = cipher.encryptor() |
| 7 | |
| 8 | if self.is_aead: |
| 9 | - aad = struct.pack('!LL', esp.spi, esp.seq) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 10 | - encryptor.authenticate_additional_data(aad) |
| 11 | + encryptor.authenticate_additional_data(sa.build_aead(esp)) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 12 | data = encryptor.update(data) + encryptor.finalize() |
| 13 | data += encryptor.tag[:self.icv_size] |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 14 | else: |
| 15 | @@ -380,10 +379,7 @@ class CryptAlgo(object): |
| 16 | |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 17 | if self.is_aead: |
| 18 | # Tag value check is done during the finalize method |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 19 | - decryptor.authenticate_additional_data( |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 20 | - struct.pack('!LL', esp.spi, esp.seq) |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 21 | - ) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 22 | - |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 23 | + decryptor.authenticate_additional_data(sa.build_aead(esp)) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 24 | try: |
| 25 | data = decryptor.update(data) + decryptor.finalize() |
| 26 | except InvalidTag as err: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 27 | @@ -518,12 +514,16 @@ class AuthAlgo(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 28 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 45 | @@ -534,16 +534,20 @@ class AuthAlgo(object): |
Neale Ranns | 49e7ef6 | 2019-04-10 17:24:29 +0000 | [diff] [blame] | 46 | |
| 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 Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 53 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 67 | @@ -574,6 +578,8 @@ class AuthAlgo(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 68 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 76 | @@ -757,7 +763,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 77 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 86 | @@ -771,6 +778,7 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 87 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 94 | @@ -782,6 +790,7 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 95 | |
| 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 102 | @@ -827,6 +836,23 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 103 | raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' % |
| 104 | (pkt.spi, self.spi)) |
| 105 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 106 | + 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 Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 112 | + 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 Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 119 | + return lower, struct.pack("!I", upper) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 120 | + else: |
| 121 | + return lower, None |
| 122 | + |
| 123 | def _encrypt_esp(self, pkt, seq_num=None, iv=None): |
| 124 | |
| 125 | if iv is None: |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 126 | @@ -835,7 +861,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 127 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 136 | @@ -857,7 +884,7 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 137 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 145 | @@ -884,7 +911,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 146 | |
| 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 155 | @@ -924,7 +952,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 156 | 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 165 | @@ -955,11 +984,12 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 166 | |
| 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 Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 179 | @@ -998,9 +1028,11 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 180 | |
| 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 |