1 ;;; mml1991.el --- Old PGP message format (RFC 1991) support for MML
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Sascha Lüdecke <sascha@meta-x.de>,
6 ;; Simon Josefsson <simon@josefsson.org> (Mailcrypt interface, Gnus glue)
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; For Emacs <22.2 and XEmacs.
30 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
)))
32 (if (locate-library "password-cache")
33 (require 'password-cache
)
43 (defvar mc-pgp-always-sign
)
45 (autoload 'quoted-printable-decode-region
"qp")
46 (autoload 'quoted-printable-encode-region
"qp")
48 (autoload 'mm-decode-content-transfer-encoding
"mm-bodies")
49 (autoload 'mm-encode-content-transfer-encoding
"mm-bodies")
50 (autoload 'message-options-get
"message")
51 (autoload 'message-options-set
"message")
53 (defvar mml1991-use mml2015-use
54 "The package used for PGP.")
56 (defvar mml1991-function-alist
57 '((mailcrypt mml1991-mailcrypt-sign
58 mml1991-mailcrypt-encrypt
)
63 "Alist of PGP functions.")
65 (defvar mml1991-cache-passphrase mml-secure-cache-passphrase
66 "If t, cache passphrase.")
68 (defvar mml1991-passphrase-cache-expiry mml-secure-passphrase-cache-expiry
69 "How many seconds the passphrase is cached.
70 Whether the passphrase is cached at all is controlled by
71 `mml1991-cache-passphrase'.")
73 (defvar mml1991-signers nil
74 "A list of your own key ID which will be used to sign a message.")
76 (defvar mml1991-encrypt-to-self nil
77 "If t, add your own key ID to recipient list when encryption.")
81 (autoload 'mc-sign-generic
"mc-toplev")
83 (defvar mml1991-decrypt-function
'mailcrypt-decrypt
)
84 (defvar mml1991-verify-function
'mailcrypt-verify
)
86 (defun mml1991-mailcrypt-sign (cont)
87 (let ((text (current-buffer))
89 (result-buffer (get-buffer-create "*GPG Result*")))
90 ;; Save MIME Content[^ ]+: headers from signing
91 (goto-char (point-min))
92 (while (looking-at "^Content[^ ]+:") (forward-line))
94 (setq headers
(buffer-string))
95 (delete-region (point-min) (point)))
96 (goto-char (point-max))
99 (quoted-printable-decode-region (point-min) (point-max))
101 (setq signature
(current-buffer))
102 (insert-buffer-substring text
)
103 (unless (mc-sign-generic (message-options-get 'message-sender
)
105 (unless (> (point-max) (point-min))
106 (pop-to-buffer result-buffer
)
107 (error "Sign error")))
108 (goto-char (point-min))
109 (while (re-search-forward "\r+$" nil t
)
110 (replace-match "" t t
))
111 (quoted-printable-encode-region (point-min) (point-max))
113 (delete-region (point-min) (point-max))
114 (if headers
(insert headers
))
116 (insert-buffer-substring signature
)
117 (goto-char (point-max)))))
119 (declare-function mc-encrypt-generic
"ext:mc-toplev"
120 (&optional recipients scheme start end from sign
))
122 (defun mml1991-mailcrypt-encrypt (cont &optional sign
)
123 (let ((text (current-buffer))
125 (or mc-pgp-always-sign
127 (eq t
(or (message-options-get 'message-sign-encrypt
)
129 'message-sign-encrypt
130 (or (y-or-n-p "Sign the message? ")
134 (result-buffer (get-buffer-create "*GPG Result*")))
135 ;; Strip MIME Content[^ ]: headers since it will be ASCII ARMORED
136 (goto-char (point-min))
137 (while (looking-at "^Content[^ ]+:") (forward-line))
139 (delete-region (point-min) (point)))
141 (inline (mm-disable-multibyte))
142 (setq cipher
(current-buffer))
143 (insert-buffer-substring text
)
144 (unless (mc-encrypt-generic
146 (message-options-get 'message-recipients
)
147 (message-options-set 'message-recipients
148 (read-string "Recipients: ")))
150 (point-min) (point-max)
151 (message-options-get 'message-sender
)
153 (unless (> (point-max) (point-min))
154 (pop-to-buffer result-buffer
)
155 (error "Encrypt error")))
156 (goto-char (point-min))
157 (while (re-search-forward "\r+$" nil t
)
158 (replace-match "" t t
))
160 (delete-region (point-min) (point-max))
161 ;;(insert "Content-Type: application/pgp-encrypted\n\n")
162 ;;(insert "Version: 1\n\n")
164 (insert-buffer-substring cipher
)
165 (goto-char (point-max)))))
169 (autoload 'pgg-sign-region
"pgg")
170 (autoload 'pgg-encrypt-region
"pgg")
172 (defvar pgg-default-user-id
)
173 (defvar pgg-errors-buffer
)
174 (defvar pgg-output-buffer
)
176 (defun mml1991-pgg-sign (cont)
177 (let ((pgg-text-mode t
)
178 (pgg-default-user-id (or (message-options-get 'mml-sender
)
179 pgg-default-user-id
))
181 ;; Don't sign headers.
182 (goto-char (point-min))
183 (when (re-search-forward "^$" nil t
)
184 (setq headers
(buffer-substring (point-min) (point)))
186 (narrow-to-region (point-min) (point))
187 (setq cte
(mail-fetch-field "content-transfer-encoding")))
189 (delete-region (point-min) (point))
191 (setq cte
(intern (downcase cte
)))
192 (mm-decode-content-transfer-encoding cte
)))
193 (unless (pgg-sign-region (point-min) (point-max) t
)
194 (pop-to-buffer pgg-errors-buffer
)
195 (error "Encrypt error"))
196 (delete-region (point-min) (point-max))
197 (mm-with-unibyte-current-buffer
198 (insert-buffer-substring pgg-output-buffer
)
199 (goto-char (point-min))
200 (while (re-search-forward "\r+$" nil t
)
201 (replace-match "" t t
))
203 (mm-encode-content-transfer-encoding cte
))
204 (goto-char (point-min))
210 (defun mml1991-pgg-encrypt (cont &optional sign
)
211 (goto-char (point-min))
212 (when (re-search-forward "^$" nil t
)
213 (let ((cte (save-restriction
214 (narrow-to-region (point-min) (point))
215 (mail-fetch-field "content-transfer-encoding"))))
216 ;; Strip MIME headers since it will be ASCII armored.
218 (delete-region (point-min) (point))
220 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
221 (unless (let ((pgg-text-mode t
))
223 (point-min) (point-max)
226 (message-options-get 'message-recipients
)
227 (message-options-set 'message-recipients
228 (read-string "Recipients: ")))
231 (pop-to-buffer pgg-errors-buffer
)
232 (error "Encrypt error"))
233 (delete-region (point-min) (point-max))
235 (insert-buffer-substring pgg-output-buffer
)
240 (defvar epg-user-id-alist
)
242 (autoload 'epg-make-context
"epg")
243 (autoload 'epg-passphrase-callback-function
"epg")
244 (autoload 'epa-select-keys
"epa")
245 (autoload 'epg-list-keys
"epg")
246 (autoload 'epg-context-set-armor
"epg")
247 (autoload 'epg-context-set-textmode
"epg")
248 (autoload 'epg-context-set-signers
"epg")
249 (autoload 'epg-context-set-passphrase-callback
"epg")
250 (autoload 'epg-key-sub-key-list
"epg")
251 (autoload 'epg-sub-key-capability
"epg")
252 (autoload 'epg-sub-key-validity
"epg")
253 (autoload 'epg-sub-key-fingerprint
"epg")
254 (autoload 'epg-sign-string
"epg")
255 (autoload 'epg-encrypt-string
"epg")
256 (autoload 'epg-configuration
"epg-config")
257 (autoload 'epg-expand-group
"epg-config")
259 (defvar mml1991-epg-secret-key-id-list nil
)
261 (defun mml1991-epg-passphrase-callback (context key-id ignore
)
263 (epg-passphrase-callback-function context key-id nil
)
264 (let* ((entry (assoc key-id epg-user-id-alist
))
267 (format "GnuPG passphrase for %s: "
275 (let ((password-cache-expiry mml1991-passphrase-cache-expiry
))
276 (password-cache-add key-id passphrase
))
277 (setq mml1991-epg-secret-key-id-list
278 (cons key-id mml1991-epg-secret-key-id-list
))
279 (copy-sequence passphrase
)))))
281 (defun mml1991-epg-find-usable-key (keys usage
)
284 (let ((pointer (epg-key-sub-key-list (car keys
))))
285 ;; The primary key will be marked as disabled, when the entire
286 ;; key is disabled (see 12 Field, Format of colon listings, in
287 ;; gnupg/doc/DETAILS)
288 (unless (memq 'disabled
(epg-sub-key-capability (car pointer
)))
290 (if (and (memq usage
(epg-sub-key-capability (car pointer
)))
291 (not (memq (epg-sub-key-validity (car pointer
))
292 '(revoked expired
))))
293 (throw 'found
(car keys
)))
294 (setq pointer
(cdr pointer
)))))
295 (setq keys
(cdr keys
)))))
297 ;; XXX: since gpg --list-secret-keys does not return validity of each
298 ;; key, `mml1991-epg-find-usable-key' defined above is not enough for
299 ;; secret keys. The function `mml1991-epg-find-usable-secret-key'
300 ;; below looks at appropriate public keys to check usability.
301 (defun mml1991-epg-find-usable-secret-key (context name usage
)
302 (let ((secret-keys (epg-list-keys context name t
))
304 (while (and (not secret-key
) secret-keys
)
305 (if (mml1991-epg-find-usable-key
306 (epg-list-keys context
(epg-sub-key-fingerprint
307 (car (epg-key-sub-key-list
308 (car secret-keys
)))))
310 (setq secret-key
(car secret-keys
)
312 (setq secret-keys
(cdr secret-keys
))))
315 (defun mml1991-epg-sign (cont)
316 (let ((context (epg-make-context))
317 headers cte signer-key signers signature
)
318 (if (eq mm-sign-option
'guided
)
319 (setq signers
(epa-select-keys context
"Select keys for signing.
320 If no one is selected, default secret key is used. "
323 (setq signers
(delq nil
327 (mml1991-epg-find-usable-secret-key
329 (unless (or signer-key
332 "No secret key for %s; skip it? "
334 (error "No secret key for %s" name
))
337 (epg-context-set-armor context t
)
338 (epg-context-set-textmode context t
)
339 (epg-context-set-signers context signers
)
340 (if mml1991-cache-passphrase
341 (epg-context-set-passphrase-callback
343 #'mml1991-epg-passphrase-callback
))
344 ;; Don't sign headers.
345 (goto-char (point-min))
346 (when (re-search-forward "^$" nil t
)
347 (setq headers
(buffer-substring (point-min) (point)))
349 (narrow-to-region (point-min) (point))
350 (setq cte
(mail-fetch-field "content-transfer-encoding")))
352 (delete-region (point-min) (point))
354 (setq cte
(intern (downcase cte
)))
355 (mm-decode-content-transfer-encoding cte
)))
356 (condition-case error
357 (setq signature
(epg-sign-string context
(buffer-string) 'clear
)
358 mml1991-epg-secret-key-id-list nil
)
360 (while mml1991-epg-secret-key-id-list
361 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
362 (setq mml1991-epg-secret-key-id-list
363 (cdr mml1991-epg-secret-key-id-list
)))
364 (signal (car error
) (cdr error
))))
365 (delete-region (point-min) (point-max))
366 (mm-with-unibyte-current-buffer
368 (goto-char (point-min))
369 (while (re-search-forward "\r+$" nil t
)
370 (replace-match "" t t
))
372 (mm-encode-content-transfer-encoding cte
))
373 (goto-char (point-min))
379 (defun mml1991-epg-encrypt (cont &optional sign
)
380 (goto-char (point-min))
381 (when (re-search-forward "^$" nil t
)
382 (let ((cte (save-restriction
383 (narrow-to-region (point-min) (point))
384 (mail-fetch-field "content-transfer-encoding"))))
385 ;; Strip MIME headers since it will be ASCII armored.
387 (delete-region (point-min) (point))
389 (mm-decode-content-transfer-encoding (intern (downcase cte
))))))
390 (let ((context (epg-make-context))
392 (if (message-options-get 'message-recipients
)
394 (message-options-get 'message-recipients
)
396 recipient-key signer-key cipher signers config
)
397 (when mml1991-encrypt-to-self
398 (unless mml1991-signers
399 (error "mml1991-signers is not set"))
400 (setq recipients
(nconc recipients mml1991-signers
)))
401 ;; We should remove this check if epg-0.0.6 is released.
402 (if (and (condition-case nil
403 (require 'epg-config
)
405 (functionp #'epg-expand-group
))
406 (setq config
(epg-configuration)
409 (mapcar (lambda (recipient)
410 (or (epg-expand-group config recipient
)
413 (if (eq mm-encrypt-option
'guided
)
415 (epa-select-keys context
"Select recipients for encryption.
416 If no one is selected, symmetric encryption will be performed. "
421 (setq recipient-key
(mml1991-epg-find-usable-key
422 (epg-list-keys context name
)
424 (unless (or recipient-key
426 (format "No public key for %s; skip it? "
428 (error "No public key for %s" name
))
432 (error "No recipient specified")))
434 (if (eq mm-sign-option
'guided
)
435 (setq signers
(epa-select-keys context
"Select keys for signing.
436 If no one is selected, default secret key is used. "
439 (setq signers
(delq nil
442 (mml1991-epg-find-usable-secret-key
445 (epg-context-set-signers context signers
))
446 (epg-context-set-armor context t
)
447 (epg-context-set-textmode context t
)
448 (if mml1991-cache-passphrase
449 (epg-context-set-passphrase-callback
451 #'mml1991-epg-passphrase-callback
))
452 (condition-case error
454 (epg-encrypt-string context
(buffer-string) recipients sign
)
455 mml1991-epg-secret-key-id-list nil
)
457 (while mml1991-epg-secret-key-id-list
458 (password-cache-remove (car mml1991-epg-secret-key-id-list
))
459 (setq mml1991-epg-secret-key-id-list
460 (cdr mml1991-epg-secret-key-id-list
)))
461 (signal (car error
) (cdr error
))))
462 (delete-region (point-min) (point-max))
463 (insert "\n" cipher
))
467 (defun mml1991-encrypt (cont &optional sign
)
468 (let ((func (nth 2 (assq mml1991-use mml1991-function-alist
))))
470 (funcall func cont sign
)
471 (error "Cannot find encrypt function"))))
474 (defun mml1991-sign (cont)
475 (let ((func (nth 1 (assq mml1991-use mml1991-function-alist
))))
478 (error "Cannot find sign function"))))
483 ;; coding: iso-8859-1
486 ;;; mml1991.el ends here