org-crypt.el 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ;;; org-crypt.el --- Public Key Encryption for Org Entries -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2007-2019 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. This setting can also be overridden in the CRYPTKEY property."
  77. :type 'string
  78. :group 'org-crypt)
  79. (defcustom org-crypt-disable-auto-save 'ask
  80. "What org-decrypt should do if `auto-save-mode' is enabled.
  81. t : Disable auto-save-mode for the current buffer
  82. prior to decrypting an entry.
  83. nil : Leave auto-save-mode enabled.
  84. This may cause data to be written to disk unencrypted!
  85. `ask' : Ask user whether or not to disable auto-save-mode
  86. for the current buffer.
  87. `encrypt': Leave auto-save-mode enabled for the current buffer,
  88. but automatically re-encrypt all decrypted entries
  89. *before* auto-saving.
  90. NOTE: This only works for entries which have a tag
  91. that matches `org-crypt-tag-matcher'."
  92. :group 'org-crypt
  93. :version "24.1"
  94. :type '(choice (const :tag "Always" t)
  95. (const :tag "Never" nil)
  96. (const :tag "Ask" ask)
  97. (const :tag "Encrypt" encrypt)))
  98. (defun org-crypt-check-auto-save ()
  99. "Check whether auto-save-mode is enabled for the current buffer.
  100. `auto-save-mode' may cause leakage when decrypting entries, so
  101. check whether it's enabled, and decide what to do about it.
  102. See `org-crypt-disable-auto-save'."
  103. (when buffer-auto-save-file-name
  104. (cond
  105. ((or
  106. (eq org-crypt-disable-auto-save t)
  107. (and
  108. (eq org-crypt-disable-auto-save 'ask)
  109. (y-or-n-p "org-decrypt: auto-save-mode may cause leakage. Disable it for current buffer? ")))
  110. (message "org-decrypt: Disabling auto-save-mode for %s"
  111. (or (buffer-file-name) (current-buffer)))
  112. ;; The argument to auto-save-mode has to be "-1", since
  113. ;; giving a "nil" argument toggles instead of disabling.
  114. (auto-save-mode -1))
  115. ((eq org-crypt-disable-auto-save nil)
  116. (message "org-decrypt: Decrypting entry with auto-save-mode enabled. This may cause leakage."))
  117. ((eq org-crypt-disable-auto-save 'encrypt)
  118. (message "org-decrypt: Enabling re-encryption on auto-save.")
  119. (add-hook 'auto-save-hook
  120. (lambda ()
  121. (message "org-crypt: Re-encrypting all decrypted entries due to auto-save.")
  122. (org-encrypt-entries))
  123. nil t))
  124. (t nil))))
  125. (defun org-crypt-key-for-heading ()
  126. "Return the encryption key for the current heading."
  127. (save-excursion
  128. (org-back-to-heading t)
  129. (or (org-entry-get nil "CRYPTKEY" 'selective)
  130. org-crypt-key
  131. (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
  132. (message "No crypt key set, using symmetric encryption."))))
  133. (defun org-encrypt-string (str crypt-key)
  134. "Return STR encrypted with CRYPT-KEY."
  135. ;; Text and key have to be identical, otherwise we re-crypt.
  136. (if (and (string= crypt-key (get-text-property 0 'org-crypt-key str))
  137. (string= (sha1 str) (get-text-property 0 'org-crypt-checksum str)))
  138. (get-text-property 0 'org-crypt-text str)
  139. (setq-local epg-context (epg-make-context nil t t))
  140. (epg-encrypt-string epg-context str (epg-list-keys epg-context crypt-key))))
  141. (defun org-encrypt-entry ()
  142. "Encrypt the content of the current headline."
  143. (interactive)
  144. (require 'epg)
  145. (org-with-wide-buffer
  146. (org-back-to-heading t)
  147. (setq-local epg-context (epg-make-context nil t t))
  148. (let ((start-heading (point)))
  149. (org-end-of-meta-data)
  150. (unless (looking-at-p "-----BEGIN PGP MESSAGE-----")
  151. (let ((folded (org-invisible-p))
  152. (crypt-key (org-crypt-key-for-heading))
  153. (beg (point)))
  154. (goto-char start-heading)
  155. (org-end-of-subtree t t)
  156. (org-back-over-empty-lines)
  157. (let ((contents (delete-and-extract-region beg (point))))
  158. (condition-case err
  159. (insert (org-encrypt-string contents crypt-key))
  160. ;; If encryption failed, make sure to insert back entry
  161. ;; contents in the buffer.
  162. (error (insert contents) (error (nth 1 err)))))
  163. (when folded
  164. (goto-char start-heading)
  165. (outline-hide-subtree))
  166. nil)))))
  167. (defun org-decrypt-entry ()
  168. "Decrypt the content of the current headline."
  169. (interactive)
  170. (require 'epg)
  171. (unless (org-before-first-heading-p)
  172. (org-with-wide-buffer
  173. (org-back-to-heading t)
  174. (let ((heading-point (point))
  175. (heading-was-invisible-p
  176. (save-excursion
  177. (outline-end-of-heading)
  178. (org-invisible-p))))
  179. (org-end-of-meta-data)
  180. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  181. (org-crypt-check-auto-save)
  182. (setq-local epg-context (epg-make-context nil t t))
  183. (let* ((end (save-excursion
  184. (search-forward "-----END PGP MESSAGE-----")
  185. (forward-line)
  186. (point)))
  187. (encrypted-text (buffer-substring-no-properties (point) end))
  188. (decrypted-text
  189. (decode-coding-string
  190. (epg-decrypt-string
  191. epg-context
  192. encrypted-text)
  193. 'utf-8)))
  194. ;; Delete region starting just before point, because the
  195. ;; outline property starts at the \n of the heading.
  196. (delete-region (1- (point)) end)
  197. ;; Store a checksum of the decrypted and the encrypted
  198. ;; text value. This allows reusing the same encrypted text
  199. ;; if the text does not change, and therefore avoid a
  200. ;; re-encryption process.
  201. (insert "\n" (propertize decrypted-text
  202. 'org-crypt-checksum (sha1 decrypted-text)
  203. 'org-crypt-key (org-crypt-key-for-heading)
  204. 'org-crypt-text encrypted-text))
  205. (when heading-was-invisible-p
  206. (goto-char heading-point)
  207. (org-flag-subtree t))
  208. nil))))))
  209. (defun org-encrypt-entries ()
  210. "Encrypt all top-level entries in the current buffer."
  211. (interactive)
  212. (let ((org--matcher-tags-todo-only nil))
  213. (org-scan-tags
  214. 'org-encrypt-entry
  215. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  216. org--matcher-tags-todo-only)))
  217. (defun org-decrypt-entries ()
  218. "Decrypt all entries in the current buffer."
  219. (interactive)
  220. (let ((org--matcher-tags-todo-only nil))
  221. (org-scan-tags
  222. 'org-decrypt-entry
  223. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  224. org--matcher-tags-todo-only)))
  225. (defun org-at-encrypted-entry-p ()
  226. "Is the current entry encrypted?"
  227. (unless (org-before-first-heading-p)
  228. (save-excursion
  229. (org-back-to-heading t)
  230. (search-forward "-----BEGIN PGP MESSAGE-----"
  231. (save-excursion (outline-next-heading)) t))))
  232. (defun org-crypt-use-before-save-magic ()
  233. "Add a hook to automatically encrypt entries before a file is saved to disk."
  234. (add-hook
  235. 'org-mode-hook
  236. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  237. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  238. (provide 'org-crypt)
  239. ;;; org-crypt.el ends here