Skip to content

Commit

Permalink
Refactor guess_kind_from_der
Browse files Browse the repository at this point in the history
Instead of chaining if-let-else constructs, we early return as soon
as we successfully parse a keypair from the provided bytes. This is
a tiny bit more code but easier to understand.
  • Loading branch information
thomaseizinger committed Oct 4, 2023
1 parent 95fb9fb commit c3abdc5
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,40 @@ impl KeyPair {
})
}

pub(crate) fn guess_kind_from_der(pkcs8: &[u8]) -> Result<KeyPair, Error> {
let (kind, alg) = if let Ok(edkp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8) {
(KeyPairKind::Ed(edkp), &PKCS_ED25519)
} else if let Ok(eckp) =
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P256_SHA256_ASN1_SIGNING, pkcs8)
{
(KeyPairKind::Ec(eckp), &PKCS_ECDSA_P256_SHA256)
} else if let Ok(eckp) =
EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P384_SHA384_ASN1_SIGNING, pkcs8)
{
(KeyPairKind::Ec(eckp), &PKCS_ECDSA_P384_SHA384)
} else if let Ok(rsakp) = RsaKeyPair::from_pkcs8(pkcs8) {
(
KeyPairKind::Rsa(rsakp, &signature::RSA_PKCS1_SHA256),
&PKCS_RSA_SHA256,
)
} else {
return Err(Error::CouldNotParseKeyPair);
};
pub(crate) fn guess_kind_from_der(der: &[u8]) -> Result<KeyPair, Error> {
if let Ok(kp) = Ed25519KeyPair::from_pkcs8_maybe_unchecked(der) {
return Ok(KeyPair {
kind: KeyPairKind::Ed(kp),
alg: &PKCS_ED25519,
serialized_der: der.to_vec(),
});
}

Ok(KeyPair {
kind,
alg,
serialized_der: pkcs8.to_vec(),
})
if let Ok(kp) = EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P256_SHA256_ASN1_SIGNING, der) {
return Ok(KeyPair {
kind: KeyPairKind::Ec(kp),
alg: &PKCS_ECDSA_P256_SHA256,
serialized_der: der.to_vec(),
});
}

if let Ok(kp) = EcdsaKeyPair::from_pkcs8(&signature::ECDSA_P384_SHA384_ASN1_SIGNING, der) {
return Ok(KeyPair {
kind: KeyPairKind::Ec(kp),
alg: &PKCS_ECDSA_P384_SHA384,
serialized_der: der.to_vec(),
});

Check warning on line 167 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L163-L167

Added lines #L163 - L167 were not covered by tests
}

if let Ok(kp) = RsaKeyPair::from_pkcs8(der) {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(kp, &signature::RSA_PKCS1_SHA256),
alg: &PKCS_RSA_SHA256,
serialized_der: der.to_vec(),
});
}

return Err(Error::CouldNotParseKeyPair);

Check warning on line 178 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L176-L178

Added lines #L176 - L178 were not covered by tests
}
}

Expand Down

0 comments on commit c3abdc5

Please sign in to comment.