* mime-pgp.el (mime-verify-application/pgp-signature): Copy the
[elisp/semi.git] / pgg-pgp5.el
1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
2
3 ;; Copyright (C) 1999 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6 ;; Created: 1999/11/02
7 ;; Keywords: PGP, OpenPGP
8
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Code:
27
28 (eval-when-compile (require 'pgg))
29
30 (defgroup pgg-pgp5 ()
31   "PGP 5.* interface"
32   :group 'pgg)
33
34 (defcustom pgg-pgp5-pgpe-program "pgpe" 
35   "PGP 5.* 'pgpe' executable."
36   :group 'pgg-pgp5
37   :type 'string)
38
39 (defcustom pgg-pgp5-pgps-program "pgps" 
40   "PGP 5.* 'pgps' executable."
41   :group 'pgg-pgp5
42   :type 'string)
43
44 (defcustom pgg-pgp5-pgpk-program "pgpk" 
45   "PGP 5.* 'pgpk' executable."
46   :group 'pgg-pgp5
47   :type 'string)
48
49 (defcustom pgg-pgp5-pgpv-program "pgpv" 
50   "PGP 5.* 'pgpv' executable."
51   :group 'pgg-pgp5
52   :type 'string)
53
54 (defcustom pgg-pgp5-shell-file-name "/bin/sh"
55   "File name to load inferior shells from.  Bourne shell or its equivalent
56 \(not tcsh) is needed for \"2>\"."
57   :group 'pgg-pgp5
58   :type 'string)
59
60 (defcustom pgg-pgp5-shell-command-switch "-c"
61   "Switch used to have the shell execute its command line argument."
62   :group 'pgg-pgp5
63   :type 'string)
64
65 (defcustom pgg-pgp5-extra-args nil
66   "Extra arguments for every PGP invocation."
67   :group 'pgg-pgp5
68   :type 'string)
69
70 (eval-and-compile
71   (luna-define-class pgg-scheme-pgp5 (pgg-scheme))
72   )
73   
74 (defvar pgg-pgp5-user-id nil
75   "GnuPG ID of your default identity.")
76
77 (defvar pgg-scheme-pgp5-instance nil)
78
79 ;;;###autoload
80 (defun pgg-make-scheme-pgp5 ()
81   (or pgg-scheme-pgp5-instance
82       (setq pgg-scheme-pgp5-instance
83             (luna-make-entity 'pgg-scheme-pgp5))))
84
85 (defun pgg-pgp5-process-region (start end passphrase program args)
86   (let* ((errors-file-name
87           (concat temporary-file-directory 
88                   (make-temp-name "pgg-errors")))
89          (args 
90           (append args 
91                   pgg-pgp5-extra-args
92                   (list (concat "2>" errors-file-name))))
93          (shell-file-name pgg-pgp5-shell-file-name)
94          (shell-command-switch pgg-pgp5-shell-command-switch)
95          (output-buffer pgg-output-buffer)
96          (errors-buffer pgg-errors-buffer)
97          (process-connection-type nil)
98          process status exit-status)
99     (with-current-buffer (get-buffer-create output-buffer)
100       (buffer-disable-undo)
101       (erase-buffer))
102     (when passphrase
103       (setenv "PGPPASSFD" "0"))
104     (setq process
105           (apply #'start-process-shell-command "*PGP*" output-buffer
106                  program args))
107     (set-process-sentinel process 'ignore)
108     (when passphrase
109       (process-send-string process (concat passphrase "\n")))
110     (process-send-region process start end)
111     (process-send-eof process)
112     (while (eq 'run (process-status process))
113       (accept-process-output process 5))
114     (setq status (process-status process)
115           exit-status (process-exit-status process))
116     (delete-process process)
117     (with-current-buffer output-buffer
118       (goto-char (point-min))
119       (while (search-forward "\r$" nil t)
120         (replace-match ""))
121       (if (memq status '(stop signal))
122           (error "%s exited abnormally: '%s'" program exit-status))
123       (if (= 127 exit-status)
124           (error "%s could not be found" program))
125
126       (set-buffer (get-buffer-create errors-buffer))
127       (buffer-disable-undo)
128       (erase-buffer)
129       (insert-file-contents errors-file-name)
130       (delete-file errors-file-name)
131       
132       (if (and process (eq 'run (process-status process)))
133           (interrupt-process process))
134       )
135     ))
136
137 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp5) 
138                                        string &optional type)
139   (let ((args (list "+language=en" "-l" string)))
140     (with-current-buffer (get-buffer-create pgg-output-buffer)
141       (buffer-disable-undo)
142       (erase-buffer)
143       (apply #'call-process pgg-pgp5-pgpk-program  nil t args)
144       (goto-char (point-min))
145       (when (re-search-forward "^sec" nil t)
146         (substring 
147          (nth 2 (split-string 
148                  (buffer-substring (match-end 0)
149                                    (progn (end-of-line)(point)))
150                  ))
151          2)))
152     ))
153
154 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp5) 
155                                     start end recipients)
156   (let* ((pgg-pgp5-user-id pgg-default-user-id)
157          (passphrase
158           (pgg-read-passphrase 
159            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
160            (luna-send scheme 'lookup-key-string 
161                       scheme pgg-pgp5-user-id 'encrypt)))
162          (args 
163           `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
164             ,@(if recipients
165                   (apply #'append 
166                          (mapcar (lambda (rcpt) 
167                                    (list "-r" 
168                                          (concat "\"" rcpt "\""))) 
169                                  recipients))))))
170     (pgg-pgp5-process-region start end passphrase 
171                              pgg-pgp5-pgpe-program args)
172     (with-current-buffer pgg-output-buffer
173       (if (zerop (buffer-size))
174           (insert-buffer-substring pgg-errors-buffer)
175         (let ((packet 
176                (cdr (assq 1 (pgg-parse-armor-region 
177                              (point-min)(point-max))))))
178           (pgg-add-passphrase-cache 
179            (cdr (assq 'key-identifier packet))
180            passphrase))))
181     (pgg-process-when-success
182       (let ((packet 
183              (cdr (assq 1 (pgg-parse-armor-region 
184                            (point-min)(point-max))))))
185         (pgg-add-passphrase-cache 
186          (cdr (assq 'key-identifier packet))
187          passphrase)))
188     ))
189
190 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp5) 
191                                     start end)
192   (let* ((pgg-pgp5-user-id pgg-default-user-id)
193          (passphrase
194           (pgg-read-passphrase 
195            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
196            (luna-send scheme 'lookup-key-string 
197                       scheme pgg-pgp5-user-id 'encrypt)))
198          (args 
199           '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
200     (pgg-pgp5-process-region start end passphrase 
201                              pgg-pgp5-pgpv-program args)
202     (pgg-process-when-success nil)
203     ))
204
205 (luna-define-method sign-region ((scheme pgg-scheme-pgp5) 
206                                  start end &optional clearsign)
207   (let* ((pgg-pgp5-user-id pgg-default-user-id)
208          (passphrase
209           (pgg-read-passphrase 
210            (format "PGP passphrase for %s: " pgg-pgp5-user-id)
211            (luna-send scheme 'lookup-key-string 
212                       scheme pgg-pgp5-user-id 'sign)))
213          (args 
214           (list (if clearsign "-fat" "-fbat")
215                 "+verbose=1" "+language=us" "+batchmode=1"
216                 "-u" pgg-pgp5-user-id)))
217     (pgg-pgp5-process-region start end passphrase 
218                              pgg-pgp5-pgps-program args)
219     (pgg-process-when-success
220       (goto-char (point-min))
221       (while (re-search-forward "\r$" nil t)
222         (replace-match ""))
223       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
224         (let ((packet 
225                (cdr (assq 2 (pgg-parse-armor-region 
226                              (progn (beginning-of-line 2)
227                                     (point))
228                              (point-max))))))
229           (pgg-add-passphrase-cache 
230            (cdr (assq 'key-identifier packet))
231            passphrase))))
232     ))
233
234 (luna-define-method verify-region ((scheme pgg-scheme-pgp5) 
235                                    start end &optional signature)
236   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
237          (orig-file (make-temp-name basename))
238          (args '("+verbose=1" "+batchmode=1" "+language=us")))
239     (write-region-as-binary start end orig-file)
240     (when (stringp signature)
241       (copy-file signature (setq signature (concat orig-file ".asc")))
242       (setq args (append args (list signature)))
243       )
244     (pgg-pgp5-process-region (point-min)(point-max) nil
245                              pgg-pgp5-pgpv-program args)
246     (delete-file orig-file)
247     (if signature (delete-file signature))
248     (pgg-process-when-success nil)
249     ))
250
251 (luna-define-method insert-key ((scheme pgg-scheme-pgp5))
252   (let* ((pgg-pgp5-user-id pgg-default-user-id)
253          (args
254           (list "+verbose=1" "+batchmode=1" "+language=us" "-x" 
255                 (concat "\"" pgg-pgp5-user-id "\""))))
256     (pgg-pgp5-process-region (point)(point) nil
257                              pgg-pgp5-pgpk-program args)
258     (insert-buffer-substring pgg-output-buffer)
259     ))
260
261 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp5)
262                                        start end)
263   (let* ((pgg-pgp5-user-id pgg-default-user-id)
264          (basename (expand-file-name "pgg" temporary-file-directory))
265          (key-file (make-temp-name basename))
266          (args 
267           (list "+verbose=1" "+batchmode=1" "+language=us" "-a" 
268                 key-file)))
269     (write-region-as-raw-text-CRLF start end key-file)
270     (pgg-pgp5-process-region start end nil
271                              pgg-pgp5-pgpk-program args)
272     (delete-file key-file)
273     (pgg-process-when-success nil)
274     ))
275
276 (provide 'pgg-pgp5)
277
278 ;;; pgg-pgp5.el ends here