#[non_exhaustive]pub enum JsonWebKey<'a> {
Rsa {
modulus: Cow<'a, [u8]>,
public_exponent: Cow<'a, [u8]>,
private_parts: Option<RsaPrivateParts<'a>>,
},
EllipticCurve {
curve: Cow<'a, str>,
x: Cow<'a, [u8]>,
y: Cow<'a, [u8]>,
secret: Option<SecretBytes<'a>>,
},
Symmetric {
secret: SecretBytes<'a>,
},
KeyPair {
curve: Cow<'a, str>,
x: Cow<'a, [u8]>,
secret: Option<SecretBytes<'a>>,
},
}Expand description
Basic JWK functionality: (de)serialization and creating thumbprints.
See RFC 7518 for the details about the fields for various key types.
Self::thumbprint() and the Display implementation
allow to get the overall presentation of the key. The latter returns JSON serialization
of the key with fields ordered alphabetically. That is, this output for verifying keys
can be used to compute key thumbprints.
§Serialization
For human-readable formats (e.g., JSON, TOML, YAML), byte fields in JsonWebKey
and embedded types (SecretBytes, RsaPrivateParts, RsaPrimeFactor) will be
serialized in base64-url encoding with no padding, as per the JWK spec.
For other formats (e.g., CBOR), byte fields will be serialized as byte sequences.
Because of the limitations
of the CBOR support in serde, a JsonWebKey serialized in CBOR is not compliant
with the CBOR Object Signing and Encryption spec (COSE). It can still be a good
way to decrease the serialized key size.
§Conversions
A JWK can be obtained from signing and verifying keys defined in the alg
module via From / Into traits. Conversion from a JWK to a specific key is fallible
and can be performed via TryFrom with JwkError as an error
type.
As a part of conversion for asymmetric signing keys, it is checked whether
the signing and verifying parts of the JWK match; JwkError::MismatchedKeys is returned
otherwise. This check is not performed for verifying keys even if the necessary data
is present in the provided JWK.
⚠ Warning. Conversions for private RSA keys are not fully compliant with RFC 7518.
See the docs for the relevant impls for more details.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Rsa
Public or private RSA key. Has kty field set to RSA.
Fields
private_parts: Option<RsaPrivateParts<'a>>Private RSA parameters. Only present for private keys.
EllipticCurve
Public or private key in an ECDSA crypto system. Has kty field set to EC.
Fields
secret: Option<SecretBytes<'a>>Secret scalar (d); not present for public keys.
Symmetric
Generic symmetric key, e.g. for HS256 algorithm. Has kty field set to oct.
Fields
secret: SecretBytes<'a>Bytes representing this key.
KeyPair
Generic asymmetric keypair. This key type is used e.g. for Ed25519 keys.
Implementations§
Source§impl JsonWebKey<'_>
impl JsonWebKey<'_>
Sourcepub const fn is_signing_key(&self) -> bool
pub const fn is_signing_key(&self) -> bool
Returns true if this key can be used for signing (has SecretBytes fields).
Sourcepub fn to_verifying_key(&self) -> JsonWebKey<'_>
pub fn to_verifying_key(&self) -> JsonWebKey<'_>
Returns a copy of this key with parts not necessary for signature verification removed.
Sourcepub fn thumbprint<D>(
&self,
) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>where
D: Digest,
pub fn thumbprint<D>(
&self,
) -> GenericArray<u8, <D as OutputSizeUser>::OutputSize>where
D: Digest,
Computes a thumbprint of this JWK. The result complies with the key thumbprint defined in RFC 7638.
Trait Implementations§
Source§impl<'a> Clone for JsonWebKey<'a>
impl<'a> Clone for JsonWebKey<'a>
Source§fn clone(&self) -> JsonWebKey<'a>
fn clone(&self) -> JsonWebKey<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a> Debug for JsonWebKey<'a>
impl<'a> Debug for JsonWebKey<'a>
Source§impl<'de, 'a> Deserialize<'de> for JsonWebKey<'a>
impl<'de, 'a> Deserialize<'de> for JsonWebKey<'a>
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<JsonWebKey<'a>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<JsonWebKey<'a>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for JsonWebKey<'_>
impl Display for JsonWebKey<'_>
Source§impl<'a> From<&'a Hs256Key> for JsonWebKey<'a>
impl<'a> From<&'a Hs256Key> for JsonWebKey<'a>
Source§fn from(key: &'a Hs256Key) -> JsonWebKey<'a>
fn from(key: &'a Hs256Key) -> JsonWebKey<'a>
Source§impl<'a> From<&'a Hs384Key> for JsonWebKey<'a>
impl<'a> From<&'a Hs384Key> for JsonWebKey<'a>
Source§fn from(key: &'a Hs384Key) -> JsonWebKey<'a>
fn from(key: &'a Hs384Key) -> JsonWebKey<'a>
Source§impl<'a> From<&'a Hs512Key> for JsonWebKey<'a>
impl<'a> From<&'a Hs512Key> for JsonWebKey<'a>
Source§fn from(key: &'a Hs512Key) -> JsonWebKey<'a>
fn from(key: &'a Hs512Key) -> JsonWebKey<'a>
Source§impl<'a> From<&'a SigningKey> for JsonWebKey<'a>
impl<'a> From<&'a SigningKey> for JsonWebKey<'a>
Source§fn from(signing_key: &'a SigningKey) -> JsonWebKey<'a>
fn from(signing_key: &'a SigningKey) -> JsonWebKey<'a>
Source§impl<'a> From<&'a VerifyingKey> for JsonWebKey<'a>
impl<'a> From<&'a VerifyingKey> for JsonWebKey<'a>
Source§fn from(key: &'a VerifyingKey) -> JsonWebKey<'a>
fn from(key: &'a VerifyingKey) -> JsonWebKey<'a>
Source§impl<'a> PartialEq for JsonWebKey<'a>
impl<'a> PartialEq for JsonWebKey<'a>
Source§impl<'a> Serialize for JsonWebKey<'a>
impl<'a> Serialize for JsonWebKey<'a>
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl TryFrom<&JsonWebKey<'_>> for Hs256Key
impl TryFrom<&JsonWebKey<'_>> for Hs256Key
Source§impl TryFrom<&JsonWebKey<'_>> for Hs384Key
impl TryFrom<&JsonWebKey<'_>> for Hs384Key
Source§impl TryFrom<&JsonWebKey<'_>> for Hs512Key
impl TryFrom<&JsonWebKey<'_>> for Hs512Key
impl<'a> StructuralPartialEq for JsonWebKey<'a>
Auto Trait Implementations§
impl<'a> Freeze for JsonWebKey<'a>
impl<'a> RefUnwindSafe for JsonWebKey<'a>
impl<'a> Send for JsonWebKey<'a>
impl<'a> Sync for JsonWebKey<'a>
impl<'a> Unpin for JsonWebKey<'a>
impl<'a> UnwindSafe for JsonWebKey<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
Source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
Source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
Source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T. Read moreSource§impl<T, U> TryIntoKey<U> for Twhere
U: TryFromKey<T>,
impl<T, U> TryIntoKey<U> for Twhere
U: TryFromKey<T>,
type Error = <U as TryFromKey<T>>::Error
fn try_into_key(self) -> Result<U, <U as TryFromKey<T>>::Error>
Source§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
Source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from.Source§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
Source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T.