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