-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy patherrors.go
More file actions
83 lines (71 loc) · 2.33 KB
/
Copy patherrors.go
File metadata and controls
83 lines (71 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package keycard
import (
"fmt"
"github.com/keycard-tech/keycard-go/v4/apdu"
)
// ============================================================================
// Status word constants
// ============================================================================
const (
SwSecurityConditionNotSatisfied uint16 = 0x6982
SwAuthenticationMethodBlocked uint16 = 0x6983
SwCardLocked uint16 = 0x6283
SwReferencedDataNotFound uint16 = 0x6A88
SwConditionsOfUseNotSatisfied uint16 = 0x6985
SwWrongPINMask uint16 = 0x63C0
)
// ============================================================================
// APDUError
// ============================================================================
// APDUError represents an APDU-level error with a status word.
type APDUError struct {
SW uint16
Message string
}
// Error implements the error interface.
func (e *APDUError) Error() string {
return fmt.Sprintf("APDU error 0x%04X: %s", e.SW, e.Message)
}
// SecurityConditionNotSatisfied returns an APDUError for the security condition
// not satisfied status word.
func SecurityConditionNotSatisfied(sw uint16) *APDUError {
return &APDUError{
SW: sw,
Message: "Security condition not satisfied",
}
}
// AuthenticationMethodBlocked returns an APDUError for the authentication method
// blocked status word.
func AuthenticationMethodBlocked(sw uint16) *APDUError {
return &APDUError{
SW: sw,
Message: "Authentication method blocked",
}
}
// UnexpectedSW returns an APDUError for an unexpected status word.
func UnexpectedSW(sw uint16, message string) *APDUError {
return &APDUError{
SW: sw,
Message: message,
}
}
// ============================================================================
// Helper function to check auth response
// ============================================================================
// CheckAuthOK checks the response status word, handling the 0x63Cx PIN retry
// mask to return a WrongPINError with the remaining attempt count.
func CheckAuthOK(resp *apdu.Response) error {
if resp == nil {
return nil
}
if resp.Sw == apdu.SwOK {
return nil
}
if (resp.Sw & 0xFF00) == SwWrongPINMask {
remaining := resp.Sw & 0x000F
return &WrongPINError{
RemainingAttempts: int(remaining),
}
}
return apdu.NewErrBadResponse(resp.Sw, "authentication failed")
}