Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of Wasi-tls (Transport Layer Security) #10249

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

jsturtevant
Copy link
Contributor

@jsturtevant jsturtevant commented Feb 19, 2025

fixes: #10089

This adds a crate that provides the Wasmtime host implementation for the wasi-tls API.
The wasi-tls world allows WebAssembly modules to perform SSL/TLS operations,
such as establishing secure connections to servers.

This initially implements it with rustls which was already included in the repository.

There are a few items that still need to be resolved but are not specially related to the implementation but wanted to start the review to make sure things moving in the correct direction.

Thanks goes out to @badeend and @dicej as much of this was based on their prior work.

jsturtevant and others added 3 commits February 19, 2025 23:34
This crate provides the Wasmtime host implementation for the [wasi-tls] API.
The [wasi-tls] world allows WebAssembly modules to perform SSL/TLS operations,
such as establishing secure connections to servers. TLS often relies on other wasi networking systems
to provide the stream so it will be common to enable the [wasi:cli] world as well with the networking features enabled.

The initial implemntation is using rustls.

Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
Signed-off-by: James Sturtevant <jstur@microsoft.com>
Signed-off-by: James Sturtevant <jstur@microsoft.com>
@jsturtevant jsturtevant requested review from a team as code owners February 19, 2025 23:40
@jsturtevant jsturtevant requested review from pchickey and removed request for a team February 19, 2025 23:40
@github-actions github-actions bot added the wasi Issues pertaining to WASI label Feb 20, 2025
@badeend
Copy link
Contributor

badeend commented Feb 20, 2025

Looking great!

CI fails because Clippy being Clippy, and I suspect the build fails because there's some missing conditional compilation.

Aside from that, I;ve left behind some remarks:

/// Represents the ClientHandshake which will be used to configure the handshake
pub struct ClientHandShake {
server_name: String,
streams: TcpStreams,
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to rename references to the term "TCP" within this file to something more generic. Since this TLS implementation doesn't actually depend on TCP, and the inner streams may come from anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Comment on lines 220 to 223
let StreamState::Pending(future) = mem::replace(&mut self.0, StreamState::Closed) else {
unreachable!()
};
self.0 = StreamState::Ready(future.await);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is cancel-safe. If the .await is canceled, the streamState will be left behind in the Closed state forever. Cancellation may happen when waiting for multiple Pollables at once.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will take a look, is there an example of how to test that?

Copy link
Contributor

Choose a reason for hiding this comment

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

To reproduce, you could try adjusting the timeout of blocking_finish in crates/test-programs/src/tls.rs to something non-0, but very small, so that both pollables are polled at least once, but the monotonic_clock pollable will finish first. After that, I suspect that the ClientHandshake::finish pollable will never resolve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are indeed correct.

Copy link
Contributor Author

@jsturtevant jsturtevant Feb 21, 2025

Choose a reason for hiding this comment

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

I've pushed an update for this, I tested it locally but working getting a test written for this scenario. Let me know if the change I made makes more sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added test that covers this in f13938e

output: StreamState<BoxOutputStream>,
}

impl AsyncWrite for TcpStreams {
Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding this impl AsyncWrite for TcpStreams block and the impl AsyncRead for TcpStreams block below: I'm fine with them existing here, but AFAIK these have nothing to do with TLS right? If so, we could move them into the wasi-io subcrate at some point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correct, not specific to TLS it just enables passing wasi-io streams to libraries that require those traits (i.e. in this case the connect() method for rustls)

}
",
path: [
"../wasi-http/wit",
"../wasi-config/wit",
"../wasi-keyvalue/wit",
"../wasi-tls/wit/world.wit",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for this being the wit file directly is bytecodealliance/wasm-tools#1996 (comment)

Cargo.toml Outdated
wit-parser = { git = "https://github.com/jsturtevant/wasm-tools.git", branch="unstable-refrence-stable" }
wit-component = { git = "https://github.com/jsturtevant/wasm-tools.git", branch="unstable-refrence-stable" }
wasm-wave = { git = "https://github.com/jsturtevant/wasm-tools.git", branch="unstable-refrence-stable" }
wasm-metadata = { git = "https://github.com/jsturtevant/wasm-tools.git", branch="unstable-refrence-stable" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this branch contains the temp fix for bytecodealliance/wasm-tools#1995

Signed-off-by: James Sturtevant <jstur@microsoft.com>
Signed-off-by: James Sturtevant <jstur@microsoft.com>
Signed-off-by: James Sturtevant <jstur@microsoft.com>
Signed-off-by: James Sturtevant <jstur@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi Issues pertaining to WASI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement wasi-tls
2 participants