Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix public share private key decryption #6

Merged
merged 1 commit into from Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "dco3_crypto"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
authors = ["Octavio Simone"]
repository = "https://github.com/unbekanntes-pferd/dco3-crypto"
Expand Down
9 changes: 5 additions & 4 deletions README.md
Expand Up @@ -42,7 +42,7 @@ Using the crate currently binds to the latest openssl version and is compiled in
See [crates.io](https://crates.io/crates/dco3_crypto)
TL;DR Add the following line to your Cargo.toml file (dependencies):
```toml
dco3_crypto = "0.4.0"
dco3_crypto = "0.4.1"
```

## Documentation
Expand All @@ -62,7 +62,7 @@ Therefore, the minimum required import is *always* `DracoonCrypto` and the relev
In order to
- generate a (plain) user keypair
- en/decrypt a user keypair
- encrypt a private only
- decrypt a private only
- encrypt a file key with a public key (user keypair)
- decrypt a file key with a private key (user keypair)

Expand All @@ -89,14 +89,15 @@ let enc_keypair = DracoonCrypto::encrypt_private_key(secret, new_keypair).unwrap

```

Encrypt a private key only:
Decrypt a private key only (for public share use):

```rust
use dco3_crypto::{DracoonCrypto, DracoonRSACrypto, UserKeypairVersion};

let new_keypair = DracoonCrypto::create_plain_user_keypair(UserKeyPairVersion::RSA4096).unwrap();
let secret ="VerySecret123!";
let enc_private_key = DracoonCrypto::encrypt_private_key_only(secret, new_keypair.private_key_container).unwrap();
let enc_keypair = DracoonCrypto::encrypt_private_key(secret, new_keypair).unwrap();
let plain_private_key = DracoonCrypto::decrypt_private_key_only(secret, enc_keypair.private_key_container).unwrap();

```

Expand Down
59 changes: 31 additions & 28 deletions src/lib.rs
Expand Up @@ -186,34 +186,6 @@ impl DracoonRSACrypto for DracoonCrypto {
private_key_pem,
))
}
/// Encrypts a private key container - specifically required for public shares - using
/// a secret provided as parameter.
/// # Example
/// ```
/// use dco3_crypto::{DracoonCrypto, DracoonRSACrypto, UserKeyPairVersion};
/// let secret = "VerySecret123!";
/// let plain_4096_keypair =
/// DracoonCrypto::create_plain_user_keypair(UserKeyPairVersion::RSA4096).unwrap();
/// let enc_4096_private_key = DracoonCrypto::encrypt_private_key_only(secret, plain_4096_keypair.private_key_container).unwrap();
/// ```
///
fn encrypt_private_key_only(
secret: &str,
plain_private_key: PrivateKeyContainer,
) -> Result<PrivateKeyContainer, DracoonCryptoError> {
let secret = secret.as_bytes();
let private_key_pem = plain_private_key.private_key.as_bytes();

let rsa = Rsa::private_key_from_pem(private_key_pem)?;
let rsa = PKey::from_rsa(rsa)?;

let private_key_pem =
rsa.private_key_to_pem_pkcs8_passphrase(Cipher::aes_256_cbc(), secret)?;
let private_key_pem = std::str::from_utf8(&private_key_pem)?;

Ok(PrivateKeyContainer::new(private_key_pem.to_string(), plain_private_key.version))

}
/// Decrypts an encrypted keypair container - specifically the private key - using
/// a secret provided as parameter.
/// # Example
Expand Down Expand Up @@ -252,6 +224,37 @@ impl DracoonRSACrypto for DracoonCrypto {
&private_key_pem,
))
}

/// Decrypts a private key container - specifically required for public shares - using
/// a secret provided as parameter.
/// # Example
/// ```
/// use dco3_crypto::{DracoonCrypto, DracoonRSACrypto, UserKeyPairVersion};
/// let secret = "VerySecret123!";
/// let plain_4096_keypair =
/// DracoonCrypto::create_plain_user_keypair(UserKeyPairVersion::RSA4096).unwrap();
/// let enc_4096_keypair = DracoonCrypto::encrypt_private_key(secret, plain_4096_keypair).unwrap();
/// let plain_private_key = DracoonCrypto::decrypt_private_key_only(secret, enc_4096_keypair.private_key_container).unwrap();
/// ```
///
fn decrypt_private_key_only(
secret: &str,
plain_private_key: PrivateKeyContainer,
) -> Result<PrivateKeyContainer, DracoonCryptoError> {
let secret = secret.as_bytes();
let private_key_pem = plain_private_key.private_key.as_bytes();

let rsa = PKey::private_key_from_pem_passphrase(private_key_pem, secret)?;
let rsa = rsa.rsa()?;
let private_key_pem = rsa
.private_key_to_pem()
.iter()
.flat_map(|buf| std::str::from_utf8(buf))
.collect::<String>();

Ok(PrivateKeyContainer::new(private_key_pem, plain_private_key.version))

}
/// Encrypts a file key used for file encryption using either the public key or a plain keypair
/// container.
/// # Example
Expand Down
9 changes: 5 additions & 4 deletions src/models.rs
Expand Up @@ -306,10 +306,6 @@ pub trait DracoonRSACrypto {
version: UserKeyPairVersion,
) -> Result<PlainUserKeyPairContainer, DracoonCryptoError>;

fn encrypt_private_key_only(
secret: &str,
plain_private_key: PrivateKeyContainer,
) -> Result<PrivateKeyContainer, DracoonCryptoError>;

fn encrypt_private_key(
secret: &str,
Expand All @@ -321,6 +317,11 @@ pub trait DracoonRSACrypto {
keypair: UserKeyPairContainer,
) -> Result<PlainUserKeyPairContainer, DracoonCryptoError>;

fn decrypt_private_key_only(
secret: &str,
plain_private_key: PrivateKeyContainer,
) -> Result<PrivateKeyContainer, DracoonCryptoError>;

fn encrypt_file_key(
plain_file_key: PlainFileKey,
public_key: impl PublicKey,
Expand Down