security_manager.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(PEER_MANAGER)
  42. #include "security_manager.h"
  43. #include <string.h>
  44. #include "ble_err.h"
  45. #include "security_dispatcher.h"
  46. #include "peer_database.h"
  47. #include "ble_conn_state.h"
  48. #include "id_manager.h"
  49. #include "sdk_common.h"
  50. #if PM_LESC_ENABLED
  51. #include "nrf_ble_lesc.h"
  52. #endif
  53. #define NRF_LOG_MODULE_NAME peer_manager_sm
  54. #if PM_LOG_ENABLED
  55. #define NRF_LOG_LEVEL PM_LOG_LEVEL
  56. #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
  57. #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
  58. #else
  59. #define NRF_LOG_LEVEL 0
  60. #endif // PM_LOG_ENABLED
  61. #include "nrf_log.h"
  62. #include "nrf_log_ctrl.h"
  63. NRF_LOG_MODULE_REGISTER();
  64. #include "nrf_strerror.h"
  65. // The number of registered event handlers.
  66. #define SM_EVENT_HANDLERS_CNT (sizeof(m_evt_handlers) / sizeof(m_evt_handlers[0]))
  67. // Security Manager event handler in Peer Manager.
  68. extern void pm_sm_evt_handler(pm_evt_t * p_sm_evt);
  69. // Security Manager events' handlers.
  70. // The number of elements in this array is SM_EVENT_HANDLERS_CNT.
  71. static pm_evt_handler_internal_t const m_evt_handlers[] =
  72. {
  73. pm_sm_evt_handler
  74. };
  75. // The context type that is used in PM_EVT_CONN_SEC_PARAMS_REQ events and in calls to sm_sec_params_reply().
  76. typedef struct
  77. {
  78. ble_gap_sec_params_t * p_sec_params; //!< The security parameters to use in the call to the security_dispatcher
  79. ble_gap_sec_params_t sec_params_mem; //!< The buffer for holding the security parameters.
  80. bool params_reply_called; //!< Whether @ref sm_sec_params_reply has been called for this context instance.
  81. } sec_params_reply_context_t;
  82. static bool m_module_initialized; //!< Whether the Security Manager module has been initialized.
  83. static ble_gap_sec_params_t m_sec_params; //!< The buffer for the default security parameters set by @ref sm_sec_params_set.
  84. static ble_gap_sec_params_t * mp_sec_params = NULL; //!< The default security parameters set by @ref sm_sec_params_set.
  85. static bool m_sec_params_set = false; //!< Whether @ref sm_sec_params_set has been called.
  86. #if PM_LESC_ENABLED == 0
  87. static ble_gap_lesc_p256_pk_t * m_p_public_key; //!< Pointer, provided by the user, to the public key to use for LESC procedures.
  88. #endif
  89. static ble_conn_state_user_flag_id_t m_flag_link_secure_pending_busy = BLE_CONN_STATE_USER_FLAG_INVALID; //!< User flag indicating whether a connection has a pending call to @ref sm_link_secure because it returned @ref NRF_ERROR_BUSY.
  90. static ble_conn_state_user_flag_id_t m_flag_link_secure_force_repairing = BLE_CONN_STATE_USER_FLAG_INVALID; //!< User flag indicating whether a pending call to @ref sm_link_secure should be called with true for the force_repairing parameter.
  91. static ble_conn_state_user_flag_id_t m_flag_link_secure_null_params = BLE_CONN_STATE_USER_FLAG_INVALID; //!< User flag indicating whether a pending call to @ref sm_link_secure should be called with NULL security parameters.
  92. static ble_conn_state_user_flag_id_t m_flag_params_reply_pending_busy = BLE_CONN_STATE_USER_FLAG_INVALID; //!< User flag indicating whether a connection has a pending call to @ref sm_sec_params_reply because it returned @ref NRF_ERROR_BUSY.
  93. /**@brief Function for sending an SM event to all registered event handlers.
  94. *
  95. * @param[in] p_event The event to send.
  96. */
  97. static void evt_send(pm_evt_t * p_event)
  98. {
  99. for (uint32_t i = 0; i < SM_EVENT_HANDLERS_CNT; i++)
  100. {
  101. m_evt_handlers[i](p_event);
  102. }
  103. }
  104. /**@brief Function for setting or clearing user flags based on error codes returned from @ref
  105. * smd_link_secure or @ref smd_params_reply.
  106. *
  107. * @param[in] conn_handle The connection the call pertained to.
  108. * @param[in] err_code The error code returned from @ref smd_link_secure or
  109. * @ref smd_params_reply.
  110. * @param[in] params_reply Whether the call was to @ref smd_params_reply.
  111. */
  112. static void flags_set_from_err_code(uint16_t conn_handle, ret_code_t err_code, bool params_reply)
  113. {
  114. bool flag_value_busy = false;
  115. if (err_code == NRF_ERROR_BUSY)
  116. {
  117. flag_value_busy = true;
  118. }
  119. else
  120. {
  121. flag_value_busy = false;
  122. }
  123. if (params_reply)
  124. {
  125. ble_conn_state_user_flag_set(conn_handle,
  126. m_flag_params_reply_pending_busy,
  127. flag_value_busy);
  128. ble_conn_state_user_flag_set(conn_handle,
  129. m_flag_link_secure_pending_busy,
  130. false);
  131. }
  132. else
  133. {
  134. ble_conn_state_user_flag_set(conn_handle,
  135. m_flag_link_secure_pending_busy,
  136. flag_value_busy);
  137. }
  138. }
  139. static inline pm_evt_t new_evt(pm_evt_id_t evt_id, uint16_t conn_handle)
  140. {
  141. pm_evt_t evt =
  142. {
  143. .evt_id = evt_id,
  144. .conn_handle = conn_handle,
  145. .peer_id = im_peer_id_get_by_conn_handle(conn_handle)
  146. };
  147. return evt;
  148. }
  149. /**@brief Function for sending a PM_EVT_ERROR_UNEXPECTED event.
  150. *
  151. * @param[in] conn_handle The connection handle the event pertains to.
  152. * @param[in] err_code The unexpected error that occurred.
  153. */
  154. static void send_unexpected_error(uint16_t conn_handle, ret_code_t err_code)
  155. {
  156. pm_evt_t error_evt = new_evt(PM_EVT_ERROR_UNEXPECTED, conn_handle);
  157. error_evt.params.error_unexpected.error = err_code;
  158. evt_send(&error_evt);
  159. }
  160. /**@brief Returns whether the LTK came from LESC bonding.
  161. *
  162. * @param[in] peer_id The peer to check.
  163. *
  164. * @return Whether the key is LESC or not.
  165. */
  166. static bool key_is_lesc(pm_peer_id_t peer_id)
  167. {
  168. pm_peer_data_flash_t peer_data;
  169. ret_code_t err_code;
  170. err_code = pdb_peer_data_ptr_get(peer_id, PM_PEER_DATA_ID_BONDING, &peer_data);
  171. return (err_code == NRF_SUCCESS) && (peer_data.p_bonding_data->own_ltk.enc_info.lesc);
  172. }
  173. /**@brief Function for sending an event based on error codes returned from @ref smd_link_secure or
  174. * @ref smd_params_reply.
  175. *
  176. * @param[in] conn_handle The connection the event pertains to.
  177. * @param[in] err_code The error code returned from @ref smd_link_secure or
  178. * @ref smd_params_reply.
  179. * @param[in] p_sec_params The security parameters attempted to pass in the call to
  180. * @ref smd_link_secure or @ref smd_params_reply.
  181. */
  182. static void events_send_from_err_code(uint16_t conn_handle,
  183. ret_code_t err_code,
  184. ble_gap_sec_params_t * p_sec_params)
  185. {
  186. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_INVALID_STATE))
  187. {
  188. if (err_code == NRF_ERROR_TIMEOUT)
  189. {
  190. NRF_LOG_WARNING("Cannot secure link because a previous security procedure ended in timeout. "\
  191. "Disconnect and retry. smd_params_reply() or smd_link_secure() returned "\
  192. "NRF_ERROR_TIMEOUT. conn_handle: %d",
  193. conn_handle);
  194. pm_evt_t evt = new_evt(PM_EVT_CONN_SEC_FAILED, conn_handle);
  195. evt.params.conn_sec_failed.procedure = ((p_sec_params != NULL) && p_sec_params->bond)
  196. ? PM_CONN_SEC_PROCEDURE_BONDING
  197. : PM_CONN_SEC_PROCEDURE_PAIRING;
  198. evt.params.conn_sec_failed.error_src = BLE_GAP_SEC_STATUS_SOURCE_LOCAL;
  199. evt.params.conn_sec_failed.error = PM_CONN_SEC_ERROR_SMP_TIMEOUT;
  200. evt_send(&evt);
  201. }
  202. else
  203. {
  204. NRF_LOG_ERROR("Could not perform security procedure. smd_params_reply() or "\
  205. "smd_link_secure() returned %s. conn_handle: %d",
  206. nrf_strerror_get(err_code),
  207. conn_handle);
  208. send_unexpected_error(conn_handle, err_code);
  209. }
  210. }
  211. }
  212. /**@brief Function for sending an PM_EVT_CONN_SEC_PARAMS_REQ event.
  213. *
  214. * @param[in] conn_handle The connection the event pertains to.
  215. * @param[in] p_peer_params The peer's security parameters to include in the event. Can be NULL.
  216. * @param[in] p_context Pointer to a context that the user must include in the call to @ref
  217. * sm_sec_params_reply().
  218. */
  219. static void params_req_send(uint16_t conn_handle,
  220. ble_gap_sec_params_t const * p_peer_params,
  221. sec_params_reply_context_t * p_context)
  222. {
  223. pm_evt_t evt = new_evt(PM_EVT_CONN_SEC_PARAMS_REQ, conn_handle);
  224. evt.params.conn_sec_params_req.p_peer_params = p_peer_params;
  225. evt.params.conn_sec_params_req.p_context = p_context;
  226. evt_send(&evt);
  227. }
  228. /**@brief Function for creating a new @ref sec_params_reply_context_t with the correct initial values.
  229. *
  230. * @return The new context.
  231. */
  232. static sec_params_reply_context_t new_context_get(void)
  233. {
  234. sec_params_reply_context_t new_context =
  235. {
  236. .p_sec_params = mp_sec_params,
  237. .params_reply_called = false
  238. };
  239. return new_context;
  240. }
  241. /**@brief Internal function corresponding to @ref sm_link_secure.
  242. *
  243. * @param[in] conn_handle The connection to secure.
  244. * @param[in] null_params Whether to pass NULL security parameters to the security_dispatcher.
  245. * @param[in] force_repairing Whether to force rebonding if peer exists.
  246. * @param[in] send_events Whether to send events based on the result of @ref smd_link_secure.
  247. *
  248. * @return Same return codes as @ref sm_link_secure.
  249. */
  250. static ret_code_t link_secure(uint16_t conn_handle,
  251. bool null_params,
  252. bool force_repairing,
  253. bool send_events)
  254. {
  255. ret_code_t err_code;
  256. ret_code_t return_err_code;
  257. ble_gap_sec_params_t * p_sec_params;
  258. if (null_params)
  259. {
  260. p_sec_params = NULL;
  261. }
  262. else
  263. {
  264. sec_params_reply_context_t context = new_context_get();
  265. params_req_send(conn_handle, NULL, &context);
  266. p_sec_params = context.p_sec_params;
  267. if (!m_sec_params_set && !context.params_reply_called)
  268. {
  269. // Security parameters have not been set.
  270. return NRF_ERROR_NOT_FOUND;
  271. }
  272. }
  273. err_code = smd_link_secure(conn_handle, p_sec_params, force_repairing);
  274. flags_set_from_err_code(conn_handle, err_code, false);
  275. switch (err_code)
  276. {
  277. case NRF_ERROR_BUSY:
  278. ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_null_params, null_params);
  279. ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_force_repairing, force_repairing);
  280. return_err_code = NRF_SUCCESS;
  281. break;
  282. case NRF_SUCCESS:
  283. case NRF_ERROR_TIMEOUT:
  284. case BLE_ERROR_INVALID_CONN_HANDLE:
  285. case NRF_ERROR_INVALID_STATE:
  286. case NRF_ERROR_INVALID_DATA:
  287. return_err_code = err_code;
  288. break;
  289. default:
  290. NRF_LOG_ERROR("Could not perform security procedure. smd_link_secure() returned %s. "\
  291. "conn_handle: %d",
  292. nrf_strerror_get(err_code),
  293. conn_handle);
  294. return_err_code = NRF_ERROR_INTERNAL;
  295. break;
  296. }
  297. if (send_events)
  298. {
  299. events_send_from_err_code(conn_handle, err_code, p_sec_params);
  300. }
  301. return return_err_code;
  302. }
  303. /**@brief Function for requesting security parameters from the user and passing them to the security_dispatcher.
  304. *
  305. * @param[in] conn_handle The connection that needs security parameters.
  306. * @param[in] p_peer_params The peer's security parameters if present. Otherwise NULL.
  307. */
  308. static void smd_params_reply_perform(uint16_t conn_handle, ble_gap_sec_params_t const * p_peer_params)
  309. {
  310. ret_code_t err_code;
  311. ble_gap_lesc_p256_pk_t * p_public_key;
  312. sec_params_reply_context_t context = new_context_get();
  313. params_req_send(conn_handle, p_peer_params, &context);
  314. #if PM_LESC_ENABLED
  315. p_public_key = nrf_ble_lesc_public_key_get();
  316. #else
  317. p_public_key = m_p_public_key;
  318. #endif // PM_LESC_ENABLED
  319. err_code = smd_params_reply(conn_handle, context.p_sec_params, p_public_key);
  320. flags_set_from_err_code(conn_handle, err_code, true);
  321. events_send_from_err_code(conn_handle, err_code, context.p_sec_params);
  322. }
  323. /**@brief Function for handling @ref PM_EVT_CONN_SEC_PARAMS_REQ events.
  324. *
  325. * @param[in] p_event The @ref PM_EVT_CONN_SEC_PARAMS_REQ event.
  326. */
  327. static __INLINE void params_req_process(pm_evt_t const * p_event)
  328. {
  329. smd_params_reply_perform(p_event->conn_handle, p_event->params.conn_sec_params_req.p_peer_params);
  330. }
  331. ret_code_t sm_conn_sec_status_get(uint16_t conn_handle, pm_conn_sec_status_t * p_conn_sec_status)
  332. {
  333. VERIFY_PARAM_NOT_NULL(p_conn_sec_status);
  334. ble_conn_state_status_t status = ble_conn_state_status(conn_handle);
  335. if (status == BLE_CONN_STATUS_INVALID)
  336. {
  337. return BLE_ERROR_INVALID_CONN_HANDLE;
  338. }
  339. pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle);
  340. p_conn_sec_status->connected = (status == BLE_CONN_STATUS_CONNECTED);
  341. p_conn_sec_status->bonded = (peer_id != PM_PEER_ID_INVALID);
  342. p_conn_sec_status->encrypted = ble_conn_state_encrypted(conn_handle);
  343. p_conn_sec_status->mitm_protected = ble_conn_state_mitm_protected(conn_handle);
  344. p_conn_sec_status->lesc = ble_conn_state_lesc(conn_handle)
  345. || (ble_conn_state_encrypted(conn_handle)
  346. && key_is_lesc(peer_id));
  347. return NRF_SUCCESS;
  348. }
  349. bool sm_sec_is_sufficient(uint16_t conn_handle, pm_conn_sec_status_t * p_sec_status_req)
  350. {
  351. pm_conn_sec_status_t sec_status = {.reserved = ~0}; // Set all bits in reserved to 1 so they are
  352. // ignored in subsequent logic.
  353. ret_code_t err_code = sm_conn_sec_status_get(conn_handle, &sec_status);
  354. STATIC_ASSERT(sizeof(pm_conn_sec_status_t) == sizeof(uint8_t));
  355. uint8_t unmet_reqs = (~(*((uint8_t *) &sec_status)) & *((uint8_t *) p_sec_status_req));
  356. return (err_code == NRF_SUCCESS) && !unmet_reqs;
  357. }
  358. /**@brief Function for handling @ref PM_EVT_SLAVE_SECURITY_REQ events.
  359. *
  360. * @param[in] p_event The @ref PM_EVT_SLAVE_SECURITY_REQ event.
  361. */
  362. static void sec_req_process(pm_evt_t const * p_event)
  363. {
  364. ret_code_t err_code;
  365. bool null_params = false;
  366. bool force_repairing = false;
  367. if (mp_sec_params == NULL)
  368. {
  369. null_params = true;
  370. }
  371. else if (ble_conn_state_encrypted(p_event->conn_handle))
  372. {
  373. pm_conn_sec_status_t sec_status_req =
  374. {
  375. .bonded = p_event->params.slave_security_req.bond,
  376. .mitm_protected = p_event->params.slave_security_req.mitm,
  377. .lesc = p_event->params.slave_security_req.lesc,
  378. };
  379. force_repairing = !sm_sec_is_sufficient(p_event->conn_handle, &sec_status_req);
  380. }
  381. err_code = link_secure(p_event->conn_handle, null_params, force_repairing, true);
  382. UNUSED_VARIABLE(err_code); // The error code has been properly handled inside link_secure().
  383. }
  384. /**@brief Function for translating an SMD event to an SM event and passing it on to SM event handlers.
  385. *
  386. * @param[in] p_event The event to forward.
  387. */
  388. static void evt_forward(pm_evt_t * p_event)
  389. {
  390. evt_send(p_event);
  391. }
  392. /**@brief Event handler for events from the Security Dispatcher module.
  393. * This handler is extern in Security Dispatcher.
  394. *
  395. * @param[in] p_event The event that has happened.
  396. */
  397. void sm_smd_evt_handler(pm_evt_t * p_event)
  398. {
  399. switch (p_event->evt_id)
  400. {
  401. case PM_EVT_CONN_SEC_PARAMS_REQ:
  402. params_req_process(p_event);
  403. break;
  404. case PM_EVT_SLAVE_SECURITY_REQ:
  405. sec_req_process(p_event);
  406. /* fallthrough */
  407. default:
  408. // Forward the event to all registered Security Manager event handlers.
  409. evt_forward(p_event);
  410. break;
  411. }
  412. }
  413. /**@brief Function handling a pending params_reply. See @ref ble_conn_state_user_function_t.
  414. */
  415. static void params_reply_pending_handle(uint16_t conn_handle, void * p_context)
  416. {
  417. UNUSED_PARAMETER(p_context);
  418. smd_params_reply_perform(conn_handle, NULL);
  419. }
  420. /**@brief Function handling a pending link_secure. See @ref ble_conn_state_user_function_t.
  421. */
  422. static void link_secure_pending_handle(uint16_t conn_handle, void * p_context)
  423. {
  424. UNUSED_PARAMETER(p_context);
  425. bool force_repairing = ble_conn_state_user_flag_get(conn_handle, m_flag_link_secure_force_repairing);
  426. bool null_params = ble_conn_state_user_flag_get(conn_handle, m_flag_link_secure_null_params);
  427. // If this fails, it will be automatically retried.
  428. ret_code_t err_code = link_secure(conn_handle, null_params, force_repairing, true);
  429. UNUSED_VARIABLE(err_code);
  430. }
  431. /**@brief Event handler for events from the Peer Database module.
  432. * This handler is extern in Peer Database.
  433. *
  434. * @param[in] p_event The event that has happened.
  435. */
  436. void sm_pdb_evt_handler(pm_evt_t * p_event)
  437. {
  438. switch (p_event->evt_id)
  439. {
  440. case PM_EVT_FLASH_GARBAGE_COLLECTED:
  441. case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
  442. case PM_EVT_PEER_DATA_UPDATE_FAILED:
  443. case PM_EVT_PEER_DELETE_SUCCEEDED:
  444. case PM_EVT_PEER_DELETE_FAILED:
  445. (void) ble_conn_state_for_each_set_user_flag(m_flag_params_reply_pending_busy,
  446. params_reply_pending_handle,
  447. NULL);
  448. (void) ble_conn_state_for_each_set_user_flag(m_flag_link_secure_pending_busy,
  449. link_secure_pending_handle,
  450. NULL);
  451. break;
  452. default:
  453. // Do nothing.
  454. break;
  455. }
  456. }
  457. /**@brief Funtion for initializing a BLE Connection State user flag.
  458. *
  459. * @param[out] flag_id The flag to initialize.
  460. */
  461. static void flag_id_init(ble_conn_state_user_flag_id_t * p_flag_id)
  462. {
  463. if (*p_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID)
  464. {
  465. *p_flag_id = ble_conn_state_user_flag_acquire();
  466. }
  467. }
  468. ret_code_t sm_init(void)
  469. {
  470. NRF_PM_DEBUG_CHECK(!m_module_initialized);
  471. #if PM_LESC_ENABLED
  472. ret_code_t err_code = nrf_ble_lesc_init();
  473. if (err_code != NRF_SUCCESS)
  474. {
  475. return err_code;
  476. }
  477. #endif
  478. flag_id_init(&m_flag_link_secure_pending_busy);
  479. flag_id_init(&m_flag_link_secure_force_repairing);
  480. flag_id_init(&m_flag_link_secure_null_params);
  481. flag_id_init(&m_flag_params_reply_pending_busy);
  482. if (m_flag_params_reply_pending_busy == BLE_CONN_STATE_USER_FLAG_INVALID)
  483. {
  484. NRF_LOG_ERROR("Could not acquire conn_state user flags. Increase "\
  485. "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module.");
  486. return NRF_ERROR_INTERNAL;
  487. }
  488. m_module_initialized = true;
  489. return NRF_SUCCESS;
  490. }
  491. void sm_ble_evt_handler(ble_evt_t const * p_ble_evt)
  492. {
  493. NRF_PM_DEBUG_CHECK(p_ble_evt != NULL);
  494. smd_ble_evt_handler(p_ble_evt);
  495. #if PM_LESC_ENABLED
  496. nrf_ble_lesc_on_ble_evt(p_ble_evt);
  497. #endif
  498. (void) ble_conn_state_for_each_set_user_flag(m_flag_params_reply_pending_busy,
  499. params_reply_pending_handle,
  500. NULL);
  501. (void) ble_conn_state_for_each_set_user_flag(m_flag_link_secure_pending_busy,
  502. link_secure_pending_handle,
  503. NULL);
  504. }
  505. /**@brief Funtion for checking whether security parameters are valid.
  506. *
  507. * @param[out] p_sec_params The security parameters to verify.
  508. *
  509. * @return Whether the security parameters are valid.
  510. */
  511. static bool sec_params_verify(ble_gap_sec_params_t * p_sec_params)
  512. {
  513. // NULL check.
  514. if (p_sec_params == NULL)
  515. {
  516. return false;
  517. }
  518. // OOB not allowed unless MITM.
  519. if (!p_sec_params->mitm && p_sec_params->oob)
  520. {
  521. return false;
  522. }
  523. // IO Capabilities must be one of the valid values from @ref BLE_GAP_IO_CAPS.
  524. if (p_sec_params->io_caps > BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY)
  525. {
  526. return false;
  527. }
  528. // Must have either IO capabilities or OOB if MITM.
  529. if (p_sec_params->mitm && (p_sec_params->io_caps == BLE_GAP_IO_CAPS_NONE) && !p_sec_params->oob)
  530. {
  531. return false;
  532. }
  533. // Minimum key size cannot be larger than maximum key size.
  534. if (p_sec_params->min_key_size > p_sec_params->max_key_size)
  535. {
  536. return false;
  537. }
  538. // Key size cannot be below 7 bytes.
  539. if (p_sec_params->min_key_size < 7)
  540. {
  541. return false;
  542. }
  543. // Key size cannot be above 16 bytes.
  544. if (p_sec_params->max_key_size > 16)
  545. {
  546. return false;
  547. }
  548. // Signing is not supported.
  549. if (p_sec_params->kdist_own.sign || p_sec_params->kdist_peer.sign)
  550. {
  551. return false;
  552. }
  553. // link bit must be 0.
  554. if (p_sec_params->kdist_own.link || p_sec_params->kdist_peer.link)
  555. {
  556. return false;
  557. }
  558. // If bonding is not enabled, no keys can be distributed.
  559. if (!p_sec_params->bond && ( p_sec_params->kdist_own.enc
  560. || p_sec_params->kdist_own.id
  561. || p_sec_params->kdist_peer.enc
  562. || p_sec_params->kdist_peer.id))
  563. {
  564. return false;
  565. }
  566. // If bonding is enabled, one or more keys must be distributed.
  567. if ( p_sec_params->bond
  568. && !p_sec_params->kdist_own.enc
  569. && !p_sec_params->kdist_own.id
  570. && !p_sec_params->kdist_peer.enc
  571. && !p_sec_params->kdist_peer.id)
  572. {
  573. return false;
  574. }
  575. return true;
  576. }
  577. ret_code_t sm_sec_params_set(ble_gap_sec_params_t * p_sec_params)
  578. {
  579. NRF_PM_DEBUG_CHECK(m_module_initialized);
  580. if (p_sec_params == NULL)
  581. {
  582. mp_sec_params = NULL;
  583. m_sec_params_set = true;
  584. return NRF_SUCCESS;
  585. }
  586. else if (sec_params_verify(p_sec_params))
  587. {
  588. m_sec_params = *p_sec_params;
  589. mp_sec_params = &m_sec_params;
  590. m_sec_params_set = true;
  591. return NRF_SUCCESS;
  592. }
  593. else
  594. {
  595. return NRF_ERROR_INVALID_PARAM;
  596. }
  597. }
  598. void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config)
  599. {
  600. NRF_PM_DEBUG_CHECK(m_module_initialized);
  601. NRF_PM_DEBUG_CHECK(p_conn_sec_config != NULL);
  602. smd_conn_sec_config_reply(conn_handle, p_conn_sec_config);
  603. }
  604. ret_code_t sm_sec_params_reply(uint16_t conn_handle,
  605. ble_gap_sec_params_t * p_sec_params,
  606. void const * p_context)
  607. {
  608. NRF_PM_DEBUG_CHECK(m_module_initialized);
  609. VERIFY_PARAM_NOT_NULL(p_context);
  610. sec_params_reply_context_t * p_sec_params_reply_context = (sec_params_reply_context_t *)p_context;
  611. if (p_sec_params == NULL)
  612. {
  613. // Set the store pointer to NULL, so that NULL is passed to the SoftDevice.
  614. p_sec_params_reply_context->p_sec_params = NULL;
  615. }
  616. else if (sec_params_verify(p_sec_params))
  617. {
  618. // Copy the provided sec_params into the store.
  619. p_sec_params_reply_context->sec_params_mem = *p_sec_params;
  620. p_sec_params_reply_context->p_sec_params = &p_sec_params_reply_context->sec_params_mem;
  621. }
  622. else
  623. {
  624. return NRF_ERROR_INVALID_PARAM;
  625. }
  626. p_sec_params_reply_context->params_reply_called = true;
  627. return NRF_SUCCESS;
  628. }
  629. ret_code_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key)
  630. {
  631. NRF_PM_DEBUG_CHECK(m_module_initialized);
  632. #if PM_LESC_ENABLED
  633. return NRF_ERROR_FORBIDDEN;
  634. #else
  635. m_p_public_key = p_public_key;
  636. return NRF_SUCCESS;
  637. #endif // PM_LESC_ENABLED
  638. }
  639. ret_code_t sm_link_secure(uint16_t conn_handle, bool force_repairing)
  640. {
  641. ret_code_t ret;
  642. NRF_PM_DEBUG_CHECK(m_module_initialized);
  643. ret = link_secure(conn_handle, false, force_repairing, false);
  644. return ret;
  645. }
  646. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)