hr with 30102
Dependencies: BLE_API mbed X_NUCLEO_IDB0XA1
Fork of BLE_HeartRate by
nRF51822/TARGET_NRF5/source/nRF5xSecurityManager.h@80:808f6f367f45, 2017-07-30 (annotated)
- Committer:
- mssarwar
- Date:
- Sun Jul 30 05:52:57 2017 +0000
- Revision:
- 80:808f6f367f45
with 30102;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mssarwar | 80:808f6f367f45 | 1 | /* mbed Microcontroller Library |
mssarwar | 80:808f6f367f45 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mssarwar | 80:808f6f367f45 | 3 | * |
mssarwar | 80:808f6f367f45 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mssarwar | 80:808f6f367f45 | 5 | * you may not use this file except in compliance with the License. |
mssarwar | 80:808f6f367f45 | 6 | * You may obtain a copy of the License at |
mssarwar | 80:808f6f367f45 | 7 | * |
mssarwar | 80:808f6f367f45 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mssarwar | 80:808f6f367f45 | 9 | * |
mssarwar | 80:808f6f367f45 | 10 | * Unless required by applicable law or agreed to in writing, software |
mssarwar | 80:808f6f367f45 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mssarwar | 80:808f6f367f45 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mssarwar | 80:808f6f367f45 | 13 | * See the License for the specific language governing permissions and |
mssarwar | 80:808f6f367f45 | 14 | * limitations under the License. |
mssarwar | 80:808f6f367f45 | 15 | */ |
mssarwar | 80:808f6f367f45 | 16 | |
mssarwar | 80:808f6f367f45 | 17 | #ifndef __NRF51822_SECURITY_MANAGER_H__ |
mssarwar | 80:808f6f367f45 | 18 | #define __NRF51822_SECURITY_MANAGER_H__ |
mssarwar | 80:808f6f367f45 | 19 | |
mssarwar | 80:808f6f367f45 | 20 | #include <stddef.h> |
mssarwar | 80:808f6f367f45 | 21 | |
mssarwar | 80:808f6f367f45 | 22 | #include "nRF5xGap.h" |
mssarwar | 80:808f6f367f45 | 23 | #include "ble/SecurityManager.h" |
mssarwar | 80:808f6f367f45 | 24 | #include "btle_security.h" |
mssarwar | 80:808f6f367f45 | 25 | |
mssarwar | 80:808f6f367f45 | 26 | class nRF5xSecurityManager : public SecurityManager |
mssarwar | 80:808f6f367f45 | 27 | { |
mssarwar | 80:808f6f367f45 | 28 | public: |
mssarwar | 80:808f6f367f45 | 29 | /* Functions that must be implemented from SecurityManager */ |
mssarwar | 80:808f6f367f45 | 30 | virtual ble_error_t init(bool enableBonding, |
mssarwar | 80:808f6f367f45 | 31 | bool requireMITM, |
mssarwar | 80:808f6f367f45 | 32 | SecurityIOCapabilities_t iocaps, |
mssarwar | 80:808f6f367f45 | 33 | const Passkey_t passkey) { |
mssarwar | 80:808f6f367f45 | 34 | return btle_initializeSecurity(enableBonding, requireMITM, iocaps, passkey); |
mssarwar | 80:808f6f367f45 | 35 | } |
mssarwar | 80:808f6f367f45 | 36 | |
mssarwar | 80:808f6f367f45 | 37 | virtual ble_error_t getLinkSecurity(Gap::Handle_t connectionHandle, LinkSecurityStatus_t *securityStatusP) { |
mssarwar | 80:808f6f367f45 | 38 | return btle_getLinkSecurity(connectionHandle, securityStatusP); |
mssarwar | 80:808f6f367f45 | 39 | } |
mssarwar | 80:808f6f367f45 | 40 | |
mssarwar | 80:808f6f367f45 | 41 | virtual ble_error_t setLinkSecurity(Gap::Handle_t connectionHandle, SecurityMode_t securityMode) { |
mssarwar | 80:808f6f367f45 | 42 | return btle_setLinkSecurity(connectionHandle, securityMode); |
mssarwar | 80:808f6f367f45 | 43 | } |
mssarwar | 80:808f6f367f45 | 44 | |
mssarwar | 80:808f6f367f45 | 45 | virtual ble_error_t purgeAllBondingState(void) { |
mssarwar | 80:808f6f367f45 | 46 | return btle_purgeAllBondingState(); |
mssarwar | 80:808f6f367f45 | 47 | } |
mssarwar | 80:808f6f367f45 | 48 | |
mssarwar | 80:808f6f367f45 | 49 | /** |
mssarwar | 80:808f6f367f45 | 50 | * @brief Returns a list of addresses from peers in the stacks bond table. |
mssarwar | 80:808f6f367f45 | 51 | * |
mssarwar | 80:808f6f367f45 | 52 | * @param[in/out] addresses |
mssarwar | 80:808f6f367f45 | 53 | * (on input) @ref Gap::Whitelist_t structure where at |
mssarwar | 80:808f6f367f45 | 54 | * most addresses.capacity addresses from bonded peers will |
mssarwar | 80:808f6f367f45 | 55 | * be stored. |
mssarwar | 80:808f6f367f45 | 56 | * (on output) A copy of the addresses from bonded peers. |
mssarwar | 80:808f6f367f45 | 57 | * |
mssarwar | 80:808f6f367f45 | 58 | * @return |
mssarwar | 80:808f6f367f45 | 59 | * BLE_ERROR_NONE if successful. |
mssarwar | 80:808f6f367f45 | 60 | */ |
mssarwar | 80:808f6f367f45 | 61 | virtual ble_error_t getAddressesFromBondTable(Gap::Whitelist_t &addresses) const { |
mssarwar | 80:808f6f367f45 | 62 | uint8_t i; |
mssarwar | 80:808f6f367f45 | 63 | |
mssarwar | 80:808f6f367f45 | 64 | ble_gap_whitelist_t whitelistFromBondTable; |
mssarwar | 80:808f6f367f45 | 65 | ble_gap_addr_t *addressPtr[YOTTA_CFG_WHITELIST_MAX_SIZE]; |
mssarwar | 80:808f6f367f45 | 66 | ble_gap_irk_t *irkPtr[YOTTA_CFG_IRK_TABLE_MAX_SIZE]; |
mssarwar | 80:808f6f367f45 | 67 | |
mssarwar | 80:808f6f367f45 | 68 | /* Initialize the structure so that we get as many addreses as the whitelist can hold */ |
mssarwar | 80:808f6f367f45 | 69 | whitelistFromBondTable.addr_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE; |
mssarwar | 80:808f6f367f45 | 70 | whitelistFromBondTable.pp_addrs = addressPtr; |
mssarwar | 80:808f6f367f45 | 71 | whitelistFromBondTable.irk_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE; |
mssarwar | 80:808f6f367f45 | 72 | whitelistFromBondTable.pp_irks = irkPtr; |
mssarwar | 80:808f6f367f45 | 73 | |
mssarwar | 80:808f6f367f45 | 74 | ble_error_t error = createWhitelistFromBondTable(whitelistFromBondTable); |
mssarwar | 80:808f6f367f45 | 75 | if (error != BLE_ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 76 | addresses.size = 0; |
mssarwar | 80:808f6f367f45 | 77 | return error; |
mssarwar | 80:808f6f367f45 | 78 | } |
mssarwar | 80:808f6f367f45 | 79 | |
mssarwar | 80:808f6f367f45 | 80 | /* Put all the addresses in the structure */ |
mssarwar | 80:808f6f367f45 | 81 | for (i = 0; i < whitelistFromBondTable.addr_count; ++i) { |
mssarwar | 80:808f6f367f45 | 82 | if (i >= addresses.capacity) { |
mssarwar | 80:808f6f367f45 | 83 | /* Ran out of space in the output Gap::Whitelist_t */ |
mssarwar | 80:808f6f367f45 | 84 | addresses.size = i; |
mssarwar | 80:808f6f367f45 | 85 | return BLE_ERROR_NONE; |
mssarwar | 80:808f6f367f45 | 86 | } |
mssarwar | 80:808f6f367f45 | 87 | memcpy(&addresses.addresses[i], whitelistFromBondTable.pp_addrs[i], sizeof(BLEProtocol::Address_t)); |
mssarwar | 80:808f6f367f45 | 88 | } |
mssarwar | 80:808f6f367f45 | 89 | |
mssarwar | 80:808f6f367f45 | 90 | /* Update the current address count */ |
mssarwar | 80:808f6f367f45 | 91 | addresses.size = i; |
mssarwar | 80:808f6f367f45 | 92 | |
mssarwar | 80:808f6f367f45 | 93 | /* The assumption here is that the underlying implementation of |
mssarwar | 80:808f6f367f45 | 94 | * createWhitelistFromBondTable() will not return the private resolvable |
mssarwar | 80:808f6f367f45 | 95 | * addresses (which is the case in the SoftDevice). Rather it returns the |
mssarwar | 80:808f6f367f45 | 96 | * IRKs, so we need to generate the private resolvable address by ourselves. |
mssarwar | 80:808f6f367f45 | 97 | */ |
mssarwar | 80:808f6f367f45 | 98 | for (i = 0; i < whitelistFromBondTable.irk_count; ++i) { |
mssarwar | 80:808f6f367f45 | 99 | if (i + addresses.size >= addresses.capacity) { |
mssarwar | 80:808f6f367f45 | 100 | /* Ran out of space in the output Gap::Whitelist_t */ |
mssarwar | 80:808f6f367f45 | 101 | addresses.size += i; |
mssarwar | 80:808f6f367f45 | 102 | return BLE_ERROR_NONE; |
mssarwar | 80:808f6f367f45 | 103 | } |
mssarwar | 80:808f6f367f45 | 104 | btle_generateResolvableAddress( |
mssarwar | 80:808f6f367f45 | 105 | *whitelistFromBondTable.pp_irks[i], |
mssarwar | 80:808f6f367f45 | 106 | (ble_gap_addr_t &) addresses.addresses[i + addresses.size] |
mssarwar | 80:808f6f367f45 | 107 | ); |
mssarwar | 80:808f6f367f45 | 108 | } |
mssarwar | 80:808f6f367f45 | 109 | |
mssarwar | 80:808f6f367f45 | 110 | /* Update the current address count */ |
mssarwar | 80:808f6f367f45 | 111 | addresses.size += i; |
mssarwar | 80:808f6f367f45 | 112 | |
mssarwar | 80:808f6f367f45 | 113 | return BLE_ERROR_NONE; |
mssarwar | 80:808f6f367f45 | 114 | } |
mssarwar | 80:808f6f367f45 | 115 | |
mssarwar | 80:808f6f367f45 | 116 | /** |
mssarwar | 80:808f6f367f45 | 117 | * @brief Clear nRF5xSecurityManager's state. |
mssarwar | 80:808f6f367f45 | 118 | * |
mssarwar | 80:808f6f367f45 | 119 | * @return |
mssarwar | 80:808f6f367f45 | 120 | * BLE_ERROR_NONE if successful. |
mssarwar | 80:808f6f367f45 | 121 | */ |
mssarwar | 80:808f6f367f45 | 122 | virtual ble_error_t reset(void) |
mssarwar | 80:808f6f367f45 | 123 | { |
mssarwar | 80:808f6f367f45 | 124 | if (SecurityManager::reset() != BLE_ERROR_NONE) { |
mssarwar | 80:808f6f367f45 | 125 | return BLE_ERROR_INVALID_STATE; |
mssarwar | 80:808f6f367f45 | 126 | } |
mssarwar | 80:808f6f367f45 | 127 | |
mssarwar | 80:808f6f367f45 | 128 | return BLE_ERROR_NONE; |
mssarwar | 80:808f6f367f45 | 129 | } |
mssarwar | 80:808f6f367f45 | 130 | |
mssarwar | 80:808f6f367f45 | 131 | bool hasInitialized(void) const { |
mssarwar | 80:808f6f367f45 | 132 | return btle_hasInitializedSecurity(); |
mssarwar | 80:808f6f367f45 | 133 | } |
mssarwar | 80:808f6f367f45 | 134 | |
mssarwar | 80:808f6f367f45 | 135 | public: |
mssarwar | 80:808f6f367f45 | 136 | /* |
mssarwar | 80:808f6f367f45 | 137 | * Allow instantiation from nRF5xn when required. |
mssarwar | 80:808f6f367f45 | 138 | */ |
mssarwar | 80:808f6f367f45 | 139 | friend class nRF5xn; |
mssarwar | 80:808f6f367f45 | 140 | |
mssarwar | 80:808f6f367f45 | 141 | nRF5xSecurityManager() { |
mssarwar | 80:808f6f367f45 | 142 | /* empty */ |
mssarwar | 80:808f6f367f45 | 143 | } |
mssarwar | 80:808f6f367f45 | 144 | |
mssarwar | 80:808f6f367f45 | 145 | private: |
mssarwar | 80:808f6f367f45 | 146 | nRF5xSecurityManager(const nRF5xSecurityManager &); |
mssarwar | 80:808f6f367f45 | 147 | const nRF5xSecurityManager& operator=(const nRF5xSecurityManager &); |
mssarwar | 80:808f6f367f45 | 148 | |
mssarwar | 80:808f6f367f45 | 149 | /* |
mssarwar | 80:808f6f367f45 | 150 | * Expose an interface that allows us to query the SoftDevice bond table |
mssarwar | 80:808f6f367f45 | 151 | * and extract a whitelist. |
mssarwar | 80:808f6f367f45 | 152 | */ |
mssarwar | 80:808f6f367f45 | 153 | ble_error_t createWhitelistFromBondTable(ble_gap_whitelist_t &whitelistFromBondTable) const { |
mssarwar | 80:808f6f367f45 | 154 | return btle_createWhitelistFromBondTable(&whitelistFromBondTable); |
mssarwar | 80:808f6f367f45 | 155 | } |
mssarwar | 80:808f6f367f45 | 156 | |
mssarwar | 80:808f6f367f45 | 157 | /* |
mssarwar | 80:808f6f367f45 | 158 | * Given a BLE address and a IRK this function check whether the address |
mssarwar | 80:808f6f367f45 | 159 | * can be generated from the IRK. To do so, this function uses the hash |
mssarwar | 80:808f6f367f45 | 160 | * function and algorithm described in the Bluetooth low Energy |
mssarwar | 80:808f6f367f45 | 161 | * Specification. Internally, Nordic SDK functions are used. |
mssarwar | 80:808f6f367f45 | 162 | */ |
mssarwar | 80:808f6f367f45 | 163 | bool matchAddressAndIrk(ble_gap_addr_t *address, ble_gap_irk_t *irk) const { |
mssarwar | 80:808f6f367f45 | 164 | return btle_matchAddressAndIrk(address, irk); |
mssarwar | 80:808f6f367f45 | 165 | } |
mssarwar | 80:808f6f367f45 | 166 | |
mssarwar | 80:808f6f367f45 | 167 | /* |
mssarwar | 80:808f6f367f45 | 168 | * Give nRF5xGap access to createWhitelistFromBondTable() and |
mssarwar | 80:808f6f367f45 | 169 | * matchAddressAndIrk() |
mssarwar | 80:808f6f367f45 | 170 | */ |
mssarwar | 80:808f6f367f45 | 171 | friend class nRF5xGap; |
mssarwar | 80:808f6f367f45 | 172 | }; |
mssarwar | 80:808f6f367f45 | 173 | |
mssarwar | 80:808f6f367f45 | 174 | #endif // ifndef __NRF51822_SECURITY_MANAGER_H__ |