* [PATCH BlueZ v1 1/3] emulator/bthost: Add helpers to test security of deferred setup
2026-11-19 22:15 [PATCH BlueZ v1 0/3] Add tests for security of deferred setup Luiz Augusto von Dentz
@ 2026-11-19 22:15 ` Luiz Augusto von Dentz
2026-11-20 0:55 ` Add tests for " bluez.test.bot
2026-11-19 22:15 ` [PATCH BlueZ v1 2/3] l2cap-tester: " Luiz Augusto von Dentz
2026-11-19 22:15 ` [PATCH BlueZ v1 3/3] rfcomm-tester: Add tests for " Luiz Augusto von Dentz
2 siblings, 1 reply; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-19 22:15 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Add helpers so testers can control the remote side while a connection
is pending:
- bthost_hold_auth_request()/bthost_release_auth_request() hold the
reply to a PIN Code or IO Capability Request, keeping pairing in
progress until it is released.
- bthost_set_l2cap_rsp_pending() keeps a request while its Connection
Response is pending, so its callback is called with the final
response as well.
- bthost_set_conn_encrypt() enables/disables the encryption of a
BR/EDR connection.
Assisted-by: OpenCode:claude-fable-5.2
---
emulator/bthost.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++
emulator/bthost.h | 6 ++++
2 files changed, 81 insertions(+)
diff --git a/emulator/bthost.c b/emulator/bthost.c
index e65d29faa989..e5526aebf2ec 100644
--- a/emulator/bthost.c
+++ b/emulator/bthost.c
@@ -273,6 +273,11 @@ struct bthost {
uint8_t io_capability;
uint8_t auth_req;
bool reject_user_confirm;
+ bool hold_auth_request;
+ bool auth_request_held;
+ bool auth_request_pin;
+ bool l2cap_rsp_pending;
+ uint8_t auth_request_bdaddr[6];
void *smp_data;
bool conn_init;
bool le;
@@ -1543,6 +1548,13 @@ static void evt_pin_code_request(struct bthost *bthost, const void *data,
if (len < sizeof(*ev))
return;
+ if (bthost->hold_auth_request) {
+ memcpy(bthost->auth_request_bdaddr, ev->bdaddr, 6);
+ bthost->auth_request_held = true;
+ bthost->auth_request_pin = true;
+ return;
+ }
+
if (bthost->pin_len > 0) {
struct bt_hci_cmd_pin_code_request_reply cp;
@@ -1667,6 +1679,13 @@ static void evt_io_cap_request(struct bthost *bthost, const void *data,
if (!conn)
return;
+ if (bthost->hold_auth_request) {
+ memcpy(bthost->auth_request_bdaddr, ev->bdaddr, 6);
+ bthost->auth_request_held = true;
+ bthost->auth_request_pin = false;
+ return;
+ }
+
memcpy(cp.bdaddr, ev->bdaddr, 6);
cp.capability = bthost->io_capability;
cp.oob_data = 0x00;
@@ -2437,6 +2456,19 @@ static void handle_pending_l2reqs(struct bthost *bthost, struct btconn *conn,
continue;
}
+ /* Keep the request while the response is only pending */
+ if (bthost->l2cap_rsp_pending &&
+ code == BT_L2CAP_PDU_CONN_RSP &&
+ len >= sizeof(struct bt_l2cap_pdu_conn_rsp)) {
+ const struct bt_l2cap_pdu_conn_rsp *rsp = data;
+
+ if (le16_to_cpu(rsp->result) == 0x0001) {
+ req->cb(code, data, len, req->user_data);
+ curr = &req->next;
+ continue;
+ }
+ }
+
*curr = req->next;
req->cb(code, data, len, req->user_data);
free(req);
@@ -4276,6 +4308,49 @@ uint8_t bthost_get_auth_req(struct bthost *bthost)
return auth_req;
}
+void bthost_hold_auth_request(struct bthost *bthost, bool hold)
+{
+ bthost->hold_auth_request = hold;
+}
+
+void bthost_release_auth_request(struct bthost *bthost)
+{
+ bthost->hold_auth_request = false;
+
+ if (!bthost->auth_request_held)
+ return;
+
+ bthost->auth_request_held = false;
+
+ if (bthost->auth_request_pin) {
+ struct bt_hci_evt_pin_code_request ev;
+
+ memcpy(ev.bdaddr, bthost->auth_request_bdaddr, 6);
+ evt_pin_code_request(bthost, &ev, sizeof(ev));
+ } else {
+ struct bt_hci_evt_io_capability_request ev;
+
+ memcpy(ev.bdaddr, bthost->auth_request_bdaddr, 6);
+ evt_io_cap_request(bthost, &ev, sizeof(ev));
+ }
+}
+
+void bthost_set_l2cap_rsp_pending(struct bthost *bthost, bool enable)
+{
+ bthost->l2cap_rsp_pending = enable;
+}
+
+void bthost_set_conn_encrypt(struct bthost *bthost, uint16_t handle,
+ uint8_t encr_mode)
+{
+ struct bt_hci_cmd_set_conn_encrypt cp;
+
+ cp.handle = cpu_to_le16(handle);
+ cp.encr_mode = encr_mode;
+
+ send_command(bthost, BT_HCI_CMD_SET_CONN_ENCRYPT, &cp, sizeof(cp));
+}
+
void bthost_set_reject_user_confirm(struct bthost *bthost, bool reject)
{
bthost->reject_user_confirm = reject;
diff --git a/emulator/bthost.h b/emulator/bthost.h
index b3e18114638a..92aaf42305da 100644
--- a/emulator/bthost.h
+++ b/emulator/bthost.h
@@ -206,6 +206,12 @@ void bthost_set_io_capability(struct bthost *bthost, uint8_t io_capability);
uint8_t bthost_get_io_capability(struct bthost *bthost);
void bthost_set_auth_req(struct bthost *bthost, uint8_t auth_req);
uint8_t bthost_get_auth_req(struct bthost *bthost);
+void bthost_hold_auth_request(struct bthost *bthost, bool hold);
+void bthost_release_auth_request(struct bthost *bthost);
+void bthost_set_l2cap_rsp_pending(struct bthost *bthost, bool enable);
+void bthost_set_conn_encrypt(struct bthost *bthost, uint16_t handle,
+ uint8_t encr_mode);
+
void bthost_set_reject_user_confirm(struct bthost *bthost, bool reject);
bool bthost_get_reject_user_confirm(struct bthost *bthost);
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH BlueZ v1 2/3] l2cap-tester: Add tests for security of deferred setup
2026-11-19 22:15 [PATCH BlueZ v1 0/3] Add tests for security of deferred setup Luiz Augusto von Dentz
2026-11-19 22:15 ` [PATCH BlueZ v1 1/3] emulator/bthost: Add helpers to test " Luiz Augusto von Dentz
@ 2026-11-19 22:15 ` Luiz Augusto von Dentz
2026-11-19 22:15 ` [PATCH BlueZ v1 3/3] rfcomm-tester: Add tests for " Luiz Augusto von Dentz
2 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-19 22:15 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Add the following tests:
L2CAP BR/EDR Server - Security Block Pairing Failed
L2CAP BR/EDR Server - Defer Success
L2CAP BR/EDR Server - Defer Encryption Off
L2CAP BR/EDR Server - Defer Early Accept
L2CAP LE Server - Defer Success
Defer Encryption Off has the peer disabling encryption while the
connection waits for the listener to authorize it, and Defer Early
Accept has the listener accepting and authorizing the connection while
authentication, which then fails, is still pending. Both expect the
connection to be blocked instead of being established.
Security Block Pairing Failed covers the connection being blocked once
pairing fails after the request has been responded as pending, since
L2CAP BR/EDR Server - Security Block is rejected before that.
Assisted-by: OpenCode:claude-fable-5.2
---
tools/l2cap-tester.c | 236 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 236 insertions(+)
diff --git a/tools/l2cap-tester.c b/tools/l2cap-tester.c
index 4d2c5cd15c64..020d31b71d27 100644
--- a/tools/l2cap-tester.c
+++ b/tools/l2cap-tester.c
@@ -54,6 +54,8 @@ struct test_data {
int sk2;
int reencrypt_sk;
int opp_sk;
+ int listen_sk;
+ struct tester_io *defer_io;
bool opp_encrypted;
bool host_disconnected;
int step;
@@ -129,6 +131,10 @@ struct l2cap_data {
bool direct_advertising;
bool close_1;
bool defer;
+ bool hold_auth;
+ bool encrypt_off;
+ /* Follow a pending connection response until the final one */
+ bool pending_rsp;
bool shut_sock_wr;
@@ -355,6 +361,8 @@ static void test_data_free(void *test_data)
test_l2cap(name, HCIEMU_TYPE_BREDRLE70, data, setup, func)
static uint8_t pair_device_pin[] = { 0x30, 0x30, 0x30, 0x30 }; /* "0000" */
+/* "1111" */
+static uint8_t pair_device_wrong_pin[] = { 0x31, 0x31, 0x31, 0x31 };
static const struct l2cap_data client_connect_success_test = {
.client_psm = 0x1001,
@@ -614,6 +622,84 @@ static const struct l2cap_data l2cap_server_sec_block_test = {
.enable_ssp = true,
};
+static const uint8_t l2cap_defer_sec_block_rsp[] = {
+ 0x40, 0x00, /* dcid */
+ 0x41, 0x00, /* scid */
+ 0x03, 0x00, /* Sec Block */
+ 0x00, 0x00 /* status */
+};
+
+static const struct l2cap_data l2cap_server_defer_success_test = {
+ .server_psm = 0x1001,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+};
+
+/* Pairing fails after the connection request has been answered as pending,
+ * unlike l2cap_server_sec_block_test which is rejected right away.
+ */
+static const struct l2cap_data l2cap_server_sec_block_pairing_test = {
+ .server_psm = 0x1001,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_device_pin,
+ .pin_len = sizeof(pair_device_pin),
+ .client_pin = pair_device_wrong_pin,
+ .client_pin_len = sizeof(pair_device_wrong_pin),
+ .pending_rsp = true,
+};
+
+/* The peer disables encryption while the connection waits for userspace to
+ * authorize it, the link no longer meets the security level so the connection
+ * shall be blocked instead of being offered again.
+ */
+static const struct l2cap_data l2cap_server_defer_encrypt_off_test = {
+ .server_psm = 0x1001,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_device_pin,
+ .pin_len = sizeof(pair_device_pin),
+ .client_pin = pair_device_pin,
+ .client_pin_len = sizeof(pair_device_pin),
+ .client_encrypt = true,
+ .encrypt_off = true,
+};
+
+/* The listener accepts and authorizes the connection while authentication is
+ * still pending and then authentication fails, the connection shall be
+ * blocked.
+ */
+static const struct l2cap_data l2cap_server_defer_early_accept_test = {
+ .server_psm = 0x1001,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_CONN_REQ,
+ .send_cmd = l2cap_connect_req,
+ .send_cmd_len = sizeof(l2cap_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_CONN_RSP,
+ .expect_cmd = l2cap_defer_sec_block_rsp,
+ .expect_cmd_len = sizeof(l2cap_defer_sec_block_rsp),
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_device_pin,
+ .pin_len = sizeof(pair_device_pin),
+ .client_pin = pair_device_wrong_pin,
+ .client_pin_len = sizeof(pair_device_wrong_pin),
+ .hold_auth = true,
+};
+
static const uint8_t l2cap_nval_psm_rsp[] = { 0x00, 0x00, /* dcid */
0x41, 0x00, /* scid */
0x02, 0x00, /* nval PSM */
@@ -1120,6 +1206,17 @@ static const struct l2cap_data le_server_success_test = {
.expect_cmd_len = sizeof(le_connect_rsp),
};
+static const struct l2cap_data le_server_defer_success_test = {
+ .server_psm = 0x0080,
+ .defer = true,
+ .send_cmd_code = BT_L2CAP_PDU_LE_CONN_REQ,
+ .send_cmd = le_connect_req,
+ .send_cmd_len = sizeof(le_connect_req),
+ .expect_cmd_code = BT_L2CAP_PDU_LE_CONN_RSP,
+ .expect_cmd = le_connect_rsp,
+ .expect_cmd_len = sizeof(le_connect_rsp),
+};
+
static const uint8_t nval_le_connect_req[] = { 0x80, 0x00, /* PSM */
0x01, 0x00, /* SCID */
0x20, 0x00, /* MTU */
@@ -2223,6 +2320,13 @@ static void setup_powered_common(void)
if (test && test->reject_ssp)
bthost_set_reject_user_confirm(bthost, true);
+ if (test && test->hold_auth)
+ bthost_hold_auth_request(bthost, true);
+
+ if (test && (test->hold_auth || test->encrypt_off ||
+ test->pending_rsp))
+ bthost_set_l2cap_rsp_pending(bthost, true);
+
if (data->hciemu_type >= HCIEMU_TYPE_LE)
mgmt_send(data->mgmt, MGMT_OP_SET_LE, data->mgmt_index,
sizeof(param), param, NULL, NULL, NULL);
@@ -3938,6 +4042,102 @@ static bool defer_accept(struct test_data *data, struct tester_io *io,
return true;
}
+/* Keep an accepted socket open until the test ends */
+static void defer_keep(struct test_data *data, struct tester_io *io)
+{
+ if (!data->io_channels)
+ data->io_channels = queue_new();
+
+ queue_push_tail(data->io_channels, io);
+}
+
+/* Authorize a deferred connection without closing its socket */
+static void defer_authorize(struct test_data *data, struct tester_io *io)
+{
+ int sk = tester_io_get_fd(io);
+ ssize_t ret;
+ char c;
+
+ ret = recv(sk, &c, 1, MSG_DONTWAIT);
+
+ tester_print("Authorize deferred setup sk = %d: %zd (%s)", sk, ret,
+ ret < 0 ? strerror(errno) : "ok");
+}
+
+/* Behave like a listener that does not wait to be notified: accept and
+ * authorize whatever connection is queued.
+ */
+static void early_accept(struct test_data *data)
+{
+ struct tester_io *io;
+ int new_sk;
+
+ new_sk = accept(data->listen_sk, NULL, NULL);
+ if (new_sk < 0) {
+ tester_print("Nothing to accept while security is pending: "
+ "%s (%d)", strerror(errno), errno);
+ return;
+ }
+
+ tester_print("Accepted sk = %d while security is pending", new_sk);
+
+ io = tester_io_new(new_sk);
+ tester_io_set_close_on_unref(io, true);
+
+ defer_keep(data, io);
+ defer_authorize(data, io);
+}
+
+/* Handle the responses of the tests that follow a pending connection until
+ * its final response. Returns true if there is nothing else to check.
+ */
+static bool server_security_rsp(struct test_data *data,
+ const struct bt_l2cap_pdu_conn_rsp *rsp)
+{
+ const struct l2cap_data *l2data = data->test_data;
+ struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+ uint16_t result = le16_to_cpu(rsp->result);
+ uint16_t status = le16_to_cpu(rsp->status);
+
+ tester_print("Connection response result 0x%04x status 0x%04x",
+ result, status);
+
+ if (!result) {
+ tester_warn("Connection accepted without required security");
+ tester_test_failed();
+ return true;
+ }
+
+ /* Not pending, check the final response */
+ if (result != 0x0001)
+ return false;
+
+ /* Authentication pending */
+ if (l2data->hold_auth && status == 0x0001 && !data->step) {
+ data->step = 1;
+ early_accept(data);
+ bthost_release_auth_request(bthost);
+ return true;
+ }
+
+ /* Authorization pending */
+ if (l2data->encrypt_off && status == 0x0002) {
+ if (!data->step) {
+ data->step = 1;
+ tester_print("Disabling encryption");
+ bthost_set_conn_encrypt(bthost, data->handle, 0x00);
+ return true;
+ }
+
+ /* Offered again after encryption was disabled, authorize it */
+ data->step = 2;
+ if (data->defer_io)
+ defer_authorize(data, data->defer_io);
+ }
+
+ return true;
+}
+
static bool l2cap_listen_cb(struct tester_io *io, uint32_t cond,
void *user_data)
{
@@ -3967,6 +4167,15 @@ static bool l2cap_listen_cb(struct tester_io *io, uint32_t cond,
return false;
}
+ /* Authorize only after the peer has disabled encryption */
+ if (l2data->encrypt_off) {
+ data->defer_io = io;
+ defer_keep(data, io);
+ if (data->step > 1)
+ defer_authorize(data, io);
+ return false;
+ }
+
if (!defer_accept(data, io, l2cap_accept_cb)) {
tester_warn("Unable to accept deferred setup");
tester_test_failed();
@@ -3994,6 +4203,12 @@ static void client_l2cap_rsp(uint8_t code, const void *data, uint16_t len,
if (code == BT_L2CAP_PDU_CONN_RSP) {
const struct bt_l2cap_pdu_conn_rsp *rsp = data;
+
+ if ((l2data->hold_auth || l2data->encrypt_off ||
+ l2data->pending_rsp) && len >= sizeof(*rsp) &&
+ server_security_rsp(test_data, rsp))
+ return;
+
if (len == sizeof(rsp) && !rsp->result && !rsp->status)
return;
@@ -4196,6 +4411,8 @@ static void start_test_server(const void *test_data, bthost_new_conn_cb conn_cb,
return;
}
+ data->listen_sk = sk;
+
io = tester_io_new(sk);
tester_io_set_close_on_unref(io, true);
@@ -4557,6 +4774,22 @@ int main(int argc, char *argv[])
&l2cap_server_sec_block_test,
setup_powered_server, test_server);
+ test_l2cap_bredr("L2CAP BR/EDR Server - Security Block Pairing Failed",
+ &l2cap_server_sec_block_pairing_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Defer Success",
+ &l2cap_server_defer_success_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Defer Encryption Off",
+ &l2cap_server_defer_encrypt_off_test,
+ setup_powered_server, test_server);
+
+ test_l2cap_bredr("L2CAP BR/EDR Server - Defer Early Accept",
+ &l2cap_server_defer_early_accept_test,
+ setup_powered_server, test_server);
+
test_l2cap_bredr("L2CAP BR/EDR Server - Invalid PSM",
&l2cap_server_nval_psm_test,
setup_powered_server, test_server);
@@ -4722,6 +4955,9 @@ int main(int argc, char *argv[])
setup_powered_client, test_connect);
test_l2cap_le("L2CAP LE Server - Success", &le_server_success_test,
setup_powered_server, test_server);
+ test_l2cap_le("L2CAP LE Server - Defer Success",
+ &le_server_defer_success_test,
+ setup_powered_server, test_server);
test_l2cap_le("L2CAP LE Server - Nval SCID", &le_server_nval_scid_test,
setup_powered_server, test_server);
test_l2cap_le("L2CAP LE Server - PHY", &le_server_phy_test,
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH BlueZ v1 3/3] rfcomm-tester: Add tests for deferred setup
2026-11-19 22:15 [PATCH BlueZ v1 0/3] Add tests for security of deferred setup Luiz Augusto von Dentz
2026-11-19 22:15 ` [PATCH BlueZ v1 1/3] emulator/bthost: Add helpers to test " Luiz Augusto von Dentz
2026-11-19 22:15 ` [PATCH BlueZ v1 2/3] l2cap-tester: " Luiz Augusto von Dentz
@ 2026-11-19 22:15 ` Luiz Augusto von Dentz
2 siblings, 0 replies; 5+ messages in thread
From: Luiz Augusto von Dentz @ 2026-11-19 22:15 UTC (permalink / raw)
To: linux-bluetooth
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Add the following tests:
Basic RFCOMM Socket Server - Defer Success
Basic RFCOMM Socket Server - Defer Early Accept
Defer Early Accept has the listener accepting and authorizing the
connection while authentication, which then fails, is still pending and
expects the connection to be refused.
Assisted-by: OpenCode:claude-fable-5.2
---
tools/rfcomm-tester.c | 146 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 146 insertions(+)
diff --git a/tools/rfcomm-tester.c b/tools/rfcomm-tester.c
index 998d698c1bb2..ae4f3c7ee7ac 100644
--- a/tools/rfcomm-tester.c
+++ b/tools/rfcomm-tester.c
@@ -43,6 +43,7 @@ struct test_data {
struct tester_io *io;
unsigned int io_id;
uint16_t conn_handle;
+ int listen_sk;
uint16_t send_len;
uint16_t recv_len;
};
@@ -61,6 +62,12 @@ struct rfcomm_server_data {
uint8_t server_channel;
uint8_t client_channel;
bool expected_status;
+ bool defer;
+ int sec_level;
+ const uint8_t *pin;
+ uint8_t pin_len;
+ const uint8_t *client_pin;
+ uint8_t client_pin_len;
const uint8_t *send_data;
const uint8_t *read_data;
uint16_t data_len;
@@ -286,13 +293,81 @@ static void setup_powered_server_callback(uint8_t status, uint16_t length,
tester_setup_complete();
}
+/* Authorize a deferred connection, the socket is kept open in data->io */
+static void defer_authorize(struct test_data *data, int sk)
+{
+ ssize_t ret;
+ char c;
+
+ ret = recv(sk, &c, 1, MSG_DONTWAIT);
+
+ tester_print("Authorize deferred setup sk = %d: %zd (%s)", sk, ret,
+ ret < 0 ? strerror(errno) : "ok");
+
+ if (data->io)
+ tester_io_unref(data->io);
+
+ data->io = tester_io_new(sk);
+ tester_io_set_close_on_unref(data->io, true);
+}
+
+static void pin_code_request_callback(uint16_t index, uint16_t length,
+ const void *param, void *user_data)
+{
+ const struct mgmt_ev_pin_code_request *ev = param;
+ struct test_data *data = user_data;
+ const struct rfcomm_server_data *srv = data->test_data;
+ struct mgmt_cp_pin_code_reply cp;
+
+ /* Behave like a listener that does not wait to be notified: accept
+ * and authorize whatever is queued while authentication is pending.
+ */
+ if (srv->defer) {
+ int new_sk;
+
+ new_sk = accept(data->listen_sk, NULL, NULL);
+ if (new_sk < 0) {
+ tester_print("Nothing to accept while authentication "
+ "is pending: %s (%d)", strerror(errno),
+ errno);
+ } else {
+ tester_print("Accepted sk = %d while authentication "
+ "is pending", new_sk);
+ defer_authorize(data, new_sk);
+ }
+ }
+
+ memset(&cp, 0, sizeof(cp));
+ memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
+ cp.pin_len = srv->pin_len;
+ memcpy(cp.pin_code, srv->pin, srv->pin_len);
+
+ mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_REPLY, data->mgmt_index,
+ sizeof(cp), &cp, NULL, NULL, NULL);
+}
+
static void setup_powered_server(const void *test_data)
{
struct test_data *data = tester_get_data();
+ const struct rfcomm_server_data *srv = test_data;
unsigned char param[] = { 0x01 };
tester_print("Powering on controller");
+ if (srv && srv->pin) {
+ struct bthost *bthost = hciemu_client_get_host(data->hciemu);
+
+ mgmt_register(data->mgmt, MGMT_EV_PIN_CODE_REQUEST,
+ data->mgmt_index, pin_code_request_callback,
+ data, NULL);
+
+ bthost_set_pin_code(bthost, srv->client_pin,
+ srv->client_pin_len);
+
+ mgmt_send(data->mgmt, MGMT_OP_SET_BONDABLE, data->mgmt_index,
+ sizeof(param), param, NULL, NULL, NULL);
+ }
+
mgmt_send(data->mgmt, MGMT_OP_SET_CONNECTABLE, data->mgmt_index,
sizeof(param), param,
NULL, NULL, NULL);
@@ -374,6 +449,33 @@ const struct rfcomm_server_data listen_read_success = {
.read_data = data
};
+const struct rfcomm_server_data listen_defer_success = {
+ .server_channel = 0x0c,
+ .client_channel = 0x0c,
+ .expected_status = true,
+ .defer = true,
+};
+
+static const uint8_t pair_pin[] = { 0x30, 0x30, 0x30, 0x30 }; /* "0000" */
+/* "1111" */
+static const uint8_t pair_wrong_pin[] = { 0x31, 0x31, 0x31, 0x31 };
+
+/* The listener accepts and authorizes the connection while authentication is
+ * still pending and then authentication fails, the connection shall be
+ * refused.
+ */
+const struct rfcomm_server_data listen_defer_early_accept = {
+ .server_channel = 0x0c,
+ .client_channel = 0x0c,
+ .expected_status = false,
+ .defer = true,
+ .sec_level = BT_SECURITY_MEDIUM,
+ .pin = pair_pin,
+ .pin_len = sizeof(pair_pin),
+ .client_pin = pair_wrong_pin,
+ .client_pin_len = sizeof(pair_wrong_pin),
+};
+
const struct rfcomm_server_data listen_nval = {
.server_channel = 0x0c,
.client_channel = 0x0e,
@@ -721,6 +823,13 @@ static bool rfcomm_listen_cb(struct tester_io *io, uint32_t cond,
return false;
}
+ /* The result is checked once the client connection completes */
+ if (srv->defer) {
+ tester_print("Accept deferred setup sk = %d", new_sk);
+ defer_authorize(data, new_sk);
+ return false;
+ }
+
if (srv->send_data) {
ssize_t ret;
@@ -803,6 +912,37 @@ static void test_server(const void *test_data)
return;
}
+ if (srv->sec_level) {
+ struct bt_security sec;
+
+ memset(&sec, 0, sizeof(sec));
+ sec.level = srv->sec_level;
+
+ if (setsockopt(sk, SOL_BLUETOOTH, BT_SECURITY, &sec,
+ sizeof(sec)) < 0) {
+ tester_warn("Can't set security level: %s (%d)",
+ strerror(errno), errno);
+ tester_test_failed();
+ close(sk);
+ return;
+ }
+ }
+
+ if (srv->defer) {
+ int opt = 1;
+
+ if (setsockopt(sk, SOL_BLUETOOTH, BT_DEFER_SETUP, &opt,
+ sizeof(opt)) < 0) {
+ tester_warn("Can't enable deferred setup: %s (%d)",
+ strerror(errno), errno);
+ tester_test_failed();
+ close(sk);
+ return;
+ }
+ }
+
+ data->listen_sk = sk;
+
if (listen(sk, 5) < 0) {
tester_warn("listening on socket failed: %s (%u)",
strerror(errno), errno);
@@ -868,6 +1008,12 @@ int main(int argc, char *argv[])
test_rfcomm("Basic RFCOMM Socket Server - Read Success",
&listen_read_success, setup_powered_server,
test_server);
+ test_rfcomm("Basic RFCOMM Socket Server - Defer Success",
+ &listen_defer_success, setup_powered_server,
+ test_server);
+ test_rfcomm("Basic RFCOMM Socket Server - Defer Early Accept",
+ &listen_defer_early_accept,
+ setup_powered_server, test_server);
test_rfcomm("Basic RFCOMM Socket Server - Conn Refused", &listen_nval,
setup_powered_server, test_server);
--
2.56.0
^ permalink raw reply related [flat|nested] 5+ messages in thread