signify_fuzz.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. //go:build gofuzz
  17. // +build gofuzz
  18. package signify
  19. import (
  20. "bufio"
  21. "fmt"
  22. "log"
  23. "os"
  24. "os/exec"
  25. fuzz "github.com/google/gofuzz"
  26. "github.com/jedisct1/go-minisign"
  27. )
  28. func Fuzz(data []byte) int {
  29. if len(data) < 32 {
  30. return -1
  31. }
  32. tmpFile, err := os.CreateTemp("", "")
  33. if err != nil {
  34. panic(err)
  35. }
  36. defer os.Remove(tmpFile.Name())
  37. defer tmpFile.Close()
  38. testSecKey, testPubKey := createKeyPair()
  39. // Create message
  40. tmpFile.Write(data)
  41. if err = tmpFile.Close(); err != nil {
  42. panic(err)
  43. }
  44. // Fuzz comments
  45. var untrustedComment string
  46. var trustedComment string
  47. f := fuzz.NewFromGoFuzz(data)
  48. f.Fuzz(&untrustedComment)
  49. f.Fuzz(&trustedComment)
  50. fmt.Printf("untrusted: %v\n", untrustedComment)
  51. fmt.Printf("trusted: %v\n", trustedComment)
  52. err = SignifySignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, untrustedComment, trustedComment)
  53. if err != nil {
  54. panic(err)
  55. }
  56. defer os.Remove(tmpFile.Name() + ".sig")
  57. signify := "signify"
  58. path := os.Getenv("SIGNIFY")
  59. if path != "" {
  60. signify = path
  61. }
  62. _, err := exec.LookPath(signify)
  63. if err != nil {
  64. panic(err)
  65. }
  66. // Write the public key into the file to pass it as
  67. // an argument to signify-openbsd
  68. pubKeyFile, err := os.CreateTemp("", "")
  69. if err != nil {
  70. panic(err)
  71. }
  72. defer os.Remove(pubKeyFile.Name())
  73. defer pubKeyFile.Close()
  74. pubKeyFile.WriteString("untrusted comment: signify public key\n")
  75. pubKeyFile.WriteString(testPubKey)
  76. pubKeyFile.WriteString("\n")
  77. cmd := exec.Command(signify, "-V", "-p", pubKeyFile.Name(), "-x", tmpFile.Name()+".sig", "-m", tmpFile.Name())
  78. if output, err := cmd.CombinedOutput(); err != nil {
  79. panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output))
  80. }
  81. // Verify the signature using a golang library
  82. sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig")
  83. if err != nil {
  84. panic(err)
  85. }
  86. pKey, err := minisign.NewPublicKey(testPubKey)
  87. if err != nil {
  88. panic(err)
  89. }
  90. valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig)
  91. if err != nil {
  92. panic(err)
  93. }
  94. if !valid {
  95. panic("invalid signature")
  96. }
  97. return 1
  98. }
  99. func getKey(fileS string) (string, error) {
  100. file, err := os.Open(fileS)
  101. if err != nil {
  102. log.Fatal(err)
  103. }
  104. defer file.Close()
  105. scanner := bufio.NewScanner(file)
  106. // Discard the first line
  107. scanner.Scan()
  108. scanner.Scan()
  109. return scanner.Text(), scanner.Err()
  110. }
  111. func createKeyPair() (string, string) {
  112. // Create key and put it in correct format
  113. tmpKey, err := os.CreateTemp("", "")
  114. if err != nil {
  115. panic(err)
  116. }
  117. defer os.Remove(tmpKey.Name())
  118. defer os.Remove(tmpKey.Name() + ".pub")
  119. defer os.Remove(tmpKey.Name() + ".sec")
  120. cmd := exec.Command("signify", "-G", "-n", "-p", tmpKey.Name()+".pub", "-s", tmpKey.Name()+".sec")
  121. if output, err := cmd.CombinedOutput(); err != nil {
  122. panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output))
  123. }
  124. secKey, err := getKey(tmpKey.Name() + ".sec")
  125. if err != nil {
  126. panic(err)
  127. }
  128. pubKey, err := getKey(tmpKey.Name() + ".pub")
  129. if err != nil {
  130. panic(err)
  131. }
  132. return secKey, pubKey
  133. }