1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999,2000 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
9 ;; This file is part of SEMI (Secure Emacs MIME Interface).
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.
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.
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.
34 (eval-and-compile (require 'luna))
44 (in-calist-package 'pgg)
46 (defun pgg-field-match-method-with-containment
47 (calist field-type field-value)
48 (let ((s-field (assq field-type calist)))
50 (cons (cons field-type field-value) calist))
51 ((memq (cdr s-field) field-value)
54 (define-calist-field-match-method 'signature-version
55 #'pgg-field-match-method-with-containment)
57 (define-calist-field-match-method 'symmetric-key-algorithm
58 #'pgg-field-match-method-with-containment)
60 (define-calist-field-match-method 'public-key-algorithm
61 #'pgg-field-match-method-with-containment)
63 (define-calist-field-match-method 'hash-algorithm
64 #'pgg-field-match-method-with-containment)
66 (defvar pgg-verify-condition nil
67 "Condition-tree about which PGP implementation is used for verifying.")
69 (defvar pgg-decrypt-condition nil
70 "Condition-tree about which PGP implementation is used for decrypting.")
72 (ctree-set-calist-strictly
74 '((signature-version 3)(public-key-algorithm RSA)(hash-algorithm MD5)
77 (ctree-set-calist-strictly
78 'pgg-decrypt-condition
79 '((public-key-algorithm RSA)(symmetric-key-algorithm IDEA)
82 (ctree-set-calist-strictly
84 '((signature-version 3 4)
85 (public-key-algorithm RSA ELG DSA)
86 (hash-algorithm MD5 SHA1 RIPEMD160)
89 (ctree-set-calist-strictly
90 'pgg-decrypt-condition
91 '((public-key-algorithm RSA ELG DSA)
92 (symmetric-key-algorithm 3DES CAST5 IDEA)
95 (ctree-set-calist-strictly
97 '((signature-version 3 4)
98 (public-key-algorithm ELG-E DSA ELG)
99 (hash-algorithm MD5 SHA1 RIPEMD160)
102 (ctree-set-calist-strictly
103 'pgg-decrypt-condition
104 '((public-key-algorithm ELG-E DSA ELG)
105 (symmetric-key-algorithm 3DES CAST5 BLOWFISH TWOFISH)
108 ;;; @ definition of the implementation scheme
112 (luna-define-class pgg-scheme ())
114 (luna-define-internal-accessors 'pgg-scheme))
116 (luna-define-generic pgg-scheme-lookup-key-string
117 (scheme string &optional type)
118 "Search keys associated with STRING")
120 (luna-define-generic pgg-scheme-encrypt-region (scheme start end recipients)
121 "Encrypt the current region between START and END.")
123 (luna-define-generic pgg-scheme-decrypt-region (scheme start end)
124 "Decrypt the current region between START and END.")
126 (luna-define-generic pgg-scheme-sign-region
127 (scheme start end &optional cleartext)
128 "Make detached signature from text between START and END.")
130 (luna-define-generic pgg-scheme-verify-region
131 (scheme start end &optional signature)
132 "Verify region between START and END
133 as the detached signature SIGNATURE.")
135 (luna-define-generic pgg-scheme-insert-key (scheme)
136 "Insert public key at point.")
138 (luna-define-generic pgg-scheme-snarf-keys-region (scheme start end)
139 "Add all public keys in region between START
140 and END to the keyring.")
142 ;;; @ utility functions
145 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
147 (defmacro pgg-make-scheme (scheme)
149 (require (intern (format "pgg-%s" ,scheme)))
150 (funcall (intern (format "pgg-make-scheme-%s"
153 (put 'pgg-save-coding-system 'lisp-indent-function 2)
155 (defmacro pgg-save-coding-system (start end &rest body)
157 (let ((buffer (current-buffer)))
159 (let (buffer-undo-list)
160 (insert-buffer-substring buffer ,start ,end)
161 (encode-coding-region (point-min)(point-max)
162 buffer-file-coding-system)
163 (prog1 (save-excursion ,@body)
164 (push nil buffer-undo-list)
165 (ignore-errors (undo))))))
167 (narrow-to-region ,start ,end)
170 (defun pgg-temp-buffer-show-function (buffer)
171 (if (one-window-p (selected-window))
172 (let ((window (split-window-vertically
174 (/ (window-height) 5)))))
175 (set-window-buffer window buffer))
176 (display-buffer buffer)))
178 (defun pgg-display-output-buffer (start end status)
181 (delete-region start end)
182 (insert-buffer-substring pgg-output-buffer)
183 (decode-coding-region start (point) buffer-file-coding-system))
184 (let ((temp-buffer-show-function
185 (function pgg-temp-buffer-show-function)))
186 (with-output-to-temp-buffer pgg-echo-buffer
187 (set-buffer standard-output)
188 (insert-buffer-substring pgg-errors-buffer)))))
190 (defvar pgg-passphrase-cache-expiry 16)
191 (defvar pgg-passphrase-cache (make-vector 7 0))
193 (defvar pgg-read-passphrase nil)
194 (defun pgg-read-passphrase (prompt &optional key)
195 (if (not pgg-read-passphrase)
196 (if (functionp 'read-passwd)
197 (setq pgg-read-passphrase 'read-passwd)
198 (if (load "passwd" t)
199 (setq pgg-read-passphrase 'read-passwd)
200 (autoload 'ange-ftp-read-passwd "ange-ftp")
201 (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
202 (or (and pgg-cache-passphrase
203 key (setq key (pgg-truncate-key-identifier key))
204 (symbol-value (intern-soft key pgg-passphrase-cache)))
205 (funcall pgg-read-passphrase prompt)))
207 (defun pgg-add-passphrase-cache (key passphrase)
208 (setq key (pgg-truncate-key-identifier key))
209 (set (intern key pgg-passphrase-cache)
211 (run-at-time pgg-passphrase-cache-expiry nil
212 #'pgg-remove-passphrase-cache
215 (defun pgg-remove-passphrase-cache (key)
216 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
218 (fillarray passphrase ?_)
219 (unintern key pgg-passphrase-cache))))
221 (defmacro pgg-convert-lbt-region (start end lbt)
222 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
228 (> (marker-position pgg-conversion-end) (point)))
232 (while (re-search-forward "\r$" pgg-conversion-end t)
233 (replace-match ""))))))
235 (put 'pgg-as-lbt 'lisp-indent-function 3)
237 (defmacro pgg-as-lbt (start end lbt &rest body)
238 `(let ((inhibit-read-only t)
241 (pgg-convert-lbt-region ,start ,end ,lbt)
242 (let ((,end (point)))
244 (push nil buffer-undo-list)
245 (ignore-errors (undo))))
247 (put 'pgg-process-when-success 'lisp-indent-function 0)
249 (defmacro pgg-process-when-success (&rest body)
250 `(with-current-buffer pgg-output-buffer
251 (if (zerop (buffer-size)) nil ,@body t)))
254 ;;; @ interface functions
258 (defun pgg-encrypt-region (start end rcpts)
259 "Encrypt the current region between START and END for RCPTS."
261 (list (region-beginning)(region-end)
262 (split-string (read-string "Recipients: ") "[ \t,]+")))
263 (let* ((entity (pgg-make-scheme pgg-default-scheme))
265 (pgg-save-coding-system start end
266 (pgg-scheme-encrypt-region entity (point-min)(point-max) rcpts))))
267 (when (interactive-p)
268 (pgg-display-output-buffer start end status))
272 (defun pgg-decrypt-region (start end)
273 "Decrypt the current region between START and END."
275 (let* ((packet (cdr (assq 1 (pgg-parse-armor-region start end))))
280 (in-calist-package 'pgg)
281 (ctree-match-calist pgg-decrypt-condition
284 (entity (pgg-make-scheme scheme))
286 (pgg-save-coding-system start end
287 (pgg-scheme-decrypt-region entity (point-min)(point-max)))))
288 (when (interactive-p)
289 (pgg-display-output-buffer start end status))
293 (defun pgg-sign-region (start end &optional cleartext)
294 "Make the signature from text between START and END.
295 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
296 a detached signature."
298 (let* ((entity (pgg-make-scheme pgg-default-scheme))
299 (status (pgg-save-coding-system start end
300 (pgg-scheme-sign-region entity (point-min)(point-max)
301 (or (interactive-p) cleartext)))))
302 (when (interactive-p)
303 (pgg-display-output-buffer start end status))
307 (defun pgg-verify-region (start end &optional signature fetch)
308 "Verify the current region between START and END.
309 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
310 the detached signature of the current region.
312 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
313 signer's public key from `pgg-default-keyserver-address'."
316 (if (null signature) nil
318 (buffer-disable-undo)
319 (set-buffer-multibyte nil)
320 (insert-file-contents signature)
321 (cdr (assq 2 (pgg-decode-armor-region
322 (point-min)(point-max)))))))
327 (in-calist-package 'pgg)
328 (ctree-match-calist pgg-verify-condition
331 (entity (pgg-make-scheme scheme))
332 (key (cdr (assq 'key-identifier packet)))
335 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
336 (null (let ((pgg-scheme scheme))
337 (pgg-lookup-key-string key)))
338 (or fetch (interactive-p))
339 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
341 (or (cdr (assq 'preferred-key-server packet))
342 pgg-default-keyserver-address))
343 (pgg-fetch-key keyserver key))
344 (setq status (pgg-save-coding-system start end
345 (pgg-scheme-verify-region entity (point-min)(point-max)
347 (when (interactive-p)
348 (let ((temp-buffer-show-function
349 (function pgg-temp-buffer-show-function)))
350 (with-output-to-temp-buffer pgg-echo-buffer
351 (set-buffer standard-output)
352 (insert-buffer-substring (if status pgg-output-buffer
353 pgg-errors-buffer)))))
357 (defun pgg-insert-key ()
358 "Insert the ASCII armored public key."
360 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
361 (pgg-scheme-insert-key entity)))
364 (defun pgg-snarf-keys-region (start end)
365 "Import public keys in the current region between START and END."
367 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
368 (pgg-save-coding-system start end
369 (pgg-scheme-snarf-keys-region entity start end))))
371 (defun pgg-lookup-key-string (string &optional type)
372 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
373 (pgg-scheme-lookup-key-string entity string type)))
375 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
377 (defun pgg-insert-url-with-w3 (url)
380 (let (buffer-file-name)
381 (url-insert-file-contents url)))
383 (defvar pgg-insert-url-extra-arguments nil)
384 (defvar pgg-insert-url-program nil)
386 (defun pgg-insert-url-with-program (url)
387 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
392 (apply #'start-process " *PGG url*" (current-buffer)
393 pgg-insert-url-program (nconc args (list url))))
394 (set-process-sentinel process #'ignore)
395 (while (eq 'run (process-status process))
396 (accept-process-output process 5))
397 (delete-process process)
398 (if (and process (eq 'run (process-status process)))
399 (interrupt-process process))
402 (defun pgg-fetch-key (keyserver key)
403 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
404 (with-current-buffer (get-buffer-create pgg-output-buffer)
405 (buffer-disable-undo)
407 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
408 (substring keyserver 0 (1- (match-end 0))))))
410 (funcall pgg-insert-url-function
412 (format "http://%s:11371/pks/lookup?op=get&search=%s"
414 (when (re-search-forward "^-+BEGIN" nil 'last)
415 (delete-region (point-min) (match-beginning 0))
416 (when (re-search-forward "^-+END" nil t)
417 (delete-region (progn (end-of-line) (point))
421 (insert-buffer-substring pgg-output-buffer)
422 (pgg-snarf-keys-region (point-min)(point-max)))))))