-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
574 lines (510 loc) · 20 KB
/
lib.rs
File metadata and controls
574 lines (510 loc) · 20 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! CVM attestation generation and verification
#[cfg(feature = "azure")]
pub mod azure;
pub mod dcap;
pub mod measurements;
use std::{
fmt::{self, Display, Formatter},
net::IpAddr,
time::{Duration, SystemTime, UNIX_EPOCH},
};
use measurements::MultiMeasurements;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{dcap::DcapVerificationError, measurements::MeasurementPolicy};
/// Used in attestation type detection to check if we are on GCP
const GCP_METADATA_API: &str =
"http://metadata.google.internal/computeMetadata/v1/project/project-id";
/// An attestation payload together with its type
#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
pub struct AttestationExchangeMessage {
/// What CVM platform is used (including none)
pub attestation_type: AttestationType,
/// The attestation evidence as bytes - in the case of DCAP this is a
/// quote
pub attestation: Vec<u8>,
}
impl AttestationExchangeMessage {
/// Create an empty attestation payload for the case that we are running
/// in a non-confidential environment
pub fn without_attestation() -> Self {
Self { attestation_type: AttestationType::None, attestation: Vec::new() }
}
/// Extract the measurements from the attestation, if present, but do
/// not verify
pub fn get_measurements(&self) -> Result<Option<MultiMeasurements>, AttestationError> {
match self.attestation_type {
AttestationType::None => Ok(None),
AttestationType::AzureTdx => {
#[cfg(feature = "azure")]
{
Ok(Some(azure::get_measurements(&self.attestation)?))
}
#[cfg(not(feature = "azure"))]
{
Err(AttestationError::AttestationTypeNotSupported)
}
}
_ => {
#[cfg(any(test, feature = "mock"))]
{
let quote = tdx_quote::Quote::from_bytes(&self.attestation)
.map_err(DcapVerificationError::from)?;
Ok(Some(MultiMeasurements::from_tdx_quote("e)))
}
#[cfg(not(any(test, feature = "mock")))]
{
let quote = dcap_qvl::verify::Quote::parse(&self.attestation)
.map_err(DcapVerificationError::from)?;
Ok(Some(MultiMeasurements::from_dcap_qvl_quote("e)?))
}
}
}
}
}
/// Type of attestaion used
/// Only supported (or soon-to-be supported) types are given
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AttestationType {
/// No attestion
None,
/// TDX on Google Cloud Platform
GcpTdx,
/// TDX on Azure, with MAA
AzureTdx,
/// TDX on Qemu (no cloud platform)
QemuTdx,
/// DCAP TDX
DcapTdx,
}
impl AttestationType {
/// Matches the names used by Constellation aTLS
pub fn as_str(&self) -> &'static str {
match self {
AttestationType::None => "none",
AttestationType::AzureTdx => "azure-tdx",
AttestationType::QemuTdx => "qemu-tdx",
AttestationType::GcpTdx => "gcp-tdx",
AttestationType::DcapTdx => "dcap-tdx",
}
}
/// Detect what platform we are on by attempting an attestation
pub async fn detect() -> Result<Self, AttestationError> {
// First attempt azure, if the feature is present
#[cfg(feature = "azure")]
{
if azure::create_azure_attestation([0; 64]).is_ok() {
return Ok(AttestationType::AzureTdx);
}
}
// Otherwise try DCAP quote - this internally checks that the quote provider
// is `tdx_guest`
if configfs_tsm::create_tdx_quote([0; 64]).is_ok() {
if running_on_gcp().await? {
return Ok(AttestationType::GcpTdx);
} else {
return Ok(AttestationType::DcapTdx);
}
}
Ok(AttestationType::None)
}
}
/// SCALE encode (used over the wire)
impl Encode for AttestationType {
fn encode(&self) -> Vec<u8> {
self.as_str().encode()
}
}
/// SCALE decode
impl Decode for AttestationType {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let s: String = String::decode(input)?;
serde_json::from_str(&format!("\"{s}\"")).map_err(|_| "Failed to decode enum".into())
}
}
impl Display for AttestationType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
/// Can generate a local attestation based on attestation type
#[derive(Debug, Clone)]
pub struct AttestationGenerator {
pub attestation_type: AttestationType,
attestation_provider_url: Option<String>,
}
impl AttestationGenerator {
/// Create an attesation generator with given attestation type
pub fn new(
attestation_type: AttestationType,
attestation_provider_url: Option<String>,
) -> Result<Self, AttestationError> {
// If an attestation provider is given, normalize the URL and check that it
// looks like a local IP
let attestation_provider_url =
attestation_provider_url.map(map_attestation_provider_url).transpose()?;
Ok(Self { attestation_type, attestation_provider_url })
}
/// Detect what confidential compute platform is present and create the
/// appropriate attestation generator
pub async fn detect() -> Result<Self, AttestationError> {
Self::new_with_detection(None, None).await
}
/// Do not generate attestations
pub fn with_no_attestation() -> Self {
Self { attestation_type: AttestationType::None, attestation_provider_url: None }
}
/// Create an [AttestationGenerator] detecting the attestation type if
/// it is not given
pub async fn new_with_detection(
attestation_type_string: Option<String>,
attestation_provider_url: Option<String>,
) -> Result<Self, AttestationError> {
if attestation_provider_url.is_some() {
// If a remote provide is used, dont do detection
let attestation_type = serde_json::from_value(serde_json::Value::String(
attestation_type_string.ok_or(AttestationError::AttestationTypeNotGiven)?,
))?;
return Self::new(attestation_type, attestation_provider_url);
};
let attestation_type_string = attestation_type_string.unwrap_or_else(|| "auto".to_string());
let attestation_type = if attestation_type_string == "auto" {
tracing::info!("Doing attestation type detection...");
AttestationType::detect().await?
} else {
serde_json::from_value(serde_json::Value::String(attestation_type_string))?
};
tracing::info!("Local platform: {attestation_type}");
Self::new(attestation_type, None)
}
/// Generate an attestation exchange message with given input data
pub async fn generate_attestation(
&self,
input_data: [u8; 64],
) -> Result<AttestationExchangeMessage, AttestationError> {
if let Some(url) = &self.attestation_provider_url {
Self::use_attestation_provider(url, self.attestation_type, input_data).await
} else {
Ok(AttestationExchangeMessage {
attestation_type: self.attestation_type,
attestation: self.generate_attestation_bytes(input_data)?,
})
}
}
/// Generate attestation evidence bytes based on attestation type, with
/// given input data
fn generate_attestation_bytes(
&self,
input_data: [u8; 64],
) -> Result<Vec<u8>, AttestationError> {
match self.attestation_type {
AttestationType::None => Ok(Vec::new()),
AttestationType::AzureTdx => {
#[cfg(feature = "azure")]
{
Ok(azure::create_azure_attestation(input_data)?)
}
#[cfg(not(feature = "azure"))]
{
tracing::error!(
"Attempted to generate an azure attestation but the `azure` feature not enabled"
);
Err(AttestationError::AttestationTypeNotSupported)
}
}
_ => dcap::create_dcap_attestation(input_data),
}
}
/// Generate an attestation by using an external service for the
/// attestation generation
async fn use_attestation_provider(
url: &str,
attestation_type: AttestationType,
input_data: [u8; 64],
) -> Result<AttestationExchangeMessage, AttestationError> {
let url = format!("{}/attest/{}", url, hex::encode(input_data));
let response = reqwest::get(url)
.await
.map_err(|err| AttestationError::AttestationProvider(err.to_string()))?
.bytes()
.await
.map_err(|err| AttestationError::AttestationProvider(err.to_string()))?
.to_vec();
// If the response is not already wrapped in an attestation exchange
// message, wrap it in one
if let Ok(message) = AttestationExchangeMessage::decode(&mut &response[..]) {
Ok(message)
} else {
Ok(AttestationExchangeMessage { attestation_type, attestation: response })
}
}
}
/// Allows remote attestations to be verified
#[derive(Clone, Debug)]
pub struct AttestationVerifier {
/// The measurement policy with accepted values and attestation types
pub measurement_policy: MeasurementPolicy,
/// If this is empty, anything will be accepted - but measurements are
/// always injected into HTTP headers, so that they can be verified
/// upstream A PCCS service to use - defaults to Intel PCS
pub pccs_url: Option<String>,
/// Whether to log quotes to a file
pub log_dcap_quote: bool,
/// Whether to override outdated TCB when on Azure
pub override_azure_outdated_tcb: bool,
}
impl AttestationVerifier {
/// Create an [AttestationVerifier] which will allow no remote
/// attestation
pub fn expect_none() -> Self {
Self {
measurement_policy: MeasurementPolicy::expect_none(),
pccs_url: None,
log_dcap_quote: false,
override_azure_outdated_tcb: false,
}
}
/// Expect mock measurements used in tests
#[cfg(any(test, feature = "mock"))]
pub fn mock() -> Self {
Self {
measurement_policy: MeasurementPolicy::mock(),
pccs_url: None,
log_dcap_quote: false,
override_azure_outdated_tcb: false,
}
}
/// Verify an attestation, and ensure the measurements match one of our
/// accepted measurements
pub async fn verify_attestation(
&self,
attestation_exchange_message: AttestationExchangeMessage,
expected_input_data: [u8; 64],
) -> Result<Option<MultiMeasurements>, AttestationError> {
let attestation_type = attestation_exchange_message.attestation_type;
tracing::debug!("Verifing {attestation_type} attestation");
if self.log_dcap_quote {
log_attestation(&attestation_exchange_message).await;
}
let measurements = match attestation_type {
AttestationType::None => {
if self.has_remote_attestion() {
return Err(AttestationError::AttestationTypeNotAccepted);
}
if attestation_exchange_message.attestation.is_empty() {
return Ok(None);
} else {
return Err(AttestationError::AttestationGivenWhenNoneExpected);
}
}
AttestationType::AzureTdx => {
#[cfg(feature = "azure")]
{
azure::verify_azure_attestation(
attestation_exchange_message.attestation,
expected_input_data,
self.pccs_url.clone(),
self.override_azure_outdated_tcb,
)
.await?
}
#[cfg(not(feature = "azure"))]
{
return Err(AttestationError::AttestationTypeNotSupported);
}
}
_ => {
dcap::verify_dcap_attestation(
attestation_exchange_message.attestation,
expected_input_data,
self.pccs_url.clone(),
)
.await?
}
};
// Do a measurement / attestation type policy check
self.measurement_policy.check_measurement(&measurements)?;
tracing::debug!("Verification successful");
Ok(Some(measurements))
}
/// Whether we allow no remote attestation
pub fn has_remote_attestion(&self) -> bool {
self.measurement_policy.has_remote_attestion()
}
}
/// Write attestation data to a log file
async fn log_attestation(attestation: &AttestationExchangeMessage) {
if attestation.attestation_type != AttestationType::None {
let timestamp =
SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_nanos();
let filename = format!("quotes/{}-{}", attestation.attestation_type, timestamp);
if let Err(err) = tokio::fs::write(&filename, attestation.attestation.clone()).await {
tracing::warn!("Failed to write {filename}: {err}");
}
}
}
/// Test whether it looks like we are running on GCP by hitting the metadata
/// API
async fn running_on_gcp() -> Result<bool, AttestationError> {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert("Metadata-Flavor", "Google".parse().expect("Cannot parse header"));
let client = reqwest::Client::builder()
.timeout(Duration::from_millis(200))
.default_headers(headers)
.build()?;
let resp = client.get(GCP_METADATA_API).send().await;
if let Ok(r) = resp {
return Ok(r.status().is_success() &&
r.headers().get("Metadata-Flavor").map(|v| v == "Google").unwrap_or(false));
}
Ok(false)
}
/// If an attestion provider service is used, we ensure that it looks like a
/// local IP
///
/// This is to avoid dangerous configuration where the attestation is
/// provided by a remote machine
///
/// This by no means guarantees a safe configuration
fn map_attestation_provider_url(url: String) -> Result<String, AttestationError> {
// Fist put it in the format that reqwest expects
let url = if url.starts_with("http://") || url.starts_with("https://") {
url.to_string()
} else {
format!("http://{}", url.trim_start_matches("http://"))
};
let url = url.strip_suffix('/').unwrap_or(&url).to_string();
// If compiled in test mode, skip this check
if !cfg!(test) {
let parsed = url
.parse::<std::net::SocketAddr>()
.or_else(|_| {
// Try parsing as a URL to extract host
let parsed = url.parse::<http::Uri>().map_err(|_| "Invalid URL")?;
let host = parsed.host().ok_or("URL missing host")?;
host.parse::<std::net::IpAddr>()
.map_err(|_| "Only local IP addresses may be used as attestation provider URL")
.map(|ip| std::net::SocketAddr::new(ip, 0))
})
.map_err(|e| AttestationError::AttestationProviderUrl(e.to_string()))?;
if !is_local_ip(parsed.ip()) {
return Err(AttestationError::AttestationProviderUrl(
"Given URL does not appear to contain a local IP address".to_string(),
));
}
}
Ok(url)
}
/// Check if an IP address looks like it is local
fn is_local_ip(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => v4.is_private() || v4.is_loopback() || v4.is_link_local(),
IpAddr::V6(v6) => v6.is_loopback() || v6.is_unique_local() || v6.is_unicast_link_local(),
}
}
/// An error when generating or verifying an attestation
#[derive(Error, Debug)]
pub enum AttestationError {
#[error("Certificate chain is empty")]
NoCertificate,
#[error("X509 parse: {0}")]
X509Parse(#[from] x509_parser::asn1_rs::Err<x509_parser::error::X509Error>),
#[error("X509: {0}")]
X509(#[from] x509_parser::error::X509Error),
#[error("Configuration mismatch - expected no remote attestation")]
AttestationGivenWhenNoneExpected,
#[error("Configfs-tsm quote generation: {0}")]
QuoteGeneration(#[from] configfs_tsm::QuoteGenerationError),
#[error("DCAP verification: {0}")]
DcapVerification(#[from] DcapVerificationError),
#[error("Attestation type not supported")]
AttestationTypeNotSupported,
#[error("Attestation type not accepted")]
AttestationTypeNotAccepted,
#[error("Measurements not accepted")]
MeasurementsNotAccepted,
#[cfg(feature = "azure")]
#[error("MAA: {0}")]
Maa(#[from] azure::MaaError),
#[error("If using a an attestation provider an attestation type must be given")]
AttestationTypeNotGiven,
#[error("Attestation provider server: {0}")]
AttestationProvider(String),
#[error("Attestation provider URL: {0}")]
AttestationProviderUrl(String),
#[error("JSON: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("HTTP client: {0}")]
Reqwest(#[from] reqwest::Error),
}
#[cfg(test)]
mod tests {
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpListener,
};
use super::*;
async fn spawn_test_attestation_provider_server(body: Vec<u8>) -> std::net::SocketAddr {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
if let Ok((mut socket, _)) = listener.accept().await {
let mut buf = [0u8; 1024];
let _ = socket.read(&mut buf).await;
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
);
let _ = socket.write_all(response.as_bytes()).await;
let _ = socket.write_all(&body).await;
let _ = socket.shutdown().await;
}
});
addr
}
#[tokio::test]
async fn attestation_detection_does_not_panic() {
// We dont enforce what platform the test is run on, only that the function
// does not panic
let _ = AttestationGenerator::new_with_detection(None, None).await;
}
#[tokio::test]
async fn running_on_gcp_check_does_not_panic() {
let _ = running_on_gcp().await;
}
#[tokio::test]
async fn attestation_provider_response_is_wrapped_if_needed() {
let input_data = [0u8; 64];
let encoded_message = AttestationExchangeMessage {
attestation_type: AttestationType::None,
attestation: vec![1, 2, 3],
}
.encode();
let encoded_addr = spawn_test_attestation_provider_server(encoded_message).await;
let encoded_url = format!("http://{encoded_addr}");
let decoded = AttestationGenerator::use_attestation_provider(
&encoded_url,
AttestationType::GcpTdx,
input_data,
)
.await
.unwrap();
assert_eq!(decoded.attestation_type, AttestationType::None);
assert_eq!(decoded.attestation, vec![1, 2, 3]);
let raw_addr = spawn_test_attestation_provider_server(vec![9, 8]).await;
let raw_url = format!("http://{raw_addr}");
let wrapped = AttestationGenerator::use_attestation_provider(
&raw_url,
AttestationType::DcapTdx,
input_data,
)
.await
.unwrap();
assert_eq!(wrapped.attestation_type, AttestationType::DcapTdx);
assert_eq!(wrapped.attestation, vec![9, 8]);
}
}