forked from saily/vnc2video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_vencryptplain.go
109 lines (98 loc) · 2.64 KB
/
security_vencryptplain.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
package vnc2video
import (
"bytes"
"encoding/binary"
"fmt"
)
func (*ClientAuthVeNCrypt02Plain) Type() SecurityType {
return SecTypeVeNCrypt
}
func (*ClientAuthVeNCrypt02Plain) SubType() SecuritySubType {
return SecSubTypeVeNCrypt02Plain
}
// ClientAuthVeNCryptPlain see https://www.berrange.com/~dan/vencrypt.txt
type ClientAuthVeNCrypt02Plain struct {
Username []byte
Password []byte
}
func (auth *ClientAuthVeNCrypt02Plain) Auth(c Conn) error {
if err := binary.Write(c, binary.BigEndian, []uint8{0, 2}); err != nil {
return err
}
if err := c.Flush(); err != nil {
return err
}
var (
major, minor uint8
)
if err := binary.Read(c, binary.BigEndian, &major); err != nil {
return err
}
if err := binary.Read(c, binary.BigEndian, &minor); err != nil {
return err
}
res := uint8(1)
if major == 0 && minor == 2 {
res = uint8(0)
}
if err := binary.Write(c, binary.BigEndian, res); err != nil {
return err
}
c.Flush()
if err := binary.Write(c, binary.BigEndian, uint8(1)); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, auth.SubType()); err != nil {
return err
}
if err := c.Flush(); err != nil {
return err
}
var secType SecuritySubType
if err := binary.Read(c, binary.BigEndian, &secType); err != nil {
return err
}
if secType != auth.SubType() {
binary.Write(c, binary.BigEndian, uint8(1))
c.Flush()
return fmt.Errorf("invalid sectype")
}
if len(auth.Password) == 0 || len(auth.Username) == 0 {
return fmt.Errorf("Security Handshake failed; no username and/or password provided for VeNCryptAuth.")
}
/*
if err := binary.Write(c, binary.BigEndian, uint32(len(auth.Username))); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, uint32(len(auth.Password))); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, auth.Username); err != nil {
return err
}
if err := binary.Write(c, binary.BigEndian, auth.Password); err != nil {
return err
}
*/
var (
uLength, pLength uint32
)
if err := binary.Read(c, binary.BigEndian, &uLength); err != nil {
return err
}
if err := binary.Read(c, binary.BigEndian, &pLength); err != nil {
return err
}
username := make([]byte, uLength)
password := make([]byte, pLength)
if err := binary.Read(c, binary.BigEndian, &username); err != nil {
return err
}
if err := binary.Read(c, binary.BigEndian, &password); err != nil {
return err
}
if !bytes.Equal(auth.Username, username) || !bytes.Equal(auth.Password, password) {
return fmt.Errorf("invalid username/password")
}
return nil
}