Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set alg and kid on expansion #502

Closed
wants to merge 18 commits into from
Closed

Conversation

nitro-neal
Copy link
Contributor

@nitro-neal nitro-neal commented Apr 29, 2024

This pr resolves #3 from this issue:
#497

Always set alg and kid on expansion (to the values in the registry), support overriding of alg values

toDnsPacket:

  • check if the alg on the JWK == the alg in the registry for the given key type

fromDnsPacket:

  • KID is always either 0 for the identity key or the JWK thumbprint for all other keys (no overriding it)
  • the alg is always set to the default from the registry (https://did-dht.com/registry/#key-type-index) but can be overriden if you specify the alg property
  • alg can be omitted and if it is you assume it’s the default

Copy link

changeset-bot bot commented Apr 29, 2024

⚠️ No Changeset found

Latest commit: 143b293

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

github-actions bot commented Apr 29, 2024

TBDocs Report

✅ No errors or warnings

@web5/api

  • Project entry file: packages/api/src/index.ts

@web5/crypto

  • Project entry file: packages/crypto/src/index.ts

@web5/crypto-aws-kms

  • Project entry file: packages/crypto-aws-kms/src/index.ts

@web5/dids

  • Project entry file: packages/dids/src/index.ts

@web5/credentials

  • Project entry file: packages/credentials/src/index.ts

TBDocs Report Updated at 2024-05-07T21:17:36Z 143b293

@codecov-commenter
Copy link

codecov-commenter commented Apr 29, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 90.81%. Comparing base (eabe5ca) to head (44b12b6).

❗ Current head 44b12b6 differs from pull request most recent head 143b293. Consider uploading reports for the commit 143b293 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #502      +/-   ##
==========================================
- Coverage   91.03%   90.81%   -0.23%     
==========================================
  Files         116      116              
  Lines       29562    29439     -123     
  Branches     2177     2161      -16     
==========================================
- Hits        26912    26734     -178     
- Misses       2615     2669      +54     
- Partials       35       36       +1     
Components Coverage Δ
agent 79.90% <ø> (-0.73%) ⬇️
api 97.91% <ø> (+0.13%) ⬆️
common 98.68% <ø> (ø)
credentials 95.25% <ø> (-0.01%) ⬇️
crypto 93.81% <ø> (ø)
dids 97.63% <94.11%> (-0.06%) ⬇️
identity-agent 96.70% <ø> (ø)
crypto-aws-kms 100.00% <ø> (ø)
proxy-agent 96.70% <ø> (ø)
user-agent 96.70% <ø> (ø)

nitro-neal and others added 2 commits May 1, 2024 11:05
Co-authored-by: Gabe <7622243+decentralgabe@users.noreply.github.com>
@nitro-neal nitro-neal marked this pull request as ready for review May 1, 2024 18:56
Copy link
Member

@thehenrytsai thehenrytsai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks legit but no need for tests? 😆

packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
@@ -1025,6 +1035,16 @@ export class DidDhtDocument {
// Convert the public key from a byte array to JWK format.
let publicKey = await DidDhtUtils.keyConverter(namedCurve).bytesToPublicKey({ publicKeyBytes });

// Always set the algorithm on did:dht expansion.
publicKey.alg = parsedAlg || getJoseSignatureAlgorithmFromPublicKey(publicKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity: getJoseSignatureAlgorithmFromPublicKey() does not seem to return Ed25519 that will be expected of vector 3.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I using this correctly?

export function getJoseSignatureAlgorithmFromPublicKey(publicKey: Jwk): string {
  const curveToJoseAlgorithm: Record<string, string> = {
    'Ed25519'   : 'EdDSA',
    'P-256'     : 'ES256',
    'P-384'     : 'ES384',
    'P-521'     : 'ES512',
    'secp256k1' : 'ES256K',
  };

  // If the key contains an `alg` property that matches a JOSE registered algorithm identifier,
  // return its value.
  if (publicKey.alg && Object.values(curveToJoseAlgorithm).includes(publicKey.alg)) {
    return publicKey.alg;
  }

  // If the key contains a `crv` property, return the corresponding algorithm.
  if (publicKey.crv && Object.keys(curveToJoseAlgorithm).includes(publicKey.crv)) {
    return curveToJoseAlgorithm[publicKey.crv];
  }

  throw new Error(
    `Unable to determine algorithm based on provided input: alg=${publicKey.alg}, crv=${publicKey.crv}. ` +
    `Supported 'alg' values: ${Object.values(curveToJoseAlgorithm).join(', ')}. ` +
    `Supported 'crv' values: ${Object.keys(curveToJoseAlgorithm).join(', ')}.`
  );
}

Copy link
Contributor Author

@nitro-neal nitro-neal May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the alg is always there for our jwks, but is that not necessarily the case for outside jwks inside the did dht??

if the alg is always there we can just do

if(parsedAlg) {
   publicKey.alg = parsedAlg
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get if(parsedAlg) { publicKey.alg = parsedAlg }, if parsedAlg is always defined, then there is nothing do right? since:

const { id, t, k, c, a: parsedAlg } = DidDhtUtils.parseTxtDataToObject(answer.data);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parsed alg will not always be defined, but publicKey.alg is always defined

publicKey.alg = parsedAlg || getJoseSignatureAlgorithmFromPublicKey(publicKey);

// Determine the Key ID (kid): '0' for the identity key or JWK thumbprint for others. Always set alg on expansion.
if (id !== '0' && publicKey.kid === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding: why would kid be defined when id is not 0? May have opportunity to simplify the condition here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplifying to:

          if (id === '0') {
            publicKey.kid = '0';
          } else if (publicKey.kid === undefined) {
            publicKey.kid = await computeJwkThumbprint({ jwk: publicKey });
          }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the publicKey.kid should be defined, but if it is not, we can recompute it deterministically with computeJwkThumbprint

packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
packages/dids/src/methods/did-dht.ts Outdated Show resolved Hide resolved
nitro-neal and others added 6 commits May 2, 2024 15:51
Co-authored-by: Henry Tsai <henrytsai@outlook.com>
Co-authored-by: Henry Tsai <henrytsai@outlook.com>
Co-authored-by: Henry Tsai <henrytsai@outlook.com>
Co-authored-by: Henry Tsai <henrytsai@outlook.com>
Co-authored-by: Henry Tsai <henrytsai@outlook.com>
decentralgabe
decentralgabe previously approved these changes May 2, 2024
Comment on lines 1042 to 1046
if (id === '0') {
publicKey.kid = '0';
} else if (publicKey.kid === undefined) {
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@decentralgabe just confirmed that id MUST be 0 for identity key, and undefined otherwise, (we should have tests if they don't exist to make sure these are true in implementation), so it seems to me the condition can simply be:

Suggested change
if (id === '0') {
publicKey.kid = '0';
} else if (publicKey.kid === undefined) {
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey });
}
if (id === undefined) {
publicKey.kid = await computeJwkThumbprint({ jwk: publicKey });
}

Happy to discuss if I am not making sense!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm going to update the spec language to always remove the id property
@thehenrytsai correctly pointed out we can discern the 0 key based on the did identifier
all other keys will use the thumbprint

@nitro-neal nitro-neal marked this pull request as draft May 7, 2024 21:14
@nitro-neal nitro-neal closed this May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subscribe api
4 participants