author | Chris Jones <chris@cjones.org> |
Tue, 27 Dec 2011 15:36:07 -0700 | |
changeset 11 | 48be1ae93fd4 |
parent 10 | f38b0ee7b1c1 |
child 12 | 122ab6208c3c |
permissions | -rw-r--r-- |
10 | 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style |
|
3 |
// license that can be found in the LICENSE file. |
|
4 |
||
5 |
// This file contains the three layers of processing for the |
|
6 |
// communication with the server: transport (where TLS happens), XML |
|
7 |
// (where strings are converted to go structures), and Stream (where |
|
8 |
// we respond to XMPP events on behalf of the library client). |
|
9 |
||
10 |
package xmpp |
|
11 |
||
12 |
import ( |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
13 |
"big" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
14 |
"crypto/md5" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
15 |
"crypto/rand" |
10 | 16 |
"crypto/tls" |
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
17 |
"encoding/base64" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
18 |
"fmt" |
10 | 19 |
"io" |
20 |
"log" |
|
21 |
"net" |
|
22 |
"os" |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
23 |
"regexp" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
24 |
"strings" |
10 | 25 |
"time" |
26 |
"xml" |
|
27 |
) |
|
28 |
||
29 |
func (cl *Client) readTransport(w io.Writer) { |
|
30 |
defer tryClose(cl.socket, w) |
|
31 |
cl.socket.SetReadTimeout(1e8) |
|
32 |
p := make([]byte, 1024) |
|
33 |
for { |
|
34 |
if cl.socket == nil { |
|
35 |
cl.waitForSocket() |
|
36 |
} |
|
37 |
nr, err := cl.socket.Read(p) |
|
38 |
if nr == 0 { |
|
39 |
if errno, ok := err.(*net.OpError) ; ok { |
|
40 |
if errno.Timeout() { |
|
41 |
continue |
|
42 |
} |
|
43 |
} |
|
44 |
log.Printf("read: %s", err.String()) |
|
45 |
break |
|
46 |
} |
|
47 |
nw, err := w.Write(p[:nr]) |
|
48 |
if nw < nr { |
|
49 |
log.Println("read: %s", err.String()) |
|
50 |
break |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
54 |
||
55 |
func (cl *Client) writeTransport(r io.Reader) { |
|
56 |
defer tryClose(r, cl.socket) |
|
57 |
p := make([]byte, 1024) |
|
58 |
for { |
|
59 |
nr, err := r.Read(p) |
|
60 |
if nr == 0 { |
|
61 |
log.Printf("write: %s", err.String()) |
|
62 |
break |
|
63 |
} |
|
64 |
nw, err := cl.socket.Write(p[:nr]) |
|
65 |
if nw < nr { |
|
66 |
log.Println("write: %s", err.String()) |
|
67 |
break |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
func readXml(r io.Reader, ch chan<- interface{}) { |
|
73 |
if debug { |
|
74 |
pr, pw := io.Pipe() |
|
75 |
go tee(r, pw, "S: ") |
|
76 |
r = pr |
|
77 |
} |
|
78 |
defer tryClose(r, ch) |
|
79 |
||
80 |
p := xml.NewParser(r) |
|
81 |
for { |
|
82 |
// Sniff the next token on the stream. |
|
83 |
t, err := p.Token() |
|
84 |
if t == nil { |
|
85 |
if err != os.EOF { |
|
86 |
log.Printf("read: %v", err) |
|
87 |
} |
|
88 |
break |
|
89 |
} |
|
90 |
var se xml.StartElement |
|
91 |
var ok bool |
|
92 |
if se, ok = t.(xml.StartElement) ; !ok { |
|
93 |
continue |
|
94 |
} |
|
95 |
||
96 |
// Allocate the appropriate structure for this token. |
|
97 |
var obj interface{} |
|
98 |
switch se.Name.Space + " " + se.Name.Local { |
|
99 |
case nsStream + " stream": |
|
100 |
st, err := parseStream(se) |
|
101 |
if err != nil { |
|
102 |
log.Printf("unmarshal stream: %v", |
|
103 |
err) |
|
104 |
break |
|
105 |
} |
|
106 |
ch <- st |
|
107 |
continue |
|
108 |
case "stream error", nsStream + " error": |
|
109 |
obj = &StreamError{} |
|
110 |
case nsStream + " features": |
|
111 |
obj = &Features{} |
|
112 |
case nsTLS + " proceed", nsTLS + " failure": |
|
113 |
obj = &starttls{} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
114 |
case nsSASL + " challenge", nsSASL + " failure", |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
115 |
nsSASL + " success": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
116 |
obj = &auth{} |
10 | 117 |
default: |
118 |
obj = &Unrecognized{} |
|
119 |
log.Printf("Ignoring unrecognized: %s %s\n", |
|
120 |
se.Name.Space, se.Name.Local) |
|
121 |
} |
|
122 |
||
123 |
// Read the complete XML stanza. |
|
124 |
err = p.Unmarshal(obj, &se) |
|
125 |
if err != nil { |
|
126 |
log.Printf("unmarshal: %v", err) |
|
127 |
break |
|
128 |
} |
|
129 |
||
130 |
// Put it on the channel. |
|
131 |
ch <- obj |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
func writeXml(w io.Writer, ch <-chan interface{}) { |
|
136 |
if debug { |
|
137 |
pr, pw := io.Pipe() |
|
138 |
go tee(pr, w, "C: ") |
|
139 |
w = pw |
|
140 |
} |
|
141 |
defer tryClose(w, ch) |
|
142 |
||
143 |
for obj := range ch { |
|
144 |
err := xml.Marshal(w, obj) |
|
145 |
if err != nil { |
|
146 |
log.Printf("write: %v", err) |
|
147 |
break |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
func writeText(w io.Writer, ch <-chan *string) { |
|
153 |
if debug { |
|
154 |
pr, pw := io.Pipe() |
|
155 |
go tee(pr, w, "C: ") |
|
156 |
w = pw |
|
157 |
} |
|
158 |
defer tryClose(w, ch) |
|
159 |
||
160 |
for str := range ch { |
|
161 |
_, err := w.Write([]byte(*str)) |
|
162 |
if err != nil { |
|
163 |
log.Printf("writeStr: %v", err) |
|
164 |
break |
|
165 |
} |
|
166 |
} |
|
167 |
} |
|
168 |
||
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
169 |
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- interface{}) { |
10 | 170 |
defer tryClose(srvIn, cliOut) |
171 |
||
172 |
for x := range srvIn { |
|
173 |
switch obj := x.(type) { |
|
174 |
case *Stream: |
|
175 |
handleStream(obj) |
|
176 |
case *Features: |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
177 |
cl.handleFeatures(obj) |
10 | 178 |
case *starttls: |
179 |
cl.handleTls(obj) |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
180 |
case *auth: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
181 |
cl.handleSasl(obj) |
10 | 182 |
default: |
183 |
cliOut <- x |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
func writeStream(srvOut chan<- interface{}, cliIn <-chan interface{}) { |
|
189 |
defer tryClose(srvOut, cliIn) |
|
190 |
||
191 |
for x := range cliIn { |
|
192 |
srvOut <- x |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
func handleStream(ss *Stream) { |
|
197 |
} |
|
198 |
||
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
199 |
func (cl *Client) handleFeatures(fe *Features) { |
10 | 200 |
if fe.Starttls != nil { |
201 |
start := &starttls{XMLName: xml.Name{Space: nsTLS, |
|
202 |
Local: "starttls"}} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
203 |
cl.xmlOut <- start |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
204 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
205 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
206 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
207 |
if len(fe.Mechanisms.Mechanism) > 0 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
208 |
cl.chooseSasl(fe) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
209 |
return |
10 | 210 |
} |
211 |
} |
|
212 |
||
213 |
// readTransport() is running concurrently. We need to stop it, |
|
214 |
// negotiate TLS, then start it again. It calls waitForSocket() in |
|
215 |
// its inner loop; see below. |
|
216 |
func (cl *Client) handleTls(t *starttls) { |
|
217 |
tcp := cl.socket |
|
218 |
||
219 |
// Set the socket to nil, and wait for the reader routine to |
|
220 |
// signal that it's paused. |
|
221 |
cl.socket = nil |
|
222 |
cl.socketSync.Add(1) |
|
223 |
cl.socketSync.Wait() |
|
224 |
||
225 |
// Negotiate TLS with the server. |
|
226 |
tls := tls.Client(tcp, nil) |
|
227 |
||
228 |
// Make the TLS connection available to the reader, and wait |
|
229 |
// for it to signal that it's working again. |
|
230 |
cl.socketSync.Add(1) |
|
231 |
cl.socket = tls |
|
232 |
cl.socketSync.Wait() |
|
233 |
||
234 |
// Reset the read timeout on the (underlying) socket so the |
|
235 |
// reader doesn't get woken up unnecessarily. |
|
236 |
tcp.SetReadTimeout(0) |
|
237 |
||
238 |
log.Println("TLS negotiation succeeded.") |
|
239 |
||
240 |
// Now re-send the initial handshake message to start the new |
|
241 |
// session. |
|
242 |
hsOut := &Stream{To: cl.Jid.Domain, Version: Version} |
|
243 |
cl.xmlOut <- hsOut |
|
244 |
} |
|
245 |
||
246 |
// Synchronize with handleTls(). Called from readTransport() when |
|
247 |
// cl.socket is nil. |
|
248 |
func (cl *Client) waitForSocket() { |
|
249 |
// Signal that we've stopped reading from the socket. |
|
250 |
cl.socketSync.Done() |
|
251 |
||
252 |
// Wait until the socket is available again. |
|
253 |
for cl.socket == nil { |
|
254 |
time.Sleep(1e8) |
|
255 |
} |
|
256 |
||
257 |
// Signal that we're going back to the read loop. |
|
258 |
cl.socketSync.Done() |
|
259 |
} |
|
11
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
260 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
261 |
func (cl *Client) chooseSasl(fe *Features) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
262 |
var digestMd5 bool |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
263 |
for _, m := range(fe.Mechanisms.Mechanism) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
264 |
switch strings.ToLower(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
265 |
case "digest-md5": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
266 |
digestMd5 = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
267 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
268 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
269 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
270 |
if digestMd5 { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
271 |
auth := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
272 |
"auth"}, Mechanism: "DIGEST-MD5"} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
273 |
cl.xmlOut <- auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
274 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
275 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
276 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
277 |
func (cl *Client) handleSasl(srv *auth) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
278 |
switch strings.ToLower(srv.XMLName.Local) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
279 |
case "challenge": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
280 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
281 |
str, err := b64.DecodeString(srv.Chardata) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
282 |
if err != nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
283 |
log.Printf("SASL challenge decode: %s", |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
284 |
err.String()) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
285 |
return; |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
286 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
287 |
srvMap := parseSasl(string(str)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
288 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
289 |
if cl.saslExpected == "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
290 |
cl.saslDigest1(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
291 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
292 |
cl.saslDigest2(srvMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
293 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
294 |
case "failure": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
295 |
log.Println("SASL authentication failed") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
296 |
case "success": |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
297 |
log.Println("SASL authentication succeeded") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
298 |
ss := &Stream{To: cl.Jid.Domain, Version: Version} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
299 |
cl.xmlOut <- ss |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
300 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
301 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
302 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
303 |
func (cl *Client) saslDigest1(srvMap map[string] string) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
304 |
// Make sure it supports qop=auth |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
305 |
var hasAuth bool |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
306 |
for _, qop := range(strings.Fields(srvMap["qop"])) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
307 |
if qop == "auth" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
308 |
hasAuth = true |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
309 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
310 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
311 |
if !hasAuth { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
312 |
log.Println("Server doesn't support SASL auth") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
313 |
return; |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
314 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
315 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
316 |
// Pick a realm. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
317 |
var realm string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
318 |
if srvMap["realm"] != "" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
319 |
realm = strings.Fields(srvMap["realm"])[0] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
320 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
321 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
322 |
passwd := cl.password |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
323 |
nonce := srvMap["nonce"] |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
324 |
digestUri := "xmpp/" + cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
325 |
nonceCount := int32(1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
326 |
nonceCountStr := fmt.Sprintf("%08x", nonceCount) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
327 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
328 |
// Begin building the response. Username is |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
329 |
// user@domain or just domain. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
330 |
var username string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
331 |
if cl.Jid.Node == nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
332 |
username = cl.Jid.Domain |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
333 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
334 |
username = *cl.Jid.Node |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
335 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
336 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
337 |
// Generate our own nonce from random data. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
338 |
randSize := big.NewInt(0) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
339 |
randSize.Lsh(big.NewInt(1), 64) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
340 |
cnonce, err := rand.Int(rand.Reader, randSize) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
341 |
if err != nil { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
342 |
log.Println("SASL rand: %s", err.String()) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
343 |
return |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
344 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
345 |
cnonceStr := fmt.Sprintf("%016x", cnonce) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
346 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
347 |
/* Now encode the actual password response, as well as the |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
348 |
* expected next challenge from the server. */ |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
349 |
response := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
350 |
cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
351 |
next := saslDigestResponse(username, realm, passwd, nonce, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
352 |
cnonceStr, "", digestUri, nonceCountStr) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
353 |
cl.saslExpected = next |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
354 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
355 |
// Build the map which will be encoded. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
356 |
clMap := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
357 |
clMap["realm"] = `"` + realm + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
358 |
clMap["username"] = `"` + username + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
359 |
clMap["nonce"] = `"` + nonce + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
360 |
clMap["cnonce"] = `"` + cnonceStr + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
361 |
clMap["nc"] = nonceCountStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
362 |
clMap["qop"] = "auth" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
363 |
clMap["digest-uri"] = `"` + digestUri + `"` |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
364 |
clMap["response"] = response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
365 |
if srvMap["charset"] == "utf-8" { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
366 |
clMap["charset"] = "utf-8" |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
367 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
368 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
369 |
// Encode the map and send it. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
370 |
clStr := packSasl(clMap) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
371 |
b64 := base64.StdEncoding |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
372 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
373 |
"response"}, Chardata: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
374 |
b64.EncodeToString([]byte(clStr))} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
375 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
376 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
377 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
378 |
func (cl *Client) saslDigest2(srvMap map[string] string) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
379 |
if cl.saslExpected == srvMap["rspauth"] { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
380 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
381 |
"response"}} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
382 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
383 |
} else { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
384 |
clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
385 |
"failure"}, Any: |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
386 |
&Unrecognized{XMLName: xml.Name{Space: nsSASL, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
387 |
Local: "abort"}}} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
388 |
cl.xmlOut <- clObj |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
389 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
390 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
391 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
392 |
// Takes a string like `key1=value1,key2="value2"...` and returns a |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
393 |
// key/value map. |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
394 |
func parseSasl(in string) map[string]string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
395 |
re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
396 |
strs := re.FindAllStringSubmatch(in, -1) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
397 |
m := make(map[string]string) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
398 |
for _, pair := range(strs) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
399 |
key := strings.ToLower(string(pair[1])) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
400 |
value := string(pair[2]) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
401 |
m[key] = value |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
402 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
403 |
return m |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
404 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
405 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
406 |
func packSasl(m map[string]string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
407 |
var terms []string |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
408 |
for key, value := range(m) { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
409 |
if key == "" || value == "" || value == `""` { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
410 |
continue |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
411 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
412 |
terms = append(terms, key + "=" + value) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
413 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
414 |
return strings.Join(terms, ",") |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
415 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
416 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
417 |
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr, |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
418 |
authenticate, digestUri, nonceCountStr string) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
419 |
h := func(text string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
420 |
h := md5.New() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
421 |
h.Write([]byte(text)) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
422 |
return h.Sum() |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
423 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
424 |
hex := func(bytes []byte) string { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
425 |
return fmt.Sprintf("%x", bytes) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
426 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
427 |
kd := func(secret, data string) []byte { |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
428 |
return h(secret + ":" + data) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
429 |
} |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
430 |
|
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
431 |
a1 := string(h(username + ":" + realm + ":" + passwd)) + ":" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
432 |
nonce + ":" + cnonceStr |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
433 |
a2 := authenticate + ":" + digestUri |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
434 |
response := hex(kd(hex(h(a1)), nonce + ":" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
435 |
nonceCountStr + ":" + cnonceStr + ":auth:" + |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
436 |
hex(h(a2)))) |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
437 |
return response |
48be1ae93fd4
Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents:
10
diff
changeset
|
438 |
} |