Trending

World technology news

→
Finding the same public key in two key feeds with a JWK thumbprint
Software Engineering · 2015
Software Engineering2015

Finding the same public key in two key feeds with a JWK thumbprint

Try the RSA public-key thumbprint from the 2015 RFC 7638 on a current Node runtime. Distinguish a renamed key from a new key.

Archive retrospective · Topic year 2015

Developer's JournalPublished: Dec 5, 20266 min read
Reading progress0%
In this article
  1. Why two feeds can look different
  2. Selecting the RSA members
  3. Two comparisons on a local machine
  4. Using labels alongside the thumbprint
  5. Watching a planned key rotation
  6. Recording enough information to reproduce a comparison

Why two feeds can look different

Suppose a reporting service moves its key feed to a new environment. The old feed gives a public key the label reports-a. The new feed gives the same key the label reports-b and returns the object's fields in a different order. Hashing the documents as files would produce different values. Conversely, a publisher may reuse an old label for a different public key. RFC 7638, published in 2015, defines the JSON Web Key thumbprint for identifying the key material in a JWK. An inventory can compare the resulting thumbprints while retaining source labels for diagnosis. This exercise uses the value to identify public keys during planned maintenance. The relying application still performs its own signature verification and trust checks.

Selecting the RSA members

The thumbprint uses the required public members for the corresponding key type. The RSA example selects e, kty and n. The field names appear in lexicographic order and the serialized JSON object contains no additional whitespace. The code builds a small object in the required order, encodes the JSON in UTF-8, hashes it with SHA-256 and encodes the digest as base64url. The incoming kid and use fields are excluded. They still carry useful information of their own. A key identifier can help a consumer find an entry, while the use field identifies an intended use. The fields selected here are specific to RSA. An elliptic curve key carries different required members, including a curve identifier and public coordinates.

Application flowConceptual sketch · summary of the article's technical flow

Two comparisons on a local machine

The example creates two temporary RSA key pairs with a current Node runtime. It exports only the public parts as JWKs. One key appears in two objects with different labels and field order. The newer key appears under the first label. The result shows true for the matching public key and false when the underlying public key changes. The SHA-256 digest occupies forty-three characters in unpadded base64url. Each execution generates fresh keys, so the particular digest strings vary. The equality results and encoded length stay fixed. The example does not persist private keys or require a running identity provider. It exercises a format from 2015 using a contemporary cryptography library, without making claims about the compatibility of the Node versions available when the RFC was published.

import assert from 'node:assert/strict';
import { createHash, generateKeyPairSync } from 'node:crypto';

function rsaThumbprint(jwk) {
  assert.equal(jwk.kty, 'RSA');
  const canonical = JSON.stringify({ e: jwk.e, kty: jwk.kty, n: jwk.n });
  return createHash('sha256').update(canonical, 'utf8').digest('base64url');
}

const publicJwk = () =>
  generateKeyPairSync('rsa', { modulusLength: 2048 }).publicKey.export({ format: 'jwk' });

const publicKey = publicJwk();
const feedA = { ...publicKey, kid: 'reports-a', use: 'sig' };
const feedB = { n: publicKey.n, kid: 'reports-b', e: publicKey.e, kty: 'RSA' };
const rotated = { ...publicJwk(), kid: feedA.kid };

const results = {
  node: process.version,
  sameKeyDifferentKid: rsaThumbprint(feedA) === rsaThumbprint(feedB),
  newKeySameKid: rsaThumbprint(feedA) === rsaThumbprint(rotated),
  encodedLength: rsaThumbprint(feedA).length,
};

assert.equal(results.sameKeyDifferentKid, true);
assert.equal(results.newKeySameKid, false);
assert.equal(results.encodedLength, 43);
console.log(JSON.stringify(results, null, 2));

Using labels alongside the thumbprint

RFC 7517 describes the key identifier as a hint for matching a key from a set. Publishers can choose their own label conventions. Begin an inventory comparison by recording the trusted source and the application for which that source is authoritative. Store its label beside the computed thumbprint. A migration can produce two entries with the same key which clients still address under different labels. Inspecting both fields helps explain why a consumer keeps requesting the old entry. A thumbprint match establishes a relationship between the represented public parameters. The publisher's authority, the application's accepted algorithm and the validity of a specific signature remain separate results. Knowing which public key the inventory saw can point an operator to the right feed version for these checks.

Watching a planned key rotation

A signer can publish a replacement key before using it and temporarily keep the previous entry available. Record when the new thumbprint appears, when the old key disappears and what verification results occur during the overlap. A rename changes a label while preserving the thumbprint. Replacement under a reused label changes the thumbprint, which makes the timing of feed refreshes important to consumers. A staging test can watch one client with the latest feed and another with an older cached feed. Compare what each client receives with the result of its ordinary verification. If labels change without a key change, diagnose failed lookups separately. If the material changes under a stable label, inspect both the feed version and the timing of cache refresh before evaluating the rollover.

Recording enough information to reproduce a comparison

An inventory report benefits from the feed address, observation time, key type, source label, hash algorithm and public-key thumbprint. Reject unsupported key types explicitly, and validate member types and presence before accepting external JWK documents. The local example assumes RSA entries exported by its own runtime. Keep private material and credentials out of the comparison record. More than one feed entry can legitimately produce the same thumbprint. The public values then identify the same verification key while those entries can retain different purposes and labels. Before removing an old label, determine which active consumers still refer to it. Measure the rollout with actual verification results from the publisher and consumers, using the thumbprint report to explain what each observer received.

479 reads ✎ Write a comment

Reader discussion

Discussion prompt

What is the most important unanswered question about this topic?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.