Skip to content

Commit 7b3b455

Browse files
committed
Encode attestation type as enum
1 parent ef5d5f6 commit 7b3b455

File tree

6 files changed

+38
-26
lines changed

6 files changed

+38
-26
lines changed

Cargo.lock

Lines changed: 10 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bytes = "1.10.1"
2727
http = "1.3.1"
2828
serde_json = "1.0.145"
2929
serde = "1.0.228"
30+
serde_plain = "1.0.2"
3031

3132
[dev-dependencies]
3233
rcgen = "0.14.5"

src/attestation/measurements.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ pub async fn get_measurements_from_file(
174174
for measurement in measurements_simple {
175175
measurements.push(MeasurementRecord {
176176
measurement_id: measurement.measurement_id,
177-
attestation_type: AttestationType::parse_from_str(&measurement.attestation_type)
178-
.map_err(|_| MeasurementFormatError::AttestationTypeNotValid)?,
177+
attestation_type: serde_json::from_value(serde_json::Value::String(
178+
measurement.attestation_type,
179+
))
180+
.map_err(|_| MeasurementFormatError::AttestationTypeNotValid)?,
179181
measurements: Measurements {
180182
platform: PlatformMeasurements {
181183
mrtd: hex::decode(&measurement.measurements["0"].expected)?

src/attestation/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const PCS_URL: &str = "https://api.trustedservices.intel.com";
2424

2525
#[derive(Debug, Serialize, Deserialize)]
2626
pub struct AttesationPayload {
27-
pub attestation_type: String, // TODO should be AttestationType
27+
pub attestation_type: AttestationType,
2828
pub attestation: Vec<u8>,
2929
}
3030

@@ -35,15 +35,16 @@ impl AttesationPayload {
3535
attesation_generator: Arc<dyn QuoteGenerator>,
3636
) -> Result<Self, AttestationError> {
3737
Ok(Self {
38-
attestation_type: attesation_generator.attestation_type().as_str().to_string(),
38+
attestation_type: attesation_generator.attestation_type(),
3939
attestation: attesation_generator.create_attestation(cert_chain, exporter)?,
4040
})
4141
}
4242
}
4343

4444
/// Type of attestaion used
4545
/// Only supported (or soon-to-be supported) types are given
46-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
46+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
47+
#[serde(rename_all = "kebab-case")]
4748
pub enum AttestationType {
4849
/// No attestion
4950
None,
@@ -72,17 +73,17 @@ impl AttestationType {
7273
}
7374
}
7475

75-
pub fn parse_from_str(input: &str) -> Result<Self, AttestationError> {
76-
match input {
77-
"none" => Ok(Self::None),
78-
"dummy" => Ok(Self::Dummy),
79-
"azure-tdx" => Ok(Self::AzureTdx),
80-
"qemu-tdx" => Ok(Self::QemuTdx),
81-
"dcap-tdx" => Ok(Self::DcapTdx),
82-
"gcp-tdx" => Ok(Self::GcpTdx),
83-
_ => Err(AttestationError::AttestationTypeNotSupported),
84-
}
85-
}
76+
// pub fn parse_from_str(input: &str) -> Result<Self, AttestationError> {
77+
// match input {
78+
// "none" => Ok(Self::None),
79+
// "dummy" => Ok(Self::Dummy),
80+
// "azure-tdx" => Ok(Self::AzureTdx),
81+
// "qemu-tdx" => Ok(Self::QemuTdx),
82+
// "dcap-tdx" => Ok(Self::DcapTdx),
83+
// "gcp-tdx" => Ok(Self::GcpTdx),
84+
// _ => Err(AttestationError::AttestationTypeNotSupported),
85+
// }
86+
// }
8687

8788
pub fn get_quote_generator(&self) -> Result<Arc<dyn QuoteGenerator>, AttestationError> {
8889
match self {
@@ -154,8 +155,7 @@ impl AttestationVerifier {
154155
cert_chain: &[CertificateDer<'_>],
155156
exporter: [u8; 32],
156157
) -> Result<Option<Measurements>, AttestationError> {
157-
let attestation_type =
158-
AttestationType::parse_from_str(&attestation_payload.attestation_type)?;
158+
let attestation_type = attestation_payload.attestation_type;
159159

160160
let measurements = match attestation_type {
161161
AttestationType::DcapTdx => {

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl ProxyServer {
210210
{
211211
let remote_attestation_payload: AttesationPayload = serde_json::from_slice(&buf)?;
212212

213-
let remote_attestation_type = remote_attestation_payload.attestation_type.clone();
213+
let remote_attestation_type = remote_attestation_payload.attestation_type;
214214
(
215215
attestation_verifier
216216
.verify_attestation(
@@ -222,7 +222,7 @@ impl ProxyServer {
222222
remote_attestation_type,
223223
)
224224
} else {
225-
(None, AttestationType::None.to_string())
225+
(None, AttestationType::None)
226226
};
227227

228228
let http = Builder::new();
@@ -501,8 +501,7 @@ impl ProxyClient {
501501
tls_stream.read_exact(&mut buf).await?;
502502

503503
let remote_attestation_payload: AttesationPayload = serde_json::from_slice(&buf)?;
504-
let remote_attestation_type =
505-
AttestationType::parse_from_str(&remote_attestation_payload.attestation_type)?;
504+
let remote_attestation_type = remote_attestation_payload.attestation_type;
506505

507506
let measurements = attestation_verifier
508507
.verify_attestation(remote_attestation_payload, &remote_cert_chain, exporter)

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ async fn main() -> anyhow::Result<()> {
137137
None => AttestationVerifier::do_not_verify(),
138138
};
139139

140-
let client_attestation_type = AttestationType::parse_from_str(
141-
&client_attestation_type.unwrap_or("none".to_string()),
140+
let client_attestation_type: AttestationType = serde_json::from_value(
141+
serde_json::Value::String(client_attestation_type.unwrap_or("none".to_string())),
142142
)?;
143143

144144
let client_attestation_generator = client_attestation_type.get_quote_generator()?;
@@ -170,8 +170,8 @@ async fn main() -> anyhow::Result<()> {
170170
let tls_cert_and_chain =
171171
load_tls_cert_and_key(tls_certificate_path, tls_private_key_path)?;
172172

173-
let server_attestation_type = AttestationType::parse_from_str(
174-
&server_attestation_type.unwrap_or("none".to_string()),
173+
let server_attestation_type: AttestationType = serde_json::from_value(
174+
serde_json::Value::String(server_attestation_type.unwrap_or("none".to_string())),
175175
)?;
176176

177177
let local_attestation_generator = server_attestation_type.get_quote_generator()?;

0 commit comments

Comments
 (0)