diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2020-09-30 03:47:47 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2020-09-30 03:47:47 +0200 |
commit | e608477da2ff300bbc7796bd3c1a42394d1f1148 (patch) | |
tree | 8b8b13e59095c98008abd42f380b27a7a783a39e /lisp/epa-file.el | |
parent | 818270286222bd90b9b0c27e703252c655adfb21 (diff) | |
download | emacs-e608477da2ff300bbc7796bd3c1a42394d1f1148.tar.gz emacs-e608477da2ff300bbc7796bd3c1a42394d1f1148.tar.bz2 emacs-e608477da2ff300bbc7796bd3c1a42394d1f1148.zip |
Give better error feedback on wrong password in .gpg files
* lisp/epa-file.el (epa-file--find-file-not-found-function): Do a
user-error when there's a wrong password (bug#43704).
(epa--wrong-password-p): New function.
(epa-file-insert-file-contents): Use it, and stash the error away
for later signalling.
* lisp/emacs-lisp/subr-x.el (if-let): Autoload.
Diffstat (limited to 'lisp/epa-file.el')
-rw-r--r-- | lisp/epa-file.el | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/lisp/epa-file.el b/lisp/epa-file.el index bb027b9abfb..7fd41784746 100644 --- a/lisp/epa-file.el +++ b/lisp/epa-file.el @@ -26,6 +26,7 @@ (require 'epa) (require 'epa-hook) +(eval-when-compile (require 'subr-x)) ;;; Options @@ -115,8 +116,17 @@ encryption is used." (let ((error epa-file-error)) (save-window-excursion (kill-buffer)) - (signal 'file-missing - (cons "Opening input file" (cdr error))))) + (if (nth 3 error) + (user-error "Wrong passphrase: %s" (nth 3 error)) + (signal 'file-missing + (cons "Opening input file" (cdr error)))))) + +(defun epa--wrong-password-p (context) + (let ((error-string (epg-context-error-output context))) + (and (string-match + "decryption failed: \\(Bad session key\\|No secret key\\)" + error-string) + (match-string 1 error-string)))) (defvar last-coding-system-used) (defun epa-file-insert-file-contents (file &optional visit beg end replace) @@ -159,7 +169,12 @@ encryption is used." (nth 3 error))) (let ((exists (file-exists-p local-file))) (when exists - (epa-display-error context) + (if-let ((wrong-password (epa--wrong-password-p context))) + ;; Don't display the *error* buffer if we just + ;; have a wrong password; let the later error + ;; handler notify the user. + (setq error (append error (list wrong-password))) + (epa-display-error context)) ;; When the .gpg file isn't an encrypted file (e.g., ;; it's a keyring.gpg file instead), then gpg will ;; say "Unexpected exit" as the error message. In |