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

Option to make yubikey connection shared #160

Closed
wants to merge 6 commits 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: 1 addition & 1 deletion piv/pcsc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runHandleTest(t *testing.T, f func(t *testing.T, h *scHandle)) {
if reader == "" {
t.Skip("could not find yubikey, skipping testing")
}
h, err := c.Connect(reader)
h, err := c.Connect(reader, false)
if err != nil {
t.Fatalf("connecting to %s: %v", reader, err)
}
Expand Down
10 changes: 8 additions & 2 deletions piv/pcsc_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,19 @@ type scHandle struct {
h C.SCARDHANDLE
}

func (c *scContext) Connect(reader string) (*scHandle, error) {
func (c *scContext) Connect(reader string, shared bool) (*scHandle, error) {
var (
handle C.SCARDHANDLE
activeProtocol C.DWORD
)

var mode C.ulong = C.SCARD_SHARE_EXCLUSIVE
if shared {
mode = C.SCARD_SHARE_SHARED
}

rc := C.SCardConnect(c.ctx, C.CString(reader),
C.SCARD_SHARE_EXCLUSIVE, C.SCARD_PROTOCOL_T1,
mode, C.SCARD_PROTOCOL_T1,
&handle, &activeProtocol)
if err := scCheck(rc); err != nil {
return nil, err
Expand Down
17 changes: 9 additions & 8 deletions piv/piv.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
//
// See: https://ludovicrousseau.blogspot.com/2010/05/what-is-in-pcsc-reader-name.html
func Cards() ([]string, error) {
var c client
var c Client
return c.Cards()
}

Expand Down Expand Up @@ -127,20 +127,21 @@

// Open connects to a YubiKey smart card.
func Open(card string) (*YubiKey, error) {
var c client
var c Client
return c.Open(card)
}

// client is a smart card client and may be exported in the future to allow
// Client is a smart card client and may be exported in the future to allow
// configuration for the top level Open() and Cards() APIs.
type client struct {
type Client struct {
// Rand is a cryptographic source of randomness used for card challenges.
//
// If nil, defaults to crypto.Rand.
Rand io.Reader
Rand io.Reader
Shared bool
}

func (c *client) Cards() ([]string, error) {
func (c *Client) Cards() ([]string, error) {
ctx, err := newSCContext()
if err != nil {
return nil, fmt.Errorf("connecting to pcsc: %w", err)
Expand All @@ -149,13 +150,13 @@
return ctx.ListReaders()
}

func (c *client) Open(card string) (*YubiKey, error) {
func (c *Client) Open(card string) (*YubiKey, error) {
ctx, err := newSCContext()
if err != nil {
return nil, fmt.Errorf("connecting to smart card daemon: %w", err)
}

h, err := ctx.Connect(card)
h, err := ctx.Connect(card, c.Shared)

Check failure on line 159 in piv/piv.go

View workflow job for this annotation

GitHub Actions / Windows (1.19.x)

too many arguments in call to ctx.Connect

Check failure on line 159 in piv/piv.go

View workflow job for this annotation

GitHub Actions / Windows (1.20.x)

too many arguments in call to ctx.Connect
if err != nil {
ctx.Close()
return nil, fmt.Errorf("connecting to smart card: %w", err)
Expand Down
Loading