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