forked from status-im/keycard-go
/
application_status.go
58 lines (43 loc) · 1.33 KB
/
application_status.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
package types
import (
"bytes"
"errors"
"github.com/alex-miller-0/keycard-go/apdu"
"github.com/alex-miller-0/keycard-go/derivationpath"
)
const hardenedStart = 0x80000000 // 2^31
var ErrApplicationStatusTemplateNotFound = errors.New("application status template not found")
type ApplicationStatus struct {
PinRetryCount int
PUKRetryCount int
KeyInitialized bool
Path string
}
func ParseApplicationStatus(data []byte) (*ApplicationStatus, error) {
tpl, err := apdu.FindTag(data, TagApplicationStatusTemplate)
if err != nil {
return parseKeyPathStatus(data)
}
appStatus := &ApplicationStatus{}
if pinRetryCount, err := apdu.FindTag(tpl, uint8(0x02)); err == nil && len(pinRetryCount) == 1 {
appStatus.PinRetryCount = int(pinRetryCount[0])
}
if pukRetryCount, err := apdu.FindTagN(tpl, 1, uint8(0x02)); err == nil && len(pukRetryCount) == 1 {
appStatus.PUKRetryCount = int(pukRetryCount[0])
}
if keyInitialized, err := apdu.FindTag(tpl, uint8(0x01)); err == nil {
if bytes.Equal(keyInitialized, []byte{0xFF}) {
appStatus.KeyInitialized = true
}
}
return appStatus, nil
}
func parseKeyPathStatus(data []byte) (*ApplicationStatus, error) {
appStatus := &ApplicationStatus{}
path, err := derivationpath.EncodeFromBytes(data)
if err != nil {
return nil, err
}
appStatus.Path = path
return appStatus, nil
}