org-crypt.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. ;;; org-crypt.el --- Public Key Encryption for Org Entries -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2007-2020 Free Software Foundation, Inc.
  3. ;; Emacs Lisp Archive Entry
  4. ;; Filename: org-crypt.el
  5. ;; Keywords: org-mode
  6. ;; Author: John Wiegley <johnw@gnu.org>
  7. ;; Maintainer: Peter Jones <pjones@pmade.com>
  8. ;; Description: Adds public key encryption to Org buffers
  9. ;; URL: http://www.newartisans.com/software/emacs.html
  10. ;; Compatibility: Emacs22
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  23. ;;; Commentary:
  24. ;; Right now this is just a set of functions to play with. It depends
  25. ;; on the epg library. Here's how you would use it:
  26. ;;
  27. ;; 1. To mark an entry for encryption, tag the heading with "crypt".
  28. ;; You can change the tag to any complex tag matching string by
  29. ;; setting the `org-crypt-tag-matcher' variable.
  30. ;;
  31. ;; 2. Set the encryption key to use in the `org-crypt-key' variable,
  32. ;; or use `M-x org-set-property' to set the property CRYPTKEY to
  33. ;; any address in your public keyring. The text of the entry (but
  34. ;; not its properties or headline) will be encrypted for this user.
  35. ;; For them to read it, the corresponding secret key must be
  36. ;; located in the secret key ring of the account where you try to
  37. ;; decrypt it. This makes it possible to leave secure notes that
  38. ;; only the intended recipient can read in a shared-org-mode-files
  39. ;; scenario.
  40. ;; If the key is not set, org-crypt will default to symmetric encryption.
  41. ;;
  42. ;; 3. To later decrypt an entry, use `org-decrypt-entries' or
  43. ;; `org-decrypt-entry'. It might be useful to bind this to a key,
  44. ;; like C-c C-/. I hope that in the future, C-c C-r can be might
  45. ;; overloaded to also decrypt an entry if it's encrypted, since
  46. ;; that fits nicely with the meaning of "reveal".
  47. ;;
  48. ;; 4. To automatically encrypt all necessary entries when saving a
  49. ;; file, call `org-crypt-use-before-save-magic' after loading
  50. ;; org-crypt.el.
  51. ;;; Thanks:
  52. ;; - Carsten Dominik
  53. ;; - Vitaly Ostanin
  54. (require 'org)
  55. ;;; Code:
  56. (declare-function epg-decrypt-string "epg" (context cipher))
  57. (declare-function epg-list-keys "epg" (context &optional name mode))
  58. (declare-function epg-make-context "epg"
  59. (&optional protocol armor textmode include-certs
  60. cipher-algorithm digest-algorithm
  61. compress-algorithm))
  62. (declare-function epg-encrypt-string "epg"
  63. (context plain recipients &optional sign always-trust))
  64. (defvar epg-context)
  65. (defgroup org-crypt nil
  66. "Org Crypt."
  67. :tag "Org Crypt"
  68. :group 'org)
  69. (defcustom org-crypt-tag-matcher "crypt"
  70. "The tag matcher used to find headings whose contents should be encrypted.
  71. See the \"Match syntax\" section of the org manual for more details."
  72. :type 'string
  73. :group 'org-crypt)
  74. (defcustom org-crypt-key ""
  75. "The default key to use when encrypting the contents of a heading.
  76. The string is matched against all keys in the key ring. In
  77. particular, the empty string matches no key.
  78. This setting can be overridden in the CRYPTKEY property.
  79. If no key is found, look for the `epa-file-encrypt-to' local
  80. variable. Ultimately fall back to symmetric encryption."
  81. :group 'org-crypt
  82. :type 'string)
  83. (defcustom org-crypt-disable-auto-save 'ask
  84. "What org-decrypt should do if `auto-save-mode' is enabled.
  85. t : Disable auto-save-mode for the current buffer
  86. prior to decrypting an entry.
  87. nil : Leave auto-save-mode enabled.
  88. This may cause data to be written to disk unencrypted!
  89. `ask' : Ask user whether or not to disable auto-save-mode
  90. for the current buffer.
  91. `encrypt': Leave auto-save-mode enabled for the current buffer,
  92. but automatically re-encrypt all decrypted entries
  93. *before* auto-saving.
  94. NOTE: This only works for entries which have a tag
  95. that matches `org-crypt-tag-matcher'."
  96. :group 'org-crypt
  97. :version "24.1"
  98. :type '(choice (const :tag "Always" t)
  99. (const :tag "Never" nil)
  100. (const :tag "Ask" ask)
  101. (const :tag "Encrypt" encrypt)))
  102. (defun org-crypt-check-auto-save ()
  103. "Check whether auto-save-mode is enabled for the current buffer.
  104. `auto-save-mode' may cause leakage when decrypting entries, so
  105. check whether it's enabled, and decide what to do about it.
  106. See `org-crypt-disable-auto-save'."
  107. (when buffer-auto-save-file-name
  108. (cond
  109. ((or
  110. (eq org-crypt-disable-auto-save t)
  111. (and
  112. (eq org-crypt-disable-auto-save 'ask)
  113. (y-or-n-p "org-decrypt: auto-save-mode may cause leakage. Disable it for current buffer? ")))
  114. (message "org-decrypt: Disabling auto-save-mode for %s"
  115. (or (buffer-file-name) (current-buffer)))
  116. ;; The argument to auto-save-mode has to be "-1", since
  117. ;; giving a "nil" argument toggles instead of disabling.
  118. (auto-save-mode -1))
  119. ((eq org-crypt-disable-auto-save nil)
  120. (message "org-decrypt: Decrypting entry with auto-save-mode enabled. This may cause leakage."))
  121. ((eq org-crypt-disable-auto-save 'encrypt)
  122. (message "org-decrypt: Enabling re-encryption on auto-save.")
  123. (add-hook 'auto-save-hook
  124. (lambda ()
  125. (message "org-crypt: Re-encrypting all decrypted entries due to auto-save.")
  126. (org-encrypt-entries))
  127. nil t))
  128. (t nil))))
  129. (defun org-crypt-key-for-heading ()
  130. "Return the encryption key(s) for the current heading.
  131. Assume `epg-context' is set."
  132. (or (epg-list-keys epg-context
  133. (or (org-entry-get nil "CRYPTKEY" 'selective)
  134. org-crypt-key))
  135. (bound-and-true-p epa-file-encrypt-to)
  136. (progn (message "No crypt key set, using symmetric encryption.") nil)))
  137. (defun org-encrypt-entry ()
  138. "Encrypt the content of the current headline."
  139. (interactive)
  140. (require 'epg)
  141. (org-with-wide-buffer
  142. (org-back-to-heading t)
  143. (let ((start-heading (point)))
  144. (org-end-of-meta-data)
  145. (unless (looking-at-p "-----BEGIN PGP MESSAGE-----")
  146. (setq-local epg-context (epg-make-context nil t t))
  147. (let ((folded (org-invisible-p))
  148. (crypt-key (org-crypt-key-for-heading))
  149. (beg (point)))
  150. (goto-char start-heading)
  151. (org-end-of-subtree t t)
  152. (org-back-over-empty-lines)
  153. (let* ((contents (delete-and-extract-region beg (point)))
  154. (key (get-text-property 0 'org-crypt-key contents))
  155. (checksum (get-text-property 0 'org-crypt-checksum contents)))
  156. (condition-case err
  157. (insert
  158. ;; Text and key have to be identical, otherwise we
  159. ;; re-crypt.
  160. (if (and (equal crypt-key key)
  161. (string= checksum (sha1 contents)))
  162. (get-text-property 0 'org-crypt-text contents)
  163. (epg-encrypt-string epg-context contents crypt-key)))
  164. ;; If encryption failed, make sure to insert back entry
  165. ;; contents in the buffer.
  166. (error
  167. (insert contents)
  168. (error (error-message-string err)))))
  169. (when folded
  170. (goto-char start-heading)
  171. (org-flag-subtree t))
  172. nil)))))
  173. (defun org-decrypt-entry ()
  174. "Decrypt the content of the current headline."
  175. (interactive)
  176. (require 'epg)
  177. (unless (org-before-first-heading-p)
  178. (org-with-wide-buffer
  179. (org-back-to-heading t)
  180. (let ((heading-point (point))
  181. (heading-was-invisible-p
  182. (save-excursion
  183. (outline-end-of-heading)
  184. (org-invisible-p))))
  185. (org-end-of-meta-data)
  186. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  187. (org-crypt-check-auto-save)
  188. (setq-local epg-context (epg-make-context nil t t))
  189. (let* ((end (save-excursion
  190. (search-forward "-----END PGP MESSAGE-----")
  191. (line-beginning-position 2)))
  192. (encrypted-text (buffer-substring-no-properties (point) end))
  193. (decrypted-text
  194. (decode-coding-string
  195. (epg-decrypt-string epg-context encrypted-text)
  196. 'utf-8)))
  197. ;; Delete region starting just before point, because the
  198. ;; outline property starts at the \n of the heading.
  199. (delete-region (1- (point)) end)
  200. ;; Store a checksum of the decrypted and the encrypted
  201. ;; text value. This allows reusing the same encrypted text
  202. ;; if the text does not change, and therefore avoid a
  203. ;; re-encryption process.
  204. (insert "\n" (propertize decrypted-text
  205. 'org-crypt-checksum (sha1 decrypted-text)
  206. 'org-crypt-key (org-crypt-key-for-heading)
  207. 'org-crypt-text encrypted-text))
  208. (when heading-was-invisible-p
  209. (goto-char heading-point)
  210. (org-flag-subtree t))
  211. nil))))))
  212. (defun org-encrypt-entries ()
  213. "Encrypt all top-level entries in the current buffer."
  214. (interactive)
  215. (let ((org--matcher-tags-todo-only nil))
  216. (org-scan-tags
  217. 'org-encrypt-entry
  218. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  219. org--matcher-tags-todo-only)))
  220. (defun org-decrypt-entries ()
  221. "Decrypt all entries in the current buffer."
  222. (interactive)
  223. (let ((org--matcher-tags-todo-only nil))
  224. (org-scan-tags
  225. 'org-decrypt-entry
  226. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  227. org--matcher-tags-todo-only)))
  228. (defun org-at-encrypted-entry-p ()
  229. "Is the current entry encrypted?"
  230. (unless (org-before-first-heading-p)
  231. (save-excursion
  232. (org-back-to-heading t)
  233. (search-forward "-----BEGIN PGP MESSAGE-----"
  234. (save-excursion (outline-next-heading)) t))))
  235. (defun org-crypt-use-before-save-magic ()
  236. "Add a hook to automatically encrypt entries before a file is saved to disk."
  237. (add-hook
  238. 'org-mode-hook
  239. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  240. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  241. (provide 'org-crypt)
  242. ;;; org-crypt.el ends here