/
sasl.go
296 lines (264 loc) · 7.31 KB
/
sasl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package kfake
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"errors"
"fmt"
"regexp"
"strings"
"github.com/twmb/franz-go/pkg/kmsg"
"golang.org/x/crypto/pbkdf2"
)
// TODO server-error-value in serverFinal
const (
saslPlain = "PLAIN"
saslScram256 = "SCRAM-SHA-256"
saslScram512 = "SCRAM-SHA-512"
scramIterations = 4096
)
type (
sasls struct {
plain map[string]string // user => pass
scram256 map[string]scramAuth // user => scram auth
scram512 map[string]scramAuth // user => scram auth
}
saslStage uint8
)
func (s sasls) empty() bool {
return len(s.plain) == 0 && len(s.scram256) == 0 && len(s.scram512) == 0
}
const (
saslStageBegin saslStage = iota
saslStageAuthPlain
saslStageAuthScram0_256
saslStageAuthScram0_512
saslStageAuthScram1
saslStageComplete
)
func (c *Cluster) handleSASL(creq *clientReq) (allow bool) {
switch creq.cc.saslStage {
case saslStageBegin:
switch creq.kreq.(type) {
case *kmsg.ApiVersionsRequest,
*kmsg.SASLHandshakeRequest:
return true
default:
return false
}
case saslStageAuthPlain,
saslStageAuthScram0_256,
saslStageAuthScram0_512,
saslStageAuthScram1:
switch creq.kreq.(type) {
case *kmsg.ApiVersionsRequest,
*kmsg.SASLAuthenticateRequest:
return true
default:
return false
}
case saslStageComplete:
return true
default:
panic("unreachable")
}
}
///////////
// PLAIN //
///////////
func saslSplitPlain(auth []byte) (user, pass string, err error) {
parts := strings.SplitN(string(auth), "\x00", 3)
if len(parts) != 3 {
return "", "", errors.New("invalid plain auth")
}
if len(parts[0]) != 0 && parts[0] != parts[1] {
return "", "", errors.New("authzid is not equal to username") // see below
}
return parts[1], parts[2], nil
}
///////////
// SCRAM //
///////////
func newScramAuth(mechanism, pass string) scramAuth {
var saltedPass []byte
salt := randBytes(10)
switch mechanism {
case saslScram256:
saltedPass = pbkdf2.Key([]byte(pass), salt, scramIterations, sha256.Size, sha256.New)
case saslScram512:
saltedPass = pbkdf2.Key([]byte(pass), salt, scramIterations, sha512.Size, sha512.New)
default:
panic("unreachable")
}
return scramAuth{
mechanism: mechanism,
iterations: scramIterations,
saltedPass: saltedPass,
salt: salt,
}
}
type scramAuth struct {
mechanism string // scram 256 or 512
iterations int
saltedPass []byte
salt []byte
}
// client-first-message
type scramClient0 struct {
user string
bare []byte // client-first-message-bare
nonce []byte // nonce in client0
}
var scramUnescaper = strings.NewReplacer("=3D", "=", "=2C", ",")
func scramParseClient0(client0 []byte) (scramClient0, error) {
m := reClient0.FindSubmatch(client0)
if len(m) == 0 {
return scramClient0{}, errors.New("invalid client0")
}
var (
zid = string(m[1])
bare = bytes.Clone(m[2])
user = string(m[3])
nonce = bytes.Clone(m[4])
ext = string(m[5])
)
if len(ext) != 0 {
return scramClient0{}, errors.New("invalid extensions")
}
if zid != "" && zid != user {
return scramClient0{}, errors.New("authzid is not equal to username") // Kafka & Redpanda enforce that a present zid == username
}
return scramClient0{
user: scramUnescaper.Replace(user),
bare: bare,
nonce: nonce,
}, nil
}
func scramServerFirst(client0 scramClient0, auth scramAuth) (scramServer0, []byte) {
nonce := append(client0.nonce, base64.RawStdEncoding.EncodeToString(randBytes(16))...)
serverFirst := []byte(fmt.Sprintf("r=%s,s=%s,i=%d",
nonce,
base64.StdEncoding.EncodeToString(auth.salt),
scramIterations,
))
return scramServer0{
a: auth,
c0bare: client0.bare,
s0: serverFirst,
}, serverFirst
}
// server-first-message
type scramServer0 struct {
a scramAuth
c0bare []byte
s0 []byte
}
// validates client-final-message and replies with server-final-message
func (s *scramServer0) serverFinal(clientFinal []byte) ([]byte, error) {
m := reClientFinal.FindSubmatch(clientFinal)
if len(m) == 0 {
return nil, errors.New("invalid client-final-message")
}
var (
finalWithoutProof = m[1]
channel = m[2]
clientProof64 = m[3]
h = sha256.New
)
if s.a.mechanism == saslScram512 {
h = sha512.New
}
if !bytes.Equal(channel, []byte("biws")) { // "biws" == base64("n,,")
return nil, errors.New("invalid channel binding")
}
clientProof, err := base64.StdEncoding.DecodeString(string(clientProof64))
if err != nil {
return nil, errors.New("client proof is not std-base64")
}
if len(clientProof) != h().Size() {
return nil, fmt.Errorf("len(client proof) %d != expected %d", len(clientProof), h().Size())
}
var clientKey []byte // := HMAC(SaltedPass, "Client Key")
{
mac := hmac.New(h, s.a.saltedPass)
mac.Write([]byte("Client Key"))
clientKey = mac.Sum(nil)
}
var storedKey []byte // := H(ClientKey)
{
h := h()
h.Write(clientKey)
storedKey = h.Sum(nil)
}
var authMessage []byte // := client-first-bare-message + "," + server-first-message + "," + client-final-message-without-proof
{
authMessage = append(s.c0bare, ',')
authMessage = append(authMessage, s.s0...)
authMessage = append(authMessage, ',')
authMessage = append(authMessage, finalWithoutProof...)
}
var clientSignature []byte // := HMAC(StoredKey, AuthMessage)
{
mac := hmac.New(h, storedKey)
mac.Write(authMessage)
clientSignature = mac.Sum(nil)
}
usedKey := clientProof // := ClientKey XOR ClientSignature
{
for i, b := range clientSignature {
usedKey[i] ^= b
}
h := h()
h.Write(usedKey)
usedKey = h.Sum(nil)
}
if !bytes.Equal(usedKey, storedKey) {
return nil, errors.New("invalid password")
}
var serverKey []byte // := HMAC(SaltedPass, "Server Key")
{
mac := hmac.New(h, s.a.saltedPass)
mac.Write([]byte("Server Key"))
serverKey = mac.Sum(nil)
}
var serverSignature []byte // := HMAC(ServerKey, AuthMessage)
{
mac := hmac.New(h, serverKey)
mac.Write(authMessage)
serverSignature = mac.Sum(nil)
}
serverFinal := []byte(fmt.Sprintf("v=%s", base64.StdEncoding.EncodeToString(serverSignature)))
return serverFinal, nil
}
var reClient0, reClientFinal *regexp.Regexp
func init() {
// https://datatracker.ietf.org/doc/html/rfc5802#section-7
const (
valueSafe = "[\x01-\x2b\x2d-\x3c\x3e-\x7f]+" // all except \0 - ,
value = "[\x01-\x2b\x2d-\x7f]+" // all except \0 ,
printable = "[\x21-\x2b\x2d-\x7e]+" // all except , (and DEL, unnoted)
saslName = "(?:[\x01-\x2b\x2d-\x3c\x3e-\x7f]|=2C|=3D)+" // valueSafe | others; kafka is lazy here
b64 = `[a-zA-Z0-9/+]+={0,3}` // we are lazy here matching up to 3 =
ext = "(?:,[a-zA-Z]+=[\x01-\x2b\x2d-\x7f]+)*"
)
// 0: entire match
// 1: authzid
// 2: client-first-message-bare
// 3: username
// 4: nonce
// 5: ext
client0 := fmt.Sprintf("^n,(?:a=(%s))?,((?:m=%s,)?n=(%s),r=(%s)(%s))$", saslName, value, saslName, printable, ext)
// We reject extensions in client0. Kafka does not validate the nonce
// and some clients may generate it incorrectly (i.e. old franz-go), so
// we do not validate it.
//
// 0: entire match
// 1: channel-final-message-without-proof
// 2: channel binding
// 3: proof
clientFinal := fmt.Sprintf("^(c=(%s),r=%s),p=(%s)$", b64, printable, b64)
reClient0 = regexp.MustCompile(client0)
reClientFinal = regexp.MustCompile(clientFinal)
}