SSH icon indicating copy to clipboard operation
SSH copied to clipboard

"unknown algorithm" error

Open Leandros opened this issue 1 year ago • 2 comments

Using ssh-key to create a new PrivateKey using an algorithm which doesn't have the correct crate feature enabled results in a runtime error (unknown algorithm).

The following code compiles fine but panics with an error at runtime.

# Cargo.toml
[dependencies]
ssh-key = "0.6"
// main.rs
use ssh_key::rand_core::OsRng;
use ssh_key::{Algorithm, PrivateKey};

fn main() {
    let private_key = PrivateKey::random(&mut OsRng, Algorithm::Ed25519).unwrap();
    dbg!(&private_key);
}

This is not very rusty. It would be much less surprising if the enum variant Ed25519 of the Algorithm enum would also be guarded by the feature that enables the algorithm.

Leandros avatar Feb 19 '25 09:02 Leandros

There are valid uses of that enum without the associated crate features, such as the ability to simply parse and serialize such keys without the associated cryptographic operations.

I suppose we could have an entirely separate, duplicated enum where each variant is gated on the associated crate features just for the use cases that require cryptographic support. I am not sure the added complexity is worth it, however.

tarcieri avatar Feb 19 '25 13:02 tarcieri

I suspected this when filing the issue. I agree that the added code duplication isn't ideal; however, the current situation isn't ideal, either.

Leandros avatar Feb 21 '25 07:02 Leandros