Skip to content

Commit 98bf7bd

Browse files
committed
Tidy
1 parent 7729575 commit 98bf7bd

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

src/attestation/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ impl AttesationPayload {
4040
attestation: attesation_generator.create_attestation(cert_chain, exporter)?,
4141
})
4242
}
43+
44+
pub fn without_attestation() -> Self {
45+
Self {
46+
attestation_type: AttestationType::None,
47+
attestation: Vec::new(),
48+
}
49+
}
4350
}
4451

4552
/// Type of attestaion used

src/lib.rs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ use attestation::{measurements::Measurements, AttestationError, AttestationType}
44
pub use attestation::{DcapTdxQuoteGenerator, NoQuoteGenerator, QuoteGenerator};
55
use bytes::Bytes;
66
use http::HeaderValue;
7-
use http_body_util::combinators::BoxBody;
8-
use http_body_util::BodyExt;
9-
use hyper::service::service_fn;
10-
use hyper::Response;
7+
use http_body_util::{combinators::BoxBody, BodyExt};
8+
use hyper::{service::service_fn, Response};
119
use hyper_util::rt::TokioIo;
12-
use parity_scale_codec::Decode;
13-
use parity_scale_codec::Encode;
10+
use parity_scale_codec::{Decode, Encode};
1411
use thiserror::Error;
1512
use tokio::sync::{mpsc, oneshot};
1613
use tokio_rustls::rustls::server::{VerifierBuilderError, WebPkiClientVerifier};
@@ -193,16 +190,12 @@ impl ProxyServer {
193190
let remote_cert_chain = connection.peer_certificates().map(|c| c.to_owned());
194191

195192
// If we are in a CVM, generate an attestation
196-
let attestation = if local_quote_generator.attestation_type() != AttestationType::None {
197-
AttesationPayload::from_attestation_generator(
198-
&cert_chain,
199-
exporter,
200-
local_quote_generator,
201-
)?
202-
.encode()
203-
} else {
204-
Vec::new()
205-
};
193+
let attestation = AttesationPayload::from_attestation_generator(
194+
&cert_chain,
195+
exporter,
196+
local_quote_generator,
197+
)?
198+
.encode();
206199

207200
// Write our attestation to the channel, with length prefix
208201
let attestation_length_prefix = length_prefix(&attestation);
@@ -218,24 +211,20 @@ impl ProxyServer {
218211
let mut buf = vec![0; length];
219212
tls_stream.read_exact(&mut buf).await?;
220213

214+
let remote_attestation_payload = AttesationPayload::decode(&mut &buf[..])?;
215+
let remote_attestation_type = remote_attestation_payload.attestation_type;
216+
221217
// If we expect an attestaion from the client, verify it and get measurements
222-
let (measurements, remote_attestation_type) = if attestation_verifier.has_remote_attestion()
223-
{
224-
let remote_attestation_payload = AttesationPayload::decode(&mut &buf[..])?;
225-
226-
let remote_attestation_type = remote_attestation_payload.attestation_type;
227-
(
228-
attestation_verifier
229-
.verify_attestation(
230-
remote_attestation_payload,
231-
&remote_cert_chain.ok_or(ProxyError::NoClientAuth)?,
232-
exporter,
233-
)
234-
.await?,
235-
remote_attestation_type,
236-
)
218+
let measurements = if attestation_verifier.has_remote_attestion() {
219+
attestation_verifier
220+
.verify_attestation(
221+
remote_attestation_payload,
222+
&remote_cert_chain.ok_or(ProxyError::NoClientAuth)?,
223+
exporter,
224+
)
225+
.await?
237226
} else {
238-
(None, AttestationType::None)
227+
None
239228
};
240229

241230
// Setup an HTTP server
@@ -627,7 +616,7 @@ impl ProxyClient {
627616
)?
628617
.encode()
629618
} else {
630-
Vec::new()
619+
AttesationPayload::without_attestation().encode()
631620
};
632621

633622
// Send our attestation (or zero bytes) prefixed with length

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,13 @@ fn load_tls_cert_and_key(
243243
Ok(TlsCertAndKey { key, cert_chain })
244244
}
245245

246+
/// load certificates from a PEM-encoded file
246247
fn load_certs_pem(path: PathBuf) -> std::io::Result<Vec<CertificateDer<'static>>> {
247248
rustls_pemfile::certs(&mut std::io::BufReader::new(File::open(path)?))
248249
.collect::<Result<Vec<_>, _>>()
249250
}
250251

252+
/// load TLS private key from a PEM-encoded file
251253
fn load_private_key_pem(path: PathBuf) -> anyhow::Result<PrivateKeyDer<'static>> {
252254
let mut reader = std::io::BufReader::new(File::open(path)?);
253255

0 commit comments

Comments
 (0)