packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /// SCRAM-SHA-256 client authentication (RFC 5802, RFC 7677; issue gabla108/louise#145): |
| 2 | /// the exchange as pure functions — a nonce goes in, the messages come |
| 3 | /// out — so the whole handshake is testable against the RFC's recorded |
| 4 | /// one, no server needed. |
| 5 | /// |
| 6 | /// The connection actor drives it: `client_first`, then on |
| 7 | /// `AuthenticationSaslContinue` `client_final`, and on |
| 8 | /// `AuthenticationSaslFinal` `verify_server`. |
| 9 | |
| 10 | import std/base64 |
| 11 | import std/bytes |
| 12 | import std/crypto |
| 13 | import std/int |
| 14 | import std/list |
| 15 | import std/option |
| 16 | import std/result |
| 17 | import std/string |
| 18 | |
| 19 | /// The state between the steps. |
| 20 | pub type Scram |
| 21 | { /// After `client_first`. |
| 22 | Started(client_nonce: String, client_first_bare: String) |
| 23 | /// After `client_final`: the signature the server must show. |
| 24 | , Answered(server_signature: Bytes) |
| 25 | } |
| 26 | |
| 27 | /// The client-first message: `n,,n=,r=<nonce>`. The `n=` (username) part |
| 28 | /// is empty: PostgreSQL reads the user from the startup message. The |
| 29 | /// nonce is the caller's (`random.bytes`-backed in production). |
| 30 | pub fn client_first :: nonce: String -> (Scram, Bytes) |
| 31 | { |
| 32 | let bare = "n=,r=" <> nonce |
| 33 | (Started(nonce, bare), bytes.from_string ("n,," <> bare)) |
| 34 | } |
| 35 | |
| 36 | /// The client-final message for `password`, answering the server-first |
| 37 | /// message (`r=<nonce>,s=<salt>,i=<iterations>`). The server's nonce must |
| 38 | /// extend ours (a downgrade or a replay has it not). |
| 39 | pub fn client_final :: |
| 40 | state: Scram |
| 41 | password: String |
| 42 | server_first: String |
| 43 | -> Result<(Scram, String), String> |
| 44 | { |
| 45 | match state |
| 46 | { Answered(_) -> Error("scram: the exchange already sent its client-final") |
| 47 | , Started(client_nonce, bare) -> |
| 48 | { |
| 49 | let fields = list.map (string.split server_first ",") field_value |
| 50 | use nonce <- |
| 51 | result.try |
| 52 | ( option.to_result |
| 53 | (field fields "r") |
| 54 | "scram: the server-first message has no nonce" |
| 55 | ) |
| 56 | if !string.starts_with nonce client_nonce |
| 57 | { |
| 58 | Error("scram: the server's nonce does not extend ours") |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | use salt64 <- |
| 63 | result.try |
| 64 | ( option.to_result |
| 65 | (field fields "s") |
| 66 | "scram: no salt in the server-first message" |
| 67 | ) |
| 68 | use its <- |
| 69 | result.try |
| 70 | ( option.to_result |
| 71 | (field fields "i") |
| 72 | "scram: no iteration count in the server-first message" |
| 73 | ) |
| 74 | use iterations <- |
| 75 | result.try |
| 76 | (string.to_int its |> result.map_error fn e { "scram: {e}" }) |
| 77 | use salt <- result.try (base64.decode salt64) |
| 78 | let salted = |
| 79 | crypto.pbkdf2_hmac_sha256 |
| 80 | (bytes.from_string password) |
| 81 | salt |
| 82 | iterations |
| 83 | 32 |
| 84 | let client_key = |
| 85 | crypto.hmac_sha256 salted (bytes.from_string "Client Key") |
| 86 | let stored_key = crypto.sha256 client_key |
| 87 | let without_proof = "c=biws,r=" <> nonce |
| 88 | let auth_message = bare <> "," <> server_first <> "," <> without_proof |
| 89 | let proof = |
| 90 | xor |
| 91 | client_key |
| 92 | (crypto.hmac_sha256 stored_key (bytes.from_string auth_message)) |
| 93 | let server_key = |
| 94 | crypto.hmac_sha256 salted (bytes.from_string "Server Key") |
| 95 | let signature = |
| 96 | crypto.hmac_sha256 server_key (bytes.from_string auth_message) |
| 97 | Ok((Answered(signature), without_proof <> ",p=" <> base64.encode proof)) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Check the server-final message's `v=<signature>`: a wrong one means the |
| 104 | /// server never knew the password either. |
| 105 | pub fn verify_server :: state: Scram server_final: String -> Result<(), String> |
| 106 | { |
| 107 | match state |
| 108 | { Started(..) -> Error("scram: no client-final was sent") |
| 109 | , Answered(expected) -> |
| 110 | { |
| 111 | let fields = list.map (string.split server_final ",") field_value |
| 112 | match field fields "v" |
| 113 | { None -> Error("scram: the server-final message has no signature") |
| 114 | , Some(signature64) -> match base64.decode signature64 |
| 115 | { Ok(signature) -> if signature == expected |
| 116 | { |
| 117 | Ok(()) |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | Error( "scram: the server never knew the password (bad server signature)" |
| 122 | ) |
| 123 | } |
| 124 | , Error(e) -> Error("scram: {e}") |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // ---- helpers ---------------------------------------------------------------- |
| 132 | |
| 133 | /// `"name=value"` as `(name, value)`; a value may itself contain `=` |
| 134 | /// (base64 padding does). |
| 135 | fn field_value :: f: String -> (String, String) |
| 136 | { |
| 137 | match string.split f "=" |
| 138 | { [name, ..rest] -> (name, string.join rest "="), [] -> (f, "") } |
| 139 | } |
| 140 | |
| 141 | /// The value of the field `name`, if present. |
| 142 | fn field :: fields: List<(String, String)> name: String -> Option<String> |
| 143 | { |
| 144 | match fields |
| 145 | { [] -> None |
| 146 | , [(n, v), ..rest] -> if n == name { Some(v) } else { field rest name } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /// Byte-wise XOR (same length: a key and its signature are both 32 bytes). |
| 151 | fn xor :: a: Bytes b: Bytes -> Bytes |
| 152 | { |
| 153 | bytes.from_list (list.zip_with (bytes.to_list a) (bytes.to_list b) int.xor) |
| 154 | } |