openpgp.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (tests-openpgp)
  19. #:use-module (guix openpgp)
  20. #:use-module (gcrypt base16)
  21. #:use-module (gcrypt hash)
  22. #:use-module (gcrypt pk-crypto)
  23. #:use-module (ice-9 binary-ports)
  24. #:use-module (ice-9 match)
  25. #:use-module (rnrs bytevectors)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-64)
  29. #:use-module (srfi srfi-71))
  30. (define %radix-64-sample
  31. ;; Example of Radix-64 encoding from Section 6.6 of RFC4880.
  32. "\
  33. -----BEGIN PGP MESSAGE-----
  34. Version: OpenPrivacy 0.99
  35. yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
  36. vBSFjNSiVHsuAA==
  37. =njUN
  38. -----END PGP MESSAGE-----\n")
  39. (define %radix-64-sample/crc-mismatch
  40. ;; This time with a wrong CRC24 value.
  41. "\
  42. -----BEGIN PGP MESSAGE-----
  43. yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
  44. vBSFjNSiVHsuAA==
  45. =AAAA
  46. -----END PGP MESSAGE-----\n")
  47. (define %binary-sample
  48. ;; Same message as %radix-64-sample, decoded into bytevector.
  49. (base16-string->bytevector
  50. "c838013b6d96c411efecef17ecefe3ca0004ce8979ea250a897995f979a9\
  51. 0ad9a9a9050a890ac5a9c945a940c1a2fcd2bc14858cd4a2547b2e00"))
  52. (define %civodul-fingerprint
  53. "3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5")
  54. (define %civodul-key-id #x090B11993D9AEBB5) ;civodul.key
  55. ;; Test keys. They were generated in a container along these lines:
  56. ;; guix environment -CP --ad-hoc gnupg pinentry
  57. ;; then, within the container:
  58. ;; mkdir ~/.gnupg
  59. ;; echo pinentry-program ~/.guix-profile/bin/pinentry-tty > ~/.gnupg/gpg-agent.conf
  60. ;; gpg --quick-gen-key '<ludo+test-rsa@chbouib.org>' rsa
  61. ;; or similar.
  62. (define %rsa-key-id #xAE25DA2A70DEED59) ;rsa.key
  63. (define %dsa-key-id #x587918047BE8BD2C) ;dsa.key
  64. (define %ed25519-key-id #x771F49CBFAAE072D) ;ed25519.key
  65. (define %rsa-key-fingerprint
  66. (base16-string->bytevector
  67. (string-downcase "385F86CFC86B665A5C165E6BAE25DA2A70DEED59")))
  68. (define %dsa-key-fingerprint
  69. (base16-string->bytevector
  70. (string-downcase "2884A980422330A4F33DD97F587918047BE8BD2C")))
  71. (define %ed25519-key-fingerprint
  72. (base16-string->bytevector
  73. (string-downcase "44D31E21AF7138F9B632280A771F49CBFAAE072D")))
  74. ;;; The following are detached signatures created commands like:
  75. ;;; echo 'Hello!' | gpg -sba --digest-algo sha512
  76. ;;; They are detached (no PACKET-ONE-PASS-SIGNATURE) and uncompressed.
  77. (define %hello-signature/rsa
  78. ;; Signature of the ASCII string "Hello!\n".
  79. "\
  80. -----BEGIN PGP SIGNATURE-----
  81. iQEzBAABCAAdFiEEOF+Gz8hrZlpcFl5rriXaKnDe7VkFAl4SRF0ACgkQriXaKnDe
  82. 7VlIyQf/TU5rGUK42/C1ULoWvvm25Mjwh6xxoPPkuBxvos8bE6yKr/vJZePU3aSE
  83. mjbVFcO7DioxHMqLd49j803bUtdllJVU18ex9MkKbKjapkgEGkJsuTTzqyONprgk
  84. 7xtZGBWuxkP1M6hJICJkA3Ys+sTdKalux/pzr5OWAe+gxytTF/vr/EyJzdmBxbJv
  85. /fhd1SeVIXSw4c5gf2Wcvcgfy4N5CiLaUb7j4646KBTvDvmUMcDZ+vmKqC/XdQeQ
  86. PrjArGKt40ErVd98fwvNHZnw7VQMx0A3nL3joL5g7/RckDOUb4mqKoqLsLd0wPHP
  87. y32DiDUY9s3sy5OMzX4Y49em8vxvlg==
  88. =ASEm
  89. -----END PGP SIGNATURE-----")
  90. (define %hello-signature/dsa
  91. "\
  92. -----BEGIN PGP SIGNATURE-----
  93. iHUEABEIAB0WIQQohKmAQiMwpPM92X9YeRgEe+i9LAUCXhJFpQAKCRBYeRgEe+i9
  94. LDAaAQC0lXPQepvZBANAUtRLMZuOwL9NQPkfhIwUXtLEBBzyFQD/So8DcybXpRBi
  95. JKOiyAQQjMs/GJ6qMEQpRAhyyJRAock=
  96. =iAEc
  97. -----END PGP SIGNATURE-----")
  98. (define %hello-signature/ed25519/sha256 ;digest-algo: sha256
  99. "\
  100. -----BEGIN PGP SIGNATURE-----
  101. iHUEABYIAB0WIQRE0x4hr3E4+bYyKAp3H0nL+q4HLQUCXqRADAAKCRB3H0nL+q4H
  102. LUImAP9/foaSjPFC/MSr52LNV5ROSL9haea4jPpUP+N6ViFGowEA+AE/xpXPIqsz
  103. R6CdxMevURuqUpqQ7rHeiMmdUepeewU=
  104. =tLXy
  105. -----END PGP SIGNATURE-----")
  106. (define %hello-signature/ed25519/sha512 ;digest-algo: sha512
  107. "\
  108. -----BEGIN PGP SIGNATURE-----
  109. iHUEABYKAB0WIQRE0x4hr3E4+bYyKAp3H0nL+q4HLQUCXqRAGgAKCRB3H0nL+q4H
  110. LTeKAP0S8LiiosJXOARlYNdhfGw9j26lHrbwJh5CORGlaqqIJAEAoMYcmtNa2b6O
  111. inlEwB/KQM88O9RwA8xH7X5a0rodOw4=
  112. =68r/
  113. -----END PGP SIGNATURE-----")
  114. (define %hello-signature/ed25519/sha1 ;digest-algo: sha1
  115. "\
  116. -----BEGIN PGP SIGNATURE-----
  117. iHUEABYCAB0WIQRE0x4hr3E4+bYyKAp3H0nL+q4HLQUCXqRALQAKCRB3H0nL+q4H
  118. LdhEAQCfkdYhIVRa43oTNw9EL/TDFGQjXSHNRFVU0ktjkWbkQwEAjIXhvj2sqy79
  119. Pz7oopeN72xgggYUNT37ezqN3MeCqw0=
  120. =AE4G
  121. -----END PGP SIGNATURE-----")
  122. (test-begin "openpgp")
  123. (test-equal "read-radix-64"
  124. '(#t "PGP MESSAGE")
  125. (let-values (((data type)
  126. (call-with-input-string %radix-64-sample read-radix-64)))
  127. (list (bytevector? data) type)))
  128. (test-equal "read-radix-64, CRC mismatch"
  129. '(#f "PGP MESSAGE")
  130. (call-with-values
  131. (lambda ()
  132. (call-with-input-string %radix-64-sample/crc-mismatch
  133. read-radix-64))
  134. list))
  135. (test-assert "port-ascii-armored?, #t"
  136. (call-with-input-string %radix-64-sample port-ascii-armored?))
  137. (test-assert "port-ascii-armored?, #f"
  138. (not (port-ascii-armored? (open-bytevector-input-port %binary-sample))))
  139. (test-assert "get-openpgp-keyring"
  140. (let* ((key (search-path %load-path "tests/civodul.key"))
  141. (keyring (get-openpgp-keyring
  142. (open-bytevector-input-port
  143. (call-with-input-file key read-radix-64)))))
  144. (let-values (((primary packets)
  145. (lookup-key-by-id keyring %civodul-key-id)))
  146. (let ((fingerprint (openpgp-public-key-fingerprint primary)))
  147. (and (= (openpgp-public-key-id primary) %civodul-key-id)
  148. (not (openpgp-public-key-subkey? primary))
  149. (string=? (openpgp-format-fingerprint fingerprint)
  150. %civodul-fingerprint)
  151. (string=? (openpgp-user-id-value (find openpgp-user-id? packets))
  152. "Ludovic Courtès <ludo@gnu.org>")
  153. (eq? (lookup-key-by-fingerprint keyring fingerprint)
  154. primary))))))
  155. (test-equal "get-openpgp-detached-signature/ascii"
  156. (list `(,%dsa-key-id ,%dsa-key-fingerprint dsa sha256)
  157. `(,%rsa-key-id ,%rsa-key-fingerprint rsa sha256)
  158. `(,%ed25519-key-id ,%ed25519-key-fingerprint eddsa sha256)
  159. `(,%ed25519-key-id ,%ed25519-key-fingerprint eddsa sha512)
  160. `(,%ed25519-key-id ,%ed25519-key-fingerprint eddsa sha1))
  161. (map (lambda (str)
  162. (let ((signature (get-openpgp-detached-signature/ascii
  163. (open-input-string str))))
  164. (list (openpgp-signature-issuer-key-id signature)
  165. (openpgp-signature-issuer-fingerprint signature)
  166. (openpgp-signature-public-key-algorithm signature)
  167. (openpgp-signature-hash-algorithm signature))))
  168. (list %hello-signature/dsa
  169. %hello-signature/rsa
  170. %hello-signature/ed25519/sha256
  171. %hello-signature/ed25519/sha512
  172. %hello-signature/ed25519/sha1)))
  173. (test-equal "verify-openpgp-signature, missing key"
  174. `(missing-key ,%rsa-key-fingerprint)
  175. (let* ((keyring (get-openpgp-keyring (%make-void-port "r")))
  176. (signature (string->openpgp-packet %hello-signature/rsa)))
  177. (let-values (((status key)
  178. (verify-openpgp-signature signature keyring
  179. (open-input-string "Hello!\n"))))
  180. (list status key))))
  181. (test-equal "verify-openpgp-signature, good signatures"
  182. `((good-signature ,%rsa-key-id)
  183. (good-signature ,%dsa-key-id)
  184. (good-signature ,%ed25519-key-id)
  185. (good-signature ,%ed25519-key-id)
  186. (good-signature ,%ed25519-key-id))
  187. (map (lambda (key signature)
  188. (let* ((key (search-path %load-path key))
  189. (keyring (get-openpgp-keyring
  190. (open-bytevector-input-port
  191. (call-with-input-file key read-radix-64))))
  192. (signature (string->openpgp-packet signature)))
  193. (let-values (((status key)
  194. (verify-openpgp-signature signature keyring
  195. (open-input-string "Hello!\n"))))
  196. (list status (openpgp-public-key-id key)))))
  197. (list "tests/rsa.key" "tests/dsa.key"
  198. "tests/ed25519.key" "tests/ed25519.key" "tests/ed25519.key")
  199. (list %hello-signature/rsa %hello-signature/dsa
  200. %hello-signature/ed25519/sha256
  201. %hello-signature/ed25519/sha512
  202. %hello-signature/ed25519/sha1)))
  203. (test-equal "verify-openpgp-signature, bad signature"
  204. `((bad-signature ,%rsa-key-id)
  205. (bad-signature ,%dsa-key-id)
  206. (bad-signature ,%ed25519-key-id)
  207. (bad-signature ,%ed25519-key-id)
  208. (bad-signature ,%ed25519-key-id))
  209. (let ((keyring (fold (lambda (key keyring)
  210. (let ((key (search-path %load-path key)))
  211. (get-openpgp-keyring
  212. (open-bytevector-input-port
  213. (call-with-input-file key read-radix-64))
  214. keyring)))
  215. %empty-keyring
  216. '("tests/rsa.key" "tests/dsa.key"
  217. "tests/ed25519.key" "tests/ed25519.key"
  218. "tests/ed25519.key"))))
  219. (map (lambda (signature)
  220. (let ((signature (string->openpgp-packet signature)))
  221. (let-values (((status key)
  222. (verify-openpgp-signature signature keyring
  223. (open-input-string "What?!"))))
  224. (list status (openpgp-public-key-id key)))))
  225. (list %hello-signature/rsa %hello-signature/dsa
  226. %hello-signature/ed25519/sha256
  227. %hello-signature/ed25519/sha512
  228. %hello-signature/ed25519/sha1))))
  229. (test-end "openpgp")