Sync with semi21-1_14_0-pre4-2.
[elisp/semi.git] / pgg-pgp.el
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
2
3 ;; Copyright (C) 1999,2000 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.
41 Bourne shell or its equivalent \(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 (defvar pgg-pgp-user-id nil
59   "PGP ID of your default identity.")
60
61 (defvar pgg-scheme-pgp-instance nil)
62
63 ;;;###autoload
64 (defun pgg-make-scheme-pgp ()
65   (or pgg-scheme-pgp-instance
66       (setq pgg-scheme-pgp-instance
67             (luna-make-entity 'pgg-scheme-pgp))))
68
69 (defun pgg-pgp-process-region (start end passphrase program args)
70   (let* ((errors-file-name
71           (concat temporary-file-directory
72                   (make-temp-name "pgg-errors")))
73          (args
74           (append args
75                   pgg-pgp-extra-args
76                   (list (concat "2>" errors-file-name))))
77          (shell-file-name pgg-pgp-shell-file-name)
78          (shell-command-switch pgg-pgp-shell-command-switch)
79          (process-environment process-environment)
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     (unwind-protect
90         (progn
91           (setq process
92                 (apply #'binary-start-process-shell-command "*PGP*"
93                        output-buffer
94                        program args))
95           (set-process-sentinel process #'ignore)
96           (when passphrase
97             (process-send-string process (concat passphrase "\n")))
98           (process-send-region process start end)
99           (process-send-eof process)
100           (while (eq 'run (process-status process))
101             (accept-process-output process 5))
102           (setq status (process-status process)
103                 exit-status (process-exit-status process))
104           (delete-process process)
105           (with-current-buffer output-buffer
106             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
107
108             (if (memq status '(stop signal))
109                 (error "%s exited abnormally: '%s'" program exit-status))
110             (if (= 127 exit-status)
111                 (error "%s could not be found" program))
112
113             (set-buffer (get-buffer-create errors-buffer))
114             (buffer-disable-undo)
115             (erase-buffer)
116             (insert-file-contents errors-file-name)))
117       (if (and process (eq 'run (process-status process)))
118           (interrupt-process process))
119       (condition-case nil
120           (delete-file errors-file-name)
121         (file-error nil)))))
122
123 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-pgp)
124                                                   string &optional type)
125   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
126     (with-current-buffer (get-buffer-create pgg-output-buffer)
127       (buffer-disable-undo)
128       (erase-buffer)
129       (apply #'call-process pgg-pgp-program nil t nil args)
130       (goto-char (point-min))
131       (cond
132        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
133         (buffer-substring (point)(+ 8 (point))))
134        ((re-search-forward "^Type" nil t);PGP 6.*
135         (beginning-of-line 2)
136         (substring
137          (nth 2 (split-string
138                  (buffer-substring (point)(progn (end-of-line) (point)))))
139          2))))))
140
141 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp)
142                                                start end recipients)
143   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
144          (args
145           `("+encrypttoself=off +verbose=1" "+batchmode"
146             "+language=us" "-fate"
147             ,@(if recipients
148                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
149                           (append recipients
150                                   (if pgg-encrypt-for-me
151                                       (list pgg-pgp-user-id))))))))
152     (pgg-pgp-process-region start end nil pgg-pgp-program args)
153     (pgg-process-when-success nil)))
154
155 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp)
156                                                start end)
157   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
158          (passphrase
159           (pgg-read-passphrase
160            (format "PGP passphrase for %s: " pgg-pgp-user-id)
161            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'encrypt)))
162          (args
163           '("+verbose=1" "+batchmode" "+language=us" "-f")))
164     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
165     (pgg-process-when-success nil)))
166
167 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp)
168                                             start end &optional clearsign)
169   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
170          (passphrase
171           (pgg-read-passphrase
172            (format "PGP passphrase for %s: " pgg-pgp-user-id)
173            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'sign)))
174          (args
175           (list (if clearsign "-fast" "-fbast")
176                 "+verbose=1" "+language=us" "+batchmode"
177                 "-u" pgg-pgp-user-id)))
178     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
179     (pgg-process-when-success
180       (goto-char (point-min))
181       (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
182         (let ((packet
183                (cdr (assq 2 (pgg-parse-armor-region
184                              (progn (beginning-of-line 2)
185                                     (point))
186                              (point-max))))))
187           (if pgg-cache-passphrase
188               (pgg-add-passphrase-cache
189                (cdr (assq 'key-identifier packet))
190                passphrase)))))))
191
192 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-pgp)
193                                               start end &optional signature)
194   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
195          (orig-file (make-temp-name basename))
196          (args '("+verbose=1" "+batchmode" "+language=us"))
197          (orig-mode (default-file-modes)))
198     (unwind-protect
199         (progn
200           (set-default-file-modes 448)
201           (binary-write-region start end orig-file))
202       (set-default-file-modes orig-mode))
203     (when (stringp signature)
204       (copy-file signature (setq signature (concat orig-file ".asc")))
205       (setq args (append args (list signature orig-file))))
206     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
207     (delete-file orig-file)
208     (if signature (delete-file signature))
209     (pgg-process-when-success
210       (goto-char (point-min))
211       (let ((case-fold-search t))
212         (while (re-search-forward "^warning: " nil t)
213           (delete-region (match-beginning 0)
214                          (progn (beginning-of-line 2) (point)))))
215       (goto-char (point-min))
216       (when (re-search-forward "^\\.$" nil t)
217         (delete-region (point-min)
218                        (progn (beginning-of-line 2)
219                               (point)))))))
220
221 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp))
222   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
223          (args
224           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
225                 (concat "\"" pgg-pgp-user-id "\""))))
226     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
227     (insert-buffer-substring pgg-output-buffer)))
228
229 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp)
230                                                   start end)
231   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
232          (basename (expand-file-name "pgg" temporary-file-directory))
233          (key-file (make-temp-name basename))
234          (args
235           (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
236                 key-file)))
237     (let ((coding-system-for-write 'raw-text-dos))
238       (write-region start end key-file))
239     (pgg-pgp-process-region start end nil pgg-pgp-program args)
240     (delete-file key-file)
241     (pgg-process-when-success nil)))
242
243 (provide 'pgg-pgp)
244
245 ;;; pgg-pgp.el ends here