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

WIP: Security manager #194

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip(sm): add struct security manager
Signed-off-by: Haobo Gu <haobogu@outlook.com>
HaoboGu committed Nov 25, 2024
commit e1e804cf94807a9f7136eb77ce39de1fc47df744
1 change: 1 addition & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ categories = ["embedded", "hardware-support", "no-std"]
resolver = "2"

[dependencies]
bitfield-struct = "0.9.2"
bt-hci = { version = "0.1.1", features = ["embassy-time"] }
embedded-io-async = { version = "0.6" }
embedded-io = { version = "0.6" }
4 changes: 4 additions & 0 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ use crate::channel_manager::{ChannelStorage, PacketChannel};
use crate::connection_manager::{ConnectionStorage, EventChannel};
use crate::l2cap::sar::SarType;
use crate::packet_pool::{PacketPool, Qos};
use crate::security_manager::SecurityManagerError;

mod fmt;

@@ -39,6 +40,7 @@ mod cursor;
pub mod packet_pool;
mod pdu;
pub mod peripheral;
mod security_manager;
pub mod types;

pub use packet_pool::Qos as PacketQos;
@@ -135,6 +137,8 @@ pub enum Error {
HciDecode(FromHciBytesError),
/// Error from the Attribute Protocol.
Att(AttErrorCode),
/// Error from the security manager
Security(SecurityManagerError),
/// Insufficient space in the buffer.
InsufficientSpace,
/// Invalid value.
97 changes: 97 additions & 0 deletions host/src/security_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use bitfield_struct::bitfield;
use bt_hci::controller::Controller;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum IoCapabilities {
DisplayOnly = 0,
DisplayYesNo = 1,
KeyboardOnly = 2,
NoInputNoOutput = 3,
KeyboardDisplay = 4,
}

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum SecurityManagerError {
PasskeyEntryFailed = 1,
OobNotAvailable,
AuthenticationRequirements,
ConfirmValueFailed,
PairingNotSupported,
EncryptionKeySize,
CommandNotSupported,
UnspecifiedReason,
RepeatedAttempts,
InvalidParameters,
DHKeyCheckFailed,
NumericComparisonFailed,
BrEdrPairingInProgress,
GenerationNotAllowed,
KeyRejected,
}

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub enum OobDataFlag {
NotPresent = 0,
Present = 1,
}

#[bitfield(u8)]
pub struct AuthReq {
#[bits(2)]
bonding_flags: u8,
#[bits(1)]
mitm: bool,
#[bits(1)]
sc: bool,
#[bits(1)]
keypress: bool,
#[bits(1)]
ct2: bool,
#[bits(2)]
rfu: u8,
}

const SM_PAIRING_REQUEST: u8 = 0x01;
const SM_PAIRING_RESPONSE: u8 = 0x02;
const SM_PAIRING_CONFIRM: u8 = 0x03;
const SM_PAIRING_RANDOM: u8 = 0x04;
const SM_PAIRING_FAILED: u8 = 0x05;
const SM_PAIRING_PUBLIC_KEY: u8 = 0x0c;
const SM_PAIRING_DHKEY_CHECK: u8 = 0x0d;

/// Security manager that handles SM packet
pub struct SecurityManager<'d, C: Controller> {
controller: &'d C,
}

impl<C: Controller> SecurityManager<'_, C> {
/// Handle packet
pub(crate) async fn handle(&mut self, payload: &[u8]) {
let data = &payload[1..];
let command = payload[0];

match command {
SM_PAIRING_REQUEST => {
todo!()
}
SM_PAIRING_PUBLIC_KEY => {
todo!()
}
SM_PAIRING_RANDOM => {
todo!()
}
SM_PAIRING_DHKEY_CHECK => {
todo!()
}
_ => {
// handle FAILURE
error!("Unknown SM command {}", command);
todo!()
}
}
}
}