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.

164 lines
7.2 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestCheckArmoredGPGKeyString(t *testing.T) {
  10. testGPGArmor := `-----BEGIN PGP PUBLIC KEY BLOCK-----
  11. mQENBFh91QoBCADciaDd7aqegYkn4ZIG7J0p1CRwpqMGjxFroJEMg6M1ZiuEVTRv
  12. z49P4kcr1+98NvFmcNc+x5uJgvPCwr/N8ZW5nqBUs2yrklbFF4MeQomyZJJegP8m
  13. /dsRT3BwIT8YMUtJuCj0iqD9vuKYfjrztcMgC1sYwcE9E9OlA0pWBvUdU2i0TIB1
  14. vOq6slWGvHHa5l5gPfm09idlVxfH5+I+L1uIMx5ovbiVVU5x2f1AR1T18f0t2TVN
  15. 0agFTyuoYE1ATmvJHmMcsfgM1Gpd9hIlr9vlupT2kKTPoNzVzsJsOU6Ku/Lf/bac
  16. mF+TfSbRCtmG7dkYZ4metLj7zG/WkW8IvJARABEBAAG0HUFudG9pbmUgR0lSQVJE
  17. IDxzYXBrQHNhcGsuZnI+iQFUBBMBCAA+FiEEEIOwJg/1vpF1itJ4roJVuKDYKOQF
  18. Alh91QoCGwMFCQPCZwAFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQroJVuKDY
  19. KORreggAlIkC2QjHP5tb7b0+LksB2JMXdY+UzZBcJxtNmvA7gNQaGvWRrhrbePpa
  20. MKDP+3A4BPDBsWFbbB7N56vQ5tROpmWbNKuFOVER4S1bj0JZV0E+xkDLqt9QwQtQ
  21. ojd7oIZJwDUwdud1PvCza2mjgBqqiFE+twbc3i9xjciCGspMniUul1eQYLxRJ0w+
  22. sbvSOUnujnq5ByMSz9ij00O6aiPfNQS5oB5AALfpjYZDvWAAljLVrtmlQJWZ6dZo
  23. T/YNwsW2dECPuti8+Nmu5FxPGDTXxdbnRaeJTQ3T6q1oUVAv7yTXBx5NXfXkMa5i
  24. iEayQIH8Joq5Ev5ja/lRGQQhArMQ2bkBDQRYfdUKAQgAv7B3coLSrOQbuTZSlgWE
  25. QeT+7DWbmqE1LAQA1pQPcUPXLBUVd60amZJxF9nzUYcY83ylDi0gUNJS+DJGOXpT
  26. pzX2IOuOMGbtUSeKwg5s9O4SUO7f2yCc3RGaegER5zgESxelmOXG+b/hoNt7JbdU
  27. JtxcnLr91Jw2PBO/Xf0ZKJ01CQG2Yzdrrj6jnrHyx94seHy0i6xH1o0OuvfVMLfN
  28. /Vbb/ZHh6ym2wHNqRX62b0VAbchcJXX/MEehXGknKTkO6dDUd+mhRgWMf9ZGRFWx
  29. ag4qALimkf1FXtAyD0vxFYeyoWUQzrOvUsm2BxIN/986R08fhkBQnp5nz07mrU02
  30. cQARAQABiQE8BBgBCAAmFiEEEIOwJg/1vpF1itJ4roJVuKDYKOQFAlh91QoCGwwF
  31. CQPCZwAACgkQroJVuKDYKOT32wf/UZqMdPn5OhyhffFzjQx7wolrf92WkF2JkxtH
  32. 6c3Htjlt/p5RhtKEeErSrNAxB4pqB7dznHaJXiOdWEZtRVXXjlNHjrokGTesqtKk
  33. lHWtK62/MuyLdr+FdCl68F3ewuT2iu/MDv+D4HPqA47zma9xVgZ9ZNwJOpv3fCOo
  34. RfY66UjGEnfgYifgtI5S84/mp2jaSc9UNvlZB6RSf8cfbJUL74kS2lq+xzSlf0yP
  35. Av844q/BfRuVsJsK1NDNG09LC30B0l3LKBqlrRmRTUMHtgchdX2dY+p7GPOoSzlR
  36. MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
  37. =i9b7
  38. -----END PGP PUBLIC KEY BLOCK-----`
  39. key, err := checkArmoredGPGKeyString(testGPGArmor)
  40. assert.Nil(t, err, "Could not parse a valid GPG armored key", key)
  41. //TODO verify value of key
  42. }
  43. func TestExtractSignature(t *testing.T) {
  44. testGPGArmor := `-----BEGIN PGP PUBLIC KEY BLOCK-----
  45. mQENBFh91QoBCADciaDd7aqegYkn4ZIG7J0p1CRwpqMGjxFroJEMg6M1ZiuEVTRv
  46. z49P4kcr1+98NvFmcNc+x5uJgvPCwr/N8ZW5nqBUs2yrklbFF4MeQomyZJJegP8m
  47. /dsRT3BwIT8YMUtJuCj0iqD9vuKYfjrztcMgC1sYwcE9E9OlA0pWBvUdU2i0TIB1
  48. vOq6slWGvHHa5l5gPfm09idlVxfH5+I+L1uIMx5ovbiVVU5x2f1AR1T18f0t2TVN
  49. 0agFTyuoYE1ATmvJHmMcsfgM1Gpd9hIlr9vlupT2kKTPoNzVzsJsOU6Ku/Lf/bac
  50. mF+TfSbRCtmG7dkYZ4metLj7zG/WkW8IvJARABEBAAG0HUFudG9pbmUgR0lSQVJE
  51. IDxzYXBrQHNhcGsuZnI+iQFUBBMBCAA+FiEEEIOwJg/1vpF1itJ4roJVuKDYKOQF
  52. Alh91QoCGwMFCQPCZwAFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQroJVuKDY
  53. KORreggAlIkC2QjHP5tb7b0+LksB2JMXdY+UzZBcJxtNmvA7gNQaGvWRrhrbePpa
  54. MKDP+3A4BPDBsWFbbB7N56vQ5tROpmWbNKuFOVER4S1bj0JZV0E+xkDLqt9QwQtQ
  55. ojd7oIZJwDUwdud1PvCza2mjgBqqiFE+twbc3i9xjciCGspMniUul1eQYLxRJ0w+
  56. sbvSOUnujnq5ByMSz9ij00O6aiPfNQS5oB5AALfpjYZDvWAAljLVrtmlQJWZ6dZo
  57. T/YNwsW2dECPuti8+Nmu5FxPGDTXxdbnRaeJTQ3T6q1oUVAv7yTXBx5NXfXkMa5i
  58. iEayQIH8Joq5Ev5ja/lRGQQhArMQ2bkBDQRYfdUKAQgAv7B3coLSrOQbuTZSlgWE
  59. QeT+7DWbmqE1LAQA1pQPcUPXLBUVd60amZJxF9nzUYcY83ylDi0gUNJS+DJGOXpT
  60. pzX2IOuOMGbtUSeKwg5s9O4SUO7f2yCc3RGaegER5zgESxelmOXG+b/hoNt7JbdU
  61. JtxcnLr91Jw2PBO/Xf0ZKJ01CQG2Yzdrrj6jnrHyx94seHy0i6xH1o0OuvfVMLfN
  62. /Vbb/ZHh6ym2wHNqRX62b0VAbchcJXX/MEehXGknKTkO6dDUd+mhRgWMf9ZGRFWx
  63. ag4qALimkf1FXtAyD0vxFYeyoWUQzrOvUsm2BxIN/986R08fhkBQnp5nz07mrU02
  64. cQARAQABiQE8BBgBCAAmFiEEEIOwJg/1vpF1itJ4roJVuKDYKOQFAlh91QoCGwwF
  65. CQPCZwAACgkQroJVuKDYKOT32wf/UZqMdPn5OhyhffFzjQx7wolrf92WkF2JkxtH
  66. 6c3Htjlt/p5RhtKEeErSrNAxB4pqB7dznHaJXiOdWEZtRVXXjlNHjrokGTesqtKk
  67. lHWtK62/MuyLdr+FdCl68F3ewuT2iu/MDv+D4HPqA47zma9xVgZ9ZNwJOpv3fCOo
  68. RfY66UjGEnfgYifgtI5S84/mp2jaSc9UNvlZB6RSf8cfbJUL74kS2lq+xzSlf0yP
  69. Av844q/BfRuVsJsK1NDNG09LC30B0l3LKBqlrRmRTUMHtgchdX2dY+p7GPOoSzlR
  70. MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
  71. =i9b7
  72. -----END PGP PUBLIC KEY BLOCK-----`
  73. ekey, err := checkArmoredGPGKeyString(testGPGArmor)
  74. assert.Nil(t, err, "Could not parse a valid GPG armored key", ekey)
  75. pubkey := ekey.PrimaryKey
  76. content, err := base64EncPubKey(pubkey)
  77. assert.Nil(t, err, "Could not base64 encode a valid PublicKey content", ekey)
  78. key := &GPGKey{
  79. KeyID: pubkey.KeyIdString(),
  80. Content: content,
  81. Created: pubkey.CreationTime,
  82. CanSign: pubkey.CanSign(),
  83. CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
  84. CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
  85. CanCertify: pubkey.PubKeyAlgo.CanSign(),
  86. }
  87. cannotsignkey := &GPGKey{
  88. KeyID: pubkey.KeyIdString(),
  89. Content: content,
  90. Created: pubkey.CreationTime,
  91. CanSign: false,
  92. CanEncryptComms: false,
  93. CanEncryptStorage: false,
  94. CanCertify: false,
  95. }
  96. testGoodSigArmor := `-----BEGIN PGP SIGNATURE-----
  97. iQEzBAABCAAdFiEEEIOwJg/1vpF1itJ4roJVuKDYKOQFAljAiQIACgkQroJVuKDY
  98. KORvCgf6A/Ehh0r7QbO2tFEghT+/Ab+bN7jRN3zP9ed6/q/ophYmkrU0NibtbJH9
  99. AwFVdHxCmj78SdiRjaTKyevklXw34nvMftmvnOI4lBNUdw6KWl25/n/7wN0l2oZW
  100. rW3UawYpZgodXiLTYarfEimkDQmT67ArScjRA6lLbkEYKO0VdwDu+Z6yBUH3GWtm
  101. 45RkXpnsF6AXUfuD7YxnfyyDE1A7g7zj4vVYUAfWukJjqow/LsCUgETETJOqj9q3
  102. 52/oQDs04fVkIEtCDulcY+K/fKlukBPJf9WceNDEqiENUzN/Z1y0E+tJ07cSy4bk
  103. yIJb+d0OAaG8bxloO7nJq4Res1Qa8Q==
  104. =puvG
  105. -----END PGP SIGNATURE-----`
  106. testGoodPayload := `tree 56ae8d2799882b20381fc11659db06c16c68c61a
  107. parent c7870c39e4e6b247235ca005797703ec4254613f
  108. author Antoine GIRARD <sapk@sapk.fr> 1489012989 +0100
  109. committer Antoine GIRARD <sapk@sapk.fr> 1489012989 +0100
  110. Goog GPG
  111. `
  112. testBadSigArmor := `-----BEGIN PGP SIGNATURE-----
  113. iQEzBAABCAAdFiEE5yr4rn9ulbdMxJFiPYI/ySNrtNkFAljAiYkACgkQPYI/ySNr
  114. tNmDdQf+NXhVRiOGt0GucpjJCGrOnK/qqVUmQyRUfrqzVUdb/1/Ws84V5/wE547I
  115. 6z3oxeBKFsJa1CtIlxYaUyVhYnDzQtphJzub+Aw3UG0E2ywiE+N7RCa1Ufl7pPxJ
  116. U0SD6gvNaeTDQV/Wctu8v8DkCtEd3N8cMCDWhvy/FQEDztVtzm8hMe0Vdm0ozEH6
  117. P0W93sDNkLC5/qpWDN44sFlYDstW5VhMrnF0r/ohfaK2kpYHhkPk7WtOoHSUwQSg
  118. c4gfhjvXIQrWFnII1Kr5jFGlmgNSR02qpb31VGkMzSnBhWVf2OaHS/kI49QHJakq
  119. AhVDEnoYLCgoDGg9c3p1Ll2452/c6Q==
  120. =uoGV
  121. -----END PGP SIGNATURE-----`
  122. testBadPayload := `tree 3074ff04951956a974e8b02d57733b0766f7cf6c
  123. parent fd3577542f7ad1554c7c7c0eb86bb57a1324ad91
  124. author Antoine GIRARD <sapk@sapk.fr> 1489013107 +0100
  125. committer Antoine GIRARD <sapk@sapk.fr> 1489013107 +0100
  126. Unkonwn GPG key with good email
  127. `
  128. //Reading Sign
  129. goodSig, err := extractSignature(testGoodSigArmor)
  130. assert.Nil(t, err, "Could not parse a valid GPG armored signature", testGoodSigArmor)
  131. badSig, err := extractSignature(testBadSigArmor)
  132. assert.Nil(t, err, "Could not parse a valid GPG armored signature", testBadSigArmor)
  133. //Generating hash of commit
  134. goodHash, err := populateHash(goodSig.Hash, []byte(testGoodPayload))
  135. assert.Nil(t, err, "Could not generate a valid hash of payload", testGoodPayload)
  136. badHash, err := populateHash(badSig.Hash, []byte(testBadPayload))
  137. assert.Nil(t, err, "Could not generate a valid hash of payload", testBadPayload)
  138. //Verify
  139. err = verifySign(goodSig, goodHash, key)
  140. assert.Nil(t, err, "Could not validate a good signature")
  141. err = verifySign(badSig, badHash, key)
  142. assert.NotNil(t, err, "Validate a bad signature")
  143. err = verifySign(goodSig, goodHash, cannotsignkey)
  144. assert.NotNil(t, err, "Validate a bad signature with a kay that can not sign")
  145. }