signature_v3_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2013 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 packet
  5. import (
  6. "bytes"
  7. "crypto"
  8. "encoding/hex"
  9. "io"
  10. "io/ioutil"
  11. "testing"
  12. "golang.org/x/crypto/openpgp/armor"
  13. )
  14. func TestSignatureV3Read(t *testing.T) {
  15. r := v3KeyReader(t)
  16. Read(r) // Skip public key
  17. Read(r) // Skip uid
  18. packet, err := Read(r) // Signature
  19. if err != nil {
  20. t.Error(err)
  21. return
  22. }
  23. sig, ok := packet.(*SignatureV3)
  24. if !ok || sig.SigType != SigTypeGenericCert || sig.PubKeyAlgo != PubKeyAlgoRSA || sig.Hash != crypto.MD5 {
  25. t.Errorf("failed to parse, got: %#v", packet)
  26. }
  27. }
  28. func TestSignatureV3Reserialize(t *testing.T) {
  29. r := v3KeyReader(t)
  30. Read(r) // Skip public key
  31. Read(r) // Skip uid
  32. packet, err := Read(r)
  33. if err != nil {
  34. t.Error(err)
  35. return
  36. }
  37. sig := packet.(*SignatureV3)
  38. out := new(bytes.Buffer)
  39. if err = sig.Serialize(out); err != nil {
  40. t.Errorf("error reserializing: %s", err)
  41. return
  42. }
  43. expected, err := ioutil.ReadAll(v3KeyReader(t))
  44. if err != nil {
  45. t.Error(err)
  46. return
  47. }
  48. expected = expected[4+141+4+39:] // See pgpdump offsets below, this is where the sig starts
  49. if !bytes.Equal(expected, out.Bytes()) {
  50. t.Errorf("output doesn't match input (got vs expected):\n%s\n%s", hex.Dump(out.Bytes()), hex.Dump(expected))
  51. }
  52. }
  53. func v3KeyReader(t *testing.T) io.Reader {
  54. armorBlock, err := armor.Decode(bytes.NewBufferString(keySigV3Armor))
  55. if err != nil {
  56. t.Fatalf("armor Decode failed: %v", err)
  57. }
  58. return armorBlock.Body
  59. }
  60. // keySigV3Armor is some V3 public key I found in an SKS dump.
  61. // Old: Public Key Packet(tag 6)(141 bytes)
  62. // Ver 4 - new
  63. // Public key creation time - Fri Sep 16 17:13:54 CDT 1994
  64. // Pub alg - unknown(pub 0)
  65. // Unknown public key(pub 0)
  66. // Old: User ID Packet(tag 13)(39 bytes)
  67. // User ID - Armin M. Warda <warda@nephilim.ruhr.de>
  68. // Old: Signature Packet(tag 2)(149 bytes)
  69. // Ver 4 - new
  70. // Sig type - unknown(05)
  71. // Pub alg - ElGamal Encrypt-Only(pub 16)
  72. // Hash alg - unknown(hash 46)
  73. // Hashed Sub: unknown(sub 81, critical)(1988 bytes)
  74. const keySigV3Armor = `-----BEGIN PGP PUBLIC KEY BLOCK-----
  75. Version: SKS 1.0.10
  76. mI0CLnoYogAAAQQA1qwA2SuJwfQ5bCQ6u5t20ulnOtY0gykf7YjiK4LiVeRBwHjGq7v30tGV
  77. 5Qti7qqRW4Ww7CDCJc4sZMFnystucR2vLkXaSoNWoFm4Fg47NiisDdhDezHwbVPW6OpCFNSi
  78. ZAamtj4QAUBu8j4LswafrJqZqR9336/V3g8Yil2l48kABRG0J0FybWluIE0uIFdhcmRhIDx3
  79. YXJkYUBuZXBoaWxpbS5ydWhyLmRlPoiVAgUQLok2xwXR6zmeWEiZAQE/DgP/WgxPQh40/Po4
  80. gSkWZCDAjNdph7zexvAb0CcUWahcwiBIgg3U5ErCx9I5CNVA9U+s8bNrDZwgSIeBzp3KhWUx
  81. 524uhGgm6ZUTOAIKA6CbV6pfqoLpJnRYvXYQU5mIWsNa99wcu2qu18OeEDnztb7aLA6Ra9OF
  82. YFCbq4EjXRoOrYM=
  83. =LPjs
  84. -----END PGP PUBLIC KEY BLOCK-----`