Skip to content

Commit

Permalink
feat: add rsa key module
Browse files Browse the repository at this point in the history
Close #27

Signed-off-by: Xynnn007 <mading.ma@alibaba-inc.com>
  • Loading branch information
Xynnn007 committed Sep 18, 2022
1 parent 76b1b9d commit 460de25
Show file tree
Hide file tree
Showing 11 changed files with 1,071 additions and 61 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ picky = { version = "7.0.0-rc.3", default-features = false, features = [ "x509",
regex = "1.5.5"
serde_json = "1.0.79"
serde = { version = "1.0.136", features = ["derive"] }
sha2 = "0.10.2"
sha2 = { version = "0.10.6", features = ["oid"] }
thiserror = "1.0.30"
tokio = { version = "1.17.0", features = ["full"] }
tough = { version = "0.12.4", features = [ "http" ] }
Expand All @@ -48,6 +48,8 @@ digest = "0.10.3"
signature = { version = "1.5.0", features = [ "digest-preview" ] }
ed25519 = { version = "1", features = [ "alloc" ] }
ed25519-dalek-fiat = "0.1.0"
rsa = { git = "https://github.com/Xynnn007/RSA", branch = "master" }
pkcs1 = "0.4.0"

[dev-dependencies]
anyhow = "1.0.54"
Expand Down
2 changes: 1 addition & 1 deletion examples/key_interface/key_pair_import/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main() -> Result<()> {
inner.to_sigstore_signer()?;
println!("Converted SigStoreKeyPair to SigStoreSigner.");
}
SigStoreKeyPair::ED25519(_) => bail!("Wrong key pair type."),
_ => bail!("Wrong key pair type."),
}

Ok(())
Expand Down
69 changes: 68 additions & 1 deletion src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ pub use signing_key::SigStoreSigner;
pub use verification_key::CosignVerificationKey;

/// Different digital signature algorithms.
/// * `RSA_PSS_SHA256`: RSA PSS padding using SHA-256
/// for RSA signatures. All the `usize` member inside
/// an RSA enum represents the key size of the RSA key.
/// * `RSA_PSS_SHA384`: RSA PSS padding using SHA-384
/// for RSA signatures.
/// * `RSA_PSS_SHA512`: RSA PSS padding using SHA-512
/// for RSA signatures.
/// * `RSA_PKCS1_SHA256`: PKCS#1 1.5 padding using
/// SHA-256 for RSA signatures.
/// * `RSA_PKCS1_SHA384`: PKCS#1 1.5 padding using
/// SHA-384 for RSA signatures.
/// * `RSA_PKCS1_SHA512`: PKCS#1 1.5 padding using
/// SHA-512 for RSA signatures.
/// * `ECDSA_P256_SHA256_ASN1`: ASN.1 DER-encoded ECDSA
/// signatures using the P-256 curve and SHA-256. It
/// is the default signing scheme.
Expand All @@ -36,7 +49,12 @@ pub use verification_key::CosignVerificationKey;
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy)]
pub enum SigningScheme {
// TODO: Support RSA
RSA_PSS_SHA256(usize),
RSA_PSS_SHA384(usize),
RSA_PSS_SHA512(usize),
RSA_PKCS1_SHA256(usize),
RSA_PKCS1_SHA384(usize),
RSA_PKCS1_SHA512(usize),
ECDSA_P256_SHA256_ASN1,
ECDSA_P384_SHA384_ASN1,
ED25519,
Expand All @@ -50,6 +68,12 @@ impl TryFrom<&str> for SigningScheme {
"ECDSA_P256_SHA256_ASN1" => Ok(Self::ECDSA_P256_SHA256_ASN1),
"ECDSA_P384_SHA384_ASN1" => Ok(Self::ECDSA_P384_SHA384_ASN1),
"ED25519" => Ok(Self::ED25519),
"RSA_PSS_SHA256" => Ok(Self::RSA_PSS_SHA256(DEFAULT_KEY_SIZE)),
"RSA_PSS_SHA384" => Ok(Self::RSA_PSS_SHA384(DEFAULT_KEY_SIZE)),
"RSA_PSS_SHA512" => Ok(Self::RSA_PSS_SHA512(DEFAULT_KEY_SIZE)),
"RSA_PKCS1_SHA256" => Ok(Self::RSA_PKCS1_SHA256(DEFAULT_KEY_SIZE)),
"RSA_PKCS1_SHA384" => Ok(Self::RSA_PKCS1_SHA384(DEFAULT_KEY_SIZE)),
"RSA_PKCS1_SHA512" => Ok(Self::RSA_PKCS1_SHA512(DEFAULT_KEY_SIZE)),
unknown => Err(format!("Unsupported signing algorithm: {}", unknown)),
}
}
Expand All @@ -68,6 +92,48 @@ impl SigningScheme {
SigningScheme::ED25519 => {
SigStoreSigner::ED25519(Ed25519Signer::from_ed25519_keys(&Ed25519Keys::new()?)?)
}
SigningScheme::RSA_PSS_SHA256(bit_size) => {
SigStoreSigner::RSA_PSS_SHA256(RSASigner::from_rsa_keys(
&RSAKeys::new(*bit_size)?,
DigestAlgorithm::Sha256,
PaddingScheme::PSS,
))
}
SigningScheme::RSA_PSS_SHA384(bit_size) => {
SigStoreSigner::RSA_PSS_SHA384(RSASigner::from_rsa_keys(
&RSAKeys::new(*bit_size)?,
DigestAlgorithm::Sha384,
PaddingScheme::PSS,
))
}
SigningScheme::RSA_PSS_SHA512(bit_size) => {
SigStoreSigner::RSA_PSS_SHA512(RSASigner::from_rsa_keys(
&RSAKeys::new(*bit_size)?,
DigestAlgorithm::Sha512,
PaddingScheme::PSS,
))
}
SigningScheme::RSA_PKCS1_SHA256(bit_size) => {
SigStoreSigner::RSA_PKCS1_SHA256(RSASigner::from_rsa_keys(
&RSAKeys::new(*bit_size)?,
DigestAlgorithm::Sha256,
PaddingScheme::PKCS1v15,
))
}
SigningScheme::RSA_PKCS1_SHA384(bit_size) => {
SigStoreSigner::RSA_PKCS1_SHA384(RSASigner::from_rsa_keys(
&RSAKeys::new(*bit_size)?,
DigestAlgorithm::Sha384,
PaddingScheme::PKCS1v15,
))
}
SigningScheme::RSA_PKCS1_SHA512(bit_size) => {
SigStoreSigner::RSA_PKCS1_SHA512(RSASigner::from_rsa_keys(
&RSAKeys::new(*bit_size)?,
DigestAlgorithm::Sha512,
PaddingScheme::PKCS1v15,
))
}
})
}
}
Expand Down Expand Up @@ -98,6 +164,7 @@ pub mod verification_key;
use self::signing_key::{
ecdsa::ec::{EcdsaKeys, EcdsaSigner},
ed25519::{Ed25519Keys, Ed25519Signer},
rsa::{keypair::RSAKeys, DigestAlgorithm, PaddingScheme, RSASigner, DEFAULT_KEY_SIZE},
};

pub mod signing_key;
Expand Down
37 changes: 36 additions & 1 deletion src/crypto/signing_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use crate::errors::*;
use self::{
ecdsa::{ec::EcdsaSigner, ECDSAKeys},
ed25519::{Ed25519Keys, Ed25519Signer},
rsa::{keypair::RSAKeys, RSASigner},
};

use super::{verification_key::CosignVerificationKey, SigningScheme};
Expand All @@ -89,6 +90,9 @@ pub const SIGSTORE_PRIVATE_KEY_PEM_LABEL: &str = "ENCRYPTED SIGSTORE PRIVATE KEY
/// The label for pem of private keys.
pub const PRIVATE_KEY_PEM_LABEL: &str = "PRIVATE KEY";

/// The label for pem of RSA private keys.
pub const RSA_PRIVATE_KEY_PEM_LABEL: &str = "RSA PRIVATE KEY";

/// Every signing scheme must implement this interface.
/// All private export methods using the wrapper `Zeroizing`.
/// It will tell the compiler when the
Expand Down Expand Up @@ -125,7 +129,7 @@ pub trait KeyPair {
pub enum SigStoreKeyPair {
ECDSA(ECDSAKeys),
ED25519(Ed25519Keys),
// RSA,
RSA(RSAKeys),
}

/// This macro helps to reduce duplicated code.
Expand All @@ -147,6 +151,7 @@ macro_rules! sigstore_keypair_code {
match $obj {
SigStoreKeyPair::ECDSA(keys) => keys.as_inner().$func($($args,)*),
SigStoreKeyPair::ED25519(keys) => keys.$func($($args,)*),
SigStoreKeyPair::RSA(keys) => keys.$func($($args,)*),
}
}
}
Expand Down Expand Up @@ -217,6 +222,12 @@ pub trait Signer {

#[allow(non_camel_case_types)]
pub enum SigStoreSigner {
RSA_PSS_SHA256(RSASigner),
RSA_PSS_SHA384(RSASigner),
RSA_PSS_SHA512(RSASigner),
RSA_PKCS1_SHA256(RSASigner),
RSA_PKCS1_SHA384(RSASigner),
RSA_PKCS1_SHA512(RSASigner),
ECDSA_P256_SHA256_ASN1(EcdsaSigner<p256::NistP256, sha2::Sha256>),
ECDSA_P384_SHA384_ASN1(EcdsaSigner<p384::NistP384, sha2::Sha384>),
ED25519(Ed25519Signer),
Expand All @@ -230,6 +241,12 @@ impl SigStoreSigner {
SigStoreSigner::ECDSA_P256_SHA256_ASN1(inner) => inner,
SigStoreSigner::ECDSA_P384_SHA384_ASN1(inner) => inner,
SigStoreSigner::ED25519(inner) => inner,
SigStoreSigner::RSA_PSS_SHA256(inner) => inner,
SigStoreSigner::RSA_PSS_SHA384(inner) => inner,
SigStoreSigner::RSA_PSS_SHA512(inner) => inner,
SigStoreSigner::RSA_PKCS1_SHA256(inner) => inner,
SigStoreSigner::RSA_PKCS1_SHA384(inner) => inner,
SigStoreSigner::RSA_PKCS1_SHA512(inner) => inner,
}
}

Expand All @@ -244,6 +261,12 @@ impl SigStoreSigner {
SigStoreSigner::ECDSA_P256_SHA256_ASN1(_) => SigningScheme::ECDSA_P256_SHA256_ASN1,
SigStoreSigner::ECDSA_P384_SHA384_ASN1(_) => SigningScheme::ECDSA_P384_SHA384_ASN1,
SigStoreSigner::ED25519(_) => SigningScheme::ED25519,
SigStoreSigner::RSA_PSS_SHA256(_) => SigningScheme::RSA_PSS_SHA256(0),
SigStoreSigner::RSA_PSS_SHA384(_) => SigningScheme::RSA_PSS_SHA384(0),
SigStoreSigner::RSA_PSS_SHA512(_) => SigningScheme::RSA_PSS_SHA512(0),
SigStoreSigner::RSA_PKCS1_SHA256(_) => SigningScheme::RSA_PKCS1_SHA256(0),
SigStoreSigner::RSA_PKCS1_SHA384(_) => SigningScheme::RSA_PKCS1_SHA384(0),
SigStoreSigner::RSA_PKCS1_SHA512(_) => SigningScheme::RSA_PKCS1_SHA512(0),
};
self.as_inner()
.key_pair()
Expand All @@ -262,6 +285,18 @@ impl SigStoreSigner {
SigStoreSigner::ED25519(inner) => {
SigStoreKeyPair::ED25519(Ed25519Keys::from_ed25519key(inner.ed25519_keys())?)
}
SigStoreSigner::RSA_PSS_SHA256(inner) => SigStoreKeyPair::RSA(inner.rsa_keys().clone()),
SigStoreSigner::RSA_PSS_SHA384(inner) => SigStoreKeyPair::RSA(inner.rsa_keys().clone()),
SigStoreSigner::RSA_PSS_SHA512(inner) => SigStoreKeyPair::RSA(inner.rsa_keys().clone()),
SigStoreSigner::RSA_PKCS1_SHA256(inner) => {
SigStoreKeyPair::RSA(inner.rsa_keys().clone())
}
SigStoreSigner::RSA_PKCS1_SHA384(inner) => {
SigStoreKeyPair::RSA(inner.rsa_keys().clone())
}
SigStoreSigner::RSA_PKCS1_SHA512(inner) => {
SigStoreKeyPair::RSA(inner.rsa_keys().clone())
}
})
}
}
Expand Down
18 changes: 0 additions & 18 deletions src/crypto/signing_key/rsa.rs

This file was deleted.

Loading

0 comments on commit 460de25

Please sign in to comment.