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 "The GnuPG executable."
44 (defcustom pgg-pgp-extra-args nil
45 "Extra arguments for every PGP invocation."
50 (luna-define-class pgg-scheme-pgp (pgg-scheme))
53 (defvar pgg-pgp-user-id nil
54 "GnuPG ID of your default identity.")
56 (defvar pgg-scheme-pgp-instance nil)
59 (defun pgg-make-scheme-pgp ()
60 (or pgg-scheme-pgp-instance
61 (setq pgg-scheme-pgp-instance
62 (luna-make-entity 'pgg-scheme-pgp))))
64 (defun pgg-pgp-process-region (start end passphrase program args)
65 (let* ((errors-file-name
66 (concat temporary-file-directory
67 (make-temp-name "pgg-errors")))
71 (list (concat "2>" errors-file-name))))
72 (shell-file-name pgg-pgp-shell-file-name)
73 (output-buffer pgg-output-buffer)
74 (errors-buffer pgg-errors-buffer)
75 (process-connection-type nil)
76 process status exit-status)
77 (with-current-buffer (get-buffer-create output-buffer)
81 (setenv "PGPPASSFD" "0"))
83 (apply #'start-process-shell-command "*PGP*" output-buffer
85 (set-process-sentinel process 'ignore)
87 (process-send-string process (concat passphrase "\n")))
88 (process-send-region process start end)
89 (process-send-eof process)
90 (while (eq 'run (process-status process))
91 (accept-process-output process 5))
92 (setq status (process-status process)
93 exit-status (process-exit-status process))
94 (delete-process process)
95 (with-current-buffer output-buffer
96 (goto-char (point-min))
97 (while (search-forward "\r$" nil t)
99 (if (memq status '(stop signal))
100 (error "%s exited abnormally: '%s'" program exit-status))
101 (if (= 127 exit-status)
102 (error "%s could not be found" program))
104 (set-buffer (get-buffer-create errors-buffer))
105 (buffer-disable-undo)
107 (insert-file-contents errors-file-name)
108 (delete-file errors-file-name)
110 (if (and process (eq 'run (process-status process)))
111 (interrupt-process process))
115 (luna-define-method lookup-key-string ((scheme pgg-scheme-pgp)
116 string &optional type)
117 (let ((args (list "+batchmode" "+language=en" "-kv" string)))
118 (with-current-buffer (get-buffer-create pgg-output-buffer)
119 (buffer-disable-undo)
121 (apply #'call-process pgg-pgp-program nil t args)
122 (goto-char (point-min))
124 ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
125 (buffer-substring (point)(+ 8 (point))))
126 ((re-search-forward "^Type" nil t);PGP 6.*
127 (beginning-of-line 2)
130 (buffer-substring (point)
131 (progn (end-of-line) (point)))
136 (luna-define-method encrypt-region ((scheme pgg-scheme-pgp)
137 start end recipients)
138 (let* ((pgg-pgp-user-id pgg-default-user-id)
141 (format "PGP passphrase for %s: " pgg-pgp-user-id)
142 (luna-send scheme 'lookup-key-string
143 scheme pgg-pgp-user-id 'encrypt)))
145 `("+encrypttoself=off +verbose=1" "+batchmode"
146 "+language=us" "-fate"
148 (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
150 (pgg-pgp-process-region start end passphrase
151 pgg-pgp-program args)
152 (pgg-process-when-success
154 (cdr (assq 1 (pgg-parse-armor-region
155 (point-min)(point-max))))))
156 (pgg-add-passphrase-cache
157 (cdr (assq 'key-identifier packet))
161 (luna-define-method decrypt-region ((scheme pgg-scheme-pgp)
163 (let* ((pgg-pgp-user-id pgg-default-user-id)
166 (format "PGP passphrase for %s: " pgg-pgp-user-id)
167 (luna-send scheme 'lookup-key-string
168 scheme pgg-pgp-user-id 'encrypt)))
170 '("+verbose=1" "+batchmode" "+language=us" "-f")))
171 (pgg-pgp-process-region start end passphrase
172 pgg-pgp-program args)
173 (pgg-process-when-success nil)
176 (luna-define-method sign-region ((scheme pgg-scheme-pgp)
177 start end &optional clearsign)
178 (let* ((pgg-pgp-user-id pgg-default-user-id)
181 (format "PGP passphrase for %s: " pgg-pgp-user-id)
182 (luna-send scheme 'lookup-key-string
183 scheme pgg-pgp-user-id 'sign)))
185 (list (if clearsign "-fast" "-fbast")
186 "+verbose=1" "+language=us" "+batchmode"
187 "-u" pgg-pgp-user-id)))
188 (pgg-pgp-process-region start end passphrase
189 pgg-pgp-program args)
190 (pgg-process-when-success
191 (goto-char (point-min))
192 (while (re-search-forward "\r$" end t)
194 (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
196 (cdr (assq 2 (pgg-parse-armor-region
197 (progn (beginning-of-line 2)
200 (pgg-add-passphrase-cache
201 (cdr (assq 'key-identifier packet))
205 (luna-define-method verify-region ((scheme pgg-scheme-pgp)
206 start end &optional signature)
207 (let* ((basename (expand-file-name "pgg" temporary-file-directory))
208 (orig-file (make-temp-name basename))
209 (args '("+verbose=1" "+batchmode" "+language=us")))
210 (write-region-as-binary start end orig-file)
211 (when (stringp signature)
212 (copy-file signature (setq signature (concat orig-file ".asc")))
213 (setq args (append args (list signature orig-file)))
215 (pgg-pgp-process-region (point-min)(point-max) nil
216 pgg-pgp-program args)
217 (delete-file orig-file)
218 (if signature (delete-file signature))
219 (pgg-process-when-success nil)
222 (luna-define-method insert-key ((scheme pgg-scheme-pgp))
223 (let* ((pgg-pgp-user-id pgg-default-user-id)
225 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
226 (concat "\"" pgg-pgp-user-id "\""))))
227 (pgg-pgp-process-region (point)(point) nil
228 pgg-pgp-program args)
229 (insert-buffer-substring pgg-output-buffer)
232 (luna-define-method snarf-keys-region ((scheme pgg-scheme-pgp)
234 (let* ((pgg-pgp-user-id pgg-default-user-id)
235 (basename (expand-file-name "pgg" temporary-file-directory))
236 (key-file (make-temp-name basename))
238 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
240 (write-region-as-raw-text-CRLF start end key-file)
241 (pgg-pgp-process-region start end nil
242 pgg-pgp-program args)
243 (delete-file key-file)
248 ;;; pgg-pgp.el ends here