security_dispatcher.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef SECURITY_DISPATCHER_H__
  13. #define SECURITY_DISPATCHER_H__
  14. #include <stdint.h>
  15. #include "sdk_errors.h"
  16. #include "ble.h"
  17. #include "ble_gap.h"
  18. #include "peer_manager_types.h"
  19. /**
  20. * @cond NO_DOXYGEN
  21. * @defgroup security_dispatcher Security Dispatcher
  22. * @ingroup peer_manager
  23. * @{
  24. * @brief An internal module of @ref peer_manager. A module for streamlining pairing, bonding, and
  25. * encryption, including flash storage of shared data.
  26. *
  27. */
  28. /**@brief Events that can come from the Security Dispatcher module.
  29. */
  30. typedef enum
  31. {
  32. SMD_EVT_PARAMS_REQ, /**< Parameters are required for a pairing procedure on the specified connection. The user must provide them using @ref smd_params_reply. */
  33. SMD_EVT_SLAVE_SECURITY_REQ, /**< The peer (slave) has requested link encryption. Call @ref smd_link_secure to honor the request. The data in the event structure must be used in the parameters. */
  34. SMD_EVT_SEC_PROCEDURE_START, /**< A security procedure has started. */
  35. SMD_EVT_PAIRING_SUCCESS, /**< A pairing procedure (and bonding if applicable) has completed with success. */
  36. SMD_EVT_PAIRING_FAIL, /**< A pairing procedure has failed which means no encryption and no bond could be established. */
  37. SMD_EVT_LINK_ENCRYPTION_UPDATE, /**< The security level of the link has been updated. The link is encrypted. */
  38. SMD_EVT_LINK_ENCRYPTION_FAILED, /**< An attempt to start encryption on an unencrypted link failed because the peripheral did not have the correct keys. If the peer is the peripheral, the force_repairing flag should be set when reattempting @ref smd_link_secure. */
  39. SMD_EVT_BONDING_INFO_STORED, /**< Information exchanged during bonding with a peer has been stored persistently. */
  40. SMD_EVT_ERROR_BONDING_INFO, /**< Information exchanged during bonding with a peer could not be stored persistently, because of an unexpected error. */
  41. // SMD_EVT_ERROR_NO_MEM, /**< An operation failed because there was no available storage room in persistent storage. Please free up room, and the operation will automatically continue. */
  42. SMD_EVT_ERROR_UNEXPECTED, /**< An operation failed with an unexpected error. The error is provided. This is possibly a fatal error. */
  43. } smd_evt_id_t;
  44. /**@brief Events parameters specific to the @ref SMD_EVT_SLAVE_SECURITY_REQ event.
  45. */
  46. typedef struct
  47. {
  48. bool bond;
  49. bool mitm;
  50. } smd_evt_slave_security_req_t;
  51. /**@brief Events parameters specific to the @ref SMD_EVT_SEC_PROCEDURE_START event.
  52. */
  53. typedef struct
  54. {
  55. pm_conn_sec_procedure_t procedure; /**< The procedure that has started. */
  56. } smd_evt_sec_procedure_start_t;
  57. /**@brief Events parameters specific to the @ref SMD_EVT_PAIRING_SUCCESS event.
  58. */
  59. typedef struct
  60. {
  61. bool bonded; /**< Whether bonding was performed. */
  62. bool mitm; /**< Whether MITM protection was used during pairing. */
  63. ble_gap_sec_kdist_t kdist_own; /**< Which keys were distributed to the peer. Only relevant if bonding was performed. */
  64. ble_gap_sec_kdist_t kdist_peer; /**< Which keys were distributed by the peer. Only relevant if bonding was performed. */
  65. } smd_evt_pairing_success_t;
  66. /**@brief Events parameters specific to the @ref SMD_EVT_PAIRING_FAIL event.
  67. */
  68. typedef struct
  69. {
  70. pm_sec_error_code_t error; /**< What went wrong. */
  71. uint8_t error_src; /**< The party that raised the error, see @ref BLE_GAP_SEC_STATUS_SOURCES. */
  72. } smd_evt_pairing_failed_t;
  73. /**@brief Events parameters specific to the @ref SMD_EVT_LINK_ENCRYPTION_UPDATE event.
  74. */
  75. typedef struct
  76. {
  77. bool mitm_protected; /**< Whether the link is now MITM protected. */
  78. } smd_evt_link_encryption_update_t;
  79. /**@brief Events parameters specific to the @ref SMD_EVT_LINK_ENCRYPTION_FAILED event.
  80. */
  81. typedef struct
  82. {
  83. pm_sec_error_code_t error; /**< What went wrong. */
  84. uint8_t error_src; /**< The party that raised the error, see @ref BLE_GAP_SEC_STATUS_SOURCES. */
  85. } smd_evt_link_encryption_failed_t;
  86. /**@brief Events parameters specific to the @ref SMD_EVT_BONDING_INFO_STORED event.
  87. */
  88. typedef struct
  89. {
  90. pm_peer_id_t peer_id; /**< The peer this event pertains to. */
  91. } smd_evt_bonding_info_stored_t;
  92. /**@brief Events parameters specific to the @ref SMD_EVT_ERROR_BONDING_INFO event.
  93. */
  94. typedef struct
  95. {
  96. pm_peer_id_t peer_id; /**< The peer this event pertains to, if previously bonded. @ref PM_PEER_ID_INVALID if no successful bonding has happened with the peer before. */
  97. ret_code_t error; /**< The unexpected error that occurred. */
  98. } smd_evt_error_bonding_info_t;
  99. // typedef struct
  100. // {
  101. // pm_peer_id_t peer_id; /**< The peer this event pertains to. */
  102. // } smd_evt_error_no_mem_t;
  103. /**@brief Events parameters specific to the @ref SMD_EVT_ERROR_UNEXPECTED event.
  104. */
  105. typedef struct
  106. {
  107. ret_code_t error; /**< The unexpected error that occurred. */
  108. } smd_evt_error_unexpected_t;
  109. typedef union
  110. {
  111. smd_evt_slave_security_req_t slave_security_req;
  112. smd_evt_sec_procedure_start_t sec_procedure_start;
  113. smd_evt_pairing_success_t pairing_success;
  114. smd_evt_pairing_failed_t pairing_failed;
  115. smd_evt_link_encryption_update_t link_encryption_update;
  116. smd_evt_link_encryption_failed_t link_encryption_failed;
  117. smd_evt_bonding_info_stored_t bonding_info_stored;
  118. smd_evt_error_bonding_info_t error_bonding_info;
  119. // smd_evt_error_no_mem_t error_no_mem;
  120. smd_evt_error_unexpected_t error_unexpected;
  121. } smd_evt_params_t; /**< Event specific parameters. Chosen based on evt_id. */
  122. /**@brief Structure describing events from the Security Dispatcher module.
  123. */
  124. typedef struct
  125. {
  126. smd_evt_id_t evt_id; /**< The type of event. */
  127. uint16_t conn_handle; /**< The connection this event pertains to. */
  128. smd_evt_params_t params; /**< Event specific parameters. Chosen based on evt_id. */
  129. } smd_evt_t;
  130. /**@brief Event handler for events from the Security Dispatcher module.
  131. *
  132. * @param[in] p_event The event that has happened.
  133. */
  134. typedef void (*smd_evt_handler_t)(smd_evt_t const * p_event);
  135. /**@brief Function for registering with the Security Dispatcher module. This function also
  136. * initializes the module if uninitialized.
  137. *
  138. * @param[in] evt_handler Callback for events from the Security Dispatcher module.
  139. *
  140. * @retval NRF_SUCCESS Registration was successful.
  141. * @retval NRF_ERROR_NO_MEM No more registrations possible.
  142. * @retval NRF_ERROR_NULL evt_handler was NULL.
  143. */
  144. ret_code_t smd_register(smd_evt_handler_t evt_handler);
  145. /**@brief Function for dispatching SoftDevice events to the Security Dispatcher module.
  146. *
  147. * @param[in] ble_evt The SoftDevice event.
  148. */
  149. void smd_ble_evt_handler(ble_evt_t * ble_evt);
  150. /**@brief Function for providing pairing and bonding parameters to use for the current pairing
  151. * procedure on a connection.
  152. *
  153. * @note If this function returns an @ref NRF_ERROR_NULL, @ref NRF_ERROR_INVALID_PARAM, @ref
  154. * BLE_ERROR_INVALID_CONN_HANDLE, or @ref NRF_ERROR_NO_MEM, this function can be called again
  155. * after corrective action.
  156. *
  157. * @note To reject a request, call this function with NULL p_sec_params.
  158. *
  159. * @param[in] conn_handle The connection handle of the connection the pairing is happening on.
  160. * @param[in] p_sec_params The security parameters to use for this link.
  161. * @param[in] p_public_key A pointer to the public key to use if using LESC, or NULL.
  162. *
  163. * @retval NRF_SUCCESS Success.
  164. * @retval NRF_ERROR_INVALID_STATE Module is not initialized, or no parameters have been
  165. * requested on that conn_handle, or this error originates
  166. * from the SoftDevice.
  167. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters (not including conn_handle).
  168. * @retval NRF_ERROR_TIMEOUT There has been an SMP timeout, so no more SMP operations
  169. * can be performed on this link.
  170. * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  171. * @retval NRF_ERROR_NO_MEM No more room in flash. Fix and reattempt later.
  172. * @retval NRF_ERROR_BUSY No write buffer. Reattempt later.
  173. */
  174. ret_code_t smd_params_reply(uint16_t conn_handle,
  175. ble_gap_sec_params_t * p_sec_params,
  176. ble_gap_lesc_p256_pk_t * p_public_key);
  177. /**@brief Function for initiating security on the link, with the specified parameters.
  178. *
  179. * @note If the connection is a peripheral connection, this will send a security request to the
  180. * master, but the master is not obligated to initiate pairing or encryption in response.
  181. * @note If the connection is a central connection and a key is available, the parameters will be
  182. * used to determine whether to re-pair or to encrypt using the existing key. If no key is
  183. * available, pairing will be started.
  184. *
  185. * @param[in] conn_handle Handle of the connection to initiate pairing on.
  186. * @param[in] p_sec_params The security parameters to use for this link. As a central, this can
  187. * be NULL to reject a slave security request.
  188. * @param[in] force_repairing Whether to force a pairing procedure to happen regardless of whether
  189. * an encryption key already exists. This argument is only relevant for
  190. * the central role. Recommended value: false
  191. *
  192. * @retval NRF_SUCCESS Success.
  193. * @retval NRF_ERROR_NULL p_sec_params was NULL (peripheral only).
  194. * @retval NRF_ERROR_INVALID_STATE Module is not initialized, or this error originates from
  195. * the SoftDevice.
  196. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters (not including conn_handle).
  197. * @retval NRF_ERROR_BUSY Unable to initiate procedure at this time.
  198. * @retval NRF_ERROR_TIMEOUT There has been an SMP timeout, so no more SMP operations
  199. * can be performed on this link.
  200. * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  201. * @retval NRF_ERROR_NO_MEM No more room in flash.
  202. * @retval NRF_ERROR_INTERNAL No more available peer IDs.
  203. */
  204. ret_code_t smd_link_secure(uint16_t conn_handle,
  205. ble_gap_sec_params_t * p_sec_params,
  206. bool force_repairing);
  207. /** @}
  208. * @endcond
  209. */
  210. #endif /* SECURITY_DISPATCHER_H__ */