/
connection.go
172 lines (134 loc) · 3.47 KB
/
connection.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
package main
import (
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"github.com/ghaskins/go-cluster/pb"
"github.com/golang/protobuf/proto"
"net"
"strings"
)
type Connection struct {
Conn *tls.Conn
Id *Identity
}
func (c *Connection) Send(m proto.Message) error {
msg, err := proto.Marshal(m)
if err != nil {
return err
}
header := make([]byte, 4)
binary.BigEndian.PutUint32(header, uint32(len(msg)))
c.Conn.Write(header)
c.Conn.Write(msg)
return nil
}
func (c *Connection) Recv(m proto.Message) error {
header := make([]byte, 4)
hLen, err := c.Conn.Read(header)
if err != nil || hLen != 4 {
return err
}
len := binary.BigEndian.Uint32(header)
// FIXME: guard against an upper MTU violation
payload := make([]byte, len)
pLen, err := c.Conn.Read(payload)
if err != nil {
return err
}
if pLen != int(len) {
return errors.New(fmt.Sprintf("Read error: expected %d bytes, got %d", len, pLen))
}
err = proto.Unmarshal(payload, m)
if err != nil {
return err
}
return nil
}
func verifyCrypto(conn *tls.Conn) (*Connection, error) {
if err := conn.Handshake(); err != nil {
return nil, err
}
certs := conn.ConnectionState().PeerCertificates
if len(certs) != 1 {
return nil, errors.New(fmt.Sprintf("Illegal number of certificates presented by peer (%d)", len(certs)))
}
cert := certs[0]
if err := cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature); err != nil {
return nil, err
}
return &Connection{Conn: conn, Id: NewIdentity(cert)}, nil
}
func newConfig(self *tls.Certificate) *tls.Config {
config := &tls.Config{
Certificates: make([]tls.Certificate, 1),
InsecureSkipVerify: true,
ClientAuth: tls.RequireAnyClientCert,
}
config.Certificates[0] = *self
return config
}
func newNegotiate() *pb.Negotiate {
return &pb.Negotiate{
Magic: proto.String("cluster"),
Version: proto.Int(1),
}
}
func verifyProtocol(ours, theirs *pb.Negotiate) error {
if strings.Compare(*ours.Magic, *theirs.Magic) != 0 || *ours.Version != *theirs.Version {
return errors.New(fmt.Sprintf("incompatible wire protocol (ours: %v, theirs: %v)", ours, theirs))
}
return nil
}
func Dial(self *tls.Certificate, peer *Identity) (conn *Connection, err error) {
tlsConn, err := tls.Dial("tcp", peer.Cert.Subject.CommonName, newConfig(self))
if err != nil {
return nil, err
}
conn, err = verifyCrypto(tlsConn)
if err != nil {
return nil, err
}
if conn.Id.Id != peer.Id {
return nil, errors.New("Unexpected peer identity")
}
// Negotiation protocol: send a Negotiate packet to the server, and wait for
// a response. Then ensure baseline compatibility
ours := newNegotiate()
theirs := &pb.Negotiate{}
conn.Send(ours)
err = conn.Recv(theirs)
if err != nil {
return nil, err
}
if err = verifyProtocol(ours, theirs); err != nil {
return nil, err
}
return conn, nil
}
func Listen(self *tls.Certificate, laddr string) (net.Listener, error) {
return tls.Listen("tcp", laddr, newConfig(self))
}
func Accept(listener net.Listener) (*Connection, error) {
tlsConn, err := listener.Accept()
if err != nil {
return nil, err
}
conn, err := verifyCrypto(tlsConn.(*tls.Conn))
if err != nil {
return nil, err
}
// Negotiation protocol: wait for a negotiate message, compare, and reply
ours := newNegotiate()
theirs := &pb.Negotiate{}
err = conn.Recv(theirs)
if err != nil {
return nil, err
}
if err = verifyProtocol(ours, theirs); err != nil {
return nil, err
}
conn.Send(ours)
return conn, nil
}