Skip to main content

KeyReference

Enum KeyReference 

Source
#[non_exhaustive]
pub enum KeyReference { DidUrl(DidUrl), Jwk(Box<Jwk>), X509Chain(Vec<Vec<u8>>), Jwks { issuer: Url, kid: String, }, Jku { url: Url, kid: Option<String>, }, }
Expand description

Where a token says the key that signed it is (ADR 0006).

A JWT names its key in one of several ways: a DID URL in kid, a #fragment in kid relative to a DID iss, a kid into the key set an HTTPS iss publishes, the key itself in a jwk header, a certificate chain in x5c, or a key set URL in jku. from_jws reads which from a JWS header; sd_jwt_vc builds one from a cnf claim. A KeyResolver turns it into a key.

Resolving a reference says nothing about trust: a key by value resolves to itself whoever made the token. Once the signature verifies, binds_to checks that the key belongs to the party the token claims.

§Examples

use identity_kit::jose::{CompactJws, JwtClaims};
use identity_kit::resolver::{KeyReference, KeyReferenceError};

// {"alg":"EdDSA","kid":"#key-1"}.{"iss":"did:example:123"}
let jws = CompactJws::decode(
    "eyJhbGciOiJFZERTQSIsImtpZCI6IiNrZXktMSJ9.eyJpc3MiOiJkaWQ6ZXhhbXBsZToxMjMifQ.c2lnbmF0dXJl",
)?;
let claims: JwtClaims = jws.claims()?;
let reference = KeyReference::from_jws(&jws, claims.iss.as_deref())?;
assert_eq!(reference.to_string(), "did:example:123#key-1");

// A relative `kid` needs a DID to resolve against.
assert!(matches!(
    KeyReference::from_jws(&jws, None),
    Err(KeyReferenceError::RelativeKidWithoutIssuer(_))
));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

DidUrl(DidUrl)

A verification method: a kid that is an absolute DID URL, or a #fragment joined with a DID iss.

§

Jwk(Box<Jwk>)

The public key itself, from a jwk header or cnf.jwk.

§

X509Chain(Vec<Vec<u8>>)

An x5c certificate chain, DER, leaf first. No resolver in the crate handles it yet.

§

Jwks

The key kid names in the key set of the HTTPS issuer issuer (SD-JWT VC §3.5).

Fields

§issuer: Url

The issuer, the iss of the token.

§kid: String

The kid of the key in the issuer’s key set.

§

Jku

The key set at url, from jku, and the kid that selects a key in it. No resolver in the crate handles it yet.

Fields

§url: Url

The key set URL.

§kid: Option<String>

The kid, if the token has one.

Implementations§

Source§

impl KeyReference

Source

pub fn from_jws( jws: &CompactJws, iss: Option<&str>, ) -> Result<Self, KeyReferenceError>

Where the key of jws is, given the iss of its claims (when iss is a string).

The header parameters are tried in this order, and the first that applies decides:

  1. A kid that parses as a DID URL is DidUrl. A path or DID parameters such as versionId are refused: they could select a document version that still lists a retired key (ADR 0004).
  2. A kid starting with # is joined with iss into a DidUrl when iss is a DID, and refused otherwise.
  3. Another kid, with an https iss, is Jwks.
  4. A jwk header is Jwk; a private key there is refused.
  5. An x5c header is X509Chain, each certificate base64-decoded but not parsed.
  6. A jku header is Jku, with the kid when there is one.

A kid that none of the first three cases takes is an error when no jwk, x5c or jku follows.

§Errors

A KeyReferenceError: Missing when the header names no key, otherwise the parameter that is wrong.

Source

pub fn binds_to(&self, party: Option<&str>, key: &Jwk) -> bool

Whether key, which this reference resolved to and which verified the signature, belongs to party, the issuer or holder the token claims:

ReferenceBinds when
DidUrlparty is the DID of the URL
Jwkparty is the did:jwk of key, or its RFC 7638 SHA-256 thumbprint, or absent (a SIOPv2 self-issued sub)
Jwksparty is the issuer URL (compared as parsed URLs)
X509Chain, Jkunever, until the crate resolves them

Only a key by value can bind to no party; a validator that requires an issuer or holder passes it.

§Examples
use identity_kit::did::DidUrl;
use identity_kit::jose::Jwk;
use identity_kit::resolver::KeyReference;

// RFC 8037 Appendix A.2.
let key: Jwk = serde_json::from_str(
    r#"{"kty":"OKP","crv":"Ed25519","x":"11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"}"#,
)?;
let by_value = KeyReference::Jwk(Box::new(key.clone()));
// RFC 8037 Appendix A.3.
assert!(by_value.binds_to(Some("kPrK_qmxVWaYVA9wwBF6Iuo3vVzz7TxHCTwXBygrS4k"), &key));
assert!(by_value.binds_to(None, &key));
assert!(!by_value.binds_to(Some("did:example:123"), &key));

let by_url = KeyReference::DidUrl(DidUrl::parse("did:example:123#key-1")?);
assert!(by_url.binds_to(Some("did:example:123"), &key));
assert!(!by_url.binds_to(Some("did:example:456"), &key));
assert!(!by_url.binds_to(None, &key));
Source§

impl KeyReference

Source

pub fn from_confirmation(cnf: &Confirmation) -> Result<Self, KeyReferenceError>

Available on crate feature sd-jwt only.

Where the holder’s key of a cnf claim (RFC 7800) is: jwk if present, else a kid that is an absolute DID URL, else jku with the kid.

RFC 7800 §3.4 leaves the meaning of a key id to the application, and the confirmation carries no DID or key set to resolve a #fragment or a bare id against (using sub would conflate the subject with the holder), so a cnf.kid must be an absolute DID URL without a path or DID parameters (ADR 0006).

§Examples
use identity_kit::resolver::{KeyReference, KeyReferenceError};
use identity_kit::sd_jwt_vc::Confirmation;

let cnf = Confirmation::kid("did:example:holder#key-1");
assert_eq!(KeyReference::from_confirmation(&cnf)?.to_string(), "did:example:holder#key-1");

let relative = Confirmation::kid("#key-1");
assert!(matches!(
    KeyReference::from_confirmation(&relative),
    Err(KeyReferenceError::InvalidKid { .. })
));
§Errors

KeyReferenceError::Missing for an empty cnf, KeyReferenceError::UnsupportedConfirmation for one that names its key only by another method (x5t#S256, jwe), and otherwise the member that is wrong.

Trait Implementations§

Source§

impl Clone for KeyReference

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyReference

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for KeyReference

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for KeyReference

Source§

impl PartialEq for KeyReference

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for KeyReference

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
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 from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

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

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more