org-crypt.el 9.2 KB

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