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