Update rsa crate to 0.5 (#1572)

* Update rsa crate to 0.5

* Don't do pem decoding ourselves
This commit is contained in:
Paolo Barbolini
2021-12-21 02:58:37 +01:00
committed by GitHub
parent 2e6ab7c3de
commit ce572bcee4
3 changed files with 81 additions and 51 deletions

View File

@@ -3,7 +3,7 @@ use bytes::Bytes;
use digest::{Digest, FixedOutput};
use generic_array::GenericArray;
use rand::thread_rng;
use rsa::{PaddingScheme, PublicKey, RSAPublicKey};
use rsa::{pkcs8::FromPublicKey, PaddingScheme, PublicKey, RsaPublicKey};
use sha1::Sha1;
use sha2::Sha256;
@@ -180,22 +180,12 @@ fn to_asciz(s: &str) -> Vec<u8> {
}
// https://docs.rs/rsa/0.3.0/rsa/struct.RSAPublicKey.html?search=#example-1
fn parse_rsa_pub_key(key: &[u8]) -> Result<RSAPublicKey, Error> {
let key = std::str::from_utf8(key).map_err(Error::protocol)?;
fn parse_rsa_pub_key(key: &[u8]) -> Result<RsaPublicKey, Error> {
let pem = std::str::from_utf8(key).map_err(Error::protocol)?;
// This takes advantage of the knowledge that we know
// we are receiving a PKCS#8 RSA Public Key at all
// times from MySQL
let encoded =
key.lines()
.filter(|line| !line.starts_with("-"))
.fold(String::new(), |mut data, line| {
data.push_str(&line);
data
});
let der = base64::decode(&encoded).map_err(Error::protocol)?;
RSAPublicKey::from_pkcs8(&der).map_err(Error::protocol)
RsaPublicKey::from_public_key_pem(&pem).map_err(Error::protocol)
}