|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "nsconfig.h" |
| 19 | +#include <string.h> |
| 20 | +#include "ns_types.h" |
| 21 | +#include "ns_list.h" |
| 22 | +#include "ns_trace.h" |
| 23 | +#include "nsdynmemLIB.h" |
| 24 | +#include "fhss_config.h" |
| 25 | +#include "NWK_INTERFACE/Include/protocol.h" |
| 26 | +#include "6LoWPAN/ws/ws_config.h" |
| 27 | +#include "Security/protocols/sec_prot_cfg.h" |
| 28 | +#include "Security/kmp/kmp_addr.h" |
| 29 | +#include "Security/kmp/kmp_api.h" |
| 30 | +#include "Security/PANA/pana_eap_header.h" |
| 31 | +#include "Security/eapol/eapol_helper.h" |
| 32 | +#include "Security/eapol/kde_helper.h" |
| 33 | +#include "Security/protocols/sec_prot_certs.h" |
| 34 | +#include "Security/protocols/sec_prot_keys.h" |
| 35 | +#include "Security/protocols/sec_prot.h" |
| 36 | +#include "Security/protocols/sec_prot_lib.h" |
| 37 | +#include "Security/protocols/msg_sec_prot/msg_sec_prot.h" |
| 38 | + |
| 39 | +#ifdef HAVE_WS |
| 40 | + |
| 41 | +#define TRACE_GROUP "msep" |
| 42 | + |
| 43 | +typedef enum { |
| 44 | + MSG_STATE_INIT = SEC_STATE_INIT, |
| 45 | + MSG_STATE_CREATE_REQ = SEC_STATE_CREATE_REQ, |
| 46 | + MSG_STATE_FINISH = SEC_STATE_FINISH, |
| 47 | + MSG_STATE_FINISHED = SEC_STATE_FINISHED |
| 48 | +} msg_sec_prot_state_e; |
| 49 | + |
| 50 | +typedef struct { |
| 51 | + sec_prot_common_t common; /**< Common data */ |
| 52 | +} msg_sec_prot_int_t; |
| 53 | + |
| 54 | +static uint16_t msg_sec_prot_size(void); |
| 55 | +static int8_t msg_sec_prot_init(sec_prot_t *prot); |
| 56 | +static void msg_sec_prot_delete(sec_prot_t *prot); |
| 57 | + |
| 58 | +static void msg_sec_prot_create_request(sec_prot_t *prot, sec_prot_keys_t *sec_keys); |
| 59 | +static void msg_sec_prot_state_machine(sec_prot_t *prot); |
| 60 | +static int8_t msg_sec_prot_auth_rejected_send(sec_prot_t *prot, sec_prot_keys_t *sec_keys); |
| 61 | + |
| 62 | +#define msg_sec_prot_get(prot) (msg_sec_prot_int_t *) &prot->data |
| 63 | + |
| 64 | +int8_t msg_sec_prot_register(kmp_service_t *service) |
| 65 | +{ |
| 66 | + if (!service) { |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + if (kmp_service_sec_protocol_register(service, MSG_PROT, msg_sec_prot_size, msg_sec_prot_init) < 0) { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + return 0; |
| 75 | +} |
| 76 | + |
| 77 | +static uint16_t msg_sec_prot_size(void) |
| 78 | +{ |
| 79 | + return sizeof(msg_sec_prot_int_t); |
| 80 | +} |
| 81 | + |
| 82 | +static int8_t msg_sec_prot_init(sec_prot_t *prot) |
| 83 | +{ |
| 84 | + prot->create_req = msg_sec_prot_create_request; |
| 85 | + prot->delete = msg_sec_prot_delete; |
| 86 | + prot->state_machine = msg_sec_prot_state_machine; |
| 87 | + |
| 88 | + msg_sec_prot_int_t *data = msg_sec_prot_get(prot); |
| 89 | + sec_prot_init(&data->common); |
| 90 | + sec_prot_state_set(prot, &data->common, MSG_STATE_INIT); |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +static void msg_sec_prot_delete(sec_prot_t *prot) |
| 96 | +{ |
| 97 | + (void) prot; |
| 98 | +} |
| 99 | + |
| 100 | +static void msg_sec_prot_create_request(sec_prot_t *prot, sec_prot_keys_t *sec_keys) |
| 101 | +{ |
| 102 | + (void) sec_keys; |
| 103 | + |
| 104 | + prot->state_machine(prot); |
| 105 | +} |
| 106 | + |
| 107 | +static int8_t msg_sec_prot_auth_rejected_send(sec_prot_t *prot, sec_prot_keys_t *sec_keys) |
| 108 | +{ |
| 109 | + (void) sec_keys; |
| 110 | + |
| 111 | + uint8_t *eapol_pdu_frame = ns_dyn_mem_temporary_alloc(prot->header_size); |
| 112 | + |
| 113 | + // Send zero length message to relay which requests LLC to remove EAPOL temporary entry based on EUI-64 |
| 114 | + if (prot->send(prot, eapol_pdu_frame, prot->header_size) < 0) { |
| 115 | + return -1; |
| 116 | + } |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +static void msg_sec_prot_state_machine(sec_prot_t *prot) |
| 122 | +{ |
| 123 | + msg_sec_prot_int_t *data = msg_sec_prot_get(prot); |
| 124 | + |
| 125 | + switch (sec_prot_state_get(&data->common)) { |
| 126 | + case MSG_STATE_INIT: |
| 127 | + sec_prot_state_set(prot, &data->common, MSG_STATE_CREATE_REQ); |
| 128 | + break; |
| 129 | + case MSG_STATE_CREATE_REQ: |
| 130 | + // KMP-CREATE.confirm |
| 131 | + prot->create_conf(prot, sec_prot_result_get(&data->common)); |
| 132 | + // Authentication rejected (will continue only after new EAPOL Initial-Key) |
| 133 | + (void) msg_sec_prot_auth_rejected_send(prot, prot->sec_keys); |
| 134 | + sec_prot_state_set(prot, &data->common, MSG_STATE_FINISH); |
| 135 | + break; |
| 136 | + case MSG_STATE_FINISH: |
| 137 | + sec_prot_state_set(prot, &data->common, MSG_STATE_FINISHED); |
| 138 | + /* fall through */ |
| 139 | + case MSG_STATE_FINISHED: |
| 140 | + prot->finished(prot); |
| 141 | + break; |
| 142 | + default: |
| 143 | + break; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#endif /* HAVE_WS */ |
| 148 | + |
0 commit comments