org-crypt.el 11 KB

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