Skip to content
Jennifer Programming Language

sasl API reference

The SASL authentication mechanisms shared by the mail clients (smtp / pop / imap). These format the client tokens; the protocol clients run the mechanism-specific wire dialogue (SMTP AUTH, IMAP AUTHENTICATE, POP3 AUTH) around them. No networking; TinyGo-clean, so it runs on both binaries.

  • Simple encoders (plain, loginUser / loginPass, bearer) - one base64 token per step. bearer builds the SASL XOAUTH2 response from an OAuth2 bearer token (named for the token it carries; "XOAUTH2" is the mechanism string the client sends).
  • Challenge-response (cram, and the SCRAM family) - keyed by the hash / crypto libraries. cram (CRAM-MD5, RFC 2195) answers a server challenge in one step. SCRAM is a multi-step exchange over a Scram handle: scramStart -> scramClientFirst (send), receive server-first -> scramClientFinal + scramFinalToken (send), receive server-final -> scramVerify (checks the server signature, so a MITM without the password is caught). Mechanism "sha1" is SCRAM-SHA-1, "sha256" is SCRAM-SHA-256. The server's iteration count must be 4096..1000000, and a mandatory extension (m=) fails the exchange. SASLprep (RFC 4013) is not applied: credentials are used as given.

Import with import "sasl.j" as sasl;. See the sasl guide for prose and examples.

Contents

NameWhat it is
bearerBuild the SASL XOAUTH2 response for an OAuth2 bearer token: base64("user=" user 0x01 "auth=Bearer " token 0...
cramBuild the CRAM-MD5 response (RFC 2195): base64 of "user <hex>", where <hex> is the lowercase HMAC-MD5 of th...
loginPassBuild the password step of SASL LOGIN (a server sends the "Password:" prompt).
loginUserBuild the username step of SASL LOGIN (a server sends the "Username:" prompt).
negotiateChoose the strongest password mechanism this module implements from the list a server advertises, returned ...
plainBuild the SASL PLAIN response: base64 of "\0user\0pass".
scramClientFinalProcess the server-first message and compute the client-final message.
scramClientFirstThe base64 client-first message to send to the server (SASL initial response).
scramFinalTokenThe base64 client-final message to send (after scramClientFinal).
scramStartBegin a SCRAM exchange: generate a crypto-grade client nonce and build the initial state.
scramVerifyVerify the server-final message: the server's signature must match the one derived in scramClientFinal (con...
Scramstruct: The state of an in-progress SCRAM exchange, threaded through the four calls.

Functions

bearer

jennifer
sasl.bearer(user as string, token as string)

Build the SASL XOAUTH2 response for an OAuth2 bearer token: base64("user=" user 0x01 "auth=Bearer " token 0x01 0x01). This is how Google and Microsoft 365 authenticate mail (both have retired password auth).

user
the account username
token
the OAuth2 bearer access token
Returns string
the base64-encoded XOAUTH2 response

cram

jennifer
sasl.cram(user as string, pass as string, challenge as string)

Build the CRAM-MD5 response (RFC 2195): base64 of "user <hex>", where <hex> is the lowercase HMAC-MD5 of the server's (base64-encoded) challenge keyed by the password. A one-step challenge-response mechanism. Named cram; the wire mechanism is "CRAM-MD5".

user
the login username
pass
the login password
challenge
the server's base64-encoded challenge
Returns string
the base64-encoded CRAM-MD5 response

loginPass

jennifer
sasl.loginPass(pass as string)

Build the password step of SASL LOGIN (a server sends the "Password:" prompt).

pass
the login password
Returns string
the base64-encoded password

loginUser

jennifer
sasl.loginUser(user as string)

Build the username step of SASL LOGIN (a server sends the "Username:" prompt).

user
the login username
Returns string
the base64-encoded username

negotiate

jennifer
sasl.negotiate(advertised as list of string)

Choose the strongest password mechanism this module implements from the list a server advertises, returned as the auth token a mail client uses: "scram-sha-256" > "scram-sha-1" > "cram", or "" when the server offers none (the caller then falls back to its protocol default - PLAIN / LOGIN / USER-PASS). XOAUTH2 is never auto-selected: it needs a bearer token, not a password.

advertised
the mechanism names the server advertises (any case)
Returns string
the chosen auth token, or "" for the protocol default

plain

jennifer
sasl.plain(user as string, pass as string)

Build the SASL PLAIN response: base64 of "\0user\0pass".

user
the login username
pass
the login password
Returns string
the base64-encoded PLAIN token

scramClientFinal

jennifer
sasl.scramClientFinal(s as Scram, serverFirst as string, password as string)

Process the server-first message and compute the client-final message. Derives the salted password (PBKDF2 in the mechanism's hash), the client proof, and the expected server signature; the returned state carries both.

s
the exchange state
serverFirst
the base64 server-first message
password
the login password
Returns Scram
the updated state; pass it to scramFinalToken and scramVerify
Throws Error
kind "sasl" if the server-first carries a mandatory extension (m=, which RFC 5802 says the client must fail on), the server nonce does not extend the client nonce, the salt is empty, or the iteration count is not 4096..1000000 (RFC 7677's floor; the cap stops a hostile server stalling the client)

scramClientFirst

jennifer
sasl.scramClientFirst(s as Scram)

The base64 client-first message to send to the server (SASL initial response).

s
the exchange state from scramStart
Returns string
the base64-encoded client-first message

scramFinalToken

jennifer
sasl.scramFinalToken(s as Scram)

The base64 client-final message to send (after scramClientFinal).

s
the state returned by scramClientFinal
Returns string
the base64-encoded client-final message

scramStart

jennifer
sasl.scramStart(algo as string, user as string)

Begin a SCRAM exchange: generate a crypto-grade client nonce and build the initial state. algo selects the mechanism ("sha1", "sha256" or "sha512").

algo
"sha1" (SCRAM-SHA-1), "sha256" (SCRAM-SHA-256) or "sha512"
user
the login username
Returns Scram
the exchange state; pass it to scramClientFirst
Throws Error
kind "sasl" on an unknown algorithm

scramVerify

jennifer
sasl.scramVerify(s as Scram, serverFinal as string)

Verify the server-final message: the server's signature must match the one derived in scramClientFinal (constant-time), proving the server also knows the password. Reject the session on false.

s
the state returned by scramClientFinal
serverFinal
the base64 server-final message
Returns bool
true if the server signature verifies

Structs

Scram

The state of an in-progress SCRAM exchange, threaded through the four calls. Value-semantic: each step returns an updated copy.

jennifer
def struct Scram {
    algo as string,
    clientNonce as string,
    clientFirstBare as string,
    clientFinal as string,
    serverSig as string
};
algo
the hash mechanism: "sha1" (SCRAM-SHA-1) or "sha256" (SCRAM-SHA-256)
clientNonce
the client-generated nonce
clientFirstBare
the client-first message without the gs2 header
clientFinal
the client-final message (set by scramClientFinal)
serverSig
the base64 ServerSignature to expect (set by scramClientFinal)