]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/verifier.go
168342fe4dbb5649259b0b10dd4ceb0dd23aba66
[govpn.git] / src / cypherpunks.ru / govpn / verifier.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         "bytes"
23         "encoding/base64"
24         "errors"
25         "fmt"
26         "hash"
27         "io/ioutil"
28         "log"
29         "os"
30         "strings"
31
32         "cypherpunks.ru/balloon"
33         "github.com/agl/ed25519"
34         "golang.org/x/crypto/blake2b"
35         "golang.org/x/crypto/ssh/terminal"
36 )
37
38 const (
39         // DefaultS is default Balloon space cost
40         DefaultS = 1 << 20 / 32
41         // DefaultT is default Balloon time cost
42         DefaultT = 1 << 4
43         // DefaultP is default Balloon number of jobs
44         DefaultP = 2
45 )
46
47 // Verifier is used to authenticate a peer
48 type Verifier struct {
49         S   int
50         T   int
51         P   int
52         ID  *PeerID
53         Pub *[ed25519.PublicKeySize]byte
54 }
55
56 // VerifierNew generates new verifier for given peer,
57 // with specified password and hashing parameters.
58 func VerifierNew(s, t, p int, id *PeerID) *Verifier {
59         return &Verifier{S: s, T: t, P: p, ID: id}
60 }
61
62 func blake2bKeyless() hash.Hash {
63         h, err := blake2b.New256(nil)
64         if err != nil {
65                 panic(err)
66         }
67         return h
68 }
69
70 // PasswordApply applies the password: create Ed25519 keypair based on it,
71 // saves public key in verifier.
72 func (v *Verifier) PasswordApply(password string) *[ed25519.PrivateKeySize]byte {
73         r := balloon.H(blake2bKeyless, []byte(password), v.ID[:], v.S, v.T, v.P)
74         defer SliceZero(r)
75         src := bytes.NewBuffer(r)
76         pub, prv, err := ed25519.GenerateKey(src)
77         if err != nil {
78                 log.Fatalln("Unable to generate Ed25519 keypair", err)
79         }
80         v.Pub = pub
81         return prv
82 }
83
84 // VerifierFromString parses either short or long verifier form.
85 func VerifierFromString(input string) (*Verifier, error) {
86         ss := strings.Split(input, "$")
87         if len(ss) < 4 || ss[1] != "balloon" {
88                 return nil, errors.New("Invalid verifier structure")
89         }
90         var s, t, p int
91         n, err := fmt.Sscanf(ss[2], "s=%d,t=%d,p=%d", &s, &t, &p)
92         if n != 3 || err != nil {
93                 return nil, errors.New("Invalid verifier parameters")
94         }
95         salt, err := base64.RawStdEncoding.DecodeString(ss[3])
96         if err != nil {
97                 return nil, err
98         }
99         v := Verifier{S: s, T: t, P: p}
100         id := new([IDSize]byte)
101         copy(id[:], salt)
102         pid := PeerID(*id)
103         v.ID = &pid
104         if len(ss) == 5 {
105                 pub, err := base64.RawStdEncoding.DecodeString(ss[4])
106                 if err != nil {
107                         return nil, err
108                 }
109                 v.Pub = new([ed25519.PublicKeySize]byte)
110                 copy(v.Pub[:], pub)
111         }
112         return &v, nil
113 }
114
115 // ShortForm outputs the short verifier string form -- it is useful
116 // for the client. It does not include public key.
117 func (v *Verifier) ShortForm() string {
118         return fmt.Sprintf(
119                 "$balloon$s=%d,t=%d,p=%d$%s",
120                 v.S, v.T, v.P, base64.RawStdEncoding.EncodeToString(v.ID[:]),
121         )
122 }
123
124 // LongForm outputs long verifier string form -- it is useful for the server.
125 // It includes public key.
126 func (v *Verifier) LongForm() string {
127         return fmt.Sprintf(
128                 "%s$%s", v.ShortForm(),
129                 base64.RawStdEncoding.EncodeToString(v.Pub[:]),
130         )
131 }
132
133 // KeyRead reads the key either from text file (if path is specified), or
134 // from the terminal.
135 func KeyRead(path string) (string, error) {
136         var p []byte
137         var err error
138         var pass string
139         if path == "" {
140                 os.Stderr.Write([]byte("Passphrase:"))
141                 p, err = terminal.ReadPassword(0)
142                 os.Stderr.Write([]byte("\n"))
143                 pass = string(p)
144         } else {
145                 p, err = ioutil.ReadFile(path)
146                 pass = strings.TrimRight(string(p), "\n")
147         }
148         if err != nil {
149                 return "", err
150         }
151         if len(pass) == 0 {
152                 return "", errors.New("Empty passphrase submitted")
153         }
154         return pass, err
155 }