aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorNeale Ranns <nranns@cisco.com>2019-07-17 15:07:14 +0000
committerDamjan Marion <dmarion@me.com>2019-07-24 11:01:47 +0000
commit6afaae156a9ab9de79474367d8873407f3b12a71 (patch)
tree016e506a1636bf72944217c7e324091d61d21b69 /test
parentae3eaacaf1df7b83d6ef6b30290e1390d38197df (diff)
ipsec: GCM, Anti-replay and ESN fixess
Type: fix Several Fixes: 1 - Anti-replay did not work with GCM becuase it overwrote the sequence number in the ESP header. To fix i added the seq num to the per-packet data so it is preserved 2 - The high sequence number was not byte swapped during ESP encrypt. 3 - openssl engine was the only one to return FAIL_DECRYPT for bad GCM the others return BAD_HMAC. removed the former 4 - improved tracing to show the low and high seq numbers 5 - documented the anti-replay window checks 6 - fixed scapy patch for ESN support for GCM 7 - tests for anti-reply (w/ and w/o ESN) for each crypto algo Change-Id: Id65d96b6d1d4dd821b2ab557e87468fff6d70e5b Signed-off-by: Neale Ranns <nranns@cisco.com>
Diffstat (limited to 'test')
-rw-r--r--test/patches/scapy-2.4/ipsec.patch52
-rw-r--r--test/template_ipsec.py228
-rw-r--r--test/test_ipsec_esp.py8
3 files changed, 210 insertions, 78 deletions
diff --git a/test/patches/scapy-2.4/ipsec.patch b/test/patches/scapy-2.4/ipsec.patch
index 731fe3ccc25..083b0b94961 100644
--- a/test/patches/scapy-2.4/ipsec.patch
+++ b/test/patches/scapy-2.4/ipsec.patch
@@ -1,30 +1,30 @@
diff --git a/scapy/layers/ipsec.py b/scapy/layers/ipsec.py
-index 69e7ae3b..0c69ba1a 100644
+index 69e7ae3b..3a1724b2 100644
--- a/scapy/layers/ipsec.py
+++ b/scapy/layers/ipsec.py
-@@ -344,7 +344,8 @@ class CryptAlgo(object):
+@@ -344,8 +344,7 @@ class CryptAlgo(object):
encryptor = cipher.encryptor()
if self.is_aead:
- aad = struct.pack('!LL', esp.spi, esp.seq)
-+ aad = struct.pack('!LQ' if sa.use_esn else '!LL',
-+ esp.spi, esp.seq)
- encryptor.authenticate_additional_data(aad)
+- encryptor.authenticate_additional_data(aad)
++ encryptor.authenticate_additional_data(sa.build_aead(esp))
data = encryptor.update(data) + encryptor.finalize()
data += encryptor.tag[:self.icv_size]
-@@ -381,9 +382,9 @@ class CryptAlgo(object):
+ else:
+@@ -380,10 +379,7 @@ class CryptAlgo(object):
+
if self.is_aead:
# Tag value check is done during the finalize method
- decryptor.authenticate_additional_data(
+- decryptor.authenticate_additional_data(
- struct.pack('!LL', esp.spi, esp.seq)
-+ struct.pack('!LQ' if sa.use_esn else '!LL',
-+ esp.spi, esp.seq)
- )
+- )
-
++ decryptor.authenticate_additional_data(sa.build_aead(esp))
try:
data = decryptor.update(data) + decryptor.finalize()
except InvalidTag as err:
-@@ -518,12 +519,16 @@ class AuthAlgo(object):
+@@ -518,12 +514,16 @@ class AuthAlgo(object):
else:
return self.mac(key, self.digestmod(), default_backend())
@@ -42,7 +42,7 @@ index 69e7ae3b..0c69ba1a 100644
@return: the signed packet
"""
-@@ -534,16 +539,20 @@ class AuthAlgo(object):
+@@ -534,16 +534,20 @@ class AuthAlgo(object):
if pkt.haslayer(ESP):
mac.update(raw(pkt[ESP]))
@@ -64,7 +64,7 @@ index 69e7ae3b..0c69ba1a 100644
"""
Check that the integrity check value (icv) of a packet is valid.
-@@ -574,6 +583,8 @@ class AuthAlgo(object):
+@@ -574,6 +578,8 @@ class AuthAlgo(object):
clone = zero_mutable_fields(pkt.copy(), sending=False)
mac.update(raw(clone))
@@ -73,7 +73,7 @@ index 69e7ae3b..0c69ba1a 100644
computed_icv = mac.finalize()[:self.icv_size]
# XXX: Cannot use mac.verify because the ICV can be truncated
-@@ -757,7 +768,8 @@ class SecurityAssociation(object):
+@@ -757,7 +763,8 @@ class SecurityAssociation(object):
SUPPORTED_PROTOS = (IP, IPv6)
def __init__(self, proto, spi, seq_num=1, crypt_algo=None, crypt_key=None,
@@ -83,7 +83,7 @@ index 69e7ae3b..0c69ba1a 100644
"""
@param proto: the IPsec proto to use (ESP or AH)
@param spi: the Security Parameters Index of this SA
-@@ -771,6 +783,7 @@ class SecurityAssociation(object):
+@@ -771,6 +778,7 @@ class SecurityAssociation(object):
to encapsulate the encrypted packets.
@param nat_t_header: an instance of a UDP header that will be used
for NAT-Traversal.
@@ -91,7 +91,7 @@ index 69e7ae3b..0c69ba1a 100644
"""
if proto not in (ESP, AH, ESP.name, AH.name):
-@@ -782,6 +795,7 @@ class SecurityAssociation(object):
+@@ -782,6 +790,7 @@ class SecurityAssociation(object):
self.spi = spi
self.seq_num = seq_num
@@ -99,10 +99,16 @@ index 69e7ae3b..0c69ba1a 100644
if crypt_algo:
if crypt_algo not in CRYPT_ALGOS:
-@@ -827,6 +841,17 @@ class SecurityAssociation(object):
+@@ -827,6 +836,23 @@ class SecurityAssociation(object):
raise TypeError('packet spi=0x%x does not match the SA spi=0x%x' %
(pkt.spi, self.spi))
++ def build_aead(self, esp):
++ if self.use_esn:
++ 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
@@ -117,7 +123,7 @@ index 69e7ae3b..0c69ba1a 100644
def _encrypt_esp(self, pkt, seq_num=None, iv=None):
if iv is None:
-@@ -835,7 +860,8 @@ class SecurityAssociation(object):
+@@ -835,7 +861,8 @@ class SecurityAssociation(object):
if len(iv) != self.crypt_algo.iv_size:
raise TypeError('iv length must be %s' % self.crypt_algo.iv_size)
@@ -127,7 +133,7 @@ index 69e7ae3b..0c69ba1a 100644
if self.tunnel_header:
tunnel = self.tunnel_header.copy()
-@@ -857,7 +883,7 @@ class SecurityAssociation(object):
+@@ -857,7 +884,7 @@ class SecurityAssociation(object):
esp = self.crypt_algo.pad(esp)
esp = self.crypt_algo.encrypt(self, esp, self.crypt_key)
@@ -136,7 +142,7 @@ index 69e7ae3b..0c69ba1a 100644
if self.nat_t_header:
nat_t_header = self.nat_t_header.copy()
-@@ -884,7 +910,8 @@ class SecurityAssociation(object):
+@@ -884,7 +911,8 @@ class SecurityAssociation(object):
def _encrypt_ah(self, pkt, seq_num=None):
@@ -146,7 +152,7 @@ index 69e7ae3b..0c69ba1a 100644
icv = b"\x00" * self.auth_algo.icv_size)
if self.tunnel_header:
-@@ -924,7 +951,8 @@ class SecurityAssociation(object):
+@@ -924,7 +952,8 @@ class SecurityAssociation(object):
else:
ip_header.plen = len(ip_header.payload) + len(ah) + len(payload)
@@ -156,7 +162,7 @@ index 69e7ae3b..0c69ba1a 100644
# sequence number must always change, unless specified by the user
if seq_num is None:
-@@ -955,11 +983,12 @@ class SecurityAssociation(object):
+@@ -955,11 +984,12 @@ class SecurityAssociation(object):
def _decrypt_esp(self, pkt, verify=True):
@@ -170,7 +176,7 @@ index 69e7ae3b..0c69ba1a 100644
esp = self.crypt_algo.decrypt(self, encrypted, self.crypt_key,
self.crypt_algo.icv_size or
-@@ -998,9 +1027,11 @@ class SecurityAssociation(object):
+@@ -998,9 +1028,11 @@ class SecurityAssociation(object):
def _decrypt_ah(self, pkt, verify=True):
diff --git a/test/template_ipsec.py b/test/template_ipsec.py
index e40fa561dbd..25cff7f2edf 100644
--- a/test/template_ipsec.py
+++ b/test/template_ipsec.py
@@ -84,14 +84,18 @@ class IPsecIPv6Params(object):
self.nat_header = None
+def mk_scapy_crpyt_key(p):
+ if p.crypt_algo == "AES-GCM":
+ return p.crypt_key + struct.pack("!I", p.salt)
+ else:
+ return p.crypt_key
+
+
def config_tun_params(p, encryption_type, tun_if):
ip_class_by_addr_type = {socket.AF_INET: IP, socket.AF_INET6: IPv6}
use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
IPSEC_API_SAD_FLAG_USE_ESN))
- if p.crypt_algo == "AES-GCM":
- crypt_key = p.crypt_key + struct.pack("!I", p.salt)
- else:
- crypt_key = p.crypt_key
+ crypt_key = mk_scapy_crpyt_key(p)
p.scapy_tun_sa = SecurityAssociation(
encryption_type, spi=p.vpp_tun_spi,
crypt_algo=p.crypt_algo,
@@ -117,10 +121,7 @@ def config_tun_params(p, encryption_type, tun_if):
def config_tra_params(p, encryption_type):
use_esn = bool(p.flags & (VppEnum.vl_api_ipsec_sad_flags_t.
IPSEC_API_SAD_FLAG_USE_ESN))
- if p.crypt_algo == "AES-GCM":
- crypt_key = p.crypt_key + struct.pack("!I", p.salt)
- else:
- crypt_key = p.crypt_key
+ crypt_key = mk_scapy_crpyt_key(p)
p.scapy_tra_sa = SecurityAssociation(
encryption_type,
spi=p.vpp_tra_spi,
@@ -269,32 +270,68 @@ class IpsecTcpTests(IpsecTcp):
class IpsecTra4(object):
""" verify methods for Transport v4 """
- def verify_tra_anti_replay(self, count=1):
+ def verify_tra_anti_replay(self):
p = self.params[socket.AF_INET]
use_esn = p.vpp_tra_sa.use_esn
- # fire in a packet with seq number 1
- pkt = (Ether(src=self.tra_if.remote_mac,
- dst=self.tra_if.local_mac) /
- p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
- dst=self.tra_if.local_ip4) /
- ICMP(),
- seq_num=1))
- recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
+ seq_cycle_node_name = ('/err/%s/sequence number cycled' %
+ self.tra4_encrypt_node_name)
+ replay_node_name = ('/err/%s/SA replayed packet' %
+ self.tra4_decrypt_node_name)
+ if ESP == self.encryption_type and p.crypt_algo == "AES-GCM":
+ hash_failed_node_name = ('/err/%s/ESP decryption failed' %
+ self.tra4_decrypt_node_name)
+ else:
+ hash_failed_node_name = ('/err/%s/Integrity check failed' %
+ self.tra4_decrypt_node_name)
+ replay_count = self.statistics.get_err_counter(replay_node_name)
+ hash_failed_count = self.statistics.get_err_counter(
+ hash_failed_node_name)
+ seq_cycle_count = self.statistics.get_err_counter(seq_cycle_node_name)
+
+ if ESP == self.encryption_type:
+ undersize_node_name = ('/err/%s/undersized packet' %
+ self.tra4_decrypt_node_name)
+ undersize_count = self.statistics.get_err_counter(
+ undersize_node_name)
+
+ #
+ # send packets with seq numbers 1->34
+ # this means the window size is still in Case B (see RFC4303
+ # Appendix A)
+ #
+ # for reasons i haven't investigated Scapy won't create a packet with
+ # seq_num=0
+ #
+ pkts = [(Ether(src=self.tra_if.remote_mac,
+ dst=self.tra_if.local_mac) /
+ p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
+ dst=self.tra_if.local_ip4) /
+ ICMP(),
+ seq_num=seq))
+ for seq in range(1, 34)]
+ recv_pkts = self.send_and_expect(self.tra_if, pkts, self.tra_if)
- # now move the window over to 235
+ # replayed packets are dropped
+ self.send_and_assert_no_replies(self.tra_if, pkts)
+ replay_count += len(pkts)
+ self.assert_error_counter_equal(replay_node_name, replay_count)
+
+ #
+ # now move the window over to 257 (more than one byte) and into Case A
+ #
pkt = (Ether(src=self.tra_if.remote_mac,
dst=self.tra_if.local_mac) /
p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
dst=self.tra_if.local_ip4) /
ICMP(),
- seq_num=235))
+ seq_num=257))
recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
# replayed packets are dropped
self.send_and_assert_no_replies(self.tra_if, pkt * 3)
- self.assert_error_counter_equal(
- '/err/%s/SA replayed packet' % self.tra4_decrypt_node_name, 3)
+ replay_count += 3
+ self.assert_error_counter_equal(replay_node_name, replay_count)
# the window size is 64 packets
# in window are still accepted
@@ -303,14 +340,14 @@ class IpsecTra4(object):
p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
dst=self.tra_if.local_ip4) /
ICMP(),
- seq_num=172))
+ seq_num=200))
recv_pkts = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
# a packet that does not decrypt does not move the window forward
bogus_sa = SecurityAssociation(self.encryption_type,
p.vpp_tra_spi,
crypt_algo=p.crypt_algo,
- crypt_key=p.crypt_key[::-1],
+ crypt_key=mk_scapy_crpyt_key(p)[::-1],
auth_algo=p.auth_algo,
auth_key=p.auth_key[::-1])
pkt = (Ether(src=self.tra_if.remote_mac,
@@ -321,8 +358,9 @@ class IpsecTra4(object):
seq_num=350))
self.send_and_assert_no_replies(self.tra_if, pkt * 17)
- self.assert_error_counter_equal(
- '/err/%s/Integrity check failed' % self.tra4_decrypt_node_name, 17)
+ hash_failed_count += 17
+ self.assert_error_counter_equal(hash_failed_node_name,
+ hash_failed_count)
# a malformed 'runt' packet
# created by a mis-constructed SA
@@ -337,8 +375,9 @@ class IpsecTra4(object):
seq_num=350))
self.send_and_assert_no_replies(self.tra_if, pkt * 17)
- self.assert_error_counter_equal(
- '/err/%s/undersized packet' % self.tra4_decrypt_node_name, 17)
+ undersize_count += 17
+ self.assert_error_counter_equal(undersize_node_name,
+ undersize_count)
# which we can determine since this packet is still in the window
pkt = (Ether(src=self.tra_if.remote_mac,
@@ -349,7 +388,11 @@ class IpsecTra4(object):
seq_num=234))
self.send_and_expect(self.tra_if, [pkt], self.tra_if)
+ #
# out of window are dropped
+ # this is Case B. So VPP will consider this to be a high seq num wrap
+ # and so the decrypt attempt will fail
+ #
pkt = (Ether(src=self.tra_if.remote_mac,
dst=self.tra_if.local_mac) /
p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
@@ -361,44 +404,59 @@ class IpsecTra4(object):
if use_esn:
# an out of window error with ESN looks like a high sequence
# wrap. but since it isn't then the verify will fail.
- self.assert_error_counter_equal(
- '/err/%s/Integrity check failed' %
- self.tra4_decrypt_node_name, 34)
+ hash_failed_count += 17
+ self.assert_error_counter_equal(hash_failed_node_name,
+ hash_failed_count)
else:
- self.assert_error_counter_equal(
- '/err/%s/SA replayed packet' %
- self.tra4_decrypt_node_name, 20)
+ replay_count += 17
+ self.assert_error_counter_equal(replay_node_name,
+ replay_count)
- # valid packet moves the window over to 236
+ # valid packet moves the window over to 258
pkt = (Ether(src=self.tra_if.remote_mac,
dst=self.tra_if.local_mac) /
p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
dst=self.tra_if.local_ip4) /
ICMP(),
- seq_num=236))
+ seq_num=258))
rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
- # move VPP's SA to just before the seq-number wrap
+ #
+ # move VPP's SA TX seq-num to just before the seq-number wrap.
+ # then fire in a packet that VPP should drop on TX because it
+ # causes the TX seq number to wrap; unless we're using extened sequence
+ # numbers.
+ #
self.vapi.cli("test ipsec sa %d seq 0xffffffff" % p.scapy_tra_sa_id)
+ self.logger.info(self.vapi.ppcli("show ipsec sa 0"))
+ self.logger.info(self.vapi.ppcli("show ipsec sa 1"))
- # then fire in a packet that VPP should drop because it causes the
- # seq number to wrap unless we're using extended.
- pkt = (Ether(src=self.tra_if.remote_mac,
- dst=self.tra_if.local_mac) /
- p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
- dst=self.tra_if.local_ip4) /
- ICMP(),
- seq_num=237))
+ pkts = [(Ether(src=self.tra_if.remote_mac,
+ dst=self.tra_if.local_mac) /
+ p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
+ dst=self.tra_if.local_ip4) /
+ ICMP(),
+ seq_num=seq))
+ for seq in range(259, 280)]
if use_esn:
- rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
- # in order to decrpyt the high order number needs to wrap
- p.vpp_tra_sa.seq_num = 0x100000000
- decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
+ rxs = self.send_and_expect(self.tra_if, pkts, self.tra_if)
- # send packets with high bits set
+ #
+ # in order for scapy to decrypt its SA's high order number needs
+ # to wrap
+ #
+ p.vpp_tra_sa.seq_num = 0x100000000
+ for rx in rxs:
+ decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
+
+ #
+ # wrap scapy's TX high sequence number. VPP is in case B, so it
+ # will consider this a high seq wrap also.
+ # The low seq num we set it to will place VPP's RX window in Case A
+ #
p.scapy_tra_sa.seq_num = 0x100000005
pkt = (Ether(src=self.tra_if.remote_mac,
dst=self.tra_if.local_mac) /
@@ -407,13 +465,72 @@ class IpsecTra4(object):
ICMP(),
seq_num=0x100000005))
rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
- # in order to decrpyt the high order number needs to wrap
decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
+
+ #
+ # A packet that has seq num between (2^32-64) and 5 is within
+ # the window
+ #
+ p.scapy_tra_sa.seq_num = 0xfffffffd
+ pkt = (Ether(src=self.tra_if.remote_mac,
+ dst=self.tra_if.local_mac) /
+ p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
+ dst=self.tra_if.local_ip4) /
+ ICMP(),
+ seq_num=0xfffffffd))
+ rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
+ decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
+
+ #
+ # While in case A we cannot wrap the high sequence number again
+ # becuase VPP will consider this packet to be one that moves the
+ # window forward
+ #
+ pkt = (Ether(src=self.tra_if.remote_mac,
+ dst=self.tra_if.local_mac) /
+ p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
+ dst=self.tra_if.local_ip4) /
+ ICMP(),
+ seq_num=0x200000999))
+ self.send_and_assert_no_replies(self.tra_if, [pkt], self.tra_if)
+
+ hash_failed_count += 1
+ self.assert_error_counter_equal(hash_failed_node_name,
+ hash_failed_count)
+
+ #
+ # but if we move the wondow forward to case B, then we can wrap
+ # again
+ #
+ p.scapy_tra_sa.seq_num = 0x100000555
+ pkt = (Ether(src=self.tra_if.remote_mac,
+ dst=self.tra_if.local_mac) /
+ p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
+ dst=self.tra_if.local_ip4) /
+ ICMP(),
+ seq_num=0x100000555))
+ rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
+ decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
+
+ p.scapy_tra_sa.seq_num = 0x200000444
+ pkt = (Ether(src=self.tra_if.remote_mac,
+ dst=self.tra_if.local_mac) /
+ p.scapy_tra_sa.encrypt(IP(src=self.tra_if.remote_ip4,
+ dst=self.tra_if.local_ip4) /
+ ICMP(),
+ seq_num=0x200000444))
+ rx = self.send_and_expect(self.tra_if, [pkt], self.tra_if)
+ decrypted = p.vpp_tra_sa.decrypt(rx[0][IP])
+
else:
- self.send_and_assert_no_replies(self.tra_if, [pkt])
- self.assert_error_counter_equal(
- '/err/%s/sequence number cycled' %
- self.tra4_encrypt_node_name, 1)
+ #
+ # without ESN TX sequence numbers can't wrap and packets are
+ # dropped from here on out.
+ #
+ self.send_and_assert_no_replies(self.tra_if, pkts)
+ seq_cycle_count += len(pkts)
+ self.assert_error_counter_equal(seq_cycle_node_name,
+ seq_cycle_count)
# move the security-associations seq number on to the last we used
self.vapi.cli("test ipsec sa %d seq 0x15f" % p.scapy_tra_sa_id)
@@ -423,6 +540,7 @@ class IpsecTra4(object):
def verify_tra_basic4(self, count=1):
""" ipsec v4 transport basic test """
self.vapi.cli("clear errors")
+ self.vapi.cli("clear ipsec sa")
try:
p = self.params[socket.AF_INET]
send_pkts = self.gen_encrypt_pkts(p.scapy_tra_sa, self.tra_if,
@@ -461,7 +579,7 @@ class IpsecTra4Tests(IpsecTra4):
""" UT test methods for Transport v4 """
def test_tra_anti_replay(self):
""" ipsec v4 transport anti-reply test """
- self.verify_tra_anti_replay(count=1)
+ self.verify_tra_anti_replay()
def test_tra_basic(self, count=1):
""" ipsec v4 transport basic test """
diff --git a/test/test_ipsec_esp.py b/test/test_ipsec_esp.py
index 4da88911016..f5bbc54bd62 100644
--- a/test/test_ipsec_esp.py
+++ b/test/test_ipsec_esp.py
@@ -486,6 +486,14 @@ class TestIpsecEspAll(ConfigIpsecESP,
#
self.unconfig_network()
+ #
+ # reconfigure the network and SA to run the
+ # anti replay tests
+ #
+ self.config_network(self.params.values())
+ self.verify_tra_anti_replay()
+ self.unconfig_network()
+
if __name__ == '__main__':
unittest.main(testRunner=VppTestRunner)