Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sec1 round-trip fails with AlgorithmParametersMissing #1610

Open
Firstyear opened this issue Nov 27, 2024 · 2 comments
Open

Sec1 round-trip fails with AlgorithmParametersMissing #1610

Firstyear opened this issue Nov 27, 2024 · 2 comments

Comments

@Firstyear
Copy link

While testing with rustls I noticed that sec1-der and pem exported ec private keys would fail to decode. The following is a minimal reproducer. I will say, this could be my fault for mishandling the API somehow, but after a lot of testing and verification I think there may be a fault in the way that sec1 is encoded.

Any advice would be welcome.

#[cfg(test)]
mod tests {
    use p384::ecdsa::SigningKey;
    use p384::pkcs8::DecodePrivateKey;
    use p384::pkcs8::EncodePrivateKey;
    use p384::SecretKey;
    use sec1::DecodeEcPrivateKey;

    #[test]
    fn sec1_pem() {
        let mut rng = rand::thread_rng();

        let signing_key = SigningKey::random(&mut rng);

        let server_private_key_pem = SecretKey::from(&signing_key)
            .to_sec1_pem(Default::default())
            .unwrap();

        let _ = SigningKey::from_sec1_pem(server_private_key_pem.as_str()).unwrap();
    }

    #[test]
    fn sec1_der() {
        let mut rng = rand::thread_rng();

        let signing_key = SigningKey::random(&mut rng);

        let server_private_key_pem = SecretKey::from(&signing_key).to_sec1_der().unwrap();

        let _ = SigningKey::from_sec1_der(server_private_key_pem.as_slice()).unwrap();
    }
}
thread 'tests::sec1_pem' panicked at src/lib.rs:19:76:
called `Result::unwrap()` on an `Err` value: Pkcs8(PublicKey(AlgorithmParametersMissing))

thread 'tests::sec1_der' panicked at src/lib.rs:30:78:
called `Result::unwrap()` on an `Err` value: Pkcs8(PublicKey(AlgorithmParametersMissing))
[package]
name = "rustcrypto-sec1"
version = "0.1.0"
edition = "2021"

[dependencies]

p384 = "0.13"
sec1 = "0.7"
rand = "0.8"
@tarcieri
Copy link
Member

tarcieri commented Nov 27, 2024

Possible dup of RustCrypto/traits#1707

@Firstyear
Copy link
Author

Firstyear commented Nov 27, 2024

I did test with openssl and it did fail to identify the keys in the manner described in that issue. So yes, could be a dup :)

Thank you!

Would it be worth having this kind of "round-trip" test as a unit test?

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

No branches or pull requests

2 participants