Backport fix for Bug#8672 from trunk
[emacs.git] / lisp / pgg-pgp.el
blobe36c1efb138b792a28b7abd16d291c4719d4c5e0
1 ;;; pgg-pgp.el --- PGP 2.* and 6.* support for PGG.
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Daiki Ueno <ueno@unixuser.org>
7 ;; Created: 1999/11/02
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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Code:
27 (eval-when-compile
28 (require 'cl) ; for pgg macros
29 (require 'pgg))
31 (defgroup pgg-pgp ()
32 "PGP 2.* and 6.* interface."
33 :group 'pgg)
35 (defcustom pgg-pgp-program "pgp"
36 "PGP 2.* and 6.* executable."
37 :group 'pgg-pgp
38 :type 'string)
40 (defcustom pgg-pgp-shell-file-name "/bin/sh"
41 "File name to load inferior shells from.
42 Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
43 :group 'pgg-pgp
44 :type 'string)
46 (defcustom pgg-pgp-shell-command-switch "-c"
47 "Switch used to have the shell execute its command line argument."
48 :group 'pgg-pgp
49 :type 'string)
51 (defcustom pgg-pgp-extra-args nil
52 "Extra arguments for every PGP invocation."
53 :group 'pgg-pgp
54 :type '(choice
55 (const :tag "None" nil)
56 (string :tag "Arguments")))
58 (defvar pgg-pgp-user-id nil
59 "PGP ID of your default identity.")
61 (defun pgg-pgp-process-region (start end passphrase program args)
62 (let* ((errors-file-name (pgg-make-temp-file "pgg-errors"))
63 (args
64 (concat args
65 pgg-pgp-extra-args
66 " 2>" (shell-quote-argument errors-file-name)))
67 (shell-file-name pgg-pgp-shell-file-name)
68 (shell-command-switch pgg-pgp-shell-command-switch)
69 (process-environment process-environment)
70 (output-buffer pgg-output-buffer)
71 (errors-buffer pgg-errors-buffer)
72 (process-connection-type nil)
73 process status exit-status)
74 (with-current-buffer (get-buffer-create output-buffer)
75 (buffer-disable-undo)
76 (erase-buffer))
77 (when passphrase
78 (setenv "PGPPASSFD" "0"))
79 (unwind-protect
80 (progn
81 (let ((coding-system-for-read 'binary)
82 (coding-system-for-write 'binary))
83 (setq process
84 (start-process-shell-command "*PGP*" output-buffer
85 (concat program " " args))))
86 (set-process-sentinel process #'ignore)
87 (when passphrase
88 (process-send-string process (concat passphrase "\n")))
89 (process-send-region process start end)
90 (process-send-eof process)
91 (while (eq 'run (process-status process))
92 (accept-process-output process 5))
93 (setq status (process-status process)
94 exit-status (process-exit-status process))
95 (delete-process process)
96 (with-current-buffer output-buffer
97 (pgg-convert-lbt-region (point-min)(point-max) 'LF)
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)
106 (erase-buffer)
107 (insert-file-contents errors-file-name)))
108 (if (and process (eq 'run (process-status process)))
109 (interrupt-process process))
110 (condition-case nil
111 (let ((delete-by-moving-to-trash nil))
112 (delete-file errors-file-name))
113 (file-error nil)))))
115 (defun pgg-pgp-lookup-key (string &optional type)
116 "Search keys associated with STRING."
117 (let ((args (list "+batchmode" "+language=en" "-kv" string)))
118 (with-current-buffer (get-buffer-create pgg-output-buffer)
119 (buffer-disable-undo)
120 (erase-buffer)
121 (apply #'call-process pgg-pgp-program nil t nil args)
122 (goto-char (point-min))
123 (cond
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)
128 (substring
129 (nth 2 (split-string
130 (buffer-substring (point)(progn (end-of-line) (point)))))
131 2))))))
133 (defun pgg-pgp-encrypt-region (start end recipients &optional sign passphrase)
134 "Encrypt the current region between START and END."
135 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
136 (passphrase (or passphrase
137 (when sign
138 (pgg-read-passphrase
139 (format "PGP passphrase for %s: "
140 pgg-pgp-user-id)
141 pgg-pgp-user-id))))
142 (args
143 (concat
144 "+encrypttoself=off +verbose=1 +batchmode +language=us -fate "
145 (if (or recipients pgg-encrypt-for-me)
146 (mapconcat 'shell-quote-argument
147 (append recipients
148 (if pgg-encrypt-for-me
149 (list pgg-pgp-user-id))) " "))
150 (if sign (concat " -s -u " (shell-quote-argument pgg-pgp-user-id))))))
151 (pgg-pgp-process-region start end nil pgg-pgp-program args)
152 (pgg-process-when-success nil)))
154 (defun pgg-pgp-decrypt-region (start end &optional passphrase)
155 "Decrypt the current region between START and END.
157 If optional PASSPHRASE is not specified, it will be obtained from the
158 passphrase cache or user."
159 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
160 (key (pgg-pgp-lookup-key pgg-pgp-user-id 'encrypt))
161 (passphrase
162 (or passphrase
163 (pgg-read-passphrase
164 (format "PGP passphrase for %s: " pgg-pgp-user-id) key)))
165 (args
166 "+verbose=1 +batchmode +language=us -f"))
167 (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
168 (pgg-process-when-success
169 (if pgg-cache-passphrase
170 (pgg-add-passphrase-to-cache key passphrase)))))
172 (defun pgg-pgp-sign-region (start end &optional clearsign passphrase)
173 "Make detached signature from text between START and END.
175 If optional PASSPHRASE is not specified, it will be obtained from the
176 passphrase cache or user."
177 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
178 (passphrase
179 (or passphrase
180 (pgg-read-passphrase
181 (format "PGP passphrase for %s: " pgg-pgp-user-id)
182 (pgg-pgp-lookup-key pgg-pgp-user-id 'sign))))
183 (args
184 (concat (if clearsign "-fast" "-fbast")
185 " +verbose=1 +language=us +batchmode"
186 " -u " (shell-quote-argument pgg-pgp-user-id))))
187 (pgg-pgp-process-region start end passphrase pgg-pgp-program args)
188 (pgg-process-when-success
189 (goto-char (point-min))
190 (when (re-search-forward "^-+BEGIN PGP" nil t);XXX
191 (let ((packet
192 (cdr (assq 2 (pgg-parse-armor-region
193 (progn (beginning-of-line 2)
194 (point))
195 (point-max))))))
196 (if pgg-cache-passphrase
197 (pgg-add-passphrase-to-cache
198 (cdr (assq 'key-identifier packet))
199 passphrase)))))))
201 (defun pgg-pgp-verify-region (start end &optional signature)
202 "Verify region between START and END as the detached signature SIGNATURE."
203 (let* ((orig-file (pgg-make-temp-file "pgg"))
204 (args "+verbose=1 +batchmode +language=us")
205 (orig-mode (default-file-modes)))
206 (unwind-protect
207 (progn
208 (set-default-file-modes 448)
209 (let ((coding-system-for-write 'binary)
210 jka-compr-compression-info-list jam-zcat-filename-list)
211 (write-region start end orig-file)))
212 (set-default-file-modes orig-mode))
213 (if (stringp signature)
214 (progn
215 (copy-file signature (setq signature (concat orig-file ".asc")))
216 (setq args (concat args " " (shell-quote-argument signature)))))
217 (setq args (concat args " " (shell-quote-argument orig-file)))
218 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
219 (let ((delete-by-moving-to-trash nil))
220 (delete-file orig-file))
221 (if signature
222 (let ((delete-by-moving-to-trash nil))
223 (delete-file signature)))
224 (pgg-process-when-success
225 (goto-char (point-min))
226 (let ((case-fold-search t))
227 (while (re-search-forward "^warning: " nil t)
228 (delete-region (match-beginning 0)
229 (progn (beginning-of-line 2) (point)))))
230 (goto-char (point-min))
231 (when (re-search-forward "^\\.$" nil t)
232 (delete-region (point-min)
233 (progn (beginning-of-line 2)
234 (point)))))))
236 (defun pgg-pgp-insert-key ()
237 "Insert public key at point."
238 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
239 (args
240 (concat "+verbose=1 +batchmode +language=us -kxaf "
241 (shell-quote-argument pgg-pgp-user-id))))
242 (pgg-pgp-process-region (point)(point) nil pgg-pgp-program args)
243 (insert-buffer-substring pgg-output-buffer)))
245 (defun pgg-pgp-snarf-keys-region (start end)
246 "Add all public keys in region between START and END to the keyring."
247 (let* ((pgg-pgp-user-id (or pgg-pgp-user-id pgg-default-user-id))
248 (key-file (pgg-make-temp-file "pgg"))
249 (args
250 (concat "+verbose=1 +batchmode +language=us -kaf "
251 (shell-quote-argument key-file))))
252 (let ((coding-system-for-write 'raw-text-dos))
253 (write-region start end key-file))
254 (pgg-pgp-process-region start end nil pgg-pgp-program args)
255 (let ((delete-by-moving-to-trash nil))
256 (delete-file key-file))
257 (pgg-process-when-success nil)))
259 (provide 'pgg-pgp)
261 ;; arch-tag: 076b7801-37b2-49a6-97c3-218fdecde33c
262 ;;; pgg-pgp.el ends here