2009-11-25 Kevin Ryde <user42@zip.com.au>
[gnus] / lisp / pgg-pgp5.el
1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
2
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Created: 1999/11/02
8 ;; Keywords: PGP, OpenPGP
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Code:
26
27 (eval-when-compile
28   (require 'cl)                         ; for pgg macros
29   (require 'pgg))
30
31 (defgroup pgg-pgp5 ()
32   "PGP 5.* interface."
33   :group 'pgg)
34
35 (defcustom pgg-pgp5-pgpe-program "pgpe"
36   "PGP 5.* 'pgpe' executable."
37   :group 'pgg-pgp5
38   :type 'string)
39
40 (defcustom pgg-pgp5-pgps-program "pgps"
41   "PGP 5.* 'pgps' executable."
42   :group 'pgg-pgp5
43   :type 'string)
44
45 (defcustom pgg-pgp5-pgpk-program "pgpk"
46   "PGP 5.* 'pgpk' executable."
47   :group 'pgg-pgp5
48   :type 'string)
49
50 (defcustom pgg-pgp5-pgpv-program "pgpv"
51   "PGP 5.* 'pgpv' executable."
52   :group 'pgg-pgp5
53   :type 'string)
54
55 (defcustom pgg-pgp5-shell-file-name "/bin/sh"
56   "File name to load inferior shells from.
57 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
58   :group 'pgg-pgp5
59   :type 'string)
60
61 (defcustom pgg-pgp5-shell-command-switch "-c"
62   "Switch used to have the shell execute its command line argument."
63   :group 'pgg-pgp5
64   :type 'string)
65
66 (defcustom pgg-pgp5-extra-args nil
67   "Extra arguments for every PGP 5.* invocation."
68   :group 'pgg-pgp5
69   :type '(choice
70           (const :tag "None" nil)
71           (string :tag "Arguments")))
72
73 (defvar pgg-pgp5-user-id nil
74   "PGP 5.* ID of your default identity.")
75
76 (defun pgg-pgp5-process-region (start end passphrase program args)
77   (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
78          (args
79           (append args
80                   pgg-pgp5-extra-args
81                   (list (concat "2>" errors-file-name))))
82          (shell-file-name pgg-pgp5-shell-file-name)
83          (shell-command-switch pgg-pgp5-shell-command-switch)
84          (process-environment process-environment)
85          (output-buffer pgg-output-buffer)
86          (errors-buffer pgg-errors-buffer)
87          (process-connection-type nil)
88          process status exit-status)
89     (with-current-buffer (get-buffer-create output-buffer)
90       (buffer-disable-undo)
91       (erase-buffer))
92     (when passphrase
93       (setenv "PGPPASSFD" "0"))
94     (unwind-protect
95         (progn
96           (let ((coding-system-for-read 'binary)
97                 (coding-system-for-write 'binary))
98             (setq process
99                   (apply #'funcall
100                          #'start-process-shell-command "*PGP*" output-buffer
101                          program args)))
102           (set-process-sentinel process #'ignore)
103           (when passphrase
104             (process-send-string process (concat passphrase "\n")))
105           (process-send-region process start end)
106           (process-send-eof process)
107           (while (eq 'run (process-status process))
108             (accept-process-output process 5))
109           (setq status (process-status process)
110                 exit-status (process-exit-status process))
111           (delete-process process)
112           (with-current-buffer output-buffer
113             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
114
115             (if (memq status '(stop signal))
116                 (error "%s exited abnormally: '%s'" program exit-status))
117             (if (= 127 exit-status)
118                 (error "%s could not be found" program))
119
120             (set-buffer (get-buffer-create errors-buffer))
121             (buffer-disable-undo)
122             (erase-buffer)
123             (insert-file-contents errors-file-name)))
124       (if (and process (eq 'run (process-status process)))
125           (interrupt-process process))
126       (condition-case nil
127           (delete-file errors-file-name)
128         (file-error nil)))))
129
130 (defun pgg-pgp5-lookup-key (string &optional type)
131   "Search keys associated with STRING."
132   (let ((args (list "+language=en" "-l" string)))
133     (with-current-buffer (get-buffer-create pgg-output-buffer)
134       (buffer-disable-undo)
135       (erase-buffer)
136       (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
137       (goto-char (point-min))
138       (when (re-search-forward "^sec" nil t)
139         (substring
140          (nth 2 (split-string
141                  (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
142          2)))))
143
144 (defun pgg-pgp5-encrypt-region (start end recipients &optional sign passphrase)
145   "Encrypt the current region between START and END."
146   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
147          (passphrase (or passphrase
148                          (when sign
149                            (pgg-read-passphrase
150                             (format "PGP passphrase for %s: "
151                                     pgg-pgp5-user-id)
152                             pgg-pgp5-user-id))))
153          (args
154           (append
155            `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
156              ,@(if (or recipients pgg-encrypt-for-me)
157                    (apply #'append
158                           (mapcar (lambda (rcpt)
159                                     (list "-r"
160                                           (concat "\"" rcpt "\"")))
161                                   (append recipients
162                                           (if pgg-encrypt-for-me
163                                               (list pgg-pgp5-user-id)))))))
164            (if sign '("-s" "-u" pgg-pgp5-user-id)))))
165     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
166     (pgg-process-when-success nil)))
167
168 (defun pgg-pgp5-decrypt-region (start end &optional passphrase)
169   "Decrypt the current region between START and END."
170   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
171          (passphrase
172           (or passphrase
173               (pgg-read-passphrase
174                (format "PGP passphrase for %s: " pgg-pgp5-user-id)
175                (pgg-pgp5-lookup-key pgg-pgp5-user-id 'encrypt))))
176          (args
177           '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
178     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
179     (pgg-process-when-success nil)))
180
181 (defun pgg-pgp5-sign-region (start end &optional clearsign passphrase)
182   "Make detached signature from text between START and END."
183   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
184          (passphrase
185           (or passphrase
186               (pgg-read-passphrase
187                (format "PGP passphrase for %s: " pgg-pgp5-user-id)
188                (pgg-pgp5-lookup-key pgg-pgp5-user-id 'sign))))
189          (args
190           (list (if clearsign "-fat" "-fbat")
191                 "+verbose=1" "+language=us" "+batchmode=1"
192                 "-u" pgg-pgp5-user-id)))
193     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
194     (pgg-process-when-success
195       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
196         (let ((packet
197                (cdr (assq 2 (pgg-parse-armor-region
198                              (progn (beginning-of-line 2)
199                                     (point))
200                              (point-max))))))
201           (if pgg-cache-passphrase
202               (pgg-add-passphrase-to-cache
203                (cdr (assq 'key-identifier packet))
204                passphrase)))))))
205
206 (defun pgg-pgp5-verify-region (start end &optional signature)
207   "Verify region between START and END as the detached signature SIGNATURE."
208   (let ((orig-file (pgg-make-temp-file "pgg"))
209         (args '("+verbose=1" "+batchmode=1" "+language=us"))
210         (orig-mode (default-file-modes)))
211     (unwind-protect
212         (progn
213           (set-default-file-modes 448)
214           (let ((coding-system-for-write 'binary)
215                 jka-compr-compression-info-list jam-zcat-filename-list)
216             (write-region start end orig-file)))
217       (set-default-file-modes orig-mode))
218     (when (stringp signature)
219       (copy-file signature (setq signature (concat orig-file ".asc")))
220       (setq args (append args (list signature))))
221     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
222     (delete-file orig-file)
223     (if signature (delete-file signature))
224     (with-current-buffer pgg-errors-buffer
225       (goto-char (point-min))
226       (if (re-search-forward "^Good signature" nil t)
227           (progn
228             (set-buffer pgg-output-buffer)
229             (insert-buffer-substring pgg-errors-buffer)
230             t)
231         nil))))
232
233 (defun pgg-pgp5-insert-key ()
234   "Insert public key at point."
235   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
236          (args
237           (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
238                 (concat "\"" pgg-pgp5-user-id "\""))))
239     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
240     (insert-buffer-substring pgg-output-buffer)))
241
242 (defun pgg-pgp5-snarf-keys-region (start end)
243   "Add all public keys in region between START and END to the keyring."
244   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
245          (key-file (pgg-make-temp-file "pgg"))
246          (args
247           (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
248                 key-file)))
249     (let ((coding-system-for-write 'raw-text-dos))
250       (write-region start end key-file))
251     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
252     (delete-file key-file)
253     (pgg-process-when-success nil)))
254
255 (provide 'pgg-pgp5)
256
257 ;; arch-tag: 3dbd1073-6b3a-466c-9f55-5c587ffa6d7b
258 ;;; pgg-pgp5.el ends here