rust-mbedtls

Warning

The state of this repository is changed to maintenance. We will not accept any more feature requests. Please check Change of status for details

mbedtls

Build Status

This is an idiomatic Rust wrapper for MbedTLS, allowing you to use MbedTLS with only safe code while being able to use such great Rust features like error handling and closures.

Additionally, building on MbedTLS's focus on embedded use, this crate can be used in a no_std environment.

Change of status

We discovered that mbedtls 3.4.X is not thread safe and will not work properly with multiple threads. This problem will not be fixed in short time, and the new PSA approach does not match our needs.

Therefore, we now decided to stay on mbedtls 2.28.X, and the state of this repository is changed to maintenance. We will not accept any more feature requests.

Old code of mbedtls 3.X on master branch has been moved to branch mbedtls-3.

Related issue: #320

Reference links:

Building

This crate depends on the mbedtls-sys-auto crate, see below for build details.

mbedtls crate features

This is a list of the Cargo features available for mbedtls. Features in bold are enabled by default.

Note: The above features were introduced so that this crate could be used as a crypto (or PKI) only library.

mbedtls-sys-auto

Unfortunately, the mbedtls-sys crate on crates.io is claimed by another, apparently inactive, project.

To use this crate, you will need to have clang and cmake installed, see below for details.

Configuring and linking MbedTLS

MbedTLS has a plethora of compile-time configuration options that significantly impact what functionality is available. To make sure Rust's view of MbedTLS matches up with what's built in C, MbedTLS must be configured using Cargo features (see next section) and built using mbedtls-sys's build script.

The mbedtls-sys crate includes the MbedTLS source code, the MbedTLS version will have the same major.minor version as the crate. Instead of using the provided source, you can specify the path to your own source tree using the RUST_MBEDTLS_SYS_SOURCE environment variable. It is not recommended to use a custom source that is based on a different version of MbedTLS than the one provided in the crate.

The build script will perform the following steps:

  1. generate an appropriate config.h (any existing config.h is ignored),
  2. compile a statically-linked MbedTLS, this requires cmake to be installed,
  3. generate Rust bindings based on the configuration, this requires clang to be installed.

mbedtls-sys-auto crate features

This is a list of the Cargo features available for mbedtls-sys. Features in bold are enabled by default.

For the complete mapping of features to config.h defines, see [mbedtls-sys/build/config.rs]. PRs adding new features are encouraged.

MbedTLS version updates

The vendored MbedTLS source lives in mbedtls-sys/vendor. Local changes that must survive vendor refreshes are stored as git am patches in mbedtls-sys/vendor-patches.

To update the vendored source to a new upstream tag or branch, run the helper script from a non-master branch:

./upgrade_vendor.sh <tag-name-or-branch-name>

The script recreates mbedtls-sys/vendor from the upstream MbedTLS repository, commits the vendor update, then applies every patch in mbedtls-sys/vendor-patches with git am --3way. If a patch no longer applies, resolve the conflict and continue the git am flow.

When adding or refreshing a local vendor change, first commit the desired changes under mbedtls-sys/vendor on your feature branch. Then generate a new patch file from the aggregate diff between master and HEAD, limited to that vendor directory:

./create_vendor_patch.sh "Short patch title"

The script writes the next numbered patch file under mbedtls-sys/vendor-patches, for example mbedtls-sys/vendor-patches/0005-Short-patch-title.patch. Uncommitted changes are not included.

After updating vendor code or patches:

  1. Use the command in mbedtls-sys/build/headers.rs to generate the list of headers, and update that file as appropriate.
  2. Check mbedtls-sys/build/config.rs against mbedtls-sys/vendor/include/mbedtls/config.h.
  3. Update the mbedtls-sys/Cargo.toml version and the [package.metadata.mbedtls] upstream version and commit hash.

Contributing

We gratefully accept bug reports and contributions from the community. By participating in this community, you agree to abide by Code of Conduct. All contributions are covered under the Developer's Certificate of Origin (DCO).

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

(d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

License

This project is primarily distributed under the terms of the Apache License version 2.0 and the GNU General Public License version 2, see LICENSE-APACHE and LICENSE-GPL for details.

A note about licensing

MbedTLS is dual-licensed Apache-2.0 / GPL-2.0+, and so are the mbedtls and mbedtls-sys-auto crates. However, the sources are distributed in two different single-licensed tarballs. The authors of the mbedtls and mbedtls-sys-auto crates do not warrant that the two versions of the MbedTLS code are exactly the same. This repository includes the Apache-2.0 version. Since Apache-2.0 is compatible with GPL-3.0+ this is probably not an issue for people whishing to use mbedtls-sys in a GPL-3.0+-licensed project, but if you want to use it in a GPL-2.0-licensed project, you should probably manually specify the GPL-2.0 source when building.