Skip to content

Commit 9e7225c

Browse files
committed
*: clean up actions and address all staticcheck findings
1 parent 7bee15c commit 9e7225c

File tree

6 files changed

+25
-70
lines changed

6 files changed

+25
-70
lines changed

.github/workflows/test.yaml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,41 @@ on:
99

1010
jobs:
1111
build:
12+
strategy:
13+
matrix:
14+
go-version: [1.18.x, 1.19.x]
1215
name: Linux
1316
runs-on: ubuntu-latest
1417
steps:
15-
- name: Set up Go 1.16
18+
- name: Set up Go
1619
uses: actions/setup-go@v2
1720
with:
18-
go-version: '^1.16.6'
21+
go-version: ${{ matrix.go-version }}
1922
id: go
2023
- name: Check out code into the Go module directory
2124
uses: actions/checkout@v2
22-
- name: Install golint
23-
run: go get -u golang.org/x/lint/golint
25+
- name: Install staticcheck
26+
run: go install honnef.co/go/tools/cmd/[email protected]
2427
- name: Install libpcsc
2528
run: sudo apt-get install -y libpcsclite-dev pcscd pcsc-tools
2629
- name: Test
2730
run: "make test"
2831
build-windows:
32+
strategy:
33+
matrix:
34+
go-version: [1.18.x, 1.19.x]
2935
name: Windows
3036
runs-on: windows-latest
3137
steps:
32-
- name: Set up Go 1.16
38+
- name: Set up Go
3339
uses: actions/setup-go@v2
3440
with:
35-
go-version: '^1.16.6'
41+
go-version: ${{ matrix.go-version }}
3642
id: go
3743
- name: Check out code into the Go module directory
3844
uses: actions/checkout@v2
39-
- name: Install golint
40-
run: go get -u golang.org/x/lint/golint
45+
- name: Install staticcheck
46+
run: go install honnef.co/go/tools/cmd/[email protected]
4147
- name: Test
4248
run: "make build"
4349
env:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test: lint
44

55
.PHONY: lint
66
lint:
7-
golint -set_exit_status ./...
7+
staticcheck ./...
88

99
.PHONY: build
1010
build: lint

piv/key.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,6 @@ func (a *Attestation) addExt(e pkix.Extension) error {
199199
return nil
200200
}
201201

202-
func verifySignature(parent, c *x509.Certificate) error {
203-
return parent.CheckSignature(c.SignatureAlgorithm, c.RawTBSCertificate, c.Signature)
204-
}
205-
206202
// Verify proves that a key was generated on a YubiKey. It ensures the slot and
207203
// YubiKey certificate chains up to the Yubico CA, parsing additional information
208204
// out of the slot certificate, such as the touch and PIN policies of a key.
@@ -232,7 +228,7 @@ func (v *verifier) Verify(attestationCert, slotCert *x509.Certificate) (*Attesta
232228
// This isn't valid as per https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.9
233229
// (fourth paragraph) and thus makes x509.go validation fail.
234230
// Work around this by setting this constraint here.
235-
if attestationCert.BasicConstraintsValid == false {
231+
if !attestationCert.BasicConstraintsValid {
236232
attestationCert.BasicConstraintsValid = true
237233
attestationCert.IsCA = true
238234
}
@@ -747,14 +743,6 @@ type KeyAuth struct {
747743
PINPolicy PINPolicy
748744
}
749745

750-
func isAuthErr(err error) bool {
751-
var e *apduErr
752-
if !errors.As(err, &e) {
753-
return false
754-
}
755-
return e.sw1 == 0x69 && e.sw2 == 0x82 // "security status not satisfied"
756-
}
757-
758746
func (k KeyAuth) authTx(yk *YubiKey, pp PINPolicy) error {
759747
// PINPolicyNever shouldn't require a PIN.
760748
if pp == PINPolicyNever {

piv/key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ func TestSetRSAPrivateKey(t *testing.T) {
767767
t.Fatalf("decrypting data: %v", err)
768768
}
769769

770-
if bytes.Compare(data, decrypted) != 0 {
770+
if !bytes.Equal(data, decrypted) {
771771
t.Fatalf("decrypted data is different to the source data")
772772
}
773773
})

piv/pcsc_windows.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ func (c *scContext) Connect(reader string) (*scHandle, error) {
127127
handle syscall.Handle
128128
activeProtocol uint16
129129
)
130+
readerPtr, err := syscall.UTF16PtrFromString(reader)
131+
if err != nil {
132+
return nil, fmt.Errorf("invalid reader string: %v", err)
133+
}
130134
r0, _, _ := procSCardConnectW.Call(
131135
uintptr(c.ctx),
132-
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(reader))),
136+
uintptr(unsafe.Pointer(readerPtr)),
133137
scardShareExclusive,
134138
scardProtocolT1,
135139
uintptr(unsafe.Pointer(&handle)),

piv/piv.go

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ func ykAuthenticate(tx *scTx, key [24]byte, rand io.Reader) error {
402402
response := make([]byte, 8)
403403
block.Encrypt(response, challenge)
404404

405-
data := append([]byte{
405+
data := []byte{
406406
0x7c, // Dynamic Authentication Template tag
407407
20, // 2+8+2+8
408408
0x80, // 'Witness'
409409
0x08, // Tag length
410-
})
410+
}
411411
data = append(data, cardResponse...)
412412
data = append(data,
413413
0x81, // 'Challenge'
@@ -632,47 +632,6 @@ func ykSerial(tx *scTx, v *version) (uint32, error) {
632632
return binary.BigEndian.Uint32(resp), nil
633633
}
634634

635-
// ykChangeManagementKey sets the Management Key to the new key provided. The
636-
// user must have authenticated with the existing key first.
637-
func ykChangeManagementKey(tx *scTx, key [24]byte) error {
638-
cmd := apdu{
639-
instruction: insSetMGMKey,
640-
param1: 0xff,
641-
param2: 0xff, // TODO: support touch policy
642-
data: append([]byte{
643-
alg3DES, keyCardManagement, 24,
644-
}, key[:]...),
645-
}
646-
if _, err := tx.Transmit(cmd); err != nil {
647-
return fmt.Errorf("command failed: %w", err)
648-
}
649-
return nil
650-
}
651-
652-
func unmarshalDERField(b []byte, tag uint64) (obj []byte, err error) {
653-
var prefix []byte
654-
for tag > 0 {
655-
prefix = append(prefix, byte(tag))
656-
tag = tag >> 8
657-
}
658-
for i, j := 0, len(prefix)-1; i < j; i, j = i+1, j-1 {
659-
prefix[i], prefix[j] = prefix[j], prefix[i]
660-
}
661-
662-
hasPrefix := bytes.HasPrefix(b, prefix)
663-
for len(b) > 0 {
664-
var v asn1.RawValue
665-
b, err = asn1.Unmarshal(b, &v)
666-
if err != nil {
667-
return nil, err
668-
}
669-
if hasPrefix {
670-
return v.Bytes, nil
671-
}
672-
}
673-
return nil, fmt.Errorf("no der value with tag 0x%x", prefix)
674-
}
675-
676635
// Metadata returns protected data stored on the card. This can be used to
677636
// retrieve PIN protected management keys.
678637
func (yk *YubiKey) Metadata(pin string) (*Metadata, error) {
@@ -780,9 +739,7 @@ func (m *Metadata) unmarshal(b []byte) error {
780739
return fmt.Errorf("invalid management key length: %d", len(v.Bytes))
781740
}
782741
var key [24]byte
783-
for i := 0; i < len(v.Bytes); i++ {
784-
key[i] = v.Bytes[i]
785-
}
742+
copy(key[:], v.Bytes)
786743
m.ManagementKey = &key
787744
}
788745
return nil

0 commit comments

Comments
 (0)