mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 21:17:16 +00:00
af370c6e56
* internal/build: implement signify's signing func * Add signify to the ci utility * fix output file format * Add unit test for signify * holiman's + travis' feedback * internal/build: verify signify's output * crypto: move signify to common dir * use go-minisign to verify binaries * more holiman feedback * crypto, ci: support minisign output * only accept one-line trusted comments * configurable untrusted comments * code cleanup in tests * revert to use ed25519 from the stdlib * bug: fix for empty untrusted comments * write timestamp as comment if trusted comment isn't present * rename line checker to commentHasManyLines * crypto: added signify fuzzer (#6) * crypto: added signify fuzzer * stuff * crypto: updated signify fuzzer to fuzz comments * crypto: repro signify crashes * rebased fuzzer on build-signify branch * hide fuzzer behind gofuzz build flag * extract key data inside a single function * don't treat \r as a newline * travis: fix signing command line * do not use an external binary in tests * crypto: move signify to crypto/signify * travis: fix formatting issue * ci: fix linter build after package move Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de> # Conflicts: # build/ci.go # go.mod # go.sum
149 lines
3.7 KiB
Go
149 lines
3.7 KiB
Go
// Copyright 2020 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library 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 Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// +build gofuzz
|
|
|
|
package signify
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
|
|
fuzz "github.com/google/gofuzz"
|
|
"github.com/jedisct1/go-minisign"
|
|
)
|
|
|
|
func Fuzz(data []byte) int {
|
|
if len(data) < 32 {
|
|
return -1
|
|
}
|
|
tmpFile, err := ioutil.TempFile("", "")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer os.Remove(tmpFile.Name())
|
|
defer tmpFile.Close()
|
|
|
|
testSecKey, testPubKey := createKeyPair()
|
|
// Create message
|
|
tmpFile.Write(data)
|
|
if err = tmpFile.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
// Fuzz comments
|
|
var untrustedComment string
|
|
var trustedComment string
|
|
f := fuzz.NewFromGoFuzz(data)
|
|
f.Fuzz(&untrustedComment)
|
|
f.Fuzz(&trustedComment)
|
|
fmt.Printf("untrusted: %v\n", untrustedComment)
|
|
fmt.Printf("trusted: %v\n", trustedComment)
|
|
|
|
err = SignifySignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, untrustedComment, trustedComment)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer os.Remove(tmpFile.Name() + ".sig")
|
|
|
|
signify := "signify"
|
|
path := os.Getenv("SIGNIFY")
|
|
if path != "" {
|
|
signify = path
|
|
}
|
|
|
|
_, err := exec.LookPath(signify)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Write the public key into the file to pass it as
|
|
// an argument to signify-openbsd
|
|
pubKeyFile, err := ioutil.TempFile("", "")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer os.Remove(pubKeyFile.Name())
|
|
defer pubKeyFile.Close()
|
|
pubKeyFile.WriteString("untrusted comment: signify public key\n")
|
|
pubKeyFile.WriteString(testPubKey)
|
|
pubKeyFile.WriteString("\n")
|
|
|
|
cmd := exec.Command(signify, "-V", "-p", pubKeyFile.Name(), "-x", tmpFile.Name()+".sig", "-m", tmpFile.Name())
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output))
|
|
}
|
|
|
|
// Verify the signature using a golang library
|
|
sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
pKey, err := minisign.NewPublicKey(testPubKey)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if !valid {
|
|
panic("invalid signature")
|
|
}
|
|
return 1
|
|
}
|
|
|
|
func getKey(fileS string) (string, error) {
|
|
file, err := os.Open(fileS)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
// Discard the first line
|
|
scanner.Scan()
|
|
scanner.Scan()
|
|
return scanner.Text(), scanner.Err()
|
|
}
|
|
|
|
func createKeyPair() (string, string) {
|
|
// Create key and put it in correct format
|
|
tmpKey, err := ioutil.TempFile("", "")
|
|
defer os.Remove(tmpKey.Name())
|
|
defer os.Remove(tmpKey.Name() + ".pub")
|
|
defer os.Remove(tmpKey.Name() + ".sec")
|
|
cmd := exec.Command("signify", "-G", "-n", "-p", tmpKey.Name()+".pub", "-s", tmpKey.Name()+".sec")
|
|
if output, err := cmd.CombinedOutput(); err != nil {
|
|
panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output))
|
|
}
|
|
secKey, err := getKey(tmpKey.Name() + ".sec")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
pubKey, err := getKey(tmpKey.Name() + ".pub")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return secKey, pubKey
|
|
}
|