org-crypt.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.4
  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. ;;
  53. ;; TODO:
  54. ;; - Allow symmetric encryption as well
  55. ;;; Thanks:
  56. ;; - Carsten Dominik
  57. ;; - Vitaly Ostanin
  58. (require 'org)
  59. ;;; Code:
  60. (declare-function epg-decrypt-string "epg" (context cipher))
  61. (declare-function epg-list-keys "epg" (context &optional name mode))
  62. (declare-function epg-make-context "epg"
  63. (&optional protocol armor textmode include-certs
  64. cipher-algorithm digest-algorithm
  65. compress-algorithm))
  66. (declare-function epg-encrypt-string "epg"
  67. (context plain recipients &optional sign always-trust))
  68. (defgroup org-crypt nil
  69. "Org Crypt"
  70. :tag "Org Crypt"
  71. :group 'org)
  72. (defcustom org-crypt-tag-matcher "crypt"
  73. "The tag matcher used to find headings whose contents should be encrypted.
  74. See the \"Match syntax\" section of the org manual for more details."
  75. :type 'string
  76. :group 'org-crypt)
  77. (defcustom org-crypt-key ""
  78. "The default key to use when encrypting the contents of a heading.
  79. This setting can also be overridden in the CRYPTKEY property."
  80. :type 'string
  81. :group 'org-crypt)
  82. (defun org-crypt-key-for-heading ()
  83. "Return the encryption key for the current heading."
  84. (save-excursion
  85. (org-back-to-heading t)
  86. (or (org-entry-get nil "CRYPTKEY" 'selective)
  87. org-crypt-key
  88. (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to)
  89. (message "No crypt key set, using symmetric encryption."))))
  90. (defun org-encrypt-string (str crypt-key)
  91. "Return STR encrypted with CRYPT-KEY."
  92. ;; Text and key have to be identical, otherwise we re-crypt.
  93. (if (and (string= crypt-key (get-text-property 0 'org-crypt-key str))
  94. (string= (sha1 str) (get-text-property 0 'org-crypt-checksum str)))
  95. (get-text-property 0 'org-crypt-text str)
  96. (let ((epg-context (epg-make-context nil t t)))
  97. (epg-encrypt-string epg-context str (epg-list-keys epg-context crypt-key)))))
  98. (defun org-encrypt-entry ()
  99. "Encrypt the content of the current headline."
  100. (interactive)
  101. (require 'epg)
  102. (save-excursion
  103. (org-back-to-heading t)
  104. (let ((start-heading (point)))
  105. (forward-line)
  106. (when (not (looking-at "-----BEGIN PGP MESSAGE-----"))
  107. (let ((folded (outline-invisible-p))
  108. (epg-context (epg-make-context nil t t))
  109. (crypt-key (org-crypt-key-for-heading))
  110. (beg (point))
  111. end encrypted-text)
  112. (goto-char start-heading)
  113. (org-end-of-subtree t t)
  114. (org-back-over-empty-lines)
  115. (setq end (point)
  116. encrypted-text
  117. (org-encrypt-string (buffer-substring beg end) crypt-key))
  118. (delete-region beg end)
  119. (insert encrypted-text)
  120. (when folded
  121. (goto-char start-heading)
  122. (hide-subtree))
  123. nil)))))
  124. (defun org-decrypt-entry ()
  125. "Decrypt the content of the current headline."
  126. (interactive)
  127. (require 'epg)
  128. (unless (org-before-first-heading-p)
  129. (save-excursion
  130. (org-back-to-heading t)
  131. (let ((heading-point (point))
  132. (heading-was-invisible-p
  133. (save-excursion
  134. (outline-end-of-heading)
  135. (outline-invisible-p))))
  136. (forward-line)
  137. (when (looking-at "-----BEGIN PGP MESSAGE-----")
  138. (let* ((end (save-excursion
  139. (search-forward "-----END PGP MESSAGE-----")
  140. (forward-line)
  141. (point)))
  142. (epg-context (epg-make-context nil t t))
  143. (encrypted-text (buffer-substring-no-properties (point) end))
  144. (decrypted-text
  145. (decode-coding-string
  146. (epg-decrypt-string
  147. epg-context
  148. encrypted-text)
  149. 'utf-8)))
  150. ;; Delete region starting just before point, because the
  151. ;; outline property starts at the \n of the heading.
  152. (delete-region (1- (point)) end)
  153. ;; Store a checksum of the decrypted and the encrypted
  154. ;; text value. This allow to reuse the same encrypted text
  155. ;; if the text does not change, and therefore avoid a
  156. ;; re-encryption process.
  157. (insert "\n" (propertize decrypted-text
  158. 'org-crypt-checksum (sha1 decrypted-text)
  159. 'org-crypt-key (org-crypt-key-for-heading)
  160. 'org-crypt-text encrypted-text))
  161. (when heading-was-invisible-p
  162. (goto-char heading-point)
  163. (org-flag-subtree t))
  164. nil))))))
  165. (defun org-encrypt-entries ()
  166. "Encrypt all top-level entries in the current buffer."
  167. (interactive)
  168. (org-scan-tags
  169. 'org-encrypt-entry
  170. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  171. (defun org-decrypt-entries ()
  172. "Decrypt all entries in the current buffer."
  173. (interactive)
  174. (org-scan-tags
  175. 'org-decrypt-entry
  176. (cdr (org-make-tags-matcher org-crypt-tag-matcher))))
  177. (defun org-crypt-use-before-save-magic ()
  178. "Add a hook to automatically encrypt entries before a file is saved to disk."
  179. (add-hook
  180. 'org-mode-hook
  181. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  182. ;; FIXME Find a better way to encrypt Org auto-saved buffers?
  183. ;; When `auto-save-default' is non-nil, make sure entries are
  184. ;; encrypted before auto-saving
  185. ;; (when auto-save-default
  186. ;; (add-hook
  187. ;; 'org-mode-hook
  188. ;; (lambda () (add-hook 'auto-save-hook 'org-encrypt-entries nil t))))
  189. (when auto-save-default
  190. (message "Warning: turn auto-save-mode off in Org buffers containing crypted entries.")
  191. (sit-for 5))
  192. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  193. (provide 'org-crypt)
  194. ;; arch-tag: 8202ed2c-221e-4001-9e4b-54674a7e846e
  195. ;;; org-crypt.el ends here