Debian Codegraph @

control/cmd_onion.go

raw symbols SCIP
1 package control
2
3 import (
4 "crypto/rsa"
5 "crypto/x509"
6 "encoding/base64"
7 "fmt"
8 "strconv"
9 "strings"
10
11 "github.com/cretz/bine/torutil"
12 "github.com/cretz/bine/torutil/ed25519"
13 )
14
15 // KeyType is a key type for Key in AddOnion.
16 type KeyType string
17
18 const (
19 // KeyTypeNew is NEW.
20 KeyTypeNew KeyType = "NEW"
21 // KeyTypeRSA1024 is RSA1024.
22 KeyTypeRSA1024 KeyType = "RSA1024"
23 // KeyTypeED25519V3 is ED25519-V3.
24 KeyTypeED25519V3 KeyType = "ED25519-V3"
25 )
26
27 // KeyAlgo is a key algorithm for GenKey on AddOnion.
28 type KeyAlgo string
29
30 const (
31 // KeyAlgoBest is BEST.
32 KeyAlgoBest KeyAlgo = "BEST"
33 // KeyAlgoRSA1024 is RSA1024.
34 KeyAlgoRSA1024 KeyAlgo = "RSA1024"
35 // KeyAlgoED25519V3 is ED25519-V3.
36 KeyAlgoED25519V3 KeyAlgo = "ED25519-V3"
37 )
38
39 // Key is a type of key to use for AddOnion. Implementations include GenKey,
40 // RSAKey, and ED25519Key.
41 type Key interface {
42 // Type is the KeyType for AddOnion.
43 Type() KeyType
44 // Blob is the serialized key for AddOnion.
45 Blob() string
46 }
47
48 // KeyFromString creates a Key for AddOnion based on a response string.
49 func KeyFromString(str string) (Key, error) {
50 typ, blob, _ := torutil.PartitionString(str, ':')
51 switch KeyType(typ) {
52 case KeyTypeNew:
53 return GenKeyFromBlob(blob), nil
54 case KeyTypeRSA1024:
55 return RSA1024KeyFromBlob(blob)
56 case KeyTypeED25519V3:
57 return ED25519KeyFromBlob(blob)
58 default:
59 return nil, fmt.Errorf("Unrecognized key type: %v", typ)
60 }
61 }
62
63 // GenKey is a Key for AddOnion that asks Tor to generate a key for the given
64 // algorithm.
65 type GenKey KeyAlgo
66
67 // GenKeyFromBlob creates a GenKey for the given response blob which is a
68 // KeyAlgo.
69 func GenKeyFromBlob(blob string) GenKey { return GenKey(KeyAlgo(blob)) }
70
71 // Type implements Key.Type.
72 func (GenKey) Type() KeyType { return KeyTypeNew }
73
74 // Blob implements Key.Blob.
75 func (g GenKey) Blob() string { return string(g) }
76
77 // RSAKey is a Key for AddOnion that is a RSA-1024 key (i.e. v2).
78 type RSAKey struct{ *rsa.PrivateKey }
79
80 // RSA1024KeyFromBlob creates a RSAKey for the given response blob.
81 func RSA1024KeyFromBlob(blob string) (*RSAKey, error) {
82 byts, err := base64.StdEncoding.DecodeString(blob)
83 if err != nil {
84 return nil, err
85 }
86 rsaKey, err := x509.ParsePKCS1PrivateKey(byts)
87 if err != nil {
88 return nil, err
89 }
90 return &RSAKey{rsaKey}, nil
91 }
92
93 // Type implements Key.Type.
94 func (*RSAKey) Type() KeyType { return KeyTypeRSA1024 }
95
96 // Blob implements Key.Blob.
97 func (r *RSAKey) Blob() string {
98 return base64.StdEncoding.EncodeToString(x509.MarshalPKCS1PrivateKey(r.PrivateKey))
99 }
100
101 // ED25519Key is a Key for AddOnion that is a ed25519 key (i.e. v3).
102 type ED25519Key struct{ ed25519.KeyPair }
103
104 // ED25519KeyFromBlob creates a ED25519Key for the given response blob.
105 func ED25519KeyFromBlob(blob string) (*ED25519Key, error) {
106 byts, err := base64.StdEncoding.DecodeString(blob)
107 if err != nil {
108 return nil, err
109 }
110 return &ED25519Key{ed25519.PrivateKey(byts).KeyPair()}, nil
111 }
112
113 // Type implements Key.Type.
114 func (*ED25519Key) Type() KeyType { return KeyTypeED25519V3 }
115
116 // Blob implements Key.Blob.
117 func (e *ED25519Key) Blob() string { return base64.StdEncoding.EncodeToString(e.PrivateKey()) }
118
119 // AddOnionRequest is a set of request params for AddOnion.
120 type AddOnionRequest struct {
121 // Key is the key to use or GenKey if Tor should generate it.
122 Key Key
123 // Flags are ADD_ONION flags.
124 Flags []string
125 // MaxStreams is ADD_ONION MaxStreams.
126 MaxStreams int
127 // Ports are ADD_ONION Port values. Key is virtual port, Val is target
128 // port (or can be empty to use virtual port).
129 Ports []*KeyVal
130 // ClientAuths are ADD_ONION ClientAuth values. If value is empty string,
131 // Tor will generate the password.
132 ClientAuths map[string]string
133 }
134
135 // AddOnionResponse is the response for AddOnion.
136 type AddOnionResponse struct {
137 // ServiceID is the ADD_ONION response ServiceID value.
138 ServiceID string
139 // Key is the ADD_ONION response PrivateKey value.
140 Key Key
141 // ClientAuths are the ADD_ONION response ClientAuth values.
142 ClientAuths map[string]string
143 // RawResponse is the raw ADD_ONION response.
144 RawResponse *Response
145 }
146
147 // AddOnion invokes ADD_ONION and returns its response.
148 func (c *Conn) AddOnion(req *AddOnionRequest) (*AddOnionResponse, error) {
149 // Build command
150 if req.Key == nil {
151 return nil, c.protoErr("Key required")
152 }
153 cmd := "ADD_ONION " + string(req.Key.Type()) + ":" + req.Key.Blob()
154 if len(req.Flags) > 0 {
155 cmd += " Flags=" + strings.Join(req.Flags, ",")
156 }
157 if req.MaxStreams > 0 {
158 cmd += " MaxStreams=" + strconv.Itoa(req.MaxStreams)
159 }
160 for _, port := range req.Ports {
161 cmd += " Port=" + port.Key
162 if port.Val != "" {
163 cmd += "," + port.Val
164 }
165 }
166 for name, blob := range req.ClientAuths {
167 cmd += " ClientAuth=" + name
168 if blob != "" {
169 cmd += ":" + blob
170 }
171 }
172 // Invoke and read response
173 resp, err := c.SendRequest(cmd)
174 if err != nil {
175 return nil, err
176 }
177 ret := &AddOnionResponse{RawResponse: resp}
178 for _, data := range resp.Data {
179 key, val, _ := torutil.PartitionString(data, '=')
180 switch key {
181 case "ServiceID":
182 ret.ServiceID = val
183 case "PrivateKey":
184 if ret.Key, err = KeyFromString(val); err != nil {
185 return nil, err
186 }
187 case "ClientAuth":
188 name, pass, _ := torutil.PartitionString(val, ':')
189 if ret.ClientAuths == nil {
190 ret.ClientAuths = map[string]string{}
191 }
192 ret.ClientAuths[name] = pass
193 }
194 }
195 return ret, nil
196 }
197
198 // DelOnion invokes DELONION.
199 func (c *Conn) DelOnion(serviceID string) error {
200 return c.sendRequestIgnoreResponse("DEL_ONION %v", serviceID)
201 }