use crate::crypto::rsa::{PaddingError, PubkeyError};
pub mod der;
pub mod rsa;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Key(#[from] PubkeyError),
#[error(transparent)]
Padding(#[from] PaddingError),
}
pub fn encrypt(pass: &[u8], key: &[u8]) -> Result<Vec<u8>, Error> {
let pub_key = self::rsa::PublicKey::from_pem(key)?;
let pad = self::rsa::Pkcs1OaepPadding::new(self::rsa::GetRandom);
Ok(pub_key.encrypt_block(pass, pad)?)
}
pub fn verify_mariadb_shared_secret(
server_shared_secret: &[u8],
password_hash: &[u8],
scramble: &[u8],
leaf_cert_fingerprint: &[u8],
) -> bool {
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(password_hash);
hasher.update(scramble);
hasher.update(leaf_cert_fingerprint);
let expected = hasher.finalize();
let expected_hex = hex::encode_upper(expected);
expected_hex.as_bytes() == server_shared_secret
}
pub struct MariaDbZeroConfigCheck {
leaf_cert_fingerprint: Option<Vec<u8>>,
}
impl MariaDbZeroConfigCheck {
pub fn new(leaf_cert_fingerprint: Option<Vec<u8>>) -> Self {
Self {
leaf_cert_fingerprint,
}
}
pub fn requires_zeroconfig_fallback(&self) -> bool {
self.leaf_cert_fingerprint.is_some()
}
pub fn leaf_cert_fingerprint(&self) -> &[u8] {
self.leaf_cert_fingerprint
.as_deref()
.expect("leaf_cert_fingerprint must not be requested if standard validation passed")
}
}