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