2018-01-03 21:58:00 +01:00
|
|
|
/* Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2016-07-07 01:49:30 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/titanous/noise"
|
2016-07-07 04:57:08 +02:00
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv4"
|
2017-06-06 16:50:20 +02:00
|
|
|
"golang.org/x/crypto/blake2s"
|
2015-06-05 15:58:00 +02:00
|
|
|
)
|
|
|
|
|
2016-07-07 13:42:35 +02:00
|
|
|
func ipChecksum(buf []byte) uint16 {
|
|
|
|
sum := uint32(0)
|
|
|
|
for ; len(buf) >= 2; buf = buf[2:] {
|
|
|
|
sum += uint32(buf[0])<<8 | uint32(buf[1])
|
|
|
|
}
|
|
|
|
if len(buf) > 0 {
|
|
|
|
sum += uint32(buf[0]) << 8
|
|
|
|
}
|
|
|
|
for sum > 0xffff {
|
|
|
|
sum = (sum >> 16) + (sum & 0xffff)
|
|
|
|
}
|
|
|
|
csum := ^uint16(sum)
|
|
|
|
if csum == 0 {
|
|
|
|
csum = 0xffff
|
|
|
|
}
|
|
|
|
return csum
|
|
|
|
}
|
|
|
|
|
2015-06-05 15:58:00 +02:00
|
|
|
func main() {
|
2016-07-07 01:49:30 +02:00
|
|
|
ourPrivate, _ := base64.StdEncoding.DecodeString("WAmgVYXkbT2bCtdcDwolI88/iVi/aV3/PHcUBTQSYmo=")
|
|
|
|
ourPublic, _ := base64.StdEncoding.DecodeString("K5sF9yESrSBsOXPd6TcpKNgqoy1Ik3ZFKl4FolzrRyI=")
|
|
|
|
theirPublic, _ := base64.StdEncoding.DecodeString("qRCwZSKInrMAq5sepfCdaCsRJaoLe5jhtzfiw7CjbwM=")
|
2017-04-27 11:10:50 +02:00
|
|
|
preshared, _ := base64.StdEncoding.DecodeString("FpCyhws9cxwWoV4xELtfJvjJN+zQVRPISllRWgeopVE=")
|
2015-06-05 15:58:00 +02:00
|
|
|
cs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashBLAKE2s)
|
2018-02-15 13:12:28 +01:00
|
|
|
hs, _ := noise.NewHandshakeState(noise.Config{
|
2017-06-01 19:08:24 +02:00
|
|
|
CipherSuite: cs,
|
|
|
|
Random: rand.Reader,
|
|
|
|
Pattern: noise.HandshakeIK,
|
|
|
|
Initiator: true,
|
|
|
|
Prologue: []byte("WireGuard v1 zx2c4 Jason@zx2c4.com"),
|
|
|
|
PresharedKey: preshared,
|
2017-04-27 11:10:50 +02:00
|
|
|
PresharedKeyPlacement: 2,
|
2017-06-01 19:08:24 +02:00
|
|
|
StaticKeypair: noise.DHKey{Private: ourPrivate, Public: ourPublic},
|
|
|
|
PeerStatic: theirPublic,
|
2016-07-07 01:49:30 +02:00
|
|
|
})
|
2017-07-16 16:12:20 +02:00
|
|
|
conn, err := net.Dial("udp", "demo.wireguard.com:12913")
|
2016-07-07 01:49:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error dialing udp socket: %s", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2016-07-07 04:57:08 +02:00
|
|
|
// write handshake initiation packet
|
2015-06-05 15:58:00 +02:00
|
|
|
now := time.Now()
|
|
|
|
tai64n := make([]byte, 12)
|
2016-07-07 13:47:32 +02:00
|
|
|
binary.BigEndian.PutUint64(tai64n[:], 4611686018427387914+uint64(now.Unix()))
|
2017-06-01 19:08:24 +02:00
|
|
|
binary.BigEndian.PutUint32(tai64n[8:], uint32(now.Nanosecond()))
|
2016-12-25 21:01:06 +01:00
|
|
|
initiationPacket := make([]byte, 8)
|
2017-06-01 19:08:24 +02:00
|
|
|
initiationPacket[0] = 1 // Type: Initiation
|
|
|
|
initiationPacket[1] = 0 // Reserved
|
|
|
|
initiationPacket[2] = 0 // Reserved
|
|
|
|
initiationPacket[3] = 0 // Reserved
|
2016-12-25 21:01:06 +01:00
|
|
|
binary.LittleEndian.PutUint32(initiationPacket[4:], 28) // Sender index: 28 (arbitrary)
|
2018-02-15 13:12:28 +01:00
|
|
|
initiationPacket, _, _, _ = hs.WriteMessage(initiationPacket, tai64n)
|
2017-06-06 16:50:20 +02:00
|
|
|
hasher, _ := blake2s.New256(nil)
|
2017-04-27 11:10:50 +02:00
|
|
|
hasher.Write([]byte("mac1----"))
|
2016-07-07 01:49:30 +02:00
|
|
|
hasher.Write(theirPublic)
|
2017-06-06 16:50:20 +02:00
|
|
|
hasher, _ = blake2s.New128(hasher.Sum(nil))
|
2016-07-07 01:49:30 +02:00
|
|
|
hasher.Write(initiationPacket)
|
|
|
|
initiationPacket = append(initiationPacket, hasher.Sum(nil)[:16]...)
|
|
|
|
initiationPacket = append(initiationPacket, make([]byte, 16)...)
|
|
|
|
if _, err := conn.Write(initiationPacket); err != nil {
|
|
|
|
log.Fatalf("error writing initiation packet: %s", err)
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2016-07-07 04:57:08 +02:00
|
|
|
// read handshake response packet
|
2016-12-25 21:01:06 +01:00
|
|
|
responsePacket := make([]byte, 92)
|
2016-07-07 01:49:30 +02:00
|
|
|
n, err := conn.Read(responsePacket)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error reading response packet: %s", err)
|
|
|
|
}
|
|
|
|
if n != len(responsePacket) {
|
|
|
|
log.Fatalf("response packet too short: want %d, got %d", len(responsePacket), n)
|
|
|
|
}
|
|
|
|
if responsePacket[0] != 2 { // Type: Response
|
|
|
|
log.Fatalf("response packet type wrong: want %d, got %d", 2, responsePacket[0])
|
|
|
|
}
|
2016-12-25 21:01:06 +01:00
|
|
|
if responsePacket[1] != 0 || responsePacket[2] != 0 || responsePacket[3] != 0 {
|
|
|
|
log.Fatalf("response packet has non-zero reserved fields")
|
|
|
|
}
|
|
|
|
theirIndex := binary.LittleEndian.Uint32(responsePacket[4:])
|
|
|
|
ourIndex := binary.LittleEndian.Uint32(responsePacket[8:])
|
2016-07-07 01:49:30 +02:00
|
|
|
if ourIndex != 28 {
|
|
|
|
log.Fatalf("response packet index wrong: want %d, got %d", 28, ourIndex)
|
|
|
|
}
|
2016-12-25 21:01:06 +01:00
|
|
|
payload, sendCipher, receiveCipher, err := hs.ReadMessage(nil, responsePacket[12:60])
|
2016-07-07 01:49:30 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error reading handshake message: %s", err)
|
|
|
|
}
|
|
|
|
if len(payload) > 0 {
|
|
|
|
log.Fatalf("unexpected payload: %x", payload)
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2016-07-07 04:57:08 +02:00
|
|
|
// write ICMP Echo packet
|
|
|
|
pingMessage, _ := (&icmp.Message{
|
|
|
|
Type: ipv4.ICMPTypeEcho,
|
|
|
|
Body: &icmp.Echo{
|
2016-07-07 13:44:01 +02:00
|
|
|
ID: 921,
|
|
|
|
Seq: 438,
|
2016-07-07 04:57:08 +02:00
|
|
|
Data: []byte("WireGuard"),
|
|
|
|
},
|
|
|
|
}).Marshal(nil)
|
|
|
|
pingHeader, err := (&ipv4.Header{
|
|
|
|
Version: ipv4.Version,
|
|
|
|
Len: ipv4.HeaderLen,
|
|
|
|
TotalLen: ipv4.HeaderLen + len(pingMessage),
|
|
|
|
Protocol: 1, // ICMP
|
2016-07-07 13:42:35 +02:00
|
|
|
TTL: 20,
|
2016-07-07 04:57:08 +02:00
|
|
|
Src: net.IPv4(10, 189, 129, 2),
|
|
|
|
Dst: net.IPv4(10, 189, 129, 1),
|
|
|
|
}).Marshal()
|
|
|
|
binary.BigEndian.PutUint16(pingHeader[2:], uint16(ipv4.HeaderLen+len(pingMessage))) // fix the length endianness on BSDs
|
2017-06-01 06:31:26 +02:00
|
|
|
pingData := append(append(pingHeader, pingMessage...), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
2016-07-07 13:58:30 +02:00
|
|
|
binary.BigEndian.PutUint16(pingData[10:], ipChecksum(pingData))
|
2016-12-25 21:01:06 +01:00
|
|
|
pingPacket := make([]byte, 16)
|
2017-06-01 19:08:24 +02:00
|
|
|
pingPacket[0] = 4 // Type: Data
|
|
|
|
pingPacket[1] = 0 // Reserved
|
|
|
|
pingPacket[2] = 0 // Reserved
|
|
|
|
pingPacket[3] = 0 // Reserved
|
|
|
|
binary.LittleEndian.PutUint32(pingPacket[4:], theirIndex) // Their index
|
|
|
|
binary.LittleEndian.PutUint64(pingPacket[8:], 0) // Nonce
|
|
|
|
pingPacket = sendCipher.Encrypt(pingPacket, nil, pingData) // Payload data
|
2016-07-07 04:57:08 +02:00
|
|
|
if _, err := conn.Write(pingPacket); err != nil {
|
|
|
|
log.Fatalf("error writing ping message: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read ICMP Echo Reply packet
|
2017-06-01 06:31:26 +02:00
|
|
|
replyPacket := make([]byte, 80)
|
2016-07-07 04:57:08 +02:00
|
|
|
n, err = conn.Read(replyPacket)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error reading ping reply message: %s", err)
|
|
|
|
}
|
|
|
|
replyPacket = replyPacket[:n]
|
|
|
|
if replyPacket[0] != 4 { // Type: Data
|
|
|
|
log.Fatalf("unexpected reply packet type: %d", replyPacket[0])
|
|
|
|
}
|
2016-12-25 21:01:06 +01:00
|
|
|
if replyPacket[1] != 0 || replyPacket[2] != 0 || replyPacket[3] != 0 {
|
|
|
|
log.Fatalf("reply packet has non-zero reserved fields")
|
|
|
|
}
|
|
|
|
replyPacket, err = receiveCipher.Decrypt(nil, nil, replyPacket[16:])
|
2016-07-07 04:57:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error decrypting reply packet: %s", err)
|
|
|
|
}
|
|
|
|
replyHeaderLen := int(replyPacket[0]&0x0f) << 2
|
|
|
|
replyLen := binary.BigEndian.Uint16(replyPacket[2:])
|
|
|
|
replyMessage, err := icmp.ParseMessage(1, replyPacket[replyHeaderLen:replyLen])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error parsing echo: %s", err)
|
|
|
|
}
|
|
|
|
echo, ok := replyMessage.Body.(*icmp.Echo)
|
|
|
|
if !ok {
|
|
|
|
log.Fatalf("unexpected reply body type %T", replyMessage.Body)
|
|
|
|
}
|
|
|
|
|
2016-07-07 13:44:01 +02:00
|
|
|
if echo.ID != 921 || echo.Seq != 438 || string(echo.Data) != "WireGuard" {
|
2016-07-07 04:57:08 +02:00
|
|
|
log.Fatalf("incorrect echo response: %#v", echo)
|
2016-07-07 01:49:30 +02:00
|
|
|
}
|
2017-06-01 18:45:20 +02:00
|
|
|
|
|
|
|
keepalivePacket := make([]byte, 16)
|
2017-06-01 19:08:24 +02:00
|
|
|
keepalivePacket[0] = 4 // Type: Data
|
|
|
|
keepalivePacket[1] = 0 // Reserved
|
|
|
|
keepalivePacket[2] = 0 // Reserved
|
|
|
|
keepalivePacket[3] = 0 // Reserved
|
|
|
|
binary.LittleEndian.PutUint32(keepalivePacket[4:], theirIndex) // Their index
|
|
|
|
binary.LittleEndian.PutUint64(keepalivePacket[8:], 1) // Nonce
|
2017-06-01 18:45:20 +02:00
|
|
|
keepalivePacket = sendCipher.Encrypt(keepalivePacket, nil, nil) // Empty data means keepalive
|
|
|
|
if _, err := conn.Write(keepalivePacket); err != nil {
|
|
|
|
log.Fatalf("error writing keepalive message: %s", err)
|
|
|
|
}
|
2015-06-05 15:58:00 +02:00
|
|
|
}
|