* pgg-pgp.el (lookup-key-string): Use `call-process' instead of
[elisp/semi.git] / pgg-pgp.el
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* 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-pgp ()
31   "PGP 2.* and 6.* interface"
32   :group 'pgg)
33
34 (defcustom pgg-pgp-program "pgp" 
35   "PGP 2.* and 6.* executable."
36   :group 'pgg-pgp
37   :type 'string)
38
39 (defcustom pgg-pgp-shell-file-name "/bin/sh"
40   "The GnuPG executable."
41   :group 'pgg-pgp
42   :type 'string)
43
44 (defcustom pgg-pgp-extra-args nil
45   "Extra arguments for every PGP invocation."
46   :group 'pgg-pgp
47   :type 'string)
48
49 (eval-and-compile
50   (luna-define-class pgg-scheme-pgp (pgg-scheme))
51   )
52   
53 (defvar pgg-pgp-user-id nil
54   "GnuPG ID of your default identity.")
55
56 (defvar pgg-scheme-pgp-instance nil)
57
58 ;;;###autoload
59 (defun pgg-make-scheme-pgp ()
60   (or pgg-scheme-pgp-instance
61       (setq pgg-scheme-pgp-instance
62             (luna-make-entity 'pgg-scheme-pgp))))
63
64 (defun pgg-pgp-process-region (start end passphrase program args)
65   (let* ((errors-file-name
66           (concat temporary-file-directory 
67                   (make-temp-name "pgg-errors")))
68          (args 
69           (append args 
70                   pgg-pgp-extra-args
71                   (list (concat "2>" errors-file-name))))
72          (shell-file-name pgg-pgp-shell-file-name)
73          (output-buffer pgg-output-buffer)
74          (errors-buffer pgg-errors-buffer)
75          (process-connection-type nil)
76          process status exit-status)
77     (with-current-buffer (get-buffer-create output-buffer)
78       (buffer-disable-undo)
79       (erase-buffer))
80     (when passphrase
81       (setenv "PGPPASSFD" "0"))
82     (setq process
83           (apply #'start-process-shell-command "*PGP*" output-buffer
84                  program args))
85     (set-process-sentinel process 'ignore)
86     (when passphrase
87       (process-send-string process (concat passphrase "\n")))
88     (process-send-region process start end)
89     (process-send-eof process)
90     (while (eq 'run (process-status process))
91       (accept-process-output process 5))
92     (setq status (process-status process)
93           exit-status (process-exit-status process))
94     (delete-process process)
95     (with-current-buffer output-buffer
96       (goto-char (point-min))
97       (while (search-forward "\r$" nil t)
98         (replace-match ""))
99       (if (memq status '(stop signal))
100           (error "%s exited abnormally: '%s'" program exit-status))
101       (if (= 127 exit-status)
102           (error "%s could not be found" program))
103
104       (set-buffer (get-buffer-create errors-buffer))
105       (buffer-disable-undo)
106       (erase-buffer)
107       (insert-file-contents errors-file-name)
108       (delete-file errors-file-name)
109       
110       (if (and process (eq 'run (process-status process)))
111           (interrupt-process process))
112       )
113     ))
114
115 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp) 
116                                        string &optional type)
117   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
118     (with-current-buffer (get-buffer-create pgg-output-buffer)
119       (buffer-disable-undo)
120       (erase-buffer)
121       (apply #'call-process pgg-pgp-program nil t args)
122       (goto-char (point-min))
123       (cond
124        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
125         (buffer-substring (point)(+ 8 (point))))
126        ((re-search-forward "^Type" nil t);PGP 6.*
127         (beginning-of-line 2)
128         (substring 
129          (nth 2 (split-string 
130                  (buffer-substring (point)
131                                    (progn (end-of-line) (point)))
132                  ))
133          2))))
134     ))
135
136 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp) 
137                                     start end recipients)
138   (let* ((pgg-pgp-user-id pgg-default-user-id)
139          (passphrase
140           (pgg-read-passphrase 
141            (format "PGP passphrase for %s: " pgg-pgp-user-id)
142            (luna-send scheme 'lookup-key-string 
143                       scheme pgg-pgp-user-id 'encrypt)))
144          (args 
145           `("+encrypttoself=off +verbose=1" "+batchmode"
146             "+language=us" "-fate"
147             ,@(if recipients
148                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
149                           recipients)))))
150     (pgg-pgp-process-region start end passphrase 
151                             pgg-pgp-program args)
152     (pgg-process-when-success
153       (let ((packet 
154              (cdr (assq 1 (pgg-parse-armor-region 
155                            (point-min)(point-max))))))
156         (pgg-add-passphrase-cache 
157          (cdr (assq 'key-identifier packet))
158          passphrase)))
159     ))
160
161 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp) 
162                                     start end)
163   (let* ((pgg-pgp-user-id pgg-default-user-id)
164          (passphrase
165           (pgg-read-passphrase 
166            (format "PGP passphrase for %s: " pgg-pgp-user-id)
167            (luna-send scheme 'lookup-key-string 
168                       scheme pgg-pgp-user-id 'encrypt)))
169          (args 
170           '("+verbose=1" "+batchmode" "+language=us" "-f")))
171     (pgg-pgp-process-region start end passphrase 
172                             pgg-pgp-program args)
173     (pgg-process-when-success nil)
174     ))
175
176 (luna-define-method sign-region ((scheme pgg-scheme-pgp) 
177                                  start end &optional clearsign)
178   (let* ((pgg-pgp-user-id pgg-default-user-id)
179          (passphrase
180           (pgg-read-passphrase 
181            (format "PGP passphrase for %s: " pgg-pgp-user-id)
182            (luna-send scheme 'lookup-key-string
183                       scheme pgg-pgp-user-id 'sign)))
184          (args 
185           (list (if clearsign "-fast" "-fbast")
186                 "+verbose=1" "+language=us" "+batchmode"
187                 "-u" pgg-pgp-user-id)))
188     (pgg-pgp-process-region start end passphrase 
189                              pgg-pgp-program args)
190     (pgg-process-when-success
191       (goto-char (point-min))
192       (while (re-search-forward "\r$" end t)
193         (replace-match ""))
194       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
195         (let ((packet 
196                (cdr (assq 2 (pgg-parse-armor-region 
197                              (progn (beginning-of-line 2)
198                                     (point))
199                              (point-max))))))
200           (pgg-add-passphrase-cache 
201            (cdr (assq 'key-identifier packet))
202            passphrase))))
203     ))
204
205 (luna-define-method verify-region ((scheme pgg-scheme-pgp) 
206                                    start end &optional signature)
207   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
208          (orig-file (make-temp-name basename))
209          (args '("+verbose=1" "+batchmode" "+language=us")))
210     (write-region-as-binary start end orig-file)
211     (when (stringp signature)
212       (copy-file signature (setq signature (concat orig-file ".asc")))
213       (setq args (append args (list signature orig-file)))
214       )
215     (pgg-pgp-process-region (point-min)(point-max) nil
216                             pgg-pgp-program args)
217     (delete-file orig-file)
218     (if signature (delete-file signature))
219     (pgg-process-when-success nil)
220     ))
221
222 (luna-define-method insert-key ((scheme pgg-scheme-pgp))
223   (let* ((pgg-pgp-user-id pgg-default-user-id)
224          (args
225           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf" 
226                 (concat "\"" pgg-pgp-user-id "\""))))
227     (pgg-pgp-process-region (point)(point) nil
228                              pgg-pgp-program args)
229     (insert-buffer-substring pgg-output-buffer)
230     ))
231
232 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp)
233                                        start end)
234   (let* ((pgg-pgp-user-id pgg-default-user-id)
235          (basename (expand-file-name "pgg" temporary-file-directory))
236          (key-file (make-temp-name basename))
237          (args 
238           (list "+verbose=1" "+batchmode" "+language=us" "-kaf" 
239                 key-file)))
240     (write-region-as-raw-text-CRLF start end key-file)
241     (pgg-pgp-process-region start end nil
242                             pgg-pgp-program args)
243     (delete-file key-file)
244     ))
245
246 (provide 'pgg-pgp)
247
248 ;;; pgg-pgp.el ends here