aboutsummaryrefslogtreecommitdiffstats
path: root/src/device.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2017-08-04 16:15:53 +0200
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2017-08-04 16:15:53 +0200
commit8c34c4cbb3780c433148966a004f5a51aace0f64 (patch)
treea590de76c326f6dfe3c92d2e27b78ce2ab792289 /src/device.go
parentMerge branch 'master' of git.zx2c4.com:wireguard-go (diff)
downloadwireguard-go-8c34c4cbb3780c433148966a004f5a51aace0f64.tar.xz
wireguard-go-8c34c4cbb3780c433148966a004f5a51aace0f64.zip
First set of code review patches
Diffstat (limited to 'src/device.go')
-rw-r--r--src/device.go44
1 files changed, 35 insertions, 9 deletions
diff --git a/src/device.go b/src/device.go
index 1185d60..de96f0b 100644
--- a/src/device.go
+++ b/src/device.go
@@ -1,6 +1,8 @@
package main
import (
+ "errors"
+ "fmt"
"net"
"runtime"
"sync"
@@ -10,6 +12,7 @@ import (
type Device struct {
mtu int32
+ tun TUNDevice
log *Logger // collection of loggers for levels
idCounter uint // for assigning debug ids to peers
fwMark uint32
@@ -43,24 +46,46 @@ type Device struct {
mac MACStateDevice
}
-func (device *Device) SetPrivateKey(sk NoisePrivateKey) {
+func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
device.mutex.Lock()
defer device.mutex.Unlock()
+ // check if public key is matching any peer
+
+ publicKey := sk.publicKey()
+ for _, peer := range device.peers {
+ h := &peer.handshake
+ h.mutex.RLock()
+ if h.remoteStatic.Equals(publicKey) {
+ h.mutex.RUnlock()
+ return errors.New("Private key matches public key of peer")
+ }
+ h.mutex.RUnlock()
+ }
+
// update key material
device.privateKey = sk
- device.publicKey = sk.publicKey()
- device.mac.Init(device.publicKey)
+ device.publicKey = publicKey
+ device.mac.Init(publicKey)
// do DH precomputations
+ isZero := device.privateKey.IsZero()
+
for _, peer := range device.peers {
h := &peer.handshake
h.mutex.Lock()
- h.precomputedStaticStatic = device.privateKey.sharedSecret(h.remoteStatic)
+ if isZero {
+ h.precomputedStaticStatic = [NoisePublicKeySize]byte{}
+ } else {
+ h.precomputedStaticStatic = device.privateKey.sharedSecret(h.remoteStatic)
+ }
+ fmt.Println(h.precomputedStaticStatic)
h.mutex.Unlock()
}
+
+ return nil
}
func (device *Device) GetMessageBuffer() *[MaxMessageSize]byte {
@@ -77,6 +102,7 @@ func NewDevice(tun TUNDevice, logLevel int) *Device {
device.mutex.Lock()
defer device.mutex.Unlock()
+ device.tun = tun
device.log = NewLogger(logLevel)
device.peers = make(map[NoisePublicKey]*Peer)
device.indices.Init()
@@ -119,22 +145,22 @@ func NewDevice(tun TUNDevice, logLevel int) *Device {
}
go device.RoutineBusyMonitor()
- go device.RoutineMTUUpdater(tun)
- go device.RoutineWriteToTUN(tun)
- go device.RoutineReadFromTUN(tun)
+ go device.RoutineMTUUpdater()
+ go device.RoutineWriteToTUN()
+ go device.RoutineReadFromTUN()
go device.RoutineReceiveIncomming()
go device.ratelimiter.RoutineGarbageCollector(device.signal.stop)
return device
}
-func (device *Device) RoutineMTUUpdater(tun TUNDevice) {
+func (device *Device) RoutineMTUUpdater() {
logError := device.log.Error
for ; ; time.Sleep(5 * time.Second) {
// load updated MTU
- mtu, err := tun.MTU()
+ mtu, err := device.tun.MTU()
if err != nil {
logError.Println("Failed to load updated MTU of device:", err)
continue