-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate.go
194 lines (159 loc) · 4.43 KB
/
calculate.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package ykoath
import (
"encoding/binary"
"errors"
"fmt"
"strings"
"cunicu.li/go-iso7816/encoding/tlv"
)
var (
ErrNoValuesFound = errors.New("no values found in response")
ErrUnknownName = errors.New("no such name configured")
ErrMultipleMatches = errors.New("multiple matches found")
ErrTouchRequired = errors.New("touch required")
ErrTouchCallbackRequired = errors.New("touch callback required")
ErrChallengeRequired = errors.New("challenge required")
)
// CalculateMatch is a high-level function that first identifies all TOTP credentials
// that are configured and returns the matching one (if no touch is required) or
// fires the callback and then fetches the name again while blocking during
// the device awaiting touch
func (c *Card) CalculateMatch(name string, touchRequiredCallback func(string) error) (string, error) {
totpChallenge := c.totpChallenge()
codes, err := c.calculateAll(totpChallenge, true)
if err != nil {
return "", err
}
// Support matching by name without issuer in the same way that ykman does
// https://github.com/Yubico/yubikey-manager/blob/f493008d78a0ad09016f23dabd1cb658929d9c0e/ykman/cli/oath.py#L543
var key string
var code Code
var matches []string
for k, c := range codes {
if strings.Contains(strings.ToLower(k), strings.ToLower(name)) {
key = k
code = c
matches = append(matches, k)
}
}
if len(matches) > 1 {
return "", fmt.Errorf("%w: %s", ErrMultipleMatches, strings.Join(matches, ","))
}
if key == "" {
return "", fmt.Errorf("%w: %s", ErrUnknownName, name)
}
if code.TouchRequired || code.Type == Hotp {
if code.TouchRequired {
if touchRequiredCallback == nil {
return "", ErrTouchCallbackRequired
}
if err := touchRequiredCallback(key); err != nil {
return "", err
}
}
var challenge []byte
if code.Type == Totp {
challenge = totpChallenge
}
if code, err = c.calculate(key, challenge, true); err != nil {
return "", err
}
}
return code.OTP(), nil
}
func (c *Card) Calculate(name string) (string, error) {
d, err := c.calculate(name, c.totpChallenge(), true)
if err != nil {
return "", err
}
return d.OTP(), nil
}
func (c *Card) CalculateChallengeResponse(name string, challenge []byte) ([]byte, int, error) {
d, err := c.calculate(name, challenge, false)
if err != nil {
return nil, -1, err
}
return d.Hash, d.Digits, nil
}
// calculate implements the "CALCULATE" instruction
func (c *Card) calculate(name string, challenge []byte, truncate bool) (Code, error) {
var trunc byte
if truncate {
trunc = 0x01
}
tvs, err := c.send(insCalculate, 0x00, trunc,
tlv.New(tagName, []byte(name)),
tlv.New(tagChallenge, challenge),
)
if err != nil {
return Code{}, err
}
for _, tv := range tvs {
switch tv.Tag {
case tagResponse, tagTruncated:
digits := int(tv.Value[0])
hash := tv.Value[1:]
return Code{
Hash: hash,
Digits: digits,
Truncated: tv.Tag == tagTruncated,
}, nil
default:
return Code{}, fmt.Errorf("%w: %x", errUnknownTag, tv.Tag)
}
}
return Code{}, ErrNoValuesFound
}
// calculateAll implements the "CALCULATE ALL" instruction to fetch all TOTP
// tokens and their codes (or a constant indicating a touch requirement)
func (c *Card) calculateAll(challenge []byte, truncate bool) (map[string]Code, error) {
var (
codes []Code
names []string
trunc byte
)
if truncate {
trunc = 0x01
}
tvs, err := c.send(insCalculateAll, 0x00, trunc,
tlv.New(tagChallenge, challenge),
)
if err != nil {
return nil, err
}
for _, tv := range tvs {
switch tv.Tag {
case tagName:
names = append(names, string(tv.Value))
case tagTouch:
codes = append(codes, Code{
Type: Totp,
TouchRequired: true,
})
case tagResponse, tagTruncated:
codes = append(codes, Code{
Type: Totp,
Hash: tv.Value[1:],
Digits: int(tv.Value[0]),
Truncated: tv.Tag == tagTruncated,
})
case tagHOTP:
codes = append(codes, Code{
Type: Hotp,
})
default:
return nil, fmt.Errorf("%w (%#x)", errUnknownTag, tv.Tag)
}
}
all := make(map[string]Code, len(names))
for idx, name := range names {
all[name] = codes[idx]
}
return all, nil
}
func (c *Card) totpChallenge() []byte {
counter := c.Clock().Unix() / int64(c.Timestep.Seconds())
return binary.BigEndian.AppendUint64(nil, uint64(counter))
}