| diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py |
| index 8251dc14..bbb71102 100644 |
| --- a/scapy/layers/ipsec.py |
| +++ b/scapy/layers/ipsec.py |
| @@ -60,7 +60,7 @@ import scapy.modules.six as six |
| from scapy.modules.six.moves import range |
| from scapy.layers.inet6 import IPv6, IPv6ExtHdrHopByHop, IPv6ExtHdrDestOpt, \ |
| IPv6ExtHdrRouting |
| - |
| +from scapy.contrib.mpls import MPLS |
| |
| ############################################################################### |
| class AH(Packet): |
| @@ -360,13 +360,16 @@ class CryptAlgo(object): |
| encryptor = cipher.encryptor() |
| |
| if self.is_aead: |
| - if esn_en: |
| - aad = struct.pack('!LLL', esp.spi, esn, esp.seq) |
| - else: |
| - aad = struct.pack('!LL', esp.spi, esp.seq) |
| + aad = sa.build_aead(esp) |
| + if self.name == 'AES-NULL-GMAC': |
| + aad = aad + esp.iv + data |
| + aes_null_gmac_data = data |
| + data = b'' |
| encryptor.authenticate_additional_data(aad) |
| data = encryptor.update(data) + encryptor.finalize() |
| data += encryptor.tag[:self.icv_size] |
| + if self.name == 'AES-NULL-GMAC': |
| + data = aes_null_gmac_data + data |
| else: |
| data = encryptor.update(data) + encryptor.finalize() |
| |
| @@ -402,16 +405,18 @@ class CryptAlgo(object): |
| |
| if self.is_aead: |
| # Tag value check is done during the finalize method |
| - if esn_en: |
| - decryptor.authenticate_additional_data( |
| - struct.pack('!LLL', esp.spi, esn, esp.seq)) |
| - else: |
| - decryptor.authenticate_additional_data( |
| - struct.pack('!LL', esp.spi, esp.seq)) |
| + aad = sa.build_aead(esp) |
| + if self.name == 'AES-NULL-GMAC': |
| + aad = aad + iv + data |
| + aes_null_gmac_data = data |
| + data = b'' |
| + decryptor.authenticate_additional_data(aad) |
| try: |
| data = decryptor.update(data) + decryptor.finalize() |
| except InvalidTag as err: |
| raise IPSecIntegrityError(err) |
| + if self.name == 'AES-NULL-GMAC': |
| + data = aes_null_gmac_data + data |
| |
| # extract padlen and nh |
| padlen = orb(data[-2]) |
| @@ -458,6 +463,13 @@ if algorithms: |
| iv_size=8, |
| icv_size=16, |
| format_mode_iv=_salt_format_mode_iv) |
| + CRYPT_ALGOS['AES-NULL-GMAC'] = CryptAlgo('AES-NULL-GMAC', |
| + cipher=algorithms.AES, |
| + mode=modes.GCM, |
| + salt_size=4, |
| + iv_size=8, |
| + icv_size=16, |
| + format_mode_iv=_salt_format_mode_iv) |
| if hasattr(modes, 'CCM'): |
| CRYPT_ALGOS['AES-CCM'] = CryptAlgo('AES-CCM', |
| cipher=algorithms.AES, |
| @@ -546,7 +558,7 @@ class AuthAlgo(object): |
| else: |
| return self.mac(key, self.digestmod(), default_backend()) |
| |
| - def sign(self, pkt, key, esn_en=False, esn=0): |
| + def sign(self, pkt, key, trailer=None, esn_en=False, esn=0): |
| """ |
| Sign an IPsec (ESP or AH) packet with this algo. |
| |
| @@ -565,20 +577,20 @@ class AuthAlgo(object): |
| |
| if pkt.haslayer(ESP): |
| mac.update(raw(pkt[ESP])) |
| + if trailer: |
| + mac.update(trailer) |
| pkt[ESP].data += mac.finalize()[:self.icv_size] |
| |
| elif pkt.haslayer(AH): |
| clone = zero_mutable_fields(pkt.copy(), sending=True) |
| - if esn_en: |
| - temp = raw(clone) + struct.pack('!L', esn) |
| - else: |
| - temp = raw(clone) |
| - mac.update(temp) |
| + mac.update(raw(clone)) |
| + if trailer: |
| + mac.update(trailer) |
| pkt[AH].icv = mac.finalize()[:self.icv_size] |
| |
| return pkt |
| |
| - def verify(self, pkt, key, esn_en=False, esn=0): |
| + def verify(self, pkt, key, trailer, esn_en=False, esn=0): |
| """ |
| Check that the integrity check value (icv) of a packet is valid. |
| |
| @@ -602,7 +614,6 @@ class AuthAlgo(object): |
| pkt_icv = pkt.data[len(pkt.data) - self.icv_size:] |
| clone = pkt.copy() |
| clone.data = clone.data[:len(clone.data) - self.icv_size] |
| - temp = raw(clone) |
| |
| elif pkt.haslayer(AH): |
| if len(pkt[AH].icv) != self.icv_size: |
| @@ -611,12 +622,10 @@ class AuthAlgo(object): |
| pkt[AH].icv = pkt[AH].icv[:self.icv_size] |
| pkt_icv = pkt[AH].icv |
| clone = zero_mutable_fields(pkt.copy(), sending=False) |
| - if esn_en: |
| - temp = raw(clone) + struct.pack('!L', esn) |
| - else: |
| - temp = raw(clone) |
| |
| - mac.update(temp) |
| + mac.update(raw(clone)) |
| + if trailer: |
| + mac.update(trailer) # bytearray(4)) #raw(trailer)) |
| computed_icv = mac.finalize()[:self.icv_size] |
| |
| # XXX: Cannot use mac.verify because the ICV can be truncated |
| @@ -805,7 +814,7 @@ class SecurityAssociation(object): |
| This class is responsible of "encryption" and "decryption" of IPsec packets. # noqa: E501 |
| """ |
| |
| - SUPPORTED_PROTOS = (IP, IPv6) |
| + SUPPORTED_PROTOS = (IP, IPv6, MPLS) |
| |
| def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None, |
| auth_algo=None, auth_key=None, tunnel_header=None, nat_t_header=None, esn_en=False, esn=0): # noqa: E501 |
| @@ -880,6 +889,23 @@ class SecurityAssociation(object): |
| raise TypeError('nat_t_header must be %s' % UDP.name) |
| self.nat_t_header = nat_t_header |
| |
| + def build_aead(self, esp): |
| + if self.esn_en: |
| + return (struct.pack('!LLL', esp.spi, self.seq_num >> 32, esp.seq)) |
| + else: |
| + return (struct.pack('!LL', esp.spi, esp.seq)) |
| + |
| + def build_seq_num(self, num): |
| + # only lower order bits are transmitted |
| + # higher order bits are used in the ICV |
| + lower = num & 0xffffffff |
| + upper = num >> 32 |
| + |
| + if self.esn_en: |
| + return lower, struct.pack("!I", upper) |
| + else: |
| + return lower, None |
| + |
| def check_spi(self, pkt): |
| if pkt.spi != self.spi: |
| raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' % |
| @@ -893,7 +919,8 @@ class SecurityAssociation(object): |
| if len(iv) != self.crypt_algo.iv_size: |
| raise TypeError('iv length must be %s' % self.crypt_algo.iv_size) # noqa: E501 |
| |
| - esp = _ESPPlain(spi=self.spi, seq=seq_num or self.seq_num, iv=iv) |
| + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num) |
| + esp = _ESPPlain(spi=self.spi, seq=low_seq_num, iv=iv) |
| |
| if self.tunnel_header: |
| tunnel = self.tunnel_header.copy() |
| @@ -917,7 +944,7 @@ class SecurityAssociation(object): |
| esn_en=esn_en or self.esn_en, |
| esn=esn or self.esn) |
| |
| - self.auth_algo.sign(esp, self.auth_key) |
| + self.auth_algo.sign(esp, self.auth_key, high_seq_num) |
| |
| if self.nat_t_header: |
| nat_t_header = self.nat_t_header.copy() |
| @@ -944,7 +971,8 @@ class SecurityAssociation(object): |
| |
| def _encrypt_ah(self, pkt, seq_num=None, esn_en=False, esn=0): |
| |
| - ah = AH(spi=self.spi, seq=seq_num or self.seq_num, |
| + low_seq_num, high_seq_num = self.build_seq_num(seq_num or self.seq_num) |
| + ah = AH(spi=self.spi, seq=low_seq_num, |
| icv=b"\x00" * self.auth_algo.icv_size) |
| |
| if self.tunnel_header: |
| @@ -985,7 +1013,7 @@ class SecurityAssociation(object): |
| ip_header.plen = len(ip_header.payload) + len(ah) + len(payload) |
| |
| signed_pkt = self.auth_algo.sign(ip_header / ah / payload, |
| - self.auth_key, |
| + self.auth_key, high_seq_num, |
| esn_en=esn_en or self.esn_en, |
| esn=esn or self.esn) |
| |
| @@ -1025,11 +1053,12 @@ class SecurityAssociation(object): |
| |
| def _decrypt_esp(self, pkt, verify=True, esn_en=None, esn=None): |
| |
| + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num) |
| encrypted = pkt[ESP] |
| |
| if verify: |
| self.check_spi(pkt) |
| - self.auth_algo.verify(encrypted, self.auth_key) |
| + self.auth_algo.verify(encrypted, self.auth_key, high_seq_num) |
| |
| esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key, |
| self.crypt_algo.icv_size or |
| @@ -1070,9 +1099,10 @@ class SecurityAssociation(object): |
| |
| def _decrypt_ah(self, pkt, verify=True, esn_en=None, esn=None): |
| |
| + low_seq_num, high_seq_num = self.build_seq_num(self.seq_num) |
| if verify: |
| self.check_spi(pkt) |
| - self.auth_algo.verify(pkt, self.auth_key, |
| + self.auth_algo.verify(pkt, self.auth_key, high_seq_num, |
| esn_en=esn_en or self.esn_en, |
| esn=esn or self.esn) |
| |