library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:35 2016 +0000
Revision:
598:814c1ce92947
Parent:
597:bcd5e287c494
Child:
599:3e66e1eb264d
Synchronized with git rev c9b6bb9b
Author: Andres Amaya Garcia
Almost complete implementation of whitelisting API

Who changed what in which revision?

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