Commit | Line | Data |
---|---|---|
23f87bed MB |
1 | ;;; pgg.el --- glue for the various PGP implementations. |
2 | ||
e84b4b86 | 3 | ;; Copyright (C) 1999, 2000, 2002, 2003, 2004, |
114f9c96 | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
23f87bed MB |
5 | |
6 | ;; Author: Daiki Ueno <ueno@unixuser.org> | |
7c700bd1 | 7 | ;; Symmetric encryption added by: Sascha Wilde <wilde@sha-bang.de> |
23f87bed MB |
8 | ;; Created: 1999/10/28 |
9 | ;; Keywords: PGP | |
10 | ||
11 | ;; This file is part of GNU Emacs. | |
12 | ||
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
23f87bed | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
23f87bed MB |
17 | |
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. | |
22 | ||
23 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
23f87bed MB |
25 | |
26 | ;;; Commentary: | |
27 | ||
43cf3760 DU |
28 | ;; This file is on its way to obsolescence, waiting for allout.el to |
29 | ;; switch to EPG. | |
30 | ||
23f87bed MB |
31 | ;;; Code: |
32 | ||
33 | (require 'pgg-def) | |
34 | (require 'pgg-parse) | |
35 | (autoload 'run-at-time "timer") | |
36 | ||
37 | ;; Don't merge these two `eval-when-compile's. | |
38 | (eval-when-compile | |
f0b7f5a8 | 39 | ;; For Emacs <22.2 and XEmacs. |
21ee0911 | 40 | (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))) |
23f87bed | 41 | (require 'cl)) |
23f87bed MB |
42 | |
43 | ;;; @ utility functions | |
44 | ;;; | |
45 | ||
af5db4a5 | 46 | (eval-when-compile |
0c43b6f8 KY |
47 | (when (featurep 'xemacs) |
48 | (defmacro pgg-run-at-time-1 (time repeat function args) | |
af5db4a5 GM |
49 | (if (condition-case nil |
50 | (let ((delete-itimer 'delete-itimer) | |
51 | (itimer-driver-start 'itimer-driver-start) | |
52 | (itimer-value 'itimer-value) | |
53 | (start-itimer 'start-itimer)) | |
54 | (unless (or (symbol-value 'itimer-process) | |
55 | (symbol-value 'itimer-timer)) | |
56 | (funcall itimer-driver-start)) | |
57 | ;; Check whether there is a bug to which the difference of | |
58 | ;; the present time and the time when the itimer driver was | |
59 | ;; woken up is subtracted from the initial itimer value. | |
60 | (let* ((inhibit-quit t) | |
61 | (ctime (current-time)) | |
62 | (itimer-timer-last-wakeup | |
63 | (prog1 | |
64 | ctime | |
65 | (setcar ctime (1- (car ctime))))) | |
66 | (itimer-list nil) | |
67 | (itimer (funcall start-itimer "pgg-run-at-time" | |
68 | 'ignore 5))) | |
69 | (sleep-for 0.1) ;; Accept the timeout interrupt. | |
70 | (prog1 | |
71 | (> (funcall itimer-value itimer) 0) | |
72 | (funcall delete-itimer itimer)))) | |
73 | (error nil)) | |
74 | `(let ((time ,time)) | |
75 | (apply #'start-itimer "pgg-run-at-time" | |
76 | ,function (if time (max time 1e-9) 1e-9) | |
0c43b6f8 KY |
77 | ,repeat nil t ,args)) |
78 | `(let ((time ,time) | |
79 | (itimers (list nil))) | |
80 | (setcar | |
81 | itimers | |
82 | (apply #'start-itimer "pgg-run-at-time" | |
83 | (lambda (itimers repeat function &rest args) | |
84 | (let ((itimer (car itimers))) | |
85 | (if repeat | |
86 | (progn | |
87 | (set-itimer-function | |
88 | itimer | |
89 | (lambda (itimer repeat function &rest args) | |
90 | (set-itimer-restart itimer repeat) | |
91 | (set-itimer-function itimer function) | |
92 | (set-itimer-function-arguments itimer args) | |
93 | (apply function args))) | |
94 | (set-itimer-function-arguments | |
95 | itimer | |
96 | (append (list itimer repeat function) args))) | |
97 | (set-itimer-function | |
98 | itimer | |
99 | (lambda (itimer function &rest args) | |
100 | (delete-itimer itimer) | |
101 | (apply function args))) | |
102 | (set-itimer-function-arguments | |
103 | itimer | |
104 | (append (list itimer function) args))))) | |
105 | 1e-9 (if time (max time 1e-9) 1e-9) | |
106 | nil t itimers ,repeat ,function ,args))))))) | |
af5db4a5 | 107 | |
bbbe940b MB |
108 | (eval-and-compile |
109 | (if (featurep 'xemacs) | |
110 | (progn | |
111 | (defun pgg-run-at-time (time repeat function &rest args) | |
112 | "Emulating function run as `run-at-time'. | |
af5db4a5 GM |
113 | TIME should be nil meaning now, or a number of seconds from now. |
114 | Return an itimer object which can be used in either `delete-itimer' | |
115 | or `cancel-timer'." | |
bbbe940b MB |
116 | (pgg-run-at-time-1 time repeat function args)) |
117 | (defun pgg-cancel-timer (timer) | |
118 | "Emulate cancel-timer for xemacs." | |
119 | (let ((delete-itimer 'delete-itimer)) | |
120 | (funcall delete-itimer timer)))) | |
121 | (defalias 'pgg-run-at-time 'run-at-time) | |
122 | (defalias 'pgg-cancel-timer 'cancel-timer))) | |
af5db4a5 | 123 | |
23f87bed MB |
124 | (defun pgg-invoke (func scheme &rest args) |
125 | (progn | |
126 | (require (intern (format "pgg-%s" scheme))) | |
127 | (apply 'funcall (intern (format "pgg-%s-%s" scheme func)) args))) | |
128 | ||
129 | (put 'pgg-save-coding-system 'lisp-indent-function 2) | |
130 | ||
131 | (defmacro pgg-save-coding-system (start end &rest body) | |
32226619 | 132 | `(if (called-interactively-p 'interactive) |
23f87bed MB |
133 | (let ((buffer (current-buffer))) |
134 | (with-temp-buffer | |
135 | (let (buffer-undo-list) | |
136 | (insert-buffer-substring buffer ,start ,end) | |
137 | (encode-coding-region (point-min)(point-max) | |
138 | buffer-file-coding-system) | |
139 | (prog1 (save-excursion ,@body) | |
140 | (push nil buffer-undo-list) | |
141 | (ignore-errors (undo)))))) | |
142 | (save-restriction | |
143 | (narrow-to-region ,start ,end) | |
144 | ,@body))) | |
145 | ||
146 | (defun pgg-temp-buffer-show-function (buffer) | |
147 | (let ((window (or (get-buffer-window buffer 'visible) | |
148 | (split-window-vertically)))) | |
149 | (set-window-buffer window buffer) | |
150 | (shrink-window-if-larger-than-buffer window))) | |
151 | ||
7c700bd1 EZ |
152 | ;; XXX `pgg-display-output-buffer' is a horrible name for this function. |
153 | ;; It should be something like `pgg-situate-output-or-display-error'. | |
23f87bed | 154 | (defun pgg-display-output-buffer (start end status) |
7c700bd1 EZ |
155 | "Situate en/decryption results or pop up an error buffer. |
156 | ||
157 | Text from START to END is replaced by contents of output buffer if STATUS | |
158 | is true, or else the output buffer is displayed." | |
23f87bed | 159 | (if status |
7c700bd1 EZ |
160 | (pgg-situate-output start end) |
161 | (pgg-display-error-buffer))) | |
162 | ||
163 | (defun pgg-situate-output (start end) | |
164 | "Place en/decryption result in place of current text from START to END." | |
165 | (delete-region start end) | |
166 | (insert-buffer-substring pgg-output-buffer) | |
167 | (decode-coding-region start (point) buffer-file-coding-system)) | |
168 | ||
169 | (defun pgg-display-error-buffer () | |
170 | "Pop up an error buffer indicating the reason for an en/decryption failure." | |
171 | (let ((temp-buffer-show-function | |
172 | (function pgg-temp-buffer-show-function))) | |
173 | (with-output-to-temp-buffer pgg-echo-buffer | |
174 | (set-buffer standard-output) | |
175 | (insert-buffer-substring pgg-errors-buffer)))) | |
23f87bed MB |
176 | |
177 | (defvar pgg-passphrase-cache (make-vector 7 0)) | |
178 | ||
7c700bd1 EZ |
179 | (defvar pgg-pending-timers (make-vector 7 0) |
180 | "Hash table for managing scheduled pgg cache management timers. | |
181 | ||
182 | We associate key and timer, so the timer can be cancelled if a new | |
183 | timeout for the key is set while an old one is still pending.") | |
184 | ||
185 | (defun pgg-read-passphrase (prompt &optional key notruncate) | |
186 | "Using PROMPT, obtain passphrase for KEY from cache or user. | |
187 | ||
188 | Truncate the key to 8 trailing characters unless NOTRUNCATE is true | |
189 | \(default false). | |
190 | ||
191 | Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry' | |
192 | regulate cache behavior." | |
193 | (or (pgg-read-passphrase-from-cache key notruncate) | |
23f87bed MB |
194 | (read-passwd prompt))) |
195 | ||
7c700bd1 EZ |
196 | (defun pgg-read-passphrase-from-cache (key &optional notruncate) |
197 | "Obtain passphrase for KEY from time-limited passphrase cache. | |
198 | ||
199 | Truncate the key to 8 trailing characters unless NOTRUNCATE is true | |
200 | \(default false). | |
201 | ||
202 | Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry' | |
203 | regulate cache behavior." | |
204 | (and pgg-cache-passphrase | |
205 | key (or notruncate | |
206 | (setq key (pgg-truncate-key-identifier key))) | |
207 | (symbol-value (intern-soft key pgg-passphrase-cache)))) | |
208 | ||
209 | (defun pgg-add-passphrase-to-cache (key passphrase &optional notruncate) | |
210 | "Associate KEY with PASSPHRASE in time-limited passphrase cache. | |
211 | ||
212 | Truncate the key to 8 trailing characters unless NOTRUNCATE is true | |
213 | \(default false). | |
214 | ||
215 | Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry' | |
216 | regulate cache behavior." | |
217 | ||
218 | (let* ((key (if notruncate key (pgg-truncate-key-identifier key))) | |
219 | (interned-timer-key (intern-soft key pgg-pending-timers)) | |
220 | (old-timer (symbol-value interned-timer-key)) | |
221 | new-timer) | |
222 | (when old-timer | |
223 | (cancel-timer old-timer) | |
224 | (unintern interned-timer-key pgg-pending-timers)) | |
225 | (set (intern key pgg-passphrase-cache) | |
226 | passphrase) | |
227 | (set (intern key pgg-pending-timers) | |
228 | (pgg-run-at-time pgg-passphrase-cache-expiry nil | |
229 | #'pgg-remove-passphrase-from-cache | |
230 | key notruncate)))) | |
231 | ||
8fbdffe5 MB |
232 | (if (fboundp 'clear-string) |
233 | (defalias 'pgg-clear-string 'clear-string) | |
234 | (defun pgg-clear-string (string) | |
235 | (fillarray string ?_))) | |
236 | ||
af5db4a5 GM |
237 | (declare-function pgg-clear-string "pgg" (string)) |
238 | ||
7c700bd1 EZ |
239 | (defun pgg-remove-passphrase-from-cache (key &optional notruncate) |
240 | "Omit passphrase associated with KEY in time-limited passphrase cache. | |
241 | ||
242 | Truncate the key to 8 trailing characters unless NOTRUNCATE is true | |
243 | \(default false). | |
244 | ||
245 | This is a no-op if there is not entry for KEY (eg, it's already expired. | |
246 | ||
247 | The memory for the passphrase is filled with underscores to clear any | |
248 | references to it. | |
249 | ||
250 | Custom variables `pgg-cache-passphrase' and `pgg-passphrase-cache-expiry' | |
251 | regulate cache behavior." | |
252 | (let* ((passphrase (pgg-read-passphrase-from-cache key notruncate)) | |
253 | (key (if notruncate key (pgg-truncate-key-identifier key))) | |
254 | (interned-timer-key (intern-soft key pgg-pending-timers)) | |
255 | (old-timer (symbol-value interned-timer-key))) | |
256 | (when passphrase | |
8fbdffe5 | 257 | (pgg-clear-string passphrase) |
7c700bd1 EZ |
258 | (unintern key pgg-passphrase-cache)) |
259 | (when old-timer | |
260 | (pgg-cancel-timer old-timer) | |
261 | (unintern interned-timer-key pgg-pending-timers)))) | |
262 | ||
23f87bed MB |
263 | (defmacro pgg-convert-lbt-region (start end lbt) |
264 | `(let ((pgg-conversion-end (set-marker (make-marker) ,end))) | |
265 | (goto-char ,start) | |
266 | (case ,lbt | |
267 | (CRLF | |
268 | (while (progn | |
269 | (end-of-line) | |
270 | (> (marker-position pgg-conversion-end) (point))) | |
271 | (insert "\r") | |
272 | (forward-line 1))) | |
273 | (LF | |
274 | (while (re-search-forward "\r$" pgg-conversion-end t) | |
275 | (replace-match "")))))) | |
276 | ||
277 | (put 'pgg-as-lbt 'lisp-indent-function 3) | |
278 | ||
279 | (defmacro pgg-as-lbt (start end lbt &rest body) | |
280 | `(let ((inhibit-read-only t) | |
281 | buffer-read-only | |
282 | buffer-undo-list) | |
283 | (pgg-convert-lbt-region ,start ,end ,lbt) | |
284 | (let ((,end (point))) | |
285 | ,@body) | |
286 | (push nil buffer-undo-list) | |
287 | (ignore-errors (undo)))) | |
288 | ||
289 | (put 'pgg-process-when-success 'lisp-indent-function 0) | |
290 | ||
291 | (defmacro pgg-process-when-success (&rest body) | |
292 | `(with-current-buffer pgg-output-buffer | |
293 | (if (zerop (buffer-size)) nil ,@body t))) | |
294 | ||
295 | (defalias 'pgg-make-temp-file | |
296 | (if (fboundp 'make-temp-file) | |
297 | 'make-temp-file | |
298 | (lambda (prefix &optional dir-flag) | |
299 | (let ((file (expand-file-name | |
300 | (make-temp-name prefix) | |
301 | (if (fboundp 'temp-directory) | |
302 | (temp-directory) | |
303 | temporary-file-directory)))) | |
304 | (if dir-flag | |
305 | (make-directory file)) | |
306 | file)))) | |
307 | ||
308 | ;;; @ interface functions | |
309 | ;;; | |
310 | ||
311 | ;;;###autoload | |
7c700bd1 | 312 | (defun pgg-encrypt-region (start end rcpts &optional sign passphrase) |
23f87bed | 313 | "Encrypt the current region between START and END for RCPTS. |
7c700bd1 EZ |
314 | |
315 | If optional argument SIGN is non-nil, do a combined sign and encrypt. | |
316 | ||
317 | If optional PASSPHRASE is not specified, it will be obtained from the | |
318 | passphrase cache or user." | |
23f87bed MB |
319 | (interactive |
320 | (list (region-beginning)(region-end) | |
321 | (split-string (read-string "Recipients: ") "[ \t,]+"))) | |
322 | (let ((status | |
323 | (pgg-save-coding-system start end | |
324 | (pgg-invoke "encrypt-region" (or pgg-scheme pgg-default-scheme) | |
7c700bd1 | 325 | (point-min) (point-max) rcpts sign passphrase)))) |
32226619 | 326 | (when (called-interactively-p 'interactive) |
7c700bd1 EZ |
327 | (pgg-display-output-buffer start end status)) |
328 | status)) | |
329 | ||
330 | ;;;###autoload | |
331 | (defun pgg-encrypt-symmetric-region (start end &optional passphrase) | |
332 | "Encrypt the current region between START and END symmetric with passphrase. | |
333 | ||
334 | If optional PASSPHRASE is not specified, it will be obtained from the | |
335 | cache or user." | |
336 | (interactive "r") | |
337 | (let ((status | |
338 | (pgg-save-coding-system start end | |
1941dba2 | 339 | (pgg-invoke "encrypt-symmetric-region" |
7c700bd1 EZ |
340 | (or pgg-scheme pgg-default-scheme) |
341 | (point-min) (point-max) passphrase)))) | |
32226619 | 342 | (when (called-interactively-p 'interactive) |
23f87bed MB |
343 | (pgg-display-output-buffer start end status)) |
344 | status)) | |
345 | ||
346 | ;;;###autoload | |
7c700bd1 EZ |
347 | (defun pgg-encrypt-symmetric (&optional start end passphrase) |
348 | "Encrypt the current buffer using a symmetric, rather than key-pair, cipher. | |
349 | ||
350 | If optional arguments START and END are specified, only encrypt within | |
351 | the region. | |
352 | ||
353 | If optional PASSPHRASE is not specified, it will be obtained from the | |
354 | passphrase cache or user." | |
355 | (interactive) | |
356 | (let* ((start (or start (point-min))) | |
357 | (end (or end (point-max))) | |
358 | (status (pgg-encrypt-symmetric-region start end passphrase))) | |
32226619 | 359 | (when (called-interactively-p 'interactive) |
7c700bd1 EZ |
360 | (pgg-display-output-buffer start end status)) |
361 | status)) | |
362 | ||
363 | ;;;###autoload | |
364 | (defun pgg-encrypt (rcpts &optional sign start end passphrase) | |
23f87bed | 365 | "Encrypt the current buffer for RCPTS. |
7c700bd1 | 366 | |
23f87bed | 367 | If optional argument SIGN is non-nil, do a combined sign and encrypt. |
7c700bd1 | 368 | |
23f87bed | 369 | If optional arguments START and END are specified, only encrypt within |
7c700bd1 EZ |
370 | the region. |
371 | ||
372 | If optional PASSPHRASE is not specified, it will be obtained from the | |
373 | passphrase cache or user." | |
23f87bed MB |
374 | (interactive (list (split-string (read-string "Recipients: ") "[ \t,]+"))) |
375 | (let* ((start (or start (point-min))) | |
376 | (end (or end (point-max))) | |
7c700bd1 | 377 | (status (pgg-encrypt-region start end rcpts sign passphrase))) |
32226619 | 378 | (when (called-interactively-p 'interactive) |
23f87bed MB |
379 | (pgg-display-output-buffer start end status)) |
380 | status)) | |
381 | ||
382 | ;;;###autoload | |
7c700bd1 EZ |
383 | (defun pgg-decrypt-region (start end &optional passphrase) |
384 | "Decrypt the current region between START and END. | |
385 | ||
386 | If optional PASSPHRASE is not specified, it will be obtained from the | |
387 | passphrase cache or user." | |
23f87bed MB |
388 | (interactive "r") |
389 | (let* ((buf (current-buffer)) | |
390 | (status | |
391 | (pgg-save-coding-system start end | |
392 | (pgg-invoke "decrypt-region" (or pgg-scheme pgg-default-scheme) | |
7c700bd1 | 393 | (point-min) (point-max) passphrase)))) |
32226619 | 394 | (when (called-interactively-p 'interactive) |
23f87bed MB |
395 | (pgg-display-output-buffer start end status)) |
396 | status)) | |
397 | ||
398 | ;;;###autoload | |
7c700bd1 | 399 | (defun pgg-decrypt (&optional start end passphrase) |
23f87bed | 400 | "Decrypt the current buffer. |
7c700bd1 | 401 | |
23f87bed | 402 | If optional arguments START and END are specified, only decrypt within |
7c700bd1 EZ |
403 | the region. |
404 | ||
405 | If optional PASSPHRASE is not specified, it will be obtained from the | |
406 | passphrase cache or user." | |
23f87bed MB |
407 | (interactive "") |
408 | (let* ((start (or start (point-min))) | |
409 | (end (or end (point-max))) | |
7c700bd1 | 410 | (status (pgg-decrypt-region start end passphrase))) |
32226619 | 411 | (when (called-interactively-p 'interactive) |
23f87bed MB |
412 | (pgg-display-output-buffer start end status)) |
413 | status)) | |
414 | ||
415 | ;;;###autoload | |
7c700bd1 | 416 | (defun pgg-sign-region (start end &optional cleartext passphrase) |
23f87bed | 417 | "Make the signature from text between START and END. |
7c700bd1 | 418 | |
23f87bed MB |
419 | If the optional 3rd argument CLEARTEXT is non-nil, it does not create |
420 | a detached signature. | |
7c700bd1 | 421 | |
23f87bed | 422 | If this function is called interactively, CLEARTEXT is enabled |
1941dba2 | 423 | and the output is displayed. |
7c700bd1 EZ |
424 | |
425 | If optional PASSPHRASE is not specified, it will be obtained from the | |
426 | passphrase cache or user." | |
23f87bed MB |
427 | (interactive "r") |
428 | (let ((status (pgg-save-coding-system start end | |
429 | (pgg-invoke "sign-region" (or pgg-scheme pgg-default-scheme) | |
430 | (point-min) (point-max) | |
32226619 JB |
431 | (or (called-interactively-p 'interactive) |
432 | cleartext) | |
7c700bd1 | 433 | passphrase)))) |
32226619 | 434 | (when (called-interactively-p 'interactive) |
23f87bed MB |
435 | (pgg-display-output-buffer start end status)) |
436 | status)) | |
437 | ||
438 | ;;;###autoload | |
7c700bd1 | 439 | (defun pgg-sign (&optional cleartext start end passphrase) |
23f87bed | 440 | "Sign the current buffer. |
7c700bd1 | 441 | |
23f87bed MB |
442 | If the optional argument CLEARTEXT is non-nil, it does not create a |
443 | detached signature. | |
7c700bd1 | 444 | |
23f87bed MB |
445 | If optional arguments START and END are specified, only sign data |
446 | within the region. | |
7c700bd1 | 447 | |
23f87bed | 448 | If this function is called interactively, CLEARTEXT is enabled |
1941dba2 | 449 | and the output is displayed. |
7c700bd1 EZ |
450 | |
451 | If optional PASSPHRASE is not specified, it will be obtained from the | |
452 | passphrase cache or user." | |
23f87bed MB |
453 | (interactive "") |
454 | (let* ((start (or start (point-min))) | |
455 | (end (or end (point-max))) | |
7c700bd1 | 456 | (status (pgg-sign-region start end |
32226619 JB |
457 | (or (called-interactively-p 'interactive) |
458 | cleartext) | |
7c700bd1 | 459 | passphrase))) |
32226619 | 460 | (when (called-interactively-p 'interactive) |
23f87bed MB |
461 | (pgg-display-output-buffer start end status)) |
462 | status)) | |
7c700bd1 | 463 | |
23f87bed MB |
464 | ;;;###autoload |
465 | (defun pgg-verify-region (start end &optional signature fetch) | |
466 | "Verify the current region between START and END. | |
467 | If the optional 3rd argument SIGNATURE is non-nil, it is treated as | |
468 | the detached signature of the current region. | |
469 | ||
470 | If the optional 4th argument FETCH is non-nil, we attempt to fetch the | |
471 | signer's public key from `pgg-default-keyserver-address'." | |
472 | (interactive "r") | |
473 | (let* ((packet | |
474 | (if (null signature) nil | |
475 | (with-temp-buffer | |
476 | (buffer-disable-undo) | |
765d4319 KY |
477 | (unless (featurep 'xemacs) |
478 | (set-buffer-multibyte nil)) | |
23f87bed MB |
479 | (insert-file-contents signature) |
480 | (cdr (assq 2 (pgg-decode-armor-region | |
481 | (point-min)(point-max))))))) | |
482 | (key (cdr (assq 'key-identifier packet))) | |
483 | status keyserver) | |
484 | (and (stringp key) | |
485 | pgg-query-keyserver | |
486 | (setq key (concat "0x" (pgg-truncate-key-identifier key))) | |
487 | (null (pgg-lookup-key key)) | |
32226619 | 488 | (or fetch (called-interactively-p 'interactive)) |
23f87bed MB |
489 | (y-or-n-p (format "Key %s not found; attempt to fetch? " key)) |
490 | (setq keyserver | |
491 | (or (cdr (assq 'preferred-key-server packet)) | |
492 | pgg-default-keyserver-address)) | |
493 | (pgg-fetch-key keyserver key)) | |
1941dba2 | 494 | (setq status |
23f87bed MB |
495 | (pgg-save-coding-system start end |
496 | (pgg-invoke "verify-region" (or pgg-scheme pgg-default-scheme) | |
497 | (point-min) (point-max) signature))) | |
32226619 | 498 | (when (called-interactively-p 'interactive) |
23f87bed MB |
499 | (let ((temp-buffer-show-function |
500 | (function pgg-temp-buffer-show-function))) | |
501 | (with-output-to-temp-buffer pgg-echo-buffer | |
502 | (set-buffer standard-output) | |
503 | (insert-buffer-substring (if status pgg-output-buffer | |
504 | pgg-errors-buffer))))) | |
505 | status)) | |
506 | ||
507 | ;;;###autoload | |
508 | (defun pgg-verify (&optional signature fetch start end) | |
509 | "Verify the current buffer. | |
510 | If the optional argument SIGNATURE is non-nil, it is treated as | |
511 | the detached signature of the current region. | |
512 | If the optional argument FETCH is non-nil, we attempt to fetch the | |
513 | signer's public key from `pgg-default-keyserver-address'. | |
514 | If optional arguments START and END are specified, only verify data | |
515 | within the region." | |
516 | (interactive "") | |
517 | (let* ((start (or start (point-min))) | |
518 | (end (or end (point-max))) | |
519 | (status (pgg-verify-region start end signature fetch))) | |
32226619 | 520 | (when (called-interactively-p 'interactive) |
23f87bed MB |
521 | (let ((temp-buffer-show-function |
522 | (function pgg-temp-buffer-show-function))) | |
523 | (with-output-to-temp-buffer pgg-echo-buffer | |
524 | (set-buffer standard-output) | |
525 | (insert-buffer-substring (if status pgg-output-buffer | |
84861437 MB |
526 | pgg-errors-buffer))))) |
527 | status)) | |
23f87bed MB |
528 | |
529 | ;;;###autoload | |
530 | (defun pgg-insert-key () | |
531 | "Insert the ASCII armored public key." | |
532 | (interactive) | |
533 | (pgg-invoke "insert-key" (or pgg-scheme pgg-default-scheme))) | |
534 | ||
535 | ;;;###autoload | |
536 | (defun pgg-snarf-keys-region (start end) | |
537 | "Import public keys in the current region between START and END." | |
538 | (interactive "r") | |
539 | (pgg-save-coding-system start end | |
540 | (pgg-invoke "snarf-keys-region" (or pgg-scheme pgg-default-scheme) | |
541 | start end))) | |
542 | ||
543 | ;;;###autoload | |
544 | (defun pgg-snarf-keys () | |
545 | "Import public keys in the current buffer." | |
546 | (interactive "") | |
547 | (pgg-snarf-keys-region (point-min) (point-max))) | |
548 | ||
549 | (defun pgg-lookup-key (string &optional type) | |
550 | (pgg-invoke "lookup-key" (or pgg-scheme pgg-default-scheme) string type)) | |
551 | ||
552 | (defvar pgg-insert-url-function (function pgg-insert-url-with-w3)) | |
553 | ||
554 | (defun pgg-insert-url-with-w3 (url) | |
555 | (ignore-errors | |
23f87bed MB |
556 | (require 'url) |
557 | (let (buffer-file-name) | |
558 | (url-insert-file-contents url)))) | |
559 | ||
560 | (defvar pgg-insert-url-extra-arguments nil) | |
561 | (defvar pgg-insert-url-program nil) | |
562 | ||
563 | (defun pgg-insert-url-with-program (url) | |
564 | (let ((args (copy-sequence pgg-insert-url-extra-arguments)) | |
565 | process) | |
566 | (insert | |
567 | (with-temp-buffer | |
568 | (setq process | |
569 | (apply #'start-process " *PGG url*" (current-buffer) | |
570 | pgg-insert-url-program (nconc args (list url)))) | |
571 | (set-process-sentinel process #'ignore) | |
572 | (while (eq 'run (process-status process)) | |
573 | (accept-process-output process 5)) | |
574 | (delete-process process) | |
575 | (if (and process (eq 'run (process-status process))) | |
576 | (interrupt-process process)) | |
577 | (buffer-string))))) | |
578 | ||
579 | (defun pgg-fetch-key (keyserver key) | |
580 | "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring." | |
581 | (with-current-buffer (get-buffer-create pgg-output-buffer) | |
582 | (buffer-disable-undo) | |
583 | (erase-buffer) | |
584 | (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver) | |
585 | (substring keyserver 0 (1- (match-end 0)))))) | |
586 | (save-excursion | |
587 | (funcall pgg-insert-url-function | |
588 | (if proto keyserver | |
589 | (format "http://%s:11371/pks/lookup?op=get&search=%s" | |
590 | keyserver key)))) | |
591 | (when (re-search-forward "^-+BEGIN" nil 'last) | |
592 | (delete-region (point-min) (match-beginning 0)) | |
593 | (when (re-search-forward "^-+END" nil t) | |
594 | (delete-region (progn (end-of-line) (point)) | |
595 | (point-max))) | |
596 | (insert "\n") | |
597 | (with-temp-buffer | |
598 | (insert-buffer-substring pgg-output-buffer) | |
599 | (pgg-snarf-keys-region (point-min)(point-max))))))) | |
600 | ||
601 | ||
602 | (provide 'pgg) | |
603 | ||
23f87bed | 604 | ;;; pgg.el ends here |