mirror of
https://github.com/status-im/keycard-go.git
synced 2025-02-20 23:58:13 +00:00
add comments and fix warning for main command and pkg apdu
This commit is contained in:
parent
9225619434
commit
7538feac95
@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// Command struct represent the data sent as an APDU command with CLA, Ins, P1, P2, Lc, Data, and Le.
|
||||
type Command struct {
|
||||
Cla uint8
|
||||
Ins uint8
|
||||
@ -15,6 +16,7 @@ type Command struct {
|
||||
requiresLe bool
|
||||
}
|
||||
|
||||
// NewCommand returns a new apdu Command.
|
||||
func NewCommand(cla, ins, p1, p2 uint8, data []byte) *Command {
|
||||
return &Command{
|
||||
Cla: cla,
|
||||
@ -26,15 +28,18 @@ func NewCommand(cla, ins, p1, p2 uint8, data []byte) *Command {
|
||||
}
|
||||
}
|
||||
|
||||
// SetLe sets the expected Le value and makes sure the Le value is sent in the apdu Command.
|
||||
func (c *Command) SetLe(le uint8) {
|
||||
c.requiresLe = true
|
||||
c.le = le
|
||||
}
|
||||
|
||||
// Le returns if Le is set and its value.
|
||||
func (c *Command) Le() (bool, uint8) {
|
||||
return c.requiresLe, c.le
|
||||
}
|
||||
|
||||
// Serialize serielizes the command into a raw bytes sequence.
|
||||
func (c *Command) Serialize() ([]byte, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
|
@ -8,14 +8,17 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// SwOK is returned from smartcards as a positive response code.
|
||||
SwOK = 0x9000
|
||||
)
|
||||
|
||||
// ErrBadResponse defines an error conaining the returned Sw code and a description message.
|
||||
type ErrBadResponse struct {
|
||||
sw uint16
|
||||
message string
|
||||
}
|
||||
|
||||
// NewErrBadResponse returns a ErrBadResponse with the specified sw and message values.
|
||||
func NewErrBadResponse(sw uint16, message string) *ErrBadResponse {
|
||||
return &ErrBadResponse{
|
||||
sw: sw,
|
||||
@ -23,10 +26,12 @@ func NewErrBadResponse(sw uint16, message string) *ErrBadResponse {
|
||||
}
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *ErrBadResponse) Error() string {
|
||||
return fmt.Sprintf("bad response %x: %s", e.sw, e.message)
|
||||
}
|
||||
|
||||
// Response represents a struct containing the smartcard response fields.
|
||||
type Response struct {
|
||||
Data []byte
|
||||
Sw1 uint8
|
||||
@ -34,8 +39,10 @@ type Response struct {
|
||||
Sw uint16
|
||||
}
|
||||
|
||||
// ErrBadRawResponse is an error returned by ParseResponse in case the response data is not long enough.
|
||||
var ErrBadRawResponse = errors.New("response data must be at least 2 bytes")
|
||||
|
||||
// ParseResponse parses a raw response and return a Response.
|
||||
func ParseResponse(data []byte) (*Response, error) {
|
||||
r := &Response{}
|
||||
return r, r.deserialize(data)
|
||||
@ -66,6 +73,7 @@ func (r *Response) deserialize(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsOK returns true if the response Sw code is 0x9000.
|
||||
func (r *Response) IsOK() bool {
|
||||
return r.Sw == SwOK
|
||||
}
|
||||
|
@ -6,14 +6,17 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// ErrTagNotFound is an error returned if a tag is not found in a TLV sequence.
|
||||
type ErrTagNotFound struct {
|
||||
tag uint8
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
func (e *ErrTagNotFound) Error() string {
|
||||
return fmt.Sprintf("tag %x not found", e.tag)
|
||||
}
|
||||
|
||||
// FindTag searches for a tag value within a TLV sequence.
|
||||
func FindTag(raw []byte, tags ...uint8) ([]byte, error) {
|
||||
if len(tags) == 0 {
|
||||
return raw, nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user