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/>.
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
)
66 "Alist of PGP functions.")
68 (defvar mml1991-verbose mml-secure-verbose
69 "If non-nil, ask the user about the current operation more verbosely.")
71 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
72 "If t, cache passphrase.")
74 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
75 "How many seconds the passphrase is cached.
76 Whether the passphrase is cached at all is controlled by
77 `mml1991-cache-passphrase'.")
79 (defvar mml1991-signers nil
80 "A list of your own key ID which will be used to sign a message.")
82 (defvar mml1991-encrypt-to-self nil
83 "If t, add your own key ID to recipient list when encryption.")
87 (autoload 'mc-sign-generic
"mc-toplev")
89 (defvar mml1991-decrypt-function
'mailcrypt-decrypt
)
90 (defvar mml1991-verify-function
'mailcrypt-verify
)
92 (defun mml1991-mailcrypt-sign (cont)
93 (let ((text (current-buffer))
95 (result-buffer (get-buffer-create "*GPG Result*")))
96 ;; Save MIME Content[^ ]+: headers from signing
97 (goto-char (point-min))
98 (while (looking-at "^Content[^ ]+:") (forward-line))
100 (setq headers
(buffer-string))
101 (delete-region (point-min) (point)))
102 (goto-char (point-max))
105 (quoted-printable-decode-region (point-min) (point-max))
107 (setq signature
(current-buffer))
108 (insert-buffer-substring text
)
109 (unless (mc-sign-generic (message-options-get 'message-sender
)
111 (unless (> (point-max) (point-min))
112 (pop-to-buffer result-buffer
)
113 (error "Sign error")))
114 (goto-char (point-min))
115 (while (re-search-forward "\r+$" nil t
)
116 (replace-match "" t t
))
117 (quoted-printable-encode-region (point-min) (point-max))
119 (delete-region (point-min) (point-max))
120 (if headers
(insert headers
))
122 (insert-buffer-substring signature
)
123 (goto-char (point-max)))))
125 (declare-function mc-encrypt-generic
"ext:mc-toplev"
126 (&optional recipients scheme start end from sign
))
128 (defun mml1991-mailcrypt-encrypt (cont &optional sign
)
129 (let ((text (current-buffer))
131 (or mc-pgp-always-sign
133 (eq t
(or (message-options-get 'message-sign-encrypt
)
135 'message-sign-encrypt
136 (or (y-or-n-p "Sign the message? ")
140 (result-buffer (get-buffer-create "*GPG Result*")))
141 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
142 (goto-char (point-min))
143 (while (looking-at "^Content[^ ]+:") (forward-line))
145 (delete-region (point-min) (point)))
146 (mm-with-unibyte-current-buffer
148 (setq cipher
(current-buffer))
149 (insert-buffer-substring text
)
150 (unless (mc-encrypt-generic
152 (message-options-get 'message-recipients
)
153 (message-options-set 'message-recipients
154 (read-string "Recipients: ")))
156 (point-min) (point-max)
157 (message-options-get 'message-sender
)
159 (unless (> (point-max) (point-min))
160 (pop-to-buffer result-buffer
)
161 (error "Encrypt error")))
162 (goto-char (point-min))
163 (while (re-search-forward "\r+$" nil t
)
164 (replace-match "" t t
))
166 (delete-region (point-min) (point-max))
167 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
168 ;;(insert "Version: 1\n\n")
170 (insert-buffer-substring cipher
)
171 (goto-char (point-max))))))
175 (autoload 'gpg-sign-cleartext
"gpg")
177 (declare-function gpg-sign-encrypt
"ext:gpg"
178 (plaintext ciphertext result recipients
&optional
179 passphrase sign-with-key armor textmode
))
180 (declare-function gpg-encrypt
"ext:gpg"
181 (plaintext ciphertext result recipients
&optional
182 passphrase armor textmode
))
184 (defun mml1991-gpg-sign (cont)
185 (let ((text (current-buffer))
187 (result-buffer (get-buffer-create "*GPG Result*")))
188 ;; Save MIME Content[^ ]+: headers from signing
189 (goto-char (point-min))
190 (while (looking-at "^Content[^ ]+:") (forward-line))
192 (setq headers
(buffer-string))
193 (delete-region (point-min) (point)))
194 (goto-char (point-max))
197 (quoted-printable-decode-region (point-min) (point-max))
199 (unless (gpg-sign-cleartext text
(setq signature
(current-buffer))
202 (message-options-get 'message-sender
))
203 (unless (> (point-max) (point-min))
204 (pop-to-buffer result-buffer
)
205 (error "Sign error")))
206 (goto-char (point-min))
207 (while (re-search-forward "\r+$" nil t
)
208 (replace-match "" t t
))
209 (quoted-printable-encode-region (point-min) (point-max))
211 (delete-region (point-min) (point-max))
212 (if headers
(insert headers
))
214 (insert-buffer-substring signature
)
215 (goto-char (point-max)))))
217 (defun mml1991-gpg-encrypt (cont &optional sign
)
218 (let ((text (current-buffer))
220 (result-buffer (get-buffer-create "*GPG Result*")))
221 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
222 (goto-char (point-min))
223 (while (looking-at "^Content[^ ]+:") (forward-line))
225 (delete-region (point-min) (point)))
226 (mm-with-unibyte-current-buffer
228 (flet ((gpg-encrypt-func
229 (sign plaintext ciphertext result recipients
&optional
230 passphrase sign-with-key armor textmode
)
233 plaintext ciphertext result recipients passphrase
234 sign-with-key armor textmode
)
236 plaintext ciphertext result recipients passphrase
238 (unless (gpg-encrypt-func
240 text
(setq cipher
(current-buffer))
244 (message-options-get 'message-recipients
)
245 (message-options-set 'message-recipients
246 (read-string "Recipients: ")))
249 (message-options-get 'message-sender
)
250 t t
) ; armor & textmode
251 (unless (> (point-max) (point-min))
252 (pop-to-buffer result-buffer
)
253 (error "Encrypt error"))))
254 (goto-char (point-min))
255 (while (re-search-forward "\r+$" nil t
)
256 (replace-match "" t t
))
258 (delete-region (point-min) (point-max))
259 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
260 ;;(insert "Version: 1\n\n")
262 (insert-buffer-substring cipher
)
263 (goto-char (point-max))))))
267 (defvar pgg-default-user-id
)
268 (defvar pgg-errors-buffer
)
269 (defvar pgg-output-buffer
)
271 (defun mml1991-pgg-sign (cont)
272 (let ((pgg-text-mode t
)
273 (pgg-default-user-id (or (message-options-get 'mml-sender
)
274 pgg-default-user-id
))
276 ;; Don't sign headers.
277 (goto-char (point-min))
278 (when (re-search-forward "^$" nil t
)
279 (setq headers
(buffer-substring (point-min) (point)))
281 (narrow-to-region (point-min) (point))
282 (setq cte
(mail-fetch-field "content-transfer-encoding")))
284 (delete-region (point-min) (point))
286 (setq cte
(intern (downcase cte
)))
287 (mm-decode-content-transfer-encoding cte
)))
288 (unless (pgg-sign-region (point-min) (point-max) t
)
289 (pop-to-buffer pgg-errors-buffer
)
290 (error "Encrypt error"))
291 (delete-region (point-min) (point-max))
292 (mm-with-unibyte-current-buffer
293 (insert-buffer-substring pgg-output-buffer
)
294 (goto-char (point-min))
295 (while (re-search-forward "\r+$" nil t
)
296 (replace-match "" t t
))
298 (mm-encode-content-transfer-encoding cte
))
299 (goto-char (point-min))
305 (defun mml1991-pgg-encrypt (cont &optional sign
)
306 (goto-char (point-min))
307 (when (re-search-forward "^$" nil t
)
308 (let ((cte (save-restriction
309 (narrow-to-region (point-min) (point))
310 (mail-fetch-field "content-transfer-encoding"))))
311 ;; Strip MIME headers since it will be ASCII armored.
313 (delete-region (point-min) (point))
315 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
316 (unless (let ((pgg-text-mode t
))
318 (point-min) (point-max)
321 (message-options-get 'message-recipients
)
322 (message-options-set 'message-recipients
323 (read-string "Recipients: ")))
326 (pop-to-buffer pgg-errors-buffer
)
327 (error "Encrypt error"))
328 (delete-region (point-min) (point-max))
330 (insert-buffer-substring pgg-output-buffer
)
335 (defvar epg-user-id-alist
)
337 (autoload 'epg-make-context
"epg")
338 (autoload 'epg-passphrase-callback-function
"epg")
339 (autoload 'epa-select-keys
"epa")
340 (autoload 'epg-list-keys
"epg")
341 (autoload 'epg-context-set-armor
"epg")
342 (autoload 'epg-context-set-textmode
"epg")
343 (autoload 'epg-context-set-signers
"epg")
344 (autoload 'epg-context-set-passphrase-callback
"epg")
345 (autoload 'epg-sign-string
"epg")
346 (autoload 'epg-encrypt-string
"epg")
347 (autoload 'epg-configuration
"epg-config")
348 (autoload 'epg-expand-group
"epg-config")
350 (defvar mml1991-epg-secret-key-id-list nil
)
352 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
354 (epg-passphrase-callback-function context key-id nil
)
355 (let* ((entry (assoc key-id epg-user-id-alist
))
358 (format "GnuPG passphrase for %s: "
366 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
367 (password-cache-add key-id passphrase
))
368 (setq mml1991-epg-secret-key-id-list
369 (cons key-id mml1991-epg-secret-key-id-list
))
370 (copy-sequence passphrase
)))))
372 (defun mml1991-epg-sign (cont)
373 (let ((context (epg-make-context))
374 headers cte signers signature
)
375 (if (eq mm-sign-option
'guided
)
376 (setq signers
(epa-select-keys context
"Select keys for signing.
377 If no one is selected, default secret key is used. "
380 (setq signers
(mapcar (lambda (name)
381 (car (epg-list-keys context name t
)))
383 (epg-context-set-armor context t
)
384 (epg-context-set-textmode context t
)
385 (epg-context-set-signers context signers
)
386 (if mml1991-cache-passphrase
387 (epg-context-set-passphrase-callback
389 #'mml1991-epg-passphrase-callback
))
390 ;; Don't sign headers.
391 (goto-char (point-min))
392 (when (re-search-forward "^$" nil t
)
393 (setq headers
(buffer-substring (point-min) (point)))
395 (narrow-to-region (point-min) (point))
396 (setq cte
(mail-fetch-field "content-transfer-encoding")))
398 (delete-region (point-min) (point))
400 (setq cte
(intern (downcase cte
)))
401 (mm-decode-content-transfer-encoding cte
)))
402 (condition-case error
403 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
404 mml1991-epg-secret-key-id-list nil
)
406 (while mml1991-epg-secret-key-id-list
407 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
408 (setq mml1991-epg-secret-key-id-list
409 (cdr mml1991-epg-secret-key-id-list
)))
410 (signal (car error
) (cdr error
))))
411 (delete-region (point-min) (point-max))
412 (mm-with-unibyte-current-buffer
414 (goto-char (point-min))
415 (while (re-search-forward "\r+$" nil t
)
416 (replace-match "" t t
))
418 (mm-encode-content-transfer-encoding cte
))
419 (goto-char (point-min))
425 (defun mml1991-epg-encrypt (cont &optional sign
)
426 (goto-char (point-min))
427 (when (re-search-forward "^$" nil t
)
428 (let ((cte (save-restriction
429 (narrow-to-region (point-min) (point))
430 (mail-fetch-field "content-transfer-encoding"))))
431 ;; Strip MIME headers since it will be ASCII armored.
433 (delete-region (point-min) (point))
435 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
436 (let ((context (epg-make-context))
438 (if (message-options-get 'message-recipients
)
440 (message-options-get 'message-recipients
)
442 cipher signers config
)
443 ;; We should remove this check if epg-0.0.6 is released.
444 (if (and (condition-case nil
445 (require 'epg-config
)
447 (functionp #'epg-expand-group
))
448 (setq config
(epg-configuration)
451 (mapcar (lambda (recipient)
452 (or (epg-expand-group config recipient
)
455 (if (eq mm-encrypt-option
'guided
)
457 (epa-select-keys context
"Select recipients for encryption.
458 If no one is selected, symmetric encryption will be performed. "
461 (delq nil
(mapcar (lambda (name)
462 (car (epg-list-keys context name
)))
464 (if mml1991-encrypt-to-self
468 (mapcar (lambda (name)
469 (car (epg-list-keys context name
)))
471 (error "mml1991-signers not set")))
473 (if (eq mm-sign-option
'guided
)
474 (setq signers
(epa-select-keys context
"Select keys for signing.
475 If no one is selected, default secret key is used. "
478 (setq signers
(mapcar (lambda (name)
479 (car (epg-list-keys context name t
)))
481 (epg-context-set-signers context signers
))
482 (epg-context-set-armor context t
)
483 (epg-context-set-textmode context t
)
484 (if mml1991-cache-passphrase
485 (epg-context-set-passphrase-callback
487 #'mml1991-epg-passphrase-callback
))
488 (condition-case error
490 (epg-encrypt-string context
(buffer-string) recipients sign
)
491 mml1991-epg-secret-key-id-list nil
)
493 (while mml1991-epg-secret-key-id-list
494 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
495 (setq mml1991-epg-secret-key-id-list
496 (cdr mml1991-epg-secret-key-id-list
)))
497 (signal (car error
) (cdr error
))))
498 (delete-region (point-min) (point-max))
499 (insert "\n" cipher
))
503 (defun mml1991-encrypt (cont &optional sign
)
504 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
506 (funcall func cont sign
)
507 (error "Cannot find encrypt function"))))
510 (defun mml1991-sign (cont)
511 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
514 (error "Cannot find sign function"))))
519 ;; coding: iso-8859-1
522 ;; arch-tag: e542be18-ab28-4393-9b33-97fe9cf30706
523 ;;; mml1991.el ends here