-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Initial implementation of Wasi-tls (Transport Layer Security) #10249
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
Merged
alexcrichton
merged 18 commits into
bytecodealliance:main
from
jsturtevant:wasi-tls-initial
Mar 7, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c8240a4
Initial implementation of wasi-tls
jsturtevant 7ce991e
Remove configuration object for now
jsturtevant e312f2a
Update cargo patch to use temp branch
jsturtevant 6cbcf43
Rename tcp streams to wasistreams to be more generic
jsturtevant 042d8b8
gate the wasi-tls ctx behind a feature
jsturtevant 0257625
cleanup and clippy fixes
jsturtevant 8a8c32f
Fix issue when another pollable cancels
jsturtevant 0d66245
prtest:full
jsturtevant f2b4ade
Skip test on riscv64/s390x
jsturtevant 7b22d5f
Drop debug info to support tests on pulley based platforms
jsturtevant 470034d
Update signature of `close-notify`
badeend 332fbf5
Use draft version
jsturtevant 490eb35
Remove patches
jsturtevant 1f85eaa
Ungate tls on riscv64 and s390x
alexcrichton e8bbb29
Un-gate wais-http on riscv64/s390x as well
alexcrichton 221f508
Add wasmtime-wasi-tls to publish list
alexcrichton 26244c8
Add wasmtime-wasi-tls to public API crate list
alexcrichton 0deabf5
Revert some changes to Cargo.lock
alexcrichton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use core::str; | ||
|
||
use test_programs::wasi::sockets::network::{IpSocketAddress, Network}; | ||
use test_programs::wasi::sockets::tcp::{ShutdownType, TcpSocket}; | ||
use test_programs::wasi::tls::types::ClientHandshake; | ||
|
||
fn test_tls_sample_application() { | ||
const PORT: u16 = 443; | ||
const DOMAIN: &'static str = "example.com"; | ||
|
||
let request = format!("GET / HTTP/1.1\r\nHost: {DOMAIN}\r\n\r\n"); | ||
|
||
let net = Network::default(); | ||
|
||
let Some(ip) = net | ||
.permissive_blocking_resolve_addresses(DOMAIN) | ||
.unwrap() | ||
.first() | ||
.map(|a| a.to_owned()) | ||
else { | ||
eprintln!("DNS lookup failed."); | ||
return; | ||
}; | ||
|
||
let socket = TcpSocket::new(ip.family()).unwrap(); | ||
let (tcp_input, tcp_output) = socket | ||
.blocking_connect(&net, IpSocketAddress::new(ip, PORT)) | ||
.unwrap(); | ||
|
||
let (client_connection, tls_input, tls_output) = | ||
ClientHandshake::new(DOMAIN, tcp_input, tcp_output) | ||
.blocking_finish() | ||
.unwrap(); | ||
|
||
tls_output.blocking_write_util(request.as_bytes()).unwrap(); | ||
client_connection | ||
.blocking_close_output(&tls_output) | ||
.unwrap(); | ||
socket.shutdown(ShutdownType::Send).unwrap(); | ||
let response = tls_input.blocking_read_to_end().unwrap(); | ||
let response = String::from_utf8(response).unwrap(); | ||
|
||
assert!(response.contains("HTTP/1.1 200 OK")); | ||
} | ||
|
||
fn main() { | ||
test_tls_sample_application(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ pub mod http; | |
pub mod nn; | ||
pub mod preview1; | ||
pub mod sockets; | ||
pub mod tls; | ||
|
||
wit_bindgen::generate!({ | ||
inline: " | ||
|
@@ -12,15 +13,17 @@ wit_bindgen::generate!({ | |
include wasi:http/[email protected]; | ||
include wasi:config/[email protected]; | ||
include wasi:keyvalue/[email protected]; | ||
include wasi:tls/[email protected]; | ||
} | ||
", | ||
path: [ | ||
"../wasi-http/wit", | ||
"../wasi-config/wit", | ||
"../wasi-keyvalue/wit", | ||
"../wasi-tls/wit/world.wit", | ||
], | ||
world: "wasmtime:test/test", | ||
features: ["cli-exit-with-code"], | ||
features: ["cli-exit-with-code", "tls"], | ||
generate_all, | ||
}); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::wasi::clocks::monotonic_clock; | ||
use crate::wasi::io::streams::StreamError; | ||
use crate::wasi::tls::types::{ClientConnection, ClientHandshake, InputStream, OutputStream}; | ||
|
||
const TIMEOUT_NS: u64 = 1_000_000_000; | ||
|
||
impl ClientHandshake { | ||
pub fn blocking_finish(self) -> Result<(ClientConnection, InputStream, OutputStream), ()> { | ||
let future = ClientHandshake::finish(self); | ||
let timeout = monotonic_clock::subscribe_duration(TIMEOUT_NS * 200); | ||
let pollable = future.subscribe(); | ||
|
||
loop { | ||
match future.get() { | ||
None => pollable.block_until(&timeout).expect("timed out"), | ||
Some(Ok(r)) => return r, | ||
Some(Err(e)) => { | ||
eprintln!("{e:?}"); | ||
unimplemented!() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl ClientConnection { | ||
pub fn blocking_close_output( | ||
&self, | ||
output: &OutputStream, | ||
) -> Result<(), crate::wasi::io::error::Error> { | ||
let timeout = monotonic_clock::subscribe_duration(TIMEOUT_NS); | ||
let pollable = output.subscribe(); | ||
|
||
self.close_output(); | ||
|
||
loop { | ||
match output.check_write() { | ||
Ok(0) => pollable.block_until(&timeout).expect("timed out"), | ||
Ok(_) => unreachable!("After calling close_output, the output stream should never accept new writes again."), | ||
Err(StreamError::Closed) => return Ok(()), | ||
Err(StreamError::LastOperationFailed(e)) => return Err(e), | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "wasmtime-wasi-tls" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
repository = "https://github.com/bytecodealliance/wasmtime" | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
description = "Wasmtime implementation of the wasi-tls API" | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
bytes = { workspace = true } | ||
tokio = { workspace = true, features = [ | ||
"net", | ||
"rt-multi-thread", | ||
"time", | ||
] } | ||
wasmtime = { workspace = true, features = ["runtime", "component-model"] } | ||
wasmtime-wasi = { workspace = true } | ||
tokio-rustls = { workspace = true } | ||
rustls = { workspace = true } | ||
webpki-roots = { workspace = true } | ||
|
||
|
||
[dev-dependencies] | ||
test-programs-artifacts = { workspace = true } | ||
wasmtime-wasi = { workspace = true } | ||
tokio = { workspace = true, features = ["macros"] } | ||
futures = { workspace = true } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)