1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999-2000, 2002-2015 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de>
9 ;; Obsolete-since: 24.1
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (autoload 'run-at-time
"timer")
32 ;; Don't merge these two `eval-when-compile's.
34 ;; For Emacs <22.2 and XEmacs.
35 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
38 ;;; @ utility functions
42 (when (featurep 'xemacs
)
43 (defmacro pgg-run-at-time-1
(time repeat function args
)
44 (if (condition-case nil
45 (let ((delete-itimer 'delete-itimer
)
46 (itimer-driver-start 'itimer-driver-start
)
47 (itimer-value 'itimer-value
)
48 (start-itimer 'start-itimer
))
49 (unless (or (symbol-value 'itimer-process
)
50 (symbol-value 'itimer-timer
))
51 (funcall itimer-driver-start
))
52 ;; Check whether there is a bug to which the difference of
53 ;; the present time and the time when the itimer driver was
54 ;; woken up is subtracted from the initial itimer value.
55 (let* ((inhibit-quit t
)
56 (ctime (current-time))
57 (itimer-timer-last-wakeup
60 (setcar ctime
(1- (car ctime
)))))
62 (itimer (funcall start-itimer
"pgg-run-at-time"
64 (sleep-for 0.1) ;; Accept the timeout interrupt.
66 (> (funcall itimer-value itimer
) 0)
67 (funcall delete-itimer itimer
))))
70 (apply #'start-itimer
"pgg-run-at-time"
71 ,function
(if time
(max time
1e-9) 1e-9)
77 (apply #'start-itimer
"pgg-run-at-time"
78 (lambda (itimers repeat function
&rest args
)
79 (let ((itimer (car itimers
)))
84 (lambda (itimer repeat function
&rest args
)
85 (set-itimer-restart itimer repeat
)
86 (set-itimer-function itimer function
)
87 (set-itimer-function-arguments itimer args
)
88 (apply function args
)))
89 (set-itimer-function-arguments
91 (append (list itimer repeat function
) args
)))
94 (lambda (itimer function
&rest args
)
95 (delete-itimer itimer
)
96 (apply function args
)))
97 (set-itimer-function-arguments
99 (append (list itimer function
) args
)))))
100 1e-9 (if time
(max time
1e-9) 1e-9)
101 nil t itimers
,repeat
,function
,args
)))))))
104 (if (featurep 'xemacs
)
106 (defun pgg-run-at-time (time repeat function
&rest args
)
107 "Emulating function run as `run-at-time'.
108 TIME should be nil meaning now, or a number of seconds from now.
109 Return an itimer object which can be used in either `delete-itimer'
111 (pgg-run-at-time-1 time repeat function args
))
112 (defun pgg-cancel-timer (timer)
113 "Emulate cancel-timer for xemacs."
114 (let ((delete-itimer 'delete-itimer
))
115 (funcall delete-itimer timer
))))
116 (defalias 'pgg-run-at-time
'run-at-time
)
117 (defalias 'pgg-cancel-timer
'cancel-timer
)))
119 (defun pgg-invoke (func scheme
&rest args
)
121 (require (intern (format "pgg-%s" scheme
)))
122 (apply 'funcall
(intern (format "pgg-%s-%s" scheme func
)) args
)))
124 (put 'pgg-save-coding-system
'lisp-indent-function
2)
126 (defmacro pgg-save-coding-system
(start end
&rest body
)
127 `(if (called-interactively-p 'interactive
)
128 (let ((buffer (current-buffer)))
130 (let (buffer-undo-list)
131 (insert-buffer-substring buffer
,start
,end
)
132 (encode-coding-region (point-min)(point-max)
133 buffer-file-coding-system
)
134 (prog1 (save-excursion ,@body
)
135 (push nil buffer-undo-list
)
136 (ignore-errors (undo))))))
138 (narrow-to-region ,start
,end
)
141 (defun pgg-temp-buffer-show-function (buffer)
142 (let ((window (or (get-buffer-window buffer
'visible
)
143 (split-window-vertically))))
144 (set-window-buffer window buffer
)
145 (shrink-window-if-larger-than-buffer window
)))
147 ;; XXX `pgg-display-output-buffer' is a horrible name for this function.
148 ;; It should be something like `pgg-situate-output-or-display-error'.
149 (defun pgg-display-output-buffer (start end status
)
150 "Situate en/decryption results or pop up an error buffer.
152 Text from START to END is replaced by contents of output buffer if STATUS
153 is true, or else the output buffer is displayed."
155 (pgg-situate-output start end
)
156 (pgg-display-error-buffer)))
158 (defun pgg-situate-output (start end
)
159 "Place en/decryption result in place of current text from START to END."
160 (delete-region start end
)
161 (insert-buffer-substring pgg-output-buffer
)
162 (decode-coding-region start
(point) buffer-file-coding-system
))
164 (defun pgg-display-error-buffer ()
165 "Pop up an error buffer indicating the reason for an en/decryption failure."
166 (let ((temp-buffer-show-function
167 (function pgg-temp-buffer-show-function
)))
168 (with-output-to-temp-buffer pgg-echo-buffer
169 (set-buffer standard-output
)
170 (insert-buffer-substring pgg-errors-buffer
))))
172 (defvar pgg-passphrase-cache
(make-vector 7 0))
174 (defvar pgg-pending-timers
(make-vector 7 0)
175 "Hash table for managing scheduled pgg cache management timers.
177 We associate key and timer, so the timer can be canceled if a new
178 timeout for the key is set while an old one is still pending.")
180 (defun pgg-read-passphrase (prompt &optional key notruncate
)
181 "Using PROMPT, obtain passphrase for KEY from cache or user.
183 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
186 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
187 regulate cache behavior."
188 (or (pgg-read-passphrase-from-cache key notruncate
)
189 (read-passwd prompt
)))
191 (defun pgg-read-passphrase-from-cache (key &optional notruncate
)
192 "Obtain passphrase for KEY from time-limited passphrase cache.
194 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
197 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
198 regulate cache behavior."
199 (and pgg-cache-passphrase
201 (setq key
(pgg-truncate-key-identifier key
)))
202 (symbol-value (intern-soft key pgg-passphrase-cache
))))
204 (defun pgg-add-passphrase-to-cache (key passphrase
&optional notruncate
)
205 "Associate KEY with PASSPHRASE in time-limited passphrase cache.
207 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
210 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
211 regulate cache behavior."
213 (let* ((key (if notruncate key
(pgg-truncate-key-identifier key
)))
214 (interned-timer-key (intern-soft key pgg-pending-timers
))
215 (old-timer (symbol-value interned-timer-key
))
218 (cancel-timer old-timer
)
219 (unintern interned-timer-key pgg-pending-timers
))
220 (set (intern key pgg-passphrase-cache
)
222 (set (intern key pgg-pending-timers
)
223 (pgg-run-at-time pgg-passphrase-cache-expiry nil
224 #'pgg-remove-passphrase-from-cache
227 (if (fboundp 'clear-string
)
228 (defalias 'pgg-clear-string
'clear-string
)
229 (defun pgg-clear-string (string)
230 (fillarray string ?_
)))
232 (declare-function pgg-clear-string
"pgg" (string))
234 (defun pgg-remove-passphrase-from-cache (key &optional notruncate
)
235 "Omit passphrase associated with KEY in time-limited passphrase cache.
237 Truncate the key to 8 trailing characters unless NOTRUNCATE is true
240 This is a no-op if there is not entry for KEY (eg, it's already expired.
242 The memory for the passphrase is filled with underscores to clear any
245 Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry'
246 regulate cache behavior."
247 (let* ((passphrase (pgg-read-passphrase-from-cache key notruncate
))
248 (key (if notruncate key
(pgg-truncate-key-identifier key
)))
249 (interned-timer-key (intern-soft key pgg-pending-timers
))
250 (old-timer (symbol-value interned-timer-key
)))
252 (pgg-clear-string passphrase
)
253 (unintern key pgg-passphrase-cache
))
255 (pgg-cancel-timer old-timer
)
256 (unintern interned-timer-key pgg-pending-timers
))))
258 (defmacro pgg-convert-lbt-region
(start end lbt
)
259 `(let ((pgg-conversion-end (set-marker (make-marker) ,end
)))
265 (> (marker-position pgg-conversion-end
) (point)))
269 (while (re-search-forward "\r$" pgg-conversion-end t
)
270 (replace-match ""))))))
272 (put 'pgg-as-lbt
'lisp-indent-function
3)
274 (defmacro pgg-as-lbt
(start end lbt
&rest body
)
275 `(let ((inhibit-read-only t
)
278 (pgg-convert-lbt-region ,start
,end
,lbt
)
279 (let ((,end
(point)))
281 (push nil buffer-undo-list
)
282 (ignore-errors (undo))))
284 (put 'pgg-process-when-success
'lisp-indent-function
0)
286 (defmacro pgg-process-when-success
(&rest body
)
287 `(with-current-buffer pgg-output-buffer
288 (if (zerop (buffer-size)) nil
,@body t
)))
290 (defalias 'pgg-make-temp-file
291 (if (fboundp 'make-temp-file
)
293 (lambda (prefix &optional dir-flag
)
294 (let ((file (expand-file-name
295 (make-temp-name prefix
)
296 (if (fboundp 'temp-directory
)
298 temporary-file-directory
))))
300 (make-directory file
))
303 ;;; @ interface functions
307 (defun pgg-encrypt-region (start end rcpts
&optional sign passphrase
)
308 "Encrypt the current region between START and END for RCPTS.
310 If optional argument SIGN is non-nil, do a combined sign and encrypt.
312 If optional PASSPHRASE is not specified, it will be obtained from the
313 passphrase cache or user."
315 (list (region-beginning)(region-end)
316 (split-string (read-string "Recipients: ") "[ \t,]+")))
318 (pgg-save-coding-system start end
319 (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme
)
320 (point-min) (point-max) rcpts sign passphrase
))))
321 (when (called-interactively-p 'interactive
)
322 (pgg-display-output-buffer start end status
))
326 (defun pgg-encrypt-symmetric-region (start end
&optional passphrase
)
327 "Encrypt the current region between START and END symmetric with passphrase.
329 If optional PASSPHRASE is not specified, it will be obtained from the
333 (pgg-save-coding-system start end
334 (pgg-invoke "encrypt-symmetric-region"
335 (or pgg-scheme pgg-default-scheme
)
336 (point-min) (point-max) passphrase
))))
337 (when (called-interactively-p 'interactive
)
338 (pgg-display-output-buffer start end status
))
342 (defun pgg-encrypt-symmetric (&optional start end passphrase
)
343 "Encrypt the current buffer using a symmetric, rather than key-pair, cipher.
345 If optional arguments START and END are specified, only encrypt within
348 If optional PASSPHRASE is not specified, it will be obtained from the
349 passphrase cache or user."
351 (let* ((start (or start
(point-min)))
352 (end (or end
(point-max)))
353 (status (pgg-encrypt-symmetric-region start end passphrase
)))
354 (when (called-interactively-p 'interactive
)
355 (pgg-display-output-buffer start end status
))
359 (defun pgg-encrypt (rcpts &optional sign start end passphrase
)
360 "Encrypt the current buffer for RCPTS.
362 If optional argument SIGN is non-nil, do a combined sign and encrypt.
364 If optional arguments START and END are specified, only encrypt within
367 If optional PASSPHRASE is not specified, it will be obtained from the
368 passphrase cache or user."
369 (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+")))
370 (let* ((start (or start
(point-min)))
371 (end (or end
(point-max)))
372 (status (pgg-encrypt-region start end rcpts sign passphrase
)))
373 (when (called-interactively-p 'interactive
)
374 (pgg-display-output-buffer start end status
))
378 (defun pgg-decrypt-region (start end
&optional passphrase
)
379 "Decrypt the current region between START and END.
381 If optional PASSPHRASE is not specified, it will be obtained from the
382 passphrase cache or user."
384 (let* ((buf (current-buffer))
386 (pgg-save-coding-system start end
387 (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme
)
388 (point-min) (point-max) passphrase
))))
389 (when (called-interactively-p 'interactive
)
390 (pgg-display-output-buffer start end status
))
394 (defun pgg-decrypt (&optional start end passphrase
)
395 "Decrypt the current buffer.
397 If optional arguments START and END are specified, only decrypt within
400 If optional PASSPHRASE is not specified, it will be obtained from the
401 passphrase cache or user."
403 (let* ((start (or start
(point-min)))
404 (end (or end
(point-max)))
405 (status (pgg-decrypt-region start end passphrase
)))
406 (when (called-interactively-p 'interactive
)
407 (pgg-display-output-buffer start end status
))
411 (defun pgg-sign-region (start end
&optional cleartext passphrase
)
412 "Make the signature from text between START and END.
414 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
415 a detached signature.
417 If this function is called interactively, CLEARTEXT is enabled
418 and the output is displayed.
420 If optional PASSPHRASE is not specified, it will be obtained from the
421 passphrase cache or user."
423 (let ((status (pgg-save-coding-system start end
424 (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme
)
425 (point-min) (point-max)
426 (or (called-interactively-p 'interactive
)
429 (when (called-interactively-p 'interactive
)
430 (pgg-display-output-buffer start end status
))
434 (defun pgg-sign (&optional cleartext start end passphrase
)
435 "Sign the current buffer.
437 If the optional argument CLEARTEXT is non-nil, it does not create a
440 If optional arguments START and END are specified, only sign data
443 If this function is called interactively, CLEARTEXT is enabled
444 and the output is displayed.
446 If optional PASSPHRASE is not specified, it will be obtained from the
447 passphrase cache or user."
449 (let* ((start (or start
(point-min)))
450 (end (or end
(point-max)))
451 (status (pgg-sign-region start end
452 (or (called-interactively-p 'interactive
)
455 (when (called-interactively-p 'interactive
)
456 (pgg-display-output-buffer start end status
))
460 (defun pgg-verify-region (start end
&optional signature fetch
)
461 "Verify the current region between START and END.
462 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
463 the detached signature of the current region.
465 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
466 signer's public key from `pgg-default-keyserver-address'."
469 (if (null signature
) nil
471 (buffer-disable-undo)
472 (unless (featurep 'xemacs
)
473 (set-buffer-multibyte nil
))
474 (insert-file-contents signature
)
475 (cdr (assq 2 (pgg-decode-armor-region
476 (point-min)(point-max)))))))
477 (key (cdr (assq 'key-identifier packet
)))
481 (setq key
(concat "0x" (pgg-truncate-key-identifier key
)))
482 (null (pgg-lookup-key key
))
483 (or fetch
(called-interactively-p 'interactive
))
484 (y-or-n-p (format "Key %s not found; attempt to fetch? " key
))
486 (or (cdr (assq 'preferred-key-server packet
))
487 pgg-default-keyserver-address
))
488 (pgg-fetch-key keyserver key
))
490 (pgg-save-coding-system start end
491 (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme
)
492 (point-min) (point-max) signature
)))
493 (when (called-interactively-p 'interactive
)
494 (let ((temp-buffer-show-function
495 (function pgg-temp-buffer-show-function
)))
496 (with-output-to-temp-buffer pgg-echo-buffer
497 (set-buffer standard-output
)
498 (insert-buffer-substring (if status pgg-output-buffer
499 pgg-errors-buffer
)))))
503 (defun pgg-verify (&optional signature fetch start end
)
504 "Verify the current buffer.
505 If the optional argument SIGNATURE is non-nil, it is treated as
506 the detached signature of the current region.
507 If the optional argument FETCH is non-nil, we attempt to fetch the
508 signer's public key from `pgg-default-keyserver-address'.
509 If optional arguments START and END are specified, only verify data
512 (let* ((start (or start
(point-min)))
513 (end (or end
(point-max)))
514 (status (pgg-verify-region start end signature fetch
)))
515 (when (called-interactively-p 'interactive
)
516 (let ((temp-buffer-show-function
517 (function pgg-temp-buffer-show-function
)))
518 (with-output-to-temp-buffer pgg-echo-buffer
519 (set-buffer standard-output
)
520 (insert-buffer-substring (if status pgg-output-buffer
521 pgg-errors-buffer
)))))
525 (defun pgg-insert-key ()
526 "Insert the ASCII armored public key."
528 (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme
)))
531 (defun pgg-snarf-keys-region (start end
)
532 "Import public keys in the current region between START and END."
534 (pgg-save-coding-system start end
535 (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme
)
539 (defun pgg-snarf-keys ()
540 "Import public keys in the current buffer."
542 (pgg-snarf-keys-region (point-min) (point-max)))
544 (defun pgg-lookup-key (string &optional type
)
545 (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme
) string type
))
547 (defvar pgg-insert-url-function
(function pgg-insert-url-with-w3
))
549 (defun pgg-insert-url-with-w3 (url)
552 (let (buffer-file-name)
553 (url-insert-file-contents url
))))
555 (defvar pgg-insert-url-extra-arguments nil
)
556 (defvar pgg-insert-url-program nil
)
558 (defun pgg-insert-url-with-program (url)
559 (let ((args (copy-sequence pgg-insert-url-extra-arguments
))
564 (apply #'start-process
" *PGG url*" (current-buffer)
565 pgg-insert-url-program
(nconc args
(list url
))))
566 (set-process-sentinel process
#'ignore
)
567 (while (eq 'run
(process-status process
))
568 (accept-process-output process
5))
569 (delete-process process
)
570 (if (and process
(eq 'run
(process-status process
)))
571 (interrupt-process process
))
574 (defun pgg-fetch-key (keyserver key
)
575 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
576 (with-current-buffer (get-buffer-create pgg-output-buffer
)
577 (buffer-disable-undo)
579 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver
)
580 (substring keyserver
0 (1- (match-end 0))))))
582 (funcall pgg-insert-url-function
584 (format "http://%s:11371/pks/lookup?op=get&search=%s"
586 (when (re-search-forward "^-+BEGIN" nil
'last
)
587 (delete-region (point-min) (match-beginning 0))
588 (when (re-search-forward "^-+END" nil t
)
589 (delete-region (progn (end-of-line) (point))
593 (insert-buffer-substring pgg-output-buffer
)
594 (pgg-snarf-keys-region (point-min)(point-max)))))))