Skip to content

Commit fae4d67

Browse files
committed
feat: server trust
1 parent 1245668 commit fae4d67

File tree

11 files changed

+78
-21
lines changed

11 files changed

+78
-21
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macros = { path = "./macros" }
3030
nb = "1.1.0"
3131
embassy-futures = "0.1.1"
3232
embassy-sync = "0.7.1"
33-
ws-framer = { version = "0.3.0", default-features = false, features = ["alloc", "http", "getrandom02"] }
33+
ws-framer = { version = "0.3.1", default-features = false, features = ["alloc", "http", "getrandom02"] }
3434
embedded-hal-async = "1.0.0"
3535
portable-atomic = { version = "1.11.1", default-features = false }
3636
critical-section = "1.2.0"

src/battery.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ pub async fn battery_read_task(
8282
level: Some(bat_percentage as f64),
8383
voltage: Some(bat_calc_mv / 1000.0),
8484
},
85-
sign_key: Some(unsafe { crate::state::SIGN_KEY }),
8685
})
8786
.await;
8887
}

src/board.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use adv_shift_registers::wrappers::ShifterValueRange;
33
use anyhow::Result;
44
use esp_hal::{
55
gpio::{AnyPin, Input, InputConfig, Level, Output, Pin, Pull},
6-
peripherals::{ADC1, BT, Peripherals, SPI2, TIMG0, TIMG1, UART1, WIFI},
6+
peripherals::{ADC1, AES, BT, Peripherals, SPI2, TIMG0, TIMG1, UART1, WIFI},
77
rng::Rng,
88
timer::timg::TimerGroup,
99
};
@@ -19,6 +19,7 @@ pub struct Board {
1919
pub wifi: WIFI<'static>,
2020
pub bt: BT<'static>,
2121
pub spi_dma: esp_hal::peripherals::DMA_CH0<'static>,
22+
pub aes: AES<'static>,
2223

2324
// spi
2425
pub miso: AnyPin<'static>,
@@ -49,6 +50,7 @@ impl Board {
4950
let adc1 = peripherals.ADC1;
5051
let wifi = peripherals.WIFI;
5152
let bt = peripherals.BT;
53+
let aes = peripherals.AES;
5254

5355
let sck = peripherals.GPIO4.degrade();
5456
let miso = peripherals.GPIO5.degrade();
@@ -95,6 +97,7 @@ impl Board {
9597
adc1,
9698
wifi,
9799
bt,
100+
aes,
98101

99102
miso,
100103
mosi,

src/buttons.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,17 @@ async fn submit_up(
142142
if !state_val.device_added.unwrap_or(false) {
143143
let mut sign_key = [0; 4];
144144
_ = getrandom::getrandom(&mut sign_key);
145+
_ = state.nvs.invalidate_key(b"SIGN_KEY").await;
145146
_ = state.nvs.append_key(b"SIGN_KEY", &sign_key).await;
146147
unsafe { crate::state::SIGN_KEY = u32::from_be_bytes(sign_key) >> 1 };
148+
unsafe { crate::state::TRUST_SERVER = true };
147149

148150
crate::ws::send_packet(crate::structs::TimerPacket {
149151
tag: None,
150152
data: crate::structs::TimerPacketInner::Add {
151153
firmware: alloc::string::ToString::to_string(crate::version::FIRMWARE),
152154
sign_key: unsafe { crate::state::SIGN_KEY },
153155
},
154-
sign_key: None,
155156
})
156157
.await;
157158

@@ -380,6 +381,11 @@ async fn delegate_hold(
380381
return Ok(false);
381382
}
382383

384+
if unsafe { !crate::state::TRUST_SERVER } {
385+
log::error!("Skipping delegate hold. Server not trusted!");
386+
return Ok(false);
387+
}
388+
383389
let inspection_time = if state_val.use_inspection()
384390
&& state_val.inspection_start.is_some()
385391
&& state_val.inspection_end.is_some()
@@ -404,6 +410,7 @@ async fn delegate_hold(
404410
delegate: true,
405411
inspection_time,
406412
group_id: state_val.solve_group.clone().map(|r| r.group_id).expect(""),
413+
sign_key: unsafe { crate::state::SIGN_KEY },
407414
};
408415

409416
state_val.delegate_hold = Some(3);

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ async fn main(spawner: Spawner) {
112112
log::info!("This firmware is in QA mode!");
113113

114114
let nvs = Nvs::new_from_part_table().expect("Wrong partition configuration!");
115-
let global_state = Rc::new(GlobalStateInner::new(&nvs));
115+
let global_state = Rc::new(GlobalStateInner::new(&nvs, board.aes));
116116
let wifi_setup_sig = Rc::new(Signal::new());
117117

118118
// TODO: add error handling here
@@ -341,7 +341,6 @@ async fn logger_task(global_state: GlobalState) {
341341
ws::send_packet(structs::TimerPacket {
342342
tag: None,
343343
data: structs::TimerPacketInner::Logs { logs: tmp_logs },
344-
sign_key: None,
345344
})
346345
.await;
347346
}

src/rfid.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,16 @@ pub async fn rfid_task(
138138
continue;
139139
}
140140

141+
if unsafe { !crate::state::TRUST_SERVER } {
142+
log::error!("Skipping card scan. Server not trusted!");
143+
continue;
144+
}
145+
141146
let resp = crate::ws::send_request::<CardInfoResponsePacket>(
142147
crate::structs::TimerPacketInner::CardInfoRequest {
143148
card_id: card_uid as u64,
144149
attendance_device: None,
150+
sign_key: unsafe { crate::state::SIGN_KEY },
145151
},
146152
)
147153
.await;
@@ -245,6 +251,11 @@ async fn process_card_info_response(
245251
return Ok(());
246252
}
247253

254+
if unsafe { !crate::state::TRUST_SERVER } {
255+
log::error!("Skipping solve send. Server not trusted!");
256+
return Ok(());
257+
}
258+
248259
let resp = crate::ws::send_request::<SolveConfirmPacket>(
249260
crate::structs::TimerPacketInner::Solve {
250261
solve_time: state.solve_time.ok_or(anyhow!("Solve time is None"))?,
@@ -256,6 +267,7 @@ async fn process_card_info_response(
256267
delegate: false,
257268
inspection_time,
258269
group_id: state.solve_group.clone().map(|r| r.group_id).expect(""),
270+
sign_key: unsafe { crate::state::SIGN_KEY },
259271
},
260272
)
261273
.await;

src/state.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ use crate::{structs::PossibleGroup, utils::signaled_mutex::SignaledMutex};
22
use alloc::{rc::Rc, string::String, vec::Vec};
33
use embassy_sync::{
44
blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex},
5+
mutex::Mutex,
56
signal::Signal,
67
};
78
use embassy_time::{Duration, Instant, Timer};
9+
use esp_hal::aes::Aes;
810
use esp_hal_wifimanager::Nvs;
911
use serde::{Deserialize, Serialize};
1012

11-
pub static mut EPOCH_BASE: u64 = 0;
1213
pub static mut SIGN_KEY: u32 = 0;
14+
pub static mut TRUST_SERVER: bool = false;
15+
16+
pub static mut EPOCH_BASE: u64 = 0;
1317
pub static mut SLEEP_STATE: bool = false;
1418
pub static mut DEEPER_SLEEP: bool = false;
1519
pub static mut OTA_STATE: bool = false;
@@ -127,20 +131,22 @@ pub struct GlobalStateInner {
127131
pub update_progress: Signal<CriticalSectionRawMutex, u8>,
128132

129133
pub nvs: Nvs,
134+
pub aes: Mutex<NoopRawMutex, Aes<'static>>,
130135

131136
#[cfg(feature = "e2e")]
132137
pub e2e: End2End,
133138
}
134139

135140
impl GlobalStateInner {
136-
pub fn new(nvs: &Nvs) -> Self {
141+
pub fn new(nvs: &Nvs, aes: esp_hal::peripherals::AES<'static>) -> Self {
137142
Self {
138143
state: SignaledMutex::new(SignaledGlobalStateInner::new()),
139144
timer_signal: Signal::new(),
140145
show_battery: Signal::new(),
141146
update_progress: Signal::new(),
142147

143148
nvs: nvs.clone(),
149+
aes: Mutex::new(Aes::new(aes)),
144150

145151
#[cfg(feature = "e2e")]
146152
e2e: End2End::new(),

src/structs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ pub struct TimerPacket {
2424
#[serde(skip_serializing_if = "Option::is_none")]
2525
pub tag: Option<u64>,
2626
pub data: TimerPacketInner,
27-
28-
#[serde(skip_serializing_if = "Option::is_none")]
29-
pub sign_key: Option<u32>,
3027
}
3128

3229
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -49,6 +46,7 @@ pub enum TimerPacketInner {
4946
delegate: bool,
5047
inspection_time: i64,
5148
group_id: String,
49+
sign_key: u32,
5250
},
5351
SolveConfirm(SolveConfirmPacket),
5452
DelegateResponse(DelegateResponsePacket),
@@ -63,6 +61,8 @@ pub enum TimerPacketInner {
6361

6462
#[serde(skip_serializing_if = "Option::is_none")]
6563
attendance_device: Option<bool>,
64+
65+
sign_key: u32,
6666
},
6767
CardInfoResponse(CardInfoResponsePacket),
6868
AttendanceMarked,

src/utils/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ pub fn set_brownout_detection(state: bool) {
1414
}
1515
}
1616

17+
pub fn get_random_u64() -> u64 {
18+
let mut buf = [0; 8];
19+
_ = getrandom::getrandom(&mut buf);
20+
u64::from_be_bytes(buf)
21+
}
22+
1723
/// This function returns value with maximum of signed integer
1824
/// (2147483647) to easily store it in postgres db as integer
1925
pub fn get_efuse_u32() -> u32 {

0 commit comments

Comments
 (0)