Skip to content

Commit

Permalink
(wip) use traits for deser
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Feb 19, 2025
1 parent eee2f62 commit 393b1d3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
20 changes: 14 additions & 6 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate core;

pub mod tlsn;
#[cfg(not(target_arch = "wasm32"))] mod tlsn_native;
#[cfg(target_arch = "wasm32")] mod tlsn_wasm32;
Expand Down Expand Up @@ -189,13 +191,19 @@ pub struct TeeProof {
pub signature: SignedVerificationReply,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TeeProofData {
pub manifest_hash: Vec<u8>,
impl TryFrom<&[u8]> for TeeProof {
type Error = serde_json::Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { serde_json::from_slice(bytes) }
}

impl TeeProof {
pub fn to_bytes(&self) -> serde_json::Result<Vec<u8>> { serde_json::to_vec(&self) }
impl TryFrom<TeeProof> for Vec<u8> {
type Error = serde_json::Error;

fn from_bytes(bytes: &[u8]) -> serde_json::Result<TeeProof> { serde_json::from_slice(bytes) }
fn try_from(proof: TeeProof) -> Result<Self, Self::Error> { serde_json::to_vec(&proof) }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct TeeProofData {
pub manifest_hash: Vec<u8>,
}
24 changes: 14 additions & 10 deletions client/src/origo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ pub(crate) async fn generate_proof(
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OrigoSecrets(HashMap<String, Vec<u8>>);

impl TryFrom<&OrigoSecrets> for Vec<u8> {
type Error = serde_json::Error;

fn try_from(secrets: &OrigoSecrets) -> Result<Self, Self::Error> { serde_json::to_vec(secrets) }
}

impl TryFrom<&[u8]> for OrigoSecrets {
type Error = serde_json::Error;

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> { serde_json::from_slice(bytes) }
}

impl OrigoSecrets {
pub fn handshake_server_iv(&self) -> Option<Vec<u8>> {
self.0.get("Handshake:server_iv").cloned()
Expand All @@ -231,10 +243,6 @@ impl OrigoSecrets {
pub fn from_origo_conn(origo_conn: &OrigoConnection) -> Self {
Self(origo_conn.secret_map.clone())
}

pub fn to_bytes(&self) -> serde_json::Result<Vec<u8>> { serde_json::to_vec(&self) }

pub fn from_bytes(bytes: &[u8]) -> serde_json::Result<Self> { serde_json::from_slice(bytes) }
}

#[cfg(test)]
Expand All @@ -246,12 +254,8 @@ mod tests {
origo_conn.secret_map.insert("Handshake:server_iv".to_string(), vec![1, 2, 3]);
let origo_secrets = OrigoSecrets::from_origo_conn(&origo_conn);

let serialized = origo_secrets.to_bytes().unwrap();
let deserialized: OrigoSecrets = OrigoSecrets::from_bytes(&serialized).unwrap();
let serialized: Vec<u8> = origo_secrets.try_into().unwrap();
let deserialized: OrigoSecrets = OrigoSecrets::try_from(&serialized).unwrap();
assert_eq!(origo_secrets, deserialized);

let wire_serialized = origo_secrets.to_bytes().unwrap();
let wire_deserialized: OrigoSecrets = OrigoSecrets::from_bytes(&wire_serialized).unwrap();
assert_eq!(origo_secrets, wire_deserialized);
}
}
8 changes: 4 additions & 4 deletions client/src/origo_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ async fn handle_origo_mode(
tokio::spawn(handled_connection_fut);

let response = request_sender.send_request(config.to_request()?).await?;

assert_eq!(response.status(), StatusCode::OK);

let payload = response.into_body().collect().await?.to_bytes();
Expand Down Expand Up @@ -279,17 +278,18 @@ async fn handle_tee_mode(
let mut framed_reunited_socket =
Framed::new(reunited_socket.compat(), LengthDelimitedCodec::new());

let manifest_bytes: Vec<u8> = config.proving.manifest.unwrap().try_into().unwrap();
let manifest_bytes: Vec<u8> = config.proving.manifest.unwrap().try_into()?;
framed_reunited_socket.send(Bytes::from(manifest_bytes)).await?;

let origo_secret_bytes = OrigoSecrets::from_origo_conn(&origo_conn).to_bytes()?;
let origo_secret = OrigoSecrets::from_origo_conn(&origo_conn);
let origo_secret_bytes: Vec<u8> = (&origo_secret).try_into().unwrap();
framed_reunited_socket.send(Bytes::from(origo_secret_bytes)).await?;

framed_reunited_socket.flush().await?;

let tee_proof_frame =
framed_reunited_socket.next().await.ok_or_else(|| ClientErrors::TeeProofMissing)??;
let tee_proof = TeeProof::from_bytes(&tee_proof_frame)?;
let tee_proof = TeeProof::try_from(tee_proof_frame.as_ref())?;
debug!("TeeProof: {:?}", tee_proof);

Ok((origo_conn, tee_proof))
Expand Down
12 changes: 7 additions & 5 deletions client/src/origo_wasm32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async fn handle_tee_mode(
let (mut request_sender, connection) =
hyper::client::conn::http1::handshake(client_tls_conn).await?;

let (connection_sender, connection_receiver) = oneshot::channel();
let (connection_sender, _) = oneshot::channel();
let connection_fut = connection.without_shutdown();
spawn_local(async {
let result = connection_fut.await;
Expand Down Expand Up @@ -197,17 +197,19 @@ async fn handle_tee_mode(
let mut framed_reunited_socket =
Framed::new(reunited_socket.compat(), LengthDelimitedCodec::new());

let manifest_bytes: Vec<u8> = config.proving.manifest.unwrap().try_into()?;
let manifest = config.proving.manifest.unwrap();
let manifest_bytes: Vec<u8> = (&manifest).try_into().unwrap();
framed_reunited_socket.send(Bytes::from(manifest_bytes)).await?;

let origo_secret_bytes = OrigoSecrets::from_origo_conn(&origo_conn).to_bytes()?;
framed_reunited_socket.send(Bytes::from(origo_secret_bytes)).await?;
let origo_secrets = OrigoSecrets::from_origo_conn(&origo_conn);
let origo_secrets_bytes: Vec<u8> = (&origo_secrets).try_into().unwrap();
framed_reunited_socket.send(Bytes::from(origo_secrets_bytes)).await?;

framed_reunited_socket.flush().await?;

let tee_proof_frame =
framed_reunited_socket.next().await.ok_or_else(|| ClientErrors::TeeProofMissing)??;
let tee_proof = TeeProof::from_bytes(&tee_proof_frame)?;
let tee_proof = TeeProof::try_from(tee_proof_frame.as_ref())?;

// TODO something will be dropped here. if it's dropped, it closes ...
// let mut client_socket = connection_receiver.await.unwrap()?.io.into_inner();
Expand Down
9 changes: 5 additions & 4 deletions notary/src/origo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ pub async fn sign(

// Log a verifier session for public inputs
debug!("inserting with session_id={:?}", query.session_id);
state.verifier_sessions.lock().unwrap().insert(query.session_id.to_string(), VerifierInputs {
request_messages: request_messages.clone(),
response_messages: response_messages.clone(),
});
state
.verifier_sessions
.lock()
.unwrap()
.insert(query.session_id.to_string(), VerifierInputs { request_messages, response_messages });

// TODO check OSCP and CT (maybe)
// TODO check target_name matches SNI and/or cert name (let's discuss)
Expand Down
5 changes: 3 additions & 2 deletions notary/src/tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub async fn tee_proxy_service<S: AsyncWrite + AsyncRead + Send + Unpin>(

let secret_frame =
framed_stream.next().await.ok_or_else(|| NotaryServerError::MissingOrigoSecrets)??;
let origo_secrets = OrigoSecrets::from_bytes(&secret_frame)?;
let origo_secrets = OrigoSecrets::try_from(secret_frame.as_ref())?;
// dbg!(&origo_secrets);

let handshake_server_key =
Expand Down Expand Up @@ -198,7 +198,8 @@ pub async fn tee_proxy_service<S: AsyncWrite + AsyncRead + Send + Unpin>(

// send TeeProof to client
let tee_proof = create_tee_proof(&manifest, &request, &response, State(state))?;
framed_stream.send(Bytes::from(tee_proof.to_bytes()?)).await?;
let tee_proof_bytes: Vec<u8> = tee_proof.try_into()?;
framed_stream.send(Bytes::from(tee_proof_bytes)).await?;
framed_stream.flush().await?;

Ok(())
Expand Down

0 comments on commit 393b1d3

Please sign in to comment.