Skip to content

feat: add ECDSA key generation to bssh-keygen and default to P-384 - #370

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-369-ecdsa-keygen
Dec 4, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-369-ecdsa-keygen

Conversation

@inureyes

@inureyes inureyes commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

OpenSSH changed ssh-keygen's default key type to ECDSA P-384 (openbsd/src c7d9309). bssh-keygen follows: it can now generate software ECDSA keys, and plain bssh-keygen writes an ecdsa-sha2-nistp384 key to ~/.ssh/id_ecdsa.

  • keygen::generate_ecdsa (new src/keygen/ecdsa.rs) generates P-256, P-384, and P-521 keys via ssh-key's PrivateKey::random, with the same encoding, 0600 private file, and .pub layout as the Ed25519 generator. No new crates.
  • The duplicated write_private_key now lives once in src/keygen/mod.rs. RSA, Ed25519, and sk output is unchanged.
  • -t defaults to ecdsa. -b is optional and resolved per type (ECDSA 384, RSA 4096, ignored for Ed25519), and is validated before the overwrite prompt, .ssh or parent directory creation, or any write.
  • Man page, README, docs guide (docs/README.md), quick start, security guide, and CHANGELOG updated. The CHANGELOG lists the default change under a separate Behavior Changes heading: scripts relying on the old default must pass -t ed25519.

User-visible: the default output file changes from id_ed25519 to id_ecdsa. bssh-server gen-host-key keeps its Ed25519 default, and the existing RSA -b size bug is left for a separate issue, as scoped.

The CLI unit tests test_cli_defaults and cli_software_key_defaults were updated because they assert the old default and the old u32 type of bits.

Test plan

  • cargo test --lib keygen (33 passed, including the ECDSA round trip per curve, 0600 mode, invalid sizes 0/224/2048/4096 leave no file, and ssh-keygen -l reporting 256/384/521 ... (ECDSA))
  • cargo test --test keygen_cli_ecdsa_test (7 passed): the real binary with HOME=<tmp> and -y -q writes ~/.ssh/id_ecdsa as P-384, -t ecdsa -b 4096 exits non-zero without creating files or prompting, -t ECDSA, -b 256/521, Ed25519 ignores -b, -O is rejected for ecdsa
  • cargo test --test keygen_ecdsa_auth_test: a generated P-384 key authenticates against BsshServer::run_at through the bssh client, and an unlisted P-384 key fails with KeyAuthFailed. The client uses a tempdir known_hosts file, so running the test never writes to the developer's ~/.ssh/known_hosts
  • cargo test --bin bssh-keygen (16 passed), cargo clippy --lib --tests -- -D warnings, cargo fmt --check
  • cargo test --lib (1988 passed) and cargo test --tests -- --skip integration_test (all binaries passed) run locally
  • Manual smoke: plain bssh-keygen output is read by ssh-keygen -l as 384 ... (ECDSA), -t rsa without -b still writes 4096 bits, -t ecdsa -b 4096 exits 1 with the size error

Closes #369

OpenSSH switched ssh-keygen's default key type to ECDSA P-384, and bssh-keygen could not generate software ECDSA keys at all. This follows upstream.

- New keygen::ecdsa module (exposed as keygen::generate_ecdsa) generates P-256, P-384, and P-521 keys through ssh-key's PrivateKey::random, with the same OpenSSH encoding, 0600 private file, and .pub layout as the Ed25519 generator. Unsupported sizes fail before any file is written.
- The duplicated write_private_key helper now lives once in keygen/mod.rs, and RSA bit validation is exposed as rsa::validate_bits. RSA and Ed25519 output is unchanged.
- bssh-keygen defaults -t to ecdsa and writes ~/.ssh/id_ecdsa. -b is now optional and resolved per type (ECDSA 384, RSA 4096, ignored for Ed25519), and is validated before the overwrite prompt, directory creation, or any file write.
- Man page, README, quick start, security guide, and CHANGELOG describe the new type and call out the default change.

Tests: ecdsa unit tests (round trip per curve, 0600 mode, invalid sizes, ssh-keygen -l when available), binary tests through the real bssh-keygen CLI, and a publickey authentication test against BsshServer with a listed and an unlisted P-384 key.

Closes #369
@inureyes inureyes added status:review Under review type:enhancement New feature or request priority:medium Medium priority issue labels Dec 4, 2026
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Add software ECDSA (P-256/P-384/P-521) generation to bssh-keygen and make ECDSA P-384 the default, keeping RSA (4096 default), Ed25519, and security-key output unchanged.

Findings Addressed

  • None required fixing: no CRITICAL or HIGH findings.

Remaining Items

  • docs/README.md:142 still labels plain bssh-keygen as "Generate Ed25519 key (recommended)", which now contradicts the default (MEDIUM), left for a follow-up docs touch.
  • resolve_bits returns 0 as a placeholder for Ed25519; an Option<u32> would be more explicit (LOW), cosmetic.

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

Ed25519 and RSA generators changed only by moving the identical write_private_key into keygen/mod.rs and extracting RSA validation with the same messages, so their output is unchanged. The security-key and -K paths never read bits or the software -t default. Ran clippy (--lib --tests -D warnings), fmt check, keygen_cli_ecdsa_test (7 passed), keygen_ecdsa_auth_test (1 passed, real server auth plus unlisted-key rejection), and --bin bssh-keygen (16 passed).

@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member Author

Security and performance review

No CRITICAL or HIGH findings, so no fixes were pushed. Verified locally: cargo test --test keygen_cli_ecdsa_test, --test keygen_ecdsa_auth_test, --lib keygen, --bin bssh-keygen, and cargo clippy --lib --tests -- -D warnings all pass.

Checked and fine: write_private_key is moved verbatim; ECDSA uses rand::rng() (OS-seeded CSPRNG) like the Ed25519 and RSA paths; -b is now validated before .ssh creation, parent creation, and the overwrite prompt, which also moves RSA validation earlier; invalid bits leave no file; P-521 generation cost is negligible.

Introduced by this PR (LOW)

  • tests/keygen_ecdsa_auth_test.rs connects with ServerCheckMethod::NoCheck and a default config, so each run appends [127.0.0.1]:<random port> with the committed test host key to the developer's real ~/.ssh/known_hosts. Existing live tests already do this. Fix: Client::connect_with_ssh_config with SshConnectionConfig::default().with_known_hosts_files(Some(vec![<tempdir>/known_hosts]), Some(vec![])).
  • The free port is probed with bind(":0") and released before run_at binds it, a loopback-only race that can only cause flakes; the keys are throwaway.

Pre-existing, not worsened (follow-up candidates)

  • MEDIUM: write_private_key opens with O_CREAT|O_TRUNC and mode(0o600), which only applies on creation. Overwriting an existing 0644 file (prompt y or -y) leaves the new private key 0644; reproduced with the built binary. Fix: write to a 0600 create_new temp file in the same directory and rename, or set_permissions(0o600) on the handle before writing.
  • MEDIUM: the software path follows symlinks and output.exists() is false for a dangling link, so no prompt appears and the key lands at the link target (reproduced). The security-key path already guards this with sk_files::check_output.
  • LOW: a failed .pub write leaves the private key behind; GeneratedKey.private_key_pem copies the zeroizing buffer into a plain String.

docs/README.md still labeled plain bssh-keygen as Ed25519. It now names the ECDSA P-384 default at ~/.ssh/id_ecdsa and adds an explicit -t ed25519 example.

tests/keygen_ecdsa_auth_test.rs connected with the default client config, so each run appended a [127.0.0.1]:<port> line to the real ~/.ssh/known_hosts. It now connects through connect_with_ssh_config with a tempdir known_hosts file and /dev/null as the global file.

Validation: cargo fmt --check, cargo clippy --lib --tests -D warnings, cargo test --test keygen_ecdsa_auth_test, --test keygen_cli_ecdsa_test and --lib keygen pass. The count of ^\[127\.0\.0\.1\]: lines in ~/.ssh/known_hosts was 98 before and after the test run.

Refs #369
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 4, 2026
@inureyes
inureyes merged commit 819094d into main Dec 4, 2026
5 checks passed
@inureyes
inureyes deleted the feature/issue-369-ecdsa-keygen branch December 4, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority issue status:done Completed type:enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: support ECDSA key generation in bssh-keygen and default to ECDSA P-384

1 participant