Karta.SASL.SCRAM (Karta v1.0.0)

SCRAM-SHA-256 (RFC 5802, RFC 7677): the server side of the exchange, without channel binding, and the derivation of the keys a server stores.

Stored keys. A password is never stored. derive/3 turns it, a random salt and an iteration count into StoredKey (the hash of the client key) and ServerKey; the server keeps those three and the count. A password given in clear (SMTP AUTH PLAIN or LOGIN) is checked the same way: check_password/2 derives StoredKey with the stored salt and compares in constant time. So one stored form serves every mechanism.

The exchange:

  1. client_first/1 parses the client's first message (n,,n=user,r=<client nonce>);
  2. server_first/4 answers with the combined nonce, the salt and the iteration count;
  3. client_final/3 checks the client's proof against StoredKey and returns the server's final message, v=<server signature>, with which the client in turn authenticates the server.

Channel binding (SCRAM-SHA-256-PLUS) is not offered: a client that asks for it (p=) is refused; y (the client could, but the server did not offer it) is accepted. An authorisation identity is accepted only when it equals the user name. Passwords and user names are used as given, without SASLprep (RFC 4013); for printable ASCII, which is what Karta generates, SASLprep changes nothing.

Summary

Functions

Checks a password in clear against stored keys (salt, iterations, stored_key).

Checks the client's final message against the stored keys. Returns the server's final message on success.

Parses the client's first message.

StoredKey and ServerKey for a password (RFC 5802 §3).

A server nonce: 18 random bytes in base64 (printable, no commas).

The server's first message for a parsed client first message, and the state for client_final/3.

Types

client_first()

@type client_first() :: %{
  gs2_header: binary(),
  username: String.t(),
  nonce: binary(),
  bare: binary()
}

keys()

@type keys() :: %{stored_key: binary(), server_key: binary()}

reason()

@type reason() ::
  :invalid | :channel_binding | :extension | :authzid | :nonce | :proof

state()

@type state() :: %{gs2_header: binary(), nonce: binary(), auth_prefix: binary()}

Functions

check_password(password, map)

@spec check_password(binary(), %{
  salt: binary(),
  iterations: pos_integer(),
  stored_key: binary()
}) ::
  boolean()

Checks a password in clear against stored keys (salt, iterations, stored_key).

client_final(state, message, map)

@spec client_final(state(), binary(), keys()) :: {:ok, binary()} | {:error, reason()}

Checks the client's final message against the stored keys. Returns the server's final message on success.

client_first(message)

@spec client_first(binary()) :: {:ok, client_first()} | {:error, reason()}

Parses the client's first message.

derive(password, salt, iterations)

@spec derive(binary(), binary(), pos_integer()) :: keys()

StoredKey and ServerKey for a password (RFC 5802 §3).

nonce()

@spec nonce() :: binary()

A server nonce: 18 random bytes in base64 (printable, no commas).

server_first(first, server_nonce, salt, iterations)

@spec server_first(client_first(), binary(), binary(), pos_integer()) ::
  {binary(), state()}

The server's first message for a parsed client first message, and the state for client_final/3.