1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
3 ;; Copyright (C) 1999 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
7 ;; Keywords: PGP, OpenPGP
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.
28 (eval-when-compile (require 'pgg))
31 "PGP 2.* and 6.* interface"
34 (defcustom pgg-pgp-program "pgp"
35 "PGP 2.* and 6.* executable."
39 (defcustom pgg-pgp-shell-file-name "/bin/sh"
40 "File name to load inferior shells from. Bourne shell or its equivalent
41 \(not tcsh) is needed for \"2>\"."
45 (defcustom pgg-pgp-shell-command-switch "-c"
46 "Switch used to have the shell execute its command line argument."
50 (defcustom pgg-pgp-extra-args nil
51 "Extra arguments for every PGP invocation."
56 (luna-define-class pgg-scheme-pgp (pgg-scheme))
59 (defvar pgg-pgp-user-id nil
60 "GnuPG ID of your default identity.")
62 (defvar pgg-scheme-pgp-instance nil)
65 (defun pgg-make-scheme-pgp ()
66 (or pgg-scheme-pgp-instance
67 (setq pgg-scheme-pgp-instance
68 (luna-make-entity 'pgg-scheme-pgp))))
70 (defun pgg-pgp-process-region (start end passphrase program args)
71 (let* ((errors-file-name
72 (concat temporary-file-directory
73 (make-temp-name "pgg-errors")))
77 (list (concat "2>" errors-file-name))))
78 (shell-file-name pgg-pgp-shell-file-name)
79 (shell-command-switch pgg-pgp-shell-command-switch)
80 (output-buffer pgg-output-buffer)
81 (errors-buffer pgg-errors-buffer)
82 (process-connection-type nil)
83 process status exit-status)
84 (with-current-buffer (get-buffer-create output-buffer)
88 (setenv "PGPPASSFD" "0"))
91 (apply #'start-process-shell-command "*PGP*" output-buffer
93 (set-process-sentinel process 'ignore)
95 (process-send-string process (concat passphrase "\n")))
96 (process-send-region process start end)
97 (process-send-eof process)
98 (while (eq 'run (process-status process))
99 (accept-process-output process 5))
100 (setq status (process-status process)
101 exit-status (process-exit-status process))
102 (delete-process process)
103 (with-current-buffer output-buffer
104 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
106 (if (memq status '(stop signal))
107 (error "%s exited abnormally: '%s'" program exit-status))
108 (if (= 127 exit-status)
109 (error "%s could not be found" program))
111 (set-buffer (get-buffer-create errors-buffer))
112 (buffer-disable-undo)
114 (insert-file-contents errors-file-name)
115 (delete-file errors-file-name)
117 (if (and process (eq 'run (process-status process)))
118 (interrupt-process process))
122 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp)
123 string &optional type)
124 (let ((args (list "+batchmode" "+language=en" "-kv" string)))
125 (with-current-buffer (get-buffer-create pgg-output-buffer)
126 (buffer-disable-undo)
128 (apply #'call-process pgg-pgp-program nil t args)
129 (goto-char (point-min))
131 ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
132 (buffer-substring (point)(+ 8 (point))))
133 ((re-search-forward "^Type" nil t);PGP 6.*
134 (beginning-of-line 2)
137 (buffer-substring (point)
138 (progn (end-of-line) (point)))
143 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp)
144 start end recipients)
145 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
147 `("+encrypttoself=off +verbose=1" "+batchmode"
148 "+language=us" "-fate"
150 (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
152 (if pgg-encrypt-for-me
153 (list pgg-pgp-user-id))))))
155 (pgg-pgp-process-region start end nil
156 pgg-pgp-program args)
157 (pgg-process-when-success nil)
160 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp)
162 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
165 (format "PGP passphrase for %s: " pgg-pgp-user-id)
166 (luna-send scheme 'lookup-key-string
167 scheme pgg-pgp-user-id 'encrypt)))
169 '("+verbose=1" "+batchmode" "+language=us" "-f")))
170 (pgg-pgp-process-region start end passphrase
171 pgg-pgp-program args)
172 (pgg-process-when-success nil)
175 (luna-define-method sign-region ((scheme pgg-scheme-pgp)
176 start end &optional clearsign)
177 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
180 (format "PGP passphrase for %s: " pgg-pgp-user-id)
181 (luna-send scheme 'lookup-key-string
182 scheme pgg-pgp-user-id 'sign)))
184 (list (if clearsign "-fast" "-fbast")
185 "+verbose=1" "+language=us" "+batchmode"
186 "-u" pgg-pgp-user-id)))
187 (pgg-pgp-process-region start end passphrase
188 pgg-pgp-program args)
189 (pgg-process-when-success
190 (goto-char (point-min))
191 (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
193 (cdr (assq 2 (pgg-parse-armor-region
194 (progn (beginning-of-line 2)
197 (if pgg-cache-passphrase
198 (pgg-add-passphrase-cache
199 (cdr (assq 'key-identifier packet))
203 (luna-define-method verify-region ((scheme pgg-scheme-pgp)
204 start end &optional signature)
205 (let* ((basename (expand-file-name "pgg" temporary-file-directory))
206 (orig-file (make-temp-name basename))
207 (args '("+verbose=1" "+batchmode" "+language=us"))
208 (orig-mode (default-file-modes)))
211 (set-default-file-modes 448)
212 (write-region-as-binary start end orig-file)
214 (set-default-file-modes orig-mode))
215 (when (stringp signature)
216 (copy-file signature (setq signature (concat orig-file ".asc")))
217 (setq args (append args (list signature orig-file)))
219 (pgg-pgp-process-region (point-min)(point-max) nil
220 pgg-pgp-program args)
221 (delete-file orig-file)
222 (if signature (delete-file signature))
223 (pgg-process-when-success
224 (goto-char (point-min))
225 (let ((case-fold-search t))
226 (while (re-search-forward "^warning: " nil t)
227 (delete-region (match-beginning 0)
228 (progn (beginning-of-line 2) (point)))))
229 (goto-char (point-min))
230 (when (re-search-forward "^\\.$" nil t)
231 (delete-region (point-min)
232 (progn (beginning-of-line 2)
236 (luna-define-method insert-key ((scheme pgg-scheme-pgp))
237 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
239 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
240 (concat "\"" pgg-pgp-user-id "\""))))
241 (pgg-pgp-process-region (point)(point) nil
242 pgg-pgp-program args)
243 (insert-buffer-substring pgg-output-buffer)
246 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp)
248 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
249 (basename (expand-file-name "pgg" temporary-file-directory))
250 (key-file (make-temp-name basename))
252 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
254 (write-region-as-raw-text-CRLF start end key-file)
255 (pgg-pgp-process-region start end nil
256 pgg-pgp-program args)
257 (delete-file key-file)
258 (pgg-process-when-success nil)
263 ;;; pgg-pgp.el ends here