org-crypt.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. (require 'org-compat)
  49. (declare-function epg-decrypt-string "epg" (context cipher))
  50. (declare-function epg-list-keys "epg" (context &optional name mode))
  51. (declare-function epg-make-context "epg"
  52. (&optional protocol armor textmode include-certs
  53. cipher-algorithm digest-algorithm
  54. compress-algorithm))
  55. (declare-function epg-encrypt-string "epg"
  56. (context plain recipients &optional sign always-trust))
  57. (defvar epg-context)
  58. (declare-function org-back-over-empty-lines "org" ())
  59. (declare-function org-back-to-heading "org" (&optional invisible-ok))
  60. (declare-function org-before-first-heading-p "org" ())
  61. (declare-function org-end-of-meta-data "org" (&optional full))
  62. (declare-function org-end-of-subtree "org" (&optional invisible-ok to-heading))
  63. (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
  64. (declare-function org-flag-subtree "org" (flag))
  65. (declare-function org-make-tags-matcher "org" (match))
  66. (declare-function org-previous-visible-heading "org" (arg))
  67. (declare-function org-scan-tags "org" (action matcher todo-only &optional start-level))
  68. (declare-function org-set-property "org" (property value))
  69. (defgroup org-crypt nil
  70. "Org Crypt."
  71. :tag "Org Crypt"
  72. :group 'org)
  73. (defcustom org-crypt-tag-matcher "crypt"
  74. "The tag matcher used to find headings whose contents should be encrypted.
  75. See the \"Match syntax\" section of the org manual for more details."
  76. :type 'string
  77. :group 'org-crypt)
  78. (defcustom org-crypt-key ""
  79. "The default key to use when encrypting the contents of a heading.
  80. If this variable is nil, always use symmetric encryption, unconditionally.
  81. Otherwise, The string is matched against all keys in the key ring.
  82. In particular, the empty string matches no key. If no key is found,
  83. look for the `epa-file-encrypt-to' local variable. Ultimately fall back
  84. to symmetric encryption.
  85. This setting can be overridden in the CRYPTKEY property."
  86. :group 'org-crypt
  87. :type '(choice
  88. (string :tag "Public key(s) matching")
  89. (const :tag "Symmetric encryption" nil)))
  90. (defcustom org-crypt-disable-auto-save 'ask
  91. "What org-decrypt should do if `auto-save-mode' is enabled.
  92. t : Disable auto-save-mode for the current buffer
  93. prior to decrypting an entry.
  94. nil : Leave auto-save-mode enabled.
  95. This may cause data to be written to disk unencrypted!
  96. `ask' : Ask user whether or not to disable auto-save-mode
  97. for the current buffer.
  98. `encrypt': Leave auto-save-mode enabled for the current buffer,
  99. but automatically re-encrypt all decrypted entries
  100. *before* auto-saving.
  101. NOTE: This only works for entries which have a tag
  102. that matches `org-crypt-tag-matcher'."
  103. :group 'org-crypt
  104. :version "24.1"
  105. :type '(choice (const :tag "Always" t)
  106. (const :tag "Never" nil)
  107. (const :tag "Ask" ask)
  108. (const :tag "Encrypt" encrypt)))
  109. (defun org-crypt--encrypted-text (beg end)
  110. "Return encrypted text in between BEG and END."
  111. ;; Ignore indentation.
  112. (replace-regexp-in-string
  113. "^[ \t]*" ""
  114. (buffer-substring-no-properties beg end)))
  115. (defun org-at-encrypted-entry-p ()
  116. "Is the current entry encrypted?
  117. When the entry is encrypted, return a pair (BEG . END) where BEG
  118. and END are buffer positions delimiting the encrypted area."
  119. (org-with-wide-buffer
  120. (unless (org-before-first-heading-p)
  121. (org-back-to-heading t)
  122. (org-end-of-meta-data 'standard)
  123. (let ((case-fold-search nil)
  124. (banner-start (rx (seq bol
  125. (zero-or-more (any "\t "))
  126. "-----BEGIN PGP MESSAGE-----"
  127. eol))))
  128. (when (looking-at banner-start)
  129. (let ((start (point))
  130. (banner-end (rx (seq bol
  131. (or (group (zero-or-more (any "\t "))
  132. "-----END PGP MESSAGE-----"
  133. eol)
  134. (seq (one-or-more "*") " "))))))
  135. (when (and (re-search-forward banner-end nil t) (match-string 1))
  136. (cons start (line-beginning-position 2)))))))))
  137. (defun org-crypt-check-auto-save ()
  138. "Check whether auto-save-mode is enabled for the current buffer.
  139. `auto-save-mode' may cause leakage when decrypting entries, so
  140. check whether it's enabled, and decide what to do about it.
  141. See `org-crypt-disable-auto-save'."
  142. (when buffer-auto-save-file-name
  143. (cond
  144. ((or
  145. (eq org-crypt-disable-auto-save t)
  146. (and
  147. (eq org-crypt-disable-auto-save 'ask)
  148. (y-or-n-p "org-decrypt: auto-save-mode may cause leakage. Disable it for current buffer? ")))
  149. (message "org-decrypt: Disabling auto-save-mode for %s"
  150. (or (buffer-file-name) (current-buffer)))
  151. ;; The argument to auto-save-mode has to be "-1", since
  152. ;; giving a "nil" argument toggles instead of disabling.
  153. (auto-save-mode -1))
  154. ((eq org-crypt-disable-auto-save nil)
  155. (message "org-decrypt: Decrypting entry with auto-save-mode enabled. This may cause leakage."))
  156. ((eq org-crypt-disable-auto-save 'encrypt)
  157. (message "org-decrypt: Enabling re-encryption on auto-save.")
  158. (add-hook 'auto-save-hook
  159. (lambda ()
  160. (message "org-crypt: Re-encrypting all decrypted entries due to auto-save.")
  161. (org-encrypt-entries))
  162. nil t))
  163. (t nil))))
  164. (defun org-crypt-key-for-heading ()
  165. "Return the encryption key(s) for the current heading.
  166. Assume `epg-context' is set."
  167. (and org-crypt-key
  168. (or (epg-list-keys epg-context
  169. (or (org-entry-get nil "CRYPTKEY" 'selective)
  170. org-crypt-key))
  171. (bound-and-true-p epa-file-encrypt-to)
  172. (progn
  173. (message "No crypt key set, using symmetric encryption.")
  174. nil))))
  175. ;;;###autoload
  176. (defun org-encrypt-entry ()
  177. "Encrypt the content of the current headline."
  178. (interactive)
  179. (unless (org-at-encrypted-entry-p)
  180. (require 'epg)
  181. (setq-local epg-context (epg-make-context nil t t))
  182. (org-with-wide-buffer
  183. (org-back-to-heading t)
  184. (let ((start-heading (point))
  185. (crypt-key (org-crypt-key-for-heading))
  186. (folded? (org-invisible-p (line-beginning-position))))
  187. (org-end-of-meta-data 'standard)
  188. (let ((beg (point))
  189. (folded-heading
  190. (and folded?
  191. (save-excursion
  192. (org-previous-visible-heading 1)
  193. (point)))))
  194. (goto-char start-heading)
  195. (org-end-of-subtree t t)
  196. (org-back-over-empty-lines)
  197. (let* ((contents (delete-and-extract-region beg (point)))
  198. (key (get-text-property 0 'org-crypt-key contents))
  199. (checksum (get-text-property 0 'org-crypt-checksum contents)))
  200. (condition-case err
  201. (insert
  202. ;; Text and key have to be identical, otherwise we
  203. ;; re-crypt.
  204. (if (and (equal crypt-key key)
  205. (string= checksum (sha1 contents)))
  206. (get-text-property 0 'org-crypt-text contents)
  207. (epg-encrypt-string epg-context contents crypt-key)))
  208. ;; If encryption failed, make sure to insert back entry
  209. ;; contents in the buffer.
  210. (error
  211. (insert contents)
  212. (error (error-message-string err)))))
  213. (when folded-heading
  214. (goto-char folded-heading)
  215. (org-flag-subtree t))
  216. nil)))))
  217. ;;;###autoload
  218. (defun org-decrypt-entry ()
  219. "Decrypt the content of the current headline."
  220. (interactive)
  221. (pcase (org-at-encrypted-entry-p)
  222. (`(,beg . ,end)
  223. (require 'epg)
  224. (setq-local epg-context (epg-make-context nil t t))
  225. (org-with-point-at beg
  226. (org-crypt-check-auto-save)
  227. (let* ((folded-heading
  228. (and (org-invisible-p)
  229. (save-excursion
  230. (org-previous-visible-heading 1)
  231. (point))))
  232. (encrypted-text (org-crypt--encrypted-text beg end))
  233. (decrypted-text
  234. (decode-coding-string
  235. (epg-decrypt-string epg-context encrypted-text)
  236. 'utf-8)))
  237. ;; Delete region starting just before point, because the
  238. ;; outline property starts at the \n of the heading.
  239. (delete-region (1- (point)) end)
  240. ;; Store a checksum of the decrypted and the encrypted text
  241. ;; value. This allows reusing the same encrypted text if the
  242. ;; text does not change, and therefore avoid a re-encryption
  243. ;; process.
  244. (insert "\n"
  245. (propertize decrypted-text
  246. 'org-crypt-checksum (sha1 decrypted-text)
  247. 'org-crypt-key (org-crypt-key-for-heading)
  248. 'org-crypt-text encrypted-text))
  249. (when folded-heading
  250. (goto-char folded-heading)
  251. (org-flag-subtree t))
  252. nil)))
  253. (_ nil)))
  254. (defvar org--matcher-tags-todo-only)
  255. ;;;###autoload
  256. (defun org-encrypt-entries ()
  257. "Encrypt all top-level entries in the current buffer."
  258. (interactive)
  259. (let ((org--matcher-tags-todo-only nil))
  260. (org-scan-tags
  261. 'org-encrypt-entry
  262. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  263. org--matcher-tags-todo-only)))
  264. ;;;###autoload
  265. (defun org-decrypt-entries ()
  266. "Decrypt all entries in the current buffer."
  267. (interactive)
  268. (let ((org--matcher-tags-todo-only nil))
  269. (org-scan-tags
  270. 'org-decrypt-entry
  271. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  272. org--matcher-tags-todo-only)))
  273. ;;;###autoload
  274. (defun org-crypt-use-before-save-magic ()
  275. "Add a hook to automatically encrypt entries before a file is saved to disk."
  276. (add-hook
  277. 'org-mode-hook
  278. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  279. (add-hook 'org-reveal-start-hook 'org-decrypt-entry)
  280. (provide 'org-crypt)
  281. ;;; org-crypt.el ends here