1 ;;; pgg.el --- glue for the various PGP implementations.
3 ;; Copyright (C) 1999 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)
52 ((memq (cdr s-field) field-value)
55 (define-calist-field-match-method 'signature-version
56 #'pgg-field-match-method-with-containment)
58 (define-calist-field-match-method 'symmetric-key-algorithm
59 #'pgg-field-match-method-with-containment)
61 (define-calist-field-match-method 'public-key-algorithm
62 #'pgg-field-match-method-with-containment)
64 (define-calist-field-match-method 'hash-algorithm
65 #'pgg-field-match-method-with-containment)
67 (defvar pgg-verify-condition nil
68 "Condition-tree about which PGP implementation is used for verifying.")
70 (defvar pgg-decrypt-condition nil
71 "Condition-tree about which PGP implementation is used for decrypting.")
73 (ctree-set-calist-strictly
75 '((signature-version 3)(public-key-algorithm RSA)(hash-algorithm MD5)
78 (ctree-set-calist-strictly
79 'pgg-decrypt-condition
80 '((public-key-algorithm RSA)(symmetric-key-algorithm IDEA)
83 (ctree-set-calist-strictly
85 '((signature-version 3 4)
86 (public-key-algorithm RSA ELG DSA)
87 (hash-algorithm MD5 SHA1 RIPEMD160)
90 (ctree-set-calist-strictly
91 'pgg-decrypt-condition
92 '((public-key-algorithm RSA ELG DSA)
93 (symmetric-key-algorithm 3DES CAST5 IDEA)
96 (ctree-set-calist-strictly
98 '((signature-version 3 4)
99 (public-key-algorithm ELG-E DSA ELG)
100 (hash-algorithm MD5 SHA1 RIPEMD160)
103 (ctree-set-calist-strictly
104 'pgg-decrypt-condition
105 '((public-key-algorithm ELG-E DSA ELG)
106 (symmetric-key-algorithm 3DES CAST5 BLOWFISH TWOFISH)
109 ;;; @ definition of the implementation scheme
113 (luna-define-class pgg-scheme ())
115 (luna-define-internal-accessors 'pgg-scheme)
118 (luna-define-generic lookup-key-string (scheme string &optional type)
119 "Search keys associated with STRING")
121 (luna-define-generic encrypt-region (scheme start end recipients)
122 "Encrypt the current region between START and END.")
124 (luna-define-generic decrypt-region (scheme start end)
125 "Decrypt the current region between START and END.")
127 (luna-define-generic sign-region (scheme start end &optional cleartext)
128 "Make detached signature from text between START and END.")
130 (luna-define-generic verify-region (scheme start end &optional signature)
131 "Verify region between START and END
132 as the detached signature SIGNATURE.")
134 (luna-define-generic insert-key (scheme)
135 "Insert public key at point.")
137 (luna-define-generic snarf-keys-region (scheme start end)
138 "Add all public keys in region between START
139 and END to the keyring.")
141 ;;; @ utility functions
144 (defvar pgg-fetch-key-function (function pgg-fetch-key-with-w3))
146 (defmacro pgg-make-scheme (scheme)
148 (require (intern (format "pgg-%s" ,scheme)))
149 (funcall (intern (format "pgg-make-scheme-%s"
152 (put 'pgg-save-coding-system 'lisp-indent-function 2)
154 (defmacro pgg-save-coding-system (start end &rest body)
156 (let ((buffer (current-buffer)))
158 (let (buffer-undo-list)
159 (insert-buffer-substring buffer ,start ,end)
160 (encode-coding-region (point-min)(point-max)
161 buffer-file-coding-system)
162 (prog1 (save-excursion ,@body)
163 (push nil buffer-undo-list)
164 (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)
185 (let ((temp-buffer-show-function
186 (function pgg-temp-buffer-show-function)))
187 (with-output-to-temp-buffer pgg-echo-buffer
188 (set-buffer standard-output)
189 (insert-buffer-substring pgg-errors-buffer)))
192 (defvar pgg-passphrase-cache-expiry 16)
193 (defvar pgg-passphrase-cache (make-vector 7 0))
195 (defvar pgg-read-passphrase nil)
196 (defun pgg-read-passphrase (prompt &optional key)
197 (if (not pgg-read-passphrase)
198 (if (functionp 'read-passwd)
199 (setq pgg-read-passphrase 'read-passwd)
200 (if (load "passwd" t)
201 (setq pgg-read-passphrase 'read-passwd)
202 (autoload 'ange-ftp-read-passwd "ange-ftp")
203 (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
204 (or (and pgg-cache-passphrase
205 key (setq key (pgg-truncate-key-identifier key))
206 (symbol-value (intern-soft key pgg-passphrase-cache)))
207 (funcall pgg-read-passphrase prompt)))
209 (defun pgg-add-passphrase-cache (key passphrase)
210 (setq key (pgg-truncate-key-identifier key))
211 (set (intern key pgg-passphrase-cache)
213 (run-at-time pgg-passphrase-cache-expiry nil
214 #'pgg-remove-passphrase-cache
217 (defun pgg-remove-passphrase-cache (key)
218 (let ((passphrase (symbol-value (intern-soft key pgg-passphrase-cache))))
220 (fillarray passphrase ?_)
221 (unintern key pgg-passphrase-cache))))
223 (defmacro pgg-convert-lbt-region (start end lbt)
224 `(let ((pgg-conversion-end (set-marker (make-marker) ,end)))
230 (> (marker-position pgg-conversion-end) (point)))
234 (while (re-search-forward "\r$" pgg-conversion-end t)
235 (replace-match ""))))
238 (put 'pgg-as-lbt 'lisp-indent-function 3)
240 (defmacro pgg-as-lbt (start end lbt &rest body)
241 `(let ((inhibit-read-only t)
244 (pgg-convert-lbt-region ,start ,end ,lbt)
245 (let ((,end (point)))
247 (push nil buffer-undo-list)
248 (ignore-errors (undo))))
250 (put 'pgg-process-when-success 'lisp-indent-function 0)
252 (defmacro pgg-process-when-success (&rest body)
253 `(with-current-buffer pgg-output-buffer
254 (if (zerop (buffer-size)) nil ,@body t)))
257 ;;; @ interface functions
261 (defun pgg-encrypt-region (start end rcpts)
262 "Encrypt the current region between START and END for RCPTS."
264 (list (region-beginning)(region-end)
265 (split-string (read-string "Recipients: ") "[ \t,]+")))
266 (let* ((entity (pgg-make-scheme pgg-default-scheme))
267 (status (pgg-save-coding-system start end
268 (luna-send entity 'encrypt-region entity
269 (point-min)(point-max) rcpts))))
270 (when (interactive-p)
271 (pgg-display-output-buffer start end status))
275 (defun pgg-decrypt-region (start end)
276 "Decrypt the current region between START and END."
278 (let* ((packet (cdr (assq 1 (pgg-parse-armor-region start end))))
283 (in-calist-package 'pgg)
284 (ctree-match-calist pgg-decrypt-condition
287 (entity (pgg-make-scheme scheme))
288 (status (pgg-save-coding-system start end
289 (luna-send entity 'decrypt-region entity
290 (point-min)(point-max)))))
291 (when (interactive-p)
292 (pgg-display-output-buffer start end status))
296 (defun pgg-sign-region (start end &optional cleartext)
297 "Make the signature from text between START and END.
298 If the optional 3rd argument CLEARTEXT is non-nil, it does not create
299 a detached signature."
301 (let* ((entity (pgg-make-scheme pgg-default-scheme))
302 (status (pgg-save-coding-system start end
303 (luna-send entity 'sign-region entity
304 (point-min)(point-max)
305 (or (interactive-p) cleartext)))))
306 (when (interactive-p)
307 (pgg-display-output-buffer start end status))
311 (defun pgg-verify-region (start end &optional signature fetch)
312 "Verify the current region between START and END.
313 If the optional 3rd argument SIGNATURE is non-nil, it is treated as
314 the detached signature of the current region.
316 If the optional 4th argument FETCH is non-nil, we attempt to fetch the
317 signer's public key from `pgg-default-keyserver-address'."
320 (if (null signature) nil
322 (buffer-disable-undo)
323 (set-buffer-multibyte nil)
324 (insert-file-contents signature)
325 (cdr (assq 2 (pgg-decode-armor-region (point-min)(point-max))))
331 (in-calist-package 'pgg)
332 (ctree-match-calist pgg-verify-condition
335 (entity (pgg-make-scheme scheme))
336 (key (cdr (assq 'key-identifier packet)))
339 (setq key (concat "0x" (pgg-truncate-key-identifier key)))
340 (null (let ((pgg-scheme scheme))
341 (pgg-lookup-key-string key)))
342 (or fetch (interactive-p))
343 (y-or-n-p (format "Key %s not found; attempt to fetch? " key))
345 (or (cdr (assq 'preferred-key-server packet))
346 pgg-default-keyserver-address))
347 (pgg-fetch-key keyserver key))
348 (setq status (pgg-save-coding-system start end
349 (luna-send entity 'verify-region entity
350 (point-min)(point-max) signature)))
351 (when (interactive-p)
352 (let ((temp-buffer-show-function
353 (function pgg-temp-buffer-show-function)))
354 (with-output-to-temp-buffer pgg-echo-buffer
355 (set-buffer standard-output)
356 (insert-buffer-substring (if status pgg-output-buffer
362 (defun pgg-insert-key ()
363 "Insert the ASCII armored public key."
365 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
366 (luna-send entity 'insert-key entity)))
369 (defun pgg-snarf-keys-region (start end)
370 "Import public keys in the current region between START and END."
372 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
373 (pgg-save-coding-system start end
374 (luna-send entity 'snarf-keys-region entity start end))))
376 (defun pgg-lookup-key-string (string &optional type)
377 (let ((entity (pgg-make-scheme (or pgg-scheme pgg-default-scheme))))
378 (luna-send entity 'lookup-key-string entity string type)))
380 (defvar pgg-insert-url-function (function pgg-insert-url-with-w3))
382 (defun pgg-insert-url-with-w3 (url)
385 (let (buffer-file-name)
386 (url-insert-file-contents url)))
388 (defvar pgg-insert-url-extra-arguments nil)
389 (defvar pgg-insert-url-program nil)
391 (defun pgg-insert-url-with-program (url)
392 (let ((args (copy-sequence pgg-insert-url-extra-arguments))
397 (apply #'start-process " *PGG url*" (current-buffer)
398 pgg-insert-url-program (nconc args (list url))))
399 (set-process-sentinel process #'ignore)
400 (while (eq 'run (process-status process))
401 (accept-process-output process 5))
402 (delete-process process)
403 (if (and process (eq 'run (process-status process)))
404 (interrupt-process process))
408 (defun pgg-fetch-key (keyserver key)
409 "Attempt to fetch a KEY from KEYSERVER for addition to PGP or GnuPG keyring."
410 (with-current-buffer (get-buffer-create pgg-output-buffer)
411 (buffer-disable-undo)
413 (let ((proto (if (string-match "^[a-zA-Z\\+\\.\\\\-]+:" keyserver)
414 (substring keyserver 0 (1- (match-end 0))))))
416 (funcall pgg-insert-url-function
418 (format "http://%s:11371/pks/lookup?op=get&search=%s"
420 (when (re-search-forward "^-+BEGIN" nil 'last)
421 (delete-region (point-min) (match-beginning 0))
422 (when (re-search-forward "^-+END" nil t)
423 (delete-region (progn (end-of-line) (point))
427 (insert-buffer-substring pgg-output-buffer)
428 (pgg-snarf-keys-region (point-min)(point-max))))