1//! Functions to encrypt a password in the client.
2//!
3//! This is intended to be used by client applications that wish to
4//! send commands like `ALTER USER joe PASSWORD 'pwd'`. The password
5//! need not be sent in cleartext if it is encrypted on the client
6//! side. This is good because it ensures the cleartext password won't
7//! end up in logs pg_stat displays, etc.
89use crate::authentication::sasl;
10use base64::display::Base64Display;
11use base64::engine::general_purpose::STANDARD;
12use hmac::{Hmac, Mac};
13use md5::Md5;
14use rand::RngCore;
15use sha2::digest::FixedOutput;
16use sha2::{Digest, Sha256};
1718#[cfg(test)]
19mod test;
2021const SCRAM_DEFAULT_ITERATIONS: u32 = 4096;
22const SCRAM_DEFAULT_SALT_LEN: usize = 16;
2324/// Hash password using SCRAM-SHA-256 with a randomly-generated
25/// salt.
26///
27/// The client may assume the returned string doesn't contain any
28/// special characters that would require escaping in an SQL command.
29pub fn scram_sha_256(password: &[u8]) -> String {
30let mut salt: [u8; SCRAM_DEFAULT_SALT_LEN] = [0; SCRAM_DEFAULT_SALT_LEN];
31let mut rng = rand::thread_rng();
32 rng.fill_bytes(&mut salt);
33 scram_sha_256_salt(password, salt)
34}
3536// Internal implementation of scram_sha_256 with a caller-provided
37// salt. This is useful for testing.
38pub(crate) fn scram_sha_256_salt(password: &[u8], salt: [u8; SCRAM_DEFAULT_SALT_LEN]) -> String {
39// Prepare the password, per [RFC
40 // 4013](https://tools.ietf.org/html/rfc4013), if possible.
41 //
42 // Postgres treats passwords as byte strings (without embedded NUL
43 // bytes), but SASL expects passwords to be valid UTF-8.
44 //
45 // Follow the behavior of libpq's PQencryptPasswordConn(), and
46 // also the backend. If the password is not valid UTF-8, or if it
47 // contains prohibited characters (such as non-ASCII whitespace),
48 // just skip the SASLprep step and use the original byte
49 // sequence.
50let prepared: Vec<u8> = match std::str::from_utf8(password) {
51Ok(password_str) => {
52match stringprep::saslprep(password_str) {
53Ok(p) => p.into_owned().into_bytes(),
54// contains invalid characters; skip saslprep
55Err(_) => Vec::from(password),
56 }
57 }
58// not valid UTF-8; skip saslprep
59Err(_) => Vec::from(password),
60 };
6162// salt password
63let salted_password = sasl::hi(&prepared, &salt, SCRAM_DEFAULT_ITERATIONS);
6465// client key
66let mut hmac = Hmac::<Sha256>::new_from_slice(&salted_password)
67 .expect("HMAC is able to accept all key sizes");
68 hmac.update(b"Client Key");
69let client_key = hmac.finalize().into_bytes();
7071// stored key
72let mut hash = Sha256::default();
73 hash.update(client_key.as_slice());
74let stored_key = hash.finalize_fixed();
7576// server key
77let mut hmac = Hmac::<Sha256>::new_from_slice(&salted_password)
78 .expect("HMAC is able to accept all key sizes");
79 hmac.update(b"Server Key");
80let server_key = hmac.finalize().into_bytes();
8182format!(
83"SCRAM-SHA-256${}:{}${}:{}",
84 SCRAM_DEFAULT_ITERATIONS,
85 Base64Display::new(&salt, &STANDARD),
86 Base64Display::new(&stored_key, &STANDARD),
87 Base64Display::new(&server_key, &STANDARD)
88 )
89}
9091/// **Not recommended, as MD5 is not considered to be secure.**
92///
93/// Hash password using MD5 with the username as the salt.
94///
95/// The client may assume the returned string doesn't contain any
96/// special characters that would require escaping.
97pub fn md5(password: &[u8], username: &str) -> String {
98// salt password with username
99let mut salted_password = Vec::from(password);
100 salted_password.extend_from_slice(username.as_bytes());
101102let mut hash = Md5::new();
103 hash.update(&salted_password);
104let digest = hash.finalize();
105format!("md5{:x}", digest)
106}