]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/identity.go
Fix handshake peer identification
[govpn.git] / src / cypherpunks.ru / govpn / identity.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package govpn
20
21 import (
22         "crypto/subtle"
23         "encoding/base64"
24         "encoding/binary"
25         "hash"
26         "log"
27         "sync"
28         "time"
29
30         "golang.org/x/crypto/blake2b"
31 )
32
33 const (
34         IDSize = 128 / 8
35 )
36
37 type PeerId [IDSize]byte
38
39 func (id PeerId) String() string {
40         return base64.RawStdEncoding.EncodeToString(id[:])
41 }
42
43 func (id PeerId) MarshalJSON() ([]byte, error) {
44         return []byte(`"` + id.String() + `"`), nil
45 }
46
47 type MACAndTimeSync struct {
48         mac hash.Hash
49         ts  int
50         l   sync.Mutex
51 }
52
53 type MACCache struct {
54         cache map[PeerId]*MACAndTimeSync
55         l     sync.RWMutex
56 }
57
58 func NewMACCache() *MACCache {
59         return &MACCache{cache: make(map[PeerId]*MACAndTimeSync)}
60 }
61
62 // Remove disappeared keys, add missing ones with initialized MACs.
63 func (mc *MACCache) Update(peers *map[PeerId]*PeerConf) {
64         mc.l.Lock()
65         for pid, _ := range mc.cache {
66                 if _, exists := (*peers)[pid]; !exists {
67                         log.Println("Cleaning key:", pid)
68                         delete(mc.cache, pid)
69                 }
70         }
71         for pid, pc := range *peers {
72                 if _, exists := mc.cache[pid]; exists {
73                         mc.cache[pid].ts = pc.TimeSync
74                 } else {
75                         log.Println("Adding key", pid)
76                         mac, err := blake2b.New256(pid[:])
77                         if err != nil {
78                                 panic(err)
79                         }
80                         mc.cache[pid] = &MACAndTimeSync{
81                                 mac: mac,
82                                 ts:  pc.TimeSync,
83                         }
84                 }
85         }
86         mc.l.Unlock()
87 }
88
89 // If timeSync > 0, then XOR timestamp with the data.
90 func AddTimeSync(ts int, data []byte) {
91         if ts == 0 {
92                 return
93         }
94         buf := make([]byte, 8)
95         binary.BigEndian.PutUint64(buf, uint64(time.Now().Unix()/int64(ts)*int64(ts)))
96         for i := 0; i < 8; i++ {
97                 data[i] ^= buf[i]
98         }
99 }
100
101 // Try to find peer's identity (that equals to MAC)
102 // by taking first blocksize sized bytes from data at the beginning
103 // as plaintext and last bytes as cyphertext.
104 func (mc *MACCache) Find(data []byte) *PeerId {
105         if len(data) < 8*2 {
106                 return nil
107         }
108         buf := make([]byte, 8)
109         mc.l.RLock()
110         for pid, mt := range mc.cache {
111                 copy(buf, data)
112                 AddTimeSync(mt.ts, buf)
113                 mt.l.Lock()
114                 mt.mac.Reset()
115                 mt.mac.Write(buf)
116                 sum := mt.mac.Sum(nil)
117                 mt.l.Unlock()
118                 if subtle.ConstantTimeCompare(sum[len(sum)-8:], data[len(data)-8:]) == 1 {
119                         ppid := PeerId(pid)
120                         mc.l.RUnlock()
121                         return &ppid
122                 }
123         }
124         mc.l.RUnlock()
125         return nil
126 }