security_manager.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_MANAGER_H__
  13. #define SECURITY_MANAGER_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. #include "security_dispatcher.h"
  20. /**
  21. * @cond NO_DOXYGEN
  22. * @defgroup security_manager Security Manager
  23. * @ingroup peer_manager
  24. * @{
  25. * @brief An internal module of @ref peer_manager. A module for streamlining pairing, bonding, and
  26. * encryption, including flash storage of shared data.
  27. */
  28. /**@brief Events that can come from the Security Manager module.
  29. */
  30. typedef enum
  31. {
  32. // SM_EVT_PARAMS_REQ = SMD_EVT_PARAMS_REQ, /**< Parameters are required for a pairing procedure on the specified connection. The user must provide them using @ref sm_sec_params_set or @ref sm_sec_params_reply (only this procedure, currently unimplemented). */
  33. SM_EVT_SLAVE_SECURITY_REQ = SMD_EVT_SLAVE_SECURITY_REQ, /**< The peer (peripheral) has requested link encryption, which has been enabled. */
  34. SM_EVT_SEC_PROCEDURE_START = SMD_EVT_SEC_PROCEDURE_START, /**< A security procedure has started. */
  35. SM_EVT_PAIRING_SUCCESS = SMD_EVT_PAIRING_SUCCESS, /**< A pairing procedure (and bonding if applicable) has completed with success. */
  36. SM_EVT_PAIRING_FAIL = SMD_EVT_PAIRING_FAIL, /**< A pairing procedure has failed which means no encryption and no bond could be established. */
  37. SM_EVT_LINK_ENCRYPTION_UPDATE = SMD_EVT_LINK_ENCRYPTION_UPDATE, /**< The security level of the link has been updated. The link is encrypted. */
  38. SM_EVT_LINK_ENCRYPTION_FAILED = 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 sm_link_secure. */
  39. SM_EVT_BONDING_INFO_STORED = SMD_EVT_BONDING_INFO_STORED, /**< Information exchanged during bonding with a peer has been stored persistently. */
  40. SM_EVT_ERROR_BONDING_INFO = SMD_EVT_ERROR_BONDING_INFO, /**< Information exchanged during bonding with a peer could not be stored persistently, because of an unexpected error. */
  41. SM_EVT_ERROR_UNEXPECTED = SMD_EVT_ERROR_UNEXPECTED, /**< An operation failed with an unexpected error. The error is provided. This is possibly a fatal error. */
  42. SM_EVT_ERROR_NO_MEM /*= 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 after the next compression. */
  43. SM_EVT_ERROR_SMP_TIMEOUT, /**< An operation failed because there has been an SMP timeout on the link, which entails that no more security operations can be performed on it. */
  44. SM_EVT_CONN_SEC_CONFIG_REQ, /**< The peer (central) has requested pairing, but a bond already exists with that peer. Reply by calling @ref sm_conn_sec_config_reply before the event handler returns. If no reply is sent, a default is used. */
  45. } sm_evt_id_t;
  46. typedef struct
  47. {
  48. sm_evt_id_t evt_id;
  49. uint16_t conn_handle;
  50. smd_evt_params_t params;
  51. } sm_evt_t;
  52. /**@brief Event handler for events from the Security Manager module.
  53. *
  54. * @param[in] event The event that has happened.
  55. * @param[in] conn_handle The connection handle the event pertains to.
  56. */
  57. typedef void (*sm_evt_handler_t)(sm_evt_t const * p_event);
  58. /**@brief Function for registering with the Security Manager module. This function also
  59. * initializes the module if uninitialized.
  60. *
  61. * @param[in] evt_handler Callback for events from the Security Manager module.
  62. *
  63. * @retval NRF_SUCCESS Registration was successful.
  64. * @retval NRF_ERROR_NO_MEM No more registrations possible.
  65. * @retval NRF_ERROR_NULL evt_handler was NULL.
  66. * @retval NRF_ERROR_INTERNAL An unexpected error occurred.
  67. */
  68. ret_code_t sm_register(sm_evt_handler_t evt_handler);
  69. /**@brief Function for dispatching SoftDevice events to the Security Manager module.
  70. *
  71. * @param[in] ble_evt The SoftDevice event.
  72. */
  73. void sm_ble_evt_handler(ble_evt_t * ble_evt);
  74. /**@brief Function for providing pairing and bonding parameters to use for pairing procedures.
  75. *
  76. * @details Until this is called, all bonding procedures initiated by the peer will be rejected.
  77. * This function can be called multiple times, even with NULL p_sec_params, in which case
  78. * it will go back to rejecting all procedures.
  79. *
  80. * @param[in] p_sec_params The security parameters to use for this link. Can be NULL to reject
  81. * all pairing procedures.
  82. *
  83. * @retval NRF_SUCCESS Success.
  84. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters.
  85. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  86. */
  87. ret_code_t sm_sec_params_set(ble_gap_sec_params_t * p_sec_params);
  88. /**@brief Function for providing security configuration for a link.
  89. *
  90. * @details This function is optional, and must be called in reply to a @ref
  91. * SM_EVT_CONN_SEC_CONFIG_REQ event, before the Peer Manager event handler returns. If it
  92. * is not called in time, a default configuration is used. See @ref pm_conn_sec_config_t
  93. * for the value of the default.
  94. *
  95. * @param[in] conn_handle The connection to set the configuration for.
  96. * @param[in] p_conn_sec_config The configuration.
  97. */
  98. void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config);
  99. /**@brief Experimental function for specifying the public key to use for LESC operations.
  100. *
  101. * @details This function can be called multiple times. The specified public key will be used for
  102. * all subsequent LESC (LE Secure Connections) operations until the next time this function
  103. * is called.
  104. *
  105. * @note The key must continue to reside in application memory as it is not copied by Peer Manager.
  106. *
  107. * @param[in] p_public_key The public key to use for all subsequent LESC operations.
  108. *
  109. * @retval NRF_SUCCESS Pairing initiated successfully.
  110. * @retval NRF_ERROR_INVALID_STATE Peer Manager is not initialized.
  111. */
  112. ret_code_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key);
  113. /**@brief Function for providing pairing and bonding parameters to use for the current pairing
  114. * procedure on a connection.
  115. *
  116. * @warning This function is not yet implemented.
  117. *
  118. * @note If this function returns an @ref NRF_ERROR_NULL, @ref NRF_ERROR_INVALID_PARAM, @ref
  119. * BLE_ERROR_INVALID_CONN_HANDLE, or @ref NRF_ERROR_NO_MEM, this function can be called again
  120. * after corrective action.
  121. *
  122. * @note To reject a request, call this function with NULL p_sec_params.
  123. *
  124. * @param[in] conn_handle The connection handle of the connection the pairing is happening on.
  125. * @param[in] p_sec_params The security parameters to use for this link.
  126. *
  127. * @retval NRF_SUCCESS Success.
  128. * @retval NRF_ERROR_INVALID_STATE Module is not initialized, or no parameters have been
  129. * requested on that conn_handle, or this error originates
  130. * from the SoftDevice.
  131. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters (not including conn_handle).
  132. * @retval NRF_ERROR_TIMEOUT There has been an SMP timeout, so no more SMP operations
  133. * can be performed on this link.
  134. * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  135. * @retval NRF_ERROR_NO_MEM No more room in flash. Fix and reattempt later.
  136. * @retval NRF_ERROR_BUSY No write buffer. Reattempt later.
  137. */
  138. ret_code_t sm_sec_params_reply(uint16_t conn_handle, ble_gap_sec_params_t * p_sec_params);
  139. /**@brief Function for initiating security on the link, with the specified parameters.
  140. *
  141. * @note If the connection is a peripheral connection, this will send a security request to the
  142. * master, but the master is not obligated to initiate pairing or encryption in response.
  143. * @note If the connection is a central connection and a key is available, the parameters will be
  144. * used to determine whether to re-pair or to encrypt using the existing key. If no key is
  145. * available, pairing will be started.
  146. *
  147. * @param[in] conn_handle Handle of the connection to initiate pairing on.
  148. * @param[in] force_repairing Whether to force a pairing procedure to happen regardless of whether
  149. * an encryption key already exists. This argument is only relevant for
  150. * the central role. Recommended value: false
  151. *
  152. * @retval NRF_SUCCESS Success.
  153. * @retval NRF_ERROR_TIMEOUT There has been an SMP timeout, so no more SMP operations
  154. * can be performed on this link.
  155. * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  156. * @retval NRF_ERROR_NOT_FOUND Security parameters have not been set.
  157. * @retval NRF_ERROR_INVALID_STATE Module is not initialized.
  158. * @retval NRF_ERROR_INTERNAL An unexpected error occurred.
  159. */
  160. ret_code_t sm_link_secure(uint16_t conn_handle, bool force_repairing);
  161. /** @}
  162. * @endcond
  163. */
  164. #endif /* SECURITY_MANAGER_H__ */