从任何语言中使用 Rustls
This crate contains FFI bindings for the rustls TLS library, so you can use the library in any language that supports FFI (C, C++, Python, etc). It also contains demo C programs that use those bindings to run an HTTPS server, and to make an HTTPS request.
Rustls is a modern TLS library written in Rust, meaning it is less likely to have memory safety vulnerabilities than equivalent TLS libraries written in memory unsafe languages.
If you are using rustls-ffi to replace OpenSSL, note that OpenSSL provides cryptographic primitives in addition to a TLS library. Rustls-ffi only provides the TLS library. If you use the cryptographic primitives from OpenSSL you may need to find another library to provide the cryptographic primitives.
To build rustls-ffi as a static or shared library you'll need to install the Rust toolchain (version 1.73 or above) as well as cargo-c.
The cargo-c tool can be installed from
your package manager
or using Cargo with cargo install cargo-c.
If you plan to build the client and server C examples
you will also need cmake and a C compiler (gcc and clang should both work,
as well as MSVC on Windows).
git clone https://github.com/rustls/rustls-ffi
cd rustls-ffi
sudo cargo capi install --releaseIf you receive a message like "error: no such command capi" you need to install
cargo-c from your package manager, or using cargo install cargo-c.
To change where the library is installed, use --prefix like:
cargo capi install --release --prefix=/tmp/rustls-ffiRunning capi install takes care of:
.a, .so, .dylib, .lib or .dll library files for rustls-ffi
(depending on the OS)..pc file if appropriate.Replace --release with --debug to generate a (slower) debug build. See
cargo capi install --help for more information
You can build rustls-ffi with different optional features, providing the
selection in your cargo capi commands.
Both rustls and rustls-ffi support choosing a cryptography provider for
implementing the cryptography required for TLS. By default, both will use
aws-lc-rs, but *ring* is available as an opt-in choice.
It is not presently supported to build with both cryptography providers activated.
Select the cryptography provider using --no-default-features and --features:
cargo capi install # aws-lc-rs default
cargo capi install --features=aws-lc-rs # aws-lc-rs explicit
cargo capi install --no-default-features --features=ring # ring
cargo capi install --no-default-features # no built-in providerFor more information on cryptography provider build requirements and supported platforms see the upstream documentation:
Post-quantum-secure key exchange using X25519MLKEM768 is supported when using the aws-lc-rs
cryptography provider and offered by default at the highest priority.
By disabling the prefer-post-quantum feature flag the X25519MLKEM768 key exchange will be
offered at a lower negotiation priority.
You can optionally enable RFC 8879
certificate compression support with --features=cert_compression.
This is disabled by default. Enabling this feature will bring in additional
dependencies (e.g. zlib-rs, brotli) and requires a minimum Rust version of
1.75+. Once enabled support is handled transparently and no code changes are
required in your application.
cargo capi install # Without cert compression.
cargo capi install --features=cert_compression # With cert compression.You can optionally enable FIPS support via --features=fips. This implicitly
enables the aws-lc-rs cryptography provider since ring does not have FIPS
140-3 support at this time.
Enabling FIPS mode adds several new build requirements depending on your
platform. For example, all platforms will require cmake and go. Windows will
require ninja.
Note that this is an experimental feature and on MacOS and Windows using this
feature requires you to dynamically link rustls-ffi - static linking is not
supported. You must additionally provide and link your own copies of the
required aws-lc-fips FIPS module .DLL or .a libraries on MacOS and
Windows when building your application.
See the upstream Windows and Apple documentation for more information.
To use rustls-ffi in your application you will need to link against the installed library. The details of this will vary based on your OS and intended use-case. The following assumes using rustls-ffi on Linux/MacOS with a traditional C application:
We recommend linking rustls-ffi as a static library as we make no guarantees of ABI stability across versions at this time, and dynamic library support is considered experimental.
After running cargo capi install you can link against the resulting static
library on Linux using these compiler arguments:
-lrustls -lgcc_s -lutil -lrt -lpthread -lm -ldl -lcTo link against the resulting static library, on macOS, use these compiler arguments:
-lrustls -liconv -lSystem -lc -lIf the linking instructions above go out of date, you can get an up-to-date list via:
RUSTFLAGS="--print native-static-libs" cargo buildIt may also be helpful to consult the example C applications.
Using rustls-ffi as a static library has some downsides. Notably each application that links the static library will need to be rebuilt for each update to rustls-ffi, and duplicated copies of rustls-ffi will be included in each application.
Building rustls-ffi as a dynamic library can resolve these issues, however this approach comes with its own trade-offs. We currently consider this option experimental.
To link against the resulting dynamic library on MacOS or Linux, use
pkg-config to populate your LDLIBS and CFLAGS as appropriate:
LDLIBS="$(pkg-config --libs rustls)"
CFLAGS="$(pkg-config --cflags rustls)"At this time rustls-ffi makes no guarantees about ABI stability. Each release of rustls-ffi may introduce breaking changes to the ABI and so the built library should use the exact rustls-ffi version as the dynamic library SONAME.
The rustls-ffi repo includes two example C programs for Linux, MacOS and Windows:
For a more realistic example of integrating rustls-ffi into a pre-existing
C project see the rustls-ffi vtls
backend in curl.
To build the client and server C examples you will also need cmake (3.15+)
and a C compiler (gcc and clang should both work, as well as MSVC on
Windows).
# Configure your build directory
cmake -S librustls -B build -DCMAKE_BUILD_TYPE=release
# Build the client/server examples
cmake --build buildUse -DCRYPTO_PROVIDER=ring to select the cryptography provider for the
examples explicitly.
Use -DCERT_COMPRESSION=on to enable certificate compression.
Use -DFIPS=on to enable FIPS mode.
Use -DDYN_LINK=on to dynamically link Rustls to the test programs instead of
statically linking (the default).
Use cmake --build build --target integration-test to build and run the
client/server integration tests.
Use cmake -LH build to see all options/help. Use cmake --build build --target help to view all targets.
Note that Windows uses the traditional CMake "multi config" arrangement, using --config instead
of -DCMAKE_BUILD_TYPE:
# Configure your build directory
cmake -S librustls -B build
# Build the client/server examples
cmake --build build --config ReleaseRunning the client/server examples in a debug build (ASAN enabled), with cert
compression, the ring cryptography provider, and clang as the compiler on
a Linux system in a my-clang-build build directory:
CC=clang CXX=clang cmake -S librustls -B my-clang-build -DCMAKE_BUILD_TYPE=Debug -DCERT_COMPRESSION=on
-DCRYPTO_PROVIDER=ring
cmake --build my-clang-build --target integration-testRustls doesn't do any I/O on its own. It provides the protocol handling, and leaves it up to the user to send and receive bytes on the network. Because of that it can be used equally well in a blocking or non-blocking I/O context. See the rustls documentation for a diagram of its input and output methods, along with a description of the TLS features it supports.
This library defines an enum, rustls_result, to indicate success or failure of
a function call. All fallible functions return a rustls_result. If a function
has other outputs, it provides them using output parameters (pointers to
caller-provided objects). For instance:
rustls_result rustls_connection_read(const rustls_connection *conn,
uint8_t *buf,
size_t count,
size_t *out_n);In this example, buf and out_n are output parameters.
For a given struct, all functions that start with the name of that struct are
either associated functions or methods of that struct. For instance,
rustls_connection_read is a method of rustls_connection. A function
that takes a pointer to a struct as the first parameter is considered a method
on that struct. Structs in this library are always created and destroyed by
library code, so the header file only gives a declaration of the structs, not
a definition.
As a result, structs are always handled using pointers. For each struct, there
is generally a function ending in _new() to create that struct. Once you've
got a pointer to a struct, it's your responsibility to (a) ensure no two
threads are concurrently mutating that struct, and (b) free that struct's
memory exactly once. Freeing a struct's memory will usually be accomplished
with a function starting with the struct's name and ending in _free().
You can tell if a method will mutate a struct by looking at the fi
暂无开放 Issues,或尚未同步最近议题。