1// Package SASL implements Simple Authentication and Security Layer, RFC 4422.
13 "golang.org/x/text/secure/precis"
15 "github.com/mjl-/mox/scram"
18// Client is a SASL client.
20// A SASL client can be used for authentication in IMAP, SMTP and other protocols.
21// A client and server exchange messages in step lock. In IMAP and SMTP, these
22// messages are encoded with base64. Each SASL mechanism has predefined steps, but
23// the transaction can be aborted by either side at any time. An IMAP or SMTP
24// client must choose a SASL mechanism, instantiate a SASL client, and call Next
25// with a nil parameter. The resulting data must be written to the server, properly
26// encoded. The client must then read the response from the server and feed it to
27// the SASL client, which will return more data to send, or an error.
28type Client interface {
29 // Name as used in SMTP or IMAP authentication, e.g. PLAIN, CRAM-MD5,
30 // SCRAM-SHA-256. cleartextCredentials indicates if credentials are exchanged in
31 // clear text, which can be used to decide if the exchange is logged.
32 Info() (name string, cleartextCredentials bool)
34 // Next must be called for each step of the SASL transaction. The first call has a
35 // nil fromServer and serves to get a possible "initial response" from the client
36 // to the server. When last is true, the message from client to server is the last
37 // one, and the server must send a verdict. If err is set, the transaction must be
40 // For the first toServer ("initial response"), a nil toServer indicates there is
41 // no data, which is different from a non-nil zero-length toServer.
42 Next(fromServer []byte) (toServer []byte, last bool, err error)
45type clientPlain struct {
46 Username, Password string
50var _ Client = (*clientPlain)(nil)
52// NewClientPlain returns a client for SASL PLAIN authentication.
54// PLAIN is specified in RFC 4616, The PLAIN Simple Authentication and Security
55// Layer (SASL) Mechanism.
56func NewClientPlain(username, password string) Client {
58 return &clientPlain{username, password, 0}
61func (a *clientPlain) Info() (name string, hasCleartextCredentials bool) {
65func (a *clientPlain) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
66 defer func() { a.step++ }()
69 return []byte(fmt.Sprintf("\u0000%s\u0000%s", a.Username, a.Password)), true, nil
71 return nil, false, fmt.Errorf("invalid step %d", a.step)
75type clientLogin struct {
76 Username, Password string
80var _ Client = (*clientLogin)(nil)
82// NewClientLogin returns a client for the obsolete SASL LOGIN authentication.
84// See https://datatracker.ietf.org/doc/html/draft-murchison-sasl-login-00
85func NewClientLogin(username, password string) Client {
87 return &clientLogin{username, password, 0}
90func (a *clientLogin) Info() (name string, hasCleartextCredentials bool) {
94func (a *clientLogin) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
95 defer func() { a.step++ }()
98 return []byte(a.Username), false, nil
100 return []byte(a.Password), true, nil
102 return nil, false, fmt.Errorf("invalid step %d", a.step)
106// Cleanup password with precis, like remote should have done. If the password
107// appears invalid, we'll return the original, there is a chance the server also
109func precisPassword(password string) string {
110 pw, err := precis.OpaqueString.String(password)
117type clientCRAMMD5 struct {
118 Username, Password string
122var _ Client = (*clientCRAMMD5)(nil)
124// NewClientCRAMMD5 returns a client for SASL CRAM-MD5 authentication.
126// CRAM-MD5 is specified in RFC 2195, IMAP/POP AUTHorize Extension for Simple
127// Challenge/Response.
128func NewClientCRAMMD5(username, password string) Client {
129 password = precisPassword(password)
130 return &clientCRAMMD5{username, password, 0}
133func (a *clientCRAMMD5) Info() (name string, hasCleartextCredentials bool) {
134 return "CRAM-MD5", false
137func (a *clientCRAMMD5) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
138 defer func() { a.step++ }()
141 return nil, false, nil
143 // Validate the challenge.
145 s := string(fromServer)
146 if !strings.HasPrefix(s, "<") || !strings.HasSuffix(s, ">") {
147 return nil, false, fmt.Errorf("invalid challenge, missing angle brackets")
149 t := strings.SplitN(s, ".", 2)
150 if len(t) != 2 || t[0] == "" {
151 return nil, false, fmt.Errorf("invalid challenge, missing dot or random digits")
153 t = strings.Split(t[1], "@")
154 if len(t) == 1 || t[0] == "" || t[len(t)-1] == "" {
155 return nil, false, fmt.Errorf("invalid challenge, empty timestamp or empty hostname")
159 key := []byte(a.Password)
164 ipad := make([]byte, md5.BlockSize)
165 opad := make([]byte, md5.BlockSize)
168 for i := range ipad {
174 ipadh.Write([]byte(fromServer))
178 opadh.Write(ipadh.Sum(nil))
181 return []byte(fmt.Sprintf("%s %x", a.Username, opadh.Sum(nil))), true, nil
184 return nil, false, fmt.Errorf("invalid step %d", a.step)
188type clientSCRAMSHA struct {
189 Username, Password string
191 hash func() hash.Hash
194 cs tls.ConnectionState
196 // When not doing PLUS variant, this field indicates whether that is because the
197 // server doesn't support the PLUS variant. Used for detecting MitM attempts.
205var _ Client = (*clientSCRAMSHA)(nil)
207// NewClientSCRAMSHA1 returns a client for SASL SCRAM-SHA-1 authentication.
209// Clients should prefer using the PLUS-variant with TLS channel binding, if
210// supported by a server. If noServerPlus is set, this mechanism was chosen because
211// the PLUS-variant was not supported by the server. If the server actually does
212// implement the PLUS variant, this can indicate a MitM attempt, which is detected
213// by the server and causes the authentication attempt to be aborted.
215// SCRAM-SHA-1 is specified in RFC 5802, "Salted Challenge Response Authentication
216// Mechanism (SCRAM) SASL and GSS-API Mechanisms".
217func NewClientSCRAMSHA1(username, password string, noServerPlus bool) Client {
218 password = precisPassword(password)
219 return &clientSCRAMSHA{username, password, sha1.New, false, tls.ConnectionState{}, noServerPlus, "SCRAM-SHA-1", 0, nil}
222// NewClientSCRAMSHA1PLUS returns a client for SASL SCRAM-SHA-1-PLUS authentication.
224// The PLUS-variant binds the authentication exchange to the TLS connection,
225// detecting any MitM attempt.
227// SCRAM-SHA-1-PLUS is specified in RFC 5802, "Salted Challenge Response
228// Authentication Mechanism (SCRAM) SASL and GSS-API Mechanisms".
229func NewClientSCRAMSHA1PLUS(username, password string, cs tls.ConnectionState) Client {
230 password = precisPassword(password)
231 return &clientSCRAMSHA{username, password, sha1.New, true, cs, false, "SCRAM-SHA-1-PLUS", 0, nil}
234// NewClientSCRAMSHA256 returns a client for SASL SCRAM-SHA-256 authentication.
236// Clients should prefer using the PLUS-variant with TLS channel binding, if
237// supported by a server. If noServerPlus is set, this mechanism was chosen because
238// the PLUS-variant was not supported by the server. If the server actually does
239// implement the PLUS variant, this can indicate a MitM attempt, which is detected
240// by the server and causes the authentication attempt to be aborted.
242// SCRAM-SHA-256 is specified in RFC 7677, "SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
243// Simple Authentication and Security Layer (SASL) Mechanisms".
244func NewClientSCRAMSHA256(username, password string, noServerPlus bool) Client {
245 password = precisPassword(password)
246 return &clientSCRAMSHA{username, password, sha256.New, false, tls.ConnectionState{}, noServerPlus, "SCRAM-SHA-256", 0, nil}
249// NewClientSCRAMSHA256PLUS returns a client for SASL SCRAM-SHA-256-PLUS authentication.
251// The PLUS-variant binds the authentication exchange to the TLS connection,
252// detecting any MitM attempt.
254// SCRAM-SHA-256-PLUS is specified in RFC 7677, "SCRAM-SHA-256 and SCRAM-SHA-256-PLUS
255// Simple Authentication and Security Layer (SASL) Mechanisms".
256func NewClientSCRAMSHA256PLUS(username, password string, cs tls.ConnectionState) Client {
257 password = precisPassword(password)
258 return &clientSCRAMSHA{username, password, sha256.New, true, cs, false, "SCRAM-SHA-256-PLUS", 0, nil}
261func (a *clientSCRAMSHA) Info() (name string, hasCleartextCredentials bool) {
265func (a *clientSCRAMSHA) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
266 defer func() { a.step++ }()
269 var cs *tls.ConnectionState
273 a.scram = scram.NewClient(a.hash, a.Username, "", a.noServerPlus, cs)
274 toserver, err := a.scram.ClientFirst()
275 return []byte(toserver), false, err
278 clientFinal, err := a.scram.ServerFirst(fromServer, a.Password)
279 return []byte(clientFinal), false, err
282 err := a.scram.ServerFinal(fromServer)
283 return nil, true, err
286 return nil, false, fmt.Errorf("invalid step %d", a.step)