-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
329 lines (292 loc) · 6.96 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
320
321
322
323
324
325
326
327
328
329
// Copyright 2015 Reed O'Brien <[email protected]>.
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"math"
"os"
"strconv"
"strings"
"time"
)
const TIME_FMT = "2006-01-02 15:04:05 MST"
var (
epochSec int64 // Representation of TOTP epoch in seconds
err error
key []byte // The decoded secret
incr func() // function to vary incrementing with window on hotp/totp
secret string // The hex or b32 secret
otp string // If an OTP is supplied for verification
nowSec int64 // Representation of "now" in seconds
// common flags are added in addFlags
b32, verbose *bool
digits, window *int
// cFlags is only here for usage()
cFlag = flag.NewFlagSet("common", flag.ContinueOnError)
// Flags
hFlag = flag.NewFlagSet("hotp", flag.ContinueOnError)
counter = hFlag.Int64("c", 0, "HOTP counter Value")
tFlag = flag.NewFlagSet("totp", flag.ContinueOnError)
now = tFlag.String("N", "", "Use this time as current time for TOTP")
step = tFlag.Int64("s", 30, "The time-step duration")
epoch = tFlag.String("S", "1970-01-01 00:00:00 UTC", "When to start counting time-steps for TOTP")
// Need a usage function since we don't build all flag sets unless the
// program is called correctly.
usage = func() {
fmt.Fprintf(os.Stderr, "usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr,
" %s [hotp|totp] <options> SECRET <OTP>\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, " SECRET is the hex or b32 encoded secret as a string\n")
fmt.Fprintf(os.Stderr, " OTP is a one-time password to validate.\n")
fmt.Fprintf(os.Stderr, "Common options:\n")
addFlags(cFlag)
cFlag.PrintDefaults()
fmt.Fprintf(os.Stderr, "hotp options:\n")
hFlag.PrintDefaults()
fmt.Fprintf(os.Stderr, "totp options:\n")
tFlag.PrintDefaults()
os.Exit(1)
}
)
func main() {
var (
passcode string
generate func() (string, error)
)
if len(os.Args) < 2 {
usage()
}
switch os.Args[1] {
case "hotp":
parseFlags(hFlag)
generate = genHOTP
incr = func() {
*counter++
}
if *verbose {
fmt.Println("Parsed hotp flags.")
if *b32 {
fmt.Println("Base 32 secret", secret)
}
if !*b32 {
fmt.Println("Hex secret:", secret)
}
if otp != "" {
fmt.Println("OTP:", otp)
}
fmt.Println("Digits:", *digits)
fmt.Println("Window size:", *window)
fmt.Println("Start Counter:", *counter)
}
if len(otp) > 0 {
valid := validateHOTP()
if valid {
if *verbose {
fmt.Println()
}
fmt.Println(*counter)
os.Exit(0)
}
if !valid {
fmt.Fprintf(os.Stderr, "%s: validating one time password failed (-2)\n", os.Args[0])
// oathtool exits with this code it seems.
os.Exit(1)
}
}
case "totp":
parseFlags(tFlag)
generate = genTOTP
if *now == "" {
nowSec = time.Now().UTC().Unix()
*now = time.Unix(nowSec, 0).Format(TIME_FMT)
} else {
nowT, err := time.Parse(TIME_FMT, *now)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse now (-N): %s", err)
}
nowSec = nowT.UTC().Unix()
}
if *verbose {
fmt.Println("Parsed totp flags.")
if *b32 {
fmt.Println("Base 32 secret", secret)
}
if !*b32 {
fmt.Println("Hex secret:", secret)
}
if otp != "" {
fmt.Println("OTP:", otp)
}
fmt.Println("Digits:", *digits)
fmt.Println("Window size:", *window)
fmt.Println("Step size (seconds):", *step)
fmt.Println("Start time:", *epoch)
fmt.Println("Current time:", *now)
fmt.Println("Counter:", nowSec / *step)
}
epochT, err := time.Parse(TIME_FMT, *epoch)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse start time (-S): %s\n", err)
}
epochSec = epochT.UTC().Unix()
incr = func() {
nowSec += *step
}
if len(otp) > 0 {
valid := validateTOTP()
if valid {
if *verbose {
fmt.Println()
}
fmt.Println(*counter)
os.Exit(0)
}
if !valid {
fmt.Fprintf(os.Stderr, "%s: validating one time password failed (-2)\n", os.Args[0])
// oathtool exits with this code it seems.
os.Exit(1)
}
}
default:
usage()
}
key, err = getKey()
if err != nil {
fmt.Fprintf(os.Stderr, "Error decoding secret: %s", err)
os.Exit(1)
}
var max int64
if *window > 0 {
max = *counter + int64(*window)
}
if *window == 0 {
max = 0
}
if *verbose {
fmt.Println()
}
for i := int64(0); i <= max; i++ {
passcode, err = generate()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to generate passcode: %s:\n", err)
}
fmt.Println(passcode)
incr()
}
}
//// OTP functions
func genHOTP() (string, error) {
var code uint32
hash := hmac.New(sha1.New, key)
err = binary.Write(hash, binary.BigEndian, *counter)
if err != nil {
return "", err
}
h := hash.Sum(nil)
offset := h[19] & 0x0f
trunc := binary.BigEndian.Uint32(h[offset : offset+4])
trunc &= 0x7fffffff
code = trunc % uint32(math.Pow(10, float64(*digits)))
passcodeFormat := "%0" + strconv.Itoa(*digits) + "d"
return fmt.Sprintf(passcodeFormat, code), nil
}
func genTOTP() (string, error) {
var code string
*counter = nowSec / *step
code, err = genHOTP()
return code, err
}
func validateHOTP() bool {
if len(otp) != *digits {
return false
}
key, err = getKey()
if err != nil {
fmt.Fprintf(os.Stderr, "Error decoding secret: %s", err)
os.Exit(1)
}
for i := 0; i < *window; i++ {
code, _ := genHOTP()
if code == otp {
return true
}
*counter++
}
return false
}
func validateTOTP() bool {
if len(otp) != *digits {
return false
}
key, err = getKey()
if err != nil {
fmt.Fprintf(os.Stderr, "Error decoding secret: %s", err)
os.Exit(1)
}
min := (nowSec / *step) - int64((*window / 2))
max := (nowSec / *step) + int64((*window / 2))
for t := min; t <= max; t++ {
code, _ := genTOTP()
if code == otp {
return true
}
nowSec += *step
}
return false
}
//// Helpers
func getKey() ([]byte, error) {
var (
key []byte
)
if *b32 {
key, err = base32.StdEncoding.DecodeString(secret)
if err != nil {
return key, err
}
}
if !*b32 {
key, err = hex.DecodeString(secret)
if err != nil {
return key, err
}
}
return key, nil
}
//// arg parsing functions
func addFlags(f *flag.FlagSet) {
// common flags add in flagParse method
b32 = f.Bool("b", false, "Use b32 encoding instead of hex")
digits = f.Int("d", 6, "The number of digits in the OTP")
window = f.Int("w", 0, "Window of counter values to test when validating OTPs")
verbose = f.Bool("v", false, "Explain what is being done.")
}
func getPositionalArgs(f *flag.FlagSet) {
secret = strings.ToUpper(f.Arg(0))
secret = strings.Replace(secret, " ", "", -1)
if secret == "" {
usage()
}
if *b32 {
// repad base 32 strings if they are short.
for len(secret) < 32 && len(secret) > 16 {
secret = secret + "="
}
}
otp = f.Arg(1)
}
func parseFlags(f *flag.FlagSet) {
addFlags(f)
err = f.Parse(os.Args[2:])
if err != nil {
os.Exit(1)
}
getPositionalArgs(f)
}