org-crypt.el 9.3 KB

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