Debian Codegraph @

control/cmd_authenticate.go

raw symbols SCIP
1 package control
2
3 import (
4 "crypto/hmac"
5 "crypto/rand"
6 "crypto/sha256"
7 "encoding/hex"
8 "io/ioutil"
9 "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 *Conn) Authenticate(password string) error {
18 if c.Authenticated {
19 return nil
20 }
21 // Determine the supported authentication methods, and the cookie path.
22 pi, err := c.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.HasAuthMethod("NULL") {
29 // No auth bytes
30 } else if pi.HasAuthMethod("SAFECOOKIE") {
31 if pi.CookieFile == "" {
32 return c.protoErr("Invalid (empty) COOKIEFILE")
33 }
34 cookie, err := ioutil.ReadFile(pi.CookieFile)
35 if err != nil {
36 return c.protoErr("Failed to read COOKIEFILE: %v", err)
37 } else if len(cookie) != 32 {
38 return c.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 := rand.Read(clientNonce[:]); err != nil {
44 return c.protoErr("Failed to generate clientNonce: %v", err)
45 }
46 resp, err := c.SendRequest("AUTHCHALLENGE %s %s", "SAFECOOKIE", hex.EncodeToString(clientNonce[:]))
47 if err != nil {
48 return err
49 }
50 splitResp := strings.Split(resp.Reply, " ")
51 if len(splitResp) != 3 || !strings.HasPrefix(splitResp[1], "SERVERHASH=") ||
52 !strings.HasPrefix(splitResp[2], "SERVERNONCE=") {
53 return c.protoErr("Invalid AUTHCHALLENGE response")
54 }
55 serverHash, err := hex.DecodeString(splitResp[1][11:])
56 if err != nil {
57 return c.protoErr("Failed to decode ServerHash: %v", err)
58 }
59 if len(serverHash) != 32 {
60 return c.protoErr("Invalid ServerHash length: %d", len(serverHash))
61 }
62 serverNonce, err := hex.DecodeString(splitResp[2][12:])
63 if err != nil {
64 return c.protoErr("Failed to decode ServerNonce: %v", err)
65 }
66 if len(serverNonce) != 32 {
67 return c.protoErr("Invalid ServerNonce length: %d", len(serverNonce))
68 }
69
70 // Validate the ServerHash.
71 m := hmac.New(sha256.New, []byte("Tor safe cookie authentication server-to-controller hash"))
72 m.Write(cookie)
73 m.Write(clientNonce[:])
74 m.Write(serverNonce)
75 dervServerHash := m.Sum(nil)
76 if !hmac.Equal(serverHash, dervServerHash) {
77 return c.protoErr("invalid ServerHash: mismatch")
78 }
79
80 // Calculate the ClientHash, and issue the AUTHENTICATE.
81 m = hmac.New(sha256.New, []byte("Tor safe cookie authentication controller-to-server hash"))
82 m.Write(cookie)
83 m.Write(clientNonce[:])
84 m.Write(serverNonce)
85 authBytes = m.Sum(nil)
86 } else if pi.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.protoErr("password auth needs a password")
91 }
92 authBytes = []byte(password)
93 } else {
94 return c.protoErr("No supported authentication methods")
95 }
96 // Send it
97 if err = c.sendAuthenticate(authBytes); err == nil {
98 c.Authenticated = true
99 }
100 return err
101 }
102
103 func (c *Conn) sendAuthenticate(byts []byte) error {
104 if len(byts) == 0 {
105 return c.sendRequestIgnoreResponse("AUTHENTICATE")
106 }
107 return c.sendRequestIgnoreResponse("AUTHENTICATE %v", hex.EncodeToString(byts))
108 }