Skip to content

Commit

Permalink
Create remaining RSA contructors
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Oct 4, 2023
1 parent 83867a1 commit 8182fbf
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ impl KeyPair {
pkcs8: &[u8],
alg: &'static SignatureAlgorithm,
) -> Result<Self, Error> {
let serialized_der = pkcs8.to_vec();

if alg == &PKCS_ED25519 {
return Ok(Self::pkcs_ed25519(pkcs8)?);
}
Expand All @@ -120,31 +118,13 @@ impl KeyPair {
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)?);
}

panic!("Unknown SignatureAlgorithm specified!")
Expand Down Expand Up @@ -207,6 +187,29 @@ impl KeyPair {
serialized_der: pkcs8.to_vec(),
})
}

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

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

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

0 comments on commit 8182fbf

Please sign in to comment.