-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeydb.go
144 lines (128 loc) · 3.11 KB
/
keydb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package keydb
import (
"bufio"
"crypto/aes"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/darkskiez/u2fhost"
)
type AuthorisedKey struct {
U2FKeyHandle, PublicKeyHash []byte
}
type AuthorisedKeys []AuthorisedKey
// KeyHandler interface
func (a AuthorisedKey) KeyHandle() u2fhost.KeyHandle {
return a.U2FKeyHandle
}
func (a AuthorisedKey) String() string {
return base64.StdEncoding.EncodeToString(a.U2FKeyHandle) + " " +
base64.StdEncoding.EncodeToString(a.PublicKeyHash)
}
func DecodeString(str string) (AuthorisedKey, error) {
parts := strings.Split(str, " ")
if len(parts) < 2 {
return AuthorisedKey{}, errors.New("expected two parts")
}
kh, err := base64.StdEncoding.DecodeString(parts[0])
if err != nil {
return AuthorisedKey{}, err
}
pkh, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return AuthorisedKey{}, err
}
if len(pkh) > 64 || len(pkh)<2{
return AuthorisedKey{},
fmt.Errorf("keyhash has wrong length (want: 2 to 64, got %v)", len(pkh))
}
return AuthorisedKey{
U2FKeyHandle: kh,
PublicKeyHash: pkh,
}, nil
}
func (a AuthorisedKey) dup() AuthorisedKey {
var d AuthorisedKey
d.PublicKeyHash = make([]byte, len(a.PublicKeyHash))
copy(d.PublicKeyHash, a.PublicKeyHash)
d.U2FKeyHandle = make([]byte, len(a.U2FKeyHandle))
copy(d.U2FKeyHandle, a.U2FKeyHandle)
return d
}
func (a AuthorisedKey) Encrypt(password string) (AuthorisedKey, error) {
h := sha256.Sum256([]byte(password))
cipher, err := aes.NewCipher(h[:])
if err != nil {
return AuthorisedKey{}, err
}
e := a.dup()
cipher.Encrypt(e.U2FKeyHandle, a.U2FKeyHandle)
return e, nil
}
func (a AuthorisedKey) Decrypt(password string) (AuthorisedKey, error) {
h := sha256.Sum256([]byte(password))
cipher, err := aes.NewCipher(h[:])
if err != nil {
return AuthorisedKey{}, err
}
e := a.dup()
cipher.Decrypt(e.U2FKeyHandle, a.U2FKeyHandle)
return e, nil
}
func (aks AuthorisedKeys) KeyHandlers() []u2fhost.KeyHandler {
khs := make([]u2fhost.KeyHandler, len(aks))
for i, v := range aks {
khs[i] = v
}
return khs
}
func LoadKeyfile(filename string) (AuthorisedKeys, error) {
aks := make([]AuthorisedKey, 0, 10)
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close() // nolint: errcheck
scanner := bufio.NewScanner(file)
i := 0
for scanner.Scan() {
i++
ak, err := DecodeString(scanner.Text())
if err != nil {
log.Printf("failed to parse: %v (line %v)", err, i)
continue
}
aks = append(aks, ak)
}
if err := file.Close(); err != nil {
return nil, err
}
return aks, scanner.Err()
}
func (a AuthorisedKey) AppendKeyfile(filename string) error {
w, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer w.Close() // nolint: errcheck
_, err = fmt.Fprintf(w, "%s\n", a.String())
if err != nil {
return err
}
return w.Close()
}
func (aks AuthorisedKeys) Decrypt(password string) (AuthorisedKeys, error) {
var ret AuthorisedKeys
for _, a := range aks {
a, err := a.Decrypt(password)
if err != nil {
return nil, err
}
ret = append(ret, a)
}
return ret, nil
}