]> Cypherpunks.ru repositories - govpn.git/blob - verifier.go
Raise copyright years
[govpn.git] / verifier.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2020 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package govpn
19
20 import (
21         "bytes"
22         "encoding/base64"
23         "errors"
24         "fmt"
25         "hash"
26         "io/ioutil"
27         "log"
28         "os"
29         "strings"
30
31         "github.com/agl/ed25519"
32         "go.cypherpunks.ru/balloon"
33         "golang.org/x/crypto/blake2b"
34         "golang.org/x/crypto/ssh/terminal"
35 )
36
37 const (
38         DefaultS = 1 << 20 / 32
39         DefaultT = 1 << 4
40         DefaultP = 2
41 )
42
43 type Verifier struct {
44         S   int
45         T   int
46         P   int
47         ID  *PeerID
48         Pub *[ed25519.PublicKeySize]byte
49 }
50
51 // Generate new verifier for given peer, with specified password and
52 // hashing parameters.
53 func VerifierNew(s, t, p int, id *PeerID) *Verifier {
54         return &Verifier{S: s, T: t, P: p, ID: id}
55 }
56
57 func blake2bKeyless() hash.Hash {
58         h, err := blake2b.New256(nil)
59         if err != nil {
60                 panic(err)
61         }
62         return h
63 }
64
65 // Apply the password: create Ed25519 keypair based on it, save public
66 // key in verifier.
67 func (v *Verifier) PasswordApply(password string) *[ed25519.PrivateKeySize]byte {
68         r := balloon.H(blake2bKeyless, []byte(password), v.ID[:], v.S, v.T, v.P)
69         defer SliceZero(r)
70         src := bytes.NewBuffer(r)
71         pub, prv, err := ed25519.GenerateKey(src)
72         if err != nil {
73                 log.Fatalln("Unable to generate Ed25519 keypair", err)
74         }
75         v.Pub = pub
76         return prv
77 }
78
79 // Parse either short or long verifier form.
80 func VerifierFromString(input string) (*Verifier, error) {
81         ss := strings.Split(input, "$")
82         if len(ss) < 4 || ss[1] != "balloon" {
83                 return nil, errors.New("Invalid verifier structure")
84         }
85         var s, t, p int
86         n, err := fmt.Sscanf(ss[2], "s=%d,t=%d,p=%d", &s, &t, &p)
87         if n != 3 || err != nil {
88                 return nil, errors.New("Invalid verifier parameters")
89         }
90         salt, err := base64.RawStdEncoding.DecodeString(ss[3])
91         if err != nil {
92                 return nil, err
93         }
94         v := Verifier{S: s, T: t, P: p}
95         id := new([IDSize]byte)
96         copy(id[:], salt)
97         pid := PeerID(*id)
98         v.ID = &pid
99         if len(ss) == 5 {
100                 pub, err := base64.RawStdEncoding.DecodeString(ss[4])
101                 if err != nil {
102                         return nil, err
103                 }
104                 v.Pub = new([ed25519.PublicKeySize]byte)
105                 copy(v.Pub[:], pub)
106         }
107         return &v, nil
108 }
109
110 // Short verifier string form -- it is useful for the client.
111 // Does not include public key.
112 func (v *Verifier) ShortForm() string {
113         return fmt.Sprintf(
114                 "$balloon$s=%d,t=%d,p=%d$%s",
115                 v.S, v.T, v.P, base64.RawStdEncoding.EncodeToString(v.ID[:]),
116         )
117 }
118
119 // Long verifier string form -- it is useful for the server.
120 // Includes public key.
121 func (v *Verifier) LongForm() string {
122         return fmt.Sprintf(
123                 "%s$%s", v.ShortForm(),
124                 base64.RawStdEncoding.EncodeToString(v.Pub[:]),
125         )
126 }
127
128 // Read the key either from text file (if path is specified), or
129 // from the terminal.
130 func KeyRead(path string) (string, error) {
131         var p []byte
132         var err error
133         var pass string
134         if path == "" {
135                 os.Stderr.WriteString("Passphrase:")
136                 p, err = terminal.ReadPassword(0)
137                 os.Stderr.WriteString("\n")
138                 pass = string(p)
139         } else {
140                 p, err = ioutil.ReadFile(path)
141                 pass = strings.TrimRight(string(p), "\n")
142         }
143         if err != nil {
144                 return "", err
145         }
146         if len(pass) == 0 {
147                 return "", errors.New("Empty passphrase submitted")
148         }
149         return pass, err
150 }