Skip to content

Commit

Permalink
Update rsa crate to 0.5 (#1572)
Browse files Browse the repository at this point in the history
* Update rsa crate to 0.5

* Don't do pem decoding ourselves
  • Loading branch information
paolobarbolini authored Dec 21, 2021
1 parent 2e6ab7c commit ce572bc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 51 deletions.
111 changes: 76 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ mysql = [
"sha2",
"generic-array",
"num-bigint",
"base64",
"digest",
"rand",
"rsa",
Expand Down Expand Up @@ -144,7 +143,7 @@ percent-encoding = "2.1.0"
parking_lot = "0.11.0"
rand = { version = "0.8.3", default-features = false, optional = true, features = ["std", "std_rng"] }
regex = { version = "1.3.9", optional = true }
rsa = { version = "0.4.0", optional = true }
rsa = { version = "0.5.0", optional = true }
rustls = { version = "0.19.0", features = ["dangerous_configuration"], optional = true }
serde = { version = "1.0.106", features = ["derive", "rc"], optional = true }
serde_json = { version = "1.0.51", features = ["raw_value"], optional = true }
Expand Down
18 changes: 4 additions & 14 deletions sqlx-core/src/mysql/connection/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
}

0 comments on commit ce572bc

Please sign in to comment.