]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/identity.go
4dc74eed0e330f4a3ec73570e0c9d73b2503baee
[govpn.git] / src / cypherpunks.ru / govpn / identity.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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         "github.com/dchest/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                         mc.cache[pid] = &MACAndTimeSync{
77                                 mac: blake2b.NewMAC(8, pid[:]),
78                                 ts:  pc.TimeSync,
79                         }
80                 }
81         }
82         mc.l.Unlock()
83 }
84
85 // If timeSync > 0, then XOR timestamp with the data.
86 func AddTimeSync(ts int, data []byte) {
87         if ts == 0 {
88                 return
89         }
90         buf := make([]byte, 8)
91         binary.BigEndian.PutUint64(buf, uint64(time.Now().Unix()/int64(ts)*int64(ts)))
92         for i := 0; i < 8; i++ {
93                 data[i] ^= buf[i]
94         }
95 }
96
97 // Try to find peer's identity (that equals to MAC)
98 // by taking first blocksize sized bytes from data at the beginning
99 // as plaintext and last bytes as cyphertext.
100 func (mc *MACCache) Find(data []byte) *PeerId {
101         if len(data) < 8*2 {
102                 return nil
103         }
104         buf := make([]byte, 8)
105         mc.l.RLock()
106         for pid, mt := range mc.cache {
107                 copy(buf, data)
108                 AddTimeSync(mt.ts, buf)
109                 mt.l.Lock()
110                 mt.mac.Reset()
111                 mt.mac.Write(buf)
112                 mt.mac.Sum(buf[:0])
113                 mt.l.Unlock()
114                 if subtle.ConstantTimeCompare(buf, data[len(data)-8:]) == 1 {
115                         ppid := PeerId(pid)
116                         mc.l.RUnlock()
117                         return &ppid
118                 }
119         }
120         mc.l.RUnlock()
121         return nil
122 }