-
Notifications
You must be signed in to change notification settings - Fork 10
/
middleware_crypt.go
166 lines (129 loc) · 3.89 KB
/
middleware_crypt.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
package go2p
import (
"bytes"
"github.com/pkg/errors"
"github.com/v-braun/go2p/crypt"
)
var prefixHandshake = []byte("hello:")
const cryptLabel = "middleware.crypt"
const headerKeyPubKey = "middleware.crypt.pubkey"
// Crypt returns the crypto middleware.
// This middleware handles encryption in your communication
// PublicKeys are exchanged on first peer communication
func Crypt() (string, MiddlewareFunc) {
key := crypt.Generate()
f := func(peer *Peer, pipe *Pipe, msg *Message) (MiddlewareResult, error) {
op, err := middlewareCryptImpl(key, peer, pipe, msg)
return op, err
}
return "Crypt", f
}
func middlewareCryptImpl(myKey *crypt.PrivKey, peer *Peer, pipe *Pipe, msg *Message) (MiddlewareResult, error) {
if isHandshakeDone(peer, pipe) {
// handshake done, just handle the message
err := messageHandle(peer, pipe, msg, myKey)
if err != nil {
return Stop, err
}
return Next, err
}
// no pub-key from remote, handle handshake
if pipe.Operation() == Receive {
// passive mode:
// the remote send us the pub key
// so the received message should be a handshake message
err := handshakePassive(peer, pipe, msg, myKey)
return Stop, err
}
// active mode:
// the active message should be postpone after the key exchange
err := handshakeActive(peer, pipe, myKey)
if err != nil {
return Stop, err
}
// handshake done, just handle the active message
err = messageHandle(peer, pipe, msg, myKey)
return Next, err
}
func messageHandle(peer *Peer, pipe *Pipe, msg *Message, myKey *crypt.PrivKey) error {
key, _ := peer.Metadata().Get(headerKeyPubKey)
theirKey := key.(*crypt.PubKey)
if pipe.Operation() == Send {
err := encrypt(msg, theirKey, myKey)
return err
}
err := decrypt(msg, myKey, theirKey)
return err
}
func encrypt(msg *Message, theirKey *crypt.PubKey, myKey *crypt.PrivKey) error {
content := msg.PayloadGet()
contentEnc, err := theirKey.Encrypt(myKey, content)
if err != nil {
return errors.Wrapf(err, "could not encrypt message (len: %d)", len(content))
}
msg.PayloadSet(contentEnc)
return nil
}
func decrypt(msg *Message, myKey *crypt.PrivKey, theirKey *crypt.PubKey) error {
content := msg.PayloadGet()
contentLen := len(content)
content, err := myKey.Decrypt(theirKey, content)
if err != nil {
return errors.Wrapf(err, "could not decrypt (len: %d)", contentLen)
}
msg.PayloadSet(content)
return nil
}
// handshake methods
func isHandshakeDone(peer *Peer, pipe *Pipe) bool {
_, found := peer.Metadata().Get(headerKeyPubKey)
return found
}
func isHandshakeMsg(msg *Message) bool {
content := msg.PayloadGet()
if len(content) < len(prefixHandshake) {
return false
}
prefix := content[:len(prefixHandshake)]
equal := bytes.Equal(prefix, prefixHandshake)
return equal
}
func handshakePassive(peer *Peer, pipe *Pipe, msg *Message, myKey *crypt.PrivKey) error {
if err := handshakeHandleResponse(peer, pipe, msg); err != nil {
errors.Wrapf(err, "received message from peer without a handshake | peer: %s", peer.RemoteAddress())
return err
}
err := handshakeSend(pipe, myKey)
return err
}
func handshakeActive(peer *Peer, pipe *Pipe, myKey *crypt.PrivKey) error {
if err := handshakeSend(pipe, myKey); err != nil {
return err
}
msg, err := pipe.Receive()
if err != nil {
return err
}
err = handshakeHandleResponse(peer, pipe, msg)
return err
}
func handshakeSend(pipe *Pipe, myKey *crypt.PrivKey) error {
rq := NewMessage()
content := append(prefixHandshake, myKey.PubKey.Bytes...)
rq.PayloadSet(content)
err := pipe.Send(rq)
return err
}
func handshakeHandleResponse(peer *Peer, pipe *Pipe, msg *Message) error {
if !isHandshakeMsg(msg) {
return errors.Errorf("invalid handshake message | peer: %s", peer.RemoteAddress())
}
content := msg.PayloadGet()
result := content[len(prefixHandshake):]
key, err := crypt.PubFromBytes(result)
if err != nil {
return err
}
peer.Metadata().Put(headerKeyPubKey, key)
return err
}