feat: add device anchor persistence and serialize vpn lifecycle

- Implement Unix device anchor discovery, root-owned persistence under /var/lib/gp, and credential delivery in gpapi and gpservice.
- Introduce LifecycleHandle to serialize VPN connect/disconnect lifecycles and ensure cancellation cannot be superseded by late connect events.
This commit is contained in:
Kevin Yue committed 2026-09-19 17:37:32 +08:00
1 parent 9d8c3b4e34
commit 308957283b
13 files changed
+866 -217

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
//! Neutral device-anchor transport model and Linux collection.
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnchorSource {
LinuxMac,
LinuxRandom,
MacosIokit,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DeviceAnchor {
pub hardware_id: String,
pub source: AnchorSource,
}
impl DeviceAnchor {
pub fn validate(&self) -> bool {
URL_SAFE_NO_PAD
.decode(&self.hardware_id)
.is_ok_and(|bytes| bytes.len() == 32 && URL_SAFE_NO_PAD.encode(&bytes) == self.hardware_id)
}
pub fn derive(source: AnchorSource, seed: impl AsRef<[u8]>) -> Self {
let domain = match source {
AnchorSource::LinuxMac => b"gp-device-anchor:v1:linux-mac:".as_slice(),
AnchorSource::LinuxRandom => b"gp-device-anchor:v1:linux-random:".as_slice(),
AnchorSource::MacosIokit => b"gp-device-anchor:v1:macos-platform-uuid:".as_slice(),
};
let mut input = domain.to_vec();
input.extend_from_slice(seed.as_ref());
Self {
hardware_id: URL_SAFE_NO_PAD.encode(openssl::sha::sha256(&input)),
source,
}
}
}
#[cfg(unix)]
pub fn collect_unix() -> anyhow::Result<DeviceAnchor> {
if let Some(mac) = mac_address::get_mac_address()? {
let bytes = mac.bytes();
if bytes != [0; 6] && bytes[0] & 1 == 0 {
return Ok(DeviceAnchor::derive(AnchorSource::LinuxMac, bytes));
}
}
let mut random = [0_u8; 32];
getrandom::fill(&mut random).map_err(|_| anyhow::anyhow!("cannot initialize device anchor"))?;
Ok(DeviceAnchor::derive(AnchorSource::LinuxRandom, random))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hashes_are_domain_separated_and_canonical() {
let mac = DeviceAnchor::derive(AnchorSource::LinuxMac, [0, 17, 34, 51, 68, 85]);
assert!(mac.validate());
assert_ne!(
mac.hardware_id,
DeviceAnchor::derive(AnchorSource::LinuxRandom, [0, 17, 34, 51, 68, 85]).hardware_id
);
}
}
+1
View File
@@ -1,6 +1,7 @@
pub mod auth;
pub mod cookie_store;
pub mod credential;
pub mod device_anchor;
pub mod error;
pub mod gateway;
pub mod gp_params;
@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zeroize::Zeroizing;
use crate::device_anchor::DeviceAnchor;
use crate::utils::base64;
use super::{MAX_CREDENTIAL_FRAME, MAX_PRODUCT_VERSION, TransportError};
@@ -14,6 +15,7 @@ pub struct SessionCredential {
session_id: Uuid,
secret: Zeroizing<[u8; 32]>,
product_version: String,
device_anchor: Option<DeviceAnchor>,
}
impl SessionCredential {
@@ -30,6 +32,7 @@ impl SessionCredential {
session_id: Uuid::new_v4(),
secret,
product_version: product_version.to_owned(),
device_anchor: None,
})
}
@@ -49,6 +52,18 @@ impl SessionCredential {
&self.product_version
}
pub fn with_device_anchor(mut self, anchor: DeviceAnchor) -> Result<Self, TransportError> {
if !anchor.validate() {
return Err(TransportError::InvalidCredential);
}
self.device_anchor = Some(anchor);
Ok(self)
}
pub fn device_anchor(&self) -> Option<&DeviceAnchor> {
self.device_anchor.as_ref()
}
pub fn encode_frame(&self) -> Result<Zeroizing<Vec<u8>>, TransportError> {
let encoded_secret = Zeroizing::new(base64::encode(self.secret()));
let dto = CredentialDtoRef {
@@ -56,6 +71,7 @@ impl SessionCredential {
session_id: self.session_id,
secret: encoded_secret.as_str(),
product_version: &self.product_version,
device_anchor: self.device_anchor.as_ref(),
};
let payload = Zeroizing::new(serde_json::to_vec(&dto)?);
@@ -85,8 +101,12 @@ impl SessionCredential {
session_id,
secret,
product_version,
device_anchor,
} = dto;
let encoded_secret = secret;
if device_anchor.as_ref().is_some_and(|anchor| !anchor.validate()) {
return Err(TransportError::InvalidCredential);
}
if product_version.is_empty() || product_version.len() > MAX_PRODUCT_VERSION {
return Err(TransportError::InvalidCredential);
}
@@ -104,6 +124,7 @@ impl SessionCredential {
session_id,
secret,
product_version,
device_anchor,
})
}
}
@@ -126,6 +147,7 @@ struct CredentialDtoRef<'a> {
session_id: Uuid,
secret: &'a str,
product_version: &'a str,
device_anchor: Option<&'a DeviceAnchor>,
}
#[derive(Deserialize)]
@@ -135,6 +157,7 @@ struct CredentialDto {
session_id: Uuid,
secret: Zeroizing<String>,
product_version: String,
device_anchor: Option<DeviceAnchor>,
}
#[cfg(test)]
@@ -161,4 +184,18 @@ mod tests {
assert!(SessionCredential::decode_frame(&[0, 2, b'{']).is_err());
assert!(SessionCredential::decode_frame(&vec![0; MAX_CREDENTIAL_FRAME + 1]).is_err());
}
#[test]
fn credential_round_trips_a_validated_device_anchor() {
let anchor = DeviceAnchor {
hardware_id: general_purpose::URL_SAFE_NO_PAD.encode([1_u8; 32]),
source: crate::device_anchor::AnchorSource::LinuxMac,
};
let credential = SessionCredential::generate(Uuid::new_v4(), "2.6.5")
.unwrap()
.with_device_anchor(anchor.clone())
.unwrap();
let decoded = SessionCredential::decode_frame(&credential.encode_frame().unwrap()).unwrap();
assert_eq!(decoded.device_anchor(), Some(&anchor));
}
}