* mime-edit.el
[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          (output-buffer pgg-output-buffer)
80          (errors-buffer pgg-errors-buffer)
81          (process-connection-type nil)
82          process status exit-status)
83     (with-current-buffer (get-buffer-create output-buffer)
84       (buffer-disable-undo)
85       (erase-buffer))
86     (when passphrase
87       (setenv "PGPPASSFD" "0"))
88     (as-binary-process
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 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-pgp)
120                                                   string &optional type)
121   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
122     (with-current-buffer (get-buffer-create pgg-output-buffer)
123       (buffer-disable-undo)
124       (erase-buffer)
125       (apply #'call-process pgg-pgp-program nil t nil args)
126       (goto-char (point-min))
127       (cond
128        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
129         (buffer-substring (point)(+ 8 (point))))
130        ((re-search-forward "^Type" nil t);PGP 6.*
131         (beginning-of-line 2)
132         (substring
133          (nth 2 (split-string
134                  (buffer-substring (point)(progn (end-of-line) (point)))))
135          2))))))
136
137 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp)
138                                                start end recipients)
139   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
140          (args
141           `("+encrypttoself=off +verbose=1" "+batchmode"
142             "+language=us" "-fate"
143             ,@(if recipients
144                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
145                           (append recipients
146                                   (if pgg-encrypt-for-me
147                                       (list pgg-pgp-user-id))))))))
148     (pgg-pgp-process-region start end nil pgg-pgp-program args)
149     (pgg-process-when-success nil)))
150
151 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp)
152                                                start end)
153   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
154          (passphrase
155           (pgg-read-passphrase
156            (format "PGP passphrase for %s: " pgg-pgp-user-id)
157            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'encrypt)))
158          (args
159           '("+verbose=1" "+batchmode" "+language=us" "-f")))
160     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
161     (pgg-process-when-success nil)))
162
163 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp)
164                                             start end &optional clearsign)
165   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
166          (passphrase
167           (pgg-read-passphrase
168            (format "PGP passphrase for %s: " pgg-pgp-user-id)
169            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'sign)))
170          (args
171           (list (if clearsign "-fast" "-fbast")
172                 "+verbose=1" "+language=us" "+batchmode"
173                 "-u" pgg-pgp-user-id)))
174     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
175     (pgg-process-when-success
176       (goto-char (point-min))
177       (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
178         (let ((packet
179                (cdr (assq 2 (pgg-parse-armor-region
180                              (progn (beginning-of-line 2)
181                                     (point))
182                              (point-max))))))
183           (if pgg-cache-passphrase
184               (pgg-add-passphrase-cache
185                (cdr (assq 'key-identifier packet))
186                passphrase)))))))
187
188 (luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-pgp)
189                                               start end &optional signature)
190   (let* ((basename (expand-file-name "pgg" temporary-file-directory))
191          (orig-file (make-temp-name basename))
192          (args '("+verbose=1" "+batchmode" "+language=us"))
193          (orig-mode (default-file-modes)))
194     (unwind-protect
195         (progn
196           (set-default-file-modes 448)
197           (write-region-as-binary start end orig-file))
198       (set-default-file-modes orig-mode))
199     (when (stringp signature)
200       (copy-file signature (setq signature (concat orig-file ".asc")))
201       (setq args (append args (list signature orig-file))))
202     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
203     (delete-file orig-file)
204     (if signature (delete-file signature))
205     (pgg-process-when-success
206       (goto-char (point-min))
207       (let ((case-fold-search t))
208         (while (re-search-forward "^warning: " nil t)
209           (delete-region (match-beginning 0)
210                          (progn (beginning-of-line 2) (point)))))
211       (goto-char (point-min))
212       (when (re-search-forward "^\\.$" nil t)
213         (delete-region (point-min)
214                        (progn (beginning-of-line 2)
215                               (point)))))))
216
217 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp))
218   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
219          (args
220           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
221                 (concat "\"" pgg-pgp-user-id "\""))))
222     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
223     (insert-buffer-substring pgg-output-buffer)))
224
225 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp)
226                                                   start end)
227   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
228          (basename (expand-file-name "pgg" temporary-file-directory))
229          (key-file (make-temp-name basename))
230          (args
231           (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
232                 key-file)))
233     (write-region-as-raw-text-CRLF start end key-file)
234     (pgg-pgp-process-region start end nil pgg-pgp-program args)
235     (delete-file key-file)
236     (pgg-process-when-success nil)))
237
238 (provide 'pgg-pgp)
239
240 ;;; pgg-pgp.el ends here