Skip to content

Sec1 round-trip fails with AlgorithmParametersMissing #1610

Open
@Firstyear

Description

@Firstyear

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions