1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
7 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
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/>.
30 ;; For Emacs <22.2 and XEmacs.
31 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
33 (if (locate-library "password-cache")
34 (require 'password-cache
)
44 (defvar mc-pgp-always-sign
)
46 (autoload 'quoted-printable-decode-region
"qp")
47 (autoload 'quoted-printable-encode-region
"qp")
49 (autoload 'mm-decode-content-transfer-encoding
"mm-bodies")
50 (autoload 'mm-encode-content-transfer-encoding
"mm-bodies")
51 (autoload 'message-options-get
"message")
52 (autoload 'message-options-set
"message")
54 (defvar mml1991-use mml2015-use
55 "The package used for PGP.")
57 (defvar mml1991-function-alist
58 '((mailcrypt mml1991-mailcrypt-sign
59 mml1991-mailcrypt-encrypt
)
64 "Alist of PGP functions.")
66 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
67 "If t, cache passphrase.")
69 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
70 "How many seconds the passphrase is cached.
71 Whether the passphrase is cached at all is controlled by
72 `mml1991-cache-passphrase'.")
74 (defvar mml1991-signers nil
75 "A list of your own key ID which will be used to sign a message.")
77 (defvar mml1991-encrypt-to-self nil
78 "If t, add your own key ID to recipient list when encryption.")
82 (autoload 'mc-sign-generic
"mc-toplev")
84 (defvar mml1991-decrypt-function
'mailcrypt-decrypt
)
85 (defvar mml1991-verify-function
'mailcrypt-verify
)
87 (defun mml1991-mailcrypt-sign (cont)
88 (let ((text (current-buffer))
90 (result-buffer (get-buffer-create "*GPG Result*")))
91 ;; Save MIME Content[^ ]+: headers from signing
92 (goto-char (point-min))
93 (while (looking-at "^Content[^ ]+:") (forward-line))
95 (setq headers
(buffer-string))
96 (delete-region (point-min) (point)))
97 (goto-char (point-max))
100 (quoted-printable-decode-region (point-min) (point-max))
102 (setq signature
(current-buffer))
103 (insert-buffer-substring text
)
104 (unless (mc-sign-generic (message-options-get 'message-sender
)
106 (unless (> (point-max) (point-min))
107 (pop-to-buffer result-buffer
)
108 (error "Sign error")))
109 (goto-char (point-min))
110 (while (re-search-forward "\r+$" nil t
)
111 (replace-match "" t t
))
112 (quoted-printable-encode-region (point-min) (point-max))
114 (delete-region (point-min) (point-max))
115 (if headers
(insert headers
))
117 (insert-buffer-substring signature
)
118 (goto-char (point-max)))))
120 (declare-function mc-encrypt-generic
"ext:mc-toplev"
121 (&optional recipients scheme start end from sign
))
123 (defun mml1991-mailcrypt-encrypt (cont &optional sign
)
124 (let ((text (current-buffer))
126 (or mc-pgp-always-sign
128 (eq t
(or (message-options-get 'message-sign-encrypt
)
130 'message-sign-encrypt
131 (or (y-or-n-p "Sign the message? ")
135 (result-buffer (get-buffer-create "*GPG Result*")))
136 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
137 (goto-char (point-min))
138 (while (looking-at "^Content[^ ]+:") (forward-line))
140 (delete-region (point-min) (point)))
141 (mm-with-unibyte-current-buffer
143 (inline (mm-disable-multibyte))
144 (setq cipher
(current-buffer))
145 (insert-buffer-substring text
)
146 (unless (mc-encrypt-generic
148 (message-options-get 'message-recipients
)
149 (message-options-set 'message-recipients
150 (read-string "Recipients: ")))
152 (point-min) (point-max)
153 (message-options-get 'message-sender
)
155 (unless (> (point-max) (point-min))
156 (pop-to-buffer result-buffer
)
157 (error "Encrypt error")))
158 (goto-char (point-min))
159 (while (re-search-forward "\r+$" nil t
)
160 (replace-match "" t t
))
162 (delete-region (point-min) (point-max))
163 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
164 ;;(insert "Version: 1\n\n")
166 (insert-buffer-substring cipher
)
167 (goto-char (point-max))))))
171 (defvar pgg-default-user-id
)
172 (defvar pgg-errors-buffer
)
173 (defvar pgg-output-buffer
)
175 (defun mml1991-pgg-sign (cont)
176 (let ((pgg-text-mode t
)
177 (pgg-default-user-id (or (message-options-get 'mml-sender
)
178 pgg-default-user-id
))
180 ;; Don't sign headers.
181 (goto-char (point-min))
182 (when (re-search-forward "^$" nil t
)
183 (setq headers
(buffer-substring (point-min) (point)))
185 (narrow-to-region (point-min) (point))
186 (setq cte
(mail-fetch-field "content-transfer-encoding")))
188 (delete-region (point-min) (point))
190 (setq cte
(intern (downcase cte
)))
191 (mm-decode-content-transfer-encoding cte
)))
192 (unless (pgg-sign-region (point-min) (point-max) t
)
193 (pop-to-buffer pgg-errors-buffer
)
194 (error "Encrypt error"))
195 (delete-region (point-min) (point-max))
196 (mm-with-unibyte-current-buffer
197 (insert-buffer-substring pgg-output-buffer
)
198 (goto-char (point-min))
199 (while (re-search-forward "\r+$" nil t
)
200 (replace-match "" t t
))
202 (mm-encode-content-transfer-encoding cte
))
203 (goto-char (point-min))
209 (defun mml1991-pgg-encrypt (cont &optional sign
)
210 (goto-char (point-min))
211 (when (re-search-forward "^$" nil t
)
212 (let ((cte (save-restriction
213 (narrow-to-region (point-min) (point))
214 (mail-fetch-field "content-transfer-encoding"))))
215 ;; Strip MIME headers since it will be ASCII armored.
217 (delete-region (point-min) (point))
219 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
220 (unless (let ((pgg-text-mode t
))
222 (point-min) (point-max)
225 (message-options-get 'message-recipients
)
226 (message-options-set 'message-recipients
227 (read-string "Recipients: ")))
230 (pop-to-buffer pgg-errors-buffer
)
231 (error "Encrypt error"))
232 (delete-region (point-min) (point-max))
234 (insert-buffer-substring pgg-output-buffer
)
239 (defvar epg-user-id-alist
)
241 (autoload 'epg-make-context
"epg")
242 (autoload 'epg-passphrase-callback-function
"epg")
243 (autoload 'epa-select-keys
"epa")
244 (autoload 'epg-list-keys
"epg")
245 (autoload 'epg-context-set-armor
"epg")
246 (autoload 'epg-context-set-textmode
"epg")
247 (autoload 'epg-context-set-signers
"epg")
248 (autoload 'epg-context-set-passphrase-callback
"epg")
249 (autoload 'epg-sign-string
"epg")
250 (autoload 'epg-encrypt-string
"epg")
251 (autoload 'epg-configuration
"epg-config")
252 (autoload 'epg-expand-group
"epg-config")
254 (defvar mml1991-epg-secret-key-id-list nil
)
256 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
258 (epg-passphrase-callback-function context key-id nil
)
259 (let* ((entry (assoc key-id epg-user-id-alist
))
262 (format "GnuPG passphrase for %s: "
270 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
271 (password-cache-add key-id passphrase
))
272 (setq mml1991-epg-secret-key-id-list
273 (cons key-id mml1991-epg-secret-key-id-list
))
274 (copy-sequence passphrase
)))))
276 (defun mml1991-epg-sign (cont)
277 (let ((context (epg-make-context))
278 headers cte signers signature
)
279 (if (eq mm-sign-option
'guided
)
280 (setq signers
(epa-select-keys context
"Select keys for signing.
281 If no one is selected, default secret key is used. "
284 (setq signers
(mapcar (lambda (name)
285 (car (epg-list-keys context name t
)))
287 (epg-context-set-armor context t
)
288 (epg-context-set-textmode context t
)
289 (epg-context-set-signers context signers
)
290 (if mml1991-cache-passphrase
291 (epg-context-set-passphrase-callback
293 #'mml1991-epg-passphrase-callback
))
294 ;; Don't sign headers.
295 (goto-char (point-min))
296 (when (re-search-forward "^$" nil t
)
297 (setq headers
(buffer-substring (point-min) (point)))
299 (narrow-to-region (point-min) (point))
300 (setq cte
(mail-fetch-field "content-transfer-encoding")))
302 (delete-region (point-min) (point))
304 (setq cte
(intern (downcase cte
)))
305 (mm-decode-content-transfer-encoding cte
)))
306 (condition-case error
307 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
308 mml1991-epg-secret-key-id-list nil
)
310 (while mml1991-epg-secret-key-id-list
311 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
312 (setq mml1991-epg-secret-key-id-list
313 (cdr mml1991-epg-secret-key-id-list
)))
314 (signal (car error
) (cdr error
))))
315 (delete-region (point-min) (point-max))
316 (mm-with-unibyte-current-buffer
318 (goto-char (point-min))
319 (while (re-search-forward "\r+$" nil t
)
320 (replace-match "" t t
))
322 (mm-encode-content-transfer-encoding cte
))
323 (goto-char (point-min))
329 (defun mml1991-epg-encrypt (cont &optional sign
)
330 (goto-char (point-min))
331 (when (re-search-forward "^$" nil t
)
332 (let ((cte (save-restriction
333 (narrow-to-region (point-min) (point))
334 (mail-fetch-field "content-transfer-encoding"))))
335 ;; Strip MIME headers since it will be ASCII armored.
337 (delete-region (point-min) (point))
339 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
340 (let ((context (epg-make-context))
342 (if (message-options-get 'message-recipients
)
344 (message-options-get 'message-recipients
)
346 cipher signers config
)
347 ;; We should remove this check if epg-0.0.6 is released.
348 (if (and (condition-case nil
349 (require 'epg-config
)
351 (functionp #'epg-expand-group
))
352 (setq config
(epg-configuration)
355 (mapcar (lambda (recipient)
356 (or (epg-expand-group config recipient
)
359 (if (eq mm-encrypt-option
'guided
)
361 (epa-select-keys context
"Select recipients for encryption.
362 If no one is selected, symmetric encryption will be performed. "
365 (delq nil
(mapcar (lambda (name)
366 (car (epg-list-keys context name
)))
368 (if mml1991-encrypt-to-self
372 (mapcar (lambda (name)
373 (car (epg-list-keys context name
)))
375 (error "mml1991-signers not set")))
377 (if (eq mm-sign-option
'guided
)
378 (setq signers
(epa-select-keys context
"Select keys for signing.
379 If no one is selected, default secret key is used. "
382 (setq signers
(mapcar (lambda (name)
383 (car (epg-list-keys context name t
)))
385 (epg-context-set-signers context signers
))
386 (epg-context-set-armor context t
)
387 (epg-context-set-textmode context t
)
388 (if mml1991-cache-passphrase
389 (epg-context-set-passphrase-callback
391 #'mml1991-epg-passphrase-callback
))
392 (condition-case error
394 (epg-encrypt-string context
(buffer-string) recipients sign
)
395 mml1991-epg-secret-key-id-list nil
)
397 (while mml1991-epg-secret-key-id-list
398 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
399 (setq mml1991-epg-secret-key-id-list
400 (cdr mml1991-epg-secret-key-id-list
)))
401 (signal (car error
) (cdr error
))))
402 (delete-region (point-min) (point-max))
403 (insert "\n" cipher
))
407 (defun mml1991-encrypt (cont &optional sign
)
408 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
410 (funcall func cont sign
)
411 (error "Cannot find encrypt function"))))
414 (defun mml1991-sign (cont)
415 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
418 (error "Cannot find sign function"))))
423 ;; coding: iso-8859-1
426 ;;; mml1991.el ends here