1 ;;; mml2015.el --- MIME Security with Pretty Good Privacy (PGP)
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: PGP MIME MML
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
31 (defvar mml2015-use (or (progn (ignore-errors
33 (and (fboundp 'mc-encrypt-generic)
34 (fboundp 'mc-sign-generic)
35 (fboundp 'mc-cleanup-recipient-headers)
40 (and (fboundp 'gpg-sign-detached)
42 "The package used for PGP/MIME.")
44 ;; Something is not RFC2015.
45 (defvar mml2015-function-alist
46 '((mailcrypt mml2015-mailcrypt-sign
47 mml2015-mailcrypt-encrypt
48 mml2015-mailcrypt-verify
49 mml2015-mailcrypt-decrypt
50 mml2015-mailcrypt-clear-verify
51 mml2015-mailcrypt-clear-decrypt)
57 mml2015-gpg-clear-decrypt))
58 "Alist of PGP/MIME functions.")
60 (defvar mml2015-result-buffer nil)
65 (autoload 'mailcrypt-decrypt "mailcrypt")
66 (autoload 'mailcrypt-verify "mailcrypt")
67 (autoload 'mc-pgp-always-sign "mailcrypt")
68 (autoload 'mc-encrypt-generic "mc-toplev")
69 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
70 (autoload 'mc-sign-generic "mc-toplev"))
73 (defvar mc-default-scheme)
76 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
77 (defvar mml2015-verify-function 'mailcrypt-verify)
79 (defun mml2015-mailcrypt-decrypt (handle ctl)
81 (let (child handles result)
82 (unless (setq child (mm-find-part-by-type
84 "application/octet-stream" nil t))
85 (mm-set-handle-multipart-parameter
86 mm-security-handle 'gnus-info "Corrupted")
87 (throw 'error handle))
89 (mm-insert-part child)
92 (funcall mml2015-decrypt-function)
94 (mm-set-handle-multipart-parameter
95 mm-security-handle 'gnus-details (cadr err))
98 (mm-set-handle-multipart-parameter
99 mm-security-handle 'gnus-details "Quit.")
102 (mm-set-handle-multipart-parameter
103 mm-security-handle 'gnus-info "Failed")
104 (throw 'error handle))
105 (setq handles (mm-dissect-buffer t)))
106 (mm-destroy-parts handle)
107 (mm-set-handle-multipart-parameter
108 mm-security-handle 'gnus-info "OK")
109 (if (listp (car handles))
113 (defun mml2015-mailcrypt-clear-decrypt ()
117 (funcall mml2015-decrypt-function)
119 (mm-set-handle-multipart-parameter
120 mm-security-handle 'gnus-details (cadr err))
123 (mm-set-handle-multipart-parameter
124 mm-security-handle 'gnus-details "Quit.")
127 (mm-set-handle-multipart-parameter
128 mm-security-handle 'gnus-info "OK")
129 (mm-set-handle-multipart-parameter
130 mm-security-handle 'gnus-info "Failed"))))
132 (defun mml2015-fix-micalg (alg)
134 (if (and alg (string-match "^pgp-" alg))
135 (substring alg (match-end 0))
138 (defun mml2015-mailcrypt-verify (handle ctl)
141 (unless (setq part (mm-find-raw-part-by-type
142 ctl (or (mail-content-type-get ctl 'protocol)
143 "application/pgp-signature")
145 (mm-set-handle-multipart-parameter
146 mm-security-handle 'gnus-info "Corrupted")
147 (throw 'error handle))
149 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
150 (insert (format "Hash: %s\n\n"
151 (or (mml2015-fix-micalg
152 (mail-content-type-get ctl 'micalg))
155 (narrow-to-region (point) (point))
157 (goto-char (point-min))
159 (if (looking-at "^-")
162 (unless (setq part (mm-find-part-by-type
163 (cdr handle) "application/pgp-signature" nil t))
164 (mm-set-handle-multipart-parameter
165 mm-security-handle 'gnus-info "Corrupted")
166 (throw 'error handle))
168 (narrow-to-region (point) (point))
169 (mm-insert-part part)
170 (goto-char (point-min))
171 (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
172 (replace-match "BEGIN PGP SIGNATURE" t t nil 1))
173 (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
174 (replace-match "END PGP SIGNATURE" t t nil 1)))
175 (unless (condition-case err
176 (funcall mml2015-verify-function)
178 (mm-set-handle-multipart-parameter
179 mm-security-handle 'gnus-details (cadr err))
182 (mm-set-handle-multipart-parameter
183 mm-security-handle 'gnus-details "Quit.")
185 (mm-set-handle-multipart-parameter
186 mm-security-handle 'gnus-info "Failed")
187 (throw 'error handle)))
188 (mm-set-handle-multipart-parameter
189 mm-security-handle 'gnus-info "OK")
192 (defun mml2015-mailcrypt-clear-verify ()
193 (if (condition-case err
194 (funcall mml2015-verify-function)
196 (mm-set-handle-multipart-parameter
197 mm-security-handle 'gnus-details (cadr err))
200 (mm-set-handle-multipart-parameter
201 mm-security-handle 'gnus-details "Quit.")
203 (mm-set-handle-multipart-parameter
204 mm-security-handle 'gnus-info "OK")
205 (mm-set-handle-multipart-parameter
206 mm-security-handle 'gnus-info "Failed")))
208 (defun mml2015-mailcrypt-sign (cont)
209 (mc-sign-generic (message-options-get 'message-sender)
212 (funcall mml-boundary-function (incf mml-multipart-number)))
214 (goto-char (point-min))
215 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
216 (error "Cannot find signed begin line." ))
217 (goto-char (match-beginning 0))
219 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
220 (error "Cannot not find PGP hash." ))
221 (setq hash (match-string 1))
222 (unless (re-search-forward "^$" nil t)
223 (error "Cannot not find PGP message." ))
225 (delete-region (point-min) (point))
226 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
228 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
230 (insert (format "\n--%s\n" boundary))
232 (goto-char (point-max))
233 (unless (re-search-backward "^-----END PGP \\(SIGNATURE\\)-----\r?$" nil t)
234 (error "Cannot find signature part." ))
235 (replace-match "MESSAGE" t t nil 1)
236 (goto-char (match-beginning 0))
237 (unless (re-search-backward "^-----BEGIN PGP \\(SIGNATURE\\)-----\r?$"
239 (error "Cannot find signature part." ))
240 (replace-match "MESSAGE" t t nil 1)
241 (goto-char (match-beginning 0))
243 (narrow-to-region point (point))
245 (while (re-search-forward "^- -" nil t)
246 (replace-match "-" t t))
247 (goto-char (point-max)))
248 (insert (format "--%s\n" boundary))
249 (insert "Content-Type: application/pgp-signature\n\n")
250 (goto-char (point-max))
251 (insert (format "--%s--\n" boundary))
252 (goto-char (point-max))))
254 (defun mml2015-mailcrypt-encrypt (cont)
255 (let ((mc-pgp-always-sign
256 (or mc-pgp-always-sign
257 (eq t (or (message-options-get 'message-sign-encrypt)
259 'message-sign-encrypt
260 (or (y-or-n-p "Sign the message? ")
264 (or (message-options-get 'message-recipients)
265 (message-options-set 'message-recipients
266 (mc-cleanup-recipient-headers
267 (read-string "Recipients: "))))
269 (message-options-get 'message-sender)))
271 (funcall mml-boundary-function (incf mml-multipart-number))))
272 (goto-char (point-min))
273 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
275 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
276 (insert (format "--%s\n" boundary))
277 (insert "Content-Type: application/pgp-encrypted\n\n")
278 (insert "Version: 1\n\n")
279 (insert (format "--%s\n" boundary))
280 (insert "Content-Type: application/octet-stream\n\n")
281 (goto-char (point-max))
282 (insert (format "--%s--\n" boundary))
283 (goto-char (point-max))))
288 (autoload 'gpg-decrypt "gpg")
289 (autoload 'gpg-verify "gpg")
290 (autoload 'gpg-sign-detached "gpg")
291 (autoload 'gpg-sign-encrypt "gpg")
292 (autoload 'gpg-passphrase-read "gpg"))
294 (defun mml2015-gpg-passphrase ()
295 (or (message-options-get 'gpg-passphrase)
296 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
298 (defun mml2015-gpg-decrypt-1 ()
299 (let ((cipher (current-buffer)) plain result)
300 (if (with-temp-buffer
302 (gpg-decrypt cipher (setq plain (current-buffer))
303 mml2015-result-buffer nil)
306 (insert-buffer plain)))
308 ;; Some wrong with the return value, check plain text buffer.
309 (if (> (point-max) (point-min))
311 (mm-set-handle-multipart-parameter
312 mm-security-handle 'gnus-details
313 (with-current-buffer mml2015-result-buffer
317 (defun mml2015-gpg-decrypt (handle ctl)
318 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
319 (mml2015-mailcrypt-decrypt handle ctl)))
321 (defun mml2015-gpg-clear-decrypt ()
323 (setq result (mml2015-gpg-decrypt-1))
325 (mm-set-handle-multipart-parameter
326 mm-security-handle 'gnus-info "OK")
327 (mm-set-handle-multipart-parameter
328 mm-security-handle 'gnus-info "Failed"))))
330 (defun mml2015-gpg-verify (handle ctl)
332 (let (part message signature)
333 (unless (setq part (mm-find-raw-part-by-type
334 ctl (or (mail-content-type-get ctl 'protocol)
335 "application/pgp-signature")
337 (mm-set-handle-multipart-parameter
338 mm-security-handle 'gnus-info "Corrupted")
339 (throw 'error handle))
341 (setq message (current-buffer))
344 (setq signature (current-buffer))
345 (unless (setq part (mm-find-part-by-type
346 (cdr handle) "application/pgp-signature" nil t))
347 (mm-set-handle-multipart-parameter
348 mm-security-handle 'gnus-info "Corrupted")
349 (throw 'error handle))
350 (mm-insert-part part)
351 (unless (condition-case err
352 (gpg-verify message signature mml2015-result-buffer)
354 (mm-set-handle-multipart-parameter
355 mm-security-handle 'gnus-details (cadr err))
358 (mm-set-handle-multipart-parameter
359 mm-security-handle 'gnus-details "Quit.")
361 (mm-set-handle-multipart-parameter
362 mm-security-handle 'gnus-details
363 (with-current-buffer mml2015-result-buffer
365 (mm-set-handle-multipart-parameter
366 mm-security-handle 'gnus-info "Failed")
367 (throw 'error handle)))
368 (mm-set-handle-multipart-parameter
369 mm-security-handle 'gnus-info "OK"))
372 (defun mml2015-gpg-sign (cont)
374 (funcall mml-boundary-function (incf mml-multipart-number)))
375 (text (current-buffer)) signature)
376 (goto-char (point-max))
380 (unless (gpg-sign-detached text (setq signature (current-buffer))
381 mml2015-result-buffer
383 (message-options-get 'message-sender)
384 t t) ; armor & textmode
385 (unless (> (point-max) (point-min))
386 (pop-to-buffer mml2015-result-buffer)
387 (error "Sign error.")))
389 (goto-char (point-min))
390 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
392 ;;; FIXME: what is the micalg?
393 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
394 (insert (format "\n--%s\n" boundary))
395 (goto-char (point-max))
396 (insert (format "\n--%s\n" boundary))
397 (insert "Content-Type: application/pgp-signature\n\n")
398 (insert-buffer signature)
399 (goto-char (point-max))
400 (insert (format "--%s--\n" boundary))
401 (goto-char (point-max)))))
403 (defun mml2015-gpg-encrypt (cont)
405 (funcall mml-boundary-function (incf mml-multipart-number)))
406 (text (current-buffer))
409 (unless (gpg-sign-encrypt
410 text (setq cipher (current-buffer))
411 mml2015-result-buffer
414 (message-options-get 'message-recipients)
415 (message-options-set 'message-recipients
416 (read-string "Recipients: ")))
419 (message-options-get 'message-sender)
420 t t) ; armor & textmode
421 (unless (> (point-max) (point-min))
422 (pop-to-buffer mml2015-result-buffer)
423 (error "Encrypt error.")))
425 (delete-region (point-min) (point-max))
426 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
428 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
429 (insert (format "--%s\n" boundary))
430 (insert "Content-Type: application/pgp-encrypted\n\n")
431 (insert "Version: 1\n\n")
432 (insert (format "--%s\n" boundary))
433 (insert "Content-Type: application/octet-stream\n\n")
434 (insert-buffer cipher)
435 (goto-char (point-max))
436 (insert (format "--%s--\n" boundary))
437 (goto-char (point-max)))))
441 (defun mml2015-clean-buffer ()
442 (if (gnus-buffer-live-p mml2015-result-buffer)
443 (with-current-buffer mml2015-result-buffer
446 (setq mml2015-result-buffer
447 (gnus-get-buffer-create "*MML2015 Result*"))
450 (defsubst mml2015-clear-decrypt-function ()
451 (nth 6 (assq mml2015-use mml2015-function-alist)))
453 (defsubst mml2015-clear-verify-function ()
454 (nth 5 (assq mml2015-use mml2015-function-alist)))
457 (defun mml2015-decrypt (handle ctl)
458 (mml2015-clean-buffer)
459 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
461 (funcall func handle ctl)
465 (defun mml2015-decrypt-test (handle ctl)
469 (defun mml2015-verify (handle ctl)
470 (mml2015-clean-buffer)
471 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
473 (funcall func handle ctl)
477 (defun mml2015-verify-test (handle ctl)
481 (defun mml2015-encrypt (cont)
482 (mml2015-clean-buffer)
483 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
486 (error "Cannot find encrypt function."))))
489 (defun mml2015-sign (cont)
490 (mml2015-clean-buffer)
491 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
494 (error "Cannot find sign function."))))
497 (defun mml2015-self-encrypt ()
498 (mml2015-encrypt nil))
502 ;;; mml2015.el ends here