Skip to content

crypto: refactor keyObject.toCryptoKey() and SubtleCrypto.getPublicKey()#63622

Open
panva wants to merge 1 commit into
nodejs:mainfrom
panva:refactor-crypto-key-conversions
Open

crypto: refactor keyObject.toCryptoKey() and SubtleCrypto.getPublicKey()#63622
panva wants to merge 1 commit into
nodejs:mainfrom
panva:refactor-crypto-key-conversions

Conversation

@panva
Copy link
Copy Markdown
Member

@panva panva commented May 28, 2026

Move KeyObject.prototype.toCryptoKey() onto the base KeyObject class and dispatch from the cached native key type. Both secret and asymmetric conversions now pass a KeyObjectHandle through the Web Crypto import paths.

Expose KeyObjectHandle.prototype.getKeyType() so asymmetric importers can validate public/private usages without wrapping the handle back into a KeyObject. Secret importers likewise consume KeyObjectHandle directly.

Use the shared asymmetric conversion helper to derive public CryptoKeys for SubtleCrypto.getPublicKey(), avoiding the temporary PrivateKeyObject/createPublicKey round trip while keeping usage validation in the import path.

Update getPublicKey and KeyObject.toCryptoKey tests to be driven from the Web Crypto supported-algorithm registry so new algorithms require either coverage or an explicit skip.

Resolves this TODO:

// TODO(panva): this is by no means a hot path, but let's still follow up to get
// rid of this awkwardness
const keyObject = createPublicKey(new PrivateKeyObject(getCryptoKeyHandle(key)));
return keyObject.toCryptoKey(getCryptoKeyAlgorithm(key), true, usages);

Move KeyObject.prototype.toCryptoKey() onto the base KeyObject
class and dispatch from the cached native key type. Both secret and
asymmetric conversions now pass a KeyObjectHandle through the Web
Crypto import paths.

Expose KeyObjectHandle.prototype.getKeyType() so asymmetric importers
can validate public/private usages without wrapping the handle back
into a KeyObject. Secret importers likewise consume KeyObjectHandle
directly.

Use the shared asymmetric conversion helper to derive public
CryptoKeys for SubtleCrypto.getPublicKey(), avoiding the temporary
PrivateKeyObject/createPublicKey round trip while keeping usage
validation in the import path.

Update getPublicKey and KeyObject.toCryptoKey tests to be driven from
the Web Crypto supported-algorithm registry so new algorithms require
either coverage or an explicit skip.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
@panva panva added crypto Issues and PRs related to the crypto subsystem. webcrypto labels May 28, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels May 28, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

❌ Patch coverage is 97.80702% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.30%. Comparing base (8d0a3b8) to head (ca3bfce).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/crypto/keys.js 97.91% 4 Missing ⚠️
src/crypto/crypto_keys.cc 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #63622   +/-   ##
=======================================
  Coverage   90.29%   90.30%           
=======================================
  Files         730      730           
  Lines      234782   234814   +32     
  Branches    43953    43961    +8     
=======================================
+ Hits       211993   212044   +51     
+ Misses      14501    14479   -22     
- Partials     8288     8291    +3     
Files with missing lines Coverage Δ
lib/internal/crypto/aes.js 92.19% <100.00%> (-0.06%) ⬇️
lib/internal/crypto/cfrg.js 94.63% <100.00%> (-0.05%) ⬇️
lib/internal/crypto/chacha20_poly1305.js 98.46% <100.00%> (-0.02%) ⬇️
lib/internal/crypto/ec.js 95.50% <100.00%> (-0.05%) ⬇️
lib/internal/crypto/mac.js 99.01% <100.00%> (-0.01%) ⬇️
lib/internal/crypto/ml_dsa.js 97.44% <100.00%> (-0.03%) ⬇️
lib/internal/crypto/ml_kem.js 95.45% <100.00%> (-0.04%) ⬇️
lib/internal/crypto/rsa.js 94.44% <100.00%> (-0.04%) ⬇️
lib/internal/crypto/webcrypto.js 97.11% <100.00%> (-0.01%) ⬇️
src/crypto/crypto_keys.h 65.11% <ø> (ø)
... and 2 more

... and 36 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crypto Issues and PRs related to the crypto subsystem. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. webcrypto

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants