cartel-pg 0.24.0

Async PostgreSQL driver for the dope runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use crate::protocol::{Length, Offset, Reason, Wire, encode};
use crate::scram::Base64;
use base64::Engine as _;
use base64::engine::general_purpose;
use dope::core::{Backend, dope_sys::thread::Thread as _, driver::route};
use dope::manifold::client::conn;
use o3::buffer::bytes::bounded;
use sha2::block_api::compress256;
use std::mem;
use subtle::ConstantTimeEq as _;
use zeroize::Zeroizing;

pub(in crate::protocol) mod kdf;
mod parser;
mod secret32;

pub(crate) use kdf::Runtime;
use secret32::Secret32;

const MECH: &str = "SCRAM-SHA-256";
const PLUS: &str = "SCRAM-SHA-256-PLUS";
const CLIENT_NONCE_BYTES: usize = 24;
const PROOF_BYTES: usize = 44;

const _: () = {
    assert!(matches!(
        base64::encoded_len(18, false),
        Some(CLIENT_NONCE_BYTES)
    ));
    assert!(matches!(base64::encoded_len(32, true), Some(PROOF_BYTES)));
    assert!(matches!(base64::encoded_len(32, false), Some(len) if len == PROOF_BYTES - 1));
};

#[derive(Default)]
#[repr(u8)]
pub(super) enum Mechanisms {
    #[default]
    None,
    Plus,
    Sha256,
}

impl Mechanisms {
    pub(super) fn offer(&mut self, name: &str) {
        match name {
            MECH => *self = Self::Sha256,
            PLUS if matches!(self, Self::None) => *self = Self::Plus,
            PLUS => {}
            _ => {}
        }
    }

    pub(super) fn pick(self) -> Result<Mechanism, crate::Error> {
        match self {
            Self::Sha256 => Ok(Mechanism::Sha256),
            Self::Plus => Err(crate::Error::Auth(Reason::SaslPlus)),
            Self::None => Err(crate::Error::Auth(Reason::NoSasl)),
        }
    }
}

const _: () = {
    assert!(std::mem::size_of::<Mechanisms>() == std::mem::size_of::<bool>());
    assert!(std::mem::align_of::<Mechanisms>() == std::mem::align_of::<bool>());
};

pub(super) enum Mechanism {
    Sha256,
}

impl Mechanism {
    pub(super) const fn as_str(&self) -> &str {
        match self {
            Self::Sha256 => MECH,
        }
    }
}

struct ClientNonce([u8; CLIENT_NONCE_BYTES]);
pub(super) struct Signature(Secret32);

pub(in crate::protocol) struct Keys {
    client: Secret32,
    stored: Secret32,
    server: Secret32,
}

impl Keys {
    fn write(&mut self, inner: &[u32; 8], outer: &[u32; 8]) {
        let mut hash = Sha256::new(64);
        hash.state.copy_from_slice(inner);
        hash.update(b"Client Key");
        hash.hmac(outer, &mut self.client);
        hash.state.copy_from_slice(inner);
        *hash.len = 64;
        hash.update(b"Server Key");
        hash.hmac(outer, &mut self.server);
        hash.state.copy_from_slice(&IV);
        *hash.len = 0;
        hash.update(self.client.as_array());
        hash.finalize();
        write_state(&hash.state, self.stored.as_array_mut());
    }
}

struct Deriving {
    server_first: parser::Retained,
    key: kdf::KdfKey,
}

enum State {
    AwaitServerFirst,
    Deriving(Deriving),
    AwaitServerFinal(Signature),
    Complete,
}

pub(super) struct Scram<'a, 'd, const ID: u8> {
    pub(super) ticket: kdf::Ticket<'a, 'd, ID>,
    nonce: ClientNonce,
    state: State,
}

pub(super) struct ClientFirst<'a>(&'a [u8; CLIENT_NONCE_BYTES]);

pub(super) struct Derive<'a> {
    nonce: &'a ClientNonce,
    state: &'a Deriving,
}

impl ClientFirst<'_> {
    pub(super) fn write_to(&self, out: &mut encode::Writer) -> Result<(), encode::Error> {
        out.extend(b"n,,n=,r=")?;
        out.extend(self.0)
    }
}

impl<'a, 'd, const ID: u8> Scram<'a, 'd, ID> {
    pub(super) fn new(
        runtime: &'a Runtime<'d, ID>,
        owner: conn::Id<'d, route::KeyTag<ID>>,
    ) -> Result<Self, crate::Error> {
        let ticket = runtime.try_entry(owner).map_err(crate::Error::ScramSlot)?;
        let mut bytes = mem::MaybeUninit::uninit();
        let raw = Backend::entropy::<18>(&mut bytes).map_err(crate::Error::Random)?;
        Ok(Self {
            ticket,
            nonce: ClientNonce(encode_nonce(raw)),
            state: State::AwaitServerFirst,
        })
    }

    pub(super) fn client_first(&self) -> ClientFirst<'_> {
        ClientFirst(&self.nonce.0)
    }

    pub(super) fn begin(&mut self, password: &[u8], bytes: Wire<'_>) -> Result<(), crate::Error> {
        let State::AwaitServerFirst = &self.state else {
            return Err(crate::Error::Protocol(Reason::DuplicateServerFirst));
        };
        let parsed = parser::Parser::new(bytes)
            .server_first()
            .map_err(crate::Error::ServerFirst)?;
        parsed.with_parts(|parsed, nonce, salt, count| {
            if nonce.len() <= self.nonce.0.len() {
                return Err(crate::Error::ServerFirst(Reason::NonceLength {
                    actual: nonce.length(),
                    minimum: Length::from_u32(CLIENT_NONCE_BYTES as u32 + 1),
                }));
            }
            if let Some((offset, (actual, expected))) = (0u32..)
                .zip(
                    nonce
                        .as_slice()
                        .iter()
                        .copied()
                        .zip(self.nonce.0.iter().copied()),
                )
                .find(|item| item.1.0 != item.1.1)
            {
                return Err(crate::Error::ServerFirst(Reason::NonceByte {
                    offset: Offset::from_u32(offset),
                    actual,
                    expected,
                }));
            }

            let mut first = Sha256::new(64);
            let mut outer = Zeroizing::new(IV);
            hmac(password, &mut first.state, &mut outer);
            decode_base64(salt, |decoded| first.update(decoded))?;
            let iterations = kdf::try_iterations(count)?;
            self.state = State::Deriving(Deriving {
                server_first: parsed.try_retain()?,
                key: kdf::KdfKey {
                    first: Secret32::new([0; 32]),
                    iterations,
                },
            });
            let State::Deriving(state) = &mut self.state else {
                unreachable!("scram")
            };
            first.update(&1u32.to_be_bytes());
            first.hmac(&outer, &mut state.key.first);
            Ok(())
        })
    }

    pub(super) fn deriving(&self) -> Option<Derive<'_>> {
        match &self.state {
            State::Deriving(state) => Some(Derive {
                nonce: &self.nonce,
                state,
            }),
            State::AwaitServerFirst | State::AwaitServerFinal(_) | State::Complete => None,
        }
    }

    pub(super) fn await_final(&mut self, sig: &Signature) {
        self.state = State::AwaitServerFinal(Signature(sig.0.clone()));
    }

    pub(super) fn verify_final(&mut self, bytes: Wire<'_>) -> Result<(), crate::Error> {
        let State::AwaitServerFinal(Signature(expected)) = &self.state else {
            return Err(crate::Error::Protocol(Reason::FinalState));
        };
        let encoded = match parser::Parser::new(bytes)
            .server_final()
            .map_err(crate::Error::ServerFinal)?
        {
            parser::ServerFinal::Error(error) => {
                return Err(crate::Error::Scram(error));
            }
            parser::ServerFinal::Verifier(encoded) => encoded,
        };
        let mut actual = Zeroizing::new([0u8; 33]);
        let len = general_purpose::STANDARD
            .decode_slice(encoded.as_slice(), &mut actual[..])
            .map_err(|error| crate::Error::ServerFinalBase64(error.into()))?;
        if len != expected.as_array().len() {
            // SAFETY: successful slice decoding returns an initialized prefix
            // of the fixed 33-byte output, which also retains the u32 bound.
            let bytes =
                Wire::from_raw(unsafe { bounded::raw::Input::new(actual.get_unchecked(..len)) });
            return Err(crate::Error::ServerFinal(Reason::SignatureLength {
                actual: bytes.length(),
                expected: Length::from_u32(32),
            }));
        }
        if !bool::from(expected.as_array().ct_eq(&actual[..32])) {
            return Err(crate::Error::ServerFinal(Reason::SignatureMismatch));
        }
        self.state = State::Complete;
        Ok(())
    }

    pub(super) fn finish(&self) -> Result<(), crate::Error> {
        if !matches!(self.state, State::Complete) {
            return Err(crate::Error::Protocol(Reason::AuthBeforeFinal));
        }
        Ok(())
    }
}

impl Derive<'_> {
    pub(super) const fn key(&self) -> &kdf::KdfKey {
        &self.state.key
    }

    pub(super) fn write_final(
        &self,
        keys: &Keys,
        out: &mut encode::Writer,
    ) -> Result<Signature, encode::Error> {
        let first = &self.state.server_first;
        let nonce = first.nonce();
        let mut proof = Secret32::new([0; 32]);
        hmac_auth_message(
            keys.stored.as_array(),
            &self.nonce.0,
            first.as_slice(),
            nonce,
            &mut proof,
        );
        for (left, right) in std::iter::zip(proof.as_array_mut(), keys.client.as_array()) {
            *left ^= *right;
        }
        let mut encoded = Zeroizing::new([b'='; PROOF_BYTES]);
        general_purpose::STANDARD
            .internal_encode(proof.as_array(), &mut encoded[..PROOF_BYTES - 1]);
        let mut expected = Secret32::new([0; 32]);
        hmac_auth_message(
            keys.server.as_array(),
            &self.nonce.0,
            first.as_slice(),
            nonce,
            &mut expected,
        );

        out.extend(b"c=biws,r=")?;
        out.extend(nonce)?;
        out.extend(b",p=")?;
        out.extend(encoded.as_slice())?;
        Ok(Signature(expected.clone()))
    }
}

fn decode_base64(encoded: Wire<'_>, mut write: impl FnMut(&[u8])) -> Result<(), crate::Error> {
    let mut chunks = (0u32..).zip(encoded.as_slice().chunks(4)).peekable();
    while let Some((index, chunk)) = chunks.next() {
        if chunks.peek().is_some()
            && let Some((position, _)) = (0u32..).zip(chunk).find(|(_, byte)| **byte == b'=')
        {
            let offset = index * 4 + position;
            return Err(crate::Error::ServerFirst(Reason::SaltPadding {
                offset: Offset::from_u32(offset),
            }));
        }
        let mut decoded = [0u8; 3];
        let len = general_purpose::STANDARD
            .decode_slice(chunk, &mut decoded)
            .map_err(|error| {
                let mut error = Base64::from(error);
                let offset = match &mut error {
                    Base64::InvalidByte { index, .. }
                    | Base64::InvalidLength(index)
                    | Base64::InvalidLastSymbol { offset: index, .. } => index,
                    Base64::InvalidPadding | Base64::OutputSliceTooSmall => {
                        return crate::Error::ServerFirstBase64(error);
                    }
                };
                *offset += (index * 4) as usize;
                crate::Error::ServerFirstBase64(error)
            })?;
        write(&decoded[..len]);
    }
    Ok(())
}

fn encode_nonce(input: &[u8; 18]) -> [u8; CLIENT_NONCE_BYTES] {
    let mut output = [0u8; CLIENT_NONCE_BYTES];
    general_purpose::STANDARD.internal_encode(input, &mut output);
    output
}

const IV: [u32; 8] = [
    0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
];

fn hmac(key: &[u8], inner: &mut [u32; 8], outer: &mut [u32; 8]) {
    let mut block = Zeroizing::new([0u8; 64]);
    if key.len() > block.len() {
        let mut hash = Sha256::new(0);
        hash.update(key);
        hash.finalize();
        write_state(&hash.state, &mut block.as_chunks_mut::<32>().0[0]);
    } else {
        block[..key.len()].copy_from_slice(key);
    }
    for (seed, pad) in [inner, outer].into_iter().zip([0x36, 0x36 ^ 0x5c]) {
        for byte in block.iter_mut() {
            *byte ^= pad;
        }
        compress256(seed, std::slice::from_ref(&*block));
    }
}

struct Sha256 {
    state: Zeroizing<[u32; 8]>,
    block: Zeroizing<[u8; 64]>,
    len: Zeroizing<u64>,
}

impl Sha256 {
    fn new(len: u64) -> Self {
        Self {
            state: Zeroizing::new(IV),
            block: Zeroizing::new([0; 64]),
            len: Zeroizing::new(len),
        }
    }

    fn update(&mut self, mut input: &[u8]) {
        let pos = (*self.len & 63) as usize;
        *self.len = self.len.wrapping_add(input.len() as u64);
        if pos != 0 {
            let take = (64 - pos).min(input.len());
            self.block[pos..pos + take].copy_from_slice(&input[..take]);
            input = &input[take..];
            if pos + take != 64 {
                return;
            }
            compress256(&mut self.state, std::slice::from_ref(&*self.block));
        }
        let (blocks, tail) = input.as_chunks::<64>();
        if !blocks.is_empty() {
            compress256(&mut self.state, blocks);
        }
        self.block[..tail.len()].copy_from_slice(tail);
    }

    fn finalize(&mut self) {
        pad(&mut self.state, &mut self.block, *self.len);
        compress256(&mut self.state, std::slice::from_ref(&*self.block));
    }

    fn hmac(&mut self, outer: &[u32; 8], output: &mut Secret32) {
        self.finalize();
        write_state(&self.state, &mut self.block.as_chunks_mut::<32>().0[0]);
        self.state.copy_from_slice(outer);
        *self.len = 96;
        self.finalize();
        write_state(&self.state, output.as_array_mut());
    }
}

fn pad(state: &mut [u32; 8], block: &mut [u8; 64], len: u64) {
    let pos = (len & 63) as usize;
    block[pos] = 0x80;
    block[pos + 1..].fill(0);
    if pos >= 56 {
        compress256(state, std::slice::from_ref(block));
        block.fill(0);
    }
    block[56..].copy_from_slice(&len.wrapping_mul(8).to_be_bytes());
}

fn write_state(state: &[u32; 8], output: &mut [u8; 32]) {
    for (word, bytes) in state.iter().zip(output.as_chunks_mut::<4>().0) {
        *bytes = word.to_be_bytes();
    }
}

fn hmac_auth_message(
    key: &[u8; 32],
    client: &[u8; CLIENT_NONCE_BYTES],
    first: &[u8],
    server: &[u8],
    output: &mut Secret32,
) {
    let mut mac = Sha256::new(64);
    let mut outer = Zeroizing::new(IV);
    hmac(key, &mut mac.state, &mut outer);
    mac.update(b"n=,r=");
    mac.update(client);
    mac.update(b",");
    mac.update(first);
    mac.update(b",c=biws,r=");
    mac.update(server);
    mac.hmac(&outer, output);
}