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