xiaoxiang781216 commented on code in PR #16364:
URL: https://github.com/apache/nuttx/pull/16364#discussion_r2106273345


##########
wireless/bluetooth/bt_smp.c:
##########
@@ -71,6 +71,16 @@
 
 /* SMP channel specific context */
 
+enum pairing_method

Review Comment:
   ```suggestion
   enum smp_pairing_method_e
   ```



##########
wireless/bluetooth/bt_smp.c:
##########
@@ -576,17 +637,79 @@ static int smp_init(struct bt_smp_s *smp)
   return 0;
 }
 
+static enum pairing_method smp_get_pairing_method(uint8_t local_io,
+                                                  uint8_t remote_io,
+                                                  uint8_t local_auth,
+                                                  uint8_t remote_auth)
+{
+  bool local_mitm = (local_auth & BT_SMP_AUTH_MITM);
+  bool remote_mitm = (remote_auth & BT_SMP_AUTH_MITM);
+  bool mitm_requested = local_mitm || remote_mitm;
+
+  wlinfo("Local IO: %d, Remote IO: %d, MITM Req: %d\n", local_io, remote_io,
+      mitm_requested);
+
+  /* Mapping based on Core Spec v4.2, Vol 3, Part H, Table 2.8
+   * and Figure 2.7 flow
+   */
+
+  switch (local_io)
+    {
+      case BT_SMP_IO_DISPLAY_ONLY:
+        switch (remote_io)
+          {
+            case BT_SMP_IO_DISPLAY_ONLY:
+            case BT_SMP_IO_NO_INPUT_OUTPUT:
+              return PAIRING_METHOD_JUST_WORKS;
+            case BT_SMP_IO_KEYBOARD_ONLY:
+            case BT_SMP_IO_KEYBOARD_DISPLAY:
+            case BT_SMP_IO_DISPLAY_YESNO:
+              return mitm_requested ? PAIRING_METHOD_PASSKEY_DISPLAY
+                                    : PAIRING_METHOD_JUST_WORKS;
+            default:
+              return PAIRING_METHOD_NOT_SUPPORTED;
+          }
+        break;
+      case BT_SMP_IO_NO_INPUT_OUTPUT:
+        return PAIRING_METHOD_JUST_WORKS; /* Cannot support MITM */
+      default:
+        wlwarn("Unhandled local IO Cap %d\n", local_io);
+        return PAIRING_METHOD_JUST_WORKS;
+    }
+}
+
+static bool method_provides_mitm(enum pairing_method method)
+{
+  return (method == PAIRING_METHOD_PASSKEY_DISPLAY ||
+        method == PAIRING_METHOD_PASSKEY_INPUT);
+}
+
+static void smp_passkey_to_tk(uint32_t passkey, uint8_t *tk)
+{
+  memset(tk, 0, 16);
+  tk[0] = passkey & 0xff;
+  tk[1] = (passkey >> 8) & 0xff;
+  tk[2] = (passkey >> 16) & 0xff;
+  tk[3] = (passkey >> 24) & 0xff;
+}
+
 static uint8_t smp_pairing_req(FAR struct bt_conn_s *conn,
                                FAR struct bt_buf_s *buf)
 {
   FAR struct bt_smp_pairing_s *req = (FAR void *)buf->data;
   FAR struct bt_smp_pairing_s *rsp;
   FAR struct bt_buf_s *rsp_buf;
   FAR struct bt_smp_s *smp = conn->smp;
-  uint8_t auth;
   int ret;

Review Comment:
   move after line 705



##########
wireless/bluetooth/bt_smp.h:
##########
@@ -163,6 +163,29 @@ begin_packed_struct struct bt_smp_security_request_s
   uint8_t auth_req;
 } end_packed_struct;
 
+begin_packed_struct struct bt_smp_auth_cb_s

Review Comment:
   why need patch the internal structure



##########
wireless/bluetooth/bt_smp.c:
##########
@@ -1062,30 +1401,49 @@ static uint8_t smp_security_request(FAR struct 
bt_conn_s *conn,
 {
   FAR struct bt_smp_security_request_s *req = (FAR void *)buf->data;
   FAR struct bt_keys_s *keys;
-  uint8_t auth;
+  uint8_t slave_auth_req = req->auth_req & BT_SMP_AUTH_MASK;
 
-  wlinfo("\n");
+  wlinfo("Security Request received (req=0x%02x)\n", slave_auth_req);
 
   keys = bt_keys_find(BT_KEYS_LTK, &conn->dst);
-  if (!keys)
+  if (keys)
     {
-      goto pair;
-    }
+      wlinfo("Found existing LTK (level=%d)\n", keys->ltk.level);

Review Comment:
   move after line 1414



##########
wireless/bluetooth/bt_smp.c:
##########
@@ -576,17 +637,79 @@ static int smp_init(struct bt_smp_s *smp)
   return 0;
 }
 
+static enum pairing_method smp_get_pairing_method(uint8_t local_io,
+                                                  uint8_t remote_io,
+                                                  uint8_t local_auth,
+                                                  uint8_t remote_auth)
+{
+  bool local_mitm = (local_auth & BT_SMP_AUTH_MITM);
+  bool remote_mitm = (remote_auth & BT_SMP_AUTH_MITM);
+  bool mitm_requested = local_mitm || remote_mitm;
+
+  wlinfo("Local IO: %d, Remote IO: %d, MITM Req: %d\n", local_io, remote_io,
+      mitm_requested);
+
+  /* Mapping based on Core Spec v4.2, Vol 3, Part H, Table 2.8
+   * and Figure 2.7 flow
+   */
+
+  switch (local_io)
+    {
+      case BT_SMP_IO_DISPLAY_ONLY:
+        switch (remote_io)
+          {
+            case BT_SMP_IO_DISPLAY_ONLY:
+            case BT_SMP_IO_NO_INPUT_OUTPUT:
+              return PAIRING_METHOD_JUST_WORKS;
+            case BT_SMP_IO_KEYBOARD_ONLY:
+            case BT_SMP_IO_KEYBOARD_DISPLAY:
+            case BT_SMP_IO_DISPLAY_YESNO:
+              return mitm_requested ? PAIRING_METHOD_PASSKEY_DISPLAY
+                                    : PAIRING_METHOD_JUST_WORKS;
+            default:
+              return PAIRING_METHOD_NOT_SUPPORTED;
+          }
+        break;
+      case BT_SMP_IO_NO_INPUT_OUTPUT:
+        return PAIRING_METHOD_JUST_WORKS; /* Cannot support MITM */
+      default:
+        wlwarn("Unhandled local IO Cap %d\n", local_io);
+        return PAIRING_METHOD_JUST_WORKS;
+    }
+}
+
+static bool method_provides_mitm(enum pairing_method method)

Review Comment:
   ```suggestion
   static bool smp_mitm_supported(enum pairing_method method)
   ```



##########
wireless/bluetooth/bt_smp.c:
##########
@@ -576,17 +637,79 @@ static int smp_init(struct bt_smp_s *smp)
   return 0;
 }
 
+static enum pairing_method smp_get_pairing_method(uint8_t local_io,
+                                                  uint8_t remote_io,
+                                                  uint8_t local_auth,
+                                                  uint8_t remote_auth)
+{
+  bool local_mitm = (local_auth & BT_SMP_AUTH_MITM);
+  bool remote_mitm = (remote_auth & BT_SMP_AUTH_MITM);
+  bool mitm_requested = local_mitm || remote_mitm;
+
+  wlinfo("Local IO: %d, Remote IO: %d, MITM Req: %d\n", local_io, remote_io,
+      mitm_requested);
+
+  /* Mapping based on Core Spec v4.2, Vol 3, Part H, Table 2.8
+   * and Figure 2.7 flow
+   */
+
+  switch (local_io)
+    {
+      case BT_SMP_IO_DISPLAY_ONLY:
+        switch (remote_io)
+          {
+            case BT_SMP_IO_DISPLAY_ONLY:
+            case BT_SMP_IO_NO_INPUT_OUTPUT:
+              return PAIRING_METHOD_JUST_WORKS;
+            case BT_SMP_IO_KEYBOARD_ONLY:
+            case BT_SMP_IO_KEYBOARD_DISPLAY:
+            case BT_SMP_IO_DISPLAY_YESNO:
+              return mitm_requested ? PAIRING_METHOD_PASSKEY_DISPLAY
+                                    : PAIRING_METHOD_JUST_WORKS;
+            default:
+              return PAIRING_METHOD_NOT_SUPPORTED;
+          }
+        break;
+      case BT_SMP_IO_NO_INPUT_OUTPUT:
+        return PAIRING_METHOD_JUST_WORKS; /* Cannot support MITM */
+      default:
+        wlwarn("Unhandled local IO Cap %d\n", local_io);
+        return PAIRING_METHOD_JUST_WORKS;
+    }
+}
+
+static bool method_provides_mitm(enum pairing_method method)
+{
+  return (method == PAIRING_METHOD_PASSKEY_DISPLAY ||

Review Comment:
   remove ()



##########
wireless/bluetooth/bt_smp.c:
##########
@@ -576,17 +637,79 @@ static int smp_init(struct bt_smp_s *smp)
   return 0;
 }
 
+static enum pairing_method smp_get_pairing_method(uint8_t local_io,
+                                                  uint8_t remote_io,
+                                                  uint8_t local_auth,
+                                                  uint8_t remote_auth)
+{
+  bool local_mitm = (local_auth & BT_SMP_AUTH_MITM);
+  bool remote_mitm = (remote_auth & BT_SMP_AUTH_MITM);
+  bool mitm_requested = local_mitm || remote_mitm;
+
+  wlinfo("Local IO: %d, Remote IO: %d, MITM Req: %d\n", local_io, remote_io,
+      mitm_requested);

Review Comment:
   ```suggestion
           mitm_requested);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to