2019-03-11 11:24:09 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/status-im/keycard-go/apdu"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrWrongApplicationInfoTemplate = errors.New("wrong application info template")
|
|
|
|
|
2019-03-11 15:12:19 +00:00
|
|
|
type Capability uint8
|
|
|
|
|
2019-03-11 11:24:09 +00:00
|
|
|
const (
|
2019-03-11 15:12:19 +00:00
|
|
|
TagSelectResponsePreInitialized uint8 = 0x80
|
|
|
|
TagApplicationStatusTemplate uint8 = 0xA3
|
|
|
|
TagApplicationInfoTemplate uint8 = 0xA4
|
|
|
|
TagApplicationInfoCapabilities uint8 = 0x8D
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CapabilitySecureChannel Capability = iota + 1
|
|
|
|
CapabilityKeyManagement Capability = 1 << iota
|
|
|
|
CapabilityCredentialsManagement Capability = 1 << iota
|
|
|
|
CapabilityNDEF Capability = 1 << iota
|
|
|
|
|
|
|
|
CapabilityAll = CapabilitySecureChannel |
|
|
|
|
CapabilityKeyManagement |
|
|
|
|
CapabilityCredentialsManagement |
|
|
|
|
CapabilityNDEF
|
2019-03-11 11:24:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ApplicationInfo struct {
|
|
|
|
Installed bool
|
|
|
|
Initialized bool
|
|
|
|
InstanceUID []byte
|
|
|
|
PublicKey []byte
|
|
|
|
Version []byte
|
|
|
|
AvailableSlots []byte
|
|
|
|
// KeyUID is the sha256 of of the master public key on the card.
|
|
|
|
// It's empty if the card doesn't contain any key.
|
2019-03-11 15:12:19 +00:00
|
|
|
KeyUID []byte
|
|
|
|
Capabilities Capability
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ApplicationInfo) HasCapability(c Capability) bool {
|
|
|
|
return a.Capabilities&c == c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ApplicationInfo) HasSecureChannelCapability() bool {
|
|
|
|
return a.HasCapability(CapabilitySecureChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ApplicationInfo) HasKeyManagementCapability() bool {
|
|
|
|
return a.HasCapability(CapabilityKeyManagement)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ApplicationInfo) HasCredentialsManagementCapability() bool {
|
|
|
|
return a.HasCapability(CapabilityCredentialsManagement)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ApplicationInfo) HasNDEFCapability() bool {
|
|
|
|
return a.HasCapability(CapabilityNDEF)
|
2019-03-11 11:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseApplicationInfo(data []byte) (info ApplicationInfo, err error) {
|
|
|
|
info.Installed = true
|
|
|
|
if data[0] == TagSelectResponsePreInitialized {
|
|
|
|
info.PublicKey = data[2:]
|
2019-03-11 15:12:19 +00:00
|
|
|
info.Capabilities = CapabilityCredentialsManagement
|
2019-03-11 11:24:09 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Initialized = true
|
|
|
|
|
|
|
|
if data[0] != TagApplicationInfoTemplate {
|
|
|
|
return info, ErrWrongApplicationInfoTemplate
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceUID, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x8F))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x80))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
appVersion, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x02))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
availableSlots, err := apdu.FindTagN(data, 1, TagApplicationInfoTemplate, uint8(0x02))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyUID, err := apdu.FindTagN(data, 0, TagApplicationInfoTemplate, uint8(0x8E))
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
2019-03-11 15:12:19 +00:00
|
|
|
capabilities := CapabilityAll
|
|
|
|
capabilitiesBytes, err := apdu.FindTag(data, TagApplicationInfoCapabilities)
|
|
|
|
if err == nil && len(capabilitiesBytes) > 0 {
|
|
|
|
capabilities = Capability(capabilitiesBytes[0])
|
|
|
|
}
|
|
|
|
|
2019-03-11 11:24:09 +00:00
|
|
|
info.InstanceUID = instanceUID
|
|
|
|
info.PublicKey = pubKey
|
|
|
|
info.Version = appVersion
|
|
|
|
info.AvailableSlots = availableSlots
|
|
|
|
info.KeyUID = keyUID
|
2019-03-11 15:12:19 +00:00
|
|
|
info.Capabilities = capabilities
|
2019-03-11 11:24:09 +00:00
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|