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))
231 (goto-char (point-max))
232 (unless (re-search-backward "^-----END PGP \\(SIGNATURE\\)-----\r?$" nil t)
233 (error "Cannot find signature part." ))
234 (replace-match "MESSAGE" t t nil 1)
235 (goto-char (match-beginning 0))
236 (unless (re-search-backward "^-----BEGIN PGP \\(SIGNATURE\\)-----\r?$"
238 (error "Cannot find signature part." ))
239 (replace-match "MESSAGE" t t nil 1)
240 (goto-char (match-beginning 0))
241 (insert (format "--%s\n" boundary))
242 (insert "Content-Type: application/pgp-signature\n\n")
243 (goto-char (point-max))
244 (insert (format "--%s--\n" boundary))
245 (goto-char (point-max))))
247 (defun mml2015-mailcrypt-encrypt (cont)
248 (let ((mc-pgp-always-sign
249 (or mc-pgp-always-sign
250 (eq t (or (message-options-get 'message-sign-encrypt)
252 'message-sign-encrypt
253 (or (y-or-n-p "Sign the message? ")
257 (or (message-options-get 'message-recipients)
258 (message-options-set 'message-recipients
259 (mc-cleanup-recipient-headers
260 (read-string "Recipients: "))))
262 (message-options-get 'message-sender)))
264 (funcall mml-boundary-function (incf mml-multipart-number))))
265 (goto-char (point-min))
266 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
268 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
269 (insert (format "--%s\n" boundary))
270 (insert "Content-Type: application/pgp-encrypted\n\n")
271 (insert "Version: 1\n\n")
272 (insert (format "--%s\n" boundary))
273 (insert "Content-Type: application/octet-stream\n\n")
274 (goto-char (point-max))
275 (insert (format "--%s--\n" boundary))
276 (goto-char (point-max))))
281 (autoload 'gpg-decrypt "gpg")
282 (autoload 'gpg-verify "gpg")
283 (autoload 'gpg-sign-detached "gpg")
284 (autoload 'gpg-sign-encrypt "gpg")
285 (autoload 'gpg-passphrase-read "gpg"))
287 (defun mml2015-gpg-passphrase ()
288 (or (message-options-get 'gpg-passphrase)
289 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
291 (defun mml2015-gpg-decrypt-1 ()
292 (let ((cipher (current-buffer)) plain result)
293 (if (with-temp-buffer
295 (gpg-decrypt cipher (setq plain (current-buffer))
296 mml2015-result-buffer nil)
299 (insert-buffer plain)))
301 ;; Some wrong with the return value, check plain text buffer.
302 (if (> (point-max) (point-min))
304 (mm-set-handle-multipart-parameter
305 mm-security-handle 'gnus-details
306 (with-current-buffer mml2015-result-buffer
310 (defun mml2015-gpg-decrypt (handle ctl)
311 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
312 (mml2015-mailcrypt-decrypt handle ctl)))
314 (defun mml2015-gpg-clear-decrypt ()
316 (setq result (mml2015-gpg-decrypt-1))
318 (mm-set-handle-multipart-parameter
319 mm-security-handle 'gnus-info "OK")
320 (mm-set-handle-multipart-parameter
321 mm-security-handle 'gnus-info "Failed"))))
323 (defun mml2015-gpg-verify (handle ctl)
325 (let (part message signature)
326 (unless (setq part (mm-find-raw-part-by-type
327 ctl (or (mail-content-type-get ctl 'protocol)
328 "application/pgp-signature")
330 (mm-set-handle-multipart-parameter
331 mm-security-handle 'gnus-info "Corrupted")
332 (throw 'error handle))
334 (setq message (current-buffer))
337 (setq signature (current-buffer))
338 (unless (setq part (mm-find-part-by-type
339 (cdr handle) "application/pgp-signature" nil t))
340 (mm-set-handle-multipart-parameter
341 mm-security-handle 'gnus-info "Corrupted")
342 (throw 'error handle))
343 (mm-insert-part part)
344 (unless (condition-case err
345 (gpg-verify message signature mml2015-result-buffer)
347 (mm-set-handle-multipart-parameter
348 mm-security-handle 'gnus-details (cadr err))
351 (mm-set-handle-multipart-parameter
352 mm-security-handle 'gnus-details "Quit.")
354 (mm-set-handle-multipart-parameter
355 mm-security-handle 'gnus-details
356 (with-current-buffer mml2015-result-buffer
358 (mm-set-handle-multipart-parameter
359 mm-security-handle 'gnus-info "Failed")
360 (throw 'error handle)))
361 (mm-set-handle-multipart-parameter
362 mm-security-handle 'gnus-info "OK"))
365 (defun mml2015-gpg-sign (cont)
367 (funcall mml-boundary-function (incf mml-multipart-number)))
368 (text (current-buffer)) signature)
369 (goto-char (point-max))
373 (unless (gpg-sign-detached text (setq signature (current-buffer))
374 mml2015-result-buffer
376 (message-options-get 'message-sender)
377 t t) ; armor & textmode
378 (unless (> (point-max) (point-min))
379 (pop-to-buffer mml2015-result-buffer)
380 (error "Sign error.")))
382 (goto-char (point-min))
383 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
385 ;;; FIXME: what is the micalg?
386 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
387 (insert (format "\n--%s\n" boundary))
388 (goto-char (point-max))
389 (insert (format "\n--%s\n" boundary))
390 (insert "Content-Type: application/pgp-signature\n\n")
391 (insert-buffer signature)
392 (goto-char (point-max))
393 (insert (format "--%s--\n" boundary))
394 (goto-char (point-max)))))
396 (defun mml2015-gpg-encrypt (cont)
398 (funcall mml-boundary-function (incf mml-multipart-number)))
399 (text (current-buffer))
402 (unless (gpg-sign-encrypt
403 text (setq cipher (current-buffer))
404 mml2015-result-buffer
407 (message-options-get 'message-recipients)
408 (message-options-set 'message-recipients
409 (read-string "Recipients: ")))
412 (message-options-get 'message-sender)
413 t t) ; armor & textmode
414 (unless (> (point-max) (point-min))
415 (pop-to-buffer mml2015-result-buffer)
416 (error "Encrypt error.")))
418 (delete-region (point-min) (point-max))
419 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
421 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
422 (insert (format "--%s\n" boundary))
423 (insert "Content-Type: application/pgp-encrypted\n\n")
424 (insert "Version: 1\n\n")
425 (insert (format "--%s\n" boundary))
426 (insert "Content-Type: application/octet-stream\n\n")
427 (insert-buffer cipher)
428 (goto-char (point-max))
429 (insert (format "--%s--\n" boundary))
430 (goto-char (point-max)))))
434 (defun mml2015-clean-buffer ()
435 (if (gnus-buffer-live-p mml2015-result-buffer)
436 (with-current-buffer mml2015-result-buffer
439 (setq mml2015-result-buffer
440 (gnus-get-buffer-create "*MML2015 Result*"))
443 (defsubst mml2015-clear-decrypt-function ()
444 (nth 6 (assq mml2015-use mml2015-function-alist)))
446 (defsubst mml2015-clear-verify-function ()
447 (nth 5 (assq mml2015-use mml2015-function-alist)))
450 (defun mml2015-decrypt (handle ctl)
451 (mml2015-clean-buffer)
452 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
454 (funcall func handle ctl)
458 (defun mml2015-decrypt-test (handle ctl)
462 (defun mml2015-verify (handle ctl)
463 (mml2015-clean-buffer)
464 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
466 (funcall func handle ctl)
470 (defun mml2015-verify-test (handle ctl)
474 (defun mml2015-encrypt (cont)
475 (mml2015-clean-buffer)
476 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
479 (error "Cannot find encrypt function."))))
482 (defun mml2015-sign (cont)
483 (mml2015-clean-buffer)
484 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
487 (error "Cannot find sign function."))))
490 (defun mml2015-self-encrypt ()
491 (mml2015-encrypt nil))
495 ;;; mml2015.el ends here