Debian Codegraph @

src/password/mod.rs

raw symbols SCIP
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.
8
9 use craterust-analyzer cargo postgres-protocol 0.6.12 crate/::authenticationrust-analyzer cargo postgres-protocol 0.6.12 authentication/::saslrust-analyzer cargo postgres-protocol 0.6.12 authentication/sasl/;
10 use craterust-analyzer cargo postgres-protocol 0.6.12 crate/::hexrust-analyzer cargo postgres-protocol 0.6.12 hex/::LowerHexWrapperrust-analyzer cargo postgres-protocol 0.6.12 hex/LowerHexWrapper#;
11 use base64rust-analyzer cargo base64 0.22.1 crate/::displayrust-analyzer cargo base64 0.22.1 display/::Base64Displayrust-analyzer cargo base64 0.22.1 display/Base64Display#;
12 use base64rust-analyzer cargo base64 0.22.1 crate/::enginerust-analyzer cargo base64 0.22.1 engine/::general_purposerust-analyzer cargo base64 0.22.1 engine/general_purpose/::STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD.;
13 use hmacrust-analyzer cargo hmac 0.13.0 crate/::{Hmacrust-analyzer cargo hmac 0.13.0 Hmac#, KeyInitrust-analyzer cargo crypto-common 0.2.2 KeyInit#, Macrust-analyzer cargo digest 0.11.3 mac/Mac#};
14 use md5rust-analyzer cargo md-5 0.11.0 crate/::Md5rust-analyzer cargo md-5 0.11.0 Md5#;
15 use randrust-analyzer cargo rand 0.10.1 crate/::Rngrust-analyzer cargo rand_core 0.10.1 Rng#;
16 use sha2rust-analyzer cargo sha2 0.11.0 crate/::digestrust-analyzer cargo digest 0.11.3 crate/::FixedOutputrust-analyzer cargo digest 0.11.3 FixedOutput#;
17 use sha2rust-analyzer cargo sha2 0.11.0 crate/::{Digestrust-analyzer cargo digest 0.11.3 digest/Digest#, Sha256rust-analyzer cargo sha2 0.11.0 Sha256#};
18
19 #[cfg(test)]
20 mod testrust-analyzer cargo postgres-protocol 0.6.12 password/test/;
21
22 const SCRAM_DEFAULT_ITERATIONSrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_ITERATIONS.: u32 = 4096;
23 const SCRAM_DEFAULT_SALT_LENrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_SALT_LEN.: usize = 16;
24
25 /// Hash password using SCRAM-SHA-256 with a randomly-generated
26 /// salt.
27 ///
28 /// The client may assume the returned string doesn't contain any
29 /// special characters that would require escaping in an SQL command.
30 pub fn scram_sha_256rust-analyzer cargo postgres-protocol 0.6.12 password/scram_sha_256().(password: &[u8]) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
31 let mut salt: [u8; SCRAM_DEFAULT_SALT_LENrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_SALT_LEN.] = [0; SCRAM_DEFAULT_SALT_LENrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_SALT_LEN.];
32 let mut rng = randrust-analyzer cargo rand 0.10.1 crate/::rngrust-analyzer cargo rand 0.10.1 rngs/thread/rng().();
33 rng.fill_bytesrust-analyzer cargo rand_core 0.10.1 impl#[R][Rng]fill_bytes().(&mut salt);
34 scram_sha_256_saltrust-analyzer cargo postgres-protocol 0.6.12 password/scram_sha_256_salt().(password, salt)
35 }
36
37 // Internal implementation of scram_sha_256 with a caller-provided
38 // salt. This is useful for testing.
39 pub(craterust-analyzer cargo postgres-protocol 0.6.12 crate/) fn scram_sha_256_saltrust-analyzer cargo postgres-protocol 0.6.12 password/scram_sha_256_salt().(password: &[u8], salt: [u8; SCRAM_DEFAULT_SALT_LENrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_SALT_LEN.]) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
40 // Prepare the password, per [RFC
41 // 4013](https://tools.ietf.org/html/rfc4013), if possible.
42 //
43 // Postgres treats passwords as byte strings (without embedded NUL
44 // bytes), but SASL expects passwords to be valid UTF-8.
45 //
46 // Follow the behavior of libpq's PQencryptPasswordConn(), and
47 // also the backend. If the password is not valid UTF-8, or if it
48 // contains prohibited characters (such as non-ASCII whitespace),
49 // just skip the SASLprep step and use the original byte
50 // sequence.
51 let prepared: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> = match stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::from_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/converts/from_utf8().(password) {
52 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(password_str) => {
53 match stringpreprust-analyzer cargo stringprep 0.1.5 crate/::saslpreprust-analyzer cargo stringprep 0.1.5 saslprep().(password_str) {
54 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(p) => p.into_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`]into_owned().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().(),
55 // contains invalid characters; skip saslprep
56 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`][`From<&[T]>`]from().(password),
57 }
58 }
59 // not valid UTF-8; skip saslprep
60 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`][`From<&[T]>`]from().(password),
61 };
62
63 // salt password
64 let salted_password = saslrust-analyzer cargo postgres-protocol 0.6.12 authentication/sasl/::hirust-analyzer cargo postgres-protocol 0.6.12 authentication/sasl/hi().(&prepared, &salt, SCRAM_DEFAULT_ITERATIONSrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_ITERATIONS.);
65
66 // client key
67 let mut hmac = Hmacrust-analyzer cargo hmac 0.13.0 Hmac#::<Sha256rust-analyzer cargo sha2 0.11.0 Sha256#>::new_from_slicerust-analyzer cargo hmac 0.13.0 impl#[`Hmac<D>`][KeyInit]new_from_slice().(&salted_password)
68 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("HMAC is able to accept all key sizes");
69 hmac.updaterust-analyzer cargo digest 0.11.3 mac/impl#[T][Mac]update().(b"Client Key");
70 let client_key = hmac.finalizerust-analyzer cargo digest 0.11.3 mac/impl#[T][Mac]finalize().().into_bytesrust-analyzer cargo digest 0.11.3 mac/impl#[`CtOutput<T>`]into_bytes().();
71
72 // stored key
73 let mut hash = Sha256rust-analyzer cargo sha2 0.11.0 Sha256#::defaultrust-analyzer cargo sha2 0.11.0 impl#[Sha256][Default]default().();
74 hash.updaterust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]update().(client_key);
75 let stored_key = hash.finalize_fixedrust-analyzer cargo digest 0.11.3 FixedOutput#finalize_fixed().();
76
77 // server key
78 let mut hmac = Hmacrust-analyzer cargo hmac 0.13.0 Hmac#::<Sha256rust-analyzer cargo sha2 0.11.0 Sha256#>::new_from_slicerust-analyzer cargo hmac 0.13.0 impl#[`Hmac<D>`][KeyInit]new_from_slice().(&salted_password)
79 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("HMAC is able to accept all key sizes");
80 hmac.updaterust-analyzer cargo digest 0.11.3 mac/impl#[T][Mac]update().(b"Server Key");
81 let server_key = hmac.finalizerust-analyzer cargo digest 0.11.3 mac/impl#[T][Mac]finalize().().into_bytesrust-analyzer cargo digest 0.11.3 mac/impl#[`CtOutput<T>`]into_bytes().();
82
83 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
84 "SCRAM-SHA-256${}:{}${}:{}",
85 SCRAM_DEFAULT_ITERATIONSrust-analyzer cargo postgres-protocol 0.6.12 password/SCRAM_DEFAULT_ITERATIONS.,
86 Base64Displayrust-analyzer cargo base64 0.22.1 display/Base64Display#::newrust-analyzer cargo base64 0.22.1 display/impl#[`Base64Display<'a, 'e, E>`]new().(&salt, &STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD.),
87 Base64Displayrust-analyzer cargo base64 0.22.1 display/Base64Display#::newrust-analyzer cargo base64 0.22.1 display/impl#[`Base64Display<'a, 'e, E>`]new().(&stored_key, &STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD.),
88 Base64Displayrust-analyzer cargo base64 0.22.1 display/Base64Display#::newrust-analyzer cargo base64 0.22.1 display/impl#[`Base64Display<'a, 'e, E>`]new().(&server_key, &STANDARDrust-analyzer cargo base64 0.22.1 engine/general_purpose/STANDARD.)
89 )
90 }
91
92 /// **Not recommended, as MD5 is not considered to be secure.**
93 ///
94 /// Hash password using MD5 with the username as the salt.
95 ///
96 /// The client may assume the returned string doesn't contain any
97 /// special characters that would require escaping.
98 pub fn md5rust-analyzer cargo postgres-protocol 0.6.12 password/md5().(password: &[u8], username: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
99 // salt password with username
100 let mut salted_password = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`][`From<&[T]>`]from().(password);
101 salted_password.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(username.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
102
103 let mut hash = Md5rust-analyzer cargo md-5 0.11.0 Md5#::newrust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]new().();
104 hash.updaterust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]update().(&salted_password);
105 let digest = LowerHexWrapperrust-analyzer cargo postgres-protocol 0.6.12 hex/LowerHexWrapper#(hash.finalizerust-analyzer cargo digest 0.11.3 digest/impl#[D][Digest]finalize().());
106 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("md5{digest:x}")
107 }