forked from beltran/gosasl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sasl.go
243 lines (203 loc) · 6.06 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
package gosasl
import (
"crypto/hmac"
"crypto/md5"
"fmt"
"regexp"
)
var (
krbSPNHost = regexp.MustCompile(`\A[^/]+/(_HOST)([@/]|\z)`)
)
// DEFAULT_MAX_LENGTH is the max length that will be requested in the negotiation
// It can be set with gssapiMechanism.MaxLength = 1000
const DEFAULT_MAX_LENGTH = 16384000
// AUTH if the flag used for just basic auth, no confidentiality
var AUTH = "auth"
// AUTH_INT is the flag for authentication and integrety
var AUTH_INT = "auth-int"
// AUTH_CONF is the flag for authentication and confidentiality. It
// the most secure option.
var AUTH_CONF = "auth-conf"
//QOP_TO_FLAG is a dict that translate the string flag name into the actual bit
// It can be used wiht gssapiMechanism.UserSelectQop = QOP_TO_FLAG[AUTH_CONF] | QOP_TO_FLAG[AUTH_INT]
var QOP_TO_FLAG = map[string]byte{
AUTH: 1,
AUTH_INT: 2,
AUTH_CONF: 4,
}
// QOP is the byte that holds the QOP flags
type QOP []byte
// MechanismConfig is the configuration to use for mechanisms
type MechanismConfig struct {
name string
score int
complete bool
hasInitialResponse bool
allowsAnonymous bool
usesPlaintext bool
activeSafe bool
dictionarySafe bool
qop QOP
// It can be set with mechanism.getConfig().AuthorizationID = "authorizationId"
AuthorizationID string
}
// Mechanism is the common interface for all mechanisms
type Mechanism interface {
start() ([]byte, error)
step(challenge []byte) ([]byte, error)
encode(outgoing []byte) ([]byte, error)
decode(incoming []byte) ([]byte, error)
dispose()
getConfig() *MechanismConfig
}
// AnonymousMechanism corresponds to NONE/ Anonymous SASL mechanism
type AnonymousMechanism struct {
config *MechanismConfig
}
// NewAnonymousMechanism returns a new AnonymousMechanism
func NewAnonymousMechanism() *AnonymousMechanism {
return &AnonymousMechanism{
config: newDefaultConfig("Anonymous"),
}
}
func (m *AnonymousMechanism) start() ([]byte, error) {
return m.step(nil)
}
func (m *AnonymousMechanism) step([]byte) ([]byte, error) {
m.config.complete = true
return []byte("Anonymous, None"), nil
}
func (m *AnonymousMechanism) encode([]byte) ([]byte, error) {
return nil, nil
}
func (m *AnonymousMechanism) decode([]byte) ([]byte, error) {
return nil, nil
}
func (m *AnonymousMechanism) dispose() {}
func (m *AnonymousMechanism) getConfig() *MechanismConfig {
return m.config
}
// PlainMechanism corresponds to PLAIN SASL mechanism
type PlainMechanism struct {
mechanismConfig *MechanismConfig
identity string
username string
password string
}
// NewPlainMechanism returns a new PlainMechanism
func NewPlainMechanism(username string, password string) *PlainMechanism {
return &PlainMechanism{
mechanismConfig: newDefaultConfig("PLAIN"),
username: username,
password: password,
}
}
func (m *PlainMechanism) start() ([]byte, error) {
return m.step(nil)
}
func (m *PlainMechanism) step(challenge []byte) ([]byte, error) {
m.mechanismConfig.complete = true
var authID string
if m.mechanismConfig.AuthorizationID != "" {
authID = m.mechanismConfig.AuthorizationID
} else {
authID = m.identity
}
NULL := "\x00"
return []byte(fmt.Sprintf("%s%s%s%s%s", authID, NULL, m.username, NULL, m.password)), nil
}
func (m *PlainMechanism) encode(outgoing []byte) ([]byte, error) {
return outgoing, nil
}
func (m *PlainMechanism) decode(incoming []byte) ([]byte, error) {
return incoming, nil
}
func (m *PlainMechanism) dispose() {
m.password = ""
}
func (m *PlainMechanism) getConfig() *MechanismConfig {
return m.mechanismConfig
}
// CramMD5Mechanism corresponds to PLAIN SASL mechanism
type CramMD5Mechanism struct {
*PlainMechanism
}
// NewCramMD5Mechanism returns a new PlainMechanism
func NewCramMD5Mechanism(username string, password string) *CramMD5Mechanism {
plain := NewPlainMechanism(username, password)
return &CramMD5Mechanism{
plain,
}
}
func (m *CramMD5Mechanism) step(challenge []byte) ([]byte, error) {
if challenge == nil {
return nil, nil
}
m.mechanismConfig.complete = true
hash := hmac.New(md5.New, []byte(m.password))
// hashed := make([]byte, hash.Size())
_, err := hash.Write(challenge)
if err != nil {
return nil, err
}
return append([]byte(fmt.Sprintf("%s ", m.username)), hash.Sum(nil)...), nil
}
// Client is the entry point for usage of this library
type Client struct {
host string
authorizationID string
mechanism Mechanism
}
func newDefaultConfig(name string) *MechanismConfig {
return &MechanismConfig{
name: name,
score: 0,
complete: false,
hasInitialResponse: false,
allowsAnonymous: true,
usesPlaintext: true,
activeSafe: false,
dictionarySafe: false,
qop: nil,
AuthorizationID: "",
}
}
// NewSaslClient creates a new client given a host and a mechanism
func NewSaslClient(host string, mechanism Mechanism) *Client {
mech, ok := mechanism.(*GSSAPIMechanism)
if ok {
mech.host = host
}
return &Client{
host: host,
mechanism: mechanism,
}
}
// Start initializes the client and may generate the first challenge
func (client *Client) Start() ([]byte, error) {
return client.mechanism.start()
}
// Step is used for the initial handshake
func (client *Client) Step(challenge []byte) ([]byte, error) {
return client.mechanism.step(challenge)
}
// Complete returns true if the handshake has ended
func (client *Client) Complete() bool {
return client.mechanism.getConfig().complete
}
// GetConfig returns the configuration of the mechanism
func (client *Client) GetConfig() *MechanismConfig {
return client.mechanism.getConfig()
}
// Encode is applied on the outgoing bytes to secure them usually
func (client *Client) Encode(outgoing []byte) ([]byte, error) {
return client.mechanism.encode(outgoing)
}
// Decode is used on the incoming data to produce the usable bytes
func (client *Client) Decode(incoming []byte) ([]byte, error) {
return client.mechanism.decode(incoming)
}
// Dispose eliminates sensitive information
func (client *Client) Dispose() {
client.mechanism.dispose()
}