org-crypt.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.5
  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. (defun org-crypt-key-for-heading ()
  80. "Return the encryption key for the current heading."
  81. (save-excursion
  82. (org-back-to-heading t)
  83. (or (org-entry-get nil "CRYPTKEY" 'selective)
  84. org-crypt-key
  85. (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
  86. (message "No crypt key set, using symmetric encryption."))))
  87. (defun org-encrypt-string (str crypt-key)
  88. "Return STR encrypted with CRYPT-KEY."
  89. ;; Text and key have to be identical, otherwise we re-crypt.
  90. (if (and (string= crypt-key (get-text-property 0 'org-crypt-key str))
  91. (string= (sha1 str) (get-text-property 0 'org-crypt-checksum str)))
  92. (get-text-property 0 'org-crypt-text str)
  93. (let ((epg-context (epg-make-context nil t t)))
  94. (epg-encrypt-string epg-context str (epg-list-keys epg-context crypt-key)))))
  95. (defun org-encrypt-entry ()
  96. "Encrypt the content of the current headline."
  97. (interactive)
  98. (require 'epg)
  99. (save-excursion
  100. (org-back-to-heading t)
  101. (let ((start-heading (point)))
  102. (forward-line)
  103. (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
  104. (let ((folded (outline-invisible-p))
  105. (epg-context (epg-make-context nil t t))
  106. (crypt-key (org-crypt-key-for-heading))
  107. (beg (point))
  108. end encrypted-text)
  109. (goto-char start-heading)
  110. (org-end-of-subtree t t)
  111. (org-back-over-empty-lines)
  112. (setq end (point)
  113. encrypted-text
  114. (org-encrypt-string (buffer-substring beg end) crypt-key))
  115. (delete-region beg end)
  116. (insert encrypted-text)
  117. (when folded
  118. (goto-char start-heading)
  119. (hide-subtree))
  120. nil)))))
  121. (defun org-decrypt-entry ()
  122. "Decrypt the content of the current headline."
  123. (interactive)
  124. (require 'epg)
  125. (unless (org-before-first-heading-p)
  126. (save-excursion
  127. (org-back-to-heading t)
  128. (let ((heading-point (point))
  129. (heading-was-invisible-p
  130. (save-excursion
  131. (outline-end-of-heading)
  132. (outline-invisible-p))))
  133. (forward-line)
  134. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  135. (let* ((end (save-excursion
  136. (search-forward "-----END PGP MESSAGE-----")
  137. (forward-line)
  138. (point)))
  139. (epg-context (epg-make-context nil t t))
  140. (encrypted-text (buffer-substring-no-properties (point) end))
  141. (decrypted-text
  142. (decode-coding-string
  143. (epg-decrypt-string
  144. epg-context
  145. encrypted-text)
  146. 'utf-8)))
  147. ;; Delete region starting just before point, because the
  148. ;; outline property starts at the \n of the heading.
  149. (delete-region (1- (point)) end)
  150. ;; Store a checksum of the decrypted and the encrypted
  151. ;; text value. This allow to reuse the same encrypted text
  152. ;; if the text does not change, and therefore avoid a
  153. ;; re-encryption process.
  154. (insert "\n" (propertize decrypted-text
  155. 'org-crypt-checksum (sha1 decrypted-text)
  156. 'org-crypt-key (org-crypt-key-for-heading)
  157. 'org-crypt-text encrypted-text))
  158. (when heading-was-invisible-p
  159. (goto-char heading-point)
  160. (org-flag-subtree t))
  161. nil))))))
  162. (defun org-encrypt-entries ()
  163. "Encrypt all top-level entries in the current buffer."
  164. (interactive)
  165. (org-scan-tags
  166. 'org-encrypt-entry
  167. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  168. (defun org-decrypt-entries ()
  169. "Decrypt all entries in the current buffer."
  170. (interactive)
  171. (org-scan-tags
  172. 'org-decrypt-entry
  173. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  174. (defun org-crypt-use-before-save-magic ()
  175. "Add a hook to automatically encrypt entries before a file is saved to disk."
  176. (add-hook
  177. 'org-mode-hook
  178. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  179. ;; FIXME Find a better way to encrypt Org auto-saved buffers?
  180. ;; When `auto-save-default' is non-nil, make sure entries are
  181. ;; encrypted before auto-saving
  182. ;; (when auto-save-default
  183. ;; (add-hook
  184. ;; 'org-mode-hook
  185. ;; (lambda () (add-hook 'auto-save-hook 'org-encrypt-entries nil t))))
  186. (when (and (not (daemonp)) auto-save-default)
  187. (message "Warning: turn auto-save-mode off in Org buffers containing crypted entries.")
  188. (sit-for 5))
  189. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  190. (provide 'org-crypt)
  191. ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
  192. ;;; org-crypt.el ends here