org-crypt.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. (pcase (org-entry-get nil "CRYPTKEY" 'selective 'literal-nil)
  172. ("nil" "")
  173. (key (or key org-crypt-key ""))))
  174. (bound-and-true-p epa-file-encrypt-to)
  175. (progn
  176. (message "No crypt key set, using symmetric encryption.")
  177. nil))))
  178. ;;;###autoload
  179. (defun org-encrypt-entry ()
  180. "Encrypt the content of the current headline."
  181. (interactive)
  182. (unless (org-at-encrypted-entry-p)
  183. (require 'epg)
  184. (setq-local epg-context (epg-make-context nil t t))
  185. (org-with-wide-buffer
  186. (org-back-to-heading t)
  187. (let ((start-heading (point))
  188. (crypt-key (org-crypt-key-for-heading))
  189. (folded? (org-invisible-p (line-beginning-position))))
  190. (org-end-of-meta-data 'standard)
  191. (let ((beg (point))
  192. (folded-heading
  193. (and folded?
  194. (save-excursion
  195. (org-previous-visible-heading 1)
  196. (point)))))
  197. (goto-char start-heading)
  198. (org-end-of-subtree t t)
  199. (org-back-over-empty-lines)
  200. (let* ((contents (delete-and-extract-region beg (point)))
  201. (key (get-text-property 0 'org-crypt-key contents))
  202. (checksum (get-text-property 0 'org-crypt-checksum contents)))
  203. (condition-case err
  204. (insert
  205. ;; Text and key have to be identical, otherwise we
  206. ;; re-crypt.
  207. (if (and (equal crypt-key key)
  208. (string= checksum (sha1 contents)))
  209. (get-text-property 0 'org-crypt-text contents)
  210. (epg-encrypt-string epg-context contents crypt-key)))
  211. ;; If encryption failed, make sure to insert back entry
  212. ;; contents in the buffer.
  213. (error
  214. (insert contents)
  215. (error (error-message-string err)))))
  216. (when folded-heading
  217. (goto-char folded-heading)
  218. (org-fold-subtree t))
  219. nil)))))
  220. ;;;###autoload
  221. (defun org-decrypt-entry ()
  222. "Decrypt the content of the current headline."
  223. (interactive)
  224. (pcase (org-at-encrypted-entry-p)
  225. (`(,beg . ,end)
  226. (require 'epg)
  227. (setq-local epg-context (epg-make-context nil t t))
  228. (org-with-point-at beg
  229. (org-crypt-check-auto-save)
  230. (let* ((folded-heading
  231. (and (org-invisible-p)
  232. (save-excursion
  233. (org-previous-visible-heading 1)
  234. (point))))
  235. (encrypted-text (org-crypt--encrypted-text beg end))
  236. (decrypted-text
  237. (decode-coding-string
  238. (epg-decrypt-string epg-context encrypted-text)
  239. 'utf-8)))
  240. ;; Delete region starting just before point, because the
  241. ;; outline property starts at the \n of the heading.
  242. (delete-region (1- (point)) end)
  243. ;; Store a checksum of the decrypted and the encrypted text
  244. ;; value. This allows reusing the same encrypted text if the
  245. ;; text does not change, and therefore avoid a re-encryption
  246. ;; process.
  247. (insert "\n"
  248. (propertize decrypted-text
  249. 'org-crypt-checksum (sha1 decrypted-text)
  250. 'org-crypt-key (org-crypt-key-for-heading)
  251. 'org-crypt-text encrypted-text))
  252. (when folded-heading
  253. (goto-char folded-heading)
  254. (org-fold-subtree t))
  255. nil)))
  256. (_ nil)))
  257. (defvar org--matcher-tags-todo-only)
  258. ;;;###autoload
  259. (defun org-encrypt-entries ()
  260. "Encrypt all top-level entries in the current buffer."
  261. (interactive)
  262. (let ((org--matcher-tags-todo-only nil))
  263. (org-scan-tags
  264. 'org-encrypt-entry
  265. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  266. org--matcher-tags-todo-only)))
  267. ;;;###autoload
  268. (defun org-decrypt-entries ()
  269. "Decrypt all entries in the current buffer."
  270. (interactive)
  271. (let ((org--matcher-tags-todo-only nil))
  272. (org-scan-tags
  273. 'org-decrypt-entry
  274. (cdr (org-make-tags-matcher org-crypt-tag-matcher))
  275. org--matcher-tags-todo-only)))
  276. ;;;###autoload
  277. (defun org-crypt-use-before-save-magic ()
  278. "Add a hook to automatically encrypt entries before a file is saved to disk."
  279. (add-hook
  280. 'org-mode-hook
  281. (lambda () (add-hook 'before-save-hook 'org-encrypt-entries nil t))))
  282. (add-hook 'org-fold-reveal-start-hook 'org-decrypt-entry)
  283. (provide 'org-crypt)
  284. ;;; org-crypt.el ends here