Upgrade rsa to v0.9.6 (#15055)

This PR upgrades the `rsa` crate to v0.9.6.

The version we were using was rather old, and for something
security-sensitive we should be using a recent version.

No behavioral changes have been made, just updates to account for
changes in the crate's API.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-23 20:11:48 -04:00 committed by GitHub
parent 1307a80e07
commit edf7f6defe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 82 deletions

View file

@ -27,7 +27,7 @@ gpui = { workspace = true, optional = true }
parking_lot.workspace = true
proto.workspace = true
rand.workspace = true
rsa = "0.4"
rsa.workspace = true
serde.workspace = true
serde_json.workspace = true
strum.workspace = true

View file

@ -1,18 +1,19 @@
use anyhow::{Context, Result};
use rand::{thread_rng, Rng as _};
use rsa::{PublicKey as _, PublicKeyEncoding, RSAPrivateKey, RSAPublicKey};
use rsa::pkcs1::{DecodeRsaPublicKey, EncodeRsaPublicKey};
use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
use std::convert::TryFrom;
pub struct PublicKey(RSAPublicKey);
pub struct PublicKey(RsaPublicKey);
pub struct PrivateKey(RSAPrivateKey);
pub struct PrivateKey(RsaPrivateKey);
/// Generate a public and private key for asymmetric encryption.
pub fn keypair() -> Result<(PublicKey, PrivateKey)> {
let mut rng = thread_rng();
let bits = 1024;
let private_key = RSAPrivateKey::new(&mut rng, bits)?;
let public_key = RSAPublicKey::from(&private_key);
let private_key = RsaPrivateKey::new(&mut rng, bits)?;
let public_key = RsaPublicKey::from(&private_key);
Ok((PublicKey(public_key), PrivateKey(private_key)))
}
@ -58,7 +59,10 @@ impl PrivateKey {
impl TryFrom<PublicKey> for String {
type Error = anyhow::Error;
fn try_from(key: PublicKey) -> Result<Self> {
let bytes = key.0.to_pkcs1().context("failed to serialize public key")?;
let bytes = key
.0
.to_pkcs1_der()
.context("failed to serialize public key")?;
let string = base64::encode_config(&bytes, base64::URL_SAFE);
Ok(string)
}
@ -69,12 +73,12 @@ impl TryFrom<String> for PublicKey {
fn try_from(value: String) -> Result<Self> {
let bytes = base64::decode_config(&value, base64::URL_SAFE)
.context("failed to base64-decode public key string")?;
let key = Self(RSAPublicKey::from_pkcs1(&bytes).context("failed to parse public key")?);
let key = Self(RsaPublicKey::from_pkcs1_der(&bytes).context("failed to parse public key")?);
Ok(key)
}
}
const PADDING_SCHEME: rsa::PaddingScheme = rsa::PaddingScheme::PKCS1v15Encrypt;
const PADDING_SCHEME: Pkcs1v15Encrypt = Pkcs1v15Encrypt;
#[cfg(test)]
mod tests {