forked from mitchellh/go-vnc
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhandshake.go
247 lines (211 loc) · 5.68 KB
/
handshake.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
244
245
246
247
// Implementation of RFC 6143 §7.1 Handshake Messages.
package vnc
import (
"fmt"
"github.com/golang/glog"
"github.com/kward/go-vnc/logging"
"golang.org/x/net/context"
)
const pvLen = 12 // ProtocolVersion message length.
func parseProtocolVersion(pv []byte) (uint, uint, error) {
var major, minor uint
if len(pv) < pvLen {
return 0, 0, fmt.Errorf("ProtocolVersion message too short (%v < %v)", len(pv), pvLen)
}
l, err := fmt.Sscanf(string(pv), "RFB %d.%d\n", &major, &minor)
if l != 2 {
return 0, 0, fmt.Errorf("error parsing ProtocolVersion.")
}
if err != nil {
return 0, 0, err
}
return major, minor, nil
}
const (
// Client ProtocolVersions.
PROTO_VERS_UNSUP = "UNSUPPORTED"
PROTO_VERS_3_3 = "RFB 003.003\n"
PROTO_VERS_3_8 = "RFB 003.008\n"
)
// protocolVersionHandshake implements §7.1.1 ProtocolVersion Handshake.
func (c *ClientConn) protocolVersionHandshake(ctx context.Context) error {
if logging.V(logging.FnDeclLevel) {
glog.Info(logging.FnName())
}
var protocolVersion [pvLen]byte
// Read the ProtocolVersion message sent by the server.
if err := c.receive(&protocolVersion); err != nil {
return err
}
if logging.V(logging.ResultLevel) {
glog.Infof("protocolVersion: %s", protocolVersion)
}
major, minor, err := parseProtocolVersion(protocolVersion[:])
if err != nil {
return err
}
pv := PROTO_VERS_UNSUP
if major == 3 {
if minor >= 8 {
pv = PROTO_VERS_3_8
} else if minor >= 3 {
pv = PROTO_VERS_3_3
}
}
if pv == PROTO_VERS_UNSUP {
return NewVNCError(fmt.Sprintf("ProtocolVersion handshake failed; unsupported version '%v'", string(protocolVersion[:])))
}
if mpv := ctx.Value("vnc_max_proto_version"); mpv != nil && mpv != "" {
switch mpv {
case "3.3":
pv = PROTO_VERS_3_3
case "3.8":
pv = PROTO_VERS_3_8
}
}
if logging.V(logging.ResultLevel) {
glog.Infof("supported protocolVersion: %s", pv)
}
c.protocolVersion = pv
// Respond with the version we will support
if err = c.send([]byte(pv)); err != nil {
return err
}
return nil
}
// securityHandshake implements §7.1.2 Security Handshake.
func (c *ClientConn) securityHandshake() error {
if logging.V(logging.FnDeclLevel) {
glog.Infof(logging.FnName())
}
switch c.protocolVersion {
case PROTO_VERS_3_3:
if err := c.securityHandshake33(); err != nil {
return err
}
case PROTO_VERS_3_8:
if err := c.securityHandshake38(); err != nil {
return err
}
default:
return NewVNCError(fmt.Sprintf("Security handshake failed; unsupported protocol"))
}
return nil
}
func (c *ClientConn) securityHandshake33() error {
if logging.V(logging.FnDeclLevel) {
glog.Infof(logging.FnName())
}
var secType uint32
if err := c.receive(&secType); err != nil {
return err
}
var auth ClientAuth
switch uint8(secType) { // 3.3 uses uint32, but 3.8 uses uint8. Unify on 3.8.
case secTypeInvalid: // Connection failed.
reason, err := c.readErrorReason()
if err != nil {
return err
}
return NewVNCError(fmt.Sprintf("Security handshake failed; connection failed: %s", reason))
case secTypeNone:
auth = &ClientAuthNone{}
case secTypeVNCAuth:
auth = &ClientAuthVNC{c.config.Password}
default:
return NewVNCError(fmt.Sprintf("Security handshake failed; invalid security type: %v", secType))
}
c.config.secType = auth.SecurityType()
if err := auth.Handshake(c); err != nil {
return err
}
return nil
}
func (c *ClientConn) securityHandshake38() error {
if logging.V(logging.FnDeclLevel) {
glog.Info(logging.FnName())
}
// Determine server supported security types.
var numSecurityTypes uint8
if err := c.receive(&numSecurityTypes); err != nil {
return err
}
if numSecurityTypes == 0 {
reason, err := c.readErrorReason()
if err != nil {
return err
}
return NewVNCError(fmt.Sprintf("Security handshake failed; no security types: %v", reason))
}
securityTypes := make([]uint8, numSecurityTypes)
if err := c.receive(&securityTypes); err != nil {
return err
}
if logging.V(logging.ResultLevel) {
glog.Infof("securityTypes: %v", securityTypes)
}
// Choose client security type.
// TODO(kward): try "better" security types first.
var auth ClientAuth
FindAuth:
for _, securityType := range securityTypes {
for _, a := range c.config.Auth {
if a.SecurityType() == securityType {
// We use the first matching supported authentication.
auth = a
break FindAuth
}
}
}
if auth == nil {
return NewVNCError(fmt.Sprintf("Security handshake failed; no suitable auth schemes found; server supports: %#v", securityTypes))
}
if err := c.send(auth.SecurityType()); err != nil {
return err
}
c.config.secType = auth.SecurityType()
if err := auth.Handshake(c); err != nil {
return err
}
return nil
}
// securityResultHandshake implements §7.1.3 SecurityResult Handshake.
func (c *ClientConn) securityResultHandshake() error {
if logging.V(logging.FnDeclLevel) {
glog.Info(logging.FnName())
}
if c.config.secType == secTypeNone {
return nil
}
var securityResult uint32
if err := c.receive(&securityResult); err != nil {
return err
}
switch securityResult {
case 0:
case 1:
reason, err := c.readErrorReason()
if err != nil {
return err
}
return NewVNCError(fmt.Sprintf("SecurityResult handshake failed: %s", reason))
default:
return NewVNCError(fmt.Sprintf("Invalid SecurityResult status: %v", securityResult))
}
return nil
}
// TODO(kward): need a context for timeout
func (c *ClientConn) readErrorReason() (string, error) {
if logging.V(logging.FnDeclLevel) {
glog.Info(logging.FnName())
}
var reasonLen uint32
if err := c.receive(&reasonLen); err != nil {
return "", err
}
reason := make([]uint8, reasonLen)
if err := c.receive(&reason); err != nil {
return "", err
}
return string(reason), nil
}