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