Commit cf1a6d7c authored by Guillaume Ballet's avatar Guillaume Ballet Committed by Péter Szilágyi

vendor: upgrade go-libpcsclite (#19420)

* vendor: remove leftover trace

* Upgrade go-libpcsclite to the latest version
parent 33d28f37
...@@ -6,18 +6,38 @@ A golang implementation of the [libpcpsclite](http://github.com/LudovicRousseau/ ...@@ -6,18 +6,38 @@ A golang implementation of the [libpcpsclite](http://github.com/LudovicRousseau/
The goal is for major open source projects to distribute a single binary that doesn't depend on `libpcsclite`. It provides an extra function `CheckPCSCDaemon` that will tell the user if `pcscd` is running. The goal is for major open source projects to distribute a single binary that doesn't depend on `libpcsclite`. It provides an extra function `CheckPCSCDaemon` that will tell the user if `pcscd` is running.
## Building ## Example
TODO ```golang
func main() {
client, err := EstablishContext(2)
if err != nil {
fmt.Printf("Error establishing context: %v\n", err)
os.Exit(1)
}
## Example _, err = client.ListReaders()
if err != nil {
fmt.Printf("Error getting the list of readers: %v\n", err)
os.Exit(1)
}
card, err := client.Connect(client.readerStateDescriptors[0].Name, ShareShared, ProtocolT0|ProtocolT1)
if err != nil {
fmt.Printf("Error connecting: %v\n", err)
os.Exit(1)
}
resp, _, err := card.Transmit([]byte{0, 0xa4, 4, 0, 0xA0, 0, 0, 8, 4, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0})
TODO card.Disconnect(LeaveCard)
}
```
## TODO ## TODO
- [ ] Finish this README - [x] Finish this README
- [ ] Lock context - [x] Lock context
- [ ] implement missing functions - [ ] implement missing functions
## License ## License
......
...@@ -31,8 +31,6 @@ ...@@ -31,8 +31,6 @@
package pcsc package pcsc
const ( const (
SCardSuccess = 0x00000000 /* No error was encountered. */
AutoAllocate = -1 /* see SCardFreeMemory() */ AutoAllocate = -1 /* see SCardFreeMemory() */
ScopeUser = 0x0000 /* Scope in user space */ ScopeUser = 0x0000 /* Scope in user space */
ScopeTerminal = 0x0001 /* Scope in terminal */ ScopeTerminal = 0x0001 /* Scope in terminal */
...@@ -94,6 +92,6 @@ const ( ...@@ -94,6 +92,6 @@ const (
// Protocol information // Protocol information
const ( const (
ProtocolVersionMajor = 4 /* IPC major */ ProtocolVersionMajor = uint32(4) /* IPC major */
ProtocolVersionMinor = 3 /* IPC minor */ ProtocolVersionMinor = uint32(3) /* IPC minor */
) )
This diff is collapsed.
...@@ -65,16 +65,20 @@ func EstablishContext(scope uint32) (*Client, error) { ...@@ -65,16 +65,20 @@ func EstablishContext(scope uint32) (*Client, error) {
} }
client.conn = conn client.conn = conn
/* Exchange version information */
payload := make([]byte, 12) payload := make([]byte, 12)
response := make([]byte, 12)
var code uint32
var minor uint32
for minor = ProtocolVersionMinor; minor <= ProtocolVersionMinor+1; minor++ {
/* Exchange version information */
binary.LittleEndian.PutUint32(payload, ProtocolVersionMajor) binary.LittleEndian.PutUint32(payload, ProtocolVersionMajor)
binary.LittleEndian.PutUint32(payload[4:], ProtocolVersionMinor) binary.LittleEndian.PutUint32(payload[4:], minor)
binary.LittleEndian.PutUint32(payload[8:], SCardSuccess) binary.LittleEndian.PutUint32(payload[8:], SCardSuccess.Code())
err = messageSendWithHeader(CommandVersion, conn, payload) err = messageSendWithHeader(CommandVersion, conn, payload)
if err != nil { if err != nil {
return nil, err return nil, err
} }
response := make([]byte, 12)
n, err := conn.Read(response) n, err := conn.Read(response)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -82,26 +86,35 @@ func EstablishContext(scope uint32) (*Client, error) { ...@@ -82,26 +86,35 @@ func EstablishContext(scope uint32) (*Client, error) {
if n != len(response) { if n != len(response) {
return nil, fmt.Errorf("invalid response length: expected %d, got %d", len(response), n) return nil, fmt.Errorf("invalid response length: expected %d, got %d", len(response), n)
} }
code := binary.LittleEndian.Uint32(response[8:]) code = binary.LittleEndian.Uint32(response[8:])
if code != SCardSuccess { if code != SCardSuccess.Code() {
return nil, fmt.Errorf("invalid response code: expected %d, got %d", SCardSuccess, code) continue
} }
client.major = binary.LittleEndian.Uint32(response) client.major = binary.LittleEndian.Uint32(response)
client.minor = binary.LittleEndian.Uint32(response[4:]) client.minor = binary.LittleEndian.Uint32(response[4:])
if client.major != ProtocolVersionMajor || client.minor != ProtocolVersionMinor { if client.major != ProtocolVersionMajor || client.minor != minor {
continue
}
break
}
if code != SCardSuccess.Code() {
return nil, fmt.Errorf("invalid response code: expected %d, got %d (%v)", SCardSuccess, code, ErrorCode(code).Error())
}
if client.major != ProtocolVersionMajor || (client.minor != minor && client.minor+1 != minor) {
return nil, fmt.Errorf("invalid version found: expected %d.%d, got %d.%d", ProtocolVersionMajor, ProtocolVersionMinor, client.major, client.minor) return nil, fmt.Errorf("invalid version found: expected %d.%d, got %d.%d", ProtocolVersionMajor, ProtocolVersionMinor, client.major, client.minor)
} }
/* Establish the context proper */ /* Establish the context proper */
binary.LittleEndian.PutUint32(payload, scope) binary.LittleEndian.PutUint32(payload, scope)
binary.LittleEndian.PutUint32(payload[4:], 0) binary.LittleEndian.PutUint32(payload[4:], 0)
binary.LittleEndian.PutUint32(payload[8:], SCardSuccess) binary.LittleEndian.PutUint32(payload[8:], SCardSuccess.Code())
err = messageSendWithHeader(SCardEstablishContext, conn, payload) err = messageSendWithHeader(SCardEstablishContext, conn, payload)
if err != nil { if err != nil {
return nil, err return nil, err
} }
response = make([]byte, 12) response = make([]byte, 12)
n, err = conn.Read(response) n, err := conn.Read(response)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -109,8 +122,8 @@ func EstablishContext(scope uint32) (*Client, error) { ...@@ -109,8 +122,8 @@ func EstablishContext(scope uint32) (*Client, error) {
return nil, fmt.Errorf("invalid response length: expected %d, got %d", len(response), n) return nil, fmt.Errorf("invalid response length: expected %d, got %d", len(response), n)
} }
code = binary.LittleEndian.Uint32(response[8:]) code = binary.LittleEndian.Uint32(response[8:])
if code != SCardSuccess { if code != SCardSuccess.Code() {
return nil, fmt.Errorf("invalid response code: expected %d, got %d", SCardSuccess, code) return nil, fmt.Errorf("invalid response code: expected %d, got %d (%v)", SCardSuccess, code, ErrorCode(code).Error())
} }
client.ctx = binary.LittleEndian.Uint32(response[4:]) client.ctx = binary.LittleEndian.Uint32(response[4:])
...@@ -125,7 +138,7 @@ func (client *Client) ReleaseContext() error { ...@@ -125,7 +138,7 @@ func (client *Client) ReleaseContext() error {
data := [8]byte{} data := [8]byte{}
binary.LittleEndian.PutUint32(data[:], client.ctx) binary.LittleEndian.PutUint32(data[:], client.ctx)
binary.LittleEndian.PutUint32(data[4:], SCardSuccess) binary.LittleEndian.PutUint32(data[4:], SCardSuccess.Code())
err := messageSendWithHeader(SCardReleaseContext, client.conn, data[:]) err := messageSendWithHeader(SCardReleaseContext, client.conn, data[:])
if err != nil { if err != nil {
return err return err
...@@ -139,8 +152,8 @@ func (client *Client) ReleaseContext() error { ...@@ -139,8 +152,8 @@ func (client *Client) ReleaseContext() error {
total += n total += n
} }
code := binary.LittleEndian.Uint32(data[4:]) code := binary.LittleEndian.Uint32(data[4:])
if code != SCardSuccess { if code != SCardSuccess.Code() {
return fmt.Errorf("invalid return code: %x", code) return fmt.Errorf("invalid return code: %x, %v", code, ErrorCode(code).Error())
} }
return nil return nil
...@@ -247,7 +260,7 @@ func (client *Client) Connect(name string, shareMode uint32, preferredProtocol u ...@@ -247,7 +260,7 @@ func (client *Client) Connect(name string, shareMode uint32, preferredProtocol u
copy(request[SCardConnectReaderNameOffset:], []byte(name)) copy(request[SCardConnectReaderNameOffset:], []byte(name))
binary.LittleEndian.PutUint32(request[SCardConnectShareModeOffset:], shareMode) binary.LittleEndian.PutUint32(request[SCardConnectShareModeOffset:], shareMode)
binary.LittleEndian.PutUint32(request[SCardConnectPreferredProtocolOffset:], preferredProtocol) binary.LittleEndian.PutUint32(request[SCardConnectPreferredProtocolOffset:], preferredProtocol)
binary.LittleEndian.PutUint32(request[SCardConnectReturnValueOffset:], SCardSuccess) binary.LittleEndian.PutUint32(request[SCardConnectReturnValueOffset:], SCardSuccess.Code())
err := messageSendWithHeader(SCardConnect, client.conn, request) err := messageSendWithHeader(SCardConnect, client.conn, request)
if err != nil { if err != nil {
...@@ -260,12 +273,12 @@ func (client *Client) Connect(name string, shareMode uint32, preferredProtocol u ...@@ -260,12 +273,12 @@ func (client *Client) Connect(name string, shareMode uint32, preferredProtocol u
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println("total, n", total, n, response) // fmt.Println("total, n", total, n, response)
total += n total += n
} }
code := binary.LittleEndian.Uint32(response[148:]) code := binary.LittleEndian.Uint32(response[148:])
if code != SCardSuccess { if code != SCardSuccess.Code() {
return nil, fmt.Errorf("invalid return code: %x", code) return nil, fmt.Errorf("invalid return code: %x (%v)", code, ErrorCode(code).Error())
} }
handle := binary.LittleEndian.Uint32(response[140:]) handle := binary.LittleEndian.Uint32(response[140:])
active := binary.LittleEndian.Uint32(response[SCardConnectPreferredProtocolOffset:]) active := binary.LittleEndian.Uint32(response[SCardConnectPreferredProtocolOffset:])
...@@ -312,7 +325,7 @@ func (card *Card) Transmit(adpu []byte) ([]byte, *SCardIoRequest, error) { ...@@ -312,7 +325,7 @@ func (card *Card) Transmit(adpu []byte) ([]byte, *SCardIoRequest, error) {
binary.LittleEndian.PutUint32(request[16:], 0) binary.LittleEndian.PutUint32(request[16:], 0)
binary.LittleEndian.PutUint32(request[20:], 0) binary.LittleEndian.PutUint32(request[20:], 0)
binary.LittleEndian.PutUint32(request[24:], 0x10000) binary.LittleEndian.PutUint32(request[24:], 0x10000)
binary.LittleEndian.PutUint32(request[28:], SCardSuccess) binary.LittleEndian.PutUint32(request[28:], SCardSuccess.Code())
err := messageSendWithHeader(SCardTransmit, card.client.conn, request[:]) err := messageSendWithHeader(SCardTransmit, card.client.conn, request[:])
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
...@@ -336,8 +349,8 @@ func (card *Card) Transmit(adpu []byte) ([]byte, *SCardIoRequest, error) { ...@@ -336,8 +349,8 @@ func (card *Card) Transmit(adpu []byte) ([]byte, *SCardIoRequest, error) {
} }
code := binary.LittleEndian.Uint32(response[28:]) code := binary.LittleEndian.Uint32(response[28:])
if code != SCardSuccess { if code != SCardSuccess.Code() {
return nil, nil, fmt.Errorf("invalid return code: %x", code) return nil, nil, fmt.Errorf("invalid return code: %x (%v)", code, ErrorCode(code).Error())
} }
// Recover the response data // Recover the response data
...@@ -367,7 +380,7 @@ func (card *Card) Disconnect(disposition uint32) error { ...@@ -367,7 +380,7 @@ func (card *Card) Disconnect(disposition uint32) error {
data := [12]byte{} data := [12]byte{}
binary.LittleEndian.PutUint32(data[:], card.handle) binary.LittleEndian.PutUint32(data[:], card.handle)
binary.LittleEndian.PutUint32(data[4:], disposition) binary.LittleEndian.PutUint32(data[4:], disposition)
binary.LittleEndian.PutUint32(data[8:], SCardSuccess) binary.LittleEndian.PutUint32(data[8:], SCardSuccess.Code())
err := messageSendWithHeader(SCardDisConnect, card.client.conn, data[:]) err := messageSendWithHeader(SCardDisConnect, card.client.conn, data[:])
if err != nil { if err != nil {
return err return err
...@@ -381,8 +394,8 @@ func (card *Card) Disconnect(disposition uint32) error { ...@@ -381,8 +394,8 @@ func (card *Card) Disconnect(disposition uint32) error {
total += n total += n
} }
code := binary.LittleEndian.Uint32(data[8:]) code := binary.LittleEndian.Uint32(data[8:])
if code != SCardSuccess { if code != SCardSuccess.Code() {
return fmt.Errorf("invalid return code: %x", code) return fmt.Errorf("invalid return code: %x (%v)", code, ErrorCode(code).Error())
} }
return nil return nil
......
...@@ -140,10 +140,10 @@ ...@@ -140,10 +140,10 @@
"revisionTime": "2018-04-18T12:24:29Z" "revisionTime": "2018-04-18T12:24:29Z"
}, },
{ {
"checksumSHA1": "GnNfMrYs/4m+HCtDBF7HpPUFFVw=", "checksumSHA1": "GXqHzd0XkPLX/iulpOncaxbxzZo=",
"path": "github.com/gballet/go-libpcsclite", "path": "github.com/gballet/go-libpcsclite",
"revision": "95b81846253cd854b8bb8f2fd9cc6056d0681ac4", "revision": "312b5175032f98274685a4dd81935a92ad2412a5",
"revisionTime": "2019-03-13T11:40:44Z" "revisionTime": "2019-04-03T18:15:18Z"
}, },
{ {
"checksumSHA1": "gxV/cPPLkByTdY8y172t7v4qcZA=", "checksumSHA1": "gxV/cPPLkByTdY8y172t7v4qcZA=",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment