# proximity-unlock Bluetooth proximity unlock for Linux (KDE Plasma 6), keyed to a paired **iPhone**, with an optional **Apple Watch** for fast, wrist-based distance. Walk away and come back: the lock screen opens about 2 seconds after you sit down, and only if your watch is unlocked on your wrist. > **Status: experimental.** Tested on one setup: KDE Plasma 6.8 (plasmalogin + > kscreenlocker) on CachyOS, a 2020 MacBook Air (Intel, T2) with BlueZ 5.88, an iPhone > and an Apple Watch. The lock-screen unlock is tested; the password-less greeter > login is implemented but not yet verified on hardware. The watch lock flag is > reverse-engineered and could change with a watchOS update. ## How it differs from earlier projects Most Bluetooth unlock tools (BlueProximity, watch-unlock-linux, watch-unlock-rs, WristKey) decide from a device's address or its BLE advertisements. Advertisements can be recorded and replayed. This project: - **Proves identity with an encrypted, bonded link.** The daemon opens an L2CAP channel at the kernel's `BT_SECURITY_HIGH` level. The kernel refuses it unless the iPhone proves it holds the pairing key. Advertisements are only used for distance. - **Bonds the Apple Watch directly with Linux, no Mac needed.** The pairing helper briefly poses as a BLE heart-rate strap, which the watch pairs with ("Just Works"). That gives BlueZ the watch's identity key (IRK), so its rotating addresses can be matched. Other tools extract the IRK from a Mac's iCloud Keychain or a Windows registry. - **Reads the watch's connection signal**, not only its advertisements: a reading every second instead of every ~6 s, so unlocking takes about 2 s. - **Is careful about when to unlock:** only on a return (away → near), never right after a restart, with a password required after boot and after a set time. ## Requirements - KDE Plasma 6 with `plasmalogin` and `kscreenlocker` (the PAM part is plasmalogin-specific; the lock-screen part only needs `loginctl unlock-session`) - BlueZ 5 with LE support, Python 3, `python-dbus`, PyGObject (`gi`), `btmgmt` - An iPhone. Optionally an Apple Watch (with a passcode and wrist detection on). ## How it decides **Identity** always comes from the iPhone's secure link (above). Without it, nothing unlocks. **Distance** comes from BLE signal strength (RSSI, in dBm): - **iPhone only:** from the phone's advertisements. The kernel matches its rotating addresses using the IRK from a one-time LE bond. Classic link RSSI (`distance = classic`) also exists, but many controllers report it relative to a "golden range" (0 out to several metres), which makes it useless for short ranges. - **With an Apple Watch** (`watch_device`): the watch decides distance, and the iPhone only proves identity from anywhere in classic range (about 10 m), so the phone can stay on the desk. The watch's advertisements and its LE connection RSSI are both used (`watch_link_rssi`). When that link drops, for example after you've been out of range, the daemon reconnects it (`watch_reconnect`). - The watch only counts as near while it's **unlocked on your wrist** (`watch_require_unlocked`). Its advertisements carry an unofficial status flag (bit 0x80 of the flags byte in Apple's "Nearby Info" message) that clears when the watch comes off the wrist and returns once you enter the passcode. A locked watch still measures distance, so walking off with it counts as away, but it can't unlock. - If the watch goes quiet for `ble_silent_seconds` while you're at the desk (off, out of battery), the iPhone measures distance instead. If it goes quiet after leaving with you, it stays "away" until the watch is back. - A watch left on the desk keeps you "near". **Near and away:** - **Near:** the last two readings are at or above the threshold you calibrate. - **Away** is deliberately slow: readings below `*_away_threshold` (or the secure link down) for `ble_away_seconds` (30 s), or no readings at all for `ble_silent_seconds` (120 s). A pocketed phone or a lowered wrist that reads a bit weaker doesn't count as leaving. - **Unlock only on a return** (away → near). Locking the screen at the desk keeps it locked. After the daemon starts (boot, restart, reinstall) it must see you away once before it unlocks anything, and turning a kill switch off doesn't count as away. - **Sleep:** a suspend longer than `ble_away_seconds` counts as being away (`sleep_counts_as_away`), since the daemon couldn't watch while asleep: close the lid, go out, come back and open it, and it unlocks once you're near. On resume it restarts scanning and reconnects right away. Reading ages use a clock that keeps counting during sleep, so readings from before a suspend never look fresh. - **Connections are retried in the background** (the iPhone's secure link and the watch's LE link), so a slow connect attempt while a device is out of range never delays the next check. **Policy:** proximity is only allowed if you've typed your real password since boot and within `max_password_age_hours` (default 12). The daemon notices password logins and lock-screen unlocks itself; the timestamp lives in `/run`, so a reboot always needs the password. Kill switches turn it off instantly. **Actions:** - **Lock screen:** `loginctl unlock-session` on a return. - **Greeter:** pressing Enter with an empty password logs in while the state is `allowed` (via a `pam_exec` check that falls through to the password prompt). - **Every unlock or login** goes to the journal and shows a desktop notification. `sudo`, polkit, SSH and KWallet still need your password. ## Install 1. Pair your iPhone with the computer in the system Bluetooth settings (accept the prompts on both sides). 2. Download it and run the guided setup: ```sh git clone https://github.com/gurselm/proximity-unlock cd proximity-unlock ./proximity-setup ``` It asks for your password (sudo), then walks you through everything: - checks dependencies and Bluetooth, and finds your iPhone; - checks the encrypted link (and explains how to fix an out-of-step pairing); - gets the iPhone's identity key (tap the computer on the iPhone when asked); - optionally bonds your Apple Watch (tap the computer under Health Devices); - installs the service, and optionally the login-screen part; - **calibrates automatically:** 25 s sitting at the desk, 25 s about 2 m away; - asks you to lock and unlock once with your password, which turns it on. Run it again any time to re-pair or add a watch. `./proximity-setup --recalibrate` only redoes the measuring. `--no-calibrate` skips it; `--user NAME` sets the user. ### Manual install 1. **Pair the iPhone** over classic Bluetooth (system popups or `bluetoothctl`) and trust it. If the two sides get out of step ("Invalid exchange", "authentication failure"), forget the pairing on both, then pair again. 2. **Get the iPhone's LE identity key.** The helper briefly advertises the computer as a BLE keyboard. On the iPhone, open Settings > Bluetooth; your computer shows up a second time under Other Devices. Tap it and accept the prompts. The helper stops once it sees an LE bond. ```sh python3 le-pair-helper.py ``` 3. **Optional, Apple Watch:** run the helper in watch mode. The computer poses as a heart-rate strap. On the watch, open Settings > Bluetooth and tap your computer under Health Devices (if the computer used to run macOS, the watch may show the old Mac name; forget that entry first). No confirmation is needed. The helper prints the watch's address when it's done. ```sh python3 le-pair-helper.py --watch ``` 4. **Install** with the iPhone's address (and the watch's). The user comes from `sudo`, or pass `--user NAME`. `--no-greeter` skips the login-screen part. ```sh sudo ./install.sh AA:BB:CC:DD:EE:FF --watch 11:22:33:44:55:66 ``` 5. **Calibrate.** Hold the phone (or wear the watch) where unlocking should start, note the median, then step back about 2 m and note it again: ```sh proximity-unlock calibrate ``` Set `ble_rssi_threshold` (and `watch_rssi_threshold`, `watch_away_threshold`) in `/etc/proximity-unlock.conf` between the two, then restart: ```sh sudo systemctl restart proximity-unlock ``` 6. **Unlock once with your password.** Proximity stays off until you do. For faster returns, `near_samples = 1` and `interval = 1` check every second while the screen is locked. While you're at the desk and unlocked it checks every `idle_interval` seconds (5) to save battery. Measured CPU use is about 70 ms per minute at the desk and 250 ms per minute while locked. ## Daily use ```sh proximity-unlock status # state, live readings, watch lock, last password time proximity-unlock disable # kill switch for your user (instant) proximity-unlock enable proximity-unlock logs proximity-unlock watch-flags # raw Apple Nearby Info bytes from the watch ``` To disable it system-wide: `sudo touch /etc/proximity-unlock.disabled`. To troubleshoot slow unlocks, set `trace = yes` to log every watch reading while away. `sudo ./uninstall.sh` removes everything except the config. ## Security model and limits This is a convenience feature with a defined threat model, not a replacement for a password: - **It trusts devices, not people.** Anyone holding your iPhone (even locked) near your computer satisfies the identity check. With a watch and `watch_require_unlocked`, the watch must also be unlocked on a wrist. The password-age limit bounds how long stolen devices keep working. - **Relay attacks** (two attackers forwarding radio traffic, as with keyless cars) aren't prevented. Notifications and the password-age limit reduce the risk. - **The watch lock flag is unprotected** and reverse-engineered. It's an extra check, not identity. If a watchOS update breaks it, set `watch_require_unlocked = no` and look at `proximity-unlock watch-flags`. - **iPhone audio:** while the link is up, the iPhone may offer the computer as an audio device. If it does, turn off audio for the computer in the iPhone's Bluetooth settings. - **Intel Macs with a T2 chip:** the Bluetooth driver (`hci_bcm4377`) often doesn't recover from suspend. LE keeps working but classic connections time out, so the iPhone's link (and with it unlocking) stops until the driver is reloaded. Install the included sleep hook, which unloads the driver before suspend and reloads it after: ```sh sudo install -m755 contrib/t2-bluetooth-sleep /usr/lib/systemd/system-sleep/ ``` - **KWallet** stays locked after a password-less greeter login. - **`/etc/pam.d/plasmalogin`** is a copy of the vendor file with one check added. Re-run `install.sh` after plasma-login-manager updates. - **Without full-disk encryption**, someone with physical access can edit these files. ## Tests ```sh tests/run.sh # add -v to list every check ``` No root or Bluetooth needed: the phone, watch, sessions and clock are faked. Covers the presence rules (near/away hysteresis, restart, kill switch, watch lock state), BLE event and Apple Nearby Info parsing, and `pam-check.sh` on a copy pointed at a temp dir. ## Prior work and credits - [furiousMAC/continuity](https://github.com/furiousMAC/continuity) and the paper [Handoff All Your Privacy](https://arxiv.org/abs/1904.10600): reverse-engineered Apple Continuity messages, including Nearby Info. - [dalehumby/ESPHome-Apple-Watch-detection](https://github.com/dalehumby/ESPHome-Apple-Watch-detection): observed the Nearby Info flags byte as 0x98 (on wrist, unlocked) vs 0x18 (locked), which matched this project's own capture. - [DavidSt49/watch-unlock-linux](https://github.com/DavidSt49/watch-unlock-linux), [KatelynHaworth/watch-unlock-rs](https://github.com/KatelynHaworth/watch-unlock-rs), [jorjanion/WristKey](https://github.com/jorjanion/WristKey): Apple Watch unlock based on advertisements and an extracted IRK. - BlueProximity: the original Linux Bluetooth proximity lock. ## License MIT. See [LICENSE](LICENSE).