test
Fork of nRF51822 by
source/btle/btle_security.cpp@616:a8f9b022d8fd, 2016-04-06 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Wed Apr 06 22:39:17 2016 +0100
- Revision:
- 616:a8f9b022d8fd
- Parent:
- 615:65ea2acfc6a2
Synchronized with git rev 67d0ac73
Author: Joe Finney
microbit: Update to indicate when system attributes require intialisation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LancasterUniversity | 615:65ea2acfc6a2 | 1 | /* mbed Microcontroller Library |
LancasterUniversity | 615:65ea2acfc6a2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
LancasterUniversity | 615:65ea2acfc6a2 | 3 | * |
LancasterUniversity | 615:65ea2acfc6a2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
LancasterUniversity | 615:65ea2acfc6a2 | 5 | * you may not use this file except in compliance with the License. |
LancasterUniversity | 615:65ea2acfc6a2 | 6 | * You may obtain a copy of the License at |
LancasterUniversity | 615:65ea2acfc6a2 | 7 | * |
LancasterUniversity | 615:65ea2acfc6a2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
LancasterUniversity | 615:65ea2acfc6a2 | 9 | * |
LancasterUniversity | 615:65ea2acfc6a2 | 10 | * Unless required by applicable law or agreed to in writing, software |
LancasterUniversity | 615:65ea2acfc6a2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
LancasterUniversity | 615:65ea2acfc6a2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
LancasterUniversity | 615:65ea2acfc6a2 | 13 | * See the License for the specific language governing permissions and |
LancasterUniversity | 615:65ea2acfc6a2 | 14 | * limitations under the License. |
LancasterUniversity | 615:65ea2acfc6a2 | 15 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 16 | |
LancasterUniversity | 615:65ea2acfc6a2 | 17 | #include "btle.h" |
LancasterUniversity | 616:a8f9b022d8fd | 18 | |
LancasterUniversity | 616:a8f9b022d8fd | 19 | #include "nRF5xn.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 20 | |
LancasterUniversity | 616:a8f9b022d8fd | 21 | extern "C" { |
LancasterUniversity | 616:a8f9b022d8fd | 22 | #include "pstorage.h" |
LancasterUniversity | 616:a8f9b022d8fd | 23 | #include "device_manager.h" |
LancasterUniversity | 616:a8f9b022d8fd | 24 | #include "id_manager.h" |
LancasterUniversity | 616:a8f9b022d8fd | 25 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 26 | |
LancasterUniversity | 615:65ea2acfc6a2 | 27 | #include "btle_security.h" |
LancasterUniversity | 615:65ea2acfc6a2 | 28 | |
LancasterUniversity | 615:65ea2acfc6a2 | 29 | static dm_application_instance_t applicationInstance; |
LancasterUniversity | 616:a8f9b022d8fd | 30 | static bool initialized = false; |
LancasterUniversity | 615:65ea2acfc6a2 | 31 | static ret_code_t dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result); |
LancasterUniversity | 615:65ea2acfc6a2 | 32 | |
LancasterUniversity | 616:a8f9b022d8fd | 33 | // default security parameters |
LancasterUniversity | 616:a8f9b022d8fd | 34 | static ble_gap_sec_params_t securityParameters = { |
LancasterUniversity | 616:a8f9b022d8fd | 35 | .bond = true, /**< Perform bonding. */ |
LancasterUniversity | 616:a8f9b022d8fd | 36 | .mitm = true, /**< Man In The Middle protection required. */ |
LancasterUniversity | 616:a8f9b022d8fd | 37 | .io_caps = SecurityManager::IO_CAPS_NONE, /**< IO capabilities, see @ref BLE_GAP_IO_CAPS. */ |
LancasterUniversity | 616:a8f9b022d8fd | 38 | .oob = 0, /**< Out Of Band data available. */ |
LancasterUniversity | 616:a8f9b022d8fd | 39 | .min_key_size = 16, /**< Minimum encryption key size in octets between 7 and 16. If 0 then not applicable in this instance. */ |
LancasterUniversity | 616:a8f9b022d8fd | 40 | .max_key_size = 16, /**< Maximum encryption key size in octets between min_key_size and 16. */ |
LancasterUniversity | 616:a8f9b022d8fd | 41 | .kdist_periph = { |
LancasterUniversity | 616:a8f9b022d8fd | 42 | .enc = 1, /**< Long Term Key and Master Identification. */ |
LancasterUniversity | 616:a8f9b022d8fd | 43 | .id = 1, /**< Identity Resolving Key and Identity Address Information. */ |
LancasterUniversity | 616:a8f9b022d8fd | 44 | .sign = 1, /**< Connection Signature Resolving Key. */ |
LancasterUniversity | 616:a8f9b022d8fd | 45 | }, /**< Key distribution bitmap: keys that the peripheral device will distribute. */ |
LancasterUniversity | 616:a8f9b022d8fd | 46 | }; |
LancasterUniversity | 616:a8f9b022d8fd | 47 | |
LancasterUniversity | 616:a8f9b022d8fd | 48 | bool |
LancasterUniversity | 616:a8f9b022d8fd | 49 | btle_hasInitializedSecurity(void) |
LancasterUniversity | 616:a8f9b022d8fd | 50 | { |
LancasterUniversity | 616:a8f9b022d8fd | 51 | return initialized; |
LancasterUniversity | 616:a8f9b022d8fd | 52 | } |
LancasterUniversity | 616:a8f9b022d8fd | 53 | |
LancasterUniversity | 615:65ea2acfc6a2 | 54 | ble_error_t |
LancasterUniversity | 615:65ea2acfc6a2 | 55 | btle_initializeSecurity(bool enableBonding, |
LancasterUniversity | 615:65ea2acfc6a2 | 56 | bool requireMITM, |
LancasterUniversity | 615:65ea2acfc6a2 | 57 | SecurityManager::SecurityIOCapabilities_t iocaps, |
LancasterUniversity | 615:65ea2acfc6a2 | 58 | const SecurityManager::Passkey_t passkey) |
LancasterUniversity | 615:65ea2acfc6a2 | 59 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 60 | /* guard against multiple initializations */ |
LancasterUniversity | 615:65ea2acfc6a2 | 61 | if (initialized) { |
LancasterUniversity | 615:65ea2acfc6a2 | 62 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 63 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 64 | |
LancasterUniversity | 615:65ea2acfc6a2 | 65 | if (pstorage_init() != NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 66 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 615:65ea2acfc6a2 | 67 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 68 | |
LancasterUniversity | 615:65ea2acfc6a2 | 69 | ret_code_t rc; |
LancasterUniversity | 615:65ea2acfc6a2 | 70 | if (passkey) { |
LancasterUniversity | 615:65ea2acfc6a2 | 71 | ble_opt_t opts; |
LancasterUniversity | 615:65ea2acfc6a2 | 72 | opts.gap_opt.passkey.p_passkey = const_cast<uint8_t *>(passkey); |
LancasterUniversity | 615:65ea2acfc6a2 | 73 | if ((rc = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &opts)) != NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 74 | switch (rc) { |
LancasterUniversity | 615:65ea2acfc6a2 | 75 | case BLE_ERROR_INVALID_CONN_HANDLE: |
LancasterUniversity | 615:65ea2acfc6a2 | 76 | case NRF_ERROR_INVALID_ADDR: |
LancasterUniversity | 615:65ea2acfc6a2 | 77 | case NRF_ERROR_INVALID_PARAM: |
LancasterUniversity | 615:65ea2acfc6a2 | 78 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 79 | return BLE_ERROR_INVALID_PARAM; |
LancasterUniversity | 615:65ea2acfc6a2 | 80 | case NRF_ERROR_INVALID_STATE: |
LancasterUniversity | 615:65ea2acfc6a2 | 81 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 615:65ea2acfc6a2 | 82 | case NRF_ERROR_BUSY: |
LancasterUniversity | 615:65ea2acfc6a2 | 83 | return BLE_STACK_BUSY; |
LancasterUniversity | 615:65ea2acfc6a2 | 84 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 85 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 86 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 87 | |
LancasterUniversity | 615:65ea2acfc6a2 | 88 | dm_init_param_t dm_init_param = { |
LancasterUniversity | 615:65ea2acfc6a2 | 89 | .clear_persistent_data = false /* Set to true in case the module should clear all persistent data. */ |
LancasterUniversity | 615:65ea2acfc6a2 | 90 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 91 | if (dm_init(&dm_init_param) != NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 92 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 615:65ea2acfc6a2 | 93 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 94 | |
LancasterUniversity | 616:a8f9b022d8fd | 95 | // update default security parameters with function call parameters |
LancasterUniversity | 616:a8f9b022d8fd | 96 | securityParameters.bond = enableBonding; |
LancasterUniversity | 616:a8f9b022d8fd | 97 | securityParameters.mitm = requireMITM; |
LancasterUniversity | 616:a8f9b022d8fd | 98 | securityParameters.io_caps = iocaps; |
LancasterUniversity | 616:a8f9b022d8fd | 99 | |
LancasterUniversity | 615:65ea2acfc6a2 | 100 | const dm_application_param_t dm_param = { |
LancasterUniversity | 615:65ea2acfc6a2 | 101 | .evt_handler = dm_handler, |
LancasterUniversity | 615:65ea2acfc6a2 | 102 | .service_type = DM_PROTOCOL_CNTXT_GATT_CLI_ID, |
LancasterUniversity | 616:a8f9b022d8fd | 103 | .sec_param = securityParameters |
LancasterUniversity | 615:65ea2acfc6a2 | 104 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 105 | |
LancasterUniversity | 615:65ea2acfc6a2 | 106 | if ((rc = dm_register(&applicationInstance, &dm_param)) != NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 107 | switch (rc) { |
LancasterUniversity | 615:65ea2acfc6a2 | 108 | case NRF_ERROR_INVALID_STATE: |
LancasterUniversity | 615:65ea2acfc6a2 | 109 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 615:65ea2acfc6a2 | 110 | case NRF_ERROR_NO_MEM: |
LancasterUniversity | 615:65ea2acfc6a2 | 111 | return BLE_ERROR_NO_MEM; |
LancasterUniversity | 615:65ea2acfc6a2 | 112 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 113 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 615:65ea2acfc6a2 | 114 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 115 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 116 | |
LancasterUniversity | 615:65ea2acfc6a2 | 117 | initialized = true; |
LancasterUniversity | 615:65ea2acfc6a2 | 118 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 119 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 120 | |
LancasterUniversity | 615:65ea2acfc6a2 | 121 | ble_error_t |
LancasterUniversity | 615:65ea2acfc6a2 | 122 | btle_purgeAllBondingState(void) |
LancasterUniversity | 615:65ea2acfc6a2 | 123 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 124 | ret_code_t rc; |
LancasterUniversity | 615:65ea2acfc6a2 | 125 | if ((rc = dm_device_delete_all(&applicationInstance)) == NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 126 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 127 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 128 | |
LancasterUniversity | 615:65ea2acfc6a2 | 129 | switch (rc) { |
LancasterUniversity | 615:65ea2acfc6a2 | 130 | case NRF_ERROR_INVALID_STATE: |
LancasterUniversity | 615:65ea2acfc6a2 | 131 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 615:65ea2acfc6a2 | 132 | case NRF_ERROR_NO_MEM: |
LancasterUniversity | 615:65ea2acfc6a2 | 133 | return BLE_ERROR_NO_MEM; |
LancasterUniversity | 615:65ea2acfc6a2 | 134 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 135 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 615:65ea2acfc6a2 | 136 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 137 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 138 | |
LancasterUniversity | 615:65ea2acfc6a2 | 139 | ble_error_t |
LancasterUniversity | 615:65ea2acfc6a2 | 140 | btle_getLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::LinkSecurityStatus_t *securityStatusP) |
LancasterUniversity | 615:65ea2acfc6a2 | 141 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 142 | ret_code_t rc; |
LancasterUniversity | 615:65ea2acfc6a2 | 143 | dm_handle_t dmHandle = { |
LancasterUniversity | 615:65ea2acfc6a2 | 144 | .appl_id = applicationInstance, |
LancasterUniversity | 615:65ea2acfc6a2 | 145 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 146 | if ((rc = dm_handle_get(connectionHandle, &dmHandle)) != NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 147 | if (rc == NRF_ERROR_NOT_FOUND) { |
LancasterUniversity | 615:65ea2acfc6a2 | 148 | return BLE_ERROR_INVALID_PARAM; |
LancasterUniversity | 615:65ea2acfc6a2 | 149 | } else { |
LancasterUniversity | 615:65ea2acfc6a2 | 150 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 615:65ea2acfc6a2 | 151 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 152 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 153 | |
LancasterUniversity | 615:65ea2acfc6a2 | 154 | if ((rc = dm_security_status_req(&dmHandle, reinterpret_cast<dm_security_status_t *>(securityStatusP))) != NRF_SUCCESS) { |
LancasterUniversity | 615:65ea2acfc6a2 | 155 | switch (rc) { |
LancasterUniversity | 615:65ea2acfc6a2 | 156 | case NRF_ERROR_INVALID_STATE: |
LancasterUniversity | 615:65ea2acfc6a2 | 157 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 615:65ea2acfc6a2 | 158 | case NRF_ERROR_NO_MEM: |
LancasterUniversity | 615:65ea2acfc6a2 | 159 | return BLE_ERROR_NO_MEM; |
LancasterUniversity | 615:65ea2acfc6a2 | 160 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 161 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 615:65ea2acfc6a2 | 162 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 163 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 164 | |
LancasterUniversity | 615:65ea2acfc6a2 | 165 | return BLE_ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 166 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 167 | |
LancasterUniversity | 616:a8f9b022d8fd | 168 | ble_error_t |
LancasterUniversity | 616:a8f9b022d8fd | 169 | btle_setLinkSecurity(Gap::Handle_t connectionHandle, SecurityManager::SecurityMode_t securityMode) |
LancasterUniversity | 616:a8f9b022d8fd | 170 | { |
LancasterUniversity | 616:a8f9b022d8fd | 171 | // use default and updated parameters as starting point |
LancasterUniversity | 616:a8f9b022d8fd | 172 | // and modify structure based on security mode. |
LancasterUniversity | 616:a8f9b022d8fd | 173 | ble_gap_sec_params_t params = securityParameters; |
LancasterUniversity | 616:a8f9b022d8fd | 174 | |
LancasterUniversity | 616:a8f9b022d8fd | 175 | switch (securityMode) { |
LancasterUniversity | 616:a8f9b022d8fd | 176 | case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK: |
LancasterUniversity | 616:a8f9b022d8fd | 177 | /**< Require no protection, open link. */ |
LancasterUniversity | 616:a8f9b022d8fd | 178 | securityParameters.bond = false; |
LancasterUniversity | 616:a8f9b022d8fd | 179 | securityParameters.mitm = false; |
LancasterUniversity | 616:a8f9b022d8fd | 180 | break; |
LancasterUniversity | 616:a8f9b022d8fd | 181 | |
LancasterUniversity | 616:a8f9b022d8fd | 182 | case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM: |
LancasterUniversity | 616:a8f9b022d8fd | 183 | /**< Require encryption, but no MITM protection. */ |
LancasterUniversity | 616:a8f9b022d8fd | 184 | securityParameters.bond = true; |
LancasterUniversity | 616:a8f9b022d8fd | 185 | securityParameters.mitm = false; |
LancasterUniversity | 616:a8f9b022d8fd | 186 | break; |
LancasterUniversity | 616:a8f9b022d8fd | 187 | |
LancasterUniversity | 616:a8f9b022d8fd | 188 | // not yet implemented security modes |
LancasterUniversity | 616:a8f9b022d8fd | 189 | case SecurityManager::SECURITY_MODE_NO_ACCESS: |
LancasterUniversity | 616:a8f9b022d8fd | 190 | case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM: |
LancasterUniversity | 616:a8f9b022d8fd | 191 | /**< Require encryption and MITM protection. */ |
LancasterUniversity | 616:a8f9b022d8fd | 192 | case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM: |
LancasterUniversity | 616:a8f9b022d8fd | 193 | /**< Require signing or encryption, but no MITM protection. */ |
LancasterUniversity | 616:a8f9b022d8fd | 194 | case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM: |
LancasterUniversity | 616:a8f9b022d8fd | 195 | /**< Require signing or encryption, and MITM protection. */ |
LancasterUniversity | 616:a8f9b022d8fd | 196 | default: |
LancasterUniversity | 616:a8f9b022d8fd | 197 | return BLE_ERROR_NOT_IMPLEMENTED; |
LancasterUniversity | 616:a8f9b022d8fd | 198 | } |
LancasterUniversity | 616:a8f9b022d8fd | 199 | |
LancasterUniversity | 616:a8f9b022d8fd | 200 | // update security settings for given connection |
LancasterUniversity | 616:a8f9b022d8fd | 201 | uint32_t result = sd_ble_gap_authenticate(connectionHandle, ¶ms); |
LancasterUniversity | 616:a8f9b022d8fd | 202 | |
LancasterUniversity | 616:a8f9b022d8fd | 203 | if (result == NRF_SUCCESS) { |
LancasterUniversity | 616:a8f9b022d8fd | 204 | return BLE_ERROR_NONE; |
LancasterUniversity | 616:a8f9b022d8fd | 205 | } else { |
LancasterUniversity | 616:a8f9b022d8fd | 206 | return BLE_ERROR_UNSPECIFIED; |
LancasterUniversity | 616:a8f9b022d8fd | 207 | } |
LancasterUniversity | 616:a8f9b022d8fd | 208 | } |
LancasterUniversity | 616:a8f9b022d8fd | 209 | |
LancasterUniversity | 615:65ea2acfc6a2 | 210 | ret_code_t |
LancasterUniversity | 615:65ea2acfc6a2 | 211 | dm_handler(dm_handle_t const *p_handle, dm_event_t const *p_event, ret_code_t event_result) |
LancasterUniversity | 615:65ea2acfc6a2 | 212 | { |
LancasterUniversity | 616:a8f9b022d8fd | 213 | nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE); |
LancasterUniversity | 616:a8f9b022d8fd | 214 | nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager(); |
LancasterUniversity | 616:a8f9b022d8fd | 215 | |
LancasterUniversity | 615:65ea2acfc6a2 | 216 | switch (p_event->event_id) { |
LancasterUniversity | 615:65ea2acfc6a2 | 217 | case DM_EVT_SECURITY_SETUP: /* started */ { |
LancasterUniversity | 615:65ea2acfc6a2 | 218 | const ble_gap_sec_params_t *peerParams = &p_event->event_param.p_gap_param->params.sec_params_request.peer_params; |
LancasterUniversity | 616:a8f9b022d8fd | 219 | securityManager.processSecuritySetupInitiatedEvent(p_event->event_param.p_gap_param->conn_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 220 | peerParams->bond, |
LancasterUniversity | 615:65ea2acfc6a2 | 221 | peerParams->mitm, |
LancasterUniversity | 615:65ea2acfc6a2 | 222 | (SecurityManager::SecurityIOCapabilities_t)peerParams->io_caps); |
LancasterUniversity | 615:65ea2acfc6a2 | 223 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 224 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 225 | case DM_EVT_SECURITY_SETUP_COMPLETE: |
LancasterUniversity | 616:a8f9b022d8fd | 226 | securityManager. |
LancasterUniversity | 615:65ea2acfc6a2 | 227 | processSecuritySetupCompletedEvent(p_event->event_param.p_gap_param->conn_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 228 | (SecurityManager::SecurityCompletionStatus_t)(p_event->event_param.p_gap_param->params.auth_status.auth_status)); |
LancasterUniversity | 615:65ea2acfc6a2 | 229 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 230 | case DM_EVT_LINK_SECURED: { |
LancasterUniversity | 615:65ea2acfc6a2 | 231 | unsigned securityMode = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.sm; |
LancasterUniversity | 615:65ea2acfc6a2 | 232 | unsigned level = p_event->event_param.p_gap_param->params.conn_sec_update.conn_sec.sec_mode.lv; |
LancasterUniversity | 615:65ea2acfc6a2 | 233 | SecurityManager::SecurityMode_t resolvedSecurityMode = SecurityManager::SECURITY_MODE_NO_ACCESS; |
LancasterUniversity | 615:65ea2acfc6a2 | 234 | switch (securityMode) { |
LancasterUniversity | 615:65ea2acfc6a2 | 235 | case 1: |
LancasterUniversity | 615:65ea2acfc6a2 | 236 | switch (level) { |
LancasterUniversity | 615:65ea2acfc6a2 | 237 | case 1: |
LancasterUniversity | 615:65ea2acfc6a2 | 238 | resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK; |
LancasterUniversity | 615:65ea2acfc6a2 | 239 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 240 | case 2: |
LancasterUniversity | 615:65ea2acfc6a2 | 241 | resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM; |
LancasterUniversity | 615:65ea2acfc6a2 | 242 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 243 | case 3: |
LancasterUniversity | 615:65ea2acfc6a2 | 244 | resolvedSecurityMode = SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM; |
LancasterUniversity | 615:65ea2acfc6a2 | 245 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 246 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 247 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 248 | case 2: |
LancasterUniversity | 615:65ea2acfc6a2 | 249 | switch (level) { |
LancasterUniversity | 615:65ea2acfc6a2 | 250 | case 1: |
LancasterUniversity | 615:65ea2acfc6a2 | 251 | resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_NO_MITM; |
LancasterUniversity | 615:65ea2acfc6a2 | 252 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 253 | case 2: |
LancasterUniversity | 615:65ea2acfc6a2 | 254 | resolvedSecurityMode = SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM; |
LancasterUniversity | 615:65ea2acfc6a2 | 255 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 256 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 257 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 258 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 259 | |
LancasterUniversity | 616:a8f9b022d8fd | 260 | securityManager.processLinkSecuredEvent(p_event->event_param.p_gap_param->conn_handle, resolvedSecurityMode); |
LancasterUniversity | 615:65ea2acfc6a2 | 261 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 262 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 263 | case DM_EVT_DEVICE_CONTEXT_STORED: |
LancasterUniversity | 616:a8f9b022d8fd | 264 | securityManager.processSecurityContextStoredEvent(p_event->event_param.p_gap_param->conn_handle); |
LancasterUniversity | 615:65ea2acfc6a2 | 265 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 266 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 267 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 268 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 269 | |
LancasterUniversity | 615:65ea2acfc6a2 | 270 | return NRF_SUCCESS; |
LancasterUniversity | 616:a8f9b022d8fd | 271 | } |
LancasterUniversity | 616:a8f9b022d8fd | 272 | |
LancasterUniversity | 616:a8f9b022d8fd | 273 | ble_error_t |
LancasterUniversity | 616:a8f9b022d8fd | 274 | btle_createWhitelistFromBondTable(ble_gap_whitelist_t *p_whitelist) |
LancasterUniversity | 616:a8f9b022d8fd | 275 | { |
LancasterUniversity | 616:a8f9b022d8fd | 276 | if (!btle_hasInitializedSecurity()) { |
LancasterUniversity | 616:a8f9b022d8fd | 277 | return BLE_ERROR_INITIALIZATION_INCOMPLETE; |
LancasterUniversity | 616:a8f9b022d8fd | 278 | } |
LancasterUniversity | 616:a8f9b022d8fd | 279 | ret_code_t err = dm_whitelist_create(&applicationInstance, p_whitelist); |
LancasterUniversity | 616:a8f9b022d8fd | 280 | if (err == NRF_SUCCESS) { |
LancasterUniversity | 616:a8f9b022d8fd | 281 | return BLE_ERROR_NONE; |
LancasterUniversity | 616:a8f9b022d8fd | 282 | } else if (err == NRF_ERROR_NULL) { |
LancasterUniversity | 616:a8f9b022d8fd | 283 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
LancasterUniversity | 616:a8f9b022d8fd | 284 | } else { |
LancasterUniversity | 616:a8f9b022d8fd | 285 | return BLE_ERROR_INVALID_STATE; |
LancasterUniversity | 616:a8f9b022d8fd | 286 | } |
LancasterUniversity | 616:a8f9b022d8fd | 287 | } |
LancasterUniversity | 616:a8f9b022d8fd | 288 | |
LancasterUniversity | 616:a8f9b022d8fd | 289 | |
LancasterUniversity | 616:a8f9b022d8fd | 290 | bool |
LancasterUniversity | 616:a8f9b022d8fd | 291 | btle_matchAddressAndIrk(ble_gap_addr_t const * p_addr, ble_gap_irk_t const * p_irk) |
LancasterUniversity | 616:a8f9b022d8fd | 292 | { |
LancasterUniversity | 616:a8f9b022d8fd | 293 | /* |
LancasterUniversity | 616:a8f9b022d8fd | 294 | * Use a helper function from the Nordic SDK to test whether the BLE |
LancasterUniversity | 616:a8f9b022d8fd | 295 | * address can be generated using the IRK. |
LancasterUniversity | 616:a8f9b022d8fd | 296 | */ |
LancasterUniversity | 616:a8f9b022d8fd | 297 | return im_address_resolve(p_addr, p_irk); |
LancasterUniversity | 616:a8f9b022d8fd | 298 | } |
LancasterUniversity | 616:a8f9b022d8fd | 299 | |
LancasterUniversity | 616:a8f9b022d8fd | 300 | void |
LancasterUniversity | 616:a8f9b022d8fd | 301 | btle_generateResolvableAddress(const ble_gap_irk_t &irk, ble_gap_addr_t &address) |
LancasterUniversity | 616:a8f9b022d8fd | 302 | { |
LancasterUniversity | 616:a8f9b022d8fd | 303 | /* Set type to resolvable */ |
LancasterUniversity | 616:a8f9b022d8fd | 304 | address.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE; |
LancasterUniversity | 616:a8f9b022d8fd | 305 | |
LancasterUniversity | 616:a8f9b022d8fd | 306 | /* |
LancasterUniversity | 616:a8f9b022d8fd | 307 | * Assign a random number to the most significant 3 bytes |
LancasterUniversity | 616:a8f9b022d8fd | 308 | * of the address. |
LancasterUniversity | 616:a8f9b022d8fd | 309 | */ |
LancasterUniversity | 616:a8f9b022d8fd | 310 | address.addr[BLE_GAP_ADDR_LEN - 3] = 0x8E; |
LancasterUniversity | 616:a8f9b022d8fd | 311 | address.addr[BLE_GAP_ADDR_LEN - 2] = 0x4F; |
LancasterUniversity | 616:a8f9b022d8fd | 312 | address.addr[BLE_GAP_ADDR_LEN - 1] = 0x7C; |
LancasterUniversity | 616:a8f9b022d8fd | 313 | |
LancasterUniversity | 616:a8f9b022d8fd | 314 | /* Calculate the hash and store it in the top half of the address */ |
LancasterUniversity | 616:a8f9b022d8fd | 315 | ah(irk.irk, &address.addr[BLE_GAP_ADDR_LEN - 3], address.addr); |
rgrover1 | 133:74079098b3c9 | 316 | } |