gnupg.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (guix tests gnupg)
  19. #:use-module (guix openpgp)
  20. #:use-module (guix utils)
  21. #:use-module (guix build utils)
  22. #:use-module (rnrs io ports)
  23. #:use-module (ice-9 match)
  24. #:export (gpg-command
  25. gpgconf-command
  26. with-fresh-gnupg-setup
  27. %ed25519-public-key-file
  28. %ed25519-secret-key-file
  29. %ed25519-2-public-key-file
  30. %ed25519-2-secret-key-file
  31. %ed25519-3-public-key-file
  32. %ed25519-3-secret-key-file
  33. read-openpgp-packet
  34. key-fingerprint
  35. key-fingerprint-vector
  36. key-id))
  37. (define gpg-command
  38. (make-parameter "gpg"))
  39. (define gpgconf-command
  40. (make-parameter "gpgconf"))
  41. (define (call-with-fresh-gnupg-setup imported thunk)
  42. (call-with-temporary-directory
  43. (lambda (home)
  44. (with-environment-variables `(("GNUPGHOME" ,home))
  45. (dynamic-wind
  46. (lambda ()
  47. (for-each (lambda (file)
  48. (invoke (gpg-command) "--import" file))
  49. imported))
  50. thunk
  51. (lambda ()
  52. ;; Terminate 'gpg-agent' & co.
  53. (invoke (gpgconf-command) "--kill" "all")))))))
  54. (define-syntax-rule (with-fresh-gnupg-setup imported exp ...)
  55. "Evaluate EXP in the context of a fresh GnuPG setup where all the files
  56. listed in IMPORTED, and only them, have been imported. This sets 'GNUPGHOME'
  57. such that the user's real GnuPG files are left untouched. The 'gpg-agent'
  58. process is terminated afterwards."
  59. (call-with-fresh-gnupg-setup imported (lambda () exp ...)))
  60. (define %ed25519-public-key-file
  61. (search-path %load-path "tests/keys/ed25519.pub"))
  62. (define %ed25519-secret-key-file
  63. (search-path %load-path "tests/keys/ed25519.sec"))
  64. (define %ed25519-2-public-key-file
  65. (search-path %load-path "tests/keys/ed25519-2.pub"))
  66. (define %ed25519-2-secret-key-file
  67. (search-path %load-path "tests/keys/ed25519-2.sec"))
  68. (define %ed25519-3-public-key-file
  69. (search-path %load-path "tests/keys/ed25519-3.pub"))
  70. (define %ed25519-3-secret-key-file
  71. (search-path %load-path "tests/keys/ed25519-3.sec"))
  72. (define (read-openpgp-packet file)
  73. (get-openpgp-packet
  74. (open-bytevector-input-port
  75. (call-with-input-file file read-radix-64))))
  76. (define key-fingerprint-vector
  77. (compose openpgp-public-key-fingerprint
  78. read-openpgp-packet))
  79. (define key-fingerprint
  80. (compose openpgp-format-fingerprint
  81. key-fingerprint-vector))