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.
30 (eval-and-compile (require 'luna))
35 (in-calist-package 'pgg)
37 (defun pgg-field-match-method-with-containment
38 (calist field-type field-value)
39 (let ((s-field (assq field-type calist)))
41 (cons (cons field-type field-value) calist)
43 ((memq (cdr s-field) field-value)
46 (define-calist-field-match-method 'signature-version
47 #'pgg-field-match-method-with-containment)
49 (define-calist-field-match-method 'cipher-algorithm
50 #'pgg-field-match-method-with-containment)
52 (define-calist-field-match-method 'public-key-algorithm
53 #'pgg-field-match-method-with-containment)
55 (define-calist-field-match-method 'hash-algorithm
56 #'pgg-field-match-method-with-containment)
58 (defvar pgg-verify-codition nil
59 "Condition-tree about how to display entity.")
61 (defvar pgg-decrypt-codition nil
62 "Condition-tree about how to display entity.")
64 (ctree-set-calist-strictly
66 '((signature-version 3)(public-key-algorithm RSA)(hash-algorithm MD5)
69 (ctree-set-calist-strictly
71 '((cipher-algorithm IDEA)(public-key-algorithm RSA)
74 (ctree-set-calist-strictly
76 '((signature-version 3 4)
77 (public-key-algorithm RSA ELG DSA)
78 (hash-algorithm MD5 SHA1 RIPEMD160)
81 (ctree-set-calist-strictly
83 '((cipher-algorithm 3DES CAST5 IDEA)
84 (public-key-algorithm RSA ELG DSA)
87 (ctree-set-calist-strictly
89 '((signature-version 3 4)
90 (public-key-algorithm ELG-E DSA ELG)
91 (hash-algorithm MD5 SHA1 RIPEMD160)
94 (ctree-set-calist-strictly
96 '((public-key-algorithm ELG-E DSA ELG)
97 (cipher-algorithm 3DES CAST5 BLOWFISH TWOFISH)
100 ;;; @ definition of the implementation scheme
104 (luna-define-class pgg-scheme ()
105 (message-beginning-line
107 signed-beginning-line
113 (luna-define-internal-accessors 'pgg-scheme)
116 (luna-define-generic encrypt-region (scheme start end recipients)
117 "Encrypt the current region between START and END.")
119 (luna-define-generic decrypt-region (scheme start end)
120 "Decrypt the current region between START and END.")
122 (luna-define-generic sign-region (scheme start end)
123 "Make detached signature from text between START and END.")
125 (luna-define-generic verify-region (scheme start end &optional signature)
126 "Verify region between START and END
127 as the detached signature SIGNATURE.")
129 (luna-define-generic insert-key (scheme)
130 "Insert public key at point.")
132 (luna-define-generic snarf-keys-region (scheme start end)
133 "Add all public keys in region between START
134 and END to the keyring.")
136 (defvar pgg-scheme-message-delimiters
137 '(:message-beginning-line
138 "^-----BEGIN PGP MESSAGE-----\r?$"
140 "^-----END PGP MESSAGE-----\r?$"
141 :signed-beginning-line
142 "^-----BEGIN PGP SIGNED MESSAGE-----\r?$"
144 "^-----END PGP SIGNATURE-----\r?$"
146 "^-----BEGIN PGP PUBLIC KEY BLOCK-----\r?$"
148 "^-----END PGP PUBLIC KEY BLOCK-----\r?$")
149 "Message delimiters")
151 (luna-define-method initialize-instance :before ((scheme pgg-scheme)
153 (let ((luna-current-method-arguments
154 (cons scheme (or init-args
155 pgg-scheme-message-delimiters))))
156 (luna-call-next-method)))
158 ;;; @ interface functions
161 (defmacro pgg-make-scheme (scheme)
163 (require (intern (format "pgg-%s" ,scheme)))
164 (funcall (intern (format "pgg-make-scheme-%s"
167 (defun pgg-encrypt-region (start end rcpts)
168 (let ((entity (pgg-make-scheme pgg-default-scheme)))
169 (luna-send entity 'encrypt-region entity start end rcpts)))
171 (defun pgg-decrypt-region (start end)
172 (let* ((packets (pgg-parse-armor-region start end))
177 (in-calist-package 'pgg)
178 (ctree-match-calist pgg-decrypt-codition
181 (entity (pgg-make-scheme scheme)))
182 (luna-send entity 'decrypt-region entity start end)))
184 (defun pgg-sign-region (start end)
185 (let ((entity (pgg-make-scheme pgg-default-scheme)))
186 (luna-send entity 'sign-region entity start end)))
188 (defun pgg-verify-region (start end &optional signature)
191 (buffer-disable-undo)
192 (set-buffer-multibyte nil)
193 (insert-file-contents signature)
194 (pgg-decode-armor-region (point-min)(point-max))
200 (in-calist-package 'pgg)
201 (ctree-match-calist pgg-verify-codition
204 (entity (pgg-make-scheme scheme)))
205 (luna-send entity 'verify-region entity start end signature)))
207 (defun pgg-insert-key ()
208 (let ((entity (pgg-make-scheme pgg-default-scheme)))
209 (luna-send entity 'insert-key entity)))
211 (defun pgg-snarf-keys-region (start end)
212 (let ((entity (pgg-make-scheme pgg-default-scheme)))
213 (luna-send entity 'snarf-keys-region start end)))
215 ;;; @ utility functions
218 (defvar pgg-passphrase-cache-expiry 16)
219 (defvar pgg-passphrase-cache (make-vector 7 0))
221 (defvar pgg-read-passphrase nil)
222 (defun pgg-read-passphrase (prompt &optional key)
223 (if (not pgg-read-passphrase)
224 (if (functionp 'read-passwd)
225 (setq pgg-read-passphrase 'read-passwd)
226 (if (load "passwd" t)
227 (setq pgg-read-passphrase 'read-passwd)
228 (autoload 'ange-ftp-read-passwd "ange-ftp")
229 (setq pgg-read-passphrase 'ange-ftp-read-passwd))))
230 (or (and key (symbol-value (intern-soft key pgg-passphrase-cache)))
231 (funcall pgg-read-passphrase prompt)))
233 (defun pgg-add-passphrase-cache (key passphrase)
234 (set (intern key pgg-passphrase-cache)
236 (run-at-time pgg-passphrase-cache-expiry nil
237 #'pgg-remove-passphrase-cache
240 (defun pgg-remove-passphrase-cache (key)
241 (unintern key pgg-passphrase-cache))