update.
[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     (setq process
81           (apply #'start-process-shell-command "*PGP*" output-buffer
82                  program args))
83     (set-process-sentinel process 'ignore)
84     (when passphrase
85       (setenv "PGPPASSFD" "0")
86       (process-send-string process (concat passphrase "\n")))
87     (process-send-region process start end)
88     (process-send-eof process)
89     (while (eq 'run (process-status process))
90       (accept-process-output process 5))
91     (setq status (process-status process)
92           exit-status (process-exit-status process))
93     (delete-process process)
94     (with-current-buffer output-buffer
95       (goto-char (point-min))
96       (while (search-forward "\r$" nil t)
97         (replace-match ""))
98       (if (memq status '(stop signal))
99           (error "%s exited abnormally: '%s'" program exit-status))
100       (if (= 127 exit-status)
101           (error "%s could not be found" program))
102
103       (set-buffer (get-buffer-create errors-buffer))
104       (buffer-disable-undo)
105       (erase-buffer)
106       (insert-file-contents errors-file-name)
107       (delete-file errors-file-name)
108       
109       (if (and process (eq 'run (process-status process)))
110           (interrupt-process process))
111       )
112     ))
113
114 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp) 
115                                        string &optional type)
116   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
117     (pgg-pgp-process-region (point)(point) nil
118                             pgg-pgp-program args)
119     (with-current-buffer pgg-output-buffer
120       (goto-char (point-min))
121       (cond
122        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
123         (buffer-substring (point)(+ 8 (point))))
124        ((re-search-forward "^Type" nil t);PGP 6.*
125         (beginning-of-line 2)
126         (substring 
127          (nth 2 (split-string 
128                  (buffer-substring (point)
129                                    (progn (end-of-line) (point)))
130                  ))
131          2))))
132     ))
133
134 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp) 
135                                     start end recipients)
136   (let* ((pgg-pgp-user-id pgg-default-user-id)
137          (passphrase
138           (pgg-read-passphrase 
139            (format "PGP passphrase for %s: " pgg-pgp-user-id)
140            (luna-send scheme 'lookup-key-string 
141                       scheme pgg-pgp-user-id 'encrypt)))
142          (args 
143           `("+encrypttoself=off +verbose=1" "+batchmode"
144             "+language=us" "-fate"
145             ,@(if recipients
146                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
147                           recipients)))))
148     (pgg-pgp-process-region start end passphrase 
149                             pgg-pgp-program args)
150     (with-current-buffer pgg-output-buffer
151       (if (zerop (buffer-size))
152           (insert-buffer-substring pgg-errors-buffer)
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     (with-current-buffer pgg-output-buffer
174       (when (zerop (buffer-size))
175         (insert-buffer-substring pgg-errors-buffer)))
176     ))
177
178 (luna-define-method sign-region ((scheme pgg-scheme-pgp) 
179                                  start end)
180   (let* ((pgg-pgp-user-id pgg-default-user-id)
181          (passphrase
182           (pgg-read-passphrase 
183            (format "PGP passphrase for %s: " pgg-pgp-user-id)
184            (luna-send scheme 'lookup-key-string
185                       scheme pgg-pgp-user-id 'sign)))
186          (args 
187           (list "-fbast" "+verbose=1" "+language=us" "+batchmode"
188                 "-u" pgg-pgp-user-id)))
189     (pgg-pgp-process-region start end passphrase 
190                              pgg-pgp-program args)
191     (with-current-buffer pgg-output-buffer
192       (if (zerop (buffer-size))
193           (insert-buffer-substring pgg-errors-buffer)
194         (let ((packet 
195                (cdr (assq 2 (pgg-parse-armor-region 
196                              (point-min)(point-max))))))
197           (pgg-add-passphrase-cache 
198            (cdr (assq 'key-identifier packet))
199            passphrase))))
200     ))
201
202 (luna-define-method verify-region ((scheme pgg-scheme-pgp) 
203                                    start end &optional signature)
204   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
205          (orig-file (make-temp-name basename))
206          (args '("+verbose=1" "+batchmode" "+language=us")))
207     (write-region-as-binary start end orig-file)
208     (when (stringp signature)
209       (copy-file signature (setq signature (concat orig-file ".asc")))
210       (setq args (append args (list signature orig-file)))
211       )
212     (pgg-pgp-process-region (point-min)(point-max) nil
213                             pgg-pgp-program args)
214     (delete-file orig-file)
215     (delete-file signature)
216     (set-buffer pgg-output-buffer)
217     (with-current-buffer pgg-output-buffer
218       (when (zerop (buffer-size))
219         (insert-buffer-substring pgg-errors-buffer)))
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