1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
8 ;; Keywords: PGP, OpenPGP
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
30 (require 'cl
) ; for pgg macros
34 "PGP 2.* and 6.* interface."
37 (defcustom pgg-pgp-program
"pgp"
38 "PGP 2.* and 6.* executable."
42 (defcustom pgg-pgp-shell-file-name
"/bin/sh"
43 "File name to load inferior shells from.
44 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
48 (defcustom pgg-pgp-shell-command-switch
"-c"
49 "Switch used to have the shell execute its command line argument."
53 (defcustom pgg-pgp-extra-args nil
54 "Extra arguments for every PGP invocation."
57 (const :tag
"None" nil
)
58 (string :tag
"Arguments")))
60 (defvar pgg-pgp-user-id nil
61 "PGP ID of your default identity.")
63 (defun pgg-pgp-process-region (start end passphrase program args
)
64 (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
68 (list (concat "2>" errors-file-name
))))
69 (shell-file-name pgg-pgp-shell-file-name
)
70 (shell-command-switch pgg-pgp-shell-command-switch
)
71 (process-environment process-environment
)
72 (output-buffer pgg-output-buffer
)
73 (errors-buffer pgg-errors-buffer
)
74 (process-connection-type nil
)
75 process status exit-status
)
76 (with-current-buffer (get-buffer-create output-buffer
)
80 (setenv "PGPPASSFD" "0"))
83 (let ((coding-system-for-read 'binary
)
84 (coding-system-for-write 'binary
))
87 #'start-process-shell-command
"*PGP*" output-buffer
89 (set-process-sentinel process
#'ignore
)
91 (process-send-string process
(concat passphrase
"\n")))
92 (process-send-region process start end
)
93 (process-send-eof process
)
94 (while (eq 'run
(process-status process
))
95 (accept-process-output process
5))
96 (setq status
(process-status process
)
97 exit-status
(process-exit-status process
))
98 (delete-process process
)
99 (with-current-buffer output-buffer
100 (pgg-convert-lbt-region (point-min)(point-max) 'LF
)
102 (if (memq status
'(stop signal
))
103 (error "%s exited abnormally: '%s'" program exit-status
))
104 (if (= 127 exit-status
)
105 (error "%s could not be found" program
))
107 (set-buffer (get-buffer-create errors-buffer
))
108 (buffer-disable-undo)
110 (insert-file-contents errors-file-name
)))
111 (if (and process
(eq 'run
(process-status process
)))
112 (interrupt-process process
))
114 (delete-file errors-file-name
)
117 (defun pgg-pgp-lookup-key (string &optional type
)
118 "Search keys associated with STRING."
119 (let ((args (list "+batchmode" "+language=en" "-kv" string
)))
120 (with-current-buffer (get-buffer-create pgg-output-buffer
)
121 (buffer-disable-undo)
123 (apply #'call-process pgg-pgp-program nil t nil args
)
124 (goto-char (point-min))
126 ((re-search-forward "^pub\\s +[0-9]+/" nil t
);PGP 2.*
127 (buffer-substring (point)(+ 8 (point))))
128 ((re-search-forward "^Type" nil t
);PGP 6.*
129 (beginning-of-line 2)
132 (buffer-substring (point)(progn (end-of-line) (point)))))
135 (defun pgg-pgp-encrypt-region (start end recipients
)
136 "Encrypt the current region between START and END."
137 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id
))
139 `("+encrypttoself=off +verbose=1" "+batchmode"
140 "+language=us" "-fate"
142 (mapcar (lambda (rcpt) (concat "\"" rcpt
"\""))
144 (if pgg-encrypt-for-me
145 (list pgg-pgp-user-id
))))))))
146 (pgg-pgp-process-region start end nil pgg-pgp-program args
)
147 (pgg-process-when-success nil
)))
149 (defun pgg-pgp-decrypt-region (start end
)
150 "Decrypt the current region between START and END."
151 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id
))
152 (key (pgg-pgp-lookup-key pgg-pgp-user-id
'encrypt
))
155 (format "PGP passphrase for %s: " pgg-pgp-user-id
) key
))
157 '("+verbose=1" "+batchmode" "+language=us" "-f")))
158 (pgg-pgp-process-region start end passphrase pgg-pgp-program args
)
159 (pgg-process-when-success
160 (if pgg-cache-passphrase
161 (pgg-add-passphrase-cache key passphrase
)))))
163 (defun pgg-pgp-sign-region (start end
&optional clearsign
)
164 "Make detached signature from text between START and END."
165 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id
))
168 (format "PGP passphrase for %s: " pgg-pgp-user-id
)
169 (pgg-pgp-lookup-key pgg-pgp-user-id
'sign
)))
171 (list (if clearsign
"-fast" "-fbast")
172 "+verbose=1" "+language=us" "+batchmode"
173 "-u" pgg-pgp-user-id
)))
174 (pgg-pgp-process-region start end passphrase pgg-pgp-program args
)
175 (pgg-process-when-success
176 (goto-char (point-min))
177 (when (re-search-forward "^-+BEGIN PGP" nil t
);XXX
179 (cdr (assq 2 (pgg-parse-armor-region
180 (progn (beginning-of-line 2)
183 (if pgg-cache-passphrase
184 (pgg-add-passphrase-cache
185 (cdr (assq 'key-identifier packet
))
188 (defun pgg-pgp-verify-region (start end
&optional signature
)
189 "Verify region between START and END as the detached signature SIGNATURE."
190 (let* ((orig-file (pgg-make-temp-file "pgg"))
191 (args '("+verbose=1" "+batchmode" "+language=us"))
192 (orig-mode (default-file-modes)))
195 (set-default-file-modes 448)
196 (let ((coding-system-for-write 'binary
)
197 jka-compr-compression-info-list jam-zcat-filename-list
)
198 (write-region start end orig-file
)))
199 (set-default-file-modes orig-mode
))
200 (if (stringp signature
)
202 (copy-file signature
(setq signature
(concat orig-file
".asc")))
203 (setq args
(append args
(list signature orig-file
))))
204 (setq args
(append args
(list orig-file
))))
205 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args
)
206 (delete-file orig-file
)
207 (if signature
(delete-file signature
))
208 (pgg-process-when-success
209 (goto-char (point-min))
210 (let ((case-fold-search t
))
211 (while (re-search-forward "^warning: " nil t
)
212 (delete-region (match-beginning 0)
213 (progn (beginning-of-line 2) (point)))))
214 (goto-char (point-min))
215 (when (re-search-forward "^\\.$" nil t
)
216 (delete-region (point-min)
217 (progn (beginning-of-line 2)
220 (defun pgg-pgp-insert-key ()
221 "Insert public key at point."
222 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id
))
224 (list "+verbose=1" "+batchmode" "+language=us" "-kxaf"
225 (concat "\"" pgg-pgp-user-id
"\""))))
226 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args
)
227 (insert-buffer-substring pgg-output-buffer
)))
229 (defun pgg-pgp-snarf-keys-region (start end
)
230 "Add all public keys in region between START and END to the keyring."
231 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id
))
232 (key-file (pgg-make-temp-file "pgg"))
234 (list "+verbose=1" "+batchmode" "+language=us" "-kaf"
236 (let ((coding-system-for-write 'raw-text-dos
))
237 (write-region start end key-file
))
238 (pgg-pgp-process-region start end nil pgg-pgp-program args
)
239 (delete-file key-file
)
240 (pgg-process-when-success nil
)))
244 ;;; arch-tag: 076b7801-37b2-49a6-97c3-218fdecde33c
245 ;;; pgg-pgp.el ends here