security_manager.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef SECURITY_MANAGER_H__
  41. #define SECURITY_MANAGER_H__
  42. #include <stdint.h>
  43. #include "sdk_errors.h"
  44. #include "ble.h"
  45. #include "ble_gap.h"
  46. #include "peer_manager_types.h"
  47. #include "security_dispatcher.h"
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * @cond NO_DOXYGEN
  53. * @defgroup security_manager Security Manager
  54. * @ingroup peer_manager
  55. * @{
  56. * @brief An internal module of @ref peer_manager. A module for streamlining pairing, bonding, and
  57. * encryption, including flash storage of shared data.
  58. */
  59. /**@brief Function for initializing the Security Manager module.
  60. *
  61. * @retval NRF_SUCCESS If initialization was successful.
  62. * @retval NRF_ERROR_INTERNAL If an unexpected error occurred.
  63. */
  64. ret_code_t sm_init(void);
  65. /**@brief Function for dispatching SoftDevice events to the Security Manager module.
  66. *
  67. * @param[in] ble_evt The SoftDevice event.
  68. */
  69. void sm_ble_evt_handler(ble_evt_t const * ble_evt);
  70. /**@brief Function for providing pairing and bonding parameters to use for pairing procedures.
  71. *
  72. * @details Until this is called, all bonding procedures initiated by the peer will be rejected.
  73. * This function can be called multiple times, even with NULL p_sec_params, in which case
  74. * it will go back to rejecting all procedures.
  75. *
  76. * @param[in] p_sec_params The security parameters to use for this link. Can be NULL to reject
  77. * all pairing procedures.
  78. *
  79. * @retval NRF_SUCCESS Success.
  80. * @retval NRF_ERROR_INVALID_PARAM Invalid combination of parameters.
  81. */
  82. ret_code_t sm_sec_params_set(ble_gap_sec_params_t * p_sec_params);
  83. /**@brief Function for providing security configuration for a link.
  84. *
  85. * @details This function is optional, and must be called in reply to a @ref
  86. * PM_EVT_CONN_SEC_CONFIG_REQ event, before the Peer Manager event handler returns. If it
  87. * is not called in time, a default configuration is used. See @ref pm_conn_sec_config_t
  88. * for the value of the default.
  89. *
  90. * @param[in] conn_handle The connection to set the configuration for.
  91. * @param[in] p_conn_sec_config The configuration.
  92. */
  93. void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config);
  94. /**@brief Function for providing security parameters for a link.
  95. *
  96. * @details This function is optional, and must be called in reply to a @ref
  97. * PM_EVT_CONN_SEC_PARAMS_REQ event, before the Security Manager event handler returns. If
  98. * it is not called in time, the parameters given in @ref sm_sec_params_set are used. See
  99. * @ref pm_conn_sec_config_t for the value of the default.
  100. *
  101. * @param[in] conn_handle The connection to set the parameters for.
  102. * @param[in] p_sec_params The parameters. If NULL, the security procedure is rejected.
  103. * @param[in] p_context The context found in the request event that this function replies to.
  104. *
  105. * @retval NRF_SUCCESS Successful reply.
  106. * @retval NRF_ERROR_NULL p_context was null.
  107. * @retval NRF_ERROR_INVALID_PARAM Value of p_sec_params was invalid.
  108. * @retval NRF_ERROR_INVALID_STATE This module is not initialized.
  109. */
  110. ret_code_t sm_sec_params_reply(uint16_t conn_handle,
  111. ble_gap_sec_params_t * p_sec_params,
  112. void const * p_context);
  113. /**@brief Experimental function for specifying the public key to use for LESC operations.
  114. *
  115. * @details This function can be called multiple times. The specified public key will be used for
  116. * all subsequent LESC (LE Secure Connections) operations until the next time this function
  117. * is called.
  118. *
  119. * @note The key must continue to reside in application memory as it is not copied by Peer Manager.
  120. *
  121. * @param[in] p_public_key The public key to use for all subsequent LESC operations.
  122. *
  123. * @retval NRF_SUCCESS Pairing initiated successfully.
  124. * @retval NRF_ERROR_INVALID_STATE This module is not initialized.
  125. */
  126. ret_code_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key);
  127. /**@brief Function for getting the security status of a connection.
  128. *
  129. * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice.
  130. * @param[out] p_conn_sec_status Security status of the link.
  131. *
  132. * @retval NRF_SUCCESS If pairing was initiated successfully.
  133. * @retval BLE_ERROR_INVALID_CONN_HANDLE If the connection handle is invalid.
  134. * @retval NRF_ERROR_NULL If @p p_conn_sec_status was NULL.
  135. */
  136. ret_code_t sm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t * p_conn_sec_status);
  137. /**@brief Function for comparing the security status of a connection against a baseline.
  138. *
  139. * @param[in] conn_handle Connection handle of the link as provided by the SoftDevice.
  140. * @param[out] p_sec_status_req Target baseline security status to compare against.
  141. *
  142. * @retval true If the security status of the connection matches or exceeds the baseline on all
  143. * points.
  144. * @retval false If the security status of the connection does not fulfill the baseline, or could
  145. * not be retrieved.
  146. */
  147. bool sm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t * p_sec_status_req);
  148. /**@brief Function for initiating security on the link, with the specified parameters.
  149. *
  150. * @note If the connection is a peripheral connection, this will send a security request to the
  151. * master, but the master is not obligated to initiate pairing or encryption in response.
  152. * @note If the connection is a central connection and a key is available, the parameters will be
  153. * used to determine whether to re-pair or to encrypt using the existing key. If no key is
  154. * available, pairing will be started.
  155. *
  156. * @param[in] conn_handle Handle of the connection to initiate pairing on.
  157. * @param[in] force_repairing Whether to force a pairing procedure to happen regardless of whether
  158. * an encryption key already exists. This argument is only relevant for
  159. * the central role. Recommended value: false
  160. *
  161. * @retval NRF_SUCCESS Success.
  162. * @retval NRF_ERROR_TIMEOUT There has been an SMP timeout, so no more SMP operations
  163. * can be performed on this link.
  164. * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  165. * @retval NRF_ERROR_INVALID_DATA Peer is bonded, but no LTK was found, and repairing was
  166. * not requested.
  167. * @retval NRF_ERROR_NOT_FOUND Security parameters have not been set.
  168. * @retval NRF_ERROR_INVALID_STATE A security procedure is already in progress on the link,
  169. * or the link is disconnecting.
  170. * @retval NRF_ERROR_INTERNAL An unexpected error occurred.
  171. */
  172. ret_code_t sm_link_secure(uint16_t conn_handle, bool force_repairing);
  173. /** @}
  174. * @endcond
  175. */
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* SECURITY_MANAGER_H__ */