-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
secure_conn.go
272 lines (256 loc) · 8.12 KB
/
secure_conn.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright 2023 Avi Zimmerman <avi.zimmerman@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wgtransport
import (
"encoding/base64"
"encoding/json"
"fmt"
"net"
"net/netip"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/core/sec"
mnet "github.com/multiformats/go-multiaddr/net"
"github.com/webmeshproj/webmesh/pkg/context"
wgcrypto "github.com/webmeshproj/webmesh/pkg/crypto"
wmproto "github.com/webmeshproj/webmesh/pkg/libp2p/protocol"
p2putil "github.com/webmeshproj/webmesh/pkg/libp2p/util"
netutil "github.com/webmeshproj/webmesh/pkg/meshnet/util"
"github.com/webmeshproj/webmesh/pkg/meshnet/wireguard"
)
// Ensure we implement the interface
var _ sec.SecureConn = (*SecureConn)(nil)
// SecureConn is a simple wrapper around the underlying connection that
// holds remote peer information.
type SecureConn struct {
*Conn
rpeer peer.ID
rkey wgcrypto.PublicKey
}
// NewSecureConn upgrades an insecure connection with peer identity.
func NewSecureConn(ctx context.Context, insecure *Conn, rpeer peer.ID, psk pnet.PSK, dir network.Direction) (*SecureConn, error) {
log := context.LoggerFrom(ctx)
sc := &SecureConn{
Conn: insecure,
}
log.Debug("Performing security negotiation")
err := sc.negotiate(ctx, psk, dir)
if err != nil {
return nil, fmt.Errorf("failed to negotiate wireguard connection: %w", err)
}
return sc, nil
}
// LocalPeer returns our peer ID
func (c *SecureConn) LocalPeer() peer.ID { return c.Conn.lpeer }
// RemotePeer returns the peer ID of the remote peer.
func (c *SecureConn) RemotePeer() peer.ID { return c.rpeer }
// RemotePublicKey returns the public key of the remote peer.
func (c *SecureConn) RemotePublicKey() crypto.PubKey { return c.rkey }
// ConnState returns information about the connection state.
func (c *SecureConn) ConnState() network.ConnectionState {
return network.ConnectionState{
Security: wmproto.SecurityID,
Transport: "tcp",
UsedEarlyMuxerNegotiation: false,
}
}
type negotiation struct {
// PeerID is the peer ID of the remote peer.
PeerID peer.ID
// Endpoints is a comma-separated list of strings of the form
// <addr>:<port> that the peer is listening on.
Endpoints []string
// SecurePort is the port that the incoming secure connection should
// be established on.
SecurePort int
// Signature which should equal this payload without the signature
// appended as a base64-encoded string.
Signature string
}
// negotiate handles the initial negotiation of the connection.
func (c *SecureConn) negotiate(ctx context.Context, psk pnet.PSK, dir network.Direction) (err error) {
var (
rula netip.Prefix
raddr netip.Addr
remoteEndpoint netip.AddrPort
ln net.Listener
securep int
)
laddr := &net.TCPAddr{
IP: c.iface.AddressV6().Addr().AsSlice(),
Port: 0,
}
log := context.LoggerFrom(ctx)
if dir == network.DirInbound {
log.Debug("Starting secure listener")
ln, err = net.ListenTCP("tcp6", laddr)
if err != nil {
err = fmt.Errorf("failed to start signaling server: %w", err)
return
}
defer ln.Close()
securep = ln.Addr().(*net.TCPAddr).Port
log.Debug("Secure listener started, handling negotiation", "address", ln.Addr().String())
}
errs := make(chan error, 1)
go func() {
defer close(errs)
log.Debug("Waiting for negotiation payload")
var msg negotiation
err := json.NewDecoder(c).Decode(&msg)
if err != nil {
errs <- fmt.Errorf("failed to decode negotiation payload: %w", err)
return
}
log.Debug("Received negotiation payload, verifying the signature")
// Marshal the payload and verify the signature
sig, err := base64.RawStdEncoding.DecodeString(msg.Signature)
if err != nil {
errs <- fmt.Errorf("failed to decode negotiation signature: %w", err)
return
}
msg.Signature = ""
data, err := json.Marshal(msg)
if err != nil {
log.Error("Failed to marshal negotiation payload", "error", err.Error())
errs <- fmt.Errorf("failed to marshal negotiation payload: %w", err)
return
}
// Extract the public key from the peer ID
key, err := p2putil.ExtractWebmeshPublicKey(ctx, msg.PeerID)
if err != nil {
log.Error("Failed to extract public key from peer ID", "error", err.Error())
errs <- fmt.Errorf("failed to extract public key from peer ID: %w", err)
return
}
c.rkey = key
ok, err := c.rkey.Verify(data, sig)
if err != nil {
log.Error("Failed to verify negotiation signature", "error", err.Error())
errs <- fmt.Errorf("failed to verify negotiation signature: %w", err)
return
}
if !ok {
log.Error("Negotiation signature is invalid")
errs <- fmt.Errorf("negotiation signature is invalid")
return
}
log.Debug("Negotiation signature is valid")
if dir == network.DirOutbound {
securep = msg.SecurePort
}
c.rpeer = msg.PeerID
c.Conn.rmaddr = wmproto.Encapsulate(c.Conn.Conn.RemoteMultiaddr(), c.rpeer)
if len(psk) > 0 {
// We seed the ULA with the PSK
rula = netutil.GenerateULAWithSeed(psk)
raddr = netutil.AssignToPrefix(rula, c.rkey).Addr()
} else {
// The peer will have their own ULA that we'll trust.
rula, raddr = netutil.GenerateULAWithKey(c.rkey)
}
log.Debug("Determined remote network addresses for for peer",
"ula", rula.String(),
"addr", raddr.String(),
)
if len(msg.Endpoints) == 0 {
// We're done here
errs <- nil
return
}
// We just use the first one for now
epString := msg.Endpoints[0]
remoteEndpoint, err = netip.ParseAddrPort(epString)
errs <- err
}()
payload := negotiation{
PeerID: c.Conn.lpeer,
Endpoints: c.Conn.eps,
SecurePort: securep,
}
data, err := json.Marshal(payload)
if err != nil {
err = fmt.Errorf("failed to marshal negotiation payload: %w", err)
return
}
// Sign the payload
sig, err := c.lkey.Sign(data)
if err != nil {
err = fmt.Errorf("failed to sign negotiation payload: %w", err)
return
}
// Remarshal with the signature
payload.Signature = base64.RawStdEncoding.EncodeToString(sig)
data, err = json.Marshal(payload)
if err != nil {
err = fmt.Errorf("failed to marshal negotiation payload: %w", err)
return
}
_, err = c.Write(data)
if err != nil {
err = fmt.Errorf("failed to write negotiation payload to wire: %w", err)
return
}
// Wait for the goroutine
select {
case <-ctx.Done():
err = ctx.Err()
return
case err = <-errs:
if err != nil {
err = fmt.Errorf("failed to complete negotiation: %w", err)
return
}
}
// Configure WireGuard
peer := wireguard.Peer{
ID: c.rpeer.String(),
PublicKey: c.rkey,
Endpoint: remoteEndpoint,
PrivateIPv6: netip.PrefixFrom(raddr, wmproto.PrefixSize),
AllowedIPs: []netip.Prefix{rula},
}
log.Debug("Adding peer to wireguard interface", "config", peer)
err = c.iface.PutPeer(context.WithLogger(ctx, log), &peer)
if err != nil {
err = fmt.Errorf("failed to add peer to wireguard interface: %w", err)
return
}
// Create the secure connection
log.Debug("Creating secure connection over WireGuard")
if dir == network.DirInbound {
sc, err := ln.Accept()
if err != nil {
return fmt.Errorf("failed to accept secure connection: %w", err)
}
c.Conn.Conn, err = mnet.WrapNetConn(sc)
if err != nil {
return fmt.Errorf("failed to wrap secure connection: %w", err)
}
} else {
sc, err := net.DialTCP("tcp6", laddr, &net.TCPAddr{
IP: raddr.AsSlice(),
Port: securep,
})
if err != nil {
return fmt.Errorf("failed to dial secure connection: %w", err)
}
c.Conn.Conn, err = mnet.WrapNetConn(sc)
if err != nil {
return fmt.Errorf("failed to wrap secure connection: %w", err)
}
}
return
}