本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
5.3KB

  1. diff --git a/openpgp/read.go b/openpgp/read.go
  2. index a6cecc5..0c9397b 100644
  3. --- a/openpgp/read.go
  4. +++ b/openpgp/read.go
  5. @@ -56,8 +56,9 @@ type MessageDetails struct {
  6. // been consumed. Once EOF has been seen, the following fields are
  7. // valid. (An authentication code failure is reported as a
  8. // SignatureError error when reading from UnverifiedBody.)
  9. - SignatureError error // nil if the signature is good.
  10. - Signature *packet.Signature // the signature packet itself.
  11. + SignatureError error // nil if the signature is good.
  12. + Signature *packet.Signature // the signature packet itself, if v4 (default)
  13. + SignatureV3 *packet.SignatureV3 // the signature packet if it is a v2 or v3 signature
  14. decrypted io.ReadCloser
  15. }
  16. @@ -334,13 +335,15 @@ func (scr *signatureCheckReader) Read(buf []byte) (n int, err error) {
  17. }
  18. var ok bool
  19. - if scr.md.Signature, ok = p.(*packet.Signature); !ok {
  20. + if scr.md.Signature, ok = p.(*packet.Signature); ok {
  21. + scr.md.SignatureError = scr.md.SignedBy.PublicKey.VerifySignature(scr.h, scr.md.Signature)
  22. + } else if scr.md.SignatureV3, ok = p.(*packet.SignatureV3); ok {
  23. + scr.md.SignatureError = scr.md.SignedBy.PublicKey.VerifySignatureV3(scr.h, scr.md.SignatureV3)
  24. + } else {
  25. scr.md.SignatureError = errors.StructuralError("LiteralData not followed by Signature")
  26. return
  27. }
  28. - scr.md.SignatureError = scr.md.SignedBy.PublicKey.VerifySignature(scr.h, scr.md.Signature)
  29. -
  30. // The SymmetricallyEncrypted packet, if any, might have an
  31. // unsigned hash of its own. In order to check this we need to
  32. // close that Reader.
  33. diff --git a/openpgp/read_test.go b/openpgp/read_test.go
  34. index 52f942c..abe8d7b 100644
  35. --- a/openpgp/read_test.go
  36. +++ b/openpgp/read_test.go
  37. @@ -13,6 +13,7 @@ import (
  38. "strings"
  39. "testing"
  40. + "golang.org/x/crypto/openpgp/armor"
  41. "golang.org/x/crypto/openpgp/errors"
  42. )
  43. @@ -411,6 +412,50 @@ func TestIssue11504(t *testing.T) {
  44. testReadMessageError(t, "9303000130303030303030303030983002303030303030030000000130")
  45. }
  46. +// TestSignatureV3Message tests the verification of V3 signature, generated
  47. +// with a modern V4-style key. Some people have their clients set to generate
  48. +// V3 signatures, so it's useful to be able to verify them.
  49. +func TestSignatureV3Message(t *testing.T) {
  50. + sig, err := armor.Decode(strings.NewReader(signedMessageV3))
  51. + if err != nil {
  52. + t.Error(err)
  53. + return
  54. + }
  55. + key, err := ReadArmoredKeyRing(strings.NewReader(keyV4forVerifyingSignedMessageV3))
  56. + if err != nil {
  57. + t.Error(err)
  58. + return
  59. + }
  60. + md, err := ReadMessage(sig.Body, key, nil, nil)
  61. + if err != nil {
  62. + t.Error(err)
  63. + return
  64. + }
  65. +
  66. + _, err = ioutil.ReadAll(md.UnverifiedBody)
  67. + if err != nil {
  68. + t.Error(err)
  69. + return
  70. + }
  71. +
  72. + // We'll see a sig error here after reading in the UnverifiedBody above,
  73. + // if there was one to see.
  74. + if err = md.SignatureError; err != nil {
  75. + t.Error(err)
  76. + return
  77. + }
  78. +
  79. + if md.SignatureV3 == nil {
  80. + t.Errorf("No available signature after checking signature")
  81. + return
  82. + }
  83. + if md.Signature != nil {
  84. + t.Errorf("Did not expect a signature V4 back")
  85. + return
  86. + }
  87. + return
  88. +}
  89. +
  90. const testKey1KeyId = 0xA34D7E18C20C31BB
  91. const testKey3KeyId = 0x338934250CCC0360
  92. @@ -504,3 +549,36 @@ const unknownHashFunctionHex = `8a00000040040001990006050253863c24000a09103b4fe6
  93. const missingHashFunctionHex = `8a00000040040001030006050253863c24000a09103b4fe6acc0b21f32ffff0101010101010101010101010101010101010101010101010101010101010101010101010101`
  94. const campbellQuine = `a0b001000300fcffa0b001000d00f2ff000300fcffa0b001000d00f2ff8270a01c00000500faff8270a01c00000500faff000500faff001400ebff8270a01c00000500faff000500faff001400ebff428821c400001400ebff428821c400001400ebff428821c400001400ebff428821c400001400ebff428821c400000000ffff000000ffff000b00f4ff428821c400000000ffff000000ffff000b00f4ff0233214c40000100feff000233214c40000100feff0000`
  95. +
  96. +const keyV4forVerifyingSignedMessageV3 = `-----BEGIN PGP PUBLIC KEY BLOCK-----
  97. +Comment: GPGTools - https://gpgtools.org
  98. +
  99. +mI0EVfxoFQEEAMBIqmbDfYygcvP6Phr1wr1XI41IF7Qixqybs/foBF8qqblD9gIY
  100. +BKpXjnBOtbkcVOJ0nljd3/sQIfH4E0vQwK5/4YRQSI59eKOqd6Fx+fWQOLG+uu6z
  101. +tewpeCj9LLHvibx/Sc7VWRnrznia6ftrXxJ/wHMezSab3tnGC0YPVdGNABEBAAG0
  102. +JEdvY3J5cHRvIFRlc3QgS2V5IDx0aGVtYXhAZ21haWwuY29tPoi5BBMBCgAjBQJV
  103. +/GgVAhsDBwsJCAcDAgEGFQgCCQoLBBYCAwECHgECF4AACgkQeXnQmhdGW9PFVAP+
  104. +K7TU0qX5ArvIONIxh/WAweyOk884c5cE8f+3NOPOOCRGyVy0FId5A7MmD5GOQh4H
  105. +JseOZVEVCqlmngEvtHZb3U1VYtVGE5WZ+6rQhGsMcWP5qaT4soYwMBlSYxgYwQcx
  106. +YhN9qOr292f9j2Y//TTIJmZT4Oa+lMxhWdqTfX+qMgG4jQRV/GgVAQQArhFSiij1
  107. +b+hT3dnapbEU+23Z1yTu1DfF6zsxQ4XQWEV3eR8v+8mEDDNcz8oyyF56k6UQ3rXi
  108. +UMTIwRDg4V6SbZmaFbZYCOwp/EmXJ3rfhm7z7yzXj2OFN22luuqbyVhuL7LRdB0M
  109. +pxgmjXb4tTvfgKd26x34S+QqUJ7W6uprY4sAEQEAAYifBBgBCgAJBQJV/GgVAhsM
  110. +AAoJEHl50JoXRlvT7y8D/02ckx4OMkKBZo7viyrBw0MLG92i+DC2bs35PooHR6zz
  111. +786mitjOp5z2QWNLBvxC70S0qVfCIz8jKupO1J6rq6Z8CcbLF3qjm6h1omUBf8Nd
  112. +EfXKD2/2HV6zMKVknnKzIEzauh+eCKS2CeJUSSSryap/QLVAjRnckaES/OsEWhNB
  113. +=RZia
  114. +-----END PGP PUBLIC KEY BLOCK-----
  115. +`
  116. +
  117. +const signedMessageV3 = `-----BEGIN PGP MESSAGE-----
  118. +Comment: GPGTools - https://gpgtools.org
  119. +
  120. +owGbwMvMwMVYWXlhlrhb9GXG03JJDKF/MtxDMjKLFYAoUaEktbhEITe1uDgxPVWP
  121. +q5NhKjMrWAVcC9evD8z/bF/uWNjqtk/X3y5/38XGRQHm/57rrDRYuGnTw597Xqka
  122. +uM3137/hH3Os+Jf2dc0fXOITKwJvXJvecPVs0ta+Vg7ZO1MLn8w58Xx+6L58mbka
  123. +DGHyU9yTueZE8D+QF/Tz28Y78dqtF56R1VPn9Xw4uJqrWYdd7b3vIZ1V6R4Nh05d
  124. +iT57d/OhWwA=
  125. +=hG7R
  126. +-----END PGP MESSAGE-----
  127. +`
上海开阖软件有限公司 沪ICP备12045867号-1