6
6
package device
7
7
8
8
import (
9
+ "encoding/base64"
10
+ "fmt"
11
+ "io"
9
12
"runtime"
10
13
"sync"
11
14
"sync/atomic"
@@ -17,6 +20,8 @@ import (
17
20
"golang.zx2c4.com/wireguard/tun"
18
21
)
19
22
23
+ type HandshakeHandler func (t time.Time , ls NoisePrivateKey , rs NoisePublicKey , le NoisePrivateKey , ps NoisePresharedKey )
24
+
20
25
type Device struct {
21
26
state struct {
22
27
// state holds the device's state. It is accessed atomically.
@@ -85,6 +90,8 @@ type Device struct {
85
90
mtu int32
86
91
}
87
92
93
+ keyLogHandler HandshakeHandler
94
+
88
95
ipcMutex sync.RWMutex
89
96
closed chan struct {}
90
97
log * Logger
@@ -94,10 +101,9 @@ type Device struct {
94
101
// There are three states: down, up, closed.
95
102
// Transitions:
96
103
//
97
- // down -----+
98
- // ↑↓ ↓
99
- // up -> closed
100
- //
104
+ // down -----+
105
+ // ↑↓ ↓
106
+ // up -> closed
101
107
type deviceState uint32
102
108
103
109
//go:generate go run golang.org/x/tools/cmd/stringer -type deviceState -trimprefix=deviceState
@@ -523,3 +529,26 @@ func (device *Device) BindClose() error {
523
529
device .net .Unlock ()
524
530
return err
525
531
}
532
+
533
+ func (device * Device ) OnHandshake (hdlr HandshakeHandler ) {
534
+ device .keyLogHandler = hdlr
535
+ }
536
+
537
+ func (device * Device ) WriteKeyLog (wr io.Writer ) {
538
+ mu := sync.Mutex {}
539
+
540
+ device .OnHandshake (func (t time.Time , ls NoisePrivateKey , rs NoisePublicKey , le NoisePrivateKey , ps NoisePresharedKey ) {
541
+ mu .Lock ()
542
+ defer mu .Unlock ()
543
+
544
+ fmt .Fprintf (wr , "LOCAL_STATIC_PRIVATE_KEY=%s\n " , base64 .StdEncoding .EncodeToString (ls [:]))
545
+ fmt .Fprintf (wr , "REMOTE_STATIC_PUBLIC_KEY=%s\n " , base64 .StdEncoding .EncodeToString (rs [:]))
546
+ fmt .Fprintf (wr , "LOCAL_EPHEMERAL_PRIVATE_KEY=%s\n " , base64 .StdEncoding .EncodeToString (le [:]))
547
+
548
+ if ! ps .IsZero () {
549
+ fmt .Fprintf (wr , "PRESHARED_KEY=%s\n " , base64 .StdEncoding .EncodeToString (ps [:]))
550
+ }
551
+
552
+ device .log .Verbosef ("Writing new ephemeral key to keylog" )
553
+ })
554
+ }
0 commit comments