Line data Source code
1 : //! Salted Challenge Response Authentication Mechanism.
2 : //!
3 : //! RFC: <https://datatracker.ietf.org/doc/html/rfc5802>.
4 : //!
5 : //! Reference implementation:
6 : //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/backend/libpq/auth-scram.c>
7 : //! * <https://github.com/postgres/postgres/blob/94226d4506e66d6e7cbf4b391f1e7393c1962841/src/interfaces/libpq/fe-auth-scram.c>
8 :
9 : mod exchange;
10 : mod key;
11 : mod messages;
12 : mod secret;
13 : mod signature;
14 :
15 : #[cfg(any(test, doc))]
16 : mod password;
17 :
18 : pub use exchange::{exchange, Exchange};
19 : pub use key::ScramKey;
20 : pub use secret::ServerSecret;
21 :
22 : use hmac::{Hmac, Mac};
23 : use sha2::{Digest, Sha256};
24 :
25 : const SCRAM_SHA_256: &str = "SCRAM-SHA-256";
26 : const SCRAM_SHA_256_PLUS: &str = "SCRAM-SHA-256-PLUS";
27 :
28 : /// A list of supported SCRAM methods.
29 : pub const METHODS: &[&str] = &[SCRAM_SHA_256_PLUS, SCRAM_SHA_256];
30 : pub const METHODS_WITHOUT_PLUS: &[&str] = &[SCRAM_SHA_256];
31 :
32 : /// Decode base64 into array without any heap allocations
33 462 : fn base64_decode_array<const N: usize>(input: impl AsRef<[u8]>) -> Option<[u8; N]> {
34 462 : let mut bytes = [0u8; N];
35 :
36 462 : let size = base64::decode_config_slice(input, base64::STANDARD, &mut bytes).ok()?;
37 462 : if size != N {
38 0 : return None;
39 462 : }
40 462 :
41 462 : Some(bytes)
42 462 : }
43 :
44 : /// This function essentially is `Hmac(sha256, key, input)`.
45 : /// Further reading: <https://datatracker.ietf.org/doc/html/rfc2104>.
46 8438 : fn hmac_sha256<'a>(key: &[u8], parts: impl IntoIterator<Item = &'a [u8]>) -> [u8; 32] {
47 8438 : let mut mac = Hmac::<Sha256>::new_from_slice(key).expect("bad key size");
48 9216 : parts.into_iter().for_each(|s| mac.update(s));
49 8438 :
50 8438 : mac.finalize().into_bytes().into()
51 8438 : }
52 :
53 141 : fn sha256<'a>(parts: impl IntoIterator<Item = &'a [u8]>) -> [u8; 32] {
54 141 : let mut hasher = Sha256::new();
55 156 : parts.into_iter().for_each(|s| hasher.update(s));
56 141 :
57 141 : hasher.finalize().into()
58 141 : }
59 :
60 : #[cfg(test)]
61 : mod tests {
62 : use crate::sasl::{Mechanism, Step};
63 :
64 : use super::{password::SaltedPassword, Exchange, ServerSecret};
65 :
66 2 : #[test]
67 2 : fn happy_path() {
68 2 : let iterations = 4096;
69 2 : let salt_base64 = "QSXCR+Q6sek8bf92";
70 2 : let pw = SaltedPassword::new(
71 2 : b"pencil",
72 2 : base64::decode(salt_base64).unwrap().as_slice(),
73 2 : iterations,
74 2 : );
75 2 :
76 2 : let secret = ServerSecret {
77 2 : iterations,
78 2 : salt_base64: salt_base64.to_owned(),
79 2 : stored_key: pw.client_key().sha256(),
80 2 : server_key: pw.server_key(),
81 2 : doomed: false,
82 2 : };
83 2 : const NONCE: [u8; 18] = [
84 2 : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
85 2 : ];
86 2 : let mut exchange = Exchange::new(
87 2 : &secret,
88 2 : || NONCE,
89 2 : crate::config::TlsServerEndPoint::Undefined,
90 2 : );
91 2 :
92 2 : let client_first = "n,,n=user,r=rOprNGfwEbeRWgbNEkqO";
93 2 : let client_final = "c=biws,r=rOprNGfwEbeRWgbNEkqOAQIDBAUGBwgJCgsMDQ4PEBES,p=rw1r5Kph5ThxmaUBC2GAQ6MfXbPnNkFiTIvdb/Rear0=";
94 2 : let server_first =
95 2 : "r=rOprNGfwEbeRWgbNEkqOAQIDBAUGBwgJCgsMDQ4PEBES,s=QSXCR+Q6sek8bf92,i=4096";
96 2 : let server_final = "v=qtUDIofVnIhM7tKn93EQUUt5vgMOldcDVu1HC+OH0o0=";
97 :
98 2 : exchange = match exchange.exchange(client_first).unwrap() {
99 2 : Step::Continue(exchange, message) => {
100 2 : assert_eq!(message, server_first);
101 2 : exchange
102 : }
103 0 : Step::Success(_, _) => panic!("expected continue, got success"),
104 0 : Step::Failure(f) => panic!("{f}"),
105 : };
106 :
107 2 : let key = match exchange.exchange(client_final).unwrap() {
108 2 : Step::Success(key, message) => {
109 2 : assert_eq!(message, server_final);
110 2 : key
111 : }
112 0 : Step::Continue(_, _) => panic!("expected success, got continue"),
113 0 : Step::Failure(f) => panic!("{f}"),
114 : };
115 :
116 2 : assert_eq!(
117 2 : key.as_bytes(),
118 2 : [
119 2 : 74, 103, 1, 132, 12, 31, 200, 48, 28, 54, 82, 232, 207, 12, 138, 189, 40, 32, 134,
120 2 : 27, 125, 170, 232, 35, 171, 167, 166, 41, 70, 228, 182, 112,
121 2 : ]
122 2 : );
123 2 : }
124 : }
|