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.

224 lines
10 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.NoError(t, err, "Could not parse a valid GPG public armored rsa key", key)
  41. //TODO verify value of key
  42. }
  43. func TestCheckArmoredbrainpoolP256r1GPGKeyString(t *testing.T) {
  44. testGPGArmor := `-----BEGIN PGP PUBLIC KEY BLOCK-----
  45. Version: GnuPG v2
  46. mFMEV6HwkhMJKyQDAwIIAQEHAgMEUsvJO/j5dFMRRj67qeZC9fSKBsGZdOHRj2+6
  47. 8wssmbUuLTfT/ZjIbExETyY8hFnURRGpD2Ifyz0cKjXcbXfJtrQTRm9vYmFyIDxm
  48. b29AYmFyLmRlPoh/BBMTCAAnBQJZOsDIAhsDBQkJZgGABQsJCAcCBhUICQoLAgQW
  49. AgMBAh4BAheAAAoJEGuJTd/DBMzmNVQA/2beUrv1yU4gyvCiPDEm3pK42cSfaL5D
  50. muCtPCUg9hlWAP4yq6M78NW8STfsXgn6oeziMYiHSTmV14nOamLuwwDWM7hXBFeh
  51. 8JISCSskAwMCCAEBBwIDBG3A+XfINAZp1CTse2mRNgeUE5DbUtEpO8ALXKA1UQsQ
  52. DLKq27b7zTgawgXIGUGP6mWsJ5oH7MNAJ/uKTsYmX40DAQgHiGcEGBMIAA8FAleh
  53. 8JICGwwFCQlmAYAACgkQa4lN38MEzOZwKAD/QKyerAgcvzzLaqvtap3XvpYcw9tc
  54. OyjLLnFQiVmq7kEA/0z0CQe3ZQiQIq5zrs7Nh1XRkFAo8GlU/SGC9XFFi722
  55. =ZiSe
  56. -----END PGP PUBLIC KEY BLOCK-----`
  57. key, err := checkArmoredGPGKeyString(testGPGArmor)
  58. assert.NoError(t, err, "Could not parse a valid GPG public armored brainpoolP256r1 key", key)
  59. //TODO verify value of key
  60. }
  61. func TestExtractSignature(t *testing.T) {
  62. testGPGArmor := `-----BEGIN PGP PUBLIC KEY BLOCK-----
  63. mQENBFh91QoBCADciaDd7aqegYkn4ZIG7J0p1CRwpqMGjxFroJEMg6M1ZiuEVTRv
  64. z49P4kcr1+98NvFmcNc+x5uJgvPCwr/N8ZW5nqBUs2yrklbFF4MeQomyZJJegP8m
  65. /dsRT3BwIT8YMUtJuCj0iqD9vuKYfjrztcM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f1FXtAyD0vxFYeyoWUQzrOvUsm2BxIN/986R08fhkBQnp5nz07mrU02
  82. cQARAQABiQE8BBgBCAAmFiEEEIOwJg/1vpF1itJ4roJVuKDYKOQFAlh91QoCGwwF
  83. CQPCZwAACgkQroJVuKDYKOT32wf/UZqMdPn5OhyhffFzjQx7wolrf92WkF2JkxtH
  84. 6c3Htjlt/p5RhtKEeErSrNAxB4pqB7dznHaJXiOdWEZtRVXXjlNHjrokGTesqtKk
  85. lHWtK62/MuyLdr+FdCl68F3ewuT2iu/MDv+D4HPqA47zma9xVgZ9ZNwJOpv3fCOo
  86. RfY66UjGEnfgYifgtI5S84/mp2jaSc9UNvlZB6RSf8cfbJUL74kS2lq+xzSlf0yP
  87. Av844q/BfRuVsJsK1NDNG09LC30B0l3LKBqlrRmRTUMHtgchdX2dY+p7GPOoSzlR
  88. MkM/fdpyc2hY7Dl/+qFmN5MG5yGmMpQcX+RNNR222ibNC1D3wg==
  89. =i9b7
  90. -----END PGP PUBLIC KEY BLOCK-----`
  91. ekey, err := checkArmoredGPGKeyString(testGPGArmor)
  92. assert.NoError(t, err, "Could not parse a valid GPG armored key", ekey)
  93. pubkey := ekey.PrimaryKey
  94. content, err := base64EncPubKey(pubkey)
  95. assert.NoError(t, err, "Could not base64 encode a valid PublicKey content", ekey)
  96. key := &GPGKey{
  97. KeyID: pubkey.KeyIdString(),
  98. Content: content,
  99. Created: pubkey.CreationTime,
  100. CanSign: pubkey.CanSign(),
  101. CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
  102. CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
  103. CanCertify: pubkey.PubKeyAlgo.CanSign(),
  104. }
  105. cannotsignkey := &GPGKey{
  106. KeyID: pubkey.KeyIdString(),
  107. Content: content,
  108. Created: pubkey.CreationTime,
  109. CanSign: false,
  110. CanEncryptComms: false,
  111. CanEncryptStorage: false,
  112. CanCertify: false,
  113. }
  114. testGoodSigArmor := `-----BEGIN PGP SIGNATURE-----
  115. iQEzBAABCAAdFiEEEIOwJg/1vpF1itJ4roJVuKDYKOQFAljAiQIACgkQroJVuKDY
  116. KORvCgf6A/Ehh0r7QbO2tFEghT+/Ab+bN7jRN3zP9ed6/q/ophYmkrU0NibtbJH9
  117. AwFVdHxCmj78SdiRjaTKyevklXw34nvMftmvnOI4lBNUdw6KWl25/n/7wN0l2oZW
  118. rW3UawYpZgodXiLTYarfEimkDQmT67ArScjRA6lLbkEYKO0VdwDu+Z6yBUH3GWtm
  119. 45RkXpnsF6AXUfuD7YxnfyyDE1A7g7zj4vVYUAfWukJjqow/LsCUgETETJOqj9q3
  120. 52/oQDs04fVkIEtCDulcY+K/fKlukBPJf9WceNDEqiENUzN/Z1y0E+tJ07cSy4bk
  121. yIJb+d0OAaG8bxloO7nJq4Res1Qa8Q==
  122. =puvG
  123. -----END PGP SIGNATURE-----`
  124. testGoodPayload := `tree 56ae8d2799882b20381fc11659db06c16c68c61a
  125. parent c7870c39e4e6b247235ca005797703ec4254613f
  126. author Antoine GIRARD <sapk@sapk.fr> 1489012989 +0100
  127. committer Antoine GIRARD <sapk@sapk.fr> 1489012989 +0100
  128. Goog GPG
  129. `
  130. testBadSigArmor := `-----BEGIN PGP SIGNATURE-----
  131. iQEzBAABCAAdFiEE5yr4rn9ulbdMxJFiPYI/ySNrtNkFAljAiYkACgkQPYI/ySNr
  132. tNmDdQf+NXhVRiOGt0GucpjJCGrOnK/qqVUmQyRUfrqzVUdb/1/Ws84V5/wE547I
  133. 6z3oxeBKFsJa1CtIlxYaUyVhYnDzQtphJzub+Aw3UG0E2ywiE+N7RCa1Ufl7pPxJ
  134. U0SD6gvNaeTDQV/Wctu8v8DkCtEd3N8cMCDWhvy/FQEDztVtzm8hMe0Vdm0ozEH6
  135. P0W93sDNkLC5/qpWDN44sFlYDstW5VhMrnF0r/ohfaK2kpYHhkPk7WtOoHSUwQSg
  136. c4gfhjvXIQrWFnII1Kr5jFGlmgNSR02qpb31VGkMzSnBhWVf2OaHS/kI49QHJakq
  137. AhVDEnoYLCgoDGg9c3p1Ll2452/c6Q==
  138. =uoGV
  139. -----END PGP SIGNATURE-----`
  140. testBadPayload := `tree 3074ff04951956a974e8b02d57733b0766f7cf6c
  141. parent fd3577542f7ad1554c7c7c0eb86bb57a1324ad91
  142. author Antoine GIRARD <sapk@sapk.fr> 1489013107 +0100
  143. committer Antoine GIRARD <sapk@sapk.fr> 1489013107 +0100
  144. Unknown GPG key with good email
  145. `
  146. //Reading Sign
  147. goodSig, err := extractSignature(testGoodSigArmor)
  148. assert.NoError(t, err, "Could not parse a valid GPG armored signature", testGoodSigArmor)
  149. badSig, err := extractSignature(testBadSigArmor)
  150. assert.NoError(t, err, "Could not parse a valid GPG armored signature", testBadSigArmor)
  151. //Generating hash of commit
  152. goodHash, err := populateHash(goodSig.Hash, []byte(testGoodPayload))
  153. assert.NoError(t, err, "Could not generate a valid hash of payload", testGoodPayload)
  154. badHash, err := populateHash(badSig.Hash, []byte(testBadPayload))
  155. assert.NoError(t, err, "Could not generate a valid hash of payload", testBadPayload)
  156. //Verify
  157. err = verifySign(goodSig, goodHash, key)
  158. assert.NoError(t, err, "Could not validate a good signature")
  159. err = verifySign(badSig, badHash, key)
  160. assert.Error(t, err, "Validate a bad signature")
  161. err = verifySign(goodSig, goodHash, cannotsignkey)
  162. assert.Error(t, err, "Validate a bad signature with a kay that can not sign")
  163. }
  164. func TestCheckGPGUserEmail(t *testing.T) {
  165. testEmailWithUpperCaseLetters := `-----BEGIN PGP PUBLIC KEY BLOCK-----
  166. Version: GnuPG v1
  167. mQENBFlEBvMBCADe+EQcfv/aKbMFy7YB8e/DE+hY39sfjvdvSgeXtNhfmYvIOUjT
  168. ORMCvce2Oxzb3HTI0rjYsJpzo9jEQ53dB3vdr0ne5Juby6N7QPjof3NR+ko50Ki2
  169. 0ilOjYuA0v6VHLIn70UBa9NEf+XDuE7P+Lbtl2L9B9OMXtcTAZoA3cJySgtNFNIG
  170. AVefPi8LeOcekL39wxJEA8Ozd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T
  186. DszmNu+Axf4IB/0S9NTc6kpwW+ZPZQNTWR5oKDEaXVCRLccOlkt33txMvk/z2jNM
  187. trEke99ss5L1bRyWB5fRA+XVsPmW9kIk8pmGFmxqp2nSxr9m9rlL5oTYH8u6dfSm
  188. zwGhqkfITjPI7hyNN52PLANwoS0o4dLzIE65ewigx6cnRlrT2IENObxG/tlxaYg1
  189. NHahJX0uFlVk0W0bLBrs3fTDw1lS/N8HpyQb+5ryQmiIb2a48aygCS/h2qeRlX1d
  190. Q0KHb+QcycSgbDx0ZAvdIacuKvBBcbxrsmFUI4LR+oIup0G9gUc0roPvr014jYQL
  191. 7f8r/8fpcN8t+I/41QHCs6L/BEIdTHW3rTQ6
  192. =zHo9
  193. -----END PGP PUBLIC KEY BLOCK-----`
  194. key, err := AddGPGKey(1, testEmailWithUpperCaseLetters)
  195. assert.NoError(t, err)
  196. assert.Len(t, key.Emails, 1)
  197. assert.Equal(t, "user1@example.com", key.Emails[0].Email)
  198. }