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 |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 2 | index 69e7ae3b..d9c1705b 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: |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 27 | @@ -422,6 +418,7 @@ if algorithms: |
| 28 | CRYPT_ALGOS['AES-CTR'] = CryptAlgo('AES-CTR', |
| 29 | cipher=algorithms.AES, |
| 30 | mode=modes.CTR, |
| 31 | + block_size=1, |
| 32 | iv_size=8, |
| 33 | salt_size=4, |
| 34 | format_mode_iv=_aes_ctr_format_mode_iv) |
| 35 | @@ -429,6 +426,7 @@ if algorithms: |
| 36 | CRYPT_ALGOS['AES-GCM'] = CryptAlgo('AES-GCM', |
| 37 | cipher=algorithms.AES, |
| 38 | mode=modes.GCM, |
| 39 | + block_size=1, |
| 40 | salt_size=4, |
| 41 | iv_size=8, |
| 42 | icv_size=16, |
| 43 | @@ -437,6 +435,7 @@ if algorithms: |
| 44 | CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM', |
| 45 | cipher=algorithms.AES, |
| 46 | mode=modes.CCM, |
| 47 | + block_size=1, |
| 48 | iv_size=8, |
| 49 | salt_size=3, |
| 50 | icv_size=16, |
| 51 | @@ -518,12 +517,16 @@ class AuthAlgo(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 52 | else: |
| 53 | return self.mac(key, self.digestmod(), default_backend()) |
| 54 | |
| 55 | - def sign(self, pkt, key): |
| 56 | + def sign(self, pkt, key, trailer=None): |
| 57 | """ |
| 58 | Sign an IPsec (ESP or AH) packet with this algo. |
| 59 | |
| 60 | @param pkt: a packet that contains a valid encrypted ESP or AH layer |
| 61 | @param key: the authentication key, a byte string |
| 62 | + @param trailer: additional data appended to the packet for ICV |
| 63 | + calculation, but not trnasmitted with the packet. |
| 64 | + For example, the high order bits of the exteneded |
| 65 | + sequence number. |
| 66 | |
| 67 | @return: the signed packet |
| 68 | """ |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 69 | @@ -534,16 +537,20 @@ class AuthAlgo(object): |
Neale Ranns | 49e7ef6 | 2019-04-10 17:24:29 +0000 | [diff] [blame] | 70 | |
| 71 | if pkt.haslayer(ESP): |
| 72 | mac.update(raw(pkt[ESP])) |
| 73 | + if trailer: |
| 74 | + mac.update(trailer) |
| 75 | pkt[ESP].data += mac.finalize()[:self.icv_size] |
| 76 | |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 77 | elif pkt.haslayer(AH): |
| 78 | clone = zero_mutable_fields(pkt.copy(), sending=True) |
| 79 | mac.update(raw(clone)) |
| 80 | + if trailer: |
| 81 | + mac.update(trailer) |
| 82 | pkt[AH].icv = mac.finalize()[:self.icv_size] |
| 83 | |
| 84 | return pkt |
| 85 | |
| 86 | - def verify(self, pkt, key): |
| 87 | + def verify(self, pkt, key, trailer): |
| 88 | """ |
| 89 | Check that the integrity check value (icv) of a packet is valid. |
| 90 | |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 91 | @@ -574,6 +581,8 @@ class AuthAlgo(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 92 | clone = zero_mutable_fields(pkt.copy(), sending=False) |
| 93 | |
| 94 | mac.update(raw(clone)) |
| 95 | + if trailer: |
| 96 | + mac.update(trailer) # bytearray(4)) #raw(trailer)) |
| 97 | computed_icv = mac.finalize()[:self.icv_size] |
| 98 | |
| 99 | # XXX: Cannot use mac.verify because the ICV can be truncated |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 100 | @@ -757,7 +766,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 101 | SUPPORTED_PROTOS = (IP, IPv6) |
| 102 | |
| 103 | def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None, |
| 104 | - auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None): |
| 105 | + auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, |
| 106 | + use_esn=False): |
| 107 | """ |
| 108 | @param proto: the IPsec proto to use (ESP or AH) |
| 109 | @param spi: the Security Parameters Index of this SA |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 110 | @@ -771,6 +781,7 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 111 | to encapsulate the encrypted packets. |
| 112 | @param nat_t_header: an instance of a UDP header that will be used |
| 113 | for NAT-Traversal. |
| 114 | + @param use_esn: Use Extended Sequence Numbers |
| 115 | """ |
| 116 | |
| 117 | if proto not in (ESP, AH, ESP.name, AH.name): |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 118 | @@ -782,6 +793,7 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 119 | |
| 120 | self.spi = spi |
| 121 | self.seq_num = seq_num |
| 122 | + self.use_esn = use_esn |
| 123 | |
| 124 | if crypt_algo: |
| 125 | if crypt_algo not in CRYPT_ALGOS: |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 126 | @@ -827,6 +839,23 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 127 | raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' % |
| 128 | (pkt.spi, self.spi)) |
| 129 | |
Neale Ranns | 6afaae1 | 2019-07-17 15:07:14 +0000 | [diff] [blame] | 130 | + def build_aead(self, esp): |
| 131 | + if self.use_esn: |
| 132 | + return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq)) |
| 133 | + else: |
| 134 | + return (struct.pack('!LL', esp.spi, esp.seq)) |
| 135 | + |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 136 | + def build_seq_num(self, num): |
| 137 | + # only lower order bits are transmitted |
| 138 | + # higher order bits are used in the ICV |
| 139 | + lower = num & 0xffffffff |
| 140 | + upper = num >> 32 |
| 141 | + |
| 142 | + if self.use_esn: |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 143 | + return lower, struct.pack("!I", upper) |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 144 | + else: |
| 145 | + return lower, None |
| 146 | + |
| 147 | def _encrypt_esp(self, pkt, seq_num=None, iv=None): |
| 148 | |
| 149 | if iv is None: |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 150 | @@ -835,7 +864,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 151 | if len(iv) != self.crypt_algo.iv_size: |
| 152 | raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) |
| 153 | |
| 154 | - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv) |
| 155 | + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num) |
| 156 | + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv) |
| 157 | |
| 158 | if self.tunnel_header: |
| 159 | tunnel = self.tunnel_header.copy() |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 160 | @@ -857,7 +887,7 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 161 | esp = self.crypt_algo.pad(esp) |
| 162 | esp = self.crypt_algo.encrypt(self, esp, self.crypt_key) |
| 163 | |
| 164 | - self.auth_algo.sign(esp, self.auth_key) |
| 165 | + self.auth_algo.sign(esp, self.auth_key, high_seq_num) |
| 166 | |
| 167 | if self.nat_t_header: |
| 168 | nat_t_header = self.nat_t_header.copy() |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 169 | @@ -884,7 +914,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 170 | |
| 171 | def _encrypt_ah(self, pkt, seq_num=None): |
| 172 | |
| 173 | - ah = AH(spi=self.spi, seq=seq_num or self.seq_num, |
| 174 | + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num) |
| 175 | + ah = AH(spi=self.spi, seq=low_seq_num, |
| 176 | icv = b"\x00" * self.auth_algo.icv_size) |
| 177 | |
| 178 | if self.tunnel_header: |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 179 | @@ -924,7 +955,8 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 180 | else: |
| 181 | ip_header.plen = len(ip_header.payload) + len(ah) + len(payload) |
| 182 | |
| 183 | - signed_pkt = self.auth_algo.sign(ip_header / ah / payload, self.auth_key) |
| 184 | + signed_pkt = self.auth_algo.sign(ip_header / ah / payload, |
| 185 | + self.auth_key, high_seq_num) |
| 186 | |
| 187 | # sequence number must always change, unless specified by the user |
| 188 | if seq_num is None: |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 189 | @@ -955,11 +987,12 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 190 | |
| 191 | def _decrypt_esp(self, pkt, verify=True): |
| 192 | |
| 193 | + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num) |
| 194 | encrypted = pkt[ESP] |
| 195 | |
| 196 | if verify: |
| 197 | self.check_spi(pkt) |
| 198 | - self.auth_algo.verify(encrypted, self.auth_key) |
| 199 | + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num) |
| 200 | |
| 201 | esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key, |
| 202 | self.crypt_algo.icv_size or |
Christian Hopps | fb7e7ed | 2019-11-03 07:02:15 -0500 | [diff] [blame^] | 203 | @@ -998,9 +1031,11 @@ class SecurityAssociation(object): |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 204 | |
| 205 | def _decrypt_ah(self, pkt, verify=True): |
| 206 | |
| 207 | + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num) |
| 208 | + |
| 209 | if verify: |
| 210 | self.check_spi(pkt) |
| 211 | - self.auth_algo.verify(pkt, self.auth_key) |
| 212 | + self.auth_algo.verify(pkt, self.auth_key, high_seq_num) |
| 213 | |
| 214 | ah = pkt[AH] |
| 215 | payload = ah.payload |