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 Free Software Foundation, Inc.
6 ;; Author: Sascha Ldecke <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/>.
31 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
39 (defvar mc-pgp-always-sign
)
41 (autoload 'quoted-printable-decode-region
"qp")
42 (autoload 'quoted-printable-encode-region
"qp")
44 (autoload 'mm-decode-content-transfer-encoding
"mm-bodies")
45 (autoload 'mm-encode-content-transfer-encoding
"mm-bodies")
46 (autoload 'message-options-get
"message")
47 (autoload 'message-options-set
"message")
49 (defvar mml1991-use mml2015-use
50 "The package used for PGP.")
52 (defvar mml1991-function-alist
53 '((mailcrypt mml1991-mailcrypt-sign
54 mml1991-mailcrypt-encrypt
)
61 "Alist of PGP functions.")
63 (defvar mml1991-verbose mml-secure-verbose
64 "If non-nil, ask the user about the current operation more verbosely.")
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 (setq cipher
(current-buffer))
144 (insert-buffer-substring text
)
145 (unless (mc-encrypt-generic
147 (message-options-get 'message-recipients
)
148 (message-options-set 'message-recipients
149 (read-string "Recipients: ")))
151 (point-min) (point-max)
152 (message-options-get 'message-sender
)
154 (unless (> (point-max) (point-min))
155 (pop-to-buffer result-buffer
)
156 (error "Encrypt error")))
157 (goto-char (point-min))
158 (while (re-search-forward "\r+$" nil t
)
159 (replace-match "" t t
))
161 (delete-region (point-min) (point-max))
162 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
163 ;;(insert "Version: 1\n\n")
165 (insert-buffer-substring cipher
)
166 (goto-char (point-max))))))
170 (autoload 'gpg-sign-cleartext
"gpg")
172 (declare-function gpg-sign-encrypt
"ext:gpg"
173 (plaintext ciphertext result recipients
&optional
174 passphrase sign-with-key armor textmode
))
175 (declare-function gpg-encrypt
"ext:gpg"
176 (plaintext ciphertext result recipients
&optional
177 passphrase armor textmode
))
179 (defun mml1991-gpg-sign (cont)
180 (let ((text (current-buffer))
182 (result-buffer (get-buffer-create "*GPG Result*")))
183 ;; Save MIME Content[^ ]+: headers from signing
184 (goto-char (point-min))
185 (while (looking-at "^Content[^ ]+:") (forward-line))
187 (setq headers
(buffer-string))
188 (delete-region (point-min) (point)))
189 (goto-char (point-max))
192 (quoted-printable-decode-region (point-min) (point-max))
194 (unless (gpg-sign-cleartext text
(setq signature
(current-buffer))
197 (message-options-get 'message-sender
))
198 (unless (> (point-max) (point-min))
199 (pop-to-buffer result-buffer
)
200 (error "Sign error")))
201 (goto-char (point-min))
202 (while (re-search-forward "\r+$" nil t
)
203 (replace-match "" t t
))
204 (quoted-printable-encode-region (point-min) (point-max))
206 (delete-region (point-min) (point-max))
207 (if headers
(insert headers
))
209 (insert-buffer-substring signature
)
210 (goto-char (point-max)))))
212 (defun mml1991-gpg-encrypt (cont &optional sign
)
213 (let ((text (current-buffer))
215 (result-buffer (get-buffer-create "*GPG Result*")))
216 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
217 (goto-char (point-min))
218 (while (looking-at "^Content[^ ]+:") (forward-line))
220 (delete-region (point-min) (point)))
221 (mm-with-unibyte-current-buffer
223 (flet ((gpg-encrypt-func
224 (sign plaintext ciphertext result recipients
&optional
225 passphrase sign-with-key armor textmode
)
228 plaintext ciphertext result recipients passphrase
229 sign-with-key armor textmode
)
231 plaintext ciphertext result recipients passphrase
233 (unless (gpg-encrypt-func
235 text
(setq cipher
(current-buffer))
239 (message-options-get 'message-recipients
)
240 (message-options-set 'message-recipients
241 (read-string "Recipients: ")))
244 (message-options-get 'message-sender
)
245 t t
) ; armor & textmode
246 (unless (> (point-max) (point-min))
247 (pop-to-buffer result-buffer
)
248 (error "Encrypt error"))))
249 (goto-char (point-min))
250 (while (re-search-forward "\r+$" nil t
)
251 (replace-match "" t t
))
253 (delete-region (point-min) (point-max))
254 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
255 ;;(insert "Version: 1\n\n")
257 (insert-buffer-substring cipher
)
258 (goto-char (point-max))))))
262 (defvar pgg-default-user-id
)
263 (defvar pgg-errors-buffer
)
264 (defvar pgg-output-buffer
)
266 (defun mml1991-pgg-sign (cont)
267 (let ((pgg-text-mode t
)
268 (pgg-default-user-id (or (message-options-get 'mml-sender
)
269 pgg-default-user-id
))
271 ;; Don't sign headers.
272 (goto-char (point-min))
273 (when (re-search-forward "^$" nil t
)
274 (setq headers
(buffer-substring (point-min) (point)))
276 (narrow-to-region (point-min) (point))
277 (setq cte
(mail-fetch-field "content-transfer-encoding")))
279 (delete-region (point-min) (point))
281 (setq cte
(intern (downcase cte
)))
282 (mm-decode-content-transfer-encoding cte
)))
283 (unless (pgg-sign-region (point-min) (point-max) t
)
284 (pop-to-buffer pgg-errors-buffer
)
285 (error "Encrypt error"))
286 (delete-region (point-min) (point-max))
287 (mm-with-unibyte-current-buffer
288 (insert-buffer-substring pgg-output-buffer
)
289 (goto-char (point-min))
290 (while (re-search-forward "\r+$" nil t
)
291 (replace-match "" t t
))
293 (mm-encode-content-transfer-encoding cte
))
294 (goto-char (point-min))
300 (defun mml1991-pgg-encrypt (cont &optional sign
)
301 (goto-char (point-min))
302 (when (re-search-forward "^$" nil t
)
303 (let ((cte (save-restriction
304 (narrow-to-region (point-min) (point))
305 (mail-fetch-field "content-transfer-encoding"))))
306 ;; Strip MIME headers since it will be ASCII armored.
308 (delete-region (point-min) (point))
310 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
311 (unless (let ((pgg-text-mode t
))
313 (point-min) (point-max)
316 (message-options-get 'message-recipients
)
317 (message-options-set 'message-recipients
318 (read-string "Recipients: ")))
321 (pop-to-buffer pgg-errors-buffer
)
322 (error "Encrypt error"))
323 (delete-region (point-min) (point-max))
325 (insert-buffer-substring pgg-output-buffer
)
330 (defvar epg-user-id-alist
)
331 (defvar password-cache-expiry
)
333 (autoload 'epg-make-context
"epg")
334 (autoload 'epg-passphrase-callback-function
"epg")
335 (autoload 'epa-select-keys
"epa")
336 (autoload 'epg-list-keys
"epg")
337 (autoload 'epg-context-set-armor
"epg")
338 (autoload 'epg-context-set-textmode
"epg")
339 (autoload 'epg-context-set-signers
"epg")
340 (autoload 'epg-context-set-passphrase-callback
"epg")
341 (autoload 'epg-sign-string
"epg")
342 (autoload 'epg-encrypt-string
"epg")
343 (autoload 'epg-configuration
"epg-config")
344 (autoload 'epg-expand-group
"epg-config")
346 (defvar mml1991-epg-secret-key-id-list nil
)
348 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
350 (epg-passphrase-callback-function context key-id nil
)
351 (let* ((entry (assoc key-id epg-user-id-alist
))
354 (format "GnuPG passphrase for %s: "
362 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
363 (password-cache-add key-id passphrase
))
364 (setq mml1991-epg-secret-key-id-list
365 (cons key-id mml1991-epg-secret-key-id-list
))
366 (copy-sequence passphrase
)))))
368 (defun mml1991-epg-sign (cont)
369 (let ((context (epg-make-context))
370 headers cte signers signature
)
372 (setq signers
(epa-select-keys context
"Select keys for signing.
373 If no one is selected, default secret key is used. "
376 (setq signers
(mapcar (lambda (name)
377 (car (epg-list-keys context name t
)))
379 (epg-context-set-armor context t
)
380 (epg-context-set-textmode context t
)
381 (epg-context-set-signers context signers
)
382 (if mml1991-cache-passphrase
383 (epg-context-set-passphrase-callback
385 #'mml1991-epg-passphrase-callback
))
386 ;; Don't sign headers.
387 (goto-char (point-min))
388 (when (re-search-forward "^$" nil t
)
389 (setq headers
(buffer-substring (point-min) (point)))
391 (narrow-to-region (point-min) (point))
392 (setq cte
(mail-fetch-field "content-transfer-encoding")))
394 (delete-region (point-min) (point))
396 (setq cte
(intern (downcase cte
)))
397 (mm-decode-content-transfer-encoding cte
)))
398 (condition-case error
399 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
400 mml1991-epg-secret-key-id-list nil
)
402 (while mml1991-epg-secret-key-id-list
403 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
404 (setq mml1991-epg-secret-key-id-list
405 (cdr mml1991-epg-secret-key-id-list
)))
406 (signal (car error
) (cdr error
))))
407 (delete-region (point-min) (point-max))
408 (mm-with-unibyte-current-buffer
410 (goto-char (point-min))
411 (while (re-search-forward "\r+$" nil t
)
412 (replace-match "" t t
))
414 (mm-encode-content-transfer-encoding cte
))
415 (goto-char (point-min))
421 (defun mml1991-epg-encrypt (cont &optional sign
)
422 (goto-char (point-min))
423 (when (re-search-forward "^$" nil t
)
424 (let ((cte (save-restriction
425 (narrow-to-region (point-min) (point))
426 (mail-fetch-field "content-transfer-encoding"))))
427 ;; Strip MIME headers since it will be ASCII armored.
429 (delete-region (point-min) (point))
431 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
432 (let ((context (epg-make-context))
434 (if (message-options-get 'message-recipients
)
436 (message-options-get 'message-recipients
)
438 cipher signers config
)
439 ;; We should remove this check if epg-0.0.6 is released.
440 (if (and (condition-case nil
441 (require 'epg-config
)
443 (functionp #'epg-expand-group
))
444 (setq config
(epg-configuration)
447 (mapcar (lambda (recipient)
448 (or (epg-expand-group config recipient
)
453 (epa-select-keys context
"Select recipients for encryption.
454 If no one is selected, symmetric encryption will be performed. "
457 (delq nil
(mapcar (lambda (name)
458 (car (epg-list-keys context name
)))
460 (if mml1991-encrypt-to-self
464 (mapcar (lambda (name)
465 (car (epg-list-keys context name
)))
467 (error "mml1991-signers not set")))
470 (setq signers
(epa-select-keys context
"Select keys for signing.
471 If no one is selected, default secret key is used. "
474 (setq signers
(mapcar (lambda (name)
475 (car (epg-list-keys context name t
)))
477 (epg-context-set-signers context signers
))
478 (epg-context-set-armor context t
)
479 (epg-context-set-textmode context t
)
480 (if mml1991-cache-passphrase
481 (epg-context-set-passphrase-callback
483 #'mml1991-epg-passphrase-callback
))
484 (condition-case error
486 (epg-encrypt-string context
(buffer-string) recipients sign
)
487 mml1991-epg-secret-key-id-list nil
)
489 (while mml1991-epg-secret-key-id-list
490 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
491 (setq mml1991-epg-secret-key-id-list
492 (cdr mml1991-epg-secret-key-id-list
)))
493 (signal (car error
) (cdr error
))))
494 (delete-region (point-min) (point-max))
495 (insert "\n" cipher
))
499 (defun mml1991-encrypt (cont &optional sign
)
500 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
502 (funcall func cont sign
)
503 (error "Cannot find encrypt function"))))
506 (defun mml1991-sign (cont)
507 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
510 (error "Cannot find sign function"))))
515 ;; coding: iso-8859-1
518 ;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
519 ;;; mml1991.el ends here