Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set LE security level #77

Closed
yggdr opened this issue Jun 19, 2019 · 11 comments
Closed

Cannot set LE security level #77

yggdr opened this issue Jun 19, 2019 · 11 comments
Assignees
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend enhancement New feature or request help wanted Extra attention is needed security Issues relating to access restrictions in BLE

Comments

@yggdr
Copy link

yggdr commented Jun 19, 2019

  • bleak version: 0.4.2
  • Python version: 3.7.3
  • Operating System: Ubuntu 19.04

Description

Cannot set BTLE security level before connecting.

I'm currently rewriting an internal testing tool from bluepy to an async library. I decided to test out bleak, but one of the problems I ran into was our device giving an "LE security request" when connecting to it. This didn't happen with the old library.
After some talking with our Bluetooth guys and testing with gatttool it seems that the old library defaults to what gatttool would call sec-level low, while bleak seems to default to at least medium. bluepy allows setting this, but I cannot find anything in the bleak API description on how to set the security level, so I looked at the bluez dbus "documentation", hoping I could hack in a workaround, but couldn't find anything either.

Any help is appreciated.

@hbldh hbldh self-assigned this Jun 24, 2019
@hbldh hbldh added enhancement New feature or request help wanted Extra attention is needed labels Jun 24, 2019
@hbldh
Copy link
Owner

hbldh commented Jun 24, 2019

This is something I definitely want to support, but I actually do not know how. I ahve received multiple questions about security related reading and writing issues with Bleak, but I have failed to find much documentation about it.

There is [https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt#n180](a read and write property) on the org.bluez.Device1 interface in the BlueZ D-Bus API. Try to set this to True and see if that changes anything. Setting might be done with something similar as to this:

from bleak.backends.bluezdbus import defs
await client._bus.callRemote(
    client._device_path,
    "Set",
    interface=defs.PROPERTIES_INTERFACE,
    destination=defs.BLUEZ_SERVICE,
    signature="ssv",
    body=[defs.DEVICE_INTERFACE, "Trusted", True],
    returnSignature="a{sv}",
).asFuture(self.loop)

Try setting both before and after connect (in the before case, use the get_device_object_path("hci0", "XX:XX...") method to get the device path)

See documentation about the org.freedesktop.DBus.Properties interface here

I have no devices to test this on myself, so you will have to help me with this.

@yggdr
Copy link
Author

yggdr commented Jun 24, 2019

Thanks for the response. Putting this before the connect yields an AttributeError: 'NoneType' object has no attribute 'callRemote', putting it after doesn't change the original outcome :(

@hbldh
Copy link
Owner

hbldh commented Jun 24, 2019

Ok. Thank you very much for testing the code; it was a bit of a leap of faith...

There is something I need to focus on in the handling of secure BLE connections apparently. Would you mind telling me what kind of device you are trying to connect to so I can buy one and try to get Bleak to work against it and by doing so hopefully all other BLE peripherals with additional security requirements? Or another BLE device, which costs as little as possible but still exibits this behaviour if you know of that?

@hbldh
Copy link
Owner

hbldh commented Jun 24, 2019

Possibly related to #64.

@hbldh hbldh added the Backend: BlueZ Issues and PRs relating to the BlueZ backend label Jun 24, 2019
@yggdr
Copy link
Author

yggdr commented Jun 24, 2019

The device is not out yet, so unfortunately I can't help you there :( I'm also not really a user of Bluetooth (ironic, I know), so I don't know any specific device that I can recommend here. I think it should be enough to take any BT device that can be set to not require a PIN code to pair to have the same effect.

@hbldh
Copy link
Owner

hbldh commented Jun 25, 2019

Ok, I will continue my search for devices that I can use for testing. Until then, I cannot do much more than that.

@stevennyman
Copy link

This Stack Overflow answer explains that elevated security levels are required for pairing iPhones, so those might be testing devices. https://stackoverflow.com/a/24725691

@vilvo
Copy link

vilvo commented Oct 4, 2019

I'm interested in the same but for Windows.
In the first phase we'd like to automate pairing with our BLE device for an internal testing tool implemented with Bleak.
I found there's this from Microsoft:
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing
So I read it should be possible.
Would you like another issue to track this request?

@hbldh
Copy link
Owner

hbldh commented Oct 8, 2019

If it is possible then you are more than welcome to do the work and send me a Pull Request.
It is not a problem as such to me since I have no interest in connecting to protected devices right now, so you commenting on this issue are more suited to perform the implementation and testing of this issue.

@andrewleech
Copy link

I've been trying to get pairing / bonding to work on windows without much luck - running into issues/limitation with pythonnet (and/or my understanding of how to use it).

I've tried discussing it with them without much resolution: pythonnet/pythonnet#940

@hbldh
Copy link
Owner

hbldh commented Sep 25, 2020

Pairing is implemented for .NET and BlueZ backends in version 0.8.0. Will close this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend enhancement New feature or request help wanted Extra attention is needed security Issues relating to access restrictions in BLE
Projects
None yet
Development

No branches or pull requests

5 participants