forked from go-zookeeper/zk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sasl.go
157 lines (122 loc) · 3.37 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
package zk
import (
"crypto/md5"
"crypto/rand"
"errors"
"fmt"
"math/big"
"strings"
)
// Handle the SASL authentification.
const (
zkSaslMd5Uri = "zookeeper/zk-sasl-md5"
zkSaslAuthQop = "auth"
zkSaslAuthIntQop = "auth-int"
zkSaslAuthConfQop = "auth-conf"
)
type setSaslResponse struct {
Nonce string
Realm string
Charset string
Algorithm string
RspAuth string
}
func getHexMd5(s string) string {
bs := []byte(s)
hash := ""
sum := md5.Sum(bs)
for _, b := range sum {
hash += fmt.Sprintf("%02x", b)
}
return hash
}
func getMd5(s string) string {
bs := []byte(s)
sum := md5.Sum(bs)
return string(sum[:])
}
func doubleQuote(s string) string {
return `"` + s + `"`
}
func rmDoubleQuote(s string) string {
leng := len(s)
return s[1 : leng-1]
}
func (r setSaslResponse) getUserPassword(auth []byte) (string, string) {
userPassword := string(auth)
split := strings.SplitN(userPassword, ":", 2)
return split[0], split[1]
}
func (r setSaslResponse) genA1(user, password, cnonce string) string {
hexStr := fmt.Sprintf("%s:%s:%s", user, r.Realm, password)
hash := getMd5(hexStr)
keyHash := fmt.Sprintf("%s:%s:%s", hash, r.Nonce, cnonce)
return getHexMd5(keyHash)
}
func (r setSaslResponse) genChallenge(user, password, cnonce, qop string, nc int) string {
rawA2 := fmt.Sprintf("%s:%s", "AUTHENTICATE", zkSaslMd5Uri)
a2 := getHexMd5(rawA2)
a1 := r.genA1(user, password, cnonce)
rv := fmt.Sprintf("%s:%s:%08x:%s:%s:%s", a1, r.Nonce, nc, cnonce, qop, a2)
return getHexMd5(rv)
}
// GenSaslChallenge refers to RFC2831 to generate a md5-digest challenge.
func (r setSaslResponse) GenSaslChallenge(auth []byte, cnonce string) (string, error) {
user, password := r.getUserPassword(auth)
if user == "" || password == "" {
return "", errors.New("found invalid user&password")
}
ch := make(map[string]string, 20)
ch["digest-uri"] = doubleQuote(zkSaslMd5Uri)
// Only "auth" qop supports so far.
qop := zkSaslAuthQop
ch["qop"] = qop
nc := 1
ch["nc"] = fmt.Sprintf("%08x", nc)
ch["realm"] = doubleQuote(r.Realm)
ch["username"] = doubleQuote(user)
// for unittest.
if cnonce == "" {
n, err := rand.Int(rand.Reader, big.NewInt(65535))
if err != nil {
return "", err
}
cnonce = fmt.Sprintf("%s", n)
}
ch["cnonce"] = doubleQuote(cnonce)
ch["nonce"] = doubleQuote(r.Nonce)
ch["response"] = r.genChallenge(user, password, cnonce, qop, nc)
items := make([]string, 0, len(ch))
for k, v := range ch {
items = append(items, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(items, ","), nil
}
// Decode decodes a md5-digest ZK SASL response.
func (r *setSaslResponse) Decode(buf []byte) (int, error) {
// Discard the first 4 bytes, they are not used here.
// According to RFC, the payload is inform of k1=v,k2=v, some of the values maybe enclosure with double quote(").
payload := string(buf[4:])
splitPayload := strings.Split(payload, ",")
if len(splitPayload) == 0 {
return 0, errors.New("invalid sasl payload")
}
r.Nonce = ""
r.Realm = ""
r.RspAuth = ""
for _, item := range splitPayload {
kv := strings.SplitN(item, "=", 2)
if len(kv) != 2 {
return 0, errors.New("invalid sasl payload format")
}
key := strings.ToLower(kv[0])
if key == "nonce" {
r.Nonce = rmDoubleQuote(kv[1])
} else if key == "realm" {
r.Realm = rmDoubleQuote(kv[1])
} else if key == "rspauth" {
r.RspAuth = kv[1]
}
}
return len(buf), nil
}