-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmain.rs
More file actions
422 lines (369 loc) · 12.8 KB
/
Copy pathmain.rs
File metadata and controls
422 lines (369 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use anyhow::{bail, Context, Result};
use clap::Parser;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_human_bytes as hex_bytes;
use sha2::{Digest, Sha512};
use std::collections::BTreeSet;
use std::time::Duration;
use tracing::{debug, error, info, warn};
use x509_parser::prelude::*;
const BASE_URL: &str = "https://crt.sh";
/// Quoted public key with TDX quote
#[derive(Debug, Deserialize)]
struct QuotedPublicKey {
/// Hex-encoded public key
public_key: String,
/// JSON-encoded GetQuoteResponse
quote: String,
}
/// GetQuoteResponse from guest-agent
#[derive(Debug, Deserialize)]
struct GetQuoteResponse {
/// TDX quote (hex-encoded in JSON)
#[serde(with = "hex_bytes")]
quote: Vec<u8>,
/// JSON-encoded event log
event_log: String,
/// VM configuration
vm_config: String,
}
/// Request for dstack-verifier
#[derive(Debug, Serialize)]
struct VerificationRequest {
quote: String,
event_log: String,
vm_config: String,
pccs_url: Option<String>,
}
/// Response from dstack-verifier
#[derive(Debug, Deserialize)]
struct VerificationResponse {
is_valid: bool,
details: VerificationDetails,
reason: Option<String>,
}
#[derive(Debug, Deserialize)]
struct VerificationDetails {
#[allow(dead_code)]
quote_verified: bool,
#[allow(dead_code)]
event_log_verified: bool,
#[allow(dead_code)]
os_image_hash_verified: bool,
report_data: Option<String>,
app_info: Option<AppInfo>,
}
/// App info from verification response
#[derive(Debug, Deserialize)]
struct AppInfo {
#[serde(with = "hex_bytes")]
app_id: Vec<u8>,
#[serde(with = "hex_bytes")]
compose_hash: Vec<u8>,
#[serde(with = "hex_bytes")]
os_image_hash: Vec<u8>,
}
#[derive(Debug, Deserialize)]
struct AcmeInfoResponse {
#[allow(dead_code)]
account_uri: String,
#[allow(dead_code)]
hist_keys: Vec<String>,
quoted_hist_keys: Vec<QuotedPublicKey>,
}
struct Monitor {
gateway_uri: String,
verifier_url: String,
pccs_url: Option<String>,
base_domain: String,
known_keys: BTreeSet<Vec<u8>>,
last_checked: Option<u64>,
client: reqwest::Client,
}
#[derive(Debug, Serialize, Deserialize)]
struct CTLog {
id: u64,
issuer_ca_id: u64,
issuer_name: String,
common_name: String,
name_value: String,
not_before: String,
not_after: String,
serial_number: String,
result_count: u64,
entry_timestamp: String,
}
impl Monitor {
/// Create a new monitor
/// `gateway` format: `base_domain[:port]`, e.g., `example.com` or `example.com:8443`
fn new(gateway: String, verifier_url: String, pccs_url: Option<String>) -> Result<Self> {
let (base_domain, gateway_uri) = Self::parse_gateway(&gateway)?;
validate_domain(&base_domain)?;
Ok(Self {
gateway_uri,
verifier_url,
pccs_url,
base_domain,
known_keys: BTreeSet::new(),
last_checked: None,
client: reqwest::Client::new(),
})
}
/// Parse gateway input into base_domain and gateway URI
/// Input: `base_domain[:port]`, e.g., `example.com` or `example.com:8443`
/// Output: (base_domain, gateway_uri)
fn parse_gateway(gateway: &str) -> Result<(String, String)> {
let (base_domain, port) = match gateway.rsplit_once(':') {
Some((domain, port_str)) => {
// Validate port is a number
let _: u16 = port_str.parse().context("invalid port number")?;
(domain.to_string(), Some(port_str.to_string()))
}
None => (gateway.to_string(), None),
};
let gateway_uri = match port {
Some(p) => format!("https://gateway.{}:{}", base_domain, p),
None => format!("https://gateway.{}", base_domain),
};
Ok((base_domain, gateway_uri))
}
/// Compute expected report_data for a public key using zt-cert content type
fn compute_expected_report_data(public_key: &[u8]) -> [u8; 64] {
// Format: sha512("zt-cert:" + public_key)
let mut hasher = Sha512::new();
hasher.update(b"zt-cert:");
hasher.update(public_key);
hasher.finalize().into()
}
/// Verify a quoted public key using the verifier service
/// Returns (public_key, app_info)
async fn verify_quoted_key(&self, quoted_key: &QuotedPublicKey) -> Result<(Vec<u8>, AppInfo)> {
let public_key =
hex::decode("ed_key.public_key).context("invalid hex in public_key")?;
if quoted_key.quote.is_empty() {
bail!("empty quote for public key");
}
// Parse the GetQuoteResponse from the quote field
let quote_response: GetQuoteResponse =
serde_json::from_str("ed_key.quote).context("failed to parse quote response")?;
// Build verification request
let verify_request = VerificationRequest {
quote: hex::encode("e_response.quote),
event_log: quote_response.event_log,
vm_config: quote_response.vm_config,
pccs_url: self.pccs_url.clone(),
};
// Call verifier
let verify_url = format!("{}/verify", self.verifier_url.trim_end_matches('/'));
let response = self
.client
.post(&verify_url)
.json(&verify_request)
.send()
.await
.context("failed to call verifier")?;
if !response.status().is_success() {
bail!("verifier returned HTTP {}", response.status().as_u16());
}
let verify_response: VerificationResponse = response
.json()
.await
.context("failed to parse verifier response")?;
if !verify_response.is_valid {
bail!(
"quote verification failed: {}",
verify_response.reason.unwrap_or_default()
);
}
// Verify report_data matches expected value
let expected_report_data = Self::compute_expected_report_data(&public_key);
let expected_hex = hex::encode(expected_report_data);
let actual_report_data = verify_response
.details
.report_data
.context("verifier did not return report_data")?;
if actual_report_data != expected_hex {
bail!(
"report_data mismatch: expected {}, got {}",
expected_hex,
actual_report_data
);
}
let app_info = verify_response
.details
.app_info
.context("verifier did not return app_info")?;
Ok((public_key, app_info))
}
async fn refresh_known_keys(&mut self) -> Result<()> {
let acme_info_url = format!(
"{}/.dstack/acme-info",
self.gateway_uri.trim_end_matches('/')
);
info!("fetching known public keys from {}", acme_info_url);
let response = self
.client
.get(&acme_info_url)
.send()
.await
.context("failed to fetch acme-info")?;
if !response.status().is_success() {
bail!(
"failed to fetch acme-info: HTTP {}",
response.status().as_u16()
);
}
let info: AcmeInfoResponse = response
.json()
.await
.context("failed to parse acme-info response")?;
info!(
"got {} quoted public keys, verifying...",
info.quoted_hist_keys.len()
);
let mut verified_keys = BTreeSet::new();
for (i, quoted_key) in info.quoted_hist_keys.iter().enumerate() {
match self.verify_quoted_key(quoted_key).await {
Ok((public_key, app_info)) => {
info!(
"✅ verified public key {}: {}",
i,
hex_fmt::HexFmt(&public_key)
);
info!(" app_id: {}", hex_fmt::HexFmt(&app_info.app_id));
info!(
" compose_hash: {}",
hex_fmt::HexFmt(&app_info.compose_hash)
);
info!(
" os_image_hash: {}",
hex_fmt::HexFmt(&app_info.os_image_hash)
);
verified_keys.insert(public_key);
}
Err(e) => {
warn!(
"⚠️ failed to verify public key {}: {}",
i,
hex_fmt::HexFmt("ed_key.public_key)
);
warn!(" error: {:#}", e);
// Continue with other keys, but don't add this one
}
}
}
if verified_keys.is_empty() && !info.quoted_hist_keys.is_empty() {
bail!("no public keys could be verified");
}
self.known_keys = verified_keys;
info!("verified {} public keys", self.known_keys.len());
for key in self.known_keys.iter() {
debug!(" {}", hex_fmt::HexFmt(key));
}
Ok(())
}
async fn get_logs(&self, count: u32) -> Result<Vec<CTLog>> {
let url = format!(
"{}/?q={}&output=json&limit={}",
BASE_URL, self.base_domain, count
);
let response = reqwest::get(&url).await?;
Ok(response.json().await?)
}
async fn check_one_log(&self, log: &CTLog) -> Result<()> {
let cert_url = format!("{}/?d={}", BASE_URL, log.id);
let cert_data = reqwest::get(&cert_url).await?.text().await?;
let pem = Pem::iter_from_buffer(cert_data.as_bytes())
.next()
.transpose()
.context("failed to parse pem")?
.context("empty pem")?;
let cert = pem.parse_x509().context("invalid x509 certificate")?;
let pubkey = cert.public_key().raw;
if !self.known_keys.contains(pubkey) {
error!("❌ error in {:?}", log);
bail!(
"certificate has issued to unknown pubkey: {:?}",
hex_fmt::HexFmt(pubkey)
);
}
info!("✅ checked log id={}", log.id);
Ok(())
}
async fn check_new_logs(&mut self) -> Result<()> {
let logs = self.get_logs(10000).await?;
debug!("got {} logs", logs.len());
let mut found_last_checked = false;
for log in logs.iter() {
let log_id = log.id;
if let Some(last_checked) = self.last_checked {
if log_id == last_checked {
found_last_checked = true;
break;
}
}
debug!("🔍 checking log id={}", log_id);
self.check_one_log(log).await?;
}
if !found_last_checked && self.last_checked.is_some() {
bail!("last checked log not found, something went wrong");
}
if !logs.is_empty() {
let last_log = &logs[0];
debug!("last checked: {}", last_log.id);
self.last_checked = Some(last_log.id);
}
Ok(())
}
async fn run(&mut self) {
info!("monitoring {}...", self.base_domain);
loop {
if let Err(err) = self.refresh_known_keys().await {
error!("error refreshing known keys: {}", err);
}
if let Err(err) = self.check_new_logs().await {
error!("error: {}", err);
}
tokio::time::sleep(Duration::from_secs(60)).await;
}
}
}
fn validate_domain(domain: &str) -> Result<()> {
let domain_regex =
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$")
.context("invalid regex")?;
if !domain_regex.is_match(domain) {
bail!("invalid domain name");
}
Ok(())
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Gateway address in format: base_domain[:port]
/// e.g., "example.com" or "example.com:8443"
#[arg(short, long, env = "GATEWAY")]
gateway: String,
/// The dstack-verifier URL
#[arg(short, long, env = "VERIFIER_URL")]
verifier_url: String,
/// PCCS URL for TDX collateral fetching (optional)
#[arg(long, env = "PCCS_URL")]
pccs_url: Option<String>,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
{
use tracing_subscriber::{fmt, EnvFilter};
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
fmt().with_env_filter(filter).with_ansi(false).init();
}
let args = Args::parse();
let mut monitor = Monitor::new(args.gateway, args.verifier_url, args.pccs_url)?;
monitor.run().await;
Ok(())
}