165 lines
6.5 KiB
Rust
165 lines
6.5 KiB
Rust
//! Stable-updater-only time transport. Reuses the enrolled device's selected
|
|
//! certificate without enrollment, rotation, ClientHello, or inventory changes.
|
|
//! SignedClock still independently verifies its OWN role/image/nonce/context.
|
|
use crate::{
|
|
certificate_rotation::{ProtectedKeyFiles, RotationStore},
|
|
helper_ipc as ipc,
|
|
update_trust::Protected,
|
|
};
|
|
use anyhow::{Context, Result, ensure};
|
|
use rmm_agent_core::auth::Binding;
|
|
use rusqlite::{Connection, OpenFlags};
|
|
use std::{
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
path::Path,
|
|
time::Duration,
|
|
};
|
|
const STATE: &str = r"C:\ProgramData\RustDiscovery\state";
|
|
const CONFIG: &str = r"C:\ProgramData\RustDiscovery\config.json";
|
|
const CA: &str = r"C:\ProgramData\RustDiscovery\server-ca.pem";
|
|
const JOURNAL: &str = r"C:\ProgramData\RustDiscovery\state\telemetry.sqlite";
|
|
const DOMAIN: &str = "discovery.ease-technologies.de";
|
|
pub struct Transport {
|
|
client: reqwest::Client,
|
|
configuration: Protected,
|
|
ca: Protected,
|
|
address: SocketAddr,
|
|
}
|
|
impl Transport {
|
|
pub fn open(binding: &Binding) -> Result<Self> {
|
|
let readers = vec![
|
|
ipc::service_sid(crate::SERVICE_NAME)?,
|
|
ipc::service_sid(ipc::HELPER_SERVICE)?,
|
|
ipc::service_sid(crate::update_ipc::SERVICE)?,
|
|
];
|
|
let (configuration, raw) = Protected::read_shared(Path::new(CONFIG), 16384, &readers)?;
|
|
let value: serde_json::Value = serde_json::from_slice(&raw)?;
|
|
let fields = value.as_object().context("UPDATE_TIME_CONFIG_OBJECT")?;
|
|
ensure!(
|
|
fields.keys().all(|key| matches!(
|
|
key.as_str(),
|
|
"server_name"
|
|
| "connect_address"
|
|
| "ca_file"
|
|
| "state_directory"
|
|
| "enrollment_token_file"
|
|
| "transport"
|
|
| "job_binding_file"
|
|
| "enable_jobs"
|
|
| "windows_echo_probe_only"
|
|
| "windows_execution_profile"
|
|
)),
|
|
"UPDATE_TIME_CONFIG_UNKNOWN_FIELD"
|
|
);
|
|
ensure!(
|
|
value["server_name"].as_str() == Some(DOMAIN)
|
|
&& value["ca_file"].as_str() == Some(CA)
|
|
&& value["state_directory"].as_str() == Some(STATE)
|
|
&& matches!(value["transport"].as_str(), Some("auto" | "https")),
|
|
"UPDATE_TIME_FIXED_ENDPOINT"
|
|
);
|
|
let address: SocketAddr = value["connect_address"]
|
|
.as_str()
|
|
.context("UPDATE_TIME_ADDRESS")?
|
|
.parse()?;
|
|
ensure!(
|
|
address.ip() == IpAddr::V4(Ipv4Addr::new(88, 99, 80, 202)) && address.port() == 443,
|
|
"UPDATE_TIME_FIXED_ADDRESS"
|
|
);
|
|
let (ca, ca_bytes) = Protected::read_shared(Path::new(CA), 65536, &readers)?;
|
|
crate::native::private_path(Path::new(STATE), true)?;
|
|
crate::native::private_path(Path::new(JOURNAL), false)?;
|
|
let journal_pin = crate::update_state::AgentStateFile::journal()?;
|
|
let db = Connection::open_with_flags(
|
|
JOURNAL,
|
|
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
|
)?;
|
|
db.busy_timeout(Duration::ZERO)?;
|
|
unsafe {
|
|
rusqlite::ffi::sqlite3_limit(db.handle(), rusqlite::ffi::SQLITE_LIMIT_LENGTH, 65536);
|
|
}
|
|
db.execute_batch("PRAGMA trusted_schema=OFF;PRAGMA query_only=ON;")?;
|
|
let keys = ProtectedKeyFiles::open(Path::new(STATE))?;
|
|
let rotation = RotationStore::new(
|
|
binding.agent_id,
|
|
binding.enrollment_generation,
|
|
ca_bytes.clone(),
|
|
)?;
|
|
let identity = rotation.read_only_current(&db, &keys, chrono::Utc::now().timestamp())?;
|
|
let mut pem = identity.certificate_pem;
|
|
pem.push(b'\n');
|
|
pem.extend_from_slice(&identity.private_key_pem);
|
|
let client = reqwest::Client::builder()
|
|
.https_only(true)
|
|
.tls_built_in_root_certs(false)
|
|
.add_root_certificate(reqwest::Certificate::from_pem(&ca_bytes)?)
|
|
.identity(reqwest::Identity::from_pem(&pem)?)
|
|
.resolve(DOMAIN, address)
|
|
.no_proxy()
|
|
.redirect(reqwest::redirect::Policy::none())
|
|
.connect_timeout(Duration::from_millis(650))
|
|
.timeout(Duration::from_millis(650))
|
|
.pool_max_idle_per_host(1)
|
|
.build()?;
|
|
db.close().map_err(|(_, error)| error)?;
|
|
journal_pin.check()?;
|
|
configuration.verify()?;
|
|
ca.verify()?;
|
|
Ok(Self {
|
|
client,
|
|
configuration,
|
|
ca,
|
|
address,
|
|
})
|
|
}
|
|
pub async fn compare(&mut self, request: &rmm_time_attestation::Request) -> Result<Vec<u8>> {
|
|
request.validate()?;
|
|
ensure!(
|
|
request.context.role == rmm_time_attestation::Role::Updater,
|
|
"UPDATE_TIME_OWN_ROLE_REQUIRED"
|
|
);
|
|
self.configuration.verify()?;
|
|
self.ca.verify()?;
|
|
let client = self.client.clone();
|
|
let address = self.address;
|
|
let body = tokio::time::timeout(Duration::from_millis(650), async move {
|
|
let mut response = client
|
|
.post(format!(
|
|
"https://{DOMAIN}:{}/agent/v1/time-attestation",
|
|
address.port()
|
|
))
|
|
.json(request)
|
|
.send()
|
|
.await?;
|
|
ensure!(
|
|
response.status() == reqwest::StatusCode::OK
|
|
&& response
|
|
.headers()
|
|
.get(reqwest::header::CONTENT_TYPE)
|
|
.and_then(|h| h.to_str().ok())
|
|
== Some("application/json")
|
|
&& !response
|
|
.headers()
|
|
.contains_key(reqwest::header::CONTENT_ENCODING)
|
|
&& response.content_length().is_none_or(|n| n <= 16384),
|
|
"UPDATE_TIME_HTTP_PROFILE"
|
|
);
|
|
let mut body = vec![];
|
|
while let Some(chunk) = response.chunk().await? {
|
|
ensure!(
|
|
chunk.len() <= 16384usize.saturating_sub(body.len()),
|
|
"UPDATE_TIME_BODY_LIMIT"
|
|
);
|
|
body.extend_from_slice(&chunk);
|
|
}
|
|
ensure!(!body.is_empty(), "UPDATE_TIME_EMPTY_RESPONSE");
|
|
Ok::<Vec<u8>, anyhow::Error>(body)
|
|
})
|
|
.await
|
|
.context("UPDATE_TIME_HTTP_DEADLINE")??;
|
|
self.configuration.verify()?;
|
|
self.ca.verify()?;
|
|
Ok(body)
|
|
}
|
|
}
|