From 16a7631037f1d4f8b752902fe3677f0ed97a7f62 Mon Sep 17 00:00:00 2001 From: kayrus Date: Thu, 12 Dec 2024 11:34:57 +0100 Subject: [PATCH 1/2] Bump golang to 1.23 --- .github/workflows/golangci-lint.yml | 24 ++++++ .golangci.yml | 109 ++++++++++++++++++++++++++++ README.md | 2 +- go.mod | 6 +- go.sum | 8 +- 5 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/golangci-lint.yml create mode 100644 .golangci.yml diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..00b2464 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,24 @@ +name: Golangci-lint +on: + push: + branches: + - master + pull_request: + +jobs: + golangci-lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.23' + + - name: Checkout + uses: actions/checkout@v4 + + - name: Golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: 'v1.62' diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..2620b66 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,109 @@ +run: + deadline: 5m + +linters-settings: + staticcheck: + go: '1.23' + stylecheck: + go: '1.23' + gci: + local-prefixes: github.com/kayrus + goimports: + local-prefixes: github.com/kayrus + depguard: + rules: + prevent_unmaintained_packages: + list-mode: lax + files: + - $all + - "!$test" + deny: + - pkg: io/ioutil + desc: "replaced by io and os packages since Go 1.16: https://tip.golang.org/doc/go1.16#ioutil" + +linters: + fast: false + disable-all: true + enable: + - asciicheck + - bodyclose + - depguard + - dogsled + - errcheck + - exhaustive + - exportloopref + - gci + - godot + - gofmt + - goheader + - goimports + - gomodguard + - goprintffuncname + - govet + - ineffassign + - misspell + - nakedret + - nolintlint + - prealloc + - staticcheck + - stylecheck + - typecheck + - unconvert + - unused + - whitespace +# - cyclop +# - dupl +# - durationcheck +# - errname +# - errorlint +# - exhaustivestruct +# - forbidigo +# - forcetypeassert +# - funlen +# - gochecknoglobals +# - gochecknoinits +# - gocognit +# - goconst +# - gocritic +# - gocyclo +# - godox +# - goerr113 +# - gofumpt +# - golint +# - gomnd +# - gomoddirectives +# - gosec (gas) +# - gosimple (megacheck) +# - ifshort +# - importas +# - interfacer +# - lll +# - makezero +# - maligned +# - nestif +# - nilerr +# - nlreturn +# - noctx +# - paralleltest +# - predeclared +# - promlinter +# - revive +# - scopelint +# - sqlclosecheck +# - tagliatelle +# - testpackage +# - thelper +# - tparallel +# - unparam +# - wastedassign +# - wrapcheck +# - wsl + +issues: + exclude-rules: + - linters: + - staticcheck + text: "SA1019:" + - linters: + - stylecheck + text: "ST1005:" diff --git a/README.md b/README.md index 9a0b932..e13c0af 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # putty -Go package to parse PuTTY private key formats. Go 1.22 or above is required. +Go package to parse PuTTY private key formats. Go 1.23 or above is required. ## Example diff --git a/go.mod b/go.mod index 14c5b09..13a8ae0 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/kayrus/putty -go 1.22 +go 1.23 -require golang.org/x/crypto v0.25.0 +require golang.org/x/crypto v0.31.0 -require golang.org/x/sys v0.22.0 // indirect +require golang.org/x/sys v0.28.0 // indirect diff --git a/go.sum b/go.sum index c339b1b..0b273f9 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= From 4e224ec9818787ac73e1e2e5ef228217de0c76b7 Mon Sep 17 00:00:00 2001 From: kayrus Date: Thu, 12 Dec 2024 11:56:42 +0100 Subject: [PATCH 2/2] Fix linter issues --- .golangci.yml | 6 +++--- putty.go | 50 ++++++++++++++++++++++++++++++++++++++++---------- putty_test.go | 1 - unmarshal.go | 1 + 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 2620b66..14b3bb0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -30,10 +30,8 @@ linters: - depguard - dogsled - errcheck - - exhaustive - - exportloopref + - copyloopvar - gci - - godot - gofmt - goheader - goimports @@ -51,6 +49,8 @@ linters: - unconvert - unused - whitespace +# - exhaustive +# - godot # - cyclop # - dupl # - durationcheck diff --git a/putty.go b/putty.go index 4f891d0..fc8d1a7 100644 --- a/putty.go +++ b/putty.go @@ -442,16 +442,46 @@ func decryptCBC(cipherKey, cipherIV, macKey, ciphertext []byte) error { // validateHMAC validates PuTTY key HMAC with a hash function func (k Key) validateHMAC(hashFunc hash.Hash) error { - binary.Write(hashFunc, binary.BigEndian, uint32(len(k.Algo))) - hashFunc.Write([]byte(k.Algo)) - binary.Write(hashFunc, binary.BigEndian, uint32(len(k.Encryption))) - hashFunc.Write([]byte(k.Encryption)) - binary.Write(hashFunc, binary.BigEndian, uint32(len(k.Comment))) - hashFunc.Write([]byte(k.Comment)) - binary.Write(hashFunc, binary.BigEndian, uint32(len(k.PublicKey))) - hashFunc.Write(k.PublicKey) - binary.Write(hashFunc, binary.BigEndian, uint32(len(k.PrivateKey))) - hashFunc.Write(k.PrivateKey) + err := binary.Write(hashFunc, binary.BigEndian, uint32(len(k.Algo))) + if err != nil { + return err + } + _, err = hashFunc.Write([]byte(k.Algo)) + if err != nil { + return err + } + err = binary.Write(hashFunc, binary.BigEndian, uint32(len(k.Encryption))) + if err != nil { + return err + } + _, err = hashFunc.Write([]byte(k.Encryption)) + if err != nil { + return err + } + err = binary.Write(hashFunc, binary.BigEndian, uint32(len(k.Comment))) + if err != nil { + return err + } + _, err = hashFunc.Write([]byte(k.Comment)) + if err != nil { + return err + } + err = binary.Write(hashFunc, binary.BigEndian, uint32(len(k.PublicKey))) + if err != nil { + return err + } + _, err = hashFunc.Write(k.PublicKey) + if err != nil { + return err + } + err = binary.Write(hashFunc, binary.BigEndian, uint32(len(k.PrivateKey))) + if err != nil { + return err + } + _, err = hashFunc.Write(k.PrivateKey) + if err != nil { + return err + } mac := hashFunc.Sum(nil) if !bytes.Equal(mac, k.PrivateMac) { diff --git a/putty_test.go b/putty_test.go index 5155c41..07910d7 100644 --- a/putty_test.go +++ b/putty_test.go @@ -446,7 +446,6 @@ Comment: a@b` if err == nil { t.Errorf("Invalid key algorithm") } - } func TestKey_Load(t *testing.T) { diff --git a/unmarshal.go b/unmarshal.go index b7b9598..918e6ca 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -44,6 +44,7 @@ func parseField(v reflect.Value, src *bytes.Reader) error { } } return nil + default: } switch fieldType {