-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
319 lines (295 loc) Β· 7 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// gauth is a two-factor authentication agent.
//
// Usage:
//
// gauth -add [-7] [-8] [-hotp] name
// gauth -list
// gauth name
//
// To add a new key to keychain use "gauth -add name", where name is a given name.
// It'll prompt a 2fa key from stdin
// 2fa keys are case-insensitive strings [A-Z2-7].
//
// Default generation algorithm is time based auth codes
// (TOTP - the same as Google Authenticator)
//
// There is also EXPERIMENTAL support of counter based auth codes (HOTP).
//
//
// To list all names in the keychain use "gauth -list"
//
// To print certain 2fa auth code use "gauth name"
//
// If no arguments are provided, gauth prints all 2fa TOTP auth codes.
//
// IMPORTANT NOTE:
// TOTP auth codes are derived from key hash and current time.
// Please ensure that system clock are adjusted via NTP.
// Acceptable fault threshold is about ~1 min.
//
// The keychain itself is stored UNENCRYPTED in $HOME/.gauth.
// Take measures to encrypt your partitions (haven't you done this yet?)
//
// Example
//
// While Google 2fa setup select "enter this text code instead"
// bypassing QR code scanning. You will get your 2fa secret - short string.
//
// Add it to 2fa under the name google, typing the secret at the prompt:
//
// $ gauth -add google
// gauth key for google: <secret>
// $
//
// Whenever Google prompts for a 2fa code, run gauth to obtain one:
//
// $ gauth google
// 438163
//
package main
import (
"bufio"
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"unicode"
)
// Keychain is a file format storage.
type Keychain struct {
file string
data []byte
keys map[string]Key
}
// Key describes `keys` in Keychain
type Key struct {
raw []byte
digits int // length
offset int // counter offset
}
const counterLen = 20
var (
flagAdd = flag.Bool("add", false, "add a key")
flagList = flag.Bool("list", false, "list keys")
flagHotp = flag.Bool("hotp", false, "add key as HOTP (counter-based) key")
)
func help() {
fmt.Fprintf(os.Stderr, "usage:\n")
fmt.Fprintf(os.Stderr, "\t%s -add [-hotp] keyname\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s -list\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s keyname\n", os.Args[0])
os.Exit(1)
}
// Read line by line into memory
// handling key length and validity
func readKeychain(file string) *Keychain {
c := &Keychain{
file: file,
keys: make(map[string]Key),
}
data, err := ioutil.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return c
}
log.Fatal(err)
}
c.data = data
lines := bytes.SplitAfter(data, []byte("\n"))
offset := 0
for i, line := range lines {
lineno := i + 1
offset += len(line)
f := bytes.Split(bytes.TrimSuffix(line, []byte("\n")), []byte(" "))
if len(f) == 1 && len(f[0]) == 0 {
continue
}
if len(f) >= 3 && len(f[1]) == 1 && '6' <= f[1][0] && f[1][0] <= '8' {
var k Key
name := string(f[0])
k.digits = int(f[1][0] - '0')
raw, err := decodeKey(string(f[2]))
if err == nil {
k.raw = raw
if len(f) == 3 {
c.keys[name] = k
continue
}
if len(f) == 4 && len(f[3]) == counterLen {
_, err := strconv.ParseUint(string(f[3]), 10, 64)
// even in case of err handle counter and pass it further
if err == nil {
k.offset = offset - counterLen
if line[len(line)-1] == '\n' {
k.offset--
}
c.keys[name] = k
continue
}
}
}
}
log.Printf("%s:%d: invalid key", c.file, lineno)
}
return c
}
// dump 2fa list
func (c *Keychain) list() {
var names []string
for name := range c.keys {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
fmt.Println(name)
}
}
func checkSpace(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}
// handle flag conflicts and verify key validity
func (c *Keychain) add(name string) {
size := 6
fmt.Fprintf(os.Stderr, "gauth key for %s: ", name)
text, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
log.Fatalf("error reading key: %v", err)
}
text = strings.Map(checkSpace, text)
if _, err := decodeKey(text); err != nil {
log.Fatalf("invalid key: %v", err)
}
line := fmt.Sprintf("%s %d %s", name, size, text)
if *flagHotp {
line += " " + strings.Repeat("0", 20)
}
line += "\n"
f, err := os.OpenFile(c.file, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600)
if err != nil {
log.Fatalf("opening keychain: %v", err)
}
// vital
f.Chmod(0600)
if _, err := f.Write([]byte(line)); err != nil {
log.Fatalf("adding key: %v", err)
}
if err := f.Close(); err != nil {
log.Fatalf("closing keychain while adding key: %v", err)
}
}
func (c *Keychain) code(name string) string {
k, ok := c.keys[name]
if !ok {
log.Fatalf("no such key %q", name)
}
var code int
if k.offset != 0 {
n, err := strconv.ParseUint(string(c.data[k.offset:k.offset+counterLen]), 10, 64)
if err != nil {
log.Fatalf("invalid key counter for %q (%q)", name, c.data[k.offset:k.offset+counterLen])
}
n++
code = genHOTP(k.raw, n, k.digits)
f, err := os.OpenFile(c.file, os.O_RDWR, 0600)
if err != nil {
log.Fatalf("opening keychain: %v", err)
}
if _, err := f.WriteAt([]byte(fmt.Sprintf("%0*d", counterLen, n)), int64(k.offset)); err != nil {
log.Fatalf("updating keychain: %v", err)
}
if err := f.Close(); err != nil {
log.Fatalf("closing keychain while updating keychain: %v", err)
}
} else {
// Time-based key.
code = genTOTP(k.raw, time.Now(), k.digits)
}
return fmt.Sprintf("%0*d", k.digits, code)
}
func (c *Keychain) print(name string) {
fmt.Printf("%s\n", c.code(name))
}
func (c *Keychain) printAll() {
var names []string
max := 0
maxDigits := 0
for name, k := range c.keys {
names = append(names, name)
if max < len(name) {
max = len(name)
}
if max < k.digits {
max = k.digits
}
}
sort.Strings(names)
for _, name := range names {
k := c.keys[name]
code := strings.Repeat("-", k.digits)
if k.offset == 0 {
code = c.code(name)
}
fmt.Printf("%-*s\t%s\n", maxDigits, code, name)
}
}
func decodeKey(key string) ([]byte, error) {
return base32.StdEncoding.DecodeString(strings.ToUpper(key))
}
func genTOTP(key []byte, t time.Time, digits int) int {
return genHOTP(key, uint64(t.UnixNano())/30e9, digits)
}
func genHOTP(key []byte, counter uint64, digits int) int {
h := hmac.New(sha1.New, key)
binary.Write(h, binary.BigEndian, counter)
sum := h.Sum(nil)
v := binary.BigEndian.Uint32(sum[sum[len(sum)-1]&0x0F:]) & 0x7FFFFFFF
d := uint32(1)
for i := 0; i < digits && i < 8; i++ {
d *= 10
}
return int(v % d)
}
func main() {
log.SetPrefix("gauth: ")
log.SetFlags(0)
flag.Usage = help
flag.Parse()
k := readKeychain(filepath.Join(os.Getenv("HOME"), ".gauth"))
if *flagList {
if flag.NArg() != 0 {
help()
}
k.list()
return
}
if flag.NArg() == 0 && !*flagAdd {
k.printAll()
return
}
if flag.NArg() != 1 {
help()
}
name := flag.Arg(0)
if strings.IndexFunc(name, unicode.IsSpace) >= 0 {
log.Fatal("spaces aren't allowed")
}
if *flagAdd {
k.add(name)
return
}
k.print(name)
}