1#!/usr/bin/env python3 2# 3# Copyright (c) 2018, The OpenThread Authors. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions are met: 8# 1. Redistributions of source code must retain the above copyright 9# notice, this list of conditions and the following disclaimer. 10# 2. Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 3. Neither the name of the copyright holder nor the 14# names of its contributors may be used to endorse or promote products 15# derived from this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27# POSSIBILITY OF SUCH DAMAGE. 28 29import wpan 30from wpan import verify 31 32# ----------------------------------------------------------------------------------------------------------------------- 33# Test description: Check insecure data transmission during joining. 34 35test_name = __file__[:-3] if __file__.endswith('.py') else __file__ 36print('-' * 120) 37print('Starting \'{}\''.format(test_name)) 38 39# ----------------------------------------------------------------------------------------------------------------------- 40# Creating `wpan.Nodes` instances 41 42node1 = wpan.Node() 43node2 = wpan.Node() 44 45# ----------------------------------------------------------------------------------------------------------------------- 46# Init all nodes 47 48wpan.Node.init_all_nodes() 49 50# ----------------------------------------------------------------------------------------------------------------------- 51# Build network topology 52 53node1.form("insec-join-test") 54 55# ----------------------------------------------------------------------------------------------------------------------- 56# Test implementation 57 58insecure_port = 1234 59NUM_MSGS = 4 60 61# Make node1 joinable and set the insecure port 62node1.permit_join(duration_sec='100', port=str(insecure_port)) 63 64# Join node1 network from node2 without setting the key 65node2.join_node(node1, should_set_key=False) 66 67verify(node2.get(wpan.WPAN_STATE) == wpan.STATE_CREDENTIALS_NEEDED) 68verify(node2.get(wpan.WPAN_NAME) == node1.get(wpan.WPAN_NAME)) 69verify(node2.get(wpan.WPAN_PANID) == node1.get(wpan.WPAN_PANID)) 70verify(node2.get(wpan.WPAN_XPANID) == node1.get(wpan.WPAN_XPANID)) 71 72ll1 = node1.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1] 73ll2 = node2.get(wpan.WPAN_IP6_LINK_LOCAL_ADDRESS)[1:-1] 74 75# Send insecure traffic from node2 to node1 using link-local IP address 76# for src/dst and insecure port number 77 78sender = node2.prepare_tx(ll2, (ll1, insecure_port), "Hi (insecure)", NUM_MSGS) 79recver = node1.prepare_rx(sender) 80wpan.Node.perform_async_tx_rx() 81verify(sender.was_successful) 82verify(recver.was_successful) 83 84# Get the random src port number used by node1 and ensure node2 allows 85# insecure rx traffic on that port 86 87rx_port = recver.all_rx_msg[0][1][1] 88node2.permit_join(duration_sec='100', port=str(rx_port)) 89 90# Send insecure reply from node1 to node2 91 92sender2 = node1.prepare_tx((ll1, insecure_port), (ll2, rx_port), "Hi back! (insecure)", NUM_MSGS) 93recver2 = node2.prepare_rx(sender2) 94wpan.Node.perform_async_tx_rx() 95verify(sender2.was_successful) 96verify(recver2.was_successful) 97 98# Now node2 fully joins the network (set the network key), check all 99# secure traffic exchange between the nodes 100 101node2.set(wpan.WPAN_KEY, node1.get(wpan.WPAN_KEY)[1:-1], binary_data=True) 102verify(node2.is_associated()) 103 104node1.permit_join('0') 105 106sender = node2.prepare_tx(ll2, (ll1, insecure_port), "Hi (now secure)", NUM_MSGS) 107recver = node1.prepare_rx(sender) 108wpan.Node.perform_async_tx_rx() 109verify(sender.was_successful) 110verify(recver.was_successful) 111 112node2.permit_join('0') 113 114sender2 = node1.prepare_tx((ll1, insecure_port), (ll2, rx_port), "Hi back! (secure now)", NUM_MSGS) 115recver2 = node2.prepare_rx(sender2) 116wpan.Node.perform_async_tx_rx() 117verify(sender2.was_successful) 118verify(recver2.was_successful) 119 120# ----------------------------------------------------------------------------------------------------------------------- 121# Test finished 122 123wpan.Node.finalize_all_nodes() 124 125print('\'{}\' passed.'.format(test_name)) 126