Skip to content

Commit 11a50aa

Browse files
committed
Check for error in response when selecting file. More informative error messages
1 parent 575e12a commit 11a50aa

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

card/apollo.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package card
22

33
import (
44
"encoding/binary"
5+
"encoding/hex"
56
"fmt"
67

78
"github.com/ubavic/bas-celik/v2/document"
@@ -132,7 +133,11 @@ func (card *Apollo) selectFile(name []byte, ne uint) ([]byte, error) {
132133
apu := buildAPDU(0x00, 0xA4, 0x08, 0x00, name, ne)
133134
rsp, err := card.smartCard.Transmit(apu)
134135
if err != nil {
135-
return nil, fmt.Errorf("selecting file: %w", err)
136+
return nil, fmt.Errorf("selecting file %s: %w", hex.EncodeToString(name), err)
137+
}
138+
139+
if !responseOK(rsp) {
140+
return nil, fmt.Errorf("selecting file %s: response %s", hex.EncodeToString(name), hex.EncodeToString(rsp))
136141
}
137142

138143
return rsp, nil

card/gemalto.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ func (card *Gemalto) selectFile(name []byte, selectionMethod, selectionOption by
230230
return nil, fmt.Errorf("selecting file: %w", err)
231231
}
232232

233+
if !responseOK(rsp) {
234+
return nil, fmt.Errorf("selecting file: response %s", hex.EncodeToString(rsp))
235+
}
236+
233237
return rsp, nil
234238
}
235239

@@ -253,7 +257,7 @@ func (card *Gemalto) InitCrypto() error {
253257
}
254258

255259
if !responseOK(rsp) {
256-
return errors.New("cryptography application not selected")
260+
return fmt.Errorf("cryptography application not selected: response %s", hex.EncodeToString(rsp))
257261
}
258262

259263
return nil
@@ -289,7 +293,7 @@ func (card *Gemalto) ChangePin(newPin, oldPin string) (int, error) {
289293
}
290294

291295
if !responseOK(rsp) {
292-
return PinTriesLeft(rsp), errors.New("verifying old pin")
296+
return PinTriesLeft(rsp), fmt.Errorf("verifying old pin: response %s", hex.EncodeToString(rsp))
293297
}
294298

295299
data := make([]byte, 0, 8)
@@ -303,7 +307,7 @@ func (card *Gemalto) ChangePin(newPin, oldPin string) (int, error) {
303307
}
304308

305309
if !responseOK(rsp) {
306-
return PinTriesLeft(rsp), errors.New("changing pin")
310+
return PinTriesLeft(rsp), fmt.Errorf("verifying old pin: response %s", hex.EncodeToString(rsp))
307311
}
308312

309313
err = card.smartCard.EndTransaction(scard.LeaveCard)

card/medical.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package card
22

33
import (
44
"encoding/binary"
5+
"encoding/hex"
56
"fmt"
67
"strings"
78

@@ -57,7 +58,7 @@ func (card *MedicalCard) InitCard() error {
5758
}
5859

5960
if !responseOK(rsp) {
60-
return fmt.Errorf("initializing card: response not OK")
61+
return fmt.Errorf("initializing card: response %s", hex.EncodeToString(rsp))
6162
}
6263

6364
return nil
@@ -175,7 +176,11 @@ func (card *MedicalCard) selectFile(name []byte) ([]byte, error) {
175176
apu := buildAPDU(0x00, 0xA4, 0x00, 0x00, name, 0)
176177
rsp, err := card.smartCard.Transmit(apu)
177178
if err != nil {
178-
return nil, fmt.Errorf("selecting file: %w", err)
179+
return nil, fmt.Errorf("selecting file %s: %w", hex.EncodeToString(name), err)
180+
}
181+
182+
if !responseOK(rsp) {
183+
return nil, fmt.Errorf("selecting file %s: response %s", hex.EncodeToString(name), hex.EncodeToString(rsp))
179184
}
180185

181186
return rsp, nil

card/vehicle.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package card
22

33
import (
4+
"encoding/hex"
45
"fmt"
56

67
"github.com/ubavic/bas-celik/v2/card/ber"
@@ -268,7 +269,11 @@ func (card *VehicleCard) selectFile(name []byte) ([]byte, error) {
268269

269270
rsp, err := card.smartCard.Transmit(apu)
270271
if err != nil {
271-
return nil, fmt.Errorf("selecting file: %w", err)
272+
return nil, fmt.Errorf("selecting file %s: %w", hex.EncodeToString(name), err)
273+
}
274+
275+
if !responseOK(rsp) {
276+
return nil, fmt.Errorf("selecting file %s: response %s", hex.EncodeToString(name), hex.EncodeToString(rsp))
272277
}
273278

274279
return rsp, nil

0 commit comments

Comments
 (0)