security_manager.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Copyright (c) 2015 - 2019, 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 initiating security on the link, with the specified parameters.
  128. *
  129. * @note If the connection is a peripheral connection, this will send a security request to the
  130. * master, but the master is not obligated to initiate pairing or encryption in response.
  131. * @note If the connection is a central connection and a key is available, the parameters will be
  132. * used to determine whether to re-pair or to encrypt using the existing key. If no key is
  133. * available, pairing will be started.
  134. *
  135. * @param[in] conn_handle Handle of the connection to initiate pairing on.
  136. * @param[in] force_repairing Whether to force a pairing procedure to happen regardless of whether
  137. * an encryption key already exists. This argument is only relevant for
  138. * the central role. Recommended value: false
  139. *
  140. * @retval NRF_SUCCESS Success.
  141. * @retval NRF_ERROR_TIMEOUT There has been an SMP timeout, so no more SMP operations
  142. * can be performed on this link.
  143. * @retval BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  144. * @retval NRF_ERROR_INVALID_DATA Peer is bonded, but no LTK was found, and repairing was
  145. * not requested.
  146. * @retval NRF_ERROR_NOT_FOUND Security parameters have not been set.
  147. * @retval NRF_ERROR_INVALID_STATE A security procedure is already in progress on the link,
  148. * or the link is disconnecting.
  149. * @retval NRF_ERROR_INTERNAL An unexpected error occurred.
  150. */
  151. ret_code_t sm_link_secure(uint16_t conn_handle, bool force_repairing);
  152. /** @}
  153. * @endcond
  154. */
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif /* SECURITY_MANAGER_H__ */