forked from jedisct1/piknik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
210 lines (196 loc) · 5.71 KB
/
client.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
package main
import (
"bufio"
"bytes"
"crypto/rand"
"crypto/subtle"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"os"
"syscall"
"time"
"github.com/yawning/chacha20"
"golang.org/x/crypto/ed25519"
)
// DefaultClientVersion - Default client version
const DefaultClientVersion = byte(5)
// Client - Client data
type Client struct {
conf Conf
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
version byte
}
func (client *Client) copyOperation(h1 []byte) {
ts := make([]byte, 8)
binary.LittleEndian.PutUint64(ts, uint64(time.Now().Unix()))
var contentWithNonceBuf bytes.Buffer
contentWithNonceBuf.Grow(24 + bytes.MinRead)
nonce := make([]byte, 24)
if _, err := rand.Read(nonce); err != nil {
log.Fatal(err)
}
contentWithNonceBuf.Write(nonce)
conf, reader, writer := client.conf, client.reader, client.writer
_, err := contentWithNonceBuf.ReadFrom(os.Stdin)
if err != nil {
log.Fatal(err)
}
contentWithNonce := contentWithNonceBuf.Bytes()
cipher, err := chacha20.NewCipher(conf.EncryptSk, nonce)
if err != nil {
log.Fatal(err)
}
opcode := byte('S')
cipher.XORKeyStream(contentWithNonce[24:], contentWithNonce[24:])
signature := ed25519.Sign(conf.SignSk, contentWithNonce)
client.conn.SetDeadline(time.Now().Add(conf.DataTimeout))
h2 := auth2store(conf, client.version, h1, opcode, conf.EncryptSkID, ts, signature)
writer.WriteByte(opcode)
writer.Write(h2)
ciphertextWithNonceLen := uint64(len(contentWithNonce))
binary.Write(writer, binary.LittleEndian, ciphertextWithNonceLen)
writer.Write(conf.EncryptSkID)
writer.Write(ts)
writer.Write(signature)
writer.Write(contentWithNonce)
if err = writer.Flush(); err != nil {
log.Fatal(err)
}
rbuf := make([]byte, 32)
if _, err = io.ReadFull(reader, rbuf); err != nil {
if err == io.ErrUnexpectedEOF {
log.Fatal("The server may be running an incompatible version")
} else {
log.Fatal(err)
}
}
h3 := rbuf
wh3 := auth3store(conf, h2)
if subtle.ConstantTimeCompare(wh3, h3) != 1 {
log.Fatal("Incorrect authentication code")
}
if IsTerminal(int(syscall.Stderr)) {
os.Stderr.WriteString("Sent\n")
}
}
func (client *Client) pasteOperation(h1 []byte, isMove bool) {
conf, reader, writer := client.conf, client.reader, client.writer
opcode := byte('G')
if isMove {
opcode = byte('M')
}
h2 := auth2get(conf, client.version, h1, opcode)
writer.WriteByte(opcode)
writer.Write(h2)
if err := writer.Flush(); err != nil {
log.Fatal(err)
}
rbuf := make([]byte, 120)
if nbread, err := io.ReadFull(reader, rbuf); err != nil {
if err == io.ErrUnexpectedEOF {
if nbread < 80 {
log.Fatal("The clipboard might be empty")
} else {
log.Fatal("The server may be running an incompatible version")
}
} else {
log.Fatal(err)
}
}
h3 := rbuf[0:32]
ciphertextWithNonceLen := binary.LittleEndian.Uint64(rbuf[32:40])
encryptSkID := rbuf[40:48]
ts := rbuf[48:56]
signature := rbuf[56:120]
wh3 := auth3get(conf, client.version, h2, encryptSkID, ts, signature)
if subtle.ConstantTimeCompare(wh3, h3) != 1 {
log.Fatal("Incorrect authentication code")
}
elapsed := time.Since(time.Unix(int64(binary.LittleEndian.Uint64(ts)), 0))
if elapsed >= conf.TTL {
log.Fatal("Clipboard content is too old")
}
if bytes.Equal(conf.EncryptSkID, encryptSkID) == false {
wEncryptSkIDStr := binary.LittleEndian.Uint64(conf.EncryptSkID)
encryptSkIDStr := binary.LittleEndian.Uint64(encryptSkID)
log.Fatal(fmt.Sprintf("Configured key ID is %v but content was encrypted using key ID %v",
wEncryptSkIDStr, encryptSkIDStr))
}
ciphertextWithNonce := make([]byte, ciphertextWithNonceLen)
client.conn.SetDeadline(time.Now().Add(conf.DataTimeout))
if _, err := io.ReadFull(reader, ciphertextWithNonce); err != nil {
if err == io.ErrUnexpectedEOF {
log.Fatal("The server may be running an incompatible version")
} else {
log.Fatal(err)
}
}
if ed25519.Verify(conf.SignPk, ciphertextWithNonce, signature) != true {
log.Fatal("Signature doesn't verify")
}
nonce := ciphertextWithNonce[0:24]
cipher, err := chacha20.NewCipher(conf.EncryptSk, nonce)
if err != nil {
log.Fatal(err)
}
content := ciphertextWithNonce[24:]
cipher.XORKeyStream(content, content)
binary.Write(os.Stdout, binary.LittleEndian, content)
}
// RunClient - Process a client query
func RunClient(conf Conf, isCopy bool, isMove bool) {
conn, err := net.DialTimeout("tcp", conf.Connect, conf.Timeout)
if err != nil {
log.Fatal(fmt.Sprintf("Unable to connect to %v - Is a Piknik server running on that host?",
conf.Connect))
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(conf.Timeout))
reader, writer := bufio.NewReader(conn), bufio.NewWriter(conn)
client := Client{
conf: conf,
conn: conn,
reader: reader,
writer: writer,
version: DefaultClientVersion,
}
r := make([]byte, 32)
if _, err = rand.Read(r); err != nil {
log.Fatal(err)
}
h0 := auth0(conf, client.version, r)
writer.Write([]byte{client.version})
writer.Write(r)
writer.Write(h0)
if err := writer.Flush(); err != nil {
log.Fatal(err)
}
rbuf := make([]byte, 65)
if nbread, err := io.ReadFull(reader, rbuf); err != nil {
if nbread < 2 {
log.Fatal("The server rejected the connection - Check that it is running the same Piknik version or retry later")
} else {
log.Fatal("The server doesn't support this protocol")
}
}
if serverVersion := rbuf[0]; serverVersion != client.version {
log.Fatal(fmt.Sprintf("Incompatible server version (client version: %v - server version: %v)",
client.version, serverVersion))
}
r2 := rbuf[1:33]
h1 := rbuf[33:65]
wh1 := auth1(conf, client.version, h0, r2)
if subtle.ConstantTimeCompare(wh1, h1) != 1 {
log.Fatal("Incorrect authentication code")
}
if isCopy {
client.copyOperation(h1)
} else {
client.pasteOperation(h1, isMove)
}
}