Skip to content

fix: support SEC1 EC keys with full domain parameters - #719

Merged
Eugeny merged 3 commits into
Eugeny:mainfrom
SakuraPuare:fix/sec1-full-domain-params
Jun 5, 2026
Merged

fix: support SEC1 EC keys with full domain parameters#719
Eugeny merged 3 commits into
Eugeny:mainfrom
SakuraPuare:fix/sec1-full-domain-params

Conversation

@SakuraPuare

Copy link
Copy Markdown
Contributor

Problem

SEC1 EC private keys that use full domain parameters (instead of a named curve OID) fail to parse with:

Keys(Der(Error { kind: TagUnexpected { expected: Some(Tag(0x10: SEQUENCE)), actual: Tag(0x04: OCTET STRING) }, position: None }))

This affects keys generated with openssl ecparam -param_enc explicit and similar tools that embed the full EC domain parameters in the [0] parameters field of the SEC1 structure.

Fixes #718

Root Cause

The sec1 crate's EcPrivateKey decoder expects the [0] parameters field to contain either a named curve OID or nothing (optional field). But some key generators produce a full EC domain parameters SEQUENCE instead, which the sec1 crate rejects with a TagUnexpected error.

Fix

Added a manual DER parser as a fallback in decode_pkcs8 that:

  1. Detects SEC1 keys with full domain parameters (when sec1::EcPrivateKey decode fails)
  2. Extracts the curve OID from the prime field length in the domain parameters
  3. Constructs the EC keypair directly from the private key bytes using p256/p384/p521 crates

The fallback only runs when the sec1 crate's strict parser fails, so there is no performance impact on normal keys.

Changes

  • russh/src/keys/format/pkcs8.rs: Added fallback parser functions for SEC1 keys with full domain parameters
  • russh/src/keys/format/tests.rs: Added tests for P-256 and P-521 keys with explicit domain parameters, and negative tests for malformed DER

Testing

  • 165 tests pass, 0 warnings
  • New tests cover P-256 and P-521 with explicit domain parameters
  • Negative tests for malformed DER and truncated keys
  • All existing tests continue to pass

SakuraPuare and others added 3 commits May 31, 2026 00:39
The sec1 crate cannot parse EC private keys that use full domain
parameters instead of a named curve OID in the [0] parameters field.
This affects keys generated with 'openssl ecparam -param_enc explicit'
and similar tools.

Add a manual DER parser as a fallback in decode_pkcs8 that:
1. Detects SEC1 keys with full domain parameters
2. Extracts the curve OID from the prime field length
3. Constructs the EC keypair directly from the private key bytes

Includes a test with a P-256 key using explicit domain parameters.

Fixes #718
@Eugeny

Eugeny commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Thank you! I've updated the code to use the der crate for parsing

@Eugeny
Eugeny merged commit e7f9f06 into Eugeny:main Jun 5, 2026
11 checks passed
@Eugeny

Eugeny commented Jun 5, 2026

Copy link
Copy Markdown
Owner

@all-contributors please add @SakuraPuare for code

@allcontributors

Copy link
Copy Markdown
Contributor

@Eugeny

I've put up a pull request to add @SakuraPuare! 🎉

Eugeny pushed a commit that referenced this pull request Jun 5, 2026
Adds @SakuraPuare as a contributor for code.

This was requested by Eugeny [in this
comment](#719 (comment))

[skip ci]

---------

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
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.

Bug: All PEM/SEC1-format EC private keys fail to parse — TagUnexpected { expected: SEQUENCE, actual: OCTET STRING }

2 participants