f86e37f19e0c9d37a4ee2d46f77e9156c0b39268
[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 (if (fboundp 'url-insert-file-contents)
49                                    (function pgg-fetch-key-with-w3)))
50
51 (defun pgg-invoke (func scheme &rest args)
52   (progn
53     (require (intern (format "pgg-%s" scheme)))
54     (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args)))
55
56 (put 'pgg-save-coding-system 'lisp-indent-function 2)
57
58 (defmacro pgg-save-coding-system (start end &rest body)
59   `(if (interactive-p)
60        (let ((buffer (current-buffer)))
61          (with-temp-buffer
62            (let (buffer-undo-list)
63              (insert-buffer-substring buffer ,start ,end)
64              (encode-coding-region (point-min)(point-max)
65                                    buffer-file-coding-system)
66              (prog1 (save-excursion ,@body)
67                (push nil buffer-undo-list)
68                (ignore-errors (undo))))))
69      (save-restriction
70        (narrow-to-region ,start ,end)
71        ,@body)))
72
73 (defun pgg-temp-buffer-show-function (buffer)
74   (let ((window (split-window-vertically)))
75     (set-window-buffer window buffer)
76     (shrink-window-if-larger-than-buffer window)))
77
78 (defun pgg-display-output-buffer (start end status)
79   (if status
80       (progn
81         (delete-region start end)
82         (insert-buffer-substring pgg-output-buffer)
83         (decode-coding-region start (point) buffer-file-coding-system))
84     (let ((temp-buffer-show-function
85            (function pgg-temp-buffer-show-function)))
86       (with-output-to-temp-buffer pgg-echo-buffer
87         (set-buffer standard-output)
88         (insert-buffer-substring pgg-errors-buffer)))))
89
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-encrypt (rcpts &optional sign start end)
172   "Encrypt the current buffer for RCPTS.
173 If optional argument SIGN is non-nil, do a combined sign and encrypt.
174 If optional arguments START and END are specified, only encrypt within
175 the region."
176   (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
177   (pgg-encrypt-region (or start (point-min)) (or end (point-max)) rcpts sign))
178
179 ;;;###autoload
180 (defun pgg-decrypt-region (start end)
181   "Decrypt the current region between START and END."
182   (interactive "r")
183   (let* ((buf (current-buffer))
184          (packet (cdr (assq 1 (with-temp-buffer
185                                 (insert-buffer buf)
186                                 (pgg-decode-armor-region
187                                  (point-min) (point-max))))))
188          (key (cdr (assq 'key-identifier packet)))
189          (pgg-default-user-id 
190           (if key
191               (concat "0x" (pgg-truncate-key-identifier key))
192             pgg-default-user-id))
193          (status
194           (pgg-save-coding-system start end
195             (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme)
196                         (point-min) (point-max)))))
197     (when (interactive-p)
198       (pgg-display-output-buffer start end status))
199     status))
200
201 ;;;###autoload
202 (defun pgg-decrypt (&optional start end)
203   "Decrypt the current buffer.
204 If optional arguments START and END are specified, only decrypt within
205 the region."
206   (interactive "")
207   (pgg-decrypt-region (or start (point-min)) (or end (point-max))))
208
209 ;;;###autoload
210 (defun pgg-sign-region (start end &optional cleartext)
211   "Make the signature from text between START and END.
212 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
213 a detached signature."
214   (interactive "r")
215   (let ((status (pgg-save-coding-system start end
216                   (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme)
217                               (point-min) (point-max)
218                               (or (interactive-p) cleartext)))))
219     (when (interactive-p)
220       (pgg-display-output-buffer start end status))
221     status))
222
223 ;;;###autoload
224 (defun pgg-sign (&optional cleartext start end)
225   "Sign the current buffer.
226 If the optional argument CLEARTEXT is non-nil, it does not create a
227 detached signature.
228 If optional arguments START and END are specified, only sign data
229 within the region."
230   (interactive "")
231   (pgg-sign-region (or start (point-min)) (or end (point-max)) cleartext))
232   
233 ;;;###autoload
234 (defun pgg-verify-region (start end &optional signature fetch)
235   "Verify the current region between START and END.
236 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
237 the detached signature of the current region.
238
239 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
240 signer's public key from `pgg-default-keyserver-address'."
241   (interactive "r")
242   (let* ((packet
243           (if (null signature) nil
244             (with-temp-buffer
245               (buffer-disable-undo)
246               (if (fboundp 'set-buffer-multibyte)
247                   (set-buffer-multibyte nil))
248               (insert-file-contents signature)
249               (cdr (assq 2 (pgg-decode-armor-region
250                             (point-min)(point-max)))))))
251          (key (cdr (assq 'key-identifier packet)))
252          status keyserver)
253     (and (stringp key)
254          pgg-query-keyserver
255          (setq key (concat "0x" (pgg-truncate-key-identifier key)))
256          (null (pgg-lookup-key key))
257          (or fetch (interactive-p))
258          (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
259          (setq keyserver
260                (or (cdr (assq 'preferred-key-server packet))
261                    pgg-default-keyserver-address))
262          (pgg-fetch-key keyserver key))
263     (setq status 
264           (pgg-save-coding-system start end
265             (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme)
266                         (point-min) (point-max) signature)))
267     (when (interactive-p)
268       (let ((temp-buffer-show-function
269              (function pgg-temp-buffer-show-function)))
270         (with-output-to-temp-buffer pgg-echo-buffer
271           (set-buffer standard-output)
272           (insert-buffer-substring (if status pgg-output-buffer
273                                      pgg-errors-buffer)))))
274     status))
275
276 ;;;###autoload
277 (defun pgg-verify (&optional signature fetch start end)
278   "Verify the current buffer.
279 If the optional argument SIGNATURE is non-nil, it is treated as
280 the detached signature of the current region.
281 If the optional argument FETCH is non-nil, we attempt to fetch the
282 signer's public key from `pgg-default-keyserver-address'.
283 If optional arguments START and END are specified, only verify data
284 within the region."
285   (interactive "")
286   (pgg-verify-region (or start (point-min)) (or end (point-max))
287                      signature fetch))
288
289 ;;;###autoload
290 (defun pgg-insert-key ()
291   "Insert the ASCII armored public key."
292   (interactive)
293   (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme)))
294
295 ;;;###autoload
296 (defun pgg-snarf-keys-region (start end)
297   "Import public keys in the current region between START and END."
298   (interactive "r")
299   (pgg-save-coding-system start end
300     (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme)
301                 start end)))
302
303 ;;;###autoload
304 (defun pgg-snarf-keys ()
305   "Import public keys in the current buffer."
306   (interactive "")
307   (pgg-snarf-keys-region (point-min) (point-max)))
308
309 (defun pgg-lookup-key (string &optional type)
310   (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type))
311
312 (defvar pgg-insert-url-function  (function pgg-insert-url-with-w3))
313
314 (defun pgg-insert-url-with-w3 (url)
315   (ignore-errors
316     (require 'w3)
317     (require 'url)
318     (let (buffer-file-name)
319       (url-insert-file-contents url))))
320
321 (defvar pgg-insert-url-extra-arguments nil)
322 (defvar pgg-insert-url-program nil)
323
324 (defun pgg-insert-url-with-program (url)
325   (let ((args (copy-sequence pgg-insert-url-extra-arguments))
326         process)
327     (insert
328      (with-temp-buffer
329        (setq process
330              (apply #'start-process " *PGG url*" (current-buffer)
331                     pgg-insert-url-program (nconc args (list url))))
332        (set-process-sentinel process #'ignore)
333        (while (eq 'run (process-status process))
334          (accept-process-output process 5))
335        (delete-process process)
336        (if (and process (eq 'run (process-status process)))
337            (interrupt-process process))
338        (buffer-string)))))
339
340 (defun pgg-fetch-key (keyserver key)
341   "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
342   (with-current-buffer (get-buffer-create pgg-output-buffer)
343     (buffer-disable-undo)
344     (erase-buffer)
345     (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
346                      (substring keyserver 0 (1- (match-end 0))))))
347       (save-excursion
348         (funcall pgg-insert-url-function
349                  (if proto keyserver
350                    (format "http://%s:11371/pks/lookup?op=get&search=%s"
351                            keyserver key))))
352       (when (re-search-forward "^-+BEGIN" nil 'last)
353         (delete-region (point-min) (match-beginning 0))
354         (when (re-search-forward "^-+END" nil t)
355           (delete-region (progn (end-of-line) (point))
356                          (point-max)))
357         (insert "\n")
358         (with-temp-buffer
359           (insert-buffer-substring pgg-output-buffer)
360           (pgg-snarf-keys-region (point-min)(point-max)))))))
361
362
363 (provide 'pgg)
364
365 ;;; pgg.el ends here