Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/BLE_API/ble/SecurityManager.h@0:b9164b348919, 2016-10-17 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 17 12:41:20 2016 +0000
- Revision:
- 0:b9164b348919
Official Sheffield ARMBand Micro:bit program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 0:b9164b348919 | 1 | /* mbed Microcontroller Library |
MrBedfordVan | 0:b9164b348919 | 2 | * Copyright (c) 2006-2015 ARM Limited |
MrBedfordVan | 0:b9164b348919 | 3 | * |
MrBedfordVan | 0:b9164b348919 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
MrBedfordVan | 0:b9164b348919 | 5 | * you may not use this file except in compliance with the License. |
MrBedfordVan | 0:b9164b348919 | 6 | * You may obtain a copy of the License at |
MrBedfordVan | 0:b9164b348919 | 7 | * |
MrBedfordVan | 0:b9164b348919 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
MrBedfordVan | 0:b9164b348919 | 9 | * |
MrBedfordVan | 0:b9164b348919 | 10 | * Unless required by applicable law or agreed to in writing, software |
MrBedfordVan | 0:b9164b348919 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
MrBedfordVan | 0:b9164b348919 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MrBedfordVan | 0:b9164b348919 | 13 | * See the License for the specific language governing permissions and |
MrBedfordVan | 0:b9164b348919 | 14 | * limitations under the License. |
MrBedfordVan | 0:b9164b348919 | 15 | */ |
MrBedfordVan | 0:b9164b348919 | 16 | |
MrBedfordVan | 0:b9164b348919 | 17 | #ifndef __SECURITY_MANAGER_H__ |
MrBedfordVan | 0:b9164b348919 | 18 | #define __SECURITY_MANAGER_H__ |
MrBedfordVan | 0:b9164b348919 | 19 | |
MrBedfordVan | 0:b9164b348919 | 20 | #include <stdint.h> |
MrBedfordVan | 0:b9164b348919 | 21 | |
MrBedfordVan | 0:b9164b348919 | 22 | #include "Gap.h" |
MrBedfordVan | 0:b9164b348919 | 23 | #include "CallChainOfFunctionPointersWithContext.h" |
MrBedfordVan | 0:b9164b348919 | 24 | |
MrBedfordVan | 0:b9164b348919 | 25 | class SecurityManager { |
MrBedfordVan | 0:b9164b348919 | 26 | public: |
MrBedfordVan | 0:b9164b348919 | 27 | enum SecurityMode_t { |
MrBedfordVan | 0:b9164b348919 | 28 | SECURITY_MODE_NO_ACCESS, |
MrBedfordVan | 0:b9164b348919 | 29 | SECURITY_MODE_ENCRYPTION_OPEN_LINK, /**< Require no protection, open link. */ |
MrBedfordVan | 0:b9164b348919 | 30 | SECURITY_MODE_ENCRYPTION_NO_MITM, /**< Require encryption, but no MITM protection. */ |
MrBedfordVan | 0:b9164b348919 | 31 | SECURITY_MODE_ENCRYPTION_WITH_MITM, /**< Require encryption and MITM protection. */ |
MrBedfordVan | 0:b9164b348919 | 32 | SECURITY_MODE_SIGNED_NO_MITM, /**< Require signing or encryption, but no MITM protection. */ |
MrBedfordVan | 0:b9164b348919 | 33 | SECURITY_MODE_SIGNED_WITH_MITM, /**< Require signing or encryption, and MITM protection. */ |
MrBedfordVan | 0:b9164b348919 | 34 | }; |
MrBedfordVan | 0:b9164b348919 | 35 | |
MrBedfordVan | 0:b9164b348919 | 36 | /** |
MrBedfordVan | 0:b9164b348919 | 37 | * @brief Defines possible security status or states. |
MrBedfordVan | 0:b9164b348919 | 38 | * |
MrBedfordVan | 0:b9164b348919 | 39 | * @details Defines possible security status or states of a link when requested by getLinkSecurity(). |
MrBedfordVan | 0:b9164b348919 | 40 | */ |
MrBedfordVan | 0:b9164b348919 | 41 | enum LinkSecurityStatus_t { |
MrBedfordVan | 0:b9164b348919 | 42 | NOT_ENCRYPTED, /**< The link is not secured. */ |
MrBedfordVan | 0:b9164b348919 | 43 | ENCRYPTION_IN_PROGRESS, /**< Link security is being established.*/ |
MrBedfordVan | 0:b9164b348919 | 44 | ENCRYPTED /**< The link is secure.*/ |
MrBedfordVan | 0:b9164b348919 | 45 | }; |
MrBedfordVan | 0:b9164b348919 | 46 | |
MrBedfordVan | 0:b9164b348919 | 47 | enum SecurityIOCapabilities_t { |
MrBedfordVan | 0:b9164b348919 | 48 | IO_CAPS_DISPLAY_ONLY = 0x00, /**< Display only. */ |
MrBedfordVan | 0:b9164b348919 | 49 | IO_CAPS_DISPLAY_YESNO = 0x01, /**< Display and yes/no entry. */ |
MrBedfordVan | 0:b9164b348919 | 50 | IO_CAPS_KEYBOARD_ONLY = 0x02, /**< Keyboard only. */ |
MrBedfordVan | 0:b9164b348919 | 51 | IO_CAPS_NONE = 0x03, /**< No I/O capabilities. */ |
MrBedfordVan | 0:b9164b348919 | 52 | IO_CAPS_KEYBOARD_DISPLAY = 0x04, /**< Keyboard and display. */ |
MrBedfordVan | 0:b9164b348919 | 53 | }; |
MrBedfordVan | 0:b9164b348919 | 54 | |
MrBedfordVan | 0:b9164b348919 | 55 | enum SecurityCompletionStatus_t { |
MrBedfordVan | 0:b9164b348919 | 56 | SEC_STATUS_SUCCESS = 0x00, /**< Procedure completed with success. */ |
MrBedfordVan | 0:b9164b348919 | 57 | SEC_STATUS_TIMEOUT = 0x01, /**< Procedure timed out. */ |
MrBedfordVan | 0:b9164b348919 | 58 | SEC_STATUS_PDU_INVALID = 0x02, /**< Invalid PDU received. */ |
MrBedfordVan | 0:b9164b348919 | 59 | SEC_STATUS_PASSKEY_ENTRY_FAILED = 0x81, /**< Passkey entry failed (user canceled or other). */ |
MrBedfordVan | 0:b9164b348919 | 60 | SEC_STATUS_OOB_NOT_AVAILABLE = 0x82, /**< Out of Band Key not available. */ |
MrBedfordVan | 0:b9164b348919 | 61 | SEC_STATUS_AUTH_REQ = 0x83, /**< Authentication requirements not met. */ |
MrBedfordVan | 0:b9164b348919 | 62 | SEC_STATUS_CONFIRM_VALUE = 0x84, /**< Confirm value failed. */ |
MrBedfordVan | 0:b9164b348919 | 63 | SEC_STATUS_PAIRING_NOT_SUPP = 0x85, /**< Pairing not supported. */ |
MrBedfordVan | 0:b9164b348919 | 64 | SEC_STATUS_ENC_KEY_SIZE = 0x86, /**< Encryption key size. */ |
MrBedfordVan | 0:b9164b348919 | 65 | SEC_STATUS_SMP_CMD_UNSUPPORTED = 0x87, /**< Unsupported SMP command. */ |
MrBedfordVan | 0:b9164b348919 | 66 | SEC_STATUS_UNSPECIFIED = 0x88, /**< Unspecified reason. */ |
MrBedfordVan | 0:b9164b348919 | 67 | SEC_STATUS_REPEATED_ATTEMPTS = 0x89, /**< Too little time elapsed since last attempt. */ |
MrBedfordVan | 0:b9164b348919 | 68 | SEC_STATUS_INVALID_PARAMS = 0x8A, /**< Invalid parameters. */ |
MrBedfordVan | 0:b9164b348919 | 69 | }; |
MrBedfordVan | 0:b9164b348919 | 70 | |
MrBedfordVan | 0:b9164b348919 | 71 | /** |
MrBedfordVan | 0:b9164b348919 | 72 | * Declaration of type containing a passkey to be used during pairing. This |
MrBedfordVan | 0:b9164b348919 | 73 | * is passed into initializeSecurity() to specify a pre-programmed passkey |
MrBedfordVan | 0:b9164b348919 | 74 | * for authentication instead of generating a random one. |
MrBedfordVan | 0:b9164b348919 | 75 | */ |
MrBedfordVan | 0:b9164b348919 | 76 | static const unsigned PASSKEY_LEN = 6; |
MrBedfordVan | 0:b9164b348919 | 77 | typedef uint8_t Passkey_t[PASSKEY_LEN]; /**< 6-digit passkey in ASCII ('0'-'9' digits only). */ |
MrBedfordVan | 0:b9164b348919 | 78 | |
MrBedfordVan | 0:b9164b348919 | 79 | public: |
MrBedfordVan | 0:b9164b348919 | 80 | typedef void (*HandleSpecificEvent_t)(Gap::Handle_t handle); |
MrBedfordVan | 0:b9164b348919 | 81 | typedef void (*SecuritySetupInitiatedCallback_t)(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityIOCapabilities_t iocaps); |
MrBedfordVan | 0:b9164b348919 | 82 | typedef void (*SecuritySetupCompletedCallback_t)(Gap::Handle_t, SecurityCompletionStatus_t status); |
MrBedfordVan | 0:b9164b348919 | 83 | typedef void (*LinkSecuredCallback_t)(Gap::Handle_t handle, SecurityMode_t securityMode); |
MrBedfordVan | 0:b9164b348919 | 84 | typedef void (*PasskeyDisplayCallback_t)(Gap::Handle_t handle, const Passkey_t passkey); |
MrBedfordVan | 0:b9164b348919 | 85 | |
MrBedfordVan | 0:b9164b348919 | 86 | typedef FunctionPointerWithContext<const SecurityManager *> SecurityManagerShutdownCallback_t; |
MrBedfordVan | 0:b9164b348919 | 87 | typedef CallChainOfFunctionPointersWithContext<const SecurityManager *> SecurityManagerShutdownCallbackChain_t; |
MrBedfordVan | 0:b9164b348919 | 88 | |
MrBedfordVan | 0:b9164b348919 | 89 | /* |
MrBedfordVan | 0:b9164b348919 | 90 | * The following functions are meant to be overridden in the platform-specific sub-class. |
MrBedfordVan | 0:b9164b348919 | 91 | */ |
MrBedfordVan | 0:b9164b348919 | 92 | public: |
MrBedfordVan | 0:b9164b348919 | 93 | /** |
MrBedfordVan | 0:b9164b348919 | 94 | * Enable the BLE stack's Security Manager. The Security Manager implements |
MrBedfordVan | 0:b9164b348919 | 95 | * the actual cryptographic algorithms and protocol exchanges that allow two |
MrBedfordVan | 0:b9164b348919 | 96 | * devices to securely exchange data and privately detect each other. |
MrBedfordVan | 0:b9164b348919 | 97 | * Calling this API is a prerequisite for encryption and pairing (bonding). |
MrBedfordVan | 0:b9164b348919 | 98 | * |
MrBedfordVan | 0:b9164b348919 | 99 | * @param[in] enableBonding Allow for bonding. |
MrBedfordVan | 0:b9164b348919 | 100 | * @param[in] requireMITM Require protection for man-in-the-middle attacks. |
MrBedfordVan | 0:b9164b348919 | 101 | * @param[in] iocaps To specify the I/O capabilities of this peripheral, |
MrBedfordVan | 0:b9164b348919 | 102 | * such as availability of a display or keyboard, to |
MrBedfordVan | 0:b9164b348919 | 103 | * support out-of-band exchanges of security data. |
MrBedfordVan | 0:b9164b348919 | 104 | * @param[in] passkey To specify a static passkey. |
MrBedfordVan | 0:b9164b348919 | 105 | * |
MrBedfordVan | 0:b9164b348919 | 106 | * @return BLE_ERROR_NONE on success. |
MrBedfordVan | 0:b9164b348919 | 107 | */ |
MrBedfordVan | 0:b9164b348919 | 108 | virtual ble_error_t init(bool enableBonding = true, |
MrBedfordVan | 0:b9164b348919 | 109 | bool requireMITM = true, |
MrBedfordVan | 0:b9164b348919 | 110 | SecurityIOCapabilities_t iocaps = IO_CAPS_NONE, |
MrBedfordVan | 0:b9164b348919 | 111 | const Passkey_t passkey = NULL) { |
MrBedfordVan | 0:b9164b348919 | 112 | /* Avoid compiler warnings about unused variables. */ |
MrBedfordVan | 0:b9164b348919 | 113 | (void)enableBonding; |
MrBedfordVan | 0:b9164b348919 | 114 | (void)requireMITM; |
MrBedfordVan | 0:b9164b348919 | 115 | (void)iocaps; |
MrBedfordVan | 0:b9164b348919 | 116 | (void)passkey; |
MrBedfordVan | 0:b9164b348919 | 117 | |
MrBedfordVan | 0:b9164b348919 | 118 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */ |
MrBedfordVan | 0:b9164b348919 | 119 | } |
MrBedfordVan | 0:b9164b348919 | 120 | |
MrBedfordVan | 0:b9164b348919 | 121 | /** |
MrBedfordVan | 0:b9164b348919 | 122 | * Get the security status of a connection. |
MrBedfordVan | 0:b9164b348919 | 123 | * |
MrBedfordVan | 0:b9164b348919 | 124 | * @param[in] connectionHandle Handle to identify the connection. |
MrBedfordVan | 0:b9164b348919 | 125 | * @param[out] securityStatusP Security status. |
MrBedfordVan | 0:b9164b348919 | 126 | * |
MrBedfordVan | 0:b9164b348919 | 127 | * @return BLE_ERROR_NONE or appropriate error code indicating the failure reason. |
MrBedfordVan | 0:b9164b348919 | 128 | */ |
MrBedfordVan | 0:b9164b348919 | 129 | virtual ble_error_t getLinkSecurity(Gap::Handle_t connectionHandle, LinkSecurityStatus_t *securityStatusP) { |
MrBedfordVan | 0:b9164b348919 | 130 | /* Avoid compiler warnings about unused variables. */ |
MrBedfordVan | 0:b9164b348919 | 131 | (void)connectionHandle; |
MrBedfordVan | 0:b9164b348919 | 132 | (void)securityStatusP; |
MrBedfordVan | 0:b9164b348919 | 133 | |
MrBedfordVan | 0:b9164b348919 | 134 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */ |
MrBedfordVan | 0:b9164b348919 | 135 | } |
MrBedfordVan | 0:b9164b348919 | 136 | |
MrBedfordVan | 0:b9164b348919 | 137 | /** |
MrBedfordVan | 0:b9164b348919 | 138 | * Set the security mode on a connection. Useful for elevating the security mode |
MrBedfordVan | 0:b9164b348919 | 139 | * once certain conditions are met, e.g., a particular service is found. |
MrBedfordVan | 0:b9164b348919 | 140 | * |
MrBedfordVan | 0:b9164b348919 | 141 | * @param[in] connectionHandle Handle to identify the connection. |
MrBedfordVan | 0:b9164b348919 | 142 | * @param[in] securityMode Requested security mode. |
MrBedfordVan | 0:b9164b348919 | 143 | * |
MrBedfordVan | 0:b9164b348919 | 144 | * @return BLE_ERROR_NONE or appropriate error code indicating the failure reason. |
MrBedfordVan | 0:b9164b348919 | 145 | */ |
MrBedfordVan | 0:b9164b348919 | 146 | virtual ble_error_t setLinkSecurity(Gap::Handle_t connectionHandle, SecurityMode_t securityMode) { |
MrBedfordVan | 0:b9164b348919 | 147 | /* Avoid compiler warnings about unused variables. */ |
MrBedfordVan | 0:b9164b348919 | 148 | (void)connectionHandle; |
MrBedfordVan | 0:b9164b348919 | 149 | (void)securityMode; |
MrBedfordVan | 0:b9164b348919 | 150 | |
MrBedfordVan | 0:b9164b348919 | 151 | return BLE_ERROR_NOT_IMPLEMENTED; |
MrBedfordVan | 0:b9164b348919 | 152 | } |
MrBedfordVan | 0:b9164b348919 | 153 | |
MrBedfordVan | 0:b9164b348919 | 154 | /** |
MrBedfordVan | 0:b9164b348919 | 155 | * Delete all peer device context and all related bonding information from |
MrBedfordVan | 0:b9164b348919 | 156 | * the database within the security manager. |
MrBedfordVan | 0:b9164b348919 | 157 | * |
MrBedfordVan | 0:b9164b348919 | 158 | * @retval BLE_ERROR_NONE On success, else an error code indicating reason for failure. |
MrBedfordVan | 0:b9164b348919 | 159 | * @retval BLE_ERROR_INVALID_STATE If the API is called without module initialization or |
MrBedfordVan | 0:b9164b348919 | 160 | * application registration. |
MrBedfordVan | 0:b9164b348919 | 161 | */ |
MrBedfordVan | 0:b9164b348919 | 162 | virtual ble_error_t purgeAllBondingState(void) { |
MrBedfordVan | 0:b9164b348919 | 163 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */ |
MrBedfordVan | 0:b9164b348919 | 164 | } |
MrBedfordVan | 0:b9164b348919 | 165 | |
MrBedfordVan | 0:b9164b348919 | 166 | /** |
MrBedfordVan | 0:b9164b348919 | 167 | * Get a list of addresses from all peers in the bond table. |
MrBedfordVan | 0:b9164b348919 | 168 | * |
MrBedfordVan | 0:b9164b348919 | 169 | * @param[in/out] addresses |
MrBedfordVan | 0:b9164b348919 | 170 | * (on input) addresses.capacity contains the maximum |
MrBedfordVan | 0:b9164b348919 | 171 | * number of addresses to be returned. |
MrBedfordVan | 0:b9164b348919 | 172 | * (on output) The populated table with copies of the |
MrBedfordVan | 0:b9164b348919 | 173 | * addresses in the implementation's whitelist. |
MrBedfordVan | 0:b9164b348919 | 174 | * |
MrBedfordVan | 0:b9164b348919 | 175 | * @retval BLE_ERROR_NONE On success, else an error code indicating reason for failure. |
MrBedfordVan | 0:b9164b348919 | 176 | * @retval BLE_ERROR_INVALID_STATE If the API is called without module initialization or |
MrBedfordVan | 0:b9164b348919 | 177 | * application registration. |
MrBedfordVan | 0:b9164b348919 | 178 | * |
MrBedfordVan | 0:b9164b348919 | 179 | * @experimental |
MrBedfordVan | 0:b9164b348919 | 180 | */ |
MrBedfordVan | 0:b9164b348919 | 181 | virtual ble_error_t getAddressesFromBondTable(Gap::Whitelist_t &addresses) const { |
MrBedfordVan | 0:b9164b348919 | 182 | /* Avoid compiler warnings about unused variables */ |
MrBedfordVan | 0:b9164b348919 | 183 | (void) addresses; |
MrBedfordVan | 0:b9164b348919 | 184 | |
MrBedfordVan | 0:b9164b348919 | 185 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if security is supported. */ |
MrBedfordVan | 0:b9164b348919 | 186 | } |
MrBedfordVan | 0:b9164b348919 | 187 | |
MrBedfordVan | 0:b9164b348919 | 188 | /* Event callback handlers. */ |
MrBedfordVan | 0:b9164b348919 | 189 | public: |
MrBedfordVan | 0:b9164b348919 | 190 | /** |
MrBedfordVan | 0:b9164b348919 | 191 | * Setup a callback to be invoked to notify the user application that the |
MrBedfordVan | 0:b9164b348919 | 192 | * SecurityManager instance is about to shutdown (possibly as a result of a call |
MrBedfordVan | 0:b9164b348919 | 193 | * to BLE::shutdown()). |
MrBedfordVan | 0:b9164b348919 | 194 | * |
MrBedfordVan | 0:b9164b348919 | 195 | * @Note: It is possible to chain together multiple onShutdown callbacks |
MrBedfordVan | 0:b9164b348919 | 196 | * (potentially from different modules of an application) to be notified |
MrBedfordVan | 0:b9164b348919 | 197 | * before the SecurityManager is shutdown. |
MrBedfordVan | 0:b9164b348919 | 198 | * |
MrBedfordVan | 0:b9164b348919 | 199 | * @Note: It is also possible to set up a callback into a member function of |
MrBedfordVan | 0:b9164b348919 | 200 | * some object. |
MrBedfordVan | 0:b9164b348919 | 201 | * |
MrBedfordVan | 0:b9164b348919 | 202 | * @Note It is possible to unregister a callback using onShutdown().detach(callback) |
MrBedfordVan | 0:b9164b348919 | 203 | */ |
MrBedfordVan | 0:b9164b348919 | 204 | void onShutdown(const SecurityManagerShutdownCallback_t& callback) { |
MrBedfordVan | 0:b9164b348919 | 205 | shutdownCallChain.add(callback); |
MrBedfordVan | 0:b9164b348919 | 206 | } |
MrBedfordVan | 0:b9164b348919 | 207 | template <typename T> |
MrBedfordVan | 0:b9164b348919 | 208 | void onShutdown(T *objPtr, void (T::*memberPtr)(void)) { |
MrBedfordVan | 0:b9164b348919 | 209 | shutdownCallChain.add(objPtr, memberPtr); |
MrBedfordVan | 0:b9164b348919 | 210 | } |
MrBedfordVan | 0:b9164b348919 | 211 | |
MrBedfordVan | 0:b9164b348919 | 212 | /** |
MrBedfordVan | 0:b9164b348919 | 213 | * @brief provide access to the callchain of shutdown event callbacks |
MrBedfordVan | 0:b9164b348919 | 214 | * It is possible to register callbacks using onShutdown().add(callback); |
MrBedfordVan | 0:b9164b348919 | 215 | * It is possible to unregister callbacks using onShutdown().detach(callback) |
MrBedfordVan | 0:b9164b348919 | 216 | * @return The shutdown event callbacks chain |
MrBedfordVan | 0:b9164b348919 | 217 | */ |
MrBedfordVan | 0:b9164b348919 | 218 | SecurityManagerShutdownCallbackChain_t& onShutdown() { |
MrBedfordVan | 0:b9164b348919 | 219 | return shutdownCallChain; |
MrBedfordVan | 0:b9164b348919 | 220 | } |
MrBedfordVan | 0:b9164b348919 | 221 | |
MrBedfordVan | 0:b9164b348919 | 222 | /** |
MrBedfordVan | 0:b9164b348919 | 223 | * To indicate that a security procedure for the link has started. |
MrBedfordVan | 0:b9164b348919 | 224 | */ |
MrBedfordVan | 0:b9164b348919 | 225 | virtual void onSecuritySetupInitiated(SecuritySetupInitiatedCallback_t callback) {securitySetupInitiatedCallback = callback;} |
MrBedfordVan | 0:b9164b348919 | 226 | |
MrBedfordVan | 0:b9164b348919 | 227 | /** |
MrBedfordVan | 0:b9164b348919 | 228 | * To indicate that the security procedure for the link has completed. |
MrBedfordVan | 0:b9164b348919 | 229 | */ |
MrBedfordVan | 0:b9164b348919 | 230 | virtual void onSecuritySetupCompleted(SecuritySetupCompletedCallback_t callback) {securitySetupCompletedCallback = callback;} |
MrBedfordVan | 0:b9164b348919 | 231 | |
MrBedfordVan | 0:b9164b348919 | 232 | /** |
MrBedfordVan | 0:b9164b348919 | 233 | * To indicate that the link with the peer is secured. For bonded devices, |
MrBedfordVan | 0:b9164b348919 | 234 | * subsequent reconnections with a bonded peer will result only in this callback |
MrBedfordVan | 0:b9164b348919 | 235 | * when the link is secured; setup procedures will not occur (unless the |
MrBedfordVan | 0:b9164b348919 | 236 | * bonding information is either lost or deleted on either or both sides). |
MrBedfordVan | 0:b9164b348919 | 237 | */ |
MrBedfordVan | 0:b9164b348919 | 238 | virtual void onLinkSecured(LinkSecuredCallback_t callback) {linkSecuredCallback = callback;} |
MrBedfordVan | 0:b9164b348919 | 239 | |
MrBedfordVan | 0:b9164b348919 | 240 | /** |
MrBedfordVan | 0:b9164b348919 | 241 | * To indicate that device context is stored persistently. |
MrBedfordVan | 0:b9164b348919 | 242 | */ |
MrBedfordVan | 0:b9164b348919 | 243 | virtual void onSecurityContextStored(HandleSpecificEvent_t callback) {securityContextStoredCallback = callback;} |
MrBedfordVan | 0:b9164b348919 | 244 | |
MrBedfordVan | 0:b9164b348919 | 245 | /** |
MrBedfordVan | 0:b9164b348919 | 246 | * To set the callback for when the passkey needs to be displayed on a peripheral with DISPLAY capability. |
MrBedfordVan | 0:b9164b348919 | 247 | */ |
MrBedfordVan | 0:b9164b348919 | 248 | virtual void onPasskeyDisplay(PasskeyDisplayCallback_t callback) {passkeyDisplayCallback = callback;} |
MrBedfordVan | 0:b9164b348919 | 249 | |
MrBedfordVan | 0:b9164b348919 | 250 | /* Entry points for the underlying stack to report events back to the user. */ |
MrBedfordVan | 0:b9164b348919 | 251 | public: |
MrBedfordVan | 0:b9164b348919 | 252 | void processSecuritySetupInitiatedEvent(Gap::Handle_t handle, bool allowBonding, bool requireMITM, SecurityIOCapabilities_t iocaps) { |
MrBedfordVan | 0:b9164b348919 | 253 | if (securitySetupInitiatedCallback) { |
MrBedfordVan | 0:b9164b348919 | 254 | securitySetupInitiatedCallback(handle, allowBonding, requireMITM, iocaps); |
MrBedfordVan | 0:b9164b348919 | 255 | } |
MrBedfordVan | 0:b9164b348919 | 256 | } |
MrBedfordVan | 0:b9164b348919 | 257 | |
MrBedfordVan | 0:b9164b348919 | 258 | void processSecuritySetupCompletedEvent(Gap::Handle_t handle, SecurityCompletionStatus_t status) { |
MrBedfordVan | 0:b9164b348919 | 259 | if (securitySetupCompletedCallback) { |
MrBedfordVan | 0:b9164b348919 | 260 | securitySetupCompletedCallback(handle, status); |
MrBedfordVan | 0:b9164b348919 | 261 | } |
MrBedfordVan | 0:b9164b348919 | 262 | } |
MrBedfordVan | 0:b9164b348919 | 263 | |
MrBedfordVan | 0:b9164b348919 | 264 | void processLinkSecuredEvent(Gap::Handle_t handle, SecurityMode_t securityMode) { |
MrBedfordVan | 0:b9164b348919 | 265 | if (linkSecuredCallback) { |
MrBedfordVan | 0:b9164b348919 | 266 | linkSecuredCallback(handle, securityMode); |
MrBedfordVan | 0:b9164b348919 | 267 | } |
MrBedfordVan | 0:b9164b348919 | 268 | } |
MrBedfordVan | 0:b9164b348919 | 269 | |
MrBedfordVan | 0:b9164b348919 | 270 | void processSecurityContextStoredEvent(Gap::Handle_t handle) { |
MrBedfordVan | 0:b9164b348919 | 271 | if (securityContextStoredCallback) { |
MrBedfordVan | 0:b9164b348919 | 272 | securityContextStoredCallback(handle); |
MrBedfordVan | 0:b9164b348919 | 273 | } |
MrBedfordVan | 0:b9164b348919 | 274 | } |
MrBedfordVan | 0:b9164b348919 | 275 | |
MrBedfordVan | 0:b9164b348919 | 276 | void processPasskeyDisplayEvent(Gap::Handle_t handle, const Passkey_t passkey) { |
MrBedfordVan | 0:b9164b348919 | 277 | if (passkeyDisplayCallback) { |
MrBedfordVan | 0:b9164b348919 | 278 | passkeyDisplayCallback(handle, passkey); |
MrBedfordVan | 0:b9164b348919 | 279 | } |
MrBedfordVan | 0:b9164b348919 | 280 | } |
MrBedfordVan | 0:b9164b348919 | 281 | |
MrBedfordVan | 0:b9164b348919 | 282 | protected: |
MrBedfordVan | 0:b9164b348919 | 283 | SecurityManager() : |
MrBedfordVan | 0:b9164b348919 | 284 | securitySetupInitiatedCallback(), |
MrBedfordVan | 0:b9164b348919 | 285 | securitySetupCompletedCallback(), |
MrBedfordVan | 0:b9164b348919 | 286 | linkSecuredCallback(), |
MrBedfordVan | 0:b9164b348919 | 287 | securityContextStoredCallback(), |
MrBedfordVan | 0:b9164b348919 | 288 | passkeyDisplayCallback() { |
MrBedfordVan | 0:b9164b348919 | 289 | /* empty */ |
MrBedfordVan | 0:b9164b348919 | 290 | } |
MrBedfordVan | 0:b9164b348919 | 291 | |
MrBedfordVan | 0:b9164b348919 | 292 | public: |
MrBedfordVan | 0:b9164b348919 | 293 | /** |
MrBedfordVan | 0:b9164b348919 | 294 | * Notify all registered onShutdown callbacks that the SecurityManager is |
MrBedfordVan | 0:b9164b348919 | 295 | * about to be shutdown and clear all SecurityManager state of the |
MrBedfordVan | 0:b9164b348919 | 296 | * associated object. |
MrBedfordVan | 0:b9164b348919 | 297 | * |
MrBedfordVan | 0:b9164b348919 | 298 | * This function is meant to be overridden in the platform-specific |
MrBedfordVan | 0:b9164b348919 | 299 | * sub-class. Nevertheless, the sub-class is only expected to reset its |
MrBedfordVan | 0:b9164b348919 | 300 | * state and not the data held in SecurityManager members. This shall be |
MrBedfordVan | 0:b9164b348919 | 301 | * achieved by a call to SecurityManager::reset() from the sub-class' |
MrBedfordVan | 0:b9164b348919 | 302 | * reset() implementation. |
MrBedfordVan | 0:b9164b348919 | 303 | * |
MrBedfordVan | 0:b9164b348919 | 304 | * @return BLE_ERROR_NONE on success. |
MrBedfordVan | 0:b9164b348919 | 305 | */ |
MrBedfordVan | 0:b9164b348919 | 306 | virtual ble_error_t reset(void) { |
MrBedfordVan | 0:b9164b348919 | 307 | /* Notify that the instance is about to shutdown */ |
MrBedfordVan | 0:b9164b348919 | 308 | shutdownCallChain.call(this); |
MrBedfordVan | 0:b9164b348919 | 309 | shutdownCallChain.clear(); |
MrBedfordVan | 0:b9164b348919 | 310 | |
MrBedfordVan | 0:b9164b348919 | 311 | securitySetupInitiatedCallback = NULL; |
MrBedfordVan | 0:b9164b348919 | 312 | securitySetupCompletedCallback = NULL; |
MrBedfordVan | 0:b9164b348919 | 313 | linkSecuredCallback = NULL; |
MrBedfordVan | 0:b9164b348919 | 314 | securityContextStoredCallback = NULL; |
MrBedfordVan | 0:b9164b348919 | 315 | passkeyDisplayCallback = NULL; |
MrBedfordVan | 0:b9164b348919 | 316 | |
MrBedfordVan | 0:b9164b348919 | 317 | return BLE_ERROR_NONE; |
MrBedfordVan | 0:b9164b348919 | 318 | } |
MrBedfordVan | 0:b9164b348919 | 319 | |
MrBedfordVan | 0:b9164b348919 | 320 | protected: |
MrBedfordVan | 0:b9164b348919 | 321 | SecuritySetupInitiatedCallback_t securitySetupInitiatedCallback; |
MrBedfordVan | 0:b9164b348919 | 322 | SecuritySetupCompletedCallback_t securitySetupCompletedCallback; |
MrBedfordVan | 0:b9164b348919 | 323 | LinkSecuredCallback_t linkSecuredCallback; |
MrBedfordVan | 0:b9164b348919 | 324 | HandleSpecificEvent_t securityContextStoredCallback; |
MrBedfordVan | 0:b9164b348919 | 325 | PasskeyDisplayCallback_t passkeyDisplayCallback; |
MrBedfordVan | 0:b9164b348919 | 326 | |
MrBedfordVan | 0:b9164b348919 | 327 | private: |
MrBedfordVan | 0:b9164b348919 | 328 | SecurityManagerShutdownCallbackChain_t shutdownCallChain; |
MrBedfordVan | 0:b9164b348919 | 329 | }; |
MrBedfordVan | 0:b9164b348919 | 330 | |
MrBedfordVan | 0:b9164b348919 | 331 | #endif /*__SECURITY_MANAGER_H__*/ |