use base64::Engine;
use base64::engine::general_purpose::STANDARD as B64;
use futures_util::{SinkExt, StreamExt};
use rand::Rng;
use sha2::Sha256;
use subtle::ConstantTimeEq;
use tokio::net::TcpStream;
use tokio_util::codec::Framed;
use crate::errors;
use crate::proto::backend::BackendMessage;
use crate::proto::codec::BackendCodec;
use crate::proto::frontend::FrontendMessage;
use super::crypto::{extract_attr, hmac_sha256, sha256};
pub struct ScramClient {
password: String,
nonce: String,
client_first_bare: String,
server_first: String,
salted_password: [u8; 32],
}
impl ScramClient {
pub fn new(password: &str) -> Self {
let mut nonce_bytes = [0u8; 18];
rand::rng().fill(&mut nonce_bytes);
let nonce = B64.encode(nonce_bytes);
let client_first_bare = format!("n=,r={nonce}");
Self {
password: password.to_owned(),
nonce,
client_first_bare,
server_first: String::new(),
salted_password: [0u8; 32],
}
}
pub fn initial_response(&self) -> Vec<u8> {
let mechanism = b"SCRAM-SHA-256\0";
let client_first = format!("n,,{}", self.client_first_bare);
let data = client_first.as_bytes();
let mut buf = Vec::with_capacity(mechanism.len() + 4 + data.len());
buf.extend_from_slice(mechanism);
buf.extend_from_slice(&(data.len() as i32).to_be_bytes());
buf.extend_from_slice(data);
buf
}
pub async fn handle_server_first(&mut self, data: &[u8]) -> Result<Vec<u8>, errors::AuthError> {
let msg = std::str::from_utf8(data)
.map_err(|_| errors::AuthError::Protocol("server-first-message is not UTF-8".into()))?;
msg.clone_into(&mut self.server_first);
let combined_nonce = extract_attr(msg, 'r')?;
if !combined_nonce.starts_with(&self.nonce) {
return Err(errors::AuthError::Protocol(
"server nonce does not start with client nonce".into(),
));
}
let salt_b64 = extract_attr(msg, 's')?;
let salt = B64
.decode(salt_b64)
.map_err(|_| errors::AuthError::Protocol("invalid base64 in salt".into()))?;
let iterations: u32 = extract_attr(msg, 'i')?
.parse()
.map_err(|_| errors::AuthError::Protocol("invalid iteration count".into()))?;
let password = self.password.clone();
self.salted_password = tokio::task::spawn_blocking(move || {
let mut out = [0u8; 32];
pbkdf2::pbkdf2_hmac::<Sha256>(password.as_bytes(), &salt, iterations, &mut out);
out
})
.await
.map_err(|e| errors::AuthError::Protocol(format!("pbkdf2 task failed: {e}")))?;
let client_key = hmac_sha256(&self.salted_password, b"Client Key");
let stored_key = sha256(&client_key);
let client_final_without_proof = format!("c=biws,r={combined_nonce}");
let auth_message = format!(
"{},{},{}",
self.client_first_bare, self.server_first, client_final_without_proof,
);
let client_signature = hmac_sha256(&stored_key, auth_message.as_bytes());
let mut client_proof = [0u8; 32];
for i in 0..32 {
client_proof[i] = client_key[i] ^ client_signature[i];
}
let client_final = format!(
"{},p={}",
client_final_without_proof,
B64.encode(client_proof),
);
Ok(client_final.as_bytes().to_vec())
}
pub fn handle_server_final(&self, data: &[u8]) -> Result<(), errors::AuthError> {
let msg = std::str::from_utf8(data)
.map_err(|_| errors::AuthError::Protocol("server-final-message is not UTF-8".into()))?;
if msg.starts_with("e=") {
return Err(errors::AuthError::Protocol(format!(
"SCRAM server error: {msg}"
)));
}
let verifier_b64 = extract_attr(msg, 'v')?;
let server_signature = B64.decode(verifier_b64).map_err(|_| {
errors::AuthError::Protocol("invalid base64 in server signature".into())
})?;
let server_key = hmac_sha256(&self.salted_password, b"Server Key");
let client_final_without_proof = {
let combined_nonce = extract_attr(&self.server_first, 'r').map_err(|_| {
errors::AuthError::Protocol("missing nonce in cached server-first".into())
})?;
format!("c=biws,r={combined_nonce}")
};
let auth_message = format!(
"{},{},{}",
self.client_first_bare, self.server_first, client_final_without_proof,
);
let expected = hmac_sha256(&server_key, auth_message.as_bytes());
if server_signature.len() != 32 || !bool::from(server_signature.ct_eq(&expected[..])) {
return Err(errors::AuthError::Protocol(
"server signature verification failed".into(),
));
}
Ok(())
}
}
pub async fn authenticate(
framed: &mut Framed<TcpStream, BackendCodec>,
password: &str,
) -> Result<(), errors::AuthError> {
let mut client = ScramClient::new(password);
framed
.send(FrontendMessage::PasswordMessage(client.initial_response()))
.await?;
let server_first = match framed.next().await.transpose()? {
Some(BackendMessage::AuthenticationSaslContinue { data }) => data,
Some(BackendMessage::ErrorResponse(err)) => {
return Err(errors::AuthError::VerifierFetch(format!(
"SCRAM auth error: {}",
err.message().unwrap_or("unknown")
)));
}
other => {
return Err(errors::AuthError::Protocol(format!(
"expected AuthenticationSASLContinue, got {other:?}"
)));
}
};
let client_final = client.handle_server_first(&server_first).await?;
framed
.send(FrontendMessage::PasswordMessage(client_final))
.await?;
match framed.next().await.transpose()? {
Some(BackendMessage::AuthenticationSaslFinal { data }) => {
client.handle_server_final(&data)?;
}
Some(BackendMessage::ErrorResponse(err)) => {
return Err(errors::AuthError::VerifierFetch(format!(
"SCRAM auth failed: {}",
err.message().unwrap_or("unknown")
)));
}
other => {
return Err(errors::AuthError::Protocol(format!(
"expected AuthenticationSASLFinal, got {other:?}"
)));
}
}
match framed.next().await.transpose()? {
Some(BackendMessage::AuthenticationOk) => Ok(()),
Some(BackendMessage::ErrorResponse(err)) => Err(errors::AuthError::VerifierFetch(format!(
"auth failed after SCRAM: {}",
err.message().unwrap_or("unknown")
))),
other => Err(errors::AuthError::Protocol(format!(
"expected AuthenticationOk, got {other:?}"
))),
}
}