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