~ubuntu-branches/ubuntu/trusty/semi/trusty

1 by Tatsuya Kinoshita
Import upstream version 1.14.6+0.20040418
1
;;; pgg-pgp5.el --- PGP 5.* support for PGG.
2
3
;; Copyright (C) 1999,2000 Daiki Ueno
4
5
;; Author: Daiki Ueno <ueno@ueda.info.waseda.ac.jp>
6
;; Created: 1999/11/02
7
;; Keywords: PGP, OpenPGP
8
9
;; This file is part of SEMI (Secure Emacs MIME Interface).
10
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.
15
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.
20
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
1.1.3 by Tatsuya Kinoshita
Import upstream version 1.14.6+0.20060328
23
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24
;; Boston, MA 02110-1301, USA.
1 by Tatsuya Kinoshita
Import upstream version 1.14.6+0.20040418
25
26
;;; Code:
27
28
(require 'mel) ; binary-to-text-funcall, binary-write-decoded-region
29
(eval-when-compile (require 'pgg))
30
31
(defgroup pgg-pgp5 ()
32
  "PGP 5.* interface"
33
  :group 'pgg)
34
35
(defcustom pgg-pgp5-pgpe-program "pgpe"
36
  "PGP 5.* 'pgpe' executable."
37
  :group 'pgg-pgp5
38
  :type 'string)
39
40
(defcustom pgg-pgp5-pgps-program "pgps"
41
  "PGP 5.* 'pgps' executable."
42
  :group 'pgg-pgp5
43
  :type 'string)
44
45
(defcustom pgg-pgp5-pgpk-program "pgpk"
46
  "PGP 5.* 'pgpk' executable."
47
  :group 'pgg-pgp5
48
  :type 'string)
49
50
(defcustom pgg-pgp5-pgpv-program "pgpv"
51
  "PGP 5.* 'pgpv' executable."
52
  :group 'pgg-pgp5
53
  :type 'string)
54
55
(defcustom pgg-pgp5-shell-file-name "/bin/sh"
56
  "File name to load inferior shells from.
57
Bourne shell or its equivalent \(not tcsh) is needed for \"2>\"."
58
  :group 'pgg-pgp5
59
  :type 'string)
60
61
(defcustom pgg-pgp5-shell-command-switch "-c"
62
  "Switch used to have the shell execute its command line argument."
63
  :group 'pgg-pgp5
64
  :type 'string)
65
66
(defcustom pgg-pgp5-extra-args nil
67
  "Extra arguments for every PGP 5.* invocation."
68
  :group 'pgg-pgp5
69
  :type 'string)
70
71
(eval-and-compile
72
  (luna-define-class pgg-scheme-pgp5 (pgg-scheme)))
73
74
(defvar pgg-pgp5-user-id nil
75
  "PGP 5.* ID of your default identity.")
76
77
(defvar pgg-scheme-pgp5-instance nil)
78
79
;;;###autoload
80
(defun pgg-make-scheme-pgp5 ()
81
  (or pgg-scheme-pgp5-instance
82
      (setq pgg-scheme-pgp5-instance
83
	    (luna-make-entity 'pgg-scheme-pgp5))))
84
85
(defun pgg-pgp5-process-region (start end passphrase program args)
86
  (let* ((errors-file-name (make-temp-file "pgg-errors"))
87
	 (args
88
	  (append args
89
		  pgg-pgp5-extra-args
90
		  (list (concat "2>" errors-file-name))))
91
	 (shell-file-name pgg-pgp5-shell-file-name)
92
	 (shell-command-switch pgg-pgp5-shell-command-switch)
93
	 (process-environment process-environment)
94
	 (output-buffer pgg-output-buffer)
95
	 (errors-buffer pgg-errors-buffer)
96
	 (process-connection-type nil)
97
	 process status exit-status)
98
    (with-current-buffer (get-buffer-create output-buffer)
99
      (buffer-disable-undo)
100
      (erase-buffer))
101
    (when passphrase
102
      (setenv "PGPPASSFD" "0"))
103
    (unwind-protect
104
	(progn
105
	  (setq process
106
		(apply #'binary-funcall
107
		       #'start-process-shell-command "*PGP*" output-buffer
108
		       program args))
109
	  (set-process-sentinel process #'ignore)
110
	  (when passphrase
111
	    (process-send-string process (concat passphrase "\n")))
112
	  (process-send-region process start end)
113
	  (process-send-eof process)
114
	  (while (eq 'run (process-status process))
115
	    (accept-process-output process 5))
116
	  (setq status (process-status process)
117
		exit-status (process-exit-status process))
118
	  (delete-process process)
119
	  (with-current-buffer output-buffer
120
	    (pgg-convert-lbt-region (point-min)(point-max) 'LF)
121
122
	    (if (memq status '(stop signal))
123
		(error "%s exited abnormally: '%s'" program exit-status))
124
	    (if (= 127 exit-status)
125
		(error "%s could not be found" program))
126
127
	    (set-buffer (get-buffer-create errors-buffer))
128
	    (buffer-disable-undo)
129
	    (erase-buffer)
130
	    (insert-file-contents errors-file-name)))
131
      (if (and process (eq 'run (process-status process)))
132
	  (interrupt-process process))
133
      (condition-case nil
134
	  (delete-file errors-file-name)
135
	(file-error nil)))))
136
137
(luna-define-method pgg-scheme-lookup-key ((scheme pgg-scheme-pgp5)
138
						  string &optional type)
139
  (let ((args (list "+language=en" "-l" string)))
140
    (with-current-buffer (get-buffer-create pgg-output-buffer)
141
      (buffer-disable-undo)
142
      (erase-buffer)
143
      (apply #'call-process pgg-pgp5-pgpk-program nil t nil args)
144
      (goto-char (point-min))
145
      (when (re-search-forward "^sec" nil t)
146
	(substring
147
	 (nth 2 (split-string
148
		 (buffer-substring (match-end 0)(progn (end-of-line)(point)))))
149
	 2)))))
150
151
(luna-define-method pgg-scheme-encrypt-region ((scheme pgg-scheme-pgp5)
152
					       start end recipients)
153
  (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
154
	 (args
155
	  `("+NoBatchInvalidKeys=off" "-fat" "+batchmode=1"
156
	    ,@(if recipients
157
		  (apply #'append
158
			 (mapcar (lambda (rcpt)
159
				   (list "-r"
160
					 (concat "\"" rcpt "\"")))
161
				 (append recipients
162
					 (if pgg-encrypt-for-me
163
					     (list pgg-pgp5-user-id)))))))))
164
    (pgg-pgp5-process-region start end nil pgg-pgp5-pgpe-program args)
165
    (pgg-process-when-success nil)))
166
167
(luna-define-method pgg-scheme-decrypt-region ((scheme pgg-scheme-pgp5)
168
					       start end)
169
  (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
170
	 (passphrase
171
	  (pgg-read-passphrase
172
	   (format "PGP passphrase for %s: " pgg-pgp5-user-id)
173
	   (pgg-scheme-lookup-key scheme pgg-pgp5-user-id 'encrypt)))
174
	 (args
175
	  '("+verbose=1" "+batchmode=1" "+language=us" "-f")))
176
    (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgpv-program args)
177
    (pgg-process-when-success nil)))
178
179
(luna-define-method pgg-scheme-sign-region ((scheme pgg-scheme-pgp5)
180
					    start end &optional clearsign)
181
  (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
182
	 (passphrase
183
	  (pgg-read-passphrase
184
	   (format "PGP passphrase for %s: " pgg-pgp5-user-id)
185
	   (pgg-scheme-lookup-key scheme pgg-pgp5-user-id 'sign)))
186
	 (args
187
	  (list (if clearsign "-fat" "-fbat")
188
		"+verbose=1" "+language=us" "+batchmode=1"
189
		"-u" pgg-pgp5-user-id)))
190
    (pgg-pgp5-process-region start end passphrase pgg-pgp5-pgps-program args)
191
    (pgg-process-when-success
192
      (when (re-search-forward "^-+BEGIN PGP SIGNATURE" nil t);XXX
193
	(let ((packet
194
	       (cdr (assq 2 (pgg-parse-armor-region
195
			     (progn (beginning-of-line 2)
196
				    (point))
197
			     (point-max))))))
198
	  (if pgg-cache-passphrase
199
	      (pgg-add-passphrase-cache
200
	       (cdr (assq 'key-identifier packet))
201
	       passphrase)))))))
202
203
(luna-define-method pgg-scheme-verify-region ((scheme pgg-scheme-pgp5)
204
					      start end &optional signature)
1.1.2 by Tatsuya Kinoshita
Import upstream version 1.14.6+0.20050510
205
  (let ((orig-file (make-temp-file "pgg"))
206
	(args '("+verbose=1" "+batchmode=1" "+language=us"))
207
	(orig-mode (default-file-modes)))
1 by Tatsuya Kinoshita
Import upstream version 1.14.6+0.20040418
208
    (unwind-protect
209
	(progn
210
	  (set-default-file-modes 448)
211
	  (binary-write-decoded-region start end orig-file))
212
      (set-default-file-modes orig-mode))
213
    (when (stringp signature)
214
      (copy-file signature (setq signature (concat orig-file ".asc")))
215
      (setq args (append args (list signature))))
216
    (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpv-program args)
217
    (delete-file orig-file)
218
    (if signature (delete-file signature))
219
    (with-current-buffer pgg-errors-buffer
220
      (goto-char (point-min))
221
      (if (re-search-forward "^Good signature" nil t)
222
	  (progn
223
	    (set-buffer pgg-output-buffer)
224
	    (insert-buffer-substring pgg-errors-buffer)
225
	    t)
226
	nil))))
227
228
(luna-define-method pgg-scheme-insert-key ((scheme pgg-scheme-pgp5))
229
  (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
230
	 (args
231
	  (list "+verbose=1" "+batchmode=1" "+language=us" "-x"
232
		(concat "\"" pgg-pgp5-user-id "\""))))
233
    (pgg-pgp5-process-region (point)(point) nil pgg-pgp5-pgpk-program args)
234
    (insert-buffer-substring pgg-output-buffer)))
235
236
(luna-define-method pgg-scheme-snarf-keys-region ((scheme pgg-scheme-pgp5)
237
						  start end)
238
  (let* ((pgg-pgp5-user-id (or pgg-pgp5-user-id pgg-default-user-id))
239
	 (key-file (make-temp-file "pgg"))
240
	 (args
241
	  (list "+verbose=1" "+batchmode=1" "+language=us" "-a"
242
		key-file)))
243
    (let ((coding-system-for-write 'raw-text-dos))
244
      (write-region start end key-file))
245
    (pgg-pgp5-process-region start end nil pgg-pgp5-pgpk-program args)
246
    (delete-file key-file)
247
    (pgg-process-when-success nil)))
248
249
(provide 'pgg-pgp5)
250
251
;;; pgg-pgp5.el ends here