Skip to content

Commit 7729575

Browse files
committed
Use SCALE rather than JSON for encoding attestation payloads
1 parent 41823ae commit 7729575

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 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
@@ -28,6 +28,7 @@ bytes = "1.10.1"
2828
http = "1.3.1"
2929
serde_json = "1.0.145"
3030
serde = "1.0.228"
31+
parity-scale-codec = "3.7.5"
3132

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

src/attestation/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod measurements;
22

33
use measurements::{CvmImageMeasurements, MeasurementRecord, Measurements, PlatformMeasurements};
4+
use parity_scale_codec::{Decode, Encode};
45
use serde::{Deserialize, Serialize};
56
use std::{
67
fmt::{self, Display, Formatter},
@@ -22,7 +23,7 @@ use x509_parser::prelude::*;
2223
/// For fetching collateral directly from intel, if no PCCS is specified
2324
const PCS_URL: &str = "https://api.trustedservices.intel.com";
2425

25-
#[derive(Debug, Serialize, Deserialize)]
26+
#[derive(Debug, Serialize, Deserialize, Encode, Decode)]
2627
pub struct AttesationPayload {
2728
pub attestation_type: AttestationType,
2829
pub attestation: Vec<u8>,
@@ -85,6 +86,21 @@ impl AttestationType {
8586
}
8687
}
8788

89+
impl Encode for AttestationType {
90+
fn encode(&self) -> Vec<u8> {
91+
self.as_str().encode()
92+
}
93+
}
94+
95+
impl Decode for AttestationType {
96+
fn decode<I: parity_scale_codec::Input>(
97+
input: &mut I,
98+
) -> Result<Self, parity_scale_codec::Error> {
99+
let s: String = String::decode(input)?;
100+
serde_json::from_str(&format!("\"{s}\"")).map_err(|_| "Failed to decode enum".into())
101+
}
102+
}
103+
88104
impl Display for AttestationType {
89105
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
90106
f.write_str(self.as_str())

src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use http_body_util::BodyExt;
99
use hyper::service::service_fn;
1010
use hyper::Response;
1111
use hyper_util::rt::TokioIo;
12+
use parity_scale_codec::Decode;
13+
use parity_scale_codec::Encode;
1214
use thiserror::Error;
1315
use tokio::sync::{mpsc, oneshot};
1416
use tokio_rustls::rustls::server::{VerifierBuilderError, WebPkiClientVerifier};
@@ -192,11 +194,12 @@ impl ProxyServer {
192194

193195
// If we are in a CVM, generate an attestation
194196
let attestation = if local_quote_generator.attestation_type() != AttestationType::None {
195-
serde_json::to_vec(&AttesationPayload::from_attestation_generator(
197+
AttesationPayload::from_attestation_generator(
196198
&cert_chain,
197199
exporter,
198200
local_quote_generator,
199-
)?)?
201+
)?
202+
.encode()
200203
} else {
201204
Vec::new()
202205
};
@@ -218,7 +221,7 @@ impl ProxyServer {
218221
// If we expect an attestaion from the client, verify it and get measurements
219222
let (measurements, remote_attestation_type) = if attestation_verifier.has_remote_attestion()
220223
{
221-
let remote_attestation_payload: AttesationPayload = serde_json::from_slice(&buf)?;
224+
let remote_attestation_payload = AttesationPayload::decode(&mut &buf[..])?;
222225

223226
let remote_attestation_type = remote_attestation_payload.attestation_type;
224227
(
@@ -607,7 +610,7 @@ impl ProxyClient {
607610
let mut buf = vec![0; length];
608611
tls_stream.read_exact(&mut buf).await?;
609612

610-
let remote_attestation_payload: AttesationPayload = serde_json::from_slice(&buf)?;
613+
let remote_attestation_payload = AttesationPayload::decode(&mut &buf[..])?;
611614
let remote_attestation_type = remote_attestation_payload.attestation_type;
612615

613616
// Verify the remote attestation against our accepted measurements
@@ -617,11 +620,12 @@ impl ProxyClient {
617620

618621
// If we are in a CVM, provide an attestation
619622
let attestation = if local_quote_generator.attestation_type() != AttestationType::None {
620-
serde_json::to_vec(&AttesationPayload::from_attestation_generator(
623+
AttesationPayload::from_attestation_generator(
621624
&cert_chain.ok_or(ProxyError::NoClientAuth)?,
622625
exporter,
623626
local_quote_generator,
624-
)?)?
627+
)?
628+
.encode()
625629
} else {
626630
Vec::new()
627631
};
@@ -705,7 +709,7 @@ async fn get_tls_cert_with_config(
705709
let mut buf = vec![0; length];
706710
tls_stream.read_exact(&mut buf).await?;
707711

708-
let remote_attestation_payload: AttesationPayload = serde_json::from_slice(&buf)?;
712+
let remote_attestation_payload = AttesationPayload::decode(&mut &buf[..])?;
709713

710714
let _measurements = attestation_verifier
711715
.verify_attestation(remote_attestation_payload, &remote_cert_chain, exporter)
@@ -741,6 +745,8 @@ pub enum ProxyError {
741745
OneShotRecv(#[from] oneshot::error::RecvError),
742746
#[error("Failed to send request, connection to proxy-server dropped")]
743747
MpscSend,
748+
#[error("Serialization: {0}")]
749+
Serialization(#[from] parity_scale_codec::Error),
744750
}
745751

746752
impl From<mpsc::error::SendError<RequestWithResponseSender>> for ProxyError {

0 commit comments

Comments
 (0)