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