secure_conn.go 2.94 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1
2
3
package conn

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"errors"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
6
7
	"net"
	"time"

Jeromy's avatar
Jeromy committed
8
9
10
11
12
	ic "github.com/ipfs/go-libp2p-crypto"
	peer "github.com/ipfs/go-libp2p-peer"
	secio "github.com/ipfs/go-libp2p-secio"
	ma "github.com/jbenet/go-multiaddr"
	context "golang.org/x/net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
14
15
16
)

// secureConn wraps another Conn object with an encrypted channel.
type secureConn struct {
17
18
	insecure Conn          // the wrapped conn
	secure   secio.Session // secure Session
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
}

// newConn constructs a new connection
func newSecureConn(ctx context.Context, sk ic.PrivKey, insecure Conn) (Conn, error) {

	if insecure == nil {
		return nil, errors.New("insecure is nil")
	}
	if insecure.LocalPeer() == "" {
		return nil, errors.New("insecure.LocalPeer() is nil")
	}
	if sk == nil {
		return nil, errors.New("private key is nil")
	}

	// NewSession performs the secure handshake, which takes multiple RTT
	sessgen := secio.SessionGenerator{LocalID: insecure.LocalPeer(), PrivateKey: sk}
36
	secure, err := sessgen.NewSession(ctx, insecure)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
38
39
40
41
42
	if err != nil {
		return nil, err
	}

	conn := &secureConn{
		insecure: insecure,
43
		secure:   secure,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
45
46
47
48
	}
	return conn, nil
}

func (c *secureConn) Close() error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
	return c.secure.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
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
}

// ID is an identifier unique to this connection.
func (c *secureConn) ID() string {
	return ID(c)
}

func (c *secureConn) String() string {
	return String(c, "secureConn")
}

func (c *secureConn) LocalAddr() net.Addr {
	return c.insecure.LocalAddr()
}

func (c *secureConn) RemoteAddr() net.Addr {
	return c.insecure.RemoteAddr()
}

func (c *secureConn) SetDeadline(t time.Time) error {
	return c.insecure.SetDeadline(t)
}

func (c *secureConn) SetReadDeadline(t time.Time) error {
	return c.insecure.SetReadDeadline(t)
}

func (c *secureConn) SetWriteDeadline(t time.Time) error {
	return c.insecure.SetWriteDeadline(t)
}

// LocalMultiaddr is the Multiaddr on this side
func (c *secureConn) LocalMultiaddr() ma.Multiaddr {
	return c.insecure.LocalMultiaddr()
}

// RemoteMultiaddr is the Multiaddr on the remote side
func (c *secureConn) RemoteMultiaddr() ma.Multiaddr {
	return c.insecure.RemoteMultiaddr()
}

// LocalPeer is the Peer on this side
func (c *secureConn) LocalPeer() peer.ID {
93
	return c.secure.LocalPeer()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
95
96
97
}

// RemotePeer is the Peer on the remote side
func (c *secureConn) RemotePeer() peer.ID {
98
	return c.secure.RemotePeer()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99
100
101
102
}

// LocalPrivateKey is the public key of the peer on this side
func (c *secureConn) LocalPrivateKey() ic.PrivKey {
103
	return c.secure.LocalPrivateKey()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104
105
106
107
}

// RemotePubKey is the public key of the peer on the remote side
func (c *secureConn) RemotePublicKey() ic.PubKey {
108
	return c.secure.RemotePublicKey()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109
110
111
112
}

// Read reads data, net.Conn style
func (c *secureConn) Read(buf []byte) (int, error) {
113
	return c.secure.ReadWriter().Read(buf)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114
115
116
117
}

// Write writes data, net.Conn style
func (c *secureConn) Write(buf []byte) (int, error) {
118
	return c.secure.ReadWriter().Write(buf)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
119
120
121
122
}

// ReleaseMsg releases a buffer
func (c *secureConn) ReleaseMsg(m []byte) {
123
	c.secure.ReadWriter().ReleaseMsg(m)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124
}