pub struct KeyPair { /* private fields */ }展开描述
用于对证书和 CSR 进行签名的密钥对
实现§
Source§impl KeyPair
impl KeyPair
Sourcepub fn generate() -> Result<Self, Error>
pub fn generate() -> Result<Self, Error>
生成一个新的随机 PKCS_ECDSA_P256_SHA256 密钥对
Sourcepub fn generate_for(alg: &'static SignatureAlgorithm) -> Result<Self, Error>
pub fn generate_for(alg: &'static SignatureAlgorithm) -> Result<Self, Error>
为指定的签名算法生成一个新的随机密钥对
若你不确定该使用哪种算法,PKCS_ECDSA_P256_SHA256 是一个不错的选择。
若传入的是一种 RSA 签名算法,则取决于后端实现:可能返回生成的密钥,
也可能因不支持密钥生成而返回错误。目前只有 aws-lc-rs 支持 RSA 密钥生成。
Sourcepub fn algorithm(&self) -> &'static SignatureAlgorithm
pub fn algorithm(&self) -> &'static SignatureAlgorithm
返回该密钥对的签名算法
Sourcepub fn from_pem(pem_str: &str) -> Result<Self, Error>
pub fn from_pem(pem_str: &str) -> Result<Self, Error>
从 ASCII PEM 格式解析密钥对
若使用了 aws_lc_rs feature,则该密钥必须是 DER 编码的明文私钥;
规定见 PKCS #8 / RFC 5958、SEC1 / RFC 5915 或 PKCS#1 / RFC 3447;
在 PEM 文件中以 “PRIVATE KEY”、“RSA PRIVATE KEY” 或 “EC PRIVATE KEY” 形式出现。
若使用的是 ring feature,则该密钥必须是 DER 编码的明文私钥;
规定见 PKCS #8 / RFC 5958;在 PEM 文件中以 “PRIVATE KEY” 形式出现。
Sourcepub fn from_pkcs8_pem_and_sign_algo(
pem_str: &str,
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error>
pub fn from_pkcs8_pem_and_sign_algo( pem_str: &str, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>
从 DER 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm
密钥必须是 DER 编码的明文私钥;规定见 PKCS #8 / RFC 5958;
在 PEM 文件中以 “PRIVATE KEY” 形式出现。 同 from_pkcs8_pem_and_sign_algo。
Sourcepub fn from_pkcs8_der_and_sign_algo(
pkcs8: &PrivatePkcs8KeyDer<'_>,
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error>
pub fn from_pkcs8_der_and_sign_algo( pkcs8: &PrivatePkcs8KeyDer<'_>, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>
从 DER 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm
若你拥有一个 PrivatePkcs8KeyDer,通常可以依赖 TryFrom 实现来得到一个 KeyPair——
它会为你确定正确的 SignatureAlgorithm。
不过,有时同一段 DER 密钥可能匹配多种签名算法。这种情况下,你可以使用本函数精确地指定 SignatureAlgorithm。
rustls_pemfile::private_key() 常用于从 PEM 输入得到 PrivateKeyDer。
若得到的 PrivateKeyDer 是 Pkcs8 变体,你可以将它的内容作为本函数的输入。
另外,若你已经有一段包含 DER 数据的字节切片,使用 Into trait 便可轻松地将其转换为 PrivatePkcs8KeyDer。
Sourcepub fn from_pem_and_sign_algo(
pem_str: &str,
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error>
pub fn from_pem_and_sign_algo( pem_str: &str, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>
从 PEM 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm
若使用了 aws_lc_rs feature,则该密钥必须是 DER 编码的明文私钥;
规定见 PKCS #8 / RFC 5958、SEC1 / RFC 5915 或 PKCS#1 / RFC 3447;
在 PEM 文件中以 “PRIVATE KEY”、“RSA PRIVATE KEY” 或 “EC PRIVATE KEY” 形式出现。
若使用的是 ring feature,则该密钥必须是 DER 编码的明文私钥;
规定见 PKCS #8 / RFC 5958;在 PEM 文件中以 “PRIVATE KEY” 形式出现。
Sourcepub fn from_der_and_sign_algo(
key: &PrivateKeyDer<'_>,
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error>
pub fn from_der_and_sign_algo( key: &PrivateKeyDer<'_>, alg: &'static SignatureAlgorithm, ) -> Result<Self, Error>
从 DER 格式的密钥解析出密钥对,并使用指定的 SignatureAlgorithm
请注意,若使用 ring feature,本函数仅支持 PrivateKeyDer::Pkcs8 变体。
若需要完整支持 PrivateKeyDer,请考虑启用 aws_lc_rs feature。
如果你有 PrivateKeyDer,通常可依赖 TryFrom 实现来获取 KeyPair —— 它会为你确定正确的 SignatureAlgorithm。但有时同一 DER 密钥可能对应多个签名算法,此时你可以使用此函数精确指定 SignatureAlgorithm。
你可以使用 rustls_pemfile::private_key 来获取密钥输入。
若你已有字节切片,只需调用 try_into() 即可将其转换为 PrivateKeyDer。
Sourcepub fn public_key_raw(&self) -> &[u8] ⓘ
pub fn public_key_raw(&self) -> &[u8] ⓘ
获取此密钥对的原始公钥
密钥为原始格式,与 KeyPair::public_key() 的输出以及
UnparsedPublicKey::verify() 所接受的格式一致。
Sourcepub fn is_compatible(&self, signature_algorithm: &SignatureAlgorithm) -> bool
pub fn is_compatible(&self, signature_algorithm: &SignatureAlgorithm) -> bool
检查此密钥对是否可用于给定的签名算法
Sourcepub fn compatible_algs(
&self,
) -> impl Iterator<Item = &'static SignatureAlgorithm>
pub fn compatible_algs( &self, ) -> impl Iterator<Item = &'static SignatureAlgorithm>
返回(可能多个)此密钥可兼容使用的 SignatureAlgorithm
Sourcepub fn public_key_pem(&self) -> String
pub fn public_key_pem(&self) -> String
以 PEM 格式返回该密钥对的公钥
返回的字符串可通过 openssl pkey --inform PEM -pubout -pubin -text 进行解析
Sourcepub fn serialize_der(&self) -> Vec<u8> ⓘ
pub fn serialize_der(&self) -> Vec<u8> ⓘ
将该密钥对(包括私钥)以 PKCS#8 DER 格式序列化
Sourcepub fn serialized_der(&self) -> &[u8] ⓘ
pub fn serialized_der(&self) -> &[u8] ⓘ
返回序列化的密钥对(含私钥)的引用,格式为 PKCS#8 的 DER 编码
Sourcepub fn serialize_pem(&self) -> String
pub fn serialize_pem(&self) -> String
将该密钥对(包括私钥)以 PKCS#8 PEM 格式序列化
Trait 实现§
Source§impl From<KeyPair> for PrivateKeyDer<'static>
Available on crate feature crypto only.
impl From<KeyPair> for PrivateKeyDer<'static>
crypto only.Source§impl From<KeyPair> for PrivatePkcs8KeyDer<'static>
Available on crate feature crypto only.
impl From<KeyPair> for PrivatePkcs8KeyDer<'static>
crypto only.Source§impl PublicKeyData for KeyPair
Available on crate feature crypto only.
impl PublicKeyData for KeyPair
crypto only.Source§impl SigningKey for KeyPair
Available on crate feature crypto only.
impl SigningKey for KeyPair
crypto only.Source§impl TryFrom<&PrivateKeyDer<'_>> for KeyPair
Available on crate feature crypto only.
impl TryFrom<&PrivateKeyDer<'_>> for KeyPair
crypto only.Source§impl TryFrom<&PrivatePkcs8KeyDer<'_>> for KeyPair
Available on crate feature crypto only.
impl TryFrom<&PrivatePkcs8KeyDer<'_>> for KeyPair
crypto only.