1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """IPsec utilities library."""
16 from ipaddress import ip_network
20 from resources.libraries.python.VatExecutor import VatExecutor
21 from resources.libraries.python.topology import Topology
22 from resources.libraries.python.VatJsonUtil import VatJsonUtil
25 # pylint: disable=too-few-public-methods
26 class PolicyAction(Enum):
32 def __init__(self, string):
36 class CryptoAlg(Enum):
37 """Encryption algorithms."""
38 AES_CBC_128 = ('aes-cbc-128', 'AES-CBC', 16)
39 AES_CBC_192 = ('aes-cbc-128', 'AES-CBC', 24)
40 AES_CBC_256 = ('aes-cbc-256', 'AES-CBC', 32)
42 def __init__(self, alg_name, scapy_name, key_len):
43 self.alg_name = alg_name
44 self.scapy_name = scapy_name
45 self.key_len = key_len
49 """Integrity algorithm."""
50 SHA1_96 = ('sha1-96', 'HMAC-SHA1-96', 20)
51 SHA_256_128 = ('sha-256-128', 'SHA2-256-128', 32)
52 SHA_384_192 = ('sha-384-192', 'SHA2-384-192', 48)
53 SHA_512_256 = ('sha-512-256', 'SHA2-512-256', 64)
55 def __init__(self, alg_name, scapy_name, key_len):
56 self.alg_name = alg_name
57 self.scapy_name = scapy_name
58 self.key_len = key_len
61 class IPsecUtil(object):
62 """IPsec utilities."""
64 # pylint: disable=too-many-arguments
65 # pylint: disable=too-many-locals
68 def policy_action_bypass():
69 """Return policy action bypass.
71 :return: PolicyAction enum BYPASS object.
74 return PolicyAction.BYPASS
77 def policy_action_discard():
78 """Return policy action discard.
80 :return: PolicyAction enum DISCARD object.
83 return PolicyAction.DISCARD
86 def policy_action_protect():
87 """Return policy action protect.
89 :return: PolicyAction enum PROTECT object.
92 return PolicyAction.PROTECT
95 def crypto_alg_aes_cbc_128():
96 """Return encryption algorithm aes-cbc-128.
98 :return: CryptoAlg enum AES_CBC_128 object.
101 return CryptoAlg.AES_CBC_128
104 def crypto_alg_aes_cbc_192():
105 """Return encryption algorithm aes-cbc-192.
107 :return: CryptoAlg enum AES_CBC_192 objec.
110 return CryptoAlg.AES_CBC_192
113 def crypto_alg_aes_cbc_256():
114 """Return encryption algorithm aes-cbc-256.
116 :return: CryptoAlg enum AES_CBC_256 object.
119 return CryptoAlg.AES_CBC_256
122 def get_crypto_alg_key_len(crypto_alg):
123 """Return encryption algorithm key length.
125 :param crypto_alg: Encryption algorithm.
126 :type crypto_alg: CryptoAlg
130 return crypto_alg.key_len
133 def get_crypto_alg_scapy_name(crypto_alg):
134 """Return encryption algorithm scapy name.
136 :param crypto_alg: Encryption algorithm.
137 :type crypto_alg: CryptoAlg
138 :return: Algorithm scapy name.
141 return crypto_alg.scapy_name
144 def integ_alg_sha1_96():
145 """Return integrity algorithm SHA1-96.
147 :return: IntegAlg enum SHA1_96 object.
150 return IntegAlg.SHA1_96
153 def integ_alg_sha_256_128():
154 """Return integrity algorithm SHA-256-128.
156 :return: IntegAlg enum SHA_256_128 object.
159 return IntegAlg.SHA_256_128
162 def integ_alg_sha_384_192():
163 """Return integrity algorithm SHA-384-192.
165 :return: IntegAlg enum SHA_384_192 object.
168 return IntegAlg.SHA_384_192
171 def integ_alg_sha_512_256():
172 """Return integrity algorithm SHA-512-256.
174 :return: IntegAlg enum SHA_512_256 object.
177 return IntegAlg.SHA_512_256
180 def get_integ_alg_key_len(integ_alg):
181 """Return integrity algorithm key length.
183 :param integ_alg: Integrity algorithm.
184 :type integ_alg: IntegAlg
188 return integ_alg.key_len
191 def get_integ_alg_scapy_name(integ_alg):
192 """Return integrity algorithm scapy name.
194 :param integ_alg: Integrity algorithm.
195 :type integ_alg: IntegAlg
196 :return: Algorithm scapy name.
199 return integ_alg.scapy_name
202 def vpp_ipsec_add_sad_entry(node, sad_id, spi, crypto_alg, crypto_key,
203 integ_alg, integ_key, tunnel_src=None,
205 """Create Security Association Database entry on the VPP node.
207 :param node: VPP node to add SAD entry on.
208 :param sad_id: SAD entry ID.
209 :param spi: Security Parameter Index of this SAD entry.
210 :param crypto_alg: The encryption algorithm name.
211 :param crypto_key: The encryption key string.
212 :param integ_alg: The integrity algorithm name.
213 :param integ_key: The integrity key string.
214 :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
215 specified ESP transport mode is used.
216 :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
217 not specified ESP transport mode is used.
221 :type crypto_alg: CryptoAlg
222 :type crypto_key: str
225 :type tunnel_src: str
226 :type tunnel_dst: str
228 ckey = crypto_key.encode('hex')
229 ikey = integ_key.encode('hex')
231 if tunnel_src is not None and tunnel_dst is not None:
232 tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src,
234 out = VatExecutor.cmd_from_template(node,
235 "ipsec/ipsec_sad_add_entry.vat",
236 sad_id=sad_id, spi=spi,
237 calg=crypto_alg.alg_name, ckey=ckey,
238 ialg=integ_alg.alg_name, ikey=ikey,
240 VatJsonUtil.verify_vat_retval(
242 err_msg='Add SAD entry failed on {0}'.format(node['host']))
245 def vpp_ipsec_add_spd(node, spd_id):
246 """Create Security Policy Database on the VPP node.
248 :param node: VPP node to add SPD on.
249 :param spd_id: SPD ID.
253 out = VatExecutor.cmd_from_template(node, "ipsec/ipsec_spd_add.vat",
255 VatJsonUtil.verify_vat_retval(
257 err_msg='Add SPD {0} failed on {1}'.format(spd_id, node['host']))
260 def vpp_ipsec_spd_add_if(node, spd_id, interface):
261 """Add interface to the SPD.
263 :param node: VPP node.
264 :param spd_id: SPD ID to add interface on.
265 :param interface: Interface name or sw_if_index.
268 :type interface: str or int
270 if isinstance(interface, basestring):
271 sw_if_index = Topology.get_interface_sw_index(node, interface)
273 sw_if_index = interface
274 out = VatExecutor.cmd_from_template(node,
275 "ipsec/ipsec_interface_add_spd.vat",
276 spd_id=spd_id, sw_if_id=sw_if_index)
277 VatJsonUtil.verify_vat_retval(
279 err_msg='Add interface {0} to SPD {1} failed on {2}'.format(
280 interface, spd_id, node['host']))
283 def vpp_ipsec_spd_add_entry(node, spd_id, priority, action, inbound=True,
284 sa_id=None, laddr_range=None, raddr_range=None,
285 proto=None, lport_range=None, rport_range=None):
286 """Create Security Policy Database entry on the VPP node.
288 :param node: VPP node to add SPD entry on.
289 :param spd_id: SPD ID to add entry on.
290 :param priority: SPD entry priority, higher number = higher priority.
291 :param action: Policy action.
292 :param inbound: If True policy is for inbound traffic, otherwise
294 :param sa_id: SAD entry ID for protect action.
295 :param laddr_range: Policy selector local IPv4 or IPv6 address range in
296 format IP/prefix or IP/mask. If no mask is provided, it's considered
298 :param raddr_range: Policy selector remote IPv4 or IPv6 address range in
299 format IP/prefix or IP/mask. If no mask is provided, it's considered
301 :param proto: Policy selector next layer protocol number.
302 :param lport_range: Policy selector local TCP/UDP port range in foramt
303 <port_start>-<port_end>.
304 :param rport_range: Policy selector remote TCP/UDP port range in foramt
305 <port_start>-<port_end>.
309 :type action: PolicyAction
312 :type laddr_range: string
313 :type raddr_range: string
315 :type lport_range: string
316 :type rport_range: string
318 direction = 'outbound'
320 direction = 'inbound'
322 act_str = action.value
323 if PolicyAction.PROTECT == action and sa_id is not None:
324 act_str += 'sa_id {0}'.format(sa_id)
327 if laddr_range is not None:
328 net = ip_network(unicode(laddr_range), strict=False)
329 selector += 'laddr_start {0} laddr_stop {1} '.format(
330 net.network_address, net.broadcast_address)
331 if raddr_range is not None:
332 net = ip_network(unicode(raddr_range), strict=False)
333 selector += 'raddr_start {0} raddr_stop {1} '.format(
334 net.network_address, net.broadcast_address)
335 if proto is not None:
336 selector += 'protocol {0} '.format(proto)
337 if lport_range is not None:
338 selector += 'lport_start {p[0]} lport_stop {p[1]} '.format(
339 p=lport_range.split('-'))
340 if rport_range is not None:
341 selector += 'rport_start {p[0]} rport_stop {p[1]} '.format(
342 p=rport_range.split('-'))
344 out = VatExecutor.cmd_from_template(node,
345 "ipsec/ipsec_spd_add_entry.vat",
346 spd_id=spd_id, priority=priority,
347 action=act_str, direction=direction,
349 VatJsonUtil.verify_vat_retval(
351 err_msg='Add entry to SPD {0} failed on {1}'.format(spd_id,
355 def vpp_ipsec_show(node):
356 """Run "show ipsec" debug CLI command.
358 :param node: Node to run command on.
361 VatExecutor().execute_script("ipsec/ipsec_show.vat", node,