clearsign_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package clearsign
  5. import (
  6. "bytes"
  7. "fmt"
  8. "testing"
  9. "golang.org/x/crypto/openpgp"
  10. "golang.org/x/crypto/openpgp/packet"
  11. )
  12. func testParse(t *testing.T, input []byte, expected, expectedPlaintext string) {
  13. b, rest := Decode(input)
  14. if b == nil {
  15. t.Fatal("failed to decode clearsign message")
  16. }
  17. if !bytes.Equal(rest, []byte("trailing")) {
  18. t.Errorf("unexpected remaining bytes returned: %s", string(rest))
  19. }
  20. if b.ArmoredSignature.Type != "PGP SIGNATURE" {
  21. t.Errorf("bad armor type, got:%s, want:PGP SIGNATURE", b.ArmoredSignature.Type)
  22. }
  23. if !bytes.Equal(b.Bytes, []byte(expected)) {
  24. t.Errorf("bad body, got:%x want:%x", b.Bytes, expected)
  25. }
  26. if !bytes.Equal(b.Plaintext, []byte(expectedPlaintext)) {
  27. t.Errorf("bad plaintext, got:%x want:%x", b.Plaintext, expectedPlaintext)
  28. }
  29. keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(signingKey))
  30. if err != nil {
  31. t.Errorf("failed to parse public key: %s", err)
  32. }
  33. if _, err := openpgp.CheckDetachedSignature(keyring, bytes.NewBuffer(b.Bytes), b.ArmoredSignature.Body); err != nil {
  34. t.Errorf("failed to check signature: %s", err)
  35. }
  36. }
  37. func TestParse(t *testing.T) {
  38. testParse(t, clearsignInput, "Hello world\r\nline 2", "Hello world\nline 2\n")
  39. testParse(t, clearsignInput2, "\r\n\r\n(This message has a couple of blank lines at the start and end.)\r\n\r\n", "\n\n(This message has a couple of blank lines at the start and end.)\n\n\n")
  40. }
  41. func TestParseInvalid(t *testing.T) {
  42. if b, _ := Decode(clearsignInput3); b != nil {
  43. t.Fatal("decoded a bad clearsigned message without any error")
  44. }
  45. }
  46. func TestParseWithNoNewlineAtEnd(t *testing.T) {
  47. input := clearsignInput
  48. input = input[:len(input)-len("trailing")-1]
  49. b, rest := Decode(input)
  50. if b == nil {
  51. t.Fatal("failed to decode clearsign message")
  52. }
  53. if len(rest) > 0 {
  54. t.Errorf("unexpected remaining bytes returned: %s", string(rest))
  55. }
  56. }
  57. var signingTests = []struct {
  58. in, signed, plaintext string
  59. }{
  60. {"", "", ""},
  61. {"a", "a", "a\n"},
  62. {"a\n", "a", "a\n"},
  63. {"-a\n", "-a", "-a\n"},
  64. {"--a\nb", "--a\r\nb", "--a\nb\n"},
  65. // leading whitespace
  66. {" a\n", " a", " a\n"},
  67. {" a\n", " a", " a\n"},
  68. // trailing whitespace (should be stripped)
  69. {"a \n", "a", "a\n"},
  70. {"a ", "a", "a\n"},
  71. // whitespace-only lines (should be stripped)
  72. {" \n", "", "\n"},
  73. {" ", "", "\n"},
  74. {"a\n \n \nb\n", "a\r\n\r\n\r\nb", "a\n\n\nb\n"},
  75. }
  76. func TestSigning(t *testing.T) {
  77. keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBufferString(signingKey))
  78. if err != nil {
  79. t.Errorf("failed to parse public key: %s", err)
  80. }
  81. for i, test := range signingTests {
  82. var buf bytes.Buffer
  83. plaintext, err := Encode(&buf, keyring[0].PrivateKey, nil)
  84. if err != nil {
  85. t.Errorf("#%d: error from Encode: %s", i, err)
  86. continue
  87. }
  88. if _, err := plaintext.Write([]byte(test.in)); err != nil {
  89. t.Errorf("#%d: error from Write: %s", i, err)
  90. continue
  91. }
  92. if err := plaintext.Close(); err != nil {
  93. t.Fatalf("#%d: error from Close: %s", i, err)
  94. continue
  95. }
  96. b, _ := Decode(buf.Bytes())
  97. if b == nil {
  98. t.Errorf("#%d: failed to decode clearsign message", i)
  99. continue
  100. }
  101. if !bytes.Equal(b.Bytes, []byte(test.signed)) {
  102. t.Errorf("#%d: bad result, got:%x, want:%x", i, b.Bytes, test.signed)
  103. continue
  104. }
  105. if !bytes.Equal(b.Plaintext, []byte(test.plaintext)) {
  106. t.Errorf("#%d: bad result, got:%x, want:%x", i, b.Plaintext, test.plaintext)
  107. continue
  108. }
  109. if _, err := openpgp.CheckDetachedSignature(keyring, bytes.NewBuffer(b.Bytes), b.ArmoredSignature.Body); err != nil {
  110. t.Errorf("#%d: failed to check signature: %s", i, err)
  111. }
  112. }
  113. }
  114. // We use this to make test keys, so that they aren't all the same.
  115. type quickRand byte
  116. func (qr *quickRand) Read(p []byte) (int, error) {
  117. for i := range p {
  118. p[i] = byte(*qr)
  119. }
  120. *qr++
  121. return len(p), nil
  122. }
  123. func TestMultiSign(t *testing.T) {
  124. zero := quickRand(0)
  125. config := packet.Config{Rand: &zero}
  126. for nKeys := 0; nKeys < 4; nKeys++ {
  127. nextTest:
  128. for nExtra := 0; nExtra < 4; nExtra++ {
  129. var signKeys []*packet.PrivateKey
  130. var verifyKeys openpgp.EntityList
  131. desc := fmt.Sprintf("%d keys; %d of which will be used to verify", nKeys+nExtra, nKeys)
  132. for i := 0; i < nKeys+nExtra; i++ {
  133. e, err := openpgp.NewEntity("name", "comment", "email", &config)
  134. if err != nil {
  135. t.Errorf("cannot create key: %v", err)
  136. continue nextTest
  137. }
  138. if i < nKeys {
  139. verifyKeys = append(verifyKeys, e)
  140. }
  141. signKeys = append(signKeys, e.PrivateKey)
  142. }
  143. input := []byte("this is random text\r\n4 17")
  144. var output bytes.Buffer
  145. w, err := EncodeMulti(&output, signKeys, nil)
  146. if err != nil {
  147. t.Errorf("EncodeMulti (%s) failed: %v", desc, err)
  148. }
  149. if _, err := w.Write(input); err != nil {
  150. t.Errorf("Write(%q) to signer (%s) failed: %v", string(input), desc, err)
  151. }
  152. if err := w.Close(); err != nil {
  153. t.Errorf("Close() of signer (%s) failed: %v", desc, err)
  154. }
  155. block, _ := Decode(output.Bytes())
  156. if string(block.Bytes) != string(input) {
  157. t.Errorf("Inline data didn't match original; got %q want %q", string(block.Bytes), string(input))
  158. }
  159. _, err = openpgp.CheckDetachedSignature(verifyKeys, bytes.NewReader(block.Bytes), block.ArmoredSignature.Body)
  160. if nKeys == 0 {
  161. if err == nil {
  162. t.Errorf("verifying inline (%s) succeeded; want failure", desc)
  163. }
  164. } else {
  165. if err != nil {
  166. t.Errorf("verifying inline (%s) failed (%v); want success", desc, err)
  167. }
  168. }
  169. }
  170. }
  171. }
  172. var clearsignInput = []byte(`
  173. ;lasjlkfdsa
  174. -----BEGIN PGP SIGNED MESSAGE-----
  175. Hash: SHA1
  176. Hello world
  177. line 2
  178. -----BEGIN PGP SIGNATURE-----
  179. Version: GnuPG v1.4.10 (GNU/Linux)
  180. iJwEAQECAAYFAk8kMuEACgkQO9o98PRieSpMsAQAhmY/vwmNpflrPgmfWsYhk5O8
  181. pjnBUzZwqTDoDeINjZEoPDSpQAHGhjFjgaDx/Gj4fAl0dM4D0wuUEBb6QOrwflog
  182. 2A2k9kfSOMOtk0IH/H5VuFN1Mie9L/erYXjTQIptv9t9J7NoRBMU0QOOaFU0JaO9
  183. MyTpno24AjIAGb+mH1U=
  184. =hIJ6
  185. -----END PGP SIGNATURE-----
  186. trailing`)
  187. var clearsignInput2 = []byte(`
  188. asdlfkjasdlkfjsadf
  189. -----BEGIN PGP SIGNED MESSAGE-----
  190. Hash: SHA256
  191. (This message has a couple of blank lines at the start and end.)
  192. -----BEGIN PGP SIGNATURE-----
  193. Version: GnuPG v1.4.11 (GNU/Linux)
  194. iJwEAQEIAAYFAlPpSREACgkQO9o98PRieSpZTAP+M8QUoCt/7Rf3YbXPcdzIL32v
  195. pt1I+cMNeopzfLy0u4ioEFi8s5VkwpL1AFmirvgViCwlf82inoRxzZRiW05JQ5LI
  196. ESEzeCoy2LIdRCQ2hcrG8pIUPzUO4TqO5D/dMbdHwNH4h5nNmGJUAEG6FpURlPm+
  197. qZg6BaTvOxepqOxnhVU=
  198. =e+C6
  199. -----END PGP SIGNATURE-----
  200. trailing`)
  201. var clearsignInput3 = []byte(`
  202. -----BEGIN PGP SIGNED MESSAGE-----
  203. Hash: SHA256
  204. (This message was truncated.)
  205. `)
  206. var signingKey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
  207. Version: GnuPG v1.4.10 (GNU/Linux)
  208. lQHYBE2rFNoBBADFwqWQIW/DSqcB4yCQqnAFTJ27qS5AnB46ccAdw3u4Greeu3Bp
  209. idpoHdjULy7zSKlwR1EA873dO/k/e11Ml3dlAFUinWeejWaK2ugFP6JjiieSsrKn
  210. vWNicdCS4HTWn0X4sjl0ZiAygw6GNhqEQ3cpLeL0g8E9hnYzJKQ0LWJa0QARAQAB
  211. AAP/TB81EIo2VYNmTq0pK1ZXwUpxCrvAAIG3hwKjEzHcbQznsjNvPUihZ+NZQ6+X
  212. 0HCfPAdPkGDCLCb6NavcSW+iNnLTrdDnSI6+3BbIONqWWdRDYJhqZCkqmG6zqSfL
  213. IdkJgCw94taUg5BWP/AAeQrhzjChvpMQTVKQL5mnuZbUCeMCAN5qrYMP2S9iKdnk
  214. VANIFj7656ARKt/nf4CBzxcpHTyB8+d2CtPDKCmlJP6vL8t58Jmih+kHJMvC0dzn
  215. gr5f5+sCAOOe5gt9e0am7AvQWhdbHVfJU0TQJx+m2OiCJAqGTB1nvtBLHdJnfdC9
  216. TnXXQ6ZXibqLyBies/xeY2sCKL5qtTMCAKnX9+9d/5yQxRyrQUHt1NYhaXZnJbHx
  217. q4ytu0eWz+5i68IYUSK69jJ1NWPM0T6SkqpB3KCAIv68VFm9PxqG1KmhSrQIVGVz
  218. dCBLZXmIuAQTAQIAIgUCTasU2gIbAwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AA
  219. CgkQO9o98PRieSoLhgQAkLEZex02Qt7vGhZzMwuN0R22w3VwyYyjBx+fM3JFETy1
  220. ut4xcLJoJfIaF5ZS38UplgakHG0FQ+b49i8dMij0aZmDqGxrew1m4kBfjXw9B/v+
  221. eIqpODryb6cOSwyQFH0lQkXC040pjq9YqDsO5w0WYNXYKDnzRV0p4H1pweo2VDid
  222. AdgETasU2gEEAN46UPeWRqKHvA99arOxee38fBt2CI08iiWyI8T3J6ivtFGixSqV
  223. bRcPxYO/qLpVe5l84Nb3X71GfVXlc9hyv7CD6tcowL59hg1E/DC5ydI8K8iEpUmK
  224. /UnHdIY5h8/kqgGxkY/T/hgp5fRQgW1ZoZxLajVlMRZ8W4tFtT0DeA+JABEBAAEA
  225. A/0bE1jaaZKj6ndqcw86jd+QtD1SF+Cf21CWRNeLKnUds4FRRvclzTyUMuWPkUeX
  226. TaNNsUOFqBsf6QQ2oHUBBK4VCHffHCW4ZEX2cd6umz7mpHW6XzN4DECEzOVksXtc
  227. lUC1j4UB91DC/RNQqwX1IV2QLSwssVotPMPqhOi0ZLNY7wIA3n7DWKInxYZZ4K+6
  228. rQ+POsz6brEoRHwr8x6XlHenq1Oki855pSa1yXIARoTrSJkBtn5oI+f8AzrnN0BN
  229. oyeQAwIA/7E++3HDi5aweWrViiul9cd3rcsS0dEnksPhvS0ozCJiHsq/6GFmy7J8
  230. QSHZPteedBnZyNp5jR+H7cIfVN3KgwH/Skq4PsuPhDq5TKK6i8Pc1WW8MA6DXTdU
  231. nLkX7RGmMwjC0DBf7KWAlPjFaONAX3a8ndnz//fy1q7u2l9AZwrj1qa1iJ8EGAEC
  232. AAkFAk2rFNoCGwwACgkQO9o98PRieSo2/QP/WTzr4ioINVsvN1akKuekmEMI3LAp
  233. BfHwatufxxP1U+3Si/6YIk7kuPB9Hs+pRqCXzbvPRrI8NHZBmc8qIGthishdCYad
  234. AHcVnXjtxrULkQFGbGvhKURLvS9WnzD/m1K2zzwxzkPTzT9/Yf06O6Mal5AdugPL
  235. VrM0m72/jnpKo04=
  236. =zNCn
  237. -----END PGP PRIVATE KEY BLOCK-----
  238. `