Module tls

Source
Expand description

TCP+TLS client “plugins” for various (a)sync TLS implementations.

This module offers the following TCP+TLS “plugins” for use with the Client interface.

Every “plugin” can be used by passing a ConnectionSettings instance to it:

let client = kmip_protocol::client::tls::<MODULE>::connect(&settings)?;

This will cause a TCP+TLS connection to be established with the server defined by the settings, if possible.

For more control you can supply your own TcpStream factory. For example you can use this to create a socket using the socket2 crate which allows you to tune the behaviour of the operating system networking stack specific to your use case. To supply a TcpStream factory use this function instead:

let client = kmip_protocol::client::tls::<MODULE>::connect_with_tcpstream_factory(&settings, factory_func)?;

The factory function must conform to this signature:

Fn(&SocketAddr, &ConnectionSettings) -> Result<TcpStream>

Note: You do not need to use a factory function to set timeouts as these are already set by the connect functions. For async connections the initial connection timeout is implemented as an async timeout around the connection attempt. For sync cn

§Enabling a plugin

To use the plugin it must also be enabled using the correct set of Cargo feature flags. The table below shows the settings required to use each “plugin”.

<MODULE>Cargo.toml kmip_protocol Dependency SettingsAsync RuntimeCrates.ioNotes
opensslfeatures = ["tls-with-openssl"]NoneviewSynchronous, uses host O/S OpenSSL
opensslfeatures = ["tls-with-openssl-vendored"]NoneviewSynchronous, uses compiled in OpenSSL
rustlsfeatures = ["tls-with-rustls"]NoneviewPure Rust, strict
tokio_native_tlsdefault-features = false, features = ["tls-with-tokio-native-tls"]TokioviewUses host O/S specific native TLS
tokio_rustlsdefault-features = false, features = ["tls-with-tokio-rustls"]TokioviewPowered by Rustls
async_tlsdefault-features = false, features = ["tls-with-async-tls"]Async StdviewPowered by Rustls

§Disabling default features

To use a plugin that require an async runtime you must disable the default-features. This is because this crate uses the Maybe-Async procedural macro to support both sync and async implementations with minimal code duplication, but that also means that sync and async implementations cannot both be compiled at the same time.

§Using the async API

The async API is identical to that of the sync API, you just have to call it from within a sync function and remember to call .await when invoking the API.

Sync plugin usage:

fn some_function() -> ... {
     let client = kmip_protocol::client::tls::rustls::connect(settings)?;
     client.create_rsa_key_pair(2048, "pubkey".into(), "privkey".into())?;
}

Compare that with async plugin usage:

async fn some_function() -> ... {
     let client = kmip_protocol::client::tls::tokio_rustls::connect(settings)?;
     client.create_rsa_key_pair(2048, "pubkey".into(), "privkey".into()).await?;
}