跳到主要内容

CryptoProvider

搜索

结构体 CryptoProvider 

Source
pub struct CryptoProvider {
    pub cipher_suites: Vec<SupportedCipherSuite>,
    pub kx_groups: Vec<&'static dyn SupportedKxGroup>,
    pub signature_verification_algorithms: WebPkiSupportedAlgorithms,
    pub secure_random: &'static dyn SecureRandom,
    pub key_provider: &'static dyn KeyProvider,
}
展开描述

Controls core cryptography 用于 rustls.

此 crate 提供两个内置选项,分别为: CryptoProvider structures:

  • [crypto::aws_lc_rs::default_provider]: (behind the aws_lc_rs crate feature, which is enabled by default). This provider uses the aws-lc-rs crate. The fips crate feature makes this option use FIPS140-3-approved cryptography.
  • crypto::ring::default_provider: (behind the ring crate feature, which is optional). This provider uses the ring crate.

此结构体提供默认值。其中的所有内容都可在 运行时根据需要替换字段值来覆盖。

§Using the per-process default CryptoProvider

There 是 concept of an implicit 默认提供者, configured at run-time once in 一个 given process.

It is 用 用于 functions like ClientConfig::builder()ServerConfig::builder()

此 intention is that an application can specify the CryptoProvider they wish 到 use once, 并 have that apply 到 the variety of places where their application does TLS (which may be wrapped inside other libraries)。 They should do this by calling CryptoProvider::install_default() early on.

为实现此目标:

§Using a specific CryptoProvider

Supply the provider when constructing your ClientConfigServerConfig:

When creating 并 configuring 一个 webpki-backed client 或 server certificate verifier, 一个 choice of provider 也 needed 到 start the configuration process:

If you install 一个 custom provider 并 want 到 avoid any accidental use of 一个 built-in provider, the feature custom-provider can be activated 到 ensure your custom provider is 用 everywhere 并 not 一个 built-in one. This will disable any implicit use of 一个 built-in provider.

§Making a custom CryptoProvider

Your goal , 到 populate an instance of this CryptoProvider struct.

§Which elements are required?

There is no requirement that the individual elements (SupportedCipherSuite, SupportedKxGroup, SigningKey, etc.) come 从 the same crate. It is allowed 并 expected that uninteresting elements would be delegated back 到 one of 默认 providers (statically) 或 一个 parent provider (dynamically)。

For example, if we want 到 make 一个 provider that just overrides key loading in the config builder API (with ConfigBuilder::with_single_cert, etc.), it might look like this:

use rustls::crypto::aws_lc_rs;

pub fn provider() -> rustls::crypto::CryptoProvider {
  rustls::crypto::CryptoProvider{
    key_provider: &HsmKeyLoader,
    ..aws_lc_rs::default_provider()
  }
}

#[derive(Debug)]
struct HsmKeyLoader;

impl rustls::crypto::KeyProvider for HsmKeyLoader {
    fn load_private_key(&self, key_der: pki_types::PrivateKeyDer<'static>) -> Result<Arc<dyn rustls::sign::SigningKey>, rustls::Error> {
         fictious_hsm_api::load_private_key(key_der)
    }
}

§References to the individual elements

此 elements are documented separately:

§Example code

See custom provider-example/ for a full client and server example that uses cryptography 从 the RustCrypto and dalek-cryptography projects.

$ cargo run --example client | head -3
Current ciphersuite: TLS13_CHACHA20_POLY1305_SHA256
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 19899

§FIPS-approved cryptography

fips crate feature enables use of the aws-lc-rs crate in FIPS mode.

You can verify the configuration at runtime by checking ServerConfig::fips()/ClientConfig::fips() return true

字段§

§cipher_suites: Vec<SupportedCipherSuite>

List of supported ciphersuites, in preference order – the first element 是 highest priority.

SupportedCipherSuite type carries both configuration 并 implementation.

A valid CryptoProvider must ensure that all cipher suites are accompanied by at least one matching key exchange group in CryptoProvider::kx_groups

§kx_groups: Vec<&'static dyn SupportedKxGroup>

List of supported key exchange groups, in preference order – the first element 是 highest priority.

此 first element in this list 是 default key share 算法, 并 in TLS 1.3 一个 key share 用于 it is sent in the client hello.

SupportedKxGroup type carries both configuration 并 implementation.

§signature_verification_algorithms: WebPkiSupportedAlgorithms

List of signature verification algorithms 用于 use with webpki.

These are 用 用于 both certificate chain verification 并 handshake signature verification.

This is called by ConfigBuilder::with_root_certificates(), server::WebPkiClientVerifier::builder_with_provider()client::WebPkiServerVerifier::builder_with_provider()

§secure_random: &'static dyn SecureRandom

加密安全的随机数源。

§key_provider: &'static dyn KeyProvider

Provider 用于 loading private SigningKeys 从 PrivateKeyDer

实现§

Source§

impl CryptoProvider

Source

pub fn install_default(self) -> Result<(), Arc<Self>>

Sets this CryptoProvider as 默认 用于 this process.

This can be called successfully at most once in any process execution.

Call this early in your process 到 configure which provider is 用 用于 the provider. 此 configuration should happen before any use of ClientConfig::builder()ServerConfig::builder()

Source

pub fn get_default() -> Option<&'static Arc<Self>>

Returns 默认 CryptoProvider 用于 this process.

This , None if no default has been set yet.

Source

pub fn fips(&self) -> bool

Returns true if this CryptoProvider is operating in FIPS mode.

This covers only the cryptographic parts of FIPS approval. There are also TLS protocol-level recommendations made by NIST. You should prefer 到 call ClientConfig::fips()ServerConfig::fips() which take these into account.

Trait 实现§

Source§

impl Clone for CryptoProvider

Source§

fn clone(&self) -> CryptoProvider

返回值的副本。 更多信息
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment 从 source. 更多信息
Source§

impl Debug for CryptoProvider

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

使用给定的格式化器格式化此值。 更多信息

自动 Trait 实现§

Blanket 实现§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. 更多信息
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows 从 an owned value. 更多信息
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows 从 an owned value. 更多信息
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment 从 selfdest. 更多信息
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

原样返回传入的参数。

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::从(self)

That is, this conversion is whatever the implementation of From<T> 用于 U 的实现方式。

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

获得所有权后的类型。
Source§

fn to_owned(&self) -> T

Creates owned data 从 borrowed data, usually by cloning. 更多信息
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data 到 replace owned data, usually by cloning. 更多信息
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

转换出错时返回的类型。
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

转换出错时返回的类型。
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。