org-crypt.el (11656B)
1 ;;; org-crypt.el --- Public Key Encryption for Org Entries -*- lexical-binding: t; -*- 2 ;; 3 ;; Copyright (C) 2007-2023 Free Software Foundation, Inc. 4 5 ;; Author: John Wiegley <johnw@gnu.org> 6 7 ;; This file is part of GNU Emacs. 8 ;; 9 ;; GNU Emacs is free software: you can redistribute it and/or modify 10 ;; it under the terms of the GNU General Public License as published by 11 ;; the Free Software Foundation, either version 3 of the License, or 12 ;; (at your option) any later version. 13 14 ;; GNU Emacs is distributed in the hope that it will be useful, 15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 ;; GNU General Public License for more details. 18 19 ;; You should have received a copy of the GNU General Public License 20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 21 22 ;;; Commentary: 23 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-/. 45 ;; 46 ;; 4. To automatically encrypt all necessary entries when saving a 47 ;; file, call `org-crypt-use-before-save-magic' after loading 48 ;; org-crypt.el. 49 50 ;;; Thanks: 51 52 ;; - Carsten Dominik 53 ;; - Vitaly Ostanin 54 55 ;;; Code: 56 57 (require 'org-macs) 58 (org-assert-version) 59 60 (require 'org-macs) 61 (require 'org-compat) 62 63 (declare-function epg-decrypt-string "epg" (context cipher)) 64 (declare-function epg-list-keys "epg" (context &optional name mode)) 65 (declare-function epg-make-context "epg" 66 (&optional protocol armor textmode include-certs 67 cipher-algorithm digest-algorithm 68 compress-algorithm)) 69 (declare-function epg-encrypt-string "epg" 70 (context plain recipients &optional sign always-trust)) 71 (defvar epg-context) 72 73 (declare-function org-back-over-empty-lines "org" ()) 74 (declare-function org-back-to-heading "org" (&optional invisible-ok)) 75 (declare-function org-before-first-heading-p "org" ()) 76 (declare-function org-end-of-meta-data "org" (&optional full)) 77 (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading)) 78 (declare-function org-entry-get "org" (pom property &optional inherit literal-nil)) 79 (declare-function org-fold-subtree "org-fold" (flag)) 80 (declare-function org-make-tags-matcher "org" (match)) 81 (declare-function org-previous-visible-heading "org" (arg)) 82 (declare-function org-scan-tags "org" (action matcher todo-only &optional start-level)) 83 (declare-function org-set-property "org" (property value)) 84 85 (defgroup org-crypt nil 86 "Org Crypt." 87 :tag "Org Crypt" 88 :group 'org) 89 90 (defcustom org-crypt-tag-matcher "crypt" 91 "The tag matcher used to find headings whose contents should be encrypted. 92 93 See the \"Match syntax\" section of the org manual for more details." 94 :type 'string 95 :group 'org-crypt) 96 97 (defcustom org-crypt-key "" 98 "The default key to use when encrypting the contents of a heading. 99 100 If this variable is nil, always use symmetric encryption, unconditionally. 101 102 Otherwise, The string is matched against all keys in the key ring. 103 In particular, the empty string matches no key. If no key is found, 104 look for the `epa-file-encrypt-to' local variable. Ultimately fall back 105 to symmetric encryption. 106 107 This setting can be overridden in the CRYPTKEY property." 108 :group 'org-crypt 109 :type '(choice 110 (string :tag "Public key(s) matching") 111 (const :tag "Symmetric encryption" nil))) 112 113 (defcustom org-crypt-disable-auto-save 'ask 114 "What org-decrypt should do if `auto-save-mode' is enabled. 115 116 t : Disable auto-save-mode for the current buffer 117 prior to decrypting an entry. 118 119 nil : Leave auto-save-mode enabled. 120 This may cause data to be written to disk unencrypted! 121 122 `ask' : Ask user whether or not to disable auto-save-mode 123 for the current buffer. 124 125 `encrypt': Leave auto-save-mode enabled for the current buffer, 126 but automatically re-encrypt all decrypted entries 127 *before* auto-saving. 128 NOTE: This only works for entries which have a tag 129 that matches `org-crypt-tag-matcher'." 130 :group 'org-crypt 131 :version "24.1" 132 :type '(choice (const :tag "Always" t) 133 (const :tag "Never" nil) 134 (const :tag "Ask" ask) 135 (const :tag "Encrypt" encrypt))) 136 137 (defun org-crypt--encrypted-text (beg end) 138 "Return encrypted text in between BEG and END." 139 ;; Ignore indentation. 140 (replace-regexp-in-string 141 "^[ \t]*" "" 142 (buffer-substring-no-properties beg end))) 143 144 (defun org-at-encrypted-entry-p () 145 "Is the current entry encrypted? 146 When the entry is encrypted, return a pair (BEG . END) where BEG 147 and END are buffer positions delimiting the encrypted area." 148 (org-with-wide-buffer 149 (unless (org-before-first-heading-p) 150 (org-back-to-heading t) 151 (org-end-of-meta-data 'standard) 152 (let ((case-fold-search nil) 153 (banner-start (rx (seq bol 154 (zero-or-more (any "\t ")) 155 "-----BEGIN PGP MESSAGE-----" 156 eol)))) 157 (when (looking-at banner-start) 158 (let ((start (point)) 159 (banner-end (rx (seq bol 160 (or (group (zero-or-more (any "\t ")) 161 "-----END PGP MESSAGE-----" 162 eol) 163 (seq (one-or-more "*") " ")))))) 164 (when (and (re-search-forward banner-end nil t) (match-string 1)) 165 (cons start (line-beginning-position 2))))))))) 166 167 (defun org-crypt-check-auto-save () 168 "Check whether auto-save-mode is enabled for the current buffer. 169 170 `auto-save-mode' may cause leakage when decrypting entries, so 171 check whether it's enabled, and decide what to do about it. 172 173 See `org-crypt-disable-auto-save'." 174 (when buffer-auto-save-file-name 175 (cond 176 ((or 177 (eq org-crypt-disable-auto-save t) 178 (and 179 (eq org-crypt-disable-auto-save 'ask) 180 (y-or-n-p "org-decrypt: auto-save-mode may cause leakage. Disable it for current buffer? "))) 181 (message "org-decrypt: Disabling auto-save-mode for %s" 182 (or (buffer-file-name) (current-buffer))) 183 ;; The argument to auto-save-mode has to be "-1", since 184 ;; giving a "nil" argument toggles instead of disabling. 185 (auto-save-mode -1)) 186 ((eq org-crypt-disable-auto-save nil) 187 (message "org-decrypt: Decrypting entry with auto-save-mode enabled. This may cause leakage.")) 188 ((eq org-crypt-disable-auto-save 'encrypt) 189 (message "org-decrypt: Enabling re-encryption on auto-save.") 190 (add-hook 'auto-save-hook 191 (lambda () 192 (message "org-crypt: Re-encrypting all decrypted entries due to auto-save.") 193 (org-encrypt-entries)) 194 nil t)) 195 (t nil)))) 196 197 (defun org-crypt-key-for-heading () 198 "Return the encryption key(s) for the current heading. 199 Assume `epg-context' is set." 200 (and org-crypt-key 201 (or (epg-list-keys epg-context 202 (pcase (org-entry-get nil "CRYPTKEY" 'selective 'literal-nil) 203 ("nil" "") 204 (key (or key org-crypt-key "")))) 205 (bound-and-true-p epa-file-encrypt-to) 206 (progn 207 (message "No crypt key set, using symmetric encryption.") 208 nil)))) 209 210 ;;;###autoload 211 (defun org-encrypt-entry () 212 "Encrypt the content of the current headline." 213 (interactive) 214 (unless (org-at-encrypted-entry-p) 215 (require 'epg) 216 (setq-local epg-context (epg-make-context nil t t)) 217 (org-with-wide-buffer 218 (org-back-to-heading t) 219 (let ((start-heading (point)) 220 (crypt-key (org-crypt-key-for-heading)) 221 (folded? (org-invisible-p (line-beginning-position)))) 222 (org-end-of-meta-data 'standard) 223 (let ((beg (point)) 224 (folded-heading 225 (and folded? 226 (save-excursion 227 (org-previous-visible-heading 1) 228 (point))))) 229 (goto-char start-heading) 230 (org-end-of-subtree t t) 231 (org-back-over-empty-lines) 232 (let* ((contents (delete-and-extract-region beg (point))) 233 (key (get-text-property 0 'org-crypt-key contents)) 234 (checksum (get-text-property 0 'org-crypt-checksum contents))) 235 (condition-case err 236 (insert 237 ;; Text and key have to be identical, otherwise we 238 ;; re-crypt. 239 (if (and (equal crypt-key key) 240 (string= checksum (sha1 contents))) 241 (get-text-property 0 'org-crypt-text contents) 242 (epg-encrypt-string epg-context contents crypt-key))) 243 ;; If encryption failed, make sure to insert back entry 244 ;; contents in the buffer. 245 (error 246 (insert contents) 247 (error (error-message-string err))))) 248 (when folded-heading 249 (goto-char folded-heading) 250 (org-fold-subtree t)) 251 nil))))) 252 253 ;;;###autoload 254 (defun org-decrypt-entry () 255 "Decrypt the content of the current headline." 256 (interactive) 257 (pcase (org-at-encrypted-entry-p) 258 (`(,beg . ,end) 259 (require 'epg) 260 (setq-local epg-context (epg-make-context nil t t)) 261 (org-with-point-at beg 262 (org-crypt-check-auto-save) 263 (let* ((folded-heading 264 (and (org-invisible-p) 265 (save-excursion 266 (org-previous-visible-heading 1) 267 (point)))) 268 (encrypted-text (org-crypt--encrypted-text beg end)) 269 (decrypted-text 270 (decode-coding-string 271 (epg-decrypt-string epg-context encrypted-text) 272 'utf-8))) 273 ;; Delete region starting just before point, because the 274 ;; outline property starts at the \n of the heading. 275 (delete-region (1- (point)) end) 276 ;; Store a checksum of the decrypted and the encrypted text 277 ;; value. This allows reusing the same encrypted text if the 278 ;; text does not change, and therefore avoid a re-encryption 279 ;; process. 280 (insert "\n" 281 (propertize decrypted-text 282 'org-crypt-checksum (sha1 decrypted-text) 283 'org-crypt-key (org-crypt-key-for-heading) 284 'org-crypt-text encrypted-text)) 285 (when folded-heading 286 (goto-char folded-heading) 287 (org-fold-subtree t)) 288 nil))) 289 (_ nil))) 290 291 (defvar org--matcher-tags-todo-only) 292 293 ;;;###autoload 294 (defun org-encrypt-entries () 295 "Encrypt all top-level entries in the current buffer." 296 (interactive) 297 (let ((org--matcher-tags-todo-only nil)) 298 (org-scan-tags 299 'org-encrypt-entry 300 (cdr (org-make-tags-matcher org-crypt-tag-matcher)) 301 org--matcher-tags-todo-only))) 302 303 ;;;###autoload 304 (defun org-decrypt-entries () 305 "Decrypt all entries in the current buffer." 306 (interactive) 307 (let ((org--matcher-tags-todo-only nil)) 308 (org-scan-tags 309 'org-decrypt-entry 310 (cdr (org-make-tags-matcher org-crypt-tag-matcher)) 311 org--matcher-tags-todo-only))) 312 313 ;;;###autoload 314 (defun org-crypt-use-before-save-magic () 315 "Add a hook to automatically encrypt entries before a file is saved to disk." 316 (add-hook 317 'org-mode-hook 318 (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t)))) 319 320 (add-hook 'org-fold-reveal-start-hook 'org-decrypt-entry) 321 322 (provide 'org-crypt) 323 324 ;;; org-crypt.el ends here