org-crypt.el 9.3 KB

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