X-Git-Url: https://cgit.sxemacs.org/?a=blobdiff_plain;f=lisp%2Fencrypt.el;h=59894dad16a1098d1b2c33ac9f1e073d2be7cb82;hb=0f63151c0cfcb3498678c203edb84e6a1d2b57e0;hp=8127158f1c4fb06b448a087868dfd5ba312a0df0;hpb=9567fd7201aad80eee741b7f878759b97c4904ac;p=gnus diff --git a/lisp/encrypt.el b/lisp/encrypt.el index 8127158f1..59894dad1 100644 --- a/lisp/encrypt.el +++ b/lisp/encrypt.el @@ -9,7 +9,7 @@ ;; GNU Emacs is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 2, or (at your option) +;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; GNU Emacs is distributed in the hope that it will be useful, @@ -19,8 +19,8 @@ ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, -;; Boston, MA 02111-1307, USA. +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. ;;; Commentary: @@ -28,11 +28,37 @@ ;;; grouping declarations and documentation relating to each ;;; particular aspect. +;;; Use in Gnus like this: +;;; (require 'encrypt) +;;; (setq +;;; nnimap-authinfo-file "~/.authinfo.enc" +;;; nntp-authinfo-file "~/.authinfo.enc" +;;; smtpmail-auth-credentials "~/.authinfo.enc" +;;; ;; GnuPG using the AES256 cipher, feel free to use your own favorite +;;; encrypt-file-alist (quote (("~/.authinfo.enc" (gpg "AES256")))) +;;; password-cache-expiry 600) + +;;; Then write ~/.authinfo.enc: + +;;; 1) open the old authinfo +;;; C-x C-f ~/.authinfo + +;;; 2) write the new authinfo.enc +;;; M-x encrypt-write-file-contents RET ~/.authinfo.enc + +;;; 3) verify the new authinfo is correct +;;; (this will insert the contents in the current buffer) + +;;; M-: (encrypt-insert-file-contents "~/.authinfo.enc") + + ;;; Code: ;; autoload password (eval-and-compile - (autoload 'password-read "password")) + (if (locate-library "password-cache") + (require 'password-cache) + (require 'password))) (defgroup encrypt '((password-cache custom-variable) (password-cache-expiry custom-variable)) @@ -44,6 +70,8 @@ Format example: '((\"beta\" (gpg \"AES\")) + (\"gamma\\\\*\" + (pgg)) (\"/home/tzz/alpha\" (encrypt-xor \"Semi-Secret\")))" @@ -53,6 +81,9 @@ Format example: (file :tag "Filename") (regexp :tag "Regular expression match")) (radio :tag "How to encrypt it" + (list + :tag "GPG Encryption via PGG (including passphrases)" + (const :tag "GPG via PGG" pgg)) (list :tag "GPG Encryption" (const :tag "GPG Program" gpg) @@ -98,43 +129,43 @@ Format example: (return model))))) ;;;###autoload -(defun encrypt-insert-file-contents (file &optional model) +(defun encrypt-insert-file-contents (file &optional model erase) "Decrypt FILE into the current buffer." (interactive "fFile to insert: ") (let* ((model (or model (encrypt-find-model file))) (method (nth 0 model)) (cipher (nth 1 model)) - (password-key (format "encrypt-password-%s-%s %s" - (symbol-name method) cipher file)) - (passphrase - (password-read-and-add - (format "%s password for cipher %s? " - (symbol-name method) cipher) - password-key)) - (buffer-file-coding-system 'binary) + (passphrase (encrypt-get-passphrase-if-needed file method cipher t)) + (buffer-file-coding-system 'binary) (coding-system-for-read 'binary) outdata) ;; note we only insert-file-contents if the method is known to be valid - (cond - ((eq method 'gpg) - (insert-file-contents file) - (setq outdata (encrypt-gpg-decode-buffer passphrase cipher))) - ((eq method 'encrypt-xor) - (insert-file-contents file) - (setq outdata (encrypt-xor-decode-buffer passphrase cipher)))) + (with-temp-buffer + (cond + ((eq method 'gpg) + (insert-file-contents file) + (setq outdata (encrypt-gpg-decode-buffer passphrase cipher))) + ((eq method 'pgg) + (insert-file-contents file) + (setq outdata (encrypt-pgg-decode-buffer))) + ((eq method 'encrypt-xor) + (insert-file-contents file) + (setq outdata (encrypt-xor-decode-buffer passphrase cipher))))) (if outdata (progn - (gnus-message 9 "%s was decrypted with %s (cipher %s)" - file (symbol-name method) cipher) - (delete-region (point-min) (point-max)) - (goto-char (point-min)) + (message "%s was decrypted with %s" + file + (encrypt-message-method-and-cipher method cipher)) + (when erase + (delete-region (point-min) (point-max))) (insert outdata)) ;; the decryption failed, alas - (password-cache-remove password-key) - (gnus-error 5 "%s was NOT decrypted with %s (cipher %s)" - file (symbol-name method) cipher)))) + (password-cache-remove (encrypt-password-key file method cipher)) + (gnus-error 5 "%s was NOT decrypted with %s" + file + (encrypt-message-method-and-cipher method cipher))))) (defun encrypt-get-file-contents (file &optional model) "Decrypt FILE and return the contents." @@ -151,38 +182,61 @@ Format example: (defun encrypt-write-file-contents (file &optional model) "Encrypt the current buffer to FILE, then continue normally." - (interactive "fFile to write: ") - (let* ((model (or model (encrypt-find-model file))) - (method (nth 0 model)) - (cipher (nth 1 model)) - (password-key (format "encrypt-password-%s-%s %s" - (symbol-name method) cipher file)) - (passphrase - (password-read - (format "%s password for cipher %s? " - (symbol-name method) cipher) - password-key)) - outdata) - - (cond - ((eq method 'gpg) - (setq outdata (encrypt-gpg-encode-buffer passphrase cipher))) - ((eq method 'encrypt-xor) - (setq outdata (encrypt-xor-encode-buffer passphrase cipher)))) - - (if outdata - (progn - (gnus-message 9 "%s was encrypted with %s (cipher %s)" - file (symbol-name method) cipher) - (delete-region (point-min) (point-max)) - (goto-char (point-min)) - (insert outdata) - ;; do not confirm overwrites - (write-file file nil)) - ;; the decryption failed, alas - (password-cache-remove password-key) - (gnus-error 5 "%s was NOT encrypted with %s (cipher %s)" - file (symbol-name method) cipher)))) + (interactive "sFile to write: ") + (setq model (or model (encrypt-find-model file))) + (if model + (let* ((method (nth 0 model)) + (cipher (nth 1 model)) + (passphrase + (encrypt-get-passphrase-if-needed file method cipher)) + (outdata + (cond + ((eq method 'gpg) + (encrypt-gpg-encode-buffer passphrase cipher)) + ((eq method 'pgg) + (encrypt-pgg-encode-buffer)) + ((eq method 'encrypt-xor) + (encrypt-xor-encode-buffer passphrase cipher))))) + + (if outdata + (progn + (message "%s was encrypted with %s" + file + (encrypt-message-method-and-cipher method cipher)) + (with-temp-buffer + (insert outdata) + ;; do not confirm overwrites + (write-file file nil))) + ;; the decryption failed, alas + (password-cache-remove (encrypt-password-key file method cipher)) + (gnus-error 5 "%s was NOT encrypted with %s" + file + (encrypt-message-method-and-cipher method cipher)))) + (gnus-error + 1 + "%s has no associated encryption model! See encrypt-file-alist." + file))) + +(defun encrypt-password-key (file method cipher) + (format "encrypt-password-%s-%s %s" (symbol-name method) cipher file)) + +(defun encrypt-get-passphrase-if-needed (file method cipher &optional add) + "Read the passphrase for FILE, METHOD, CIPHER if necessary." + (when (not (eq method 'pgg)) + (let ((password-key (encrypt-password-key file method cipher)) + (password-question + (format "password for %s (file %s)? " + (encrypt-message-method-and-cipher method cipher) + file))) + (if add + (password-read-and-add password-question password-key) + (password-read password-question password-key))))) + + +(defun encrypt-message-method-and-cipher (method cipher) + (format "method %s%s" + (symbol-name method) + (if cipher (format " (cipher %s)" cipher) ""))) (defun encrypt-xor-encode-buffer (passphrase cipher) (encrypt-xor-process-buffer passphrase cipher t)) @@ -213,7 +267,7 @@ Format example: (progn (setq new-list (reverse (split-string bs))) (dolist (x new-list) - (setq x (string-to-int x)) + (setq x (string-to-number x)) (insert (format "%c" (logxor x passphrase-sum)))))) (buffer-substring-no-properties (point-min) (point-max))))) @@ -223,14 +277,14 @@ Format example: (defun encrypt-gpg-decode-buffer (passphrase cipher) (encrypt-gpg-process-buffer passphrase cipher nil)) -(defun encrypt-gpg-process-buffer (passphrase - cipher +(defun encrypt-gpg-process-buffer (passphrase + cipher &optional encode) "With PASSPHRASE, use GPG to encode or decode the current buffer." (let* ((program encrypt-gpg-path) (input (buffer-substring-no-properties (point-min) (point-max))) - (temp-maker (if (fboundp 'make-temp-file) - 'make-temp-file + (temp-maker (if (fboundp 'make-temp-file) + 'make-temp-file 'make-temp-name)) (temp-file (funcall temp-maker encrypt-temp-prefix)) (default-enable-multibyte-characters nil) @@ -240,7 +294,7 @@ Format example: "--passphrase-fd" "0" "--no-tty")) exit-status exit-data) - + (when encode (setq args (append args @@ -267,7 +321,27 @@ Format example: (gnus-error 5 "GPG is not installed.")) exit-data)) +(defun encrypt-pgg-encode-buffer () + (encrypt-pgg-process-buffer t)) + +(defun encrypt-pgg-decode-buffer () + (encrypt-pgg-process-buffer)) + +(defun encrypt-pgg-process-buffer (&optional encode) + "Use PGG to encode or decode the current buffer." + (let ((pfft (if encode 'pgg-encrypt-symmetric 'pgg-decrypt)) + (default-enable-multibyte-characters nil) + (input (buffer-substring-no-properties (point-min) (point-max))) + exit-data) + (with-temp-buffer + (insert input) + ;; note that we call pfft before pgg-display-output-buffer + (pgg-display-output-buffer (point-min) (point-max) (funcall pfft)) + (setq exit-data + (buffer-substring-no-properties (point-min) (point-max)))) + (debug exit-data) + exit-data)) + (provide 'encrypt) -;;; encrypt.el ends here -;; arch-tag: d907e4f1-71b5-42b1-a180-fc7b84ff0648 +;;; encrypt.el ends here