Subject: Re: bug#63627: Improve plstore.el and fix various issues of it

From: Jens Schmidt <[email-masked]>
Date: Thu, 14 Sep 2023 23:24:54 +0200

On 2023-09-08  13:34, Eli Zaretskii wrote:
>> Date: Fri, 8 Sep 2023 11:16:50 +0200
>> From: Jens Schmidt <[email-masked]>
>> Cc: [email-masked]
>>
>> Independently of the tests, I'll do the Emacs 29 bugs next, also on this
>> bug.
> 
> Thanks.

After some consideration actually only one Emacs 29 bug requires fixing.
Namely that the sequence:

    (setq plstore (plstore-open plstore-test-file))
    (plstore-save plstore)
    (plstore-close plstore)

silently drops any encrypted data in PLSTORE.  Verified in my local
master with the new ERT tests, and also verified that the attached patch
fixes the issue.

Please consider for committing on emacs-29.

Thanks
From 70ebdcee78202070a59cc9dbb1fe96eabefd93c1 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Tue, 30 May 2023 23:00:56 +0200
Subject: [PATCH] Fix loss of encrypted data in plstore.el

* lisp/plstore.el (plstore--insert-buffer): Fix loss of encrypted data
when a plstore gets opened and saved without being decrypted between
these steps.  (Bug#63627)
---
 lisp/plstore.el | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/lisp/plstore.el b/lisp/plstore.el
index 7dc991aeec7..758f9fc7292 100644
--- a/lisp/plstore.el
+++ b/lisp/plstore.el
@@ -570,18 +570,23 @@ plstore-delete
 
 (defvar pp-escape-newlines)
 (defun plstore--insert-buffer (plstore)
-  "Insert the file representation of PLSTORE at point.
-Assumes that PLSTORE has been decrypted."
+  "Insert the file representation of PLSTORE at point."
   (insert ";;; public entries -*- mode: plstore -*- \n"
 	  (pp-to-string (plstore--get-alist plstore)))
-  (if (plstore--get-secret-alist plstore)
+  (let ((pp-escape-newlines nil)
+        (cipher nil))
+    (cond
+     ;; Reuse the encrypted data as cipher text if this store has not
+     ;; been decrypted yet.
+     ((plstore--get-encrypted-data plstore)
+      (setq cipher (plstore--get-encrypted-data plstore)))
+     ;; Encrypt the secret alist to generate the cipher text.
+     ((plstore--get-secret-alist plstore)
       (let ((context (epg-make-context 'OpenPGP))
-	    (pp-escape-newlines nil)
 	    (recipients
 	     (cond
 	      ((listp plstore-encrypt-to) plstore-encrypt-to)
-	      ((stringp plstore-encrypt-to) (list plstore-encrypt-to))))
-	    cipher)
+	      ((stringp plstore-encrypt-to) (list plstore-encrypt-to)))))
 	(setf (epg-context-armor context) t)
 	(epg-context-set-passphrase-callback
 	 context
@@ -601,9 +606,10 @@ plstore--insert-buffer
 If no one is selected, symmetric encryption will be performed.  "
 			   recipients)
 			(if plstore-encrypt-to
-			    (epg-list-keys context recipients)))))
-	(goto-char (point-max))
-	(insert ";;; secret entries\n" (pp-to-string cipher)))))
+			    (epg-list-keys context recipients))))))))
+    (when cipher
+      (goto-char (point-max))
+      (insert ";;; secret entries\n" (pp-to-string cipher)))))
 
 (defun plstore-save (plstore)
   "Save PLSTORE to its associated file.
-- 
2.30.2