<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/80693/ble-man-in-the-middle-protection-multiple-passkeys</link><description>Hello all, 
 I am developing a BLE application with the Softdevices, the peer management module, and with MITM protection. 
 I&amp;#39;ve set a bonding static passkey as shown here: 
 
 I want to set a general static passkey to be used for maintenance people</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 19 Oct 2021 13:25:21 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/80693/ble-man-in-the-middle-protection-multiple-passkeys" /><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334905?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 13:25:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef309753-6b87-4d69-a14e-d3ab995de3b5</guid><dc:creator>pedrovfr_LNNano</dc:creator><description>&lt;p&gt;Ok, I see now where the issue is. Also great tip, thanks for the assistance!&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/80693/ble-man-in-the-middle-protection-multiple-passkeys/334887#334887"]&lt;p&gt;nstance like this:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div class="evolution-code-editor theme-clouds"&gt;&lt;a href="#" class="fullscreen"&gt;Fullscreen&lt;/a&gt;&lt;div style="width:100%;height:28px;" class=" ace_editor ace-clouds"&gt;&lt;/div&gt;&lt;/div&gt;[/quote]
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334887?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 13:04:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3be2b53c-de2c-4664-b61b-27ba87f46fa6</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="pedrovfr_LNNano"]Does it means the peer is disconnecting?[/quote]
&lt;p&gt;Yes, you see here that the local host terminated the connection (which refers to the Android device in the Android log). On the nRF side I expect you see that the remote user terminated the connection. I do not believe you can do anything about this on Android (or iOS) devices.&lt;/p&gt;
[quote user="pedrovfr_LNNano"]Also, is there a better way to see the reasons from the SD events?[/quote]
&lt;p&gt;Yes. When&amp;nbsp; you get the&amp;nbsp;BLE_GAP_EVT_DISCONNECTED you can check the reason and print it for instance like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;            NRF_LOG_INFO(&amp;quot;Disconnected, reason %d.&amp;quot;,
                          p_ble_evt-&amp;gt;evt.gap_evt.params.disconnected.reason);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The number get here represents a standard&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.3.0/group___b_l_e___h_c_i___s_t_a_t_u_s___c_o_d_e_s.html"&gt;&lt;span&gt;Bluetooth&lt;/span&gt;&lt;/a&gt;&lt;span&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s140.api.v7.3.0/group___b_l_e___h_c_i___s_t_a_t_u_s___c_o_d_e_s.html"&gt;&amp;nbsp;status&amp;nbsp;code&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334880?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 12:56:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e53fcfb3-bfe5-4e8b-87e4-d3bd3443960f</guid><dc:creator>pedrovfr_LNNano</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;My peer is an Android 10 motorola smartphone using the nRF Connect mobile app. I got the log there:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;D 09:39:52.754 gatt.setCharacteristicNotification(00002aa7-0000-1000-8000-00805f9b34fb, true)
D 09:39:52.756 gatt.setCharacteristicNotification(00002a52-0000-1000-8000-00805f9b34fb, true)
D 09:39:52.757 gatt.setCharacteristicNotification(00002aac-0000-1000-8000-00805f9b34fb, true)
D 09:39:52.759 gatt.setCharacteristicNotification(00002a19-0000-1000-8000-00805f9b34fb, true)
I 09:39:52.816 Connection parameters updated (interval: 48.75ms, latency: 0, timeout: 5000ms)
D 09:39:54.983 [Broadcast] Action received: android.bluetooth.device.action.BOND_STATE_CHANGED, bond state changed to: BOND_BONDING (11)
D 09:39:55.010 [Broadcast] Action received: android.bluetooth.device.action.PAIRING_REQUEST, pairing variant: PAIRING_VARIANT_CONSENT (3)
D 09:39:57.268 [Broadcast] Action received: android.bluetooth.device.action.PAIRING_REQUEST, pairing variant: PAIRING_VARIANT_PIN (0)
D 09:40:06.620 [Broadcast] Action received: android.bluetooth.device.action.BOND_STATE_CHANGED, bond state changed to: BOND_NONE (10)
I 09:40:06.620 Bonding failed
I 09:40:06.903 Connection parameters updated (interval: 498.75ms, latency: 0, timeout: 4000ms)
D 09:40:10.056 [Callback] Connection state changed with status: 22 and new state: DISCONNECTED (0)
E 09:40:10.056 Error 22 (0x16): GATT CONN TERMINATE LOCAL HOST
I 09:40:10.056 Disconnected
D 09:40:10.204 [Broadcast] Action received: android.bluetooth.device.action.ACL_DISCONNECTED&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Does it means the peer is disconnecting?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/80693/ble-man-in-the-middle-protection-multiple-passkeys/334871#334871"]From the log we only see that you get BLE event 0x11, which is&amp;nbsp;BLE_GAP_EVT_DISCONNECTED, but not why. Can you print the disconnect reason? [/quote]
&lt;p&gt;I am not sure how to print the reason on the application, could track only the event from the Soft Devices using a breakpoint:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1634648034763v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Also, is there a better way to see the reasons from the SD events?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334871?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 12:34:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9089afb9-f3be-40cf-b235-43e7d9b67ae3</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;From the log we only see that you get BLE event 0x11, which is&amp;nbsp;BLE_GAP_EVT_DISCONNECTED, but not why. Can you print the disconnect reason? It is not unlikely that the peer disconnects when the pairing fails. If so, you need to modify the behavior of the peer in order to prevent the disconnect (if possible).&amp;nbsp;What is the peer device her?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334865?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 12:26:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ea3c46b0-2424-424d-9720-12b921afd3c9</guid><dc:creator>pedrovfr_LNNano</dc:creator><description>&lt;p&gt;Hello, thanks for the reply.&lt;/p&gt;
[quote userid="7377" url="~/f/nordic-q-a/80693/ble-man-in-the-middle-protection-multiple-passkeys/334748#334748"]The SDK examples and peer manager module will disconnect on failure by calling&amp;nbsp;pm_handler_disconnect_on_sec_failure(). But this is really up to you, if you do not want to disconnect on failure do not call this in your peer manager event handler, and instead handle the&amp;nbsp;PM_EVT_CONN_SEC_FAILED event in a different way that fits your application.[/quote]
&lt;p&gt;I have done that, but it still disconnects:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Function for handling Peer Manager events.
 *
 * @param[in] p_evt  Peer Manager event.
 */
static void pm_evt_handler(pm_evt_t const * p_evt)
{
    ret_code_t err_code;
    pm_handler_on_pm_evt(p_evt);
    //pm_handler_disconnect_on_sec_failure(p_evt);
    pm_handler_flash_clean(p_evt);

    switch (p_evt-&amp;gt;evt_id)
    {
        case PM_EVT_PEER_DELETE_SUCCEEDED:
            if (!delete_bonds_pending() &amp;amp;&amp;amp; !delete_all_pending)
            {
                // No more peers are flagged for deletion and we are not going to delete all peers.
                advertising_start(false);
            }
            break;
        case  PM_EVT_CONN_SEC_FAILED:

            printf(&amp;quot;FAILED BONDING&amp;quot;);
                m_static_pin_option.gap_opt.passkey.p_passkey = ADMIN_PASSKEY;
                err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &amp;amp;m_static_pin_option);
                APP_ERROR_CHECK(err_code);
           
            break;

        case PM_EVT_PEERS_DELETE_SUCCEEDED:
            delete_all_pending = false;
            advertising_start(false);
            break;

        case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
            if (     p_evt-&amp;gt;params.peer_data_update_succeeded.flash_changed
                 &amp;amp;&amp;amp; (p_evt-&amp;gt;params.peer_data_update_succeeded.data_id == PM_PEER_DATA_ID_BONDING))
            {
                NRF_LOG_INFO(&amp;quot;New Bond, add the peer to the whitelist if possible&amp;quot;);
                // Note: You should check on what kind of white list policy your application should use.

                whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR);
            }
            break;
                    /** @snippet [NFC Pairing Lib usage_1] */
          case PM_EVT_CONN_SEC_PARAMS_REQ:
          {
              // Send event to the NFC BLE pairing library as it may dynamically alternate
              // security parameters to achieve highest possible security level.
              
   //           APP_ERROR_CHECK(nfc_ble_pair_on_pm_params_req(p_evt));
          } break;
          /** @snippet [NFC Pairing Lib usage_1] */

            
        default:
            break;
    }
}&lt;/pre&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here the debug terminal during a bonding attempt:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;&amp;lt;debug&amp;gt; app: pm_whitelist_get returns 0 addr in whitelist and 0 irk whitelist
&amp;lt;info&amp;gt; app: Fast advertising.
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x10.
&amp;lt;info&amp;gt; app: Connected
&amp;lt;debug&amp;gt; nrf_ble_gq: Registering connection handle: 0x0000
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x12.
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x12.
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x13.
&amp;lt;info&amp;gt; peer_manager_handler: Connection security procedure started: role: Peripheral, conn_handle: 0, procedure: Bonding
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x15.
&amp;lt;info&amp;gt; app: Passkey: 123456
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x12.
FAILED BONDING&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x19.
&amp;lt;info&amp;gt; peer_manager_handler: Connection security failed: role: Peripheral, conn_handle: 0x0, procedure: Bonding, error: 132
&amp;lt;debug&amp;gt; nrf_sdh_ble: BLE event: 0x11.
&amp;lt;info&amp;gt; app: Disconnected&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am receiving the BLE event BLE_GAP_EVT_DISCONNECTED in the ble event handler:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code = NRF_SUCCESS;
    pm_handler_secure_on_connection(p_ble_evt);
    /////PEDROMOD
    pm_handler_secure_on_error(p_ble_evt);
    ////////////
    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected&amp;quot;);
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
            m_conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
            err_code = nrf_ble_bms_set_conn_handle(&amp;amp;m_bms, m_conn_handle);
            APP_ERROR_CHECK(err_code);
            err_code = nrf_ble_qwr_conn_handle_assign(&amp;amp;m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);
            err_code = nrf_ble_cgms_conn_handle_assign(&amp;amp;m_cgms, m_conn_handle);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected&amp;quot;);
            if (delete_bonds_pending())
            {
                // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED or PM_EVT_PEERS_DELETE_SUCCEEDED event.
                delete_disconnected_bonds();
            }
            else
            {
                advertising_start(false);
            }
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            //cgm_stop = true;
            break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
            NRF_LOG_DEBUG(&amp;quot;PHY update request.&amp;quot;);
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            err_code = sd_ble_gap_phy_update(p_ble_evt-&amp;gt;evt.gap_evt.conn_handle, &amp;amp;phys);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GATTC_EVT_TIMEOUT:
            // Disconnect on GATT Client timeout event.
            NRF_LOG_DEBUG(&amp;quot;GATT Client Timeout.&amp;quot;);
            err_code = sd_ble_gap_disconnect(p_ble_evt-&amp;gt;evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server timeout event.
            NRF_LOG_DEBUG(&amp;quot;GATT Server Timeout.&amp;quot;);
            err_code = sd_ble_gap_disconnect(p_ble_evt-&amp;gt;evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;
        case BLE_GAP_EVT_PASSKEY_DISPLAY:
        {
            char passkey[PASSKEY_LENGTH + 1];
            memcpy(passkey, p_ble_evt-&amp;gt;evt.gap_evt.params.passkey_display.passkey, PASSKEY_LENGTH);
            passkey[PASSKEY_LENGTH] = 0;

            NRF_LOG_INFO(&amp;quot;Passkey: %s&amp;quot;, nrf_log_push(passkey));
        } break;
        case BLE_GAP_EVT_AUTH_KEY_REQUEST :
        {

            printf(&amp;quot;KEY REQUEST&amp;quot;);
        } break;
        default:
            // No implementation needed.
            break;
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Still not sure how the soft devices is handling the passkey, but once it doesn&amp;#39;t match the connection is ended. Any ideias how to workaround this?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334748?ContentTypeID=1</link><pubDate>Tue, 19 Oct 2021 07:34:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7a43c933-70b7-49a7-bcc6-22fc6aa03b7a</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="pedrovfr_LNNano"]Can you suggest me how to implement the handle swapping in the application?[/quote]
&lt;p&gt;You just call&amp;nbsp;sd_ble_opt_set() again when you want to change the key. When that is will be application dependent so that is up to you.&lt;/p&gt;
[quote user="pedrovfr_LNNano"]I though setting a new passkey using the sd_ble_opt_set() after the first attempt be fail, but to do this I wish the continue on the connection and retry without the peer be informed of that. But I just found after the miss value on the passkey the SoftDevices disconnects and return BLE_GAP_EVT_AUTH_STATUS not giving me the chance to authenticate manually before it disconnects.[/quote]
&lt;p&gt;The SDK examples and peer manager module will disconnect on failure by calling&amp;nbsp;pm_handler_disconnect_on_sec_failure(). But this is really up to you, if you do not want to disconnect on failure do not call this in your peer manager event handler, and instead handle the&amp;nbsp;PM_EVT_CONN_SEC_FAILED event in a different way that fits your application.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334685?ContentTypeID=1</link><pubDate>Mon, 18 Oct 2021 17:34:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:20f7a07a-f659-42bf-8470-24b7406db0be</guid><dc:creator>pedrovfr_LNNano</dc:creator><description>&lt;p&gt;Thanks for the reply.&lt;/p&gt;
&lt;p&gt;Can you suggest me how to implement the handle swapping in the application?&lt;/p&gt;
&lt;p&gt;I though setting a new passkey using the sd_ble_opt_set() after the first attempt be fail, but to do this I wish the continue on the connection and retry without the peer be informed of that. But I just found after the miss value on the passkey the SoftDevices disconnects and return BLE_GAP_EVT_AUTH_STATUS not giving me the chance to authenticate manually before it disconnects.&lt;/p&gt;
&lt;p&gt;Any idea how to solve this case will be much appreciated!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: BLE Man-in-the-middle protection, multiple passkeys</title><link>https://devzone.nordicsemi.com/thread/334289?ContentTypeID=1</link><pubDate>Fri, 15 Oct 2021 09:11:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c1ae311b-3288-4161-9708-6cf2fd2f3d20</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You cannot configure more than one passkey at a time, but you can call sd_ble_opt_set() with a new pass key. So you would have to keep the array in your application, and handle swapping between the keys yourself.&lt;/p&gt;
&lt;p&gt;Note that&amp;nbsp;because of the way the passkey is&amp;nbsp;exchanged in BLE using static passkey is not secure, as it can be brute forced quite fast (particularly&amp;nbsp;with LE Secure Connections).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>