Skip to content

fix(x509): explorePrivateKey no longer crashes on EC keys - #83

Merged
erossignon merged 1 commit into
masterfrom
fix/explore-ec-private-key
Aug 23, 2026
Merged

erossignon merged 1 commit into
masterfrom
fix/explore-ec-private-key

Conversation

@erossignon

Copy link
Copy Markdown
Member

Fixes #12.

Problem

explorePrivateKey() unconditionally assumed a PKCS#1 RSA structure. On an EC private key it took a fallback path that indexes into ASN.1 blocks that simply aren't there for a SEC1 ECPrivateKey, and crashed with a bare, unactionable error:

TypeError: Cannot read property 'position' of undefined
    at f (.../node-opcua-crypto/dist/source/explore_private_key.js:7:27)

PR #10 (referenced in the issue) added EC support to certificate exploring, but never to private-key exploring — this issue's gap.

Fix

  • explorePrivateKey() now checks the key's asymmetricKeyType up front and throws a clear, actionable error for anything other than RSA, instead of crashing on an out-of-bounds ASN.1 read.
  • A new exploreEcPrivateKey() actually parses the SEC1 ECPrivateKey structure (version, private scalar d, named curve from the [0] ECParameters OID, and the optional [1] publicKey bit string), for both raw SEC1 (-----BEGIN EC PRIVATE KEY-----) and PKCS#8-wrapped EC keys — normalized to the same SEC1 DER via Node's own KeyObject.export(), so both encodings are handled uniformly.
  • RSA behavior of explorePrivateKey() is unchanged (same fields, same values) — verified against the existing RSA fixtures/tests, no regressions in the 283-test suite.

Why a new function instead of widening PrivateKeyInternals

explorePrivateKey()'s return shape (modulus, publicExponent, ...) is RSA-specific, and two existing callers (certificateMatchesPrivateKey, publicKeyAndPrivateKeyMatches) rely on .modulus directly — a concept that doesn't exist for EC keys. Turning the return type into a discriminated union would force those callers (and everyone else's TypeScript) to add narrowing they don't need. A separate exploreEcPrivateKey() keeps the RSA API and its callers untouched while giving EC keys their own, correctly-typed result.

Testing

  • Added 4 tests to test_explore_private_key.ts: EC key exploring (SEC1 and PKCS#8-wrapped), and the two "wrong key type" error paths for both functions.
  • Manually reproduced the original crash against master before the fix, confirmed it no longer reproduces after.
  • Full node-opcua-crypto-test suite: 283 passed, 6 skipped, 0 regressions.

explorePrivateKey() assumed a PKCS#1 RSA structure unconditionally; on
an EC key it indexed into ASN.1 blocks that don't exist there and threw
a bare "Cannot read properties of undefined (reading 'position')" (#12).

It now checks the key type up front and fails with an actionable error
instead. A new exploreEcPrivateKey() actually parses the SEC1
ECPrivateKey structure (version, private scalar, named curve, optional
public point), for both raw SEC1 and PKCS#8-wrapped EC keys.
@erossignon
erossignon merged commit 7dce56a into master Aug 23, 2026
20 checks passed
@erossignon
erossignon deleted the fix/explore-ec-private-key branch August 23, 2026 14:18
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.

ECC private keys can't explored

1 participant