-
Notifications
You must be signed in to change notification settings - Fork 43
/
session.go
200 lines (160 loc) · 3.75 KB
/
session.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
package hap
import (
"time"
"github.com/brutella/hap/chacha20poly1305"
"github.com/brutella/hap/hkdf"
"bytes"
"encoding/binary"
"fmt"
"io"
"net/http"
"sync"
)
var mux = &sync.Mutex{}
var cons = make(map[string]*conn)
func setConn(addr string, conn *conn) {
mux.Lock()
defer mux.Unlock()
cons[addr] = conn
}
func getConn(req *http.Request) *conn {
mux.Lock()
defer mux.Unlock()
if con, ok := cons[req.RemoteAddr]; !ok {
return nil
} else {
return con
}
}
func conns() map[string]*conn {
copy := map[string]*conn{}
mux.Lock()
for k, v := range cons {
copy[k] = v
}
mux.Unlock()
return copy
}
type session struct {
Pairing Pairing
encryptKey [32]byte
decryptKey [32]byte
encryptCount uint64
decryptCount uint64
mu sync.Mutex
twr *TimedWrite
}
type TimedWrite struct {
deadline time.Time
pid uint64
}
func newSession(shared [32]byte, p Pairing) (*session, error) {
salt := []byte("Control-Salt")
out := []byte("Control-Read-Encryption-Key")
in := []byte("Control-Write-Encryption-Key")
s := &session{
Pairing: p,
}
var err error
s.encryptKey, err = hkdf.Sha512(shared[:], salt, out)
s.encryptCount = 0
if err != nil {
return nil, err
}
s.decryptKey, err = hkdf.Sha512(shared[:], salt, in)
s.decryptCount = 0
return s, err
}
// Encrypt return the encrypted data by splitting it into packets
// [ length (2 bytes)] [ data ] [ auth (16 bytes)]
func (s *session) Encrypt(r io.Reader) (io.Reader, error) {
packets := packetsFromBytes(r)
var buf bytes.Buffer
for _, p := range packets {
var nonce [8]byte
s.mu.Lock()
binary.LittleEndian.PutUint64(nonce[:], s.encryptCount)
s.encryptCount++
s.mu.Unlock()
bLength := make([]byte, 2)
binary.LittleEndian.PutUint16(bLength, uint16(p.length))
encrypted, mac, err := chacha20poly1305.EncryptAndSeal(s.encryptKey[:], nonce[:], p.value, bLength[:])
if err != nil {
return nil, err
}
buf.Write(bLength[:])
buf.Write(encrypted)
buf.Write(mac[:])
}
return &buf, nil
}
// Decrypt returns the decrypted data
func (s *session) Decrypt(r io.Reader) (io.Reader, error) {
var buf bytes.Buffer
for {
var length uint16
if err := binary.Read(r, binary.LittleEndian, &length); err != nil {
if err == io.EOF {
break
}
return nil, err
}
var b = make([]byte, length)
if err := binary.Read(r, binary.LittleEndian, &b); err != nil {
return nil, err
}
var mac [16]byte
if err := binary.Read(r, binary.LittleEndian, &mac); err != nil {
return nil, err
}
var nonce [8]byte
s.mu.Lock()
binary.LittleEndian.PutUint64(nonce[:], s.decryptCount)
s.decryptCount++
s.mu.Unlock()
lengthBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(lengthBytes, uint16(length))
decrypted, err := chacha20poly1305.DecryptAndVerify(s.decryptKey[:], nonce[:], b, mac, lengthBytes)
if err != nil {
return nil, fmt.Errorf("Data encryption failed %s", err)
}
buf.Write(decrypted)
// Finish when all bytes fit in b
if length < packetLengthMax {
break
}
}
return &buf, nil
}
const (
// packetLengthMax is the max length of encrypted packets
packetLengthMax = 0x400
)
type packet struct {
length int
value []byte
}
// packetsWithSizeFromBytes returns lv (tlv without t(ype)) packets
func packetsWithSizeFromBytes(length int, r io.Reader) []packet {
var packets []packet
for {
var value = make([]byte, length)
n, err := r.Read(value)
if n == 0 {
break
}
if n > length {
panic("Invalid length")
}
p := packet{length: n, value: value[:n]}
packets = append(packets, p)
if n < length || err == io.EOF {
break
}
}
return packets
}
// packetsFromBytes returns packets with length packetLengthMax
func packetsFromBytes(r io.Reader) []packet {
return packetsWithSizeFromBytes(packetLengthMax, r)
}