Skip to content

Conversation

@jul-sh
Copy link

@jul-sh jul-sh commented Apr 21, 2022

Ref: #2741, in preparation for #2742

@jul-sh jul-sh force-pushed the extract-session-logic branch from 2807dac to 0ab8ecd Compare April 21, 2022 16:30
remove unused deps

format

oak-prefix crate name for consistency

fix typo
@jul-sh jul-sh force-pushed the extract-session-logic branch from 0ab8ecd to 7c0b815 Compare April 21, 2022 16:32
Comment on lines +29 to +95
pub enum SessionState {
// Boxed due to large size difference, ref: https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
HandshakeInProgress(Box<ServerHandshaker>),
EncryptedMessageExchange(Encryptor),
}

/// Maintains remote attestation state for a number of sessions
pub struct SessionTracker {
/// PEM encoded X.509 certificate that signs TEE firmware key.
tee_certificate: Vec<u8>,
/// Configuration information to provide to the client for the attestation step.
additional_info: Arc<Vec<u8>>,
known_sessions: LruCache<SessionId, SessionState>,
}

impl SessionTracker {
pub fn create(cache_size: usize, tee_certificate: Vec<u8>, additional_info: Vec<u8>) -> Self {
let known_sessions = LruCache::new(cache_size);
Self {
tee_certificate,
additional_info: Arc::new(additional_info),
known_sessions,
}
}

/// Consumes remote attestation state of an existing session. Creates
/// initial state if the session is not known.
///
/// Note that getting the remote attestation state of a session always
/// implicitly removes it from the set of tracked sessions. After
/// using the state to process a request with this state it must explicitly
/// be put back into the SessionTracker. This an intentional choice meant
/// to ensure that faulty state that leads to errors when processing
/// a request is not persistent.
pub fn pop_session_state(&mut self, session_id: SessionId) -> anyhow::Result<SessionState> {
match self.known_sessions.pop(&session_id) {
None => match AttestationBehavior::create_self_attestation(&self.tee_certificate) {
Ok(behavior) => Ok(SessionState::HandshakeInProgress(Box::new(
ServerHandshaker::new(behavior, self.additional_info.clone()),
))),
Err(error) => Err(error.context("Couldn't create self attestation behavior")),
},
Some(SessionState::HandshakeInProgress(handshaker)) => {
// Completed handshakers are functionally just wrap an
// encryptor. In that case the underlying handshaker is
// returned, ensuring consistent state representation.
match handshaker.is_completed() {
false => Ok(SessionState::HandshakeInProgress(handshaker)),
true => match handshaker.get_encryptor() {
Ok(encryptor) => Ok(SessionState::EncryptedMessageExchange(encryptor)),
Err(error) => Err(error.context("Couldn't get encryptor")),
},
}
}
Some(SessionState::EncryptedMessageExchange(encryptor)) => {
Ok(SessionState::EncryptedMessageExchange(encryptor))
}
}
}

/// Record a session in the tracker. Unlike `pop_session_state` it does not
/// normalize session state, instead relying on normalization occuring
/// at retrieval time.
pub fn put_session_state(&mut self, session_id: SessionId, session_state: SessionState) {
self.known_sessions.put(session_id, session_state);
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved without changes

@jul-sh jul-sh marked this pull request as ready for review April 21, 2022 17:06
@jul-sh jul-sh requested a review from a team as a code owner April 21, 2022 17:06
@jul-sh jul-sh requested review from andrisaar and removed request for a team April 21, 2022 17:06
edition = "2021"
license = "Apache-2.0"

[features]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this if you're not declaring any features.

@jul-sh jul-sh merged commit 190f01f into project-oak:main Apr 21, 2022
@jul-sh jul-sh deleted the extract-session-logic branch April 21, 2022 20:56
@github-actions
Copy link

Reproducibility Index:

e3b3a415d6c62143e9318e070faf897a276a7f1c3fd95c61ca2244fd1ed6f2ab  ./target/x86_64-unknown-linux-musl/release/oak_functions_loader_base
83d6028b4d04fcf30623dfb6e10eff47a9cd63097c48eca6e6649f9d8ec68213  ./target/x86_64-unknown-linux-musl/release/oak_functions_loader_unsafe

Reproducibility Index diff:

diff --git a/reproducibility_index b/reproducibility_index
index b08a214..04a8fc0 100644
--- a/reproducibility_index
+++ b/reproducibility_index
@@ -1,2 +1,2 @@
-0553ab40d081e0315c0a93e0876da41cb5268952f3a536cdedf7fd45b34e393e  ./target/x86_64-unknown-linux-musl/release/oak_functions_loader_base
-120e2d3443a3c6f2c2e3cc76b59e62ae1e8bdca89546baa305be454b0571bde6  ./target/x86_64-unknown-linux-musl/release/oak_functions_loader_unsafe
+e3b3a415d6c62143e9318e070faf897a276a7f1c3fd95c61ca2244fd1ed6f2ab  ./target/x86_64-unknown-linux-musl/release/oak_functions_loader_base
+83d6028b4d04fcf30623dfb6e10eff47a9cd63097c48eca6e6649f9d8ec68213  ./target/x86_64-unknown-linux-musl/release/oak_functions_loader_unsafe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants