security_manager.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. /**@brief Function for sending an event based on error codes returned from @ref smd_link_secure or
  140. * @ref smd_params_reply.
  141. *
  142. * @param[in] conn_handle The connection the event pertains to.
  143. * @param[in] err_code The error code returned from @ref smd_link_secure or
  144. * @ref smd_params_reply.
  145. * @param[in] p_sec_params The security parameters attempted to pass in the call to
  146. * @ref smd_link_secure or @ref smd_params_reply.
  147. */
  148. static void events_send_from_err_code(uint16_t conn_handle,
  149. ret_code_t err_code,
  150. ble_gap_sec_params_t * p_sec_params)
  151. {
  152. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_INVALID_STATE))
  153. {
  154. pm_evt_t evt =
  155. {
  156. .conn_handle = conn_handle,
  157. .peer_id = im_peer_id_get_by_conn_handle(conn_handle),
  158. };
  159. if (err_code == NRF_ERROR_TIMEOUT)
  160. {
  161. NRF_LOG_WARNING("Cannot secure link because a previous security procedure ended in timeout. "\
  162. "Disconnect and retry. smd_params_reply() or smd_link_secure() returned "\
  163. "NRF_ERROR_TIMEOUT. conn_handle: %d",
  164. conn_handle);
  165. evt.evt_id = PM_EVT_CONN_SEC_FAILED;
  166. evt.params.conn_sec_failed.procedure = ((p_sec_params != NULL) && p_sec_params->bond)
  167. ? PM_CONN_SEC_PROCEDURE_BONDING
  168. : PM_CONN_SEC_PROCEDURE_PAIRING;
  169. evt.params.conn_sec_failed.error_src = BLE_GAP_SEC_STATUS_SOURCE_LOCAL;
  170. evt.params.conn_sec_failed.error = PM_CONN_SEC_ERROR_SMP_TIMEOUT;
  171. }
  172. else
  173. {
  174. NRF_LOG_ERROR("Could not perform security procedure. smd_params_reply() or "\
  175. "smd_link_secure() returned %s. conn_handle: %d",
  176. nrf_strerror_get(err_code),
  177. conn_handle);
  178. evt.evt_id = PM_EVT_ERROR_UNEXPECTED;
  179. evt.params.error_unexpected.error = err_code;
  180. evt.params.error_unexpected.fds_error = false;
  181. }
  182. evt_send(&evt);
  183. }
  184. }
  185. /**@brief Function for sending an PM_EVT_CONN_SEC_PARAMS_REQ event.
  186. *
  187. * @param[in] conn_handle The connection the event pertains to.
  188. * @param[in] p_peer_params The peer's security parameters to include in the event. Can be NULL.
  189. * @param[in] p_context Pointer to a context that the user must include in the call to @ref
  190. * sm_sec_params_reply().
  191. */
  192. static void params_req_send(uint16_t conn_handle,
  193. ble_gap_sec_params_t const * p_peer_params,
  194. sec_params_reply_context_t * p_context)
  195. {
  196. pm_evt_t evt;
  197. evt.evt_id = PM_EVT_CONN_SEC_PARAMS_REQ;
  198. evt.conn_handle = conn_handle;
  199. evt.peer_id = im_peer_id_get_by_conn_handle(conn_handle);
  200. evt.params.conn_sec_params_req.p_peer_params = p_peer_params;
  201. evt.params.conn_sec_params_req.p_context = p_context;
  202. evt_send(&evt);
  203. }
  204. /**@brief Function for creating a new @ref sec_params_reply_context_t with the correct initial values.
  205. *
  206. * @return The new context.
  207. */
  208. static sec_params_reply_context_t new_context_get(void)
  209. {
  210. sec_params_reply_context_t new_context =
  211. {
  212. .p_sec_params = mp_sec_params,
  213. .params_reply_called = false
  214. };
  215. return new_context;
  216. }
  217. /**@brief Internal function corresponding to @ref sm_link_secure.
  218. *
  219. * @param[in] conn_handle The connection to secure.
  220. * @param[in] null_params Whether to pass NULL security parameters to the security_dispatcher.
  221. * @param[in] force_repairing Whether to force rebonding if peer exists.
  222. * @param[in] send_events Whether to send events based on the result of @ref smd_link_secure.
  223. *
  224. * @return Same return codes as @ref sm_link_secure.
  225. */
  226. static ret_code_t link_secure(uint16_t conn_handle,
  227. bool null_params,
  228. bool force_repairing,
  229. bool send_events)
  230. {
  231. ret_code_t err_code;
  232. ret_code_t return_err_code;
  233. ble_gap_sec_params_t * p_sec_params;
  234. if (null_params)
  235. {
  236. p_sec_params = NULL;
  237. }
  238. else
  239. {
  240. sec_params_reply_context_t context = new_context_get();
  241. params_req_send(conn_handle, NULL, &context);
  242. p_sec_params = context.p_sec_params;
  243. if (!m_sec_params_set && !context.params_reply_called)
  244. {
  245. // Security parameters have not been set.
  246. return NRF_ERROR_NOT_FOUND;
  247. }
  248. }
  249. err_code = smd_link_secure(conn_handle, p_sec_params, force_repairing);
  250. flags_set_from_err_code(conn_handle, err_code, false);
  251. switch (err_code)
  252. {
  253. case NRF_ERROR_BUSY:
  254. ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_null_params, null_params);
  255. ble_conn_state_user_flag_set(conn_handle, m_flag_link_secure_force_repairing, force_repairing);
  256. return_err_code = NRF_SUCCESS;
  257. break;
  258. case NRF_SUCCESS:
  259. case NRF_ERROR_TIMEOUT:
  260. case BLE_ERROR_INVALID_CONN_HANDLE:
  261. case NRF_ERROR_INVALID_STATE:
  262. case NRF_ERROR_INVALID_DATA:
  263. return_err_code = err_code;
  264. break;
  265. default:
  266. NRF_LOG_ERROR("Could not perform security procedure. smd_link_secure() returned %s. "\
  267. "conn_handle: %d",
  268. nrf_strerror_get(err_code),
  269. conn_handle);
  270. return_err_code = NRF_ERROR_INTERNAL;
  271. break;
  272. }
  273. if (send_events)
  274. {
  275. events_send_from_err_code(conn_handle, err_code, p_sec_params);
  276. }
  277. return return_err_code;
  278. }
  279. /**@brief Function for requesting security parameters from the user and passing them to the security_dispatcher.
  280. *
  281. * @param[in] conn_handle The connection that needs security parameters.
  282. * @param[in] p_peer_params The peer's security parameters if present. Otherwise NULL.
  283. */
  284. static void smd_params_reply_perform(uint16_t conn_handle, ble_gap_sec_params_t const * p_peer_params)
  285. {
  286. ret_code_t err_code;
  287. ble_gap_lesc_p256_pk_t * p_public_key;
  288. sec_params_reply_context_t context = new_context_get();
  289. params_req_send(conn_handle, p_peer_params, &context);
  290. #if PM_LESC_ENABLED
  291. p_public_key = nrf_ble_lesc_public_key_get();
  292. #else
  293. p_public_key = m_p_public_key;
  294. #endif // PM_LESC_ENABLED
  295. err_code = smd_params_reply(conn_handle, context.p_sec_params, p_public_key);
  296. flags_set_from_err_code(conn_handle, err_code, true);
  297. events_send_from_err_code(conn_handle, err_code, context.p_sec_params);
  298. }
  299. /**@brief Function for handling @ref PM_EVT_CONN_SEC_PARAMS_REQ events.
  300. *
  301. * @param[in] p_event The @ref PM_EVT_CONN_SEC_PARAMS_REQ event.
  302. */
  303. static __INLINE void params_req_process(pm_evt_t const * p_event)
  304. {
  305. smd_params_reply_perform(p_event->conn_handle, p_event->params.conn_sec_params_req.p_peer_params);
  306. }
  307. /**@brief Function for handling @ref PM_EVT_SLAVE_SECURITY_REQ events.
  308. *
  309. * @param[in] p_event The @ref PM_EVT_SLAVE_SECURITY_REQ event.
  310. */
  311. static void sec_req_process(pm_evt_t const * p_event)
  312. {
  313. bool null_params = false;
  314. if (mp_sec_params == NULL)
  315. {
  316. null_params = true;
  317. }
  318. else if ((bool)m_sec_params.bond < (bool)p_event->params.slave_security_req.bond)
  319. {
  320. null_params = true;
  321. }
  322. else if ((bool)m_sec_params.mitm < (bool)p_event->params.slave_security_req.mitm)
  323. {
  324. null_params = true;
  325. }
  326. else
  327. {
  328. // No action.
  329. }
  330. ret_code_t err_code = link_secure(p_event->conn_handle, null_params, false, true);
  331. UNUSED_VARIABLE(err_code); // It is acceptable to ignore the return code because it is
  332. // acceptable to ignore a security request.
  333. }
  334. /**@brief Function for translating an SMD event to an SM event and passing it on to SM event handlers.
  335. *
  336. * @param[in] p_event The event to forward.
  337. */
  338. static void evt_forward(pm_evt_t * p_event)
  339. {
  340. evt_send(p_event);
  341. }
  342. /**@brief Event handler for events from the Security Dispatcher module.
  343. * This handler is extern in Security Dispatcher.
  344. *
  345. * @param[in] p_event The event that has happened.
  346. */
  347. void sm_smd_evt_handler(pm_evt_t * p_event)
  348. {
  349. switch (p_event->evt_id)
  350. {
  351. case PM_EVT_CONN_SEC_PARAMS_REQ:
  352. params_req_process(p_event);
  353. break;
  354. case PM_EVT_SLAVE_SECURITY_REQ:
  355. sec_req_process(p_event);
  356. /* fallthrough */
  357. default:
  358. // Forward the event to all registered Security Manager event handlers.
  359. evt_forward(p_event);
  360. break;
  361. }
  362. }
  363. /**@brief Function handling a pending params_reply. See @ref ble_conn_state_user_function_t.
  364. */
  365. static void params_reply_pending_handle(uint16_t conn_handle, void * p_context)
  366. {
  367. UNUSED_PARAMETER(p_context);
  368. smd_params_reply_perform(conn_handle, NULL);
  369. }
  370. /**@brief Function handling a pending link_secure. See @ref ble_conn_state_user_function_t.
  371. */
  372. static void link_secure_pending_handle(uint16_t conn_handle, void * p_context)
  373. {
  374. UNUSED_PARAMETER(p_context);
  375. bool force_repairing = ble_conn_state_user_flag_get(conn_handle, m_flag_link_secure_force_repairing);
  376. bool null_params = ble_conn_state_user_flag_get(conn_handle, m_flag_link_secure_null_params);
  377. // If this fails, it will be automatically retried.
  378. ret_code_t err_code = link_secure(conn_handle, null_params, force_repairing, true);
  379. UNUSED_VARIABLE(err_code);
  380. }
  381. /**@brief Event handler for events from the Peer Database module.
  382. * This handler is extern in Peer Database.
  383. *
  384. * @param[in] p_event The event that has happened.
  385. */
  386. void sm_pdb_evt_handler(pm_evt_t * p_event)
  387. {
  388. switch (p_event->evt_id)
  389. {
  390. case PM_EVT_FLASH_GARBAGE_COLLECTED:
  391. case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
  392. case PM_EVT_PEER_DATA_UPDATE_FAILED:
  393. case PM_EVT_PEER_DELETE_SUCCEEDED:
  394. case PM_EVT_PEER_DELETE_FAILED:
  395. (void) ble_conn_state_for_each_set_user_flag(m_flag_params_reply_pending_busy,
  396. params_reply_pending_handle,
  397. NULL);
  398. (void) ble_conn_state_for_each_set_user_flag(m_flag_link_secure_pending_busy,
  399. link_secure_pending_handle,
  400. NULL);
  401. break;
  402. default:
  403. // Do nothing.
  404. break;
  405. }
  406. }
  407. /**@brief Funtion for initializing a BLE Connection State user flag.
  408. *
  409. * @param[out] flag_id The flag to initialize.
  410. */
  411. static void flag_id_init(ble_conn_state_user_flag_id_t * p_flag_id)
  412. {
  413. if (*p_flag_id == BLE_CONN_STATE_USER_FLAG_INVALID)
  414. {
  415. *p_flag_id = ble_conn_state_user_flag_acquire();
  416. }
  417. }
  418. ret_code_t sm_init(void)
  419. {
  420. NRF_PM_DEBUG_CHECK(!m_module_initialized);
  421. #if PM_LESC_ENABLED
  422. ret_code_t err_code = nrf_ble_lesc_init();
  423. if (err_code != NRF_SUCCESS)
  424. {
  425. return err_code;
  426. }
  427. #endif
  428. flag_id_init(&m_flag_link_secure_pending_busy);
  429. flag_id_init(&m_flag_link_secure_force_repairing);
  430. flag_id_init(&m_flag_link_secure_null_params);
  431. flag_id_init(&m_flag_params_reply_pending_busy);
  432. if (m_flag_params_reply_pending_busy == BLE_CONN_STATE_USER_FLAG_INVALID)
  433. {
  434. NRF_LOG_ERROR("Could not acquire conn_state user flags. Increase "\
  435. "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module.");
  436. return NRF_ERROR_INTERNAL;
  437. }
  438. m_module_initialized = true;
  439. return NRF_SUCCESS;
  440. }
  441. void sm_ble_evt_handler(ble_evt_t const * p_ble_evt)
  442. {
  443. NRF_PM_DEBUG_CHECK(p_ble_evt != NULL);
  444. smd_ble_evt_handler(p_ble_evt);
  445. #if PM_LESC_ENABLED
  446. nrf_ble_lesc_on_ble_evt(p_ble_evt);
  447. #endif
  448. (void) ble_conn_state_for_each_set_user_flag(m_flag_params_reply_pending_busy,
  449. params_reply_pending_handle,
  450. NULL);
  451. (void) ble_conn_state_for_each_set_user_flag(m_flag_link_secure_pending_busy,
  452. link_secure_pending_handle,
  453. NULL);
  454. }
  455. /**@brief Funtion for checking whether security parameters are valid.
  456. *
  457. * @param[out] p_sec_params The security parameters to verify.
  458. *
  459. * @return Whether the security parameters are valid.
  460. */
  461. static bool sec_params_verify(ble_gap_sec_params_t * p_sec_params)
  462. {
  463. // NULL check.
  464. if (p_sec_params == NULL)
  465. {
  466. return false;
  467. }
  468. // OOB not allowed unless MITM.
  469. if (!p_sec_params->mitm && p_sec_params->oob)
  470. {
  471. return false;
  472. }
  473. // IO Capabilities must be one of the valid values from @ref BLE_GAP_IO_CAPS.
  474. if (p_sec_params->io_caps > BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY)
  475. {
  476. return false;
  477. }
  478. // Must have either IO capabilities or OOB if MITM.
  479. if (p_sec_params->mitm && (p_sec_params->io_caps == BLE_GAP_IO_CAPS_NONE) && !p_sec_params->oob)
  480. {
  481. return false;
  482. }
  483. // Minimum key size cannot be larger than maximum key size.
  484. if (p_sec_params->min_key_size > p_sec_params->max_key_size)
  485. {
  486. return false;
  487. }
  488. // Key size cannot be below 7 bytes.
  489. if (p_sec_params->min_key_size < 7)
  490. {
  491. return false;
  492. }
  493. // Key size cannot be above 16 bytes.
  494. if (p_sec_params->max_key_size > 16)
  495. {
  496. return false;
  497. }
  498. // Signing is not supported.
  499. if (p_sec_params->kdist_own.sign || p_sec_params->kdist_peer.sign)
  500. {
  501. return false;
  502. }
  503. // link bit must be 0.
  504. if (p_sec_params->kdist_own.link || p_sec_params->kdist_peer.link)
  505. {
  506. return false;
  507. }
  508. // If bonding is not enabled, no keys can be distributed.
  509. if (!p_sec_params->bond && ( p_sec_params->kdist_own.enc
  510. || p_sec_params->kdist_own.id
  511. || p_sec_params->kdist_peer.enc
  512. || p_sec_params->kdist_peer.id))
  513. {
  514. return false;
  515. }
  516. // If bonding is enabled, one or more keys must be distributed.
  517. if ( p_sec_params->bond
  518. && !p_sec_params->kdist_own.enc
  519. && !p_sec_params->kdist_own.id
  520. && !p_sec_params->kdist_peer.enc
  521. && !p_sec_params->kdist_peer.id)
  522. {
  523. return false;
  524. }
  525. return true;
  526. }
  527. ret_code_t sm_sec_params_set(ble_gap_sec_params_t * p_sec_params)
  528. {
  529. NRF_PM_DEBUG_CHECK(m_module_initialized);
  530. if (p_sec_params == NULL)
  531. {
  532. mp_sec_params = NULL;
  533. m_sec_params_set = true;
  534. return NRF_SUCCESS;
  535. }
  536. else if (sec_params_verify(p_sec_params))
  537. {
  538. m_sec_params = *p_sec_params;
  539. mp_sec_params = &m_sec_params;
  540. m_sec_params_set = true;
  541. return NRF_SUCCESS;
  542. }
  543. else
  544. {
  545. return NRF_ERROR_INVALID_PARAM;
  546. }
  547. }
  548. void sm_conn_sec_config_reply(uint16_t conn_handle, pm_conn_sec_config_t * p_conn_sec_config)
  549. {
  550. NRF_PM_DEBUG_CHECK(m_module_initialized);
  551. NRF_PM_DEBUG_CHECK(p_conn_sec_config != NULL);
  552. smd_conn_sec_config_reply(conn_handle, p_conn_sec_config);
  553. }
  554. ret_code_t sm_sec_params_reply(uint16_t conn_handle,
  555. ble_gap_sec_params_t * p_sec_params,
  556. void const * p_context)
  557. {
  558. NRF_PM_DEBUG_CHECK(m_module_initialized);
  559. VERIFY_PARAM_NOT_NULL(p_context);
  560. sec_params_reply_context_t * p_sec_params_reply_context = (sec_params_reply_context_t *)p_context;
  561. if (p_sec_params == NULL)
  562. {
  563. // Set the store pointer to NULL, so that NULL is passed to the SoftDevice.
  564. p_sec_params_reply_context->p_sec_params = NULL;
  565. }
  566. else if (sec_params_verify(p_sec_params))
  567. {
  568. // Copy the provided sec_params into the store.
  569. p_sec_params_reply_context->sec_params_mem = *p_sec_params;
  570. p_sec_params_reply_context->p_sec_params = &p_sec_params_reply_context->sec_params_mem;
  571. }
  572. else
  573. {
  574. return NRF_ERROR_INVALID_PARAM;
  575. }
  576. p_sec_params_reply_context->params_reply_called = true;
  577. return NRF_SUCCESS;
  578. }
  579. ret_code_t sm_lesc_public_key_set(ble_gap_lesc_p256_pk_t * p_public_key)
  580. {
  581. NRF_PM_DEBUG_CHECK(m_module_initialized);
  582. #if PM_LESC_ENABLED
  583. return NRF_ERROR_FORBIDDEN;
  584. #else
  585. m_p_public_key = p_public_key;
  586. return NRF_SUCCESS;
  587. #endif // PM_LESC_ENABLED
  588. }
  589. ret_code_t sm_link_secure(uint16_t conn_handle, bool force_repairing)
  590. {
  591. ret_code_t ret;
  592. NRF_PM_DEBUG_CHECK(m_module_initialized);
  593. ret = link_secure(conn_handle, false, force_repairing, false);
  594. return ret;
  595. }
  596. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)