Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pcsc: Replace custom code with github/com/ebfe/scard #131

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/go-piv/piv-go

go 1.16

require github.com/ebfe/scard v0.0.0-20230420082256-7db3f9b7c8a7
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/ebfe/scard v0.0.0-20230420082256-7db3f9b7c8a7 h1:HYAhfGa9dEemCZgGZWL5AvVsctBCsHxl2CI0HUXzHQE=
github.com/ebfe/scard v0.0.0-20230420082256-7db3f9b7c8a7/go.mod h1:BkYEeWL6FbT4Ek+TcOBnPzEKnL7kOq2g19tTQXkorHY=
50 changes: 38 additions & 12 deletions piv/pcsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,9 @@ package piv
import (
"errors"
"fmt"
)

type scErr struct {
// rc holds the return code for a given call.
rc int64
}

func (e *scErr) Error() string {
if msg, ok := pcscErrMsgs[e.rc]; ok {
return msg
}
return fmt.Sprintf("unknown pcsc return code 0x%08x", e.rc)
}
"github.com/ebfe/scard"
)

// AuthErr is an error indicating an authentication error occurred (wrong PIN or blocked).
type AuthErr struct {
Expand Down Expand Up @@ -134,6 +124,42 @@ type apdu struct {
data []byte
}

type scTx struct {
*scard.Card
}

func newTx(h *scard.Card) (*scTx, error) {
if err := h.BeginTransaction(); err != nil {
return nil, err
}

return &scTx{
Card: h,
}, nil
}

func (t *scTx) Close() error {
return t.Card.EndTransaction(scard.LeaveCard)
}

func (t *scTx) transmit(req []byte) (more bool, b []byte, err error) {
resp, err := t.Card.Transmit(req)
if err != nil {
return false, nil, fmt.Errorf("transmitting request: %w", err)
} else if len(resp) < 2 {
return false, nil, fmt.Errorf("scard response too short: %d", len(resp))
}
sw1 := resp[len(resp)-2]
sw2 := resp[len(resp)-1]
if sw1 == 0x90 && sw2 == 0x00 {
return false, resp[:len(resp)-2], nil
}
if sw1 == 0x61 {
return true, resp[:len(resp)-2], nil
}
return false, nil, &apduErr{sw1, sw2}
}

func (t *scTx) Transmit(d apdu) ([]byte, error) {
data := d.data
var resp []byte
Expand Down
38 changes: 0 additions & 38 deletions piv/pcsc_darwin.go

This file was deleted.

179 changes: 0 additions & 179 deletions piv/pcsc_errors

This file was deleted.

82 changes: 0 additions & 82 deletions piv/pcsc_errors.go

This file was deleted.

Loading