1// Package SASL implements Simple Authentication and Security Layer, RFC 4422.
13 "github.com/mjl-/mox/scram"
16// Client is a SASL client.
18// A SASL client can be used for authentication in IMAP, SMTP and other protocols.
19// A client and server exchange messages in step lock. In IMAP and SMTP, these
20// messages are encoded with base64. Each SASL mechanism has predefined steps, but
21// the transaction can be aborted by either side at any time. An IMAP or SMTP
22// client must choose a SASL mechanism, instantiate a SASL client, and call Next
23// with a nil parameter. The resulting data must be written to the server, properly
24// encoded. The client must then read the response from the server and feed it to
25// the SASL client, which will return more data to send, or an error.
26type Client interface {
27 // Name as used in SMTP or IMAP authentication, e.g. PLAIN, CRAM-MD5,
28 // SCRAM-SHA-256. cleartextCredentials indicates if credentials are exchanged in
29 // clear text, which can be used to decide if the exchange is logged.
30 Info() (name string, cleartextCredentials bool)
32 // Next must be called for each step of the SASL transaction. The first call has a
33 // nil fromServer and serves to get a possible "initial response" from the client
34 // to the server. When last is true, the message from client to server is the last
35 // one, and the server must send a verdict. If err is set, the transaction must be
38 // For the first toServer ("initial response"), a nil toServer indicates there is
39 // no data, which is different from a non-nil zero-length toServer.
40 Next(fromServer []byte) (toServer []byte, last bool, err error)
43type clientPlain struct {
44 Username, Password string
48var _ Client = (*clientPlain)(nil)
50// NewClientPlain returns a client for SASL PLAIN authentication.
52// PLAIN is specified in RFC 4616, The PLAIN Simple Authentication and Security
53// Layer (SASL) Mechanism.
54func NewClientPlain(username, password string) Client {
55 return &clientPlain{username, password, 0}
58func (a *clientPlain) Info() (name string, hasCleartextCredentials bool) {
62func (a *clientPlain) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
63 defer func() { a.step++ }()
66 return []byte(fmt.Sprintf("\u0000%s\u0000%s", a.Username, a.Password)), true, nil
68 return nil, false, fmt.Errorf("invalid step %d", a.step)
72type clientLogin struct {
73 Username, Password string
77var _ Client = (*clientLogin)(nil)
79// NewClientLogin returns a client for the obsolete SASL LOGIN authentication.
81// See https://datatracker.ietf.org/doc/html/draft-murchison-sasl-login-00
82func NewClientLogin(username, password string) Client {
83 return &clientLogin{username, password, 0}
86func (a *clientLogin) Info() (name string, hasCleartextCredentials bool) {
90func (a *clientLogin) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
91 defer func() { a.step++ }()
94 return []byte(a.Username), false, nil
96 return []byte(a.Password), true, nil
98 return nil, false, fmt.Errorf("invalid step %d", a.step)
102type clientCRAMMD5 struct {
103 Username, Password string
107var _ Client = (*clientCRAMMD5)(nil)
109// NewClientCRAMMD5 returns a client for SASL CRAM-MD5 authentication.
111// CRAM-MD5 is specified in RFC 2195, IMAP/POP AUTHorize Extension for Simple
112// Challenge/Response.
113func NewClientCRAMMD5(username, password string) Client {
114 return &clientCRAMMD5{username, password, 0}
117func (a *clientCRAMMD5) Info() (name string, hasCleartextCredentials bool) {
118 return "CRAM-MD5", false
121func (a *clientCRAMMD5) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
122 defer func() { a.step++ }()
125 return nil, false, nil
127 // Validate the challenge.
129 s := string(fromServer)
130 if !strings.HasPrefix(s, "<") || !strings.HasSuffix(s, ">") {
131 return nil, false, fmt.Errorf("invalid challenge, missing angle brackets")
133 t := strings.SplitN(s, ".", 2)
134 if len(t) != 2 || t[0] == "" {
135 return nil, false, fmt.Errorf("invalid challenge, missing dot or random digits")
137 t = strings.Split(t[1], "@")
138 if len(t) == 1 || t[0] == "" || t[len(t)-1] == "" {
139 return nil, false, fmt.Errorf("invalid challenge, empty timestamp or empty hostname")
143 key := []byte(a.Password)
148 ipad := make([]byte, md5.BlockSize)
149 opad := make([]byte, md5.BlockSize)
152 for i := range ipad {
158 ipadh.Write([]byte(fromServer))
162 opadh.Write(ipadh.Sum(nil))
165 return []byte(fmt.Sprintf("%s %x", a.Username, opadh.Sum(nil))), true, nil
168 return nil, false, fmt.Errorf("invalid step %d", a.step)
172type clientSCRAMSHA struct {
173 Username, Password string
175 hash func() hash.Hash
178 cs tls.ConnectionState
180 // When not doing PLUS variant, this field indicates whether that is because the
181 // server doesn't support the PLUS variant. Used for detecting MitM attempts.
189var _ Client = (*clientSCRAMSHA)(nil)
191// NewClientSCRAMSHA1 returns a client for SASL SCRAM-SHA-1 authentication.
193// Clients should prefer using the PLUS-variant with TLS channel binding, if
194// supported by a server. If noServerPlus is set, this mechanism was chosen because
195// the PLUS-variant was not supported by the server. If the server actually does
196// implement the PLUS variant, this can indicate a MitM attempt, which is detected
197// by the server and causes the authentication attempt to be aborted.
199// SCRAM-SHA-1 is specified in RFC 5802, "Salted Challenge Response Authentication
200// Mechanism (SCRAM) SASL and GSS-API Mechanisms".
201func NewClientSCRAMSHA1(username, password string, noServerPlus bool) Client {
202 return &clientSCRAMSHA{username, password, sha1.New, false, tls.ConnectionState{}, noServerPlus, "SCRAM-SHA-1", 0, nil}
205// NewClientSCRAMSHA1PLUS returns a client for SASL SCRAM-SHA-1-PLUS authentication.
207// The PLUS-variant binds the authentication exchange to the TLS connection,
208// detecting any MitM attempt.
210// SCRAM-SHA-1-PLUS is specified in RFC 5802, "Salted Challenge Response
211// Authentication Mechanism (SCRAM) SASL and GSS-API Mechanisms".
212func NewClientSCRAMSHA1PLUS(username, password string, cs tls.ConnectionState) Client {
213 return &clientSCRAMSHA{username, password, sha1.New, true, cs, false, "SCRAM-SHA-1-PLUS", 0, nil}
216// NewClientSCRAMSHA256 returns a client for SASL SCRAM-SHA-256 authentication.
218// Clients should prefer using the PLUS-variant with TLS channel binding, if
219// supported by a server. If noServerPlus is set, this mechanism was chosen because
220// the PLUS-variant was not supported by the server. If the server actually does
221// implement the PLUS variant, this can indicate a MitM attempt, which is detected
222// by the server and causes the authentication attempt to be aborted.
224// SCRAM-SHA-256 is specified in RFC 7677, "SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
225// Simple Authentication and Security Layer (SASL) Mechanisms".
226func NewClientSCRAMSHA256(username, password string, noServerPlus bool) Client {
227 return &clientSCRAMSHA{username, password, sha256.New, false, tls.ConnectionState{}, noServerPlus, "SCRAM-SHA-256", 0, nil}
230// NewClientSCRAMSHA256PLUS returns a client for SASL SCRAM-SHA-256-PLUS authentication.
232// The PLUS-variant binds the authentication exchange to the TLS connection,
233// detecting any MitM attempt.
235// SCRAM-SHA-256-PLUS is specified in RFC 7677, "SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
236// Simple Authentication and Security Layer (SASL) Mechanisms".
237func NewClientSCRAMSHA256PLUS(username, password string, cs tls.ConnectionState) Client {
238 return &clientSCRAMSHA{username, password, sha256.New, true, cs, false, "SCRAM-SHA-256-PLUS", 0, nil}
241func (a *clientSCRAMSHA) Info() (name string, hasCleartextCredentials bool) {
245func (a *clientSCRAMSHA) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
246 defer func() { a.step++ }()
249 var cs *tls.ConnectionState
253 a.scram = scram.NewClient(a.hash, a.Username, "", a.noServerPlus, cs)
254 toserver, err := a.scram.ClientFirst()
255 return []byte(toserver), false, err
258 clientFinal, err := a.scram.ServerFirst(fromServer, a.Password)
259 return []byte(clientFinal), false, err
262 err := a.scram.ServerFinal(fromServer)
263 return nil, true, err
266 return nil, false, fmt.Errorf("invalid step %d", a.step)