keycard-go/types/application_status.go

59 lines
1.3 KiB
Go
Raw Normal View History

2019-03-13 15:20:51 +00:00
package types
import (
"bytes"
"errors"
"github.com/status-im/keycard-go/apdu"
2019-03-15 16:03:01 +00:00
"github.com/status-im/keycard-go/derivationpath"
2019-03-13 15:20:51 +00:00
)
2019-03-15 15:09:38 +00:00
const hardenedStart = 0x80000000 // 2^31
2019-03-13 15:20:51 +00:00
var ErrApplicationStatusTemplateNotFound = errors.New("application status template not found")
type ApplicationStatus struct {
PinRetryCount int
PUKRetryCount int
KeyInitialized bool
2019-03-15 15:09:38 +00:00
Path string
2019-03-13 15:20:51 +00:00
}
func ParseApplicationStatus(data []byte) (*ApplicationStatus, error) {
tpl, err := apdu.FindTag(data, TagApplicationStatusTemplate)
if err != nil {
2019-03-15 15:09:38 +00:00
return parseKeyPathStatus(data)
2019-03-13 15:20:51 +00:00
}
2019-03-15 15:09:38 +00:00
appStatus := &ApplicationStatus{}
2019-03-13 15:20:51 +00:00
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
}
2019-03-15 15:09:38 +00:00
func parseKeyPathStatus(data []byte) (*ApplicationStatus, error) {
appStatus := &ApplicationStatus{}
2019-03-15 16:03:01 +00:00
path, err := derivationpath.EncodeFromBytes(data)
2019-03-15 15:09:38 +00:00
if err != nil {
return nil, err
}
2019-03-15 16:03:01 +00:00
appStatus.Path = path
2019-03-15 15:09:38 +00:00
return appStatus, nil
}