From 0e1c703ad85e5ef8eb33a89f33ce466dd54d5a79 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Tue, 6 Jun 2023 17:14:02 +0200 Subject: [PATCH] add factory reset command --- command_set.go | 6 ++++++ commands.go | 13 +++++++++++++ types/application_info.go | 8 +++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/command_set.go b/command_set.go index 220bed7..d2b598d 100644 --- a/command_set.go +++ b/command_set.go @@ -423,6 +423,12 @@ func (cs *CommandSet) StoreData(typ uint8, data []byte) error { return cs.checkOK(resp, err) } +func (cs *CommandSet) FactoryReset() error { + cmd := NewCommandFactoryReset() + resp, err := cs.sc.Send(cmd) + return cs.checkOK(resp, err) +} + func (cs *CommandSet) mutualAuthenticate() error { data := make([]byte, 32) if _, err := rand.Read(data); err != nil { diff --git a/commands.go b/commands.go index 97452fe..bd9e027 100644 --- a/commands.go +++ b/commands.go @@ -12,6 +12,7 @@ import ( const ( InsInit = 0xFE + InsFactoryReset = 0xFD InsOpenSecureChannel = 0x10 InsMutuallyAuthenticate = 0x11 InsPair = 0x12 @@ -56,6 +57,8 @@ const ( P2ExportKeyPublicOnly = 0x01 P2ExportKeyExtendedPublic = 0x02 P1LoadKeySeed = 0x03 + P1FactoryResetMagic = 0xAA + P2FactoryResetMagic = 0x55 SwNoAvailablePairingSlots = 0x6A84 ) @@ -372,6 +375,16 @@ func NewCommandStoreData(typ uint8, data []byte) *apdu.Command { ) } +func NewCommandFactoryReset() *apdu.Command { + return apdu.NewCommand( + globalplatform.ClaGp, + InsFactoryReset, + P1FactoryResetMagic, + P2FactoryResetMagic, + []byte{}, + ) +} + // Internal function. Get the type of starting point for the derivation path. // Used for both DeriveKey and ExportKey func derivationP1FromStartingPoint(s derivationpath.StartingPoint) (uint8, error) { diff --git a/types/application_info.go b/types/application_info.go index 7eed222..4a3cc26 100644 --- a/types/application_info.go +++ b/types/application_info.go @@ -22,11 +22,13 @@ const ( CapabilityKeyManagement CapabilityCredentialsManagement CapabilityNDEF + CapabilityFactoryReset CapabilityAll = CapabilitySecureChannel | CapabilityKeyManagement | CapabilityCredentialsManagement | - CapabilityNDEF + CapabilityNDEF | + CapabilityFactoryReset ) type ApplicationInfo struct { @@ -62,6 +64,10 @@ func (a *ApplicationInfo) HasNDEFCapability() bool { return a.HasCapability(CapabilityNDEF) } +func (a *ApplicationInfo) HasFactoryResetCapability() bool { + return a.HasCapability(CapabilityFactoryReset) +} + func ParseApplicationInfo(data []byte) (*ApplicationInfo, error) { info := &ApplicationInfo{ Installed: true,