1// Package SASL implements Simple Authentication and Security Layer, RFC 4422.
12 "github.com/mjl-/mox/scram"
15// Client is a SASL client
16type Client interface {
17 // Name as used in SMTP AUTH, e.g. PLAIN, CRAM-MD5, SCRAM-SHA-256.
18 // cleartextCredentials indicates if credentials are exchanged in clear text, which influences whether they are logged.
19 Info() (name string, cleartextCredentials bool)
21 // Next is called for each step of the SASL communication. The first call has a nil
22 // fromServer and serves to get a possible "initial response" from the client. If
23 // the client sends its final message it indicates so with last. Returning an error
24 // aborts the authentication attempt.
25 // For the first toServer ("initial response"), a nil toServer indicates there is
26 // no data, which is different from a non-nil zero-length toServer.
27 Next(fromServer []byte) (toServer []byte, last bool, err error)
30type clientPlain struct {
31 Username, Password string
35var _ Client = (*clientPlain)(nil)
37// NewClientPlain returns a client for SASL PLAIN authentication.
38func NewClientPlain(username, password string) Client {
39 return &clientPlain{username, password, 0}
42func (a *clientPlain) Info() (name string, hasCleartextCredentials bool) {
46func (a *clientPlain) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
47 defer func() { a.step++ }()
50 return []byte(fmt.Sprintf("\u0000%s\u0000%s", a.Username, a.Password)), true, nil
52 return nil, false, fmt.Errorf("invalid step %d", a.step)
56type clientCRAMMD5 struct {
57 Username, Password string
61var _ Client = (*clientCRAMMD5)(nil)
63// NewClientCRAMMD5 returns a client for SASL CRAM-MD5 authentication.
64func NewClientCRAMMD5(username, password string) Client {
65 return &clientCRAMMD5{username, password, 0}
68func (a *clientCRAMMD5) Info() (name string, hasCleartextCredentials bool) {
69 return "CRAM-MD5", false
72func (a *clientCRAMMD5) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
73 defer func() { a.step++ }()
76 return nil, false, nil
78 // Validate the challenge.
80 s := string(fromServer)
81 if !strings.HasPrefix(s, "<") || !strings.HasSuffix(s, ">") {
82 return nil, false, fmt.Errorf("invalid challenge, missing angle brackets")
84 t := strings.SplitN(s, ".", 2)
85 if len(t) != 2 || t[0] == "" {
86 return nil, false, fmt.Errorf("invalid challenge, missing dot or random digits")
88 t = strings.Split(t[1], "@")
89 if len(t) == 1 || t[0] == "" || t[len(t)-1] == "" {
90 return nil, false, fmt.Errorf("invalid challenge, empty timestamp or empty hostname")
94 key := []byte(a.Password)
99 ipad := make([]byte, md5.BlockSize)
100 opad := make([]byte, md5.BlockSize)
103 for i := range ipad {
109 ipadh.Write([]byte(fromServer))
113 opadh.Write(ipadh.Sum(nil))
116 return []byte(fmt.Sprintf("%s %x", a.Username, opadh.Sum(nil))), true, nil
119 return nil, false, fmt.Errorf("invalid step %d", a.step)
123type clientSCRAMSHA struct {
124 Username, Password string
131var _ Client = (*clientSCRAMSHA)(nil)
133// NewClientSCRAMSHA1 returns a client for SASL SCRAM-SHA-1 authentication.
134func NewClientSCRAMSHA1(username, password string) Client {
135 return &clientSCRAMSHA{username, password, "SCRAM-SHA-1", 0, nil}
138// NewClientSCRAMSHA256 returns a client for SASL SCRAM-SHA-256 authentication.
139func NewClientSCRAMSHA256(username, password string) Client {
140 return &clientSCRAMSHA{username, password, "SCRAM-SHA-256", 0, nil}
143func (a *clientSCRAMSHA) Info() (name string, hasCleartextCredentials bool) {
147func (a *clientSCRAMSHA) Next(fromServer []byte) (toServer []byte, last bool, rerr error) {
148 defer func() { a.step++ }()
151 var h func() hash.Hash
155 case "SCRAM-SHA-256":
158 return nil, false, fmt.Errorf("invalid SCRAM-SHA variant %q", a.name)
161 a.scram = scram.NewClient(h, a.Username, "")
162 toserver, err := a.scram.ClientFirst()
163 return []byte(toserver), false, err
166 clientFinal, err := a.scram.ServerFirst(fromServer, a.Password)
167 return []byte(clientFinal), false, err
170 err := a.scram.ServerFinal(fromServer)
171 return nil, true, err
174 return nil, false, fmt.Errorf("invalid step %d", a.step)