2 govpn -- high-performance secure virtual private network daemon
3 Copyright (C) 2014 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 "code.google.com/p/go.crypto/poly1305"
32 "code.google.com/p/go.crypto/salsa20"
36 remoteAddr = flag.String("remote", "", "Remote server address")
37 bindAddr = flag.String("bind", "", "Bind to address")
38 ifaceName = flag.String("iface", "tap0", "TAP network interface")
39 keyPath = flag.String("key", "", "Path to authentication key file")
40 mtu = flag.Int("mtu", 1500, "MTU")
41 timeout = flag.Int("timeout", 60, "Timeout seconds")
42 verbose = flag.Bool("v", false, "Increase verbosity")
48 // S20BS is Salsa20's internal blocksize in bytes
59 key *[KeySize]byte // encryption key
60 nonceOur uint64 // nonce for our messages
61 nonceRecv uint64 // latest received nonce from remote peer
71 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
74 keyData, err := ioutil.ReadFile(*keyPath)
76 panic("Unable to read keyfile: " + err.Error())
78 if len(keyData) < 64 {
79 panic("Key must be 64 hex characters long")
81 keyDecoded, err := hex.DecodeString(string(keyData[0:64]))
83 panic("Unable to decode the key: " + err.Error())
85 key := new([KeySize]byte)
86 copy(key[:], keyDecoded)
90 // Interface listening
91 maxIfacePktSize := *mtu - poly1305.TagSize - NonceSize
92 log.Println("Max MTU", maxIfacePktSize, "on interface", *ifaceName)
93 iface := NewTAP(*ifaceName)
94 ethBuf := make([]byte, maxIfacePktSize)
95 ethSink := make(chan int)
96 ethSinkReady := make(chan bool)
100 n, err := iface.Read(ethBuf)
109 // Network address parsing
110 if (len(*bindAddr) > 1 && len(*remoteAddr) > 1) ||
111 (len(*bindAddr) == 0 && len(*remoteAddr) == 0) {
112 panic("Either -bind or -remote must be specified only")
114 var conn *net.UDPConn
115 var remote *net.UDPAddr
117 bindTo := "0.0.0.0:0"
119 if len(*bindAddr) > 1 {
124 bind, err := net.ResolveUDPAddr("udp", bindTo)
128 conn, err = net.ListenUDP("udp", bind)
133 if len(*remoteAddr) > 1 {
134 remote, err = net.ResolveUDPAddr("udp", *remoteAddr)
140 udpBuf := make([]byte, *mtu)
141 udpSink := make(chan *UDPPkt)
142 udpSinkReady := make(chan bool)
143 go func(conn *net.UDPConn) {
146 conn.SetReadDeadline(time.Now().Add(time.Second))
147 n, addr, err := conn.ReadFromUDP(udpBuf)
154 udpSink <- &UDPPkt{addr, n}
162 var udpPktData []byte
169 states := make(map[string]*Handshake)
170 nonce := make([]byte, NonceSize)
171 keyAuth := new([KeySize]byte)
172 tag := new([poly1305.TagSize]byte)
173 buf := make([]byte, *mtu+S20BS)
174 emptyKey := make([]byte, KeySize)
175 ethPkt := make([]byte, maxIfacePktSize)
176 udpPktDataBuf := make([]byte, *mtu)
179 states[remote.String()] = HandshakeStart(conn, remote, key)
188 case udpPkt = <-udpSink:
190 if !serverMode && timeouts >= *timeout {
197 copy(udpPktDataBuf, udpBuf[:udpPkt.size])
199 udpPktData = udpPktDataBuf[:udpPkt.size]
200 if isValidHandshakePkt(udpPktData) {
201 addr = udpPkt.addr.String()
202 state, exists := states[addr]
205 state = &Handshake{addr: udpPkt.addr}
208 p = state.Server(conn, key, udpPktData)
214 p = state.Client(conn, key, udpPktData)
226 nonceRecv, _ := binary.Uvarint(udpPktData[:8])
227 if peer.nonceRecv >= nonceRecv {
231 copy(buf[:KeySize], emptyKey)
232 copy(tag[:], udpPktData[udpPkt.size-poly1305.TagSize:])
233 copy(buf[S20BS:], udpPktData[NonceSize:udpPkt.size-poly1305.TagSize])
234 salsa20.XORKeyStream(
235 buf[:S20BS+udpPkt.size-poly1305.TagSize],
236 buf[:S20BS+udpPkt.size-poly1305.TagSize],
237 udpPktData[:NonceSize],
240 copy(keyAuth[:], buf[:KeySize])
241 if !poly1305.Verify(tag, udpPktData[:udpPkt.size-poly1305.TagSize], keyAuth) {
245 peer.nonceRecv = nonceRecv
247 if _, err := iface.Write(buf[S20BS : S20BS+udpPkt.size-NonceSize-poly1305.TagSize]); err != nil {
248 log.Println("Error writing to iface: ", err)
253 case ethPktSize = <-ethSink:
254 if ethPktSize > maxIfacePktSize {
255 panic("Too large packet on interface")
261 copy(ethPkt, ethBuf[:ethPktSize])
263 peer.nonceOur = peer.nonceOur + 2
264 binary.PutUvarint(nonce, peer.nonceOur)
265 copy(buf[:KeySize], emptyKey)
266 copy(buf[S20BS:], ethPkt[:ethPktSize])
267 salsa20.XORKeyStream(buf, buf, nonce, peer.key)
268 copy(buf[S20BS-NonceSize:S20BS], nonce)
269 copy(keyAuth[:], buf[:KeySize])
270 dataToSend := buf[S20BS-NonceSize : S20BS+ethPktSize]
271 poly1305.Sum(tag, dataToSend, keyAuth)
272 if _, err := conn.WriteTo(append(dataToSend, tag[:]...), peer.addr); err != nil {
273 log.Println("Error sending UDP", err)