Debian Codegraph @

control/cmd_authenticate.go

raw symbols SCIP
1 package controlscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/
2
3 import (
4 "crypto/hmacscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/"
5 "crypto/randscip-go gomod github.com/golang/go/src go1.15 `crypto/rand`/"
6 "crypto/sha256scip-go gomod github.com/golang/go/src go1.15 `crypto/sha256`/"
7 "encoding/hexscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/"
8 "io/ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/"
9 "stringsscip-go gomod github.com/golang/go/src go1.15 strings/"
10 )
11
12 // Authenticate authenticates with the Tor instance using the "best" possible
13 // authentication method if not already authenticated and sets the Authenticated
14 // field. The password argument is optional, and will only be used if the
15 // "SAFECOOKIE" and "NULL" authentication methods are not available and
16 // "HASHEDPASSWORD" is.
17 func (c *Connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#) Authenticatescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Authenticate().(password string) error {
18 if c.Authenticatedscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Authenticated. {
19 return nil
20 }
21 // Determine the supported authentication methods, and the cookie path.
22 pi, err := c.ProtocolInfoscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#ProtocolInfo().()
23 if err != nil {
24 return err
25 }
26 // Get the bytes to pass to with authenticate
27 var authBytes []byte
28 if pi.HasAuthMethodscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ProtocolInfo#HasAuthMethod().("NULL") {
29 // No auth bytes
30 } else if pi.HasAuthMethodscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ProtocolInfo#HasAuthMethod().("SAFECOOKIE") {
31 if pi.CookieFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ProtocolInfo#CookieFile. == "" {
32 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid (empty) COOKIEFILE")
33 }
34 cookie, err := ioutilscip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.15 `io/ioutil`/ReadFile().(pi.CookieFilescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ProtocolInfo#CookieFile.)
35 if err != nil {
36 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Failed to read COOKIEFILE: %v", err)
37 } else if len(cookie) != 32 {
38 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid cookie file length: %v", len(cookie))
39 }
40
41 // Send an AUTHCHALLENGE command, and parse the response.
42 var clientNonce [32]byte
43 if _, err := randscip-go gomod github.com/golang/go/src go1.15 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.15 `crypto/rand`/Read().(clientNonce[:]); err != nil {
44 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Failed to generate clientNonce: %v", err)
45 }
46 resp, err := c.SendRequestscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#SendRequest().("AUTHCHALLENGE %s %s", "SAFECOOKIE", hexscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/EncodeToString().(clientNonce[:]))
47 if err != nil {
48 return err
49 }
50 splitResp := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.Splitscip-go gomod github.com/golang/go/src go1.15 strings/Split().(resp.Replyscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Response#Reply., " ")
51 if len(splitResp) != 3 || !stringsscip-go gomod github.com/golang/go/src go1.15 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.15 strings/HasPrefix().(splitResp[1], "SERVERHASH=") ||
52 !stringsscip-go gomod github.com/golang/go/src go1.15 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.15 strings/HasPrefix().(splitResp[2], "SERVERNONCE=") {
53 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid AUTHCHALLENGE response")
54 }
55 serverHash, err := hexscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/DecodeString().(splitResp[1][11:])
56 if err != nil {
57 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Failed to decode ServerHash: %v", err)
58 }
59 if len(serverHash) != 32 {
60 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid ServerHash length: %d", len(serverHash))
61 }
62 serverNonce, err := hexscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/DecodeString().(splitResp[2][12:])
63 if err != nil {
64 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Failed to decode ServerNonce: %v", err)
65 }
66 if len(serverNonce) != 32 {
67 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("Invalid ServerNonce length: %d", len(serverNonce))
68 }
69
70 // Validate the ServerHash.
71 m := hmacscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/.Newscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/New().(sha256scip-go gomod github.com/golang/go/src go1.15 `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src go1.15 `crypto/sha256`/New()., []byte("Tor safe cookie authentication server-to-controller hash"))
72 m.Writescip-go gomod github.com/golang/go/src go1.15 io/Writer#Write().(cookie)
73 m.Writescip-go gomod github.com/golang/go/src go1.15 io/Writer#Write().(clientNonce[:])
74 m.Writescip-go gomod github.com/golang/go/src go1.15 io/Writer#Write().(serverNonce)
75 dervServerHash := m.Sumscip-go gomod github.com/golang/go/src go1.15 hash/Hash#Sum().(nil)
76 if !hmacscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/.Equalscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/Equal().(serverHash, dervServerHash) {
77 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("invalid ServerHash: mismatch")
78 }
79
80 // Calculate the ClientHash, and issue the AUTHENTICATE.
81 m = hmacscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/.Newscip-go gomod github.com/golang/go/src go1.15 `crypto/hmac`/New().(sha256scip-go gomod github.com/golang/go/src go1.15 `crypto/sha256`/.Newscip-go gomod github.com/golang/go/src go1.15 `crypto/sha256`/New()., []byte("Tor safe cookie authentication controller-to-server hash"))
82 m.Writescip-go gomod github.com/golang/go/src go1.15 io/Writer#Write().(cookie)
83 m.Writescip-go gomod github.com/golang/go/src go1.15 io/Writer#Write().(clientNonce[:])
84 m.Writescip-go gomod github.com/golang/go/src go1.15 io/Writer#Write().(serverNonce)
85 authBytes = m.Sumscip-go gomod github.com/golang/go/src go1.15 hash/Hash#Sum().(nil)
86 } else if pi.HasAuthMethodscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/ProtocolInfo#HasAuthMethod().("HASHEDPASSWORD") {
87 // Despite the name HASHEDPASSWORD, the raw password is actually sent. According to the code, this can either be
88 // a QuotedString, or base16 encoded, so go with the later since it's easier to handle.
89 if password == "" {
90 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("password auth needs a password")
91 }
92 authBytes = []byte(password)
93 } else {
94 return c.protoErrscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#protoErr().("No supported authentication methods")
95 }
96 // Send it
97 if err = c.sendAuthenticatescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#sendAuthenticate().(authBytes); err == nil {
98 c.Authenticatedscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#Authenticated. = true
99 }
100 return err
101 }
102
103 func (c *Connscip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#) sendAuthenticatescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#sendAuthenticate().(byts []byte) error {
104 if len(byts) == 0 {
105 return c.sendRequestIgnoreResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#sendRequestIgnoreResponse().("AUTHENTICATE")
106 }
107 return c.sendRequestIgnoreResponsescip-go gomod github.com/cretz/bine . `github.com/cretz/bine/control`/Conn#sendRequestIgnoreResponse().("AUTHENTICATE %v", hexscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src go1.15 `encoding/hex`/EncodeToString().(byts))
108 }