Skip to content

Commit

Permalink
pcsc: Replace custom code with github/com/ebfe/scard
Browse files Browse the repository at this point in the history
This uses the ebfe/scard package to replace custom code
for interfacing with the PIV token on various platforms.
  • Loading branch information
stv0g committed Oct 8, 2023
1 parent 8c3a0ff commit ca40a90
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 843 deletions.
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

0 comments on commit ca40a90

Please sign in to comment.