* lpath.el: Fbind string-as-multibyte for XEmacs.
[gnus] / lisp / pgg-pgp5.el
1 ;;; pgg-pgp5.el --- PGP 5.* support for PGG.
2
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Created: 1999/11/02
8 ;; Keywords: PGP, OpenPGP
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Code:
28
29 (eval-when-compile
30   (require 'cl)                         ; for pgg macros
31   (require 'pgg))
32
33 (defgroup pgg-pgp5 ()
34   "PGP 5.* interface."
35   :group 'pgg)
36
37 (defcustom pgg-pgp5-pgpe-program "pgpe"
38   "PGP 5.* 'pgpe' executable."
39   :group 'pgg-pgp5
40   :type 'string)
41
42 (defcustom pgg-pgp5-pgps-program "pgps"
43   "PGP 5.* 'pgps' executable."
44   :group 'pgg-pgp5
45   :type 'string)
46
47 (defcustom pgg-pgp5-pgpk-program "pgpk"
48   "PGP 5.* 'pgpk' executable."
49   :group 'pgg-pgp5
50   :type 'string)
51
52 (defcustom pgg-pgp5-pgpv-program "pgpv"
53   "PGP 5.* 'pgpv' executable."
54   :group 'pgg-pgp5
55   :type 'string)
56
57 (defcustom pgg-pgp5-shell-file-name "/bin/sh"
58   "File name to load inferior shells from.
59 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
60   :group 'pgg-pgp5
61   :type 'string)
62
63 (defcustom pgg-pgp5-shell-command-switch "-c"
64   "Switch used to have the shell execute its command line argument."
65   :group 'pgg-pgp5
66   :type 'string)
67
68 (defcustom pgg-pgp5-extra-args nil
69   "Extra arguments for every PGP 5.* invocation."
70   :group 'pgg-pgp5
71   :type '(choice
72           (const :tag "None" nil)
73           (string :tag "Arguments")))
74
75 (defvar pgg-pgp5-user-id nil
76   "PGP 5.* ID of your default identity.")
77
78 (defun pgg-pgp5-process-region (start end passphrase program args)
79   (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
80          (args
81           (append args
82                   pgg-pgp5-extra-args
83                   (list (concat "2>" errors-file-name))))
84          (shell-file-name pgg-pgp5-shell-file-name)
85          (shell-command-switch pgg-pgp5-shell-command-switch)
86          (process-environment process-environment)
87          (output-buffer pgg-output-buffer)
88          (errors-buffer pgg-errors-buffer)
89          (process-connection-type nil)
90          process status exit-status)
91     (with-current-buffer (let ((default-enable-multibyte-characters t))
92                            (get-buffer-create output-buffer))
93       (buffer-disable-undo)
94       (erase-buffer))
95     (when passphrase
96       (setenv "PGPPASSFD" "0"))
97     (unwind-protect
98         (progn
99           (let ((coding-system-for-read 'binary)
100                 (coding-system-for-write 'binary))
101             (setq process
102                   (apply #'funcall
103                          #'start-process-shell-command "*PGP*" output-buffer
104                          program args)))
105           (set-process-sentinel process #'ignore)
106           (when passphrase
107             (process-send-string process (concat passphrase "\n")))
108           (process-send-region process start end)
109           (process-send-eof process)
110           (while (eq 'run (process-status process))
111             (accept-process-output process 5))
112           (setq status (process-status process)
113                 exit-status (process-exit-status process))
114           (delete-process process)
115           (with-current-buffer output-buffer
116             (pgg-convert-lbt-region (point-min)(point-max) 'LF)
117
118             (if (memq status '(stop signal))
119                 (error "%s exited abnormally: '%s'" program exit-status))
120             (if (= 127 exit-status)
121                 (error "%s could not be found" program))
122
123             (set-buffer (get-buffer-create errors-buffer))
124             (buffer-disable-undo)
125             (erase-buffer)
126             (insert-file-contents errors-file-name)))
127       (if (and process (eq 'run (process-status process)))
128           (interrupt-process process))
129       (condition-case nil
130           (delete-file errors-file-name)
131         (file-error nil)))))
132
133 (defun pgg-pgp5-lookup-key (string &optional type)
134   "Search keys associated with STRING."
135   (let ((args (list "+language=en" "-l" string)))
136     (with-current-buffer (let ((default-enable-multibyte-characters t))
137                            (get-buffer-create pgg-output-buffer))
138       (buffer-disable-undo)
139       (erase-buffer)
140       (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
141       (goto-char (point-min))
142       (when (re-search-forward "^sec" nil t)
143         (substring
144          (nth 2 (split-string
145                  (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
146          2)))))
147
148 (defun pgg-pgp5-encrypt-region (start end recipients &optional sign passphrase)
149   "Encrypt the current region between START and END."
150   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
151          (passphrase (or passphrase
152                          (when sign
153                            (pgg-read-passphrase
154                             (format "PGP passphrase for %s: "
155                                     pgg-pgp5-user-id)
156                             pgg-pgp5-user-id))))
157          (args
158           (append
159            `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
160              ,@(if recipients
161                    (apply #'append
162                           (mapcar (lambda (rcpt)
163                                     (list "-r"
164                                           (concat "\"" rcpt "\"")))
165                                   (append recipients
166                                           (if pgg-encrypt-for-me
167                                               (list pgg-pgp5-user-id)))))))
168            (if sign '("-s" "-u" pgg-pgp5-user-id)))))
169     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
170     (pgg-process-when-success nil)))
171
172 (defun pgg-pgp5-decrypt-region (start end &optional passphrase)
173   "Decrypt the current region between START and END."
174   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
175          (passphrase
176           (or passphrase
177               (pgg-read-passphrase
178                (format "PGP passphrase for %s: " pgg-pgp5-user-id)
179                (pgg-pgp5-lookup-key pgg-pgp5-user-id 'encrypt))))
180          (args
181           '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
182     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
183     (pgg-process-when-success nil)))
184
185 (defun pgg-pgp5-sign-region (start end &optional clearsign passphrase)
186   "Make detached signature from text between START and END."
187   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
188          (passphrase
189           (or passphrase
190               (pgg-read-passphrase
191                (format "PGP passphrase for %s: " pgg-pgp5-user-id)
192                (pgg-pgp5-lookup-key pgg-pgp5-user-id 'sign))))
193          (args
194           (list (if clearsign "-fat" "-fbat")
195                 "+verbose=1" "+language=us" "+batchmode=1"
196                 "-u" pgg-pgp5-user-id)))
197     (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
198     (pgg-process-when-success
199       (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
200         (let ((packet
201                (cdr (assq 2 (pgg-parse-armor-region
202                              (progn (beginning-of-line 2)
203                                     (point))
204                              (point-max))))))
205           (if pgg-cache-passphrase
206               (pgg-add-passphrase-to-cache
207                (cdr (assq 'key-identifier packet))
208                passphrase)))))))
209
210 (defun pgg-pgp5-verify-region (start end &optional signature)
211   "Verify region between START and END as the detached signature SIGNATURE."
212   (let ((orig-file (pgg-make-temp-file "pgg"))
213         (args '("+verbose=1" "+batchmode=1" "+language=us"))
214         (orig-mode (default-file-modes)))
215     (unwind-protect
216         (progn
217           (set-default-file-modes 448)
218           (let ((coding-system-for-write 'binary)
219                 jka-compr-compression-info-list jam-zcat-filename-list)
220             (write-region start end orig-file)))
221       (set-default-file-modes orig-mode))
222     (when (stringp signature)
223       (copy-file signature (setq signature (concat orig-file ".asc")))
224       (setq args (append args (list signature))))
225     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
226     (delete-file orig-file)
227     (if signature (delete-file signature))
228     (with-current-buffer pgg-errors-buffer
229       (goto-char (point-min))
230       (if (re-search-forward "^Good signature" nil t)
231           (progn
232             (set-buffer pgg-output-buffer)
233             (insert-buffer-substring pgg-errors-buffer)
234             t)
235         nil))))
236
237 (defun pgg-pgp5-insert-key ()
238   "Insert public key at point."
239   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
240          (args
241           (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
242                 (concat "\"" pgg-pgp5-user-id "\""))))
243     (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
244     (insert-buffer-substring pgg-output-buffer)))
245
246 (defun pgg-pgp5-snarf-keys-region (start end)
247   "Add all public keys in region between START and END to the keyring."
248   (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
249          (key-file (pgg-make-temp-file "pgg"))
250          (args
251           (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
252                 key-file)))
253     (let ((coding-system-for-write 'raw-text-dos))
254       (write-region start end key-file))
255     (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
256     (delete-file key-file)
257     (pgg-process-when-success nil)))
258
259 (provide 'pgg-pgp5)
260
261 ;;; arch-tag: 3dbd1073-6b3a-466c-9f55-5c587ffa6d7b
262 ;;; pgg-pgp5.el ends here