Skip to main content

Crate wasmtime_wasi_tls

Crate wasmtime_wasi_tls 

Source
Expand description

§Wasmtime’s wasi-tls (Transport Layer Security) Implementation

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.

§An example of how to configure wasi-tls is the following:

use wasmtime_wasi::{WasiCtx, WasiCtxView, WasiView};
use wasmtime::{
    component::{Linker, ResourceTable},
    Store, Engine, Result,
};
use wasmtime_wasi_tls::{WasiTlsCtx, WasiTlsCtxBuilder, WasiTlsView, WasiTlsCtxView};
use wasmtime_wasi_tls::p2::LinkOptions;

struct Ctx {
    table: ResourceTable,
    wasi_ctx: WasiCtx,
    wasi_tls_ctx: WasiTlsCtx,
}

impl WasiView for Ctx {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView { ctx: &mut self.wasi_ctx, table: &mut self.table }
    }
}

impl WasiTlsView for Ctx {
    fn tls(&mut self) -> WasiTlsCtxView<'_> {
        WasiTlsCtxView { ctx: &mut self.wasi_tls_ctx, table: &mut self.table }
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let ctx = Ctx {
        table: ResourceTable::new(),
        wasi_ctx: WasiCtx::builder()
            .inherit_stderr()
            .inherit_network()
            .allow_ip_name_lookup(true)
            .build(),
        wasi_tls_ctx: WasiTlsCtxBuilder::new()
            // Optionally, configure a specific TLS provider:
            // .provider(Box::new(wasmtime_wasi_tls::RustlsProvider::default()))
            // .provider(Box::new(wasmtime_wasi_tls::NativeTlsProvider::default()))
            // .provider(Box::new(wasmtime_wasi_tls::OpenSslProvider::default()))
            .build(),
    };

    let engine = Engine::default();

    // Set up wasi-cli
    let mut store = Store::new(&engine, ctx);
    let mut linker: Linker<Ctx> = Linker::new(&engine);
    wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;

    // Add wasi-tls types and turn on the feature in linker
    let mut opts = LinkOptions::default();
    opts.tls(true);
    wasmtime_wasi_tls::p2::add_to_linker(&mut linker, &opts)?;

    // ... use `linker` to instantiate within `store` ...
    Ok(())
}

Modules§

p2
WASIp2 (wasi:tls@0.2.0-draft) host implementation.
p3
WASIp3 (wasi:tls@0.3.0-draft) host implementation. Experimental, unstable and incomplete implementation of wasip3 version of wasi:tls.

Structs§

DefaultProvider
The rustls provider.
Error
TLS error
NativeTlsProvider
The native_tls provider.
OpenSslProvider
The openssl provider.
RustlsProvider
The rustls provider.
UnsupportedProvider
A pseudo TLS provider that returns an error for all operations. This is the default provider when no real TLS providers were enabled at compile time.
WasiTlsCtx
Wasi TLS context needed for internal wasi-tls state.
WasiTlsCtxBuilder
Builder-style structure used to create a WasiTlsCtx.
WasiTlsCtxView
View into WasiTlsCtx implementation and ResourceTable.

Traits§

TlsProvider
A TLS implementation.
TlsStream
A TLS connection.
TlsTransport
The data stream that carries the encrypted TLS data. Typically this is a TCP stream.
WasiTlsView
A trait which provides internal WASI TLS state.