Skip to content

Commit

Permalink
Move pub priv key functions to rsa module
Browse files Browse the repository at this point in the history
  • Loading branch information
eliykat committed Oct 26, 2023
1 parent feaeb68 commit e767bdd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/bitwarden/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub use self::rsa::RsaKeyPair;
#[cfg(feature = "internal")]
pub use self::rsa::encrypt_rsa;
#[cfg(feature = "internal")]
pub use self::rsa::public_key_from_b64;
#[cfg(feature = "internal")]
pub use self::rsa::private_key_from_bytes;
#[cfg(feature = "internal")]

#[cfg(feature = "internal")]
mod fingerprint;
Expand Down
12 changes: 11 additions & 1 deletion crates/bitwarden/src/crypto/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use base64::Engine;
use rsa::{
pkcs8::{EncodePrivateKey, EncodePublicKey},
pkcs8::{der::Decode, EncodePrivateKey, EncodePublicKey, SubjectPublicKeyInfo, DecodePrivateKey},
RsaPrivateKey, RsaPublicKey,
Oaep
};
Expand Down Expand Up @@ -54,6 +54,16 @@ pub fn encrypt_rsa(data: Vec<u8>, key: &RsaPublicKey) -> Result<Vec<u8>> {
.map_err(|_| CryptoError::InvalidKey.into()) // need better error
}

pub fn public_key_from_b64(b64: &str) -> Result<RsaPublicKey> {
let public_key_bytes = BASE64_ENGINE.decode(b64)?;
let public_key_info = SubjectPublicKeyInfo::from_der(&public_key_bytes).unwrap(); // TODO: error handling
RsaPublicKey::try_from(public_key_info).map_err(|_| Error::Crypto(CryptoError::InvalidKey))
}

pub fn private_key_from_bytes(bytes: &Vec<u8>) -> Result<RsaPrivateKey> {
rsa::RsaPrivateKey::from_pkcs8_der(bytes).map_err(|_| Error::Crypto(CryptoError::InvalidKey))
}

#[cfg(test)]
mod tests {
use base64::Engine;
Expand Down

0 comments on commit e767bdd

Please sign in to comment.