pgp.lisp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (in-package :cl-user)
  2. (defpackage #:assboard.pgp
  3. (:use :cl :assboard.utils)
  4. (:export #:list-fingerprints
  5. #:recv-key
  6. #:delete-key
  7. #:verify))
  8. (in-package #:assboard.pgp)
  9. (defvar *gpg-program* "/usr/bin/gpg" "gpg command")
  10. (defvar *gpg-homedir* (make-pathname :name ".gnupg"
  11. :defaults +project-path+)
  12. "gpg homedir (key storage)")
  13. (defvar *gpg-keyserver* "pgpkeys.mit.edu" "Keyserver to use")
  14. (defun extract-fingerprint (line)
  15. (let* ((last (1- (length line)))
  16. (first (1+ (position #\: line :from-end t :end last))))
  17. (subseq line first last)))
  18. (defun run-gpg (args &key input)
  19. (with-output-to-string (s)
  20. (sb-ext:run-program *gnupg-program*
  21. (append
  22. (list "--homedir" (namestring *gnupg-homedir*)
  23. "--status-fd" "1"
  24. "--logger-fd" "2"
  25. "--no-tty")
  26. args)
  27. :input input
  28. :output s
  29. :error nil)))
  30. (defun list-fingerprints ()
  31. (loop-lines (run-gpg '("--with-colons"
  32. "--with-fingerprint"
  33. "--list-keys"))
  34. if (starts-with "fpr:" line)
  35. collect (extract-fingerprint line)))
  36. (defun recv-key (fingerprint)
  37. (loop-lines (run-gpg (list "--keyserver" *gpg-keyserver*
  38. "--recv-keys" fingerprint))
  39. if (starts-with "[GNUPG:] IMPORT_OK" line)
  40. do (return t)))
  41. (defun delete-key (fingerprint)
  42. (not (loop-lines (run-gpg (list "--batch"
  43. "--delete-keys" fingerprint))
  44. if (starts-with "[GNUPG:] DELETE_PROBLEM" line)
  45. do (return t))))
  46. (defun verify (clearsigned)
  47. (with-input-from-string (input clearsigned)
  48. (loop-lines (run-gpg '("--verify") :input input)
  49. if (starts-with "[GNUPG:] VALIDSIG" line)
  50. do (return (values (subseq line (1+ (position #\Space line :from-end t)))
  51. (parse-integer line
  52. :start 70
  53. :end (position #\Space line :start 70)))))))