2020-12-04 14:01:47 +00:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-12-09 12:59:24 +00:00
|
|
|
"fmt"
|
2022-05-16 09:59:35 +00:00
|
|
|
"os"
|
2020-12-04 14:01:47 +00:00
|
|
|
"path/filepath"
|
2020-12-09 12:59:24 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-12-04 14:01:47 +00:00
|
|
|
"testing"
|
2022-01-11 09:39:04 +00:00
|
|
|
|
|
|
|
"github.com/jedisct1/go-minisign"
|
2020-12-04 14:01:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVerification(t *testing.T) {
|
2023-09-07 08:18:46 +00:00
|
|
|
// Signatures generated with `minisign`. Legacy format, not pre-hashed file.
|
|
|
|
t.Run("minisig-legacy", func(t *testing.T) {
|
|
|
|
// For this test, the pubkey is in testdata/vcheck/minisign.pub
|
2020-12-04 14:01:47 +00:00
|
|
|
// (the privkey is `minisign.sec`, if we want to expand this test. Password 'test' )
|
|
|
|
pub := "RWQkliYstQBOKOdtClfgC3IypIPX6TAmoEi7beZ4gyR3wsaezvqOMWsp"
|
|
|
|
testVerification(t, pub, "./testdata/vcheck/minisig-sigs/")
|
|
|
|
})
|
2023-09-07 08:18:46 +00:00
|
|
|
t.Run("minisig-new", func(t *testing.T) {
|
|
|
|
// For this test, the pubkey is in testdata/vcheck/minisign.pub
|
|
|
|
// (the privkey is `minisign.sec`, if we want to expand this test. Password 'test' )
|
|
|
|
// `minisign -S -s ./minisign.sec -m data.json -x ./minisig-sigs-new/data.json.minisig`
|
|
|
|
pub := "RWQkliYstQBOKOdtClfgC3IypIPX6TAmoEi7beZ4gyR3wsaezvqOMWsp"
|
|
|
|
testVerification(t, pub, "./testdata/vcheck/minisig-sigs-new/")
|
|
|
|
})
|
2020-12-04 14:01:47 +00:00
|
|
|
// Signatures generated with `signify-openbsd`
|
|
|
|
t.Run("signify-openbsd", func(t *testing.T) {
|
|
|
|
t.Skip("This currently fails, minisign expects 4 lines of data, signify provides only 2")
|
2023-09-07 08:18:46 +00:00
|
|
|
// For this test, the pubkey is in testdata/vcheck/signifykey.pub
|
2020-12-04 14:01:47 +00:00
|
|
|
// (the privkey is `signifykey.sec`, if we want to expand this test. Password 'test' )
|
|
|
|
pub := "RWSKLNhZb0KdATtRT7mZC/bybI3t3+Hv/O2i3ye04Dq9fnT9slpZ1a2/"
|
|
|
|
testVerification(t, pub, "./testdata/vcheck/signify-sigs/")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testVerification(t *testing.T, pubkey, sigdir string) {
|
|
|
|
// Data to verify
|
2022-05-16 09:59:35 +00:00
|
|
|
data, err := os.ReadFile("./testdata/vcheck/data.json")
|
2020-12-04 14:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Signatures, with and without comments, both trusted and untrusted
|
2022-05-17 12:27:04 +00:00
|
|
|
files, err := os.ReadDir(sigdir)
|
2020-12-04 14:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-09-07 08:18:46 +00:00
|
|
|
if len(files) == 0 {
|
|
|
|
t.Fatal("Missing tests")
|
|
|
|
}
|
2020-12-04 14:01:47 +00:00
|
|
|
for _, f := range files {
|
2022-05-16 09:59:35 +00:00
|
|
|
sig, err := os.ReadFile(filepath.Join(sigdir, f.Name()))
|
2020-12-04 14:01:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = verifySignature([]string{pubkey}, data, sig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 12:59:24 +00:00
|
|
|
func versionUint(v string) int {
|
|
|
|
mustInt := func(s string) int {
|
|
|
|
a, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(v)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
components := strings.Split(strings.TrimPrefix(v, "v"), ".")
|
|
|
|
a := mustInt(components[0])
|
|
|
|
b := mustInt(components[1])
|
|
|
|
c := mustInt(components[2])
|
|
|
|
return a*100*100 + b*100 + c
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestMatching can be used to check that the regexps are correct
|
|
|
|
func TestMatching(t *testing.T) {
|
2022-05-16 09:59:35 +00:00
|
|
|
data, _ := os.ReadFile("./testdata/vcheck/vulnerabilities.json")
|
2020-12-04 14:01:47 +00:00
|
|
|
var vulns []vulnJson
|
|
|
|
if err := json.Unmarshal(data, &vulns); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-12-09 12:59:24 +00:00
|
|
|
check := func(version string) {
|
|
|
|
vFull := fmt.Sprintf("Geth/%v-unstable-15339cf1-20201204/linux-amd64/go1.15.4", version)
|
|
|
|
for _, vuln := range vulns {
|
|
|
|
r, err := regexp.Compile(vuln.Check)
|
|
|
|
vulnIntro := versionUint(vuln.Introduced)
|
|
|
|
vulnFixed := versionUint(vuln.Fixed)
|
|
|
|
current := versionUint(version)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if vuln.Name == "Denial of service due to Go CVE-2020-28362" {
|
|
|
|
// this one is not tied to geth-versions
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if vulnIntro <= current && vulnFixed > current {
|
|
|
|
// Should be vulnerable
|
|
|
|
if !r.MatchString(vFull) {
|
|
|
|
t.Errorf("Should be vulnerable, version %v, intro: %v, fixed: %v %v %v",
|
|
|
|
version, vuln.Introduced, vuln.Fixed, vuln.Name, vuln.Check)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if r.MatchString(vFull) {
|
|
|
|
t.Errorf("Should not be flagged vulnerable, version %v, intro: %v, fixed: %v %v %d %d %d",
|
|
|
|
version, vuln.Introduced, vuln.Fixed, vuln.Name, vulnIntro, current, vulnFixed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 14:01:47 +00:00
|
|
|
}
|
2020-12-09 12:59:24 +00:00
|
|
|
for major := 1; major < 2; major++ {
|
|
|
|
for minor := 0; minor < 30; minor++ {
|
|
|
|
for patch := 0; patch < 30; patch++ {
|
|
|
|
vShort := fmt.Sprintf("v%d.%d.%d", major, minor, patch)
|
|
|
|
check(vShort)
|
|
|
|
}
|
|
|
|
}
|
2020-12-04 14:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-11 09:39:04 +00:00
|
|
|
|
|
|
|
func TestGethPubKeysParseable(t *testing.T) {
|
|
|
|
for _, pubkey := range gethPubKeys {
|
|
|
|
_, err := minisign.NewPublicKey(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Should be parseable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyID(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
id [8]byte
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"@holiman key", args{id: extractKeyId(gethPubKeys[0])}, "FB1D084D39BAEC24"},
|
|
|
|
{"second key", args{id: extractKeyId(gethPubKeys[1])}, "138B1CA303E51687"},
|
|
|
|
{"third key", args{id: extractKeyId(gethPubKeys[2])}, "FD9813B2D2098484"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := keyID(tt.args.id); got != tt.want {
|
|
|
|
t.Errorf("keyID() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractKeyId(pubkey string) [8]byte {
|
|
|
|
p, _ := minisign.NewPublicKey(pubkey)
|
|
|
|
return p.KeyId
|
|
|
|
}
|