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

communication/u2fhid: prepend 0 byte to packet #73

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 16 additions & 2 deletions communication/u2fhid/u2fhid.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"bytes"
"encoding/binary"
"io"
"runtime"
"sync"

"github.com/digitalbitbox/bitbox02-api-go/util/errp"
Expand Down Expand Up @@ -103,6 +104,18 @@ func (communication *Communication) sendFrame(msg string) error {
buf.WriteByte(0xee)
}
x := buf.Bytes() // needs to be in a var: https://github.com/golang/go/issues/14210#issuecomment-346402945
if runtime.GOOS != "windows" {
// packets have a 0 byte report ID in front. The karalabe usb library adds it
// automatically for windows, and not for unix, as there, it is stripped by the signal11
// hid library. We have to add it (to be stripped by signal11), otherwise a zero that is
// actually part of the packet would be stripped, leading to a corrupt packet. Our
// packets could start with a zero if e.g. the 4-bytes CID starts with a zero byte
//
// See
// - https://github.com/karalabe/usb/blob/87927bb2c8544d009d8ac7350b1ac892b60c8115/hid_enabled.go#L126-L128.
// - https://github.com/karalabe/usb/blob/87927bb2c8544d009d8ac7350b1ac892b60c8115/hidapi/libusb/hid.c#L1003-L1007
x = append([]byte{0}, x...)
}
_, err := communication.device.Write(x)
return errp.WithMessage(errp.WithStack(err), "Failed to send message")
}
Expand Down Expand Up @@ -153,8 +166,9 @@ func (communication *Communication) readFrame() ([]byte, error) {
if readLen < 7 {
return nil, errp.New("expected minimum read length of 7")
}
if read[0] != 0xff || read[1] != 0 || read[2] != 0 || read[3] != 0 {
return nil, errp.Newf("USB command ID mismatch %d %d %d %d", read[0], read[1], read[2], read[3])
replyCid := binary.BigEndian.Uint32(read[:4])
if replyCid != cid {
return nil, errp.Newf("USB command ID mismatch, %v != %v", cid, replyCid)
}
if read[4] != communication.cmd {
return nil, errp.Newf("USB command frame mismatch (%d, expected %d)", read[4], communication.cmd)
Expand Down