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