def testInvalidAlgorithms(self): key = "af442892cdcd0ef650e9c299f9a8436a".decode("hex") invalid_auth = (xfrm.XfrmAlgoAuth(("invalid(algo)", 128, 96)), key) invalid_crypt = (xfrm.XfrmAlgo(("invalid(algo)", 128)), key) with self.assertRaisesErrno(ENOSYS): self.xfrm.AddSaInfo(TEST_ADDR1, TEST_ADDR2, 0x1234, xfrm.XFRM_MODE_TRANSPORT, 0, xfrm_base._ALGO_CBC_AES_256, invalid_auth, None, None, None, 0) with self.assertRaisesErrno(ENOSYS): self.xfrm.AddSaInfo(TEST_ADDR1, TEST_ADDR2, 0x1234, xfrm.XFRM_MODE_TRANSPORT, 0, invalid_crypt, xfrm_base._ALGO_HMAC_SHA1, None, None, None, 0)
def ParamTestSocketPolicySimple(self, params): """Test two-way traffic using transport mode and socket policies.""" def AssertEncrypted(packet): # This gives a free pass to ICMP and ICMPv6 packets, which show up # nondeterministically in tests. self.assertEquals(None, packet.getlayer(scapy.UDP), "UDP packet sent in the clear") self.assertEquals(None, packet.getlayer(scapy.TCP), "TCP packet sent in the clear") # We create a pair of sockets, "left" and "right", that will talk to each # other using transport mode ESP. Because of TapTwister, both sockets # perceive each other as owning "remote_addr". netid = self.RandomNetid() family = net_test.GetAddressFamily(params["version"]) local_addr = self.MyAddress(params["version"], netid) remote_addr = self.GetRemoteSocketAddress(params["version"]) crypt_left = (xfrm.XfrmAlgo( (params["crypt"].name, params["crypt"].key_len)), os.urandom(params["crypt"].key_len / 8)) if params["crypt"] else None crypt_right = (xfrm.XfrmAlgo( (params["crypt"].name, params["crypt"].key_len)), os.urandom(params["crypt"].key_len / 8)) if params["crypt"] else None auth_left = (xfrm.XfrmAlgoAuth( (params["auth"].name, params["auth"].key_len, params["auth"].trunc_len)), os.urandom(params["auth"].key_len / 8)) if params["auth"] else None auth_right = (xfrm.XfrmAlgoAuth( (params["auth"].name, params["auth"].key_len, params["auth"].trunc_len)), os.urandom(params["auth"].key_len / 8)) if params["auth"] else None aead_left = (xfrm.XfrmAlgoAead( (params["aead"].name, params["aead"].key_len, params["aead"].icv_len)), os.urandom(params["aead"].key_len / 8)) if params["aead"] else None aead_right = (xfrm.XfrmAlgoAead( (params["aead"].name, params["aead"].key_len, params["aead"].icv_len)), os.urandom(params["aead"].key_len / 8)) if params["aead"] else None spi_left = 0xbeefface spi_right = 0xcafed00d req_ids = [100, 200, 300, 400] # Used to match templates and SAs. # Left outbound SA self.xfrm.AddSaInfo(src=local_addr, dst=remote_addr, spi=spi_right, mode=xfrm.XFRM_MODE_TRANSPORT, reqid=req_ids[0], encryption=crypt_right, auth_trunc=auth_right, aead=aead_right, encap=None, mark=None, output_mark=None) # Right inbound SA self.xfrm.AddSaInfo(src=remote_addr, dst=local_addr, spi=spi_right, mode=xfrm.XFRM_MODE_TRANSPORT, reqid=req_ids[1], encryption=crypt_right, auth_trunc=auth_right, aead=aead_right, encap=None, mark=None, output_mark=None) # Right outbound SA self.xfrm.AddSaInfo(src=local_addr, dst=remote_addr, spi=spi_left, mode=xfrm.XFRM_MODE_TRANSPORT, reqid=req_ids[2], encryption=crypt_left, auth_trunc=auth_left, aead=aead_left, encap=None, mark=None, output_mark=None) # Left inbound SA self.xfrm.AddSaInfo(src=remote_addr, dst=local_addr, spi=spi_left, mode=xfrm.XFRM_MODE_TRANSPORT, reqid=req_ids[3], encryption=crypt_left, auth_trunc=auth_left, aead=aead_left, encap=None, mark=None, output_mark=None) # Make two sockets. sock_left = socket(family, params["proto"], 0) sock_left.settimeout(2.0) sock_left.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.SelectInterface(sock_left, netid, "mark") sock_right = socket(family, params["proto"], 0) sock_right.settimeout(2.0) sock_right.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) self.SelectInterface(sock_right, netid, "mark") # For UDP, set SO_LINGER to 0, to prevent TCP sockets from hanging around # in a TIME_WAIT state. if params["proto"] == SOCK_STREAM: net_test.DisableFinWait(sock_left) net_test.DisableFinWait(sock_right) # Apply the left outbound socket policy. xfrm_base.ApplySocketPolicy(sock_left, family, xfrm.XFRM_POLICY_OUT, spi_right, req_ids[0], None) # Apply right inbound socket policy. xfrm_base.ApplySocketPolicy(sock_right, family, xfrm.XFRM_POLICY_IN, spi_right, req_ids[1], None) # Apply right outbound socket policy. xfrm_base.ApplySocketPolicy(sock_right, family, xfrm.XFRM_POLICY_OUT, spi_left, req_ids[2], None) # Apply left inbound socket policy. xfrm_base.ApplySocketPolicy(sock_left, family, xfrm.XFRM_POLICY_IN, spi_left, req_ids[3], None) server_ready = threading.Event() server_error = None # Save exceptions thrown by the server. def TcpServer(sock, client_port): try: sock.listen(1) server_ready.set() accepted, peer = sock.accept() self.assertEquals(remote_addr, peer[0]) self.assertEquals(client_port, peer[1]) data = accepted.recv(2048) self.assertEquals("hello request", data) accepted.send("hello response") except Exception as e: server_error = e finally: sock.close() def UdpServer(sock, client_port): try: server_ready.set() data, peer = sock.recvfrom(2048) self.assertEquals(remote_addr, peer[0]) self.assertEquals(client_port, peer[1]) self.assertEquals("hello request", data) sock.sendto("hello response", peer) except Exception as e: server_error = e finally: sock.close() # Server and client need to know each other's port numbers in advance. wildcard_addr = net_test.GetWildcardAddress(params["version"]) sock_left.bind((wildcard_addr, 0)) sock_right.bind((wildcard_addr, 0)) left_port = sock_left.getsockname()[1] right_port = sock_right.getsockname()[1] # Start the appropriate server type on sock_right. target = TcpServer if params["proto"] == SOCK_STREAM else UdpServer server = threading.Thread(target=target, args=(sock_right, left_port), name="SocketServer") server.start() # Wait for server to be ready before attempting to connect. TCP retries # hide this problem, but UDP will fail outright if the server socket has # not bound when we send. self.assertTrue(server_ready.wait(2.0), "Timed out waiting for server thread") with TapTwister(fd=self.tuns[netid].fileno(), validator=AssertEncrypted): sock_left.connect((remote_addr, right_port)) sock_left.send("hello request") data = sock_left.recv(2048) self.assertEquals("hello response", data) sock_left.close() server.join() if server_error: raise server_error
import itertools from scapy import all as scapy from socket import * # pylint: disable=wildcard-import import subprocess import threading import unittest import multinetwork_base import net_test from tun_twister import TapTwister import xfrm import xfrm_base # List of encryption algorithms for use in ParamTests. CRYPT_ALGOS = [ xfrm.XfrmAlgo((xfrm.XFRM_EALG_CBC_AES, 128)), xfrm.XfrmAlgo((xfrm.XFRM_EALG_CBC_AES, 192)), xfrm.XfrmAlgo((xfrm.XFRM_EALG_CBC_AES, 256)), ] # List of auth algorithms for use in ParamTests. AUTH_ALGOS = [ # RFC 4868 specifies that the only supported truncation length is half the # hash size. xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_MD5, 128, 96)), xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_SHA1, 160, 96)), xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_SHA256, 256, 128)), xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_SHA384, 384, 192)), xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_SHA512, 512, 256)), # Test larger truncation lengths for good measure. xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_MD5, 128, 128)),
XFRM_ADDR_ANY = 16 * "\x00" LOOPBACK = 15 * "\x00" + "\x01" ENCRYPTED_PAYLOAD = ("b1c74998efd6326faebe2061f00f2c750e90e76001664a80c287b150" "59e74bf949769cc6af71e51b539e7de3a2a14cb05a231b969e035174" "d98c5aa0cef1937db98889ec0d08fa408fecf616") ENCRYPTION_KEY = ("308146eb3bd84b044573d60f5a5fd159" "57c7d4fe567a2120f35bae0f9869ec22".decode("hex")) AUTH_TRUNC_KEY = "af442892cdcd0ef650e9c299f9a8436a".decode("hex") TEST_ADDR1 = "2001:4860:4860::8888" TEST_ADDR2 = "2001:4860:4860::8844" TEST_SPI = 0x1234 ALL_ALGORITHMS = 0xffffffff ALGO_CBC_AES_256 = xfrm.XfrmAlgo(("cbc(aes)", 256)) ALGO_HMAC_SHA1 = xfrm.XfrmAlgoAuth(("hmac(sha1)", 128, 96)) class XfrmTest(multinetwork_base.MultiNetworkBaseTest): @classmethod def setUpClass(cls): super(XfrmTest, cls).setUpClass() cls.xfrm = xfrm.Xfrm() def setUp(self): # TODO: delete this when we're more diligent about deleting our SAs. super(XfrmTest, self).setUp() subprocess.call("ip xfrm state flush".split()) def expectIPv6EspPacketOn(self, netid, spi, seq, length):
import csocket import cstruct import multinetwork_base import net_test import util import xfrm _ENCRYPTION_KEY_256 = ("308146eb3bd84b044573d60f5a5fd159" "57c7d4fe567a2120f35bae0f9869ec22".decode("hex")) _AUTHENTICATION_KEY_128 = "af442892cdcd0ef650e9c299f9a8436a".decode("hex") _ALGO_AUTH_NULL = (xfrm.XfrmAlgoAuth(("digest_null", 0, 0)), "") _ALGO_HMAC_SHA1 = (xfrm.XfrmAlgoAuth((xfrm.XFRM_AALG_HMAC_SHA1, 128, 96)), _AUTHENTICATION_KEY_128) _ALGO_CRYPT_NULL = (xfrm.XfrmAlgo(("ecb(cipher_null)", 0)), "") _ALGO_CBC_AES_256 = (xfrm.XfrmAlgo((xfrm.XFRM_EALG_CBC_AES, 256)), _ENCRYPTION_KEY_256) # Match all bits of the mark MARK_MASK_ALL = 0xffffffff def SetPolicySockopt(sock, family, opt_data): optlen = len(opt_data) if opt_data is not None else 0 if family == AF_INET: csocket.Setsockopt(sock, IPPROTO_IP, xfrm.IP_XFRM_POLICY, opt_data, optlen) else: csocket.Setsockopt(sock, IPPROTO_IPV6, xfrm.IPV6_XFRM_POLICY, opt_data, optlen)