* pgg.el (pgg-encrypt-region): Support sign.
[gnus] / lisp / pgg.el
1 ;;; pgg.el --- glue for the various PGP implementations.
2
3 ;; Copyright (C) 1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Created: 1999/10/28
7 ;; Keywords: PGP
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
27 ;;; Commentary:
28 ;; 
29
30 ;;; Code:
31
32 (require 'pgg-def)
33 (require 'pgg-parse)
34
35 (eval-when-compile
36   (ignore-errors
37     (require 'w3)
38     (require 'url)))
39
40 (defvar pgg-temporary-file-directory
41   (cond ((fboundp 'temp-directory) (temp-directory))
42         ((boundp 'temporary-file-directory) temporary-file-directory)
43         ("/tmp/")))
44
45 ;;; @ utility functions
46 ;;;
47
48 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
49
50 (defun pgg-invoke (func scheme &rest args)
51   (progn
52     (require (intern (format "pgg-%s" scheme)))
53     (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
54
55 (put 'pgg-save-coding-system 'lisp-indent-function 2)
56
57 (defmacro pgg-save-coding-system (start end &rest body)
58   `(if (interactive-p)
59        (let ((buffer (current-buffer)))
60          (with-temp-buffer
61            (let (buffer-undo-list)
62              (insert-buffer-substring buffer ,start ,end)
63              (encode-coding-region (point-min)(point-max)
64                                    buffer-file-coding-system)
65              (prog1 (save-excursion ,@body)
66                (push nil buffer-undo-list)
67                (ignore-errors (undo))))))
68      (save-restriction
69        (narrow-to-region ,start ,end)
70        ,@body)))
71
72 (defun pgg-temp-buffer-show-function (buffer)
73   (let ((window (split-window-vertically)))
74     (set-window-buffer window buffer)
75     (shrink-window-if-larger-than-buffer window)))
76
77 (defun pgg-display-output-buffer (start end status)
78   (if status
79       (progn
80         (delete-region start end)
81         (insert-buffer-substring pgg-output-buffer)
82         (decode-coding-region start (point) buffer-file-coding-system))
83     (let ((temp-buffer-show-function
84            (function pgg-temp-buffer-show-function)))
85       (with-output-to-temp-buffer pgg-echo-buffer
86         (set-buffer standard-output)
87         (insert-buffer-substring pgg-errors-buffer)))))
88
89 (defvar pgg-passphrase-cache-expiry 16)
90 (defvar pgg-passphrase-cache (make-vector 7 0))
91
92 (defvar pgg-read-passphrase nil)
93 (defun pgg-read-passphrase (prompt &optional key)
94   (if (not pgg-read-passphrase)
95       (if (functionp 'read-passwd)
96           (setq pgg-read-passphrase 'read-passwd)
97         (if (load "passwd" t)
98             (setq pgg-read-passphrase 'read-passwd)
99           (autoload 'ange-ftp-read-passwd "ange-ftp")
100           (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
101   (or (and pgg-cache-passphrase
102            key (setq key (pgg-truncate-key-identifier key))
103            (symbol-value (intern-soft key pgg-passphrase-cache)))
104       (funcall pgg-read-passphrase prompt)))
105
106 (defun pgg-add-passphrase-cache (key passphrase)
107   (setq key (pgg-truncate-key-identifier key))
108   (set (intern key pgg-passphrase-cache)
109        passphrase)
110   (run-at-time pgg-passphrase-cache-expiry nil
111                #'pgg-remove-passphrase-cache
112                key))
113
114 (defun pgg-remove-passphrase-cache (key)
115   (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
116     (when passphrase
117       (fillarray passphrase ?_)
118       (unintern key pgg-passphrase-cache))))
119
120 (defmacro pgg-convert-lbt-region (start end lbt)
121   `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
122      (goto-char ,start)
123      (case ,lbt
124        (CRLF
125         (while (progn
126                  (end-of-line)
127                  (> (marker-position pgg-conversion-end) (point)))
128           (insert "\r")
129           (forward-line 1)))
130        (LF
131         (while (re-search-forward "\r$" pgg-conversion-end t)
132           (replace-match ""))))))
133
134 (put 'pgg-as-lbt 'lisp-indent-function 3)
135
136 (defmacro pgg-as-lbt (start end lbt &rest body)
137   `(let ((inhibit-read-only t)
138          buffer-read-only
139          buffer-undo-list)
140      (pgg-convert-lbt-region ,start ,end ,lbt)
141      (let ((,end (point)))
142        ,@body)
143      (push nil buffer-undo-list)
144      (ignore-errors (undo))))
145
146 (put 'pgg-process-when-success 'lisp-indent-function 0)
147
148 (defmacro pgg-process-when-success (&rest body)
149   `(with-current-buffer pgg-output-buffer
150      (if (zerop (buffer-size)) nil ,@body t)))
151
152 ;;; @ interface functions
153 ;;;
154
155 ;;;###autoload
156 (defun pgg-encrypt-region (start end rcpts &optional sign)
157   "Encrypt the current region between START and END for RCPTS.
158 If optional argument SIGN is non-nil, do a combined sign and encrypt."
159   (interactive
160    (list (region-beginning)(region-end)
161          (split-string (read-string "Recipients: ") "[ \t,]+")))
162   (let ((status
163          (pgg-save-coding-system start end
164            (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme)
165                        (point-min) (point-max) rcpts sign))))
166     (when (interactive-p)
167       (pgg-display-output-buffer start end status))
168     status))
169
170 ;;;###autoload
171 (defun pgg-decrypt-region (start end)
172   "Decrypt the current region between START and END."
173   (interactive "r")
174   (let ((status
175          (pgg-save-coding-system start end
176            (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
177                        (point-min) (point-max)))))
178     (when (interactive-p)
179       (pgg-display-output-buffer start end status))
180     status))
181
182 ;;;###autoload
183 (defun pgg-sign-region (start end &optional cleartext)
184   "Make the signature from text between START and END.
185 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
186 a detached signature."
187   (interactive "r")
188   (let ((status (pgg-save-coding-system start end
189                   (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
190                               (point-min) (point-max)
191                               (or (interactive-p) cleartext)))))
192     (when (interactive-p)
193       (pgg-display-output-buffer start end status))
194     status))
195
196 ;;;###autoload
197 (defun pgg-verify-region (start end &optional signature fetch)
198   "Verify the current region between START and END.
199 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
200 the detached signature of the current region.
201
202 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
203 signer's public key from `pgg-default-keyserver-address'."
204   (interactive "r")
205   (let* ((packet
206           (if (null signature) nil
207             (with-temp-buffer
208               (buffer-disable-undo)
209               (if (fboundp 'set-buffer-multibyte)
210                   (set-buffer-multibyte nil))
211               (insert-file-contents signature)
212               (cdr (assq 2 (pgg-decode-armor-region
213                             (point-min)(point-max)))))))
214          (key (cdr (assq 'key-identifier packet)))
215          status keyserver)
216     (and (stringp key)
217          (setq key (concat "0x" (pgg-truncate-key-identifier key)))
218          (null (pgg-lookup-key key))
219          (or fetch (interactive-p))
220          (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
221          (setq keyserver
222                (or (cdr (assq 'preferred-key-server packet))
223                    pgg-default-keyserver-address))
224          (pgg-fetch-key keyserver key))
225     (setq status 
226           (pgg-save-coding-system start end
227             (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
228                         (point-min) (point-max) signature)))
229     (when (interactive-p)
230       (let ((temp-buffer-show-function
231              (function pgg-temp-buffer-show-function)))
232         (with-output-to-temp-buffer pgg-echo-buffer
233           (set-buffer standard-output)
234           (insert-buffer-substring (if status pgg-output-buffer
235                                      pgg-errors-buffer)))))
236     status))
237
238 ;;;###autoload
239 (defun pgg-insert-key ()
240   "Insert the ASCII armored public key."
241   (interactive)
242   (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
243
244 ;;;###autoload
245 (defun pgg-snarf-keys-region (start end)
246   "Import public keys in the current region between START and END."
247   (interactive "r")
248   (pgg-save-coding-system start end
249     (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
250                 start end)))
251
252 (defun pgg-lookup-key (string &optional type)
253   (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
254
255 (defvar pgg-insert-url-function  (function pgg-insert-url-with-w3))
256
257 (defun pgg-insert-url-with-w3 (url)
258   (require 'w3)
259   (require 'url)
260   (let (buffer-file-name)
261     (url-insert-file-contents url)))
262
263 (defvar pgg-insert-url-extra-arguments nil)
264 (defvar pgg-insert-url-program nil)
265
266 (defun pgg-insert-url-with-program (url)
267   (let ((args (copy-sequence pgg-insert-url-extra-arguments))
268         process)
269     (insert
270      (with-temp-buffer
271        (setq process
272              (apply #'start-process " *PGG url*" (current-buffer)
273                     pgg-insert-url-program (nconc args (list url))))
274        (set-process-sentinel process #'ignore)
275        (while (eq 'run (process-status process))
276          (accept-process-output process 5))
277        (delete-process process)
278        (if (and process (eq 'run (process-status process)))
279            (interrupt-process process))
280        (buffer-string)))))
281
282 (defun pgg-fetch-key (keyserver key)
283   "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
284   (with-current-buffer (get-buffer-create pgg-output-buffer)
285     (buffer-disable-undo)
286     (erase-buffer)
287     (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
288                      (substring keyserver 0 (1- (match-end 0))))))
289       (save-excursion
290         (funcall pgg-insert-url-function
291                  (if proto keyserver
292                    (format "http://%s:11371/pks/lookup?op=get&search=%s"
293                            keyserver key))))
294       (when (re-search-forward "^-+BEGIN" nil 'last)
295         (delete-region (point-min) (match-beginning 0))
296         (when (re-search-forward "^-+END" nil t)
297           (delete-region (progn (end-of-line) (point))
298                          (point-max)))
299         (insert "\n")
300         (with-temp-buffer
301           (insert-buffer-substring pgg-output-buffer)
302           (pgg-snarf-keys-region (point-min)(point-max)))))))
303
304
305 (provide 'pgg)
306
307 ;;; pgg.el ends here