1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
3 ;; Copyright (C) 1999, 2000, 2003 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Keywords: PGP, OpenPGP
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU 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.
29 (require 'cl) ; for pgg macros
33 "PGP 2.* and 6.* interface"
36 (defcustom pgg-pgp-program "pgp"
37 "PGP 2.* and 6.* executable."
41 (defcustom pgg-pgp-shell-file-name "/bin/sh"
42 "File name to load inferior shells from.
43 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
47 (defcustom pgg-pgp-shell-command-switch "-c"
48 "Switch used to have the shell execute its command line argument."
52 (defcustom pgg-pgp-extra-args nil
53 "Extra arguments for every PGP invocation."
56 (const :tag "None" nil)
57 (string :tag "Arguments")))
59 (defvar pgg-pgp-user-id nil
60 "PGP ID of your default identity.")
62 (defun pgg-pgp-process-region (start end passphrase program args)
63 (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
67 (list (concat "2>" errors-file-name))))
68 (shell-file-name pgg-pgp-shell-file-name)
69 (shell-command-switch pgg-pgp-shell-command-switch)
70 (process-environment process-environment)
71 (output-buffer pgg-output-buffer)
72 (errors-buffer pgg-errors-buffer)
73 (process-connection-type nil)
74 process status exit-status)
75 (with-current-buffer (get-buffer-create output-buffer)
79 (setenv "PGPPASSFD" "0"))
82 (let ((coding-system-for-read 'binary)
83 (coding-system-for-write 'binary))
86 #'start-process-shell-command "*PGP*" output-buffer
88 (set-process-sentinel process #'ignore)
90 (process-send-string process (concat passphrase "\n")))
91 (process-send-region process start end)
92 (process-send-eof process)
93 (while (eq 'run (process-status process))
94 (accept-process-output process 5))
95 (setq status (process-status process)
96 exit-status (process-exit-status process))
97 (delete-process process)
98 (with-current-buffer output-buffer
99 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
101 (if (memq status '(stop signal))
102 (error "%s exited abnormally: '%s'" program exit-status))
103 (if (= 127 exit-status)
104 (error "%s could not be found" program))
106 (set-buffer (get-buffer-create errors-buffer))
107 (buffer-disable-undo)
109 (insert-file-contents errors-file-name)))
110 (if (and process (eq 'run (process-status process)))
111 (interrupt-process process))
113 (delete-file errors-file-name)
116 (defun pgg-pgp-lookup-key (string &optional type)
117 "Search keys associated with STRING."
118 (let ((args (list "+batchmode" "+language=en" "-kv" string)))
119 (with-current-buffer (get-buffer-create pgg-output-buffer)
120 (buffer-disable-undo)
122 (apply #'call-process pgg-pgp-program nil t nil args)
123 (goto-char (point-min))
125 ((re-search-forward "^pub\\s +[0-9]+/" nil t);PGP 2.*
126 (buffer-substring (point)(+ 8 (point))))
127 ((re-search-forward "^Type" nil t);PGP 6.*
128 (beginning-of-line 2)
131 (buffer-substring (point)(progn (end-of-line) (point)))))
134 (defun pgg-pgp-encrypt-region (start end recipients)
135 "Encrypt the current region between START and END."
136 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
138 `("+encrypttoself=off +verbose=1" "+batchmode"
139 "+language=us" "-fate"
141 (mapcar (lambda (rcpt) (concat "\"" rcpt "\""))
143 (if pgg-encrypt-for-me
144 (list pgg-pgp-user-id))))))))
145 (pgg-pgp-process-region start end nil pgg-pgp-program args)
146 (pgg-process-when-success nil)))
148 (defun pgg-pgp-decrypt-region (start end)
149 "Decrypt the current region between START and END."
150 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
151 (key (pgg-pgp-lookup-key pgg-pgp-user-id 'encrypt))
154 (format "PGP passphrase for %s: " pgg-pgp-user-id) key))
156 '("+verbose=1" "+batchmode" "+language=us" "-f")))
157 (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
158 (pgg-process-when-success
159 (if pgg-cache-passphrase
160 (pgg-add-passphrase-cache key passphrase)))))
162 (defun pgg-pgp-sign-region (start end &optional clearsign)
163 "Make detached signature from text between START and END."
164 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
167 (format "PGP passphrase for %s: " pgg-pgp-user-id)
168 (pgg-pgp-lookup-key pgg-pgp-user-id 'sign)))
170 (list (if clearsign "-fast" "-fbast")
171 "+verbose=1" "+language=us" "+batchmode"
172 "-u" pgg-pgp-user-id)))
173 (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
174 (pgg-process-when-success
175 (goto-char (point-min))
176 (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
178 (cdr (assq 2 (pgg-parse-armor-region
179 (progn (beginning-of-line 2)
182 (if pgg-cache-passphrase
183 (pgg-add-passphrase-cache
184 (cdr (assq 'key-identifier packet))
187 (defun pgg-pgp-verify-region (start end &optional signature)
188 "Verify region between START and END as the detached signature SIGNATURE."
189 (let* ((orig-file (pgg-make-temp-file "pgg"))
190 (args '("+verbose=1" "+batchmode" "+language=us"))
191 (orig-mode (default-file-modes)))
194 (set-default-file-modes 448)
195 (let ((coding-system-for-write 'binary)
196 jka-compr-compression-info-list jam-zcat-filename-list)
197 (write-region start end orig-file)))
198 (set-default-file-modes orig-mode))
199 (if (stringp signature)
201 (copy-file signature (setq signature (concat orig-file ".asc")))
202 (setq args (append args (list signature orig-file))))
203 (setq args (append args (list orig-file))))
204 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
205 (delete-file orig-file)
206 (if signature (delete-file signature))
207 (pgg-process-when-success
208 (goto-char (point-min))
209 (let ((case-fold-search t))
210 (while (re-search-forward "^warning: " nil t)
211 (delete-region (match-beginning 0)
212 (progn (beginning-of-line 2) (point)))))
213 (goto-char (point-min))
214 (when (re-search-forward "^\\.$" nil t)
215 (delete-region (point-min)
216 (progn (beginning-of-line 2)
219 (defun pgg-pgp-insert-key ()
220 "Insert public key at point."
221 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
223 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
224 (concat "\"" pgg-pgp-user-id "\""))))
225 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
226 (insert-buffer-substring pgg-output-buffer)))
228 (defun pgg-pgp-snarf-keys-region (start end)
229 "Add all public keys in region between START and END to the keyring."
230 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
231 (key-file (pgg-make-temp-file "pgg"))
233 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
235 (let ((coding-system-for-write 'raw-text-dos))
236 (write-region start end key-file))
237 (pgg-pgp-process-region start end nil pgg-pgp-program args)
238 (delete-file key-file)
239 (pgg-process-when-success nil)))
243 ;;; arch-tag: 076b7801-37b2-49a6-97c3-218fdecde33c
244 ;;; pgg-pgp.el ends here