Commit Graph

55 Commits

Author SHA1 Message Date
Joseph Birr-Pixton 363910b701 Extract all straight lookups of suites and kx groups 2024-02-20 10:35:13 +00:00
Joseph Birr-Pixton 651b5a4f14 Select key exchange group and cipher suite together
This is complex because the choice of usable cipher suites depends
on selected protocol version, and the set of mutually supported
key exchange groups.  Then, the usable set of key exchange groups
depends on the actually-selected cipher suite.
2024-02-19 19:36:13 +00:00
Joseph Birr-Pixton 96dc28de32 Add manual section for FIPS 2024-02-19 11:05:47 +00:00
Joseph Birr-Pixton 8e4afc6d14 Improve/extend docs of `default_fips_provider()` 2024-02-12 10:00:59 +00:00
Joseph Birr-Pixton 7415b5ff3d Change crate default provider to aws-lc-rs 2024-02-12 10:00:59 +00:00
Joseph Birr-Pixton e7a1b41852 Introduce concept of "process default" provider
One can be installed with `CryptoProvider::install_default`.
First call wins.

The current value can be retrieved with `CryptoProvider::get_default()`.

This can be set from the crate features, if and only if they are unambigious,
by installing the result of `CryptoProvider::from_crate_features()`.

Use this for `ClientConfig::builder` and `ServerConfig::builder` et al.
Naturally, `ClientConfig::builder_with_provider` and co. continue to exist.
2024-02-07 16:02:26 +00:00
Arash Sahebolamri c8c56a7aef Implement FFDHE support
+ Make server avoid cipher suites with kx without common kx groups with client
+ Handle FFDHE shared secret leading zeros correctly
2024-02-07 14:02:26 +00:00
Arash Sahebolamri 32f3d50a87 Refactor `SharedSecret` definition 2024-02-07 14:02:26 +00:00
Joseph Birr-Pixton 5cc71572e4 Default to `require_ems` in FIPS mode
Change default for `require_ems` based on `fips` crate feature,
generalising the existing tests for `require_ems` to verify this too.

Include `require_ems` in `fips()` determination.
2024-02-05 16:37:30 +00:00
Sean McGrail 6bd851e72e Add 'fips' Cargo feature
Add `rustls::crypto::default_fips_provider()` behind this
feature.
2024-02-02 16:57:39 +00:00
Joseph Birr-Pixton c83b4243b6 Expose FIPS "service indicator"
This means a `ClientConfig` and `ServerConfig` can be asked whether it
is in fips mode, and it answers by asking the same of all its
constituent cryptography.

Take new rustls-webpki and pki-types to ask the same of
`SignatureVerificationAlgorithm`.
2024-02-02 16:57:39 +00:00
Daniel McCarney fccff80241 crypto: KeyProvider pointer to customizing private key usage
The `KeyProvider` trait associted with the `CryptoProvider` struct is
specific to private key material that can be loaded from a DER
representation. For users that want to use private keys used through
a handle, or PKCS11 style interface an alternative integration approach
is needed.

This commit adds a doc string update to the `KeyProvider` to guide such
users to look at the Rustls manual's section on customizing private key
usage.
2024-01-05 14:22:13 +00:00
Jacob Hoffman-Andrews 6845c013cb doc: remove `crate::` prefix for links
Instead, use `#[cfg(doc)]` to conditionally import names that we want to
use in the docs. This provides a user-friendlier link name.
2023-12-04 17:58:51 +00:00
Daniel McCarney 822f86e822 docs: link to ActiveKeyExchange::complete from SharedSecret
Also drops "as a value".
2023-12-01 18:27:53 +00:00
Daniel McCarney e34d46d46d docs: link to SupportedKxGroup for ActiveKeyExchange doc 2023-12-01 18:27:53 +00:00
Jacob Hoffman-Andrews 58c2d26ed0 api: move CipherSuiteCommon into crypto
The top level of the crate is meant for "paved path" exports.

In 0.21.x, this type was in `cipher_suites`, along with a few other
types that got moved to specific crypto providers. Moving this to
`crypto` instead of re-exporting under its old name in `cipher_suites`
seems acceptable, because it will mainly be used in implementing crypto
providers. Also, its internals have changed significantly so there is
already churn for this type.
2023-11-30 17:44:07 +00:00
Daniel McCarney b92fd839e3 crypto: rework CryptoProvider as struct
This commit replaces the existing `CryptoProvider` trait with
a `CryptoProvider` struct. This has several advantages:

* it consolidates all of the cryptography related settings into one API
  surface, the `CryptoProvider` struct members. Previously the provider
  had methods to suggest default ciphersuites, key exchanges etc, but
  the builder API methods could override them in confusing ways.
* it allows removing the `WantsCipherSuites` and `WantsKxGroups` builder
  states - the "safe defaults" are automatically supplied by the choice
  of a crypto provider. Customization is achieved by overriding the
  provider's struct fields. Having fewer builder states makes the API
  easier to understand and document.
* it makes customization easier: the end user can rely on "struct update
  syntax"[0] to only specify fields values for the required
  customization, and defer the rest to an existing `CryptoProvider`.

Achieving this requires a couple of additional changes:

* The cipher suite and key exchange groups are now expressed as `Vec`
  elements. This avoids imposing a `&'static` lifetime that would
  preclude runtime customization (e.g. the tls*-mio examples that
  build the list of ciphersuites at runtime based on command line
  flags).
* As a result of the `Vec` members we can no longer offer the concrete
  `CryptoProvider`s as `static` members of their respective modules.
  Instead we add `pub fn default_provider() -> CryptoProvider` methods
  to the `ring` and `aws-lc-rs` module that construct the `CryptoProvider`
  with the safe defaults, ready for further customization.

[0]: https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
2023-11-30 15:53:39 +00:00
Daniel McCarney b4971785fd crypto: CryptoProvider is-a KeyProvider -> has-a
In preparation for moving to a struct based model where
a `CryptoProvider` has a `&'static dyn KeyProvider` field, this commit
splits the `KeyProvider` trait from the `CryptoProvider` trait. In its
place `CryptoProvider` gets a `key_provider(&self)` fn that acts as
a stand-in for what will be a field in the struct based approach.
2023-11-30 15:53:39 +00:00
Daniel McCarney 2b791938bb crypto: split out KeyProvider trait
We're working towards making `CryptoProvider` a struct holding distinct
elements to be used for cryptography. To support this the
`load_private_key` fn needs to be lifted to a new trait, `KeyProvider`.
We can hold a `&dyn KeyProvider` in the to-be-added struct to invoke
as required for `load_private_key`.

This commit adds the new trait, includes `KeyProvider` in the existing
`CryptoProvider` trait bounds, and updates the *ring*, aws-lc-rs, and
provider example crypto providers to implement `KeyProvider`.
2023-11-30 15:53:39 +00:00
Daniel McCarney 3b5cf17ade crypto: CryptoProvider is-a SecureRandom -> has-a
In preparation for moving to a struct based model where
a `CryptoProvider` has a `&'static dyn SecureRandom` field, this commit
splits the `SecureRandom` trait from the `CryptoProvider` trait. In its
place `CryptoProvider` gets a `secure_random(&self)` fn that acts as
a stand-in for what will be a field in the struct based approach.
2023-11-30 15:53:39 +00:00
Daniel McCarney 53ed597fa1 crypto: split out SecureRandom trait
We're working towards making `CryptoProvider` a struct holding distinct
elements to be used for cryptography. To support this the `fill_random`
fn needs to be lifted to a new trait, `SecureRandom`. We can hold
a `&dyn SecureRandom` in the to-be-added struct to invoke as required
for `fill_random`. Since the trait now provides additional context, the
fn is renamed from `fill_random` to `fill`.

This commit adds the new trait, includes `SecureRandom` in the existing
`CryptoProvider` trait bounds, and updates the *ring*, aws-lc-rs, and
provider example crypto providers to implement `SecureRandom`.
2023-11-30 15:53:39 +00:00
Daniel McCarney 44298191d7 lib: export webpki tls12/tls13 sig verify helpers under crypto
The `verify_tls12_signature` and `verify_tls13_signature` helpers from
the `webpki::verify` module can be useful when implementing a custom
client/server certificate verifier. This commit exports them under the
`crypto` mod alongside the `WebPkiSupportedAlgorithms` type they rely
on.
2023-11-30 15:12:14 +00:00
Jacob Hoffman-Andrews d931562cf3 api: move WebPkiSupportedAlgorithms to crypto
The top level of the crate is meant for "paved path" exports.

This newly exported type is used for cryptographic provider
customization, so it properly belongs in the `crypto` module.
2023-11-30 14:29:53 +00:00
Jan Rüth 0c03f660ac Issue-1632: Ensure SharedSecret::secret_bytes is publicly accessible
When implementing a `CryptoProvider` external to this crate, one needs to be able to access the underlying `secret_bytes` after a key exchange when performing the TLS 1.2 PRF.

This change ensures that the bytes can be safely accessed.
2023-11-27 10:52:35 +00:00
Jacob Hoffman-Andrews a72f6697e5 doc: CryptoProvider defaults can be overridden
It was surprising to me that builder_with_provider could set a
CryptoProvider, and then with_cipher_suites could choose implementations
from a different CryptoProvider. I've tried to document things to make
that a little less surprising.
2023-11-23 15:23:14 +00:00
Daniel McCarney e9316dfcd4 crypto: make signer mod pub(crate)
Historically the types that now live in `rustls::crypto::signer` were
present in `rustls::sign`. When the crypto provider work refactored them
into their new home, we also added a `lib.rs` re-export under
`rustls::sign`. This left two import paths for accessing the same types.

To avoid duplicated import paths without causing more downstream
churn from moving the types this commit makes the
`rustls::crypto::signer` module `pub(crate)`, leaving `rustls::sign` as
the sole way to access the contained types externally.
2023-11-23 08:28:49 +00:00
Joseph Birr-Pixton 7595236ddf Expand documentation for `CryptoProvider` and associated 2023-11-21 19:15:40 +00:00
Daniel McCarney 8173b77bb0 crypto: doc-hide the HPKE module
The bits and pieces we're landing for HPKE support aren't ready for
broad use yet. To avoid confusion before the 0.22 release this commit
adds a `#[doc(hidden)]` attribute to the `crypto/hpke.rs` mod.
2023-11-20 22:16:36 +00:00
Daniel McCarney b4f0bd96a2 crypto: add HPKE module and traits
This commit introduces a trait for a hybrid public key encryption (HPKE)
provider. HPKE is specified in RFC 9180[0], and is a pre-requisite for
implementing encrypted client hello (ECH).

Implementations of this trait can use the cryptographic provider of
their choice to provide HPKE using existing primitives from the crypto
provider.

We've tailored the HPKE trait in Rustls to just what is required for
ECH, e.g. it doesn't support modes other than the unauthenticated 'base'
mode, and it only offers the "single-shot" APIs.

[0]: https://www.rfc-editor.org/rfc/rfc9180
2023-11-16 19:32:49 +00:00
Joseph Birr-Pixton aaf21d1cdf Allow optional use of aws-lc-rs
Provide shims for limited number of places where ring 0.17 and
aws-lc-rs (ring 0.16-era) APIs have diverged.  This is a
short-term fix, as they are likely to diverge more over time.
Eventually we'll have to stop sharing the code like this.

For unit-like tests, export a `test_provider` alias that resolves
to a provider module, for use in these tests.

This resolves to:

- *ring* if cfg(feature = "ring"), else
- aws-lc-rs if cfg(feature = "aws_lc_rs"), else
- is absent
2023-11-09 16:18:11 +00:00
Joseph Birr-Pixton c6c792b616 Delegate choosing webpki algorithms to `CryptoProvider`
This drastically simplifies `provider-example`.  But the
primary goal is ensuring a client configured `with_provider(AWS_LC_RS)`
only uses algorithms from aws-lc-rs, irrespective of crate features.
2023-11-09 16:18:11 +00:00
Joseph Birr-Pixton 3897bceeca Delegate private key loading to `CryptoProvider` 2023-11-09 16:18:11 +00:00
Yuxiang Cao 3355e06f97 refactor: more general error in SupportedKxGroup
Use `Error` instead of `GetRandomFailed` in trait `SupportedKxGroup`,
so that underlying crypto provider could throw errors other than RNG
related errors.
2023-11-07 00:53:49 +00:00
Joseph Birr-Pixton 636b772c39 Put HKDF use behind trait
Have an impl of this for hmac::Hmac
2023-10-26 11:12:05 +00:00
Joseph Birr-Pixton 65ad987c26 Move tls12::prf to crypto::tls12 2023-10-26 11:12:05 +00:00
Joe Birr-Pixton 071c580d5c `SharedSecret`: zeroize on drop 2023-10-20 09:04:27 +00:00
Jorge Aparicio 5427a4d6e9 use `core::prelude` instead of `std::prelude` 2023-10-10 15:53:23 +00:00
Joseph Birr-Pixton 048ff10740 Use dynamic dispatch for `CryptoProvider`
Instead of the type `rustls::crypto:💍:Ring`, the value
`rustls::crypto:💍:RING` implements this, and is more
entertaining to write.

`ServerConfig::builder()` references this by default, and
is equivalent to `ServerConfig::builder_with_provider(crypto:💍:RING)`.
2023-09-19 11:09:38 +00:00
Joseph Birr-Pixton 0e1908890d Use dynamic dispatch for key exchanges
This turns `SupportedKxGroup` into a trait, which can tell you
which `NamedGroup` it is, and `start()` an `ActiveKeyExchange`.

An `ActiveKeyExchange` represents the need for the peer's public key
which can be passed to `ActiveKeyExchange::complete`.

Unfortunately we can't be generic at compile-time over the various uses
of the resulting shared secret, so define a further type
which encapsulates the resulting shared secret.

Predefined key exchange algorithms (eg `rustls::kx_group::X25519`)
are now `&'static dyn rustls::SupportedKxGroup`.

The remainder of this commit is noise as much code ceased needing
to be generic of CryptoProvider (for its `KeyExchange` associated type).
2023-09-19 11:09:38 +00:00
Joseph Birr-Pixton b145054882 Make *ring* an optional dependency
Using the crate without this feature means something external
needs to provide all the cryptography, and (eg) convenient integrated
key loading APIs disappear.
2023-09-13 15:32:29 +00:00
Joseph Birr-Pixton 8f2f34e913 Move crate::sign to crate::crypto::signer 2023-09-13 15:32:29 +00:00
Joseph Birr-Pixton b20c024109 Have CryptoProvider supply default cipher suites 2023-08-25 14:01:04 +00:00
Joseph Birr-Pixton 3ea338a805 Introduce provider for hashing 2023-08-25 14:01:04 +00:00
Joseph Birr-Pixton c994e8267d Add crypto::hmac interface and use it for TLS1.2 PRF 2023-08-15 13:11:12 +00:00
Joseph Birr-Pixton ea2e846e2a Move cipher.rs to new crypto module
The intention is to delineate interfaces to be implemented by
pluggable crypto providers.
2023-08-15 13:11:12 +00:00
Joseph Birr-Pixton 1b752ea221 Expose & simplify KeyExchangeAlgorithm
This is how a TLS1.2 ciphersuite specifies how to decode a {Client,Server}KeyExchange
message.

This previously had a bunch of unused values: make it non_exhaustive so we can
extend it in the future, but otherwise remove all the unused items that could
mislead people as to what is actually implemented.

This needs to be public so a `rustls::Tls12CipherSuite` can be constructed outside
the core crate.
2023-08-15 13:11:12 +00:00
Joseph Birr-Pixton 1fc30c7cbf Make it possible to implement fill_random outside crate 2023-08-15 13:11:12 +00:00
Jorge Aparicio 01a9c6cd7f directly use core:: & alloc:: API instead of std:: re-exports 2023-07-31 17:38:19 +00:00
Dirkjan Ochtman 304116b476 crypto: fix typo in docstring 2023-07-24 13:15:27 +00:00
Joseph Birr-Pixton 83be0aa348 Refactor crypto::KeyExchange to simplify
This replaces the one use of `start()` (in TLS1.2 server) with
`choose()`, and then calls the result `start()` which I think is
slightly clearer.
2023-07-18 17:47:43 +00:00