Update copyright header.
[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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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-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 (make-temp-file "pgg-errors"))
72          (args
73           (append args
74                   pgg-pgp-extra-args
75                   (list (concat "2>" errors-file-name))))
76          (shell-file-name pgg-pgp-shell-file-name)
77          (shell-command-switch pgg-pgp-shell-command-switch)
78          (process-environment process-environment)
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     (unwind-protect
89         (progn
90           (setq process
91                 (apply #'binary-funcall
92                        #'start-process-shell-command "*PGP*" output-buffer
93                        program args))
94           (set-process-sentinel process #'ignore)
95           (when passphrase
96             (process-send-string process (concat passphrase "\n")))
97           (process-send-region process start end)
98           (process-send-eof process)
99           (while (eq 'run (process-status process))
100             (accept-process-output process 5))
101           (setq status (process-status process)
102                 exit-status (process-exit-status process))
103           (delete-process process)
104           (with-current-buffer output-buffer
105             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
106
107             (if (memq status '(stop signal))
108                 (error "%s exited abnormally: '%s'" program exit-status))
109             (if (= 127 exit-status)
110                 (error "%s could not be found" program))
111
112             (set-buffer (get-buffer-create errors-buffer))
113             (buffer-disable-undo)
114             (erase-buffer)
115             (insert-file-contents errors-file-name)))
116       (if (and process (eq 'run (process-status process)))
117           (interrupt-process process))
118       (condition-case nil
119           (delete-file errors-file-name)
120         (file-error nil)))))
121
122 (luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-pgp)
123                                                   string &optional type)
124   (let ((args (list "+batchmode" "+language=en" "-kv" string)))
125     (with-current-buffer (get-buffer-create pgg-output-buffer)
126       (buffer-disable-undo)
127       (erase-buffer)
128       (apply #'call-process pgg-pgp-program nil t nil args)
129       (goto-char (point-min))
130       (cond
131        ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
132         (buffer-substring (point)(+ 8 (point))))
133        ((re-search-forward "^Type" nil t);PGP 6.*
134         (beginning-of-line 2)
135         (substring
136          (nth 2 (split-string
137                  (buffer-substring (point)(progn (end-of-line) (point)))))
138          2))))))
139
140 (luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp)
141                                                start end recipients)
142   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
143          (args
144           `("+encrypttoself=off +verbose=1" "+batchmode"
145             "+language=us" "-fate"
146             ,@(if recipients
147                   (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
148                           (append recipients
149                                   (if pgg-encrypt-for-me
150                                       (list pgg-pgp-user-id))))))))
151     (pgg-pgp-process-region start end nil pgg-pgp-program args)
152     (pgg-process-when-success nil)))
153
154 (luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp)
155                                                start end)
156   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
157          (passphrase
158           (pgg-read-passphrase
159            (format "PGP passphrase for %s: " pgg-pgp-user-id)
160            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'encrypt)))
161          (args
162           '("+verbose=1" "+batchmode" "+language=us" "-f")))
163     (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
164     (pgg-process-when-success nil)))
165
166 (luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp)
167                                             start end &optional clearsign)
168   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
169          (passphrase
170           (pgg-read-passphrase
171            (format "PGP passphrase for %s: " pgg-pgp-user-id)
172            (pgg-scheme-lookup-key scheme pgg-pgp-user-id 'sign)))
173          (args
174           (list (if clearsign "-fast" "-fbas")
175                 "+verbose=1" "+language=us" "+batchmode"
176                 "-u" pgg-pgp-user-id)))
177     (pgg-as-lbt start end 'CRLF
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 ((orig-file (make-temp-file "pgg"))
195         (args '("+verbose=1" "+batchmode" "+language=us"))
196         (orig-mode (default-file-modes)))
197     (unwind-protect
198         (progn
199           (set-default-file-modes 448)
200           (binary-write-decoded-region start end orig-file))
201       (set-default-file-modes orig-mode))
202     (when (stringp signature)
203       (copy-file signature (setq signature (concat orig-file ".asc")))
204       (setq args (append args (list signature orig-file))))
205     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
206     (delete-file orig-file)
207     (if signature (delete-file signature))
208     (pgg-process-when-success
209       (goto-char (point-min))
210       (let ((case-fold-search t))
211         (while (re-search-forward "^warning: " nil t)
212           (delete-region (match-beginning 0)
213                          (progn (beginning-of-line 2) (point)))))
214       (goto-char (point-min))
215       (when (re-search-forward "^\\.$" nil t)
216         (delete-region (point-min)
217                        (progn (beginning-of-line 2)
218                               (point)))))))
219
220 (luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp))
221   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
222          (args
223           (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
224                 (concat "\"" pgg-pgp-user-id "\""))))
225     (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
226     (insert-buffer-substring pgg-output-buffer)))
227
228 (luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp)
229                                                   start end)
230   (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
231          (key-file (make-temp-file "pgg"))
232          (args
233           (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
234                 key-file)))
235     (let ((coding-system-for-write 'raw-text-dos))
236       (write-region start end key-file))
237     (pgg-pgp-process-region start end nil pgg-pgp-program args)
238     (delete-file key-file)
239     (pgg-process-when-success nil)))
240
241 (provide 'pgg-pgp)
242
243 ;;; pgg-pgp.el ends here