-
Notifications
You must be signed in to change notification settings - Fork 0
/
select.go
58 lines (45 loc) · 1.1 KB
/
select.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
// SPDX-FileCopyrightText: 2018 Joern Barthel <joern.barthel@kreuzwerker.de>
// SPDX-License-Identifier: Apache-2.0
package ykoath
import (
"fmt"
iso "cunicu.li/go-iso7816"
"cunicu.li/go-iso7816/encoding/tlv"
)
// Select encapsulates the results of the "SELECT" instruction
type Select struct {
Algorithm []byte
Challenge []byte
Name []byte
Version []byte
}
func (s *Select) UnmarshalBinary(b []byte) error {
tvs, err := tlv.DecodeSimple(b)
if err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
for _, tv := range tvs {
switch tv.Tag {
case tagAlgorithm:
s.Algorithm = tv.Value
case tagChallenge:
s.Challenge = tv.Value
case tagName:
s.Name = tv.Value
case tagVersion:
s.Version = tv.Value
default:
return fmt.Errorf("%w (%#x)", errUnknownTag, tv.Tag)
}
}
return nil
}
// Select sends a "SELECT" instruction, initializing the device for an OATH session
func (c *Card) Select() (*Select, error) {
resp, err := c.Card.Select(iso.AidYubicoOATH)
if err != nil {
return nil, wrapError(err)
}
s := &Select{}
return s, s.UnmarshalBinary(resp)
}