signify_fuzz.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // +build gofuzz
  17. package signify
  18. import (
  19. "bufio"
  20. "fmt"
  21. "io/ioutil"
  22. "log"
  23. "os"
  24. "os/exec"
  25. "runtime"
  26. fuzz "github.com/google/gofuzz"
  27. "github.com/jedisct1/go-minisign"
  28. )
  29. func Fuzz(data []byte) int {
  30. if len(data) < 32 {
  31. return -1
  32. }
  33. tmpFile, err := ioutil.TempFile("", "")
  34. if err != nil {
  35. panic(err)
  36. }
  37. defer os.Remove(tmpFile.Name())
  38. defer tmpFile.Close()
  39. testSecKey, testPubKey := createKeyPair()
  40. // Create message
  41. tmpFile.Write(data)
  42. if err = tmpFile.Close(); err != nil {
  43. panic(err)
  44. }
  45. // Fuzz comments
  46. var untrustedComment string
  47. var trustedComment string
  48. f := fuzz.NewFromGoFuzz(data)
  49. f.Fuzz(&untrustedComment)
  50. f.Fuzz(&trustedComment)
  51. fmt.Printf("untrusted: %v\n", untrustedComment)
  52. fmt.Printf("trusted: %v\n", trustedComment)
  53. err = SignifySignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, untrustedComment, trustedComment)
  54. if err != nil {
  55. panic(err)
  56. }
  57. defer os.Remove(tmpFile.Name() + ".sig")
  58. signify := "signify"
  59. path := os.Getenv("SIGNIFY")
  60. if path != "" {
  61. signify = path
  62. }
  63. _, err := exec.LookPath(signify)
  64. if err != nil {
  65. panic(err)
  66. }
  67. // Write the public key into the file to pass it as
  68. // an argument to signify-openbsd
  69. pubKeyFile, err := ioutil.TempFile("", "")
  70. if err != nil {
  71. panic(err)
  72. }
  73. defer os.Remove(pubKeyFile.Name())
  74. defer pubKeyFile.Close()
  75. pubKeyFile.WriteString("untrusted comment: signify public key\n")
  76. pubKeyFile.WriteString(testPubKey)
  77. pubKeyFile.WriteString("\n")
  78. cmd := exec.Command(signify, "-V", "-p", pubKeyFile.Name(), "-x", tmpFile.Name()+".sig", "-m", tmpFile.Name())
  79. if output, err := cmd.CombinedOutput(); err != nil {
  80. panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output))
  81. }
  82. // Verify the signature using a golang library
  83. sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig")
  84. if err != nil {
  85. panic(err)
  86. }
  87. pKey, err := minisign.NewPublicKey(testPubKey)
  88. if err != nil {
  89. panic(err)
  90. }
  91. valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig)
  92. if err != nil {
  93. panic(err)
  94. }
  95. if !valid {
  96. panic("invalid signature")
  97. }
  98. return 1
  99. }
  100. func getKey(fileS string) (string, error) {
  101. file, err := os.Open(fileS)
  102. if err != nil {
  103. log.Fatal(err)
  104. }
  105. defer file.Close()
  106. scanner := bufio.NewScanner(file)
  107. // Discard the first line
  108. scanner.Scan()
  109. scanner.Scan()
  110. return scanner.Text(), scanner.Err()
  111. }
  112. func createKeyPair() (string, string) {
  113. // Create key and put it in correct format
  114. tmpKey, err := ioutil.TempFile("", "")
  115. if err != nil {
  116. panic(err)
  117. }
  118. defer os.Remove(tmpKey.Name())
  119. defer os.Remove(tmpKey.Name() + ".pub")
  120. defer os.Remove(tmpKey.Name() + ".sec")
  121. cmd := exec.Command("signify", "-G", "-n", "-p", tmpKey.Name()+".pub", "-s", tmpKey.Name()+".sec")
  122. if output, err := cmd.CombinedOutput(); err != nil {
  123. panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output))
  124. }
  125. secKey, err := getKey(tmpKey.Name() + ".sec")
  126. if err != nil {
  127. panic(err)
  128. }
  129. pubKey, err := getKey(tmpKey.Name() + ".pub")
  130. if err != nil {
  131. panic(err)
  132. }
  133. return secKey, pubKey
  134. }