This project implements a secure BLE-based locking system using MQTT for session key delivery and RSA encryption for key security. The system consists of three main components: Backend (session key issuer), Lock (BLE scanner), and Guest (advertiser app). Communication is limited to BLE advertisements and MQTT protocols.
- Role: Issues session keys on demand via MQTT
- Responsibilities:
- Listens for session key requests on MQTT topic
backend/session_requests - Generates random 32-byte session keys
- Encrypts session key for lock using RSA-OAEP
- Signs the payload with backend RSA private key
- Publishes encrypted key to
locks/{lock_id}/session - Publishes plain key to
guests/{lock_id}/session
- Listens for session key requests on MQTT topic
- Role: BLE scanner that detects and validates guest advertisements
- Responsibilities:
- Subscribes to MQTT topic
locks/{lock_id}/sessionfor encrypted session keys - Decrypts session keys using lock's RSA private key
- Scans for BLE advertisements from guests
- Validates HMAC token in advertisement local name
- Triggers unlock if token is valid
- Validates session key expiry
- Subscribes to MQTT topic
- Role: App that requests session key and advertises unlock token
- Responsibilities:
- Publishes request to
backend/session_requestswith lock_id - Subscribes to
guests/{lock_id}/sessionfor session key - Generates HMAC token using session key
- Advertises with token in manufacturer data (company ID 0xFFFF)
- Validates session key expiry
- Publishes request to
- Role: Message broker for secure key delivery
- Configuration: Mosquitto with anonymous access enabled
- RSA Encryption: Session keys encrypted for lock using RSA-OAEP
- Digital Signatures: Payloads signed with PSS padding
- HMAC Tokens: Advertisement tokens use HMAC-SHA256
- Session Key Expiry: Keys valid for 5 minutes
- Nonce: Prevents replay attacks
-
Session Key Request:
- Guest publishes
{"lock_id": "lock_01"}tobackend/session_requests
- Guest publishes
-
Key Generation:
- Backend generates 32-byte random session key
- Encrypts with lock's RSA public key
- Signs payload with backend private key
- Publishes encrypted payload to
locks/lock_01/session - Publishes plain session key to
guests/lock_01/session
-
Key Reception:
- Lock receives encrypted key, decrypts with private key
- Guest receives plain key
-
BLE Unlock:
- Guest generates HMAC-SHA256(session_key, "unlock")[:16]
- Guest advertises with manufacturer data containing the token (company ID 0xFFFF)
- Lock scans for advertisements
- Lock extracts token from manufacturer data
- Lock verifies token matches expected HMAC
- If valid, unlock successful
- Linux with BlueZ (BLE support)
- Python 3.8+
- MQTT broker (Mosquitto)
-
Clone repository:
git clone <repo-url> cd ble-locking-system
-
Install dependencies:
pip install -r requirements.txt
-
Install and configure MQTT broker:
sudo apt install mosquitto sudo nano /etc/mosquitto/mosquitto.conf
Add:
listener 1883 0.0.0.0 allow_anonymous truesudo systemctl restart mosquitto
-
Configure network:
- Update
MQTT_BROKERin all scripts to the broker's IP address - Ensure firewall allows port 1883
- Update
RSA keys are pre-generated in the code. For production:
- Generate new RSA keypairs for backend and each lock
- Update
DEVICE_REGISTRYinsession_key_issuer.py - Update
LOCK_PRIVATE_KEY_PEMinlock.py
-
Start MQTT broker:
mosquitto
-
Start backend:
python session_key_issuer.py
-
Start lock:
python lock.py
(Requires BLE hardware/adapter for scanning)
-
Run guest app:
python ble_unlock_app.py
(Requires BLE hardware/adapter for advertising)
- Run backend and MQTT broker on server
- Run lock on device with BLE for scanning
- Run guest on device with BLE for advertising
- Backend: "Backend listening for session requests on backend/session_requests"
- Lock: "Subscribed to locks/lock_01/session for session keys" + BLE scan messages
- Guest: "Requested session key", "Received session key", then "Advertising with token"
- Check broker IP and port
- Verify
mosquittois running:sudo systemctl status mosquitto - Test connection:
mosquitto_sub -h <broker_ip> -t test
- Check BlueZ:
bluetoothctl show - Ensure BLE adapter:
hciconfig - Permissions: Run with sudo if needed
- For advertising (guest): Ensure bluez-peripheral can access BLE
- For scanning (lock): Ensure bleak can access BLE
- Verify RSA keys are correct format
- Check expiry times
- Ensure cryptography library installed
- Use strong RSA keys (2048+ bits)
- Implement proper authentication for guest requests
- Rotate session keys frequently
- Monitor MQTT traffic
- Use TLS for MQTT in production
- Add user authentication
- Implement lock status publishing
- Add multiple lock support
- Integrate with mobile apps
- Add audit logging