Skip to content

Commit 7d5886d

Browse files
karalabegballet
authored andcommitted
accounts, console: frendly card errors, support pin unblock
1 parent 3869439 commit 7d5886d

6 files changed

Lines changed: 169 additions & 112 deletions

File tree

‎accounts/scwallet/apdu.go‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,15 @@ import (
2121
"encoding/binary"
2222
)
2323

24-
const (
25-
claISO7816 = 0
26-
27-
insSelect = 0xA4
28-
insGetResponse = 0xC0
29-
insPair = 0x12
30-
insUnpair = 0x13
31-
insOpenSecureChannel = 0x10
32-
insMutuallyAuthenticate = 0x11
33-
34-
sw1GetResponse = 0x61
35-
sw1Ok = 0x90
36-
)
37-
38-
// CommandAPDU represents an application data unit sent to a smartcard.
39-
type CommandAPDU struct {
24+
// commandAPDU represents an application data unit sent to a smartcard.
25+
type commandAPDU struct {
4026
Cla, Ins, P1, P2 uint8 // Class, Instruction, Parameter 1, Parameter 2
4127
Data []byte // Command data
4228
Le uint8 // Command data length
4329
}
4430

4531
// serialize serializes a command APDU.
46-
func (ca CommandAPDU) serialize() ([]byte, error) {
32+
func (ca commandAPDU) serialize() ([]byte, error) {
4733
buf := new(bytes.Buffer)
4834

4935
if err := binary.Write(buf, binary.BigEndian, ca.Cla); err != nil {
@@ -72,14 +58,14 @@ func (ca CommandAPDU) serialize() ([]byte, error) {
7258
return buf.Bytes(), nil
7359
}
7460

75-
// ResponseAPDU represents an application data unit received from a smart card.
76-
type ResponseAPDU struct {
61+
// responseAPDU represents an application data unit received from a smart card.
62+
type responseAPDU struct {
7763
Data []byte // response data
7864
Sw1, Sw2 uint8 // status words 1 and 2
7965
}
8066

8167
// deserialize deserializes a response APDU.
82-
func (ra *ResponseAPDU) deserialize(data []byte) error {
68+
func (ra *responseAPDU) deserialize(data []byte) error {
8369
ra.Data = make([]byte, len(data)-2)
8470

8571
buf := bytes.NewReader(data)

‎accounts/scwallet/hub.go‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"encoding/json"
3737
"io/ioutil"
3838
"os"
39+
"sort"
3940
"sync"
4041
"time"
4142

@@ -51,7 +52,7 @@ const Scheme = "pcsc"
5152

5253
// refreshCycle is the maximum time between wallet refreshes (if USB hotplug
5354
// notifications don't work).
54-
const refreshCycle = 5 * time.Second
55+
const refreshCycle = time.Second
5556

5657
// refreshThrottling is the minimum time between wallet refreshes to avoid thrashing.
5758
const refreshThrottling = 500 * time.Millisecond
@@ -132,7 +133,7 @@ func (hub *Hub) writePairings() error {
132133
return pairingFile.Close()
133134
}
134135

135-
func (hub *Hub) getPairing(wallet *Wallet) *smartcardPairing {
136+
func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing {
136137
pairing, ok := hub.pairings[string(wallet.PublicKey)]
137138
if ok {
138139
return &pairing
@@ -182,13 +183,7 @@ func (hub *Hub) Wallets() []accounts.Wallet {
182183
for _, wallet := range hub.wallets {
183184
cpy = append(cpy, wallet)
184185
}
185-
for i := 0; i < len(cpy); i++ {
186-
for j := i + 1; j < len(cpy); j++ {
187-
if cpy[i].URL().Cmp(cpy[j].URL()) > 0 {
188-
cpy[i], cpy[j] = cpy[j], cpy[i]
189-
}
190-
}
191-
}
186+
sort.Sort(accounts.WalletsByURL(cpy))
192187
return cpy
193188
}
194189

‎accounts/scwallet/securechannel.go‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
package scwallet
1818

1919
import (
20-
//"crypto/ecdsa"
2120
"bytes"
2221
"crypto/aes"
2322
"crypto/cipher"
2423
"crypto/rand"
2524
"crypto/sha256"
2625
"crypto/sha512"
2726
"fmt"
28-
//"math/big"
27+
2928
"github.com/ebfe/scard"
3029
"github.com/ethereum/go-ethereum/crypto"
31-
ecdh "github.com/wsddn/go-ecdh"
30+
"github.com/wsddn/go-ecdh"
3231
)
3332

3433
const (
@@ -38,6 +37,11 @@ const (
3837

3938
scSecretLength = 32
4039
scBlockSize = 16
40+
41+
insOpenSecureChannel = 0x10
42+
insMutuallyAuthenticate = 0x11
43+
insPair = 0x12
44+
insUnpair = 0x13
4145
)
4246

4347
// SecureChannelSession enables secure communication with a hardware wallet.
@@ -192,8 +196,8 @@ func (s *SecureChannelSession) mutuallyAuthenticate() error {
192196
}
193197

194198
// open is an internal method that sends an open APDU.
195-
func (s *SecureChannelSession) open() (*ResponseAPDU, error) {
196-
return transmit(s.card, &CommandAPDU{
199+
func (s *SecureChannelSession) open() (*responseAPDU, error) {
200+
return transmit(s.card, &commandAPDU{
197201
Cla: claSCWallet,
198202
Ins: insOpenSecureChannel,
199203
P1: s.PairingIndex,
@@ -204,8 +208,8 @@ func (s *SecureChannelSession) open() (*ResponseAPDU, error) {
204208
}
205209

206210
// pair is an internal method that sends a pair APDU.
207-
func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*ResponseAPDU, error) {
208-
return transmit(s.card, &CommandAPDU{
211+
func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*responseAPDU, error) {
212+
return transmit(s.card, &commandAPDU{
209213
Cla: claSCWallet,
210214
Ins: insPair,
211215
P1: p1,
@@ -216,7 +220,7 @@ func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*ResponseAPDU, error
216220
}
217221

218222
// TransmitEncrypted sends an encrypted message, and decrypts and returns the response.
219-
func (s *SecureChannelSession) TransmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*ResponseAPDU, error) {
223+
func (s *SecureChannelSession) TransmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) {
220224
if s.iv == nil {
221225
return nil, fmt.Errorf("Channel not open")
222226
}
@@ -234,7 +238,7 @@ func (s *SecureChannelSession) TransmitEncrypted(cla, ins, p1, p2 byte, data []b
234238
copy(fulldata, s.iv)
235239
copy(fulldata[len(s.iv):], data)
236240

237-
response, err := transmit(s.card, &CommandAPDU{
241+
response, err := transmit(s.card, &commandAPDU{
238242
Cla: cla,
239243
Ins: ins,
240244
P1: p1,
@@ -260,7 +264,7 @@ func (s *SecureChannelSession) TransmitEncrypted(cla, ins, p1, p2 byte, data []b
260264
return nil, fmt.Errorf("Invalid MAC in response")
261265
}
262266

263-
rapdu := &ResponseAPDU{}
267+
rapdu := &responseAPDU{}
264268
rapdu.deserialize(plainData)
265269

266270
if rapdu.Sw1 != sw1Ok {

0 commit comments

Comments
 (0)