Skip to content

Commit

Permalink
Remove duplication between constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 4, 2023
1 parent 0b14bd5 commit e086f4a
Showing 1 changed file with 76 additions and 76 deletions.
152 changes: 76 additions & 76 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,111 +105,111 @@ impl KeyPair {
pkcs8: &[u8],
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error> {
let serialized_der = pkcs8.to_vec();

if alg == &PKCS_ED25519 {
return Ok(KeyPair {
kind: KeyPairKind::Ed(Ed25519KeyPair::from_pkcs8_maybe_unchecked(pkcs8)?),
alg,
serialized_der,
});
return Ok(Self::pkcs_ed25519(pkcs8)?);

Check warning on line 109 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L109

Added line #L109 was not covered by tests
}
if alg == &PKCS_ECDSA_P256_SHA256 {
return Ok(KeyPair {
kind: KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P256_SHA256_ASN1_SIGNING,
pkcs8,
)?),
alg,
serialized_der,
});
return Ok(Self::pkcs_ecdsa_p256_sha256(pkcs8)?);

Check warning on line 112 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L112

Added line #L112 was not covered by tests
}
if alg == &PKCS_ECDSA_P384_SHA384 {
return Ok(KeyPair {
kind: KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P384_SHA384_ASN1_SIGNING,
pkcs8,
)?),
alg,
serialized_der,
});
return Ok(Self::pkcs_ecdsa_p384_sha384(pkcs8)?);

Check warning on line 115 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L115

Added line #L115 was not covered by tests
}
if alg == &PKCS_RSA_SHA256 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(
RsaKeyPair::from_pkcs8(pkcs8)?,
&signature::RSA_PKCS1_SHA256,
),
alg,
serialized_der,
});
return Ok(Self::pkcs_rsa_sha256(pkcs8)?);
}
if alg == &PKCS_RSA_SHA384 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(
RsaKeyPair::from_pkcs8(pkcs8)?,
&signature::RSA_PKCS1_SHA384,
),
alg,
serialized_der,
});
return Ok(Self::pkcs_rsa_sha384(pkcs8)?);
}
if alg == &PKCS_RSA_SHA512 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(
RsaKeyPair::from_pkcs8(pkcs8)?,
&signature::RSA_PKCS1_SHA512,
),
alg,
serialized_der,
});
return Ok(Self::pkcs_rsa_sha512(pkcs8)?);
}
if alg == &PKCS_RSA_PSS_SHA256 {
return Ok(KeyPair {
kind: KeyPairKind::Rsa(RsaKeyPair::from_pkcs8(pkcs8)?, &signature::RSA_PSS_SHA256),
alg,
serialized_der,
});
return Ok(Self::pkcs_rsa_pss_sha256(pkcs8)?);
}

Check warning on line 128 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L125-L128

Added lines #L125 - L128 were not covered by tests

panic!("Unknown SignatureAlgorithm specified!")

Check warning on line 130 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L130

Added line #L130 was not covered by tests
}

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(),
});
if let Ok(kp) = Self::pkcs_ed25519(der) {
return Ok(kp);
}

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) = Self::pkcs_ecdsa_p256_sha256(der) {
return Ok(kp);
}

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(),
});
if let Ok(kp) = Self::pkcs_ecdsa_p384_sha384(der) {
return Ok(kp);

Check warning on line 143 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L143

Added line #L143 was 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(),
});
if let Ok(kp) = Self::pkcs_rsa_sha256(der) {
return Ok(kp);
}

return Err(Error::CouldNotParseKeyPair);

Check warning on line 150 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L148-L150

Added lines #L148 - L150 were not covered by tests
}

fn pkcs_ed25519(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Ed(Ed25519KeyPair::from_pkcs8_maybe_unchecked(der)?),
alg: &PKCS_ED25519,
serialized_der: der.to_vec(),
})
}

fn pkcs_ecdsa_p256_sha256(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P256_SHA256_ASN1_SIGNING,
der,
)?),
alg: &PKCS_ECDSA_P256_SHA256,
serialized_der: der.to_vec(),
})
}

fn pkcs_ecdsa_p384_sha384(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Ec(EcdsaKeyPair::from_pkcs8(
&signature::ECDSA_P384_SHA384_ASN1_SIGNING,
der,
)?),
alg: &PKCS_ECDSA_P384_SHA384,
serialized_der: der.to_vec(),

Check warning on line 179 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L178-L179

Added lines #L178 - L179 were not covered by tests
})
}

fn pkcs_rsa_sha256(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Rsa(RsaKeyPair::from_pkcs8(der)?, &signature::RSA_PKCS1_SHA256),
alg: &PKCS_RSA_SHA256,
serialized_der: der.to_vec(),
})
}

fn pkcs_rsa_pss_sha256(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Rsa(RsaKeyPair::from_pkcs8(der)?, &signature::RSA_PSS_SHA256),
alg: &PKCS_RSA_PSS_SHA256,
serialized_der: der.to_vec(),

Check warning on line 195 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L191-L195

Added lines #L191 - L195 were not covered by tests
})
}

Check warning on line 197 in src/key_pair.rs

View check run for this annotation

Codecov / codecov/patch

src/key_pair.rs#L197

Added line #L197 was not covered by tests

fn pkcs_rsa_sha384(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Rsa(RsaKeyPair::from_pkcs8(der)?, &signature::RSA_PKCS1_SHA384),
alg: &PKCS_RSA_SHA384,
serialized_der: der.to_vec(),
})
}
fn pkcs_rsa_sha512(der: &[u8]) -> Result<KeyPair, Error> {
Ok(KeyPair {
kind: KeyPairKind::Rsa(RsaKeyPair::from_pkcs8(der)?, &signature::RSA_PKCS1_SHA512),
alg: &PKCS_RSA_SHA512,
serialized_der: der.to_vec(),
})
}
}

/// A private key that is not directly accessible, but can be used to sign messages
Expand Down

0 comments on commit e086f4a

Please sign in to comment.