org-crypt.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ;;; org-crypt.el --- Public key encryption for org-mode entries
  2. ;; Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
  3. ;; Emacs Lisp Archive Entry
  4. ;; Filename: org-crypt.el
  5. ;; Version: 7.6
  6. ;; Keywords: org-mode
  7. ;; Author: John Wiegley <johnw@gnu.org>
  8. ;; Maintainer: Peter Jones <pjones@pmade.com>
  9. ;; Description: Adds public key encryption to org-mode buffers
  10. ;; URL: http://www.newartisans.com/software/emacs.html
  11. ;; Compatibility: Emacs22
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;; Right now this is just a set of functions to play with. It depends
  26. ;; on the epg library. Here's how you would use it:
  27. ;;
  28. ;; 1. To mark an entry for encryption, tag the heading with "crypt".
  29. ;; You can change the tag to any complex tag matching string by
  30. ;; setting the `org-crypt-tag-matcher' variable.
  31. ;;
  32. ;; 2. Set the encryption key to use in the `org-crypt-key' variable,
  33. ;; or use `M-x org-set-property' to set the property CRYPTKEY to
  34. ;; any address in your public keyring. The text of the entry (but
  35. ;; not its properties or headline) will be encrypted for this user.
  36. ;; For them to read it, the corresponding secret key must be
  37. ;; located in the secret key ring of the account where you try to
  38. ;; decrypt it. This makes it possible to leave secure notes that
  39. ;; only the intended recipient can read in a shared-org-mode-files
  40. ;; scenario.
  41. ;; If the key is not set, org-crypt will default to symmetric encryption.
  42. ;;
  43. ;; 3. To later decrypt an entry, use `org-decrypt-entries' or
  44. ;; `org-decrypt-entry'. It might be useful to bind this to a key,
  45. ;; like C-c C-/. I hope that in the future, C-c C-r can be might
  46. ;; overloaded to also decrypt an entry if it's encrypted, since
  47. ;; that fits nicely with the meaning of "reveal".
  48. ;;
  49. ;; 4. To automatically encrypt all necessary entries when saving a
  50. ;; file, call `org-crypt-use-before-save-magic' after loading
  51. ;; org-crypt.el.
  52. ;;; Thanks:
  53. ;; - Carsten Dominik
  54. ;; - Vitaly Ostanin
  55. (require 'org)
  56. ;;; Code:
  57. (declare-function epg-decrypt-string "epg" (context cipher))
  58. (declare-function epg-list-keys "epg" (context &optional name mode))
  59. (declare-function epg-make-context "epg"
  60. (&optional protocol armor textmode include-certs
  61. cipher-algorithm digest-algorithm
  62. compress-algorithm))
  63. (declare-function epg-encrypt-string "epg"
  64. (context plain recipients &optional sign always-trust))
  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. :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-key-for-heading ()
  98. "Return the encryption key for the current heading."
  99. (save-excursion
  100. (org-back-to-heading t)
  101. (or (org-entry-get nil "CRYPTKEY" 'selective)
  102. org-crypt-key
  103. (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
  104. (message "No crypt key set, using symmetric encryption."))))
  105. (defun org-encrypt-string (str crypt-key)
  106. "Return STR encrypted with CRYPT-KEY."
  107. ;; Text and key have to be identical, otherwise we re-crypt.
  108. (if (and (string= crypt-key (get-text-property 0 'org-crypt-key str))
  109. (string= (sha1 str) (get-text-property 0 'org-crypt-checksum str)))
  110. (get-text-property 0 'org-crypt-text str)
  111. (let ((epg-context (epg-make-context nil t t)))
  112. (epg-encrypt-string epg-context str (epg-list-keys epg-context crypt-key)))))
  113. (defun org-encrypt-entry ()
  114. "Encrypt the content of the current headline."
  115. (interactive)
  116. (require 'epg)
  117. (save-excursion
  118. (org-back-to-heading t)
  119. (let ((start-heading (point)))
  120. (forward-line)
  121. (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
  122. (let ((folded (outline-invisible-p))
  123. (epg-context (epg-make-context nil t t))
  124. (crypt-key (org-crypt-key-for-heading))
  125. (beg (point))
  126. end encrypted-text)
  127. (goto-char start-heading)
  128. (org-end-of-subtree t t)
  129. (org-back-over-empty-lines)
  130. (setq end (point)
  131. encrypted-text
  132. (org-encrypt-string (buffer-substring beg end) crypt-key))
  133. (delete-region beg end)
  134. (insert encrypted-text)
  135. (when folded
  136. (goto-char start-heading)
  137. (hide-subtree))
  138. nil)))))
  139. (defun org-decrypt-entry ()
  140. "Decrypt the content of the current headline."
  141. (interactive)
  142. ; auto-save-mode may cause leakage, so check whether it's enabled.
  143. (when buffer-auto-save-file-name
  144. (cond
  145. ((or
  146. (eq org-crypt-disable-auto-save t)
  147. (and
  148. (eq org-crypt-disable-auto-save 'ask)
  149. (y-or-n-p "org-decrypt: auto-save-mode may cause leakage. Disable it for current buffer? ")))
  150. (message (concat "org-decrypt: Disabling auto-save-mode for " (or (buffer-file-name) (current-buffer))))
  151. ; The argument to auto-save-mode has to be "-1", since
  152. ; giving a "nil" argument toggles instead of disabling.
  153. (auto-save-mode -1))
  154. ((eq org-crypt-disable-auto-save nil)
  155. (message "org-decrypt: Decrypting entry with auto-save-mode enabled. This may cause leakage."))
  156. ((eq org-crypt-disable-auto-save 'encrypt)
  157. (message "org-decrypt: Enabling re-encryption on auto-save.")
  158. (add-hook 'auto-save-hook
  159. (lambda ()
  160. (message "org-crypt: Re-encrypting all decrypted entries due to auto-save.")
  161. (org-encrypt-entries))
  162. nil t))
  163. (t nil)))
  164. (require 'epg)
  165. (unless (org-before-first-heading-p)
  166. (save-excursion
  167. (org-back-to-heading t)
  168. (let ((heading-point (point))
  169. (heading-was-invisible-p
  170. (save-excursion
  171. (outline-end-of-heading)
  172. (outline-invisible-p))))
  173. (forward-line)
  174. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  175. (let* ((end (save-excursion
  176. (search-forward "-----END PGP MESSAGE-----")
  177. (forward-line)
  178. (point)))
  179. (epg-context (epg-make-context nil t t))
  180. (encrypted-text (buffer-substring-no-properties (point) end))
  181. (decrypted-text
  182. (decode-coding-string
  183. (epg-decrypt-string
  184. epg-context
  185. encrypted-text)
  186. 'utf-8)))
  187. ;; Delete region starting just before point, because the
  188. ;; outline property starts at the \n of the heading.
  189. (delete-region (1- (point)) end)
  190. ;; Store a checksum of the decrypted and the encrypted
  191. ;; text value. This allow to reuse the same encrypted text
  192. ;; if the text does not change, and therefore avoid a
  193. ;; re-encryption process.
  194. (insert "\n" (propertize decrypted-text
  195. 'org-crypt-checksum (sha1 decrypted-text)
  196. 'org-crypt-key (org-crypt-key-for-heading)
  197. 'org-crypt-text encrypted-text))
  198. (when heading-was-invisible-p
  199. (goto-char heading-point)
  200. (org-flag-subtree t))
  201. nil))))))
  202. (defun org-encrypt-entries ()
  203. "Encrypt all top-level entries in the current buffer."
  204. (interactive)
  205. (org-scan-tags
  206. 'org-encrypt-entry
  207. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  208. (defun org-decrypt-entries ()
  209. "Decrypt all entries in the current buffer."
  210. (interactive)
  211. (org-scan-tags
  212. 'org-decrypt-entry
  213. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  214. (defun org-crypt-use-before-save-magic ()
  215. "Add a hook to automatically encrypt entries before a file is saved to disk."
  216. (add-hook
  217. 'org-mode-hook
  218. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  219. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  220. (provide 'org-crypt)
  221. ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
  222. ;;; org-crypt.el ends here