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-info "Failed")
100 (throw 'error handle))
101 (setq handles (mm-dissect-buffer t)))
102 (mm-destroy-parts handle)
103 (mm-set-handle-multipart-parameter
104 mm-security-handle 'gnus-info "OK")
105 (if (listp (car handles))
109 (defun mml2015-mailcrypt-clear-decrypt ()
113 (funcall mml2015-decrypt-function)
115 (mm-set-handle-multipart-parameter
116 mm-security-handle 'gnus-details (cadr err))
119 (mm-set-handle-multipart-parameter
120 mm-security-handle 'gnus-info "OK")
121 (mm-set-handle-multipart-parameter
122 mm-security-handle 'gnus-info "Failed"))))
124 (defun mml2015-fix-micalg (alg)
126 (if (and alg (string-match "^pgp-" alg))
127 (substring alg (match-end 0))
130 (defun mml2015-mailcrypt-verify (handle ctl)
133 (unless (setq part (mm-find-raw-part-by-type
134 ctl (or (mail-content-type-get ctl 'protocol)
135 "application/pgp-signature")
137 (mm-set-handle-multipart-parameter
138 mm-security-handle 'gnus-info "Corrupted")
139 (throw 'error handle))
141 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
142 (insert (format "Hash: %s\n\n"
143 (or (mml2015-fix-micalg
144 (mail-content-type-get ctl 'micalg))
147 (narrow-to-region (point) (point))
149 (goto-char (point-min))
151 (if (looking-at "^-")
154 (unless (setq part (mm-find-part-by-type
155 (cdr handle) "application/pgp-signature" nil t))
156 (mm-set-handle-multipart-parameter
157 mm-security-handle 'gnus-info "Corrupted")
158 (throw 'error handle))
160 (narrow-to-region (point) (point))
161 (mm-insert-part part)
162 (goto-char (point-min))
163 (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
164 (replace-match "BEGIN PGP SIGNATURE" t t nil 1))
165 (if (re-search-forward "^-----\\([^-]+\\)-----$" nil t)
166 (replace-match "END PGP SIGNATURE" t t nil 1)))
167 (unless (condition-case err
168 (funcall mml2015-verify-function)
170 (mm-set-handle-multipart-parameter
171 mm-security-handle 'gnus-details (cadr err))
173 (mm-set-handle-multipart-parameter
174 mm-security-handle 'gnus-info "Failed")
175 (throw 'error handle)))
176 (mm-set-handle-multipart-parameter
177 mm-security-handle 'gnus-info "OK")
180 (defun mml2015-mailcrypt-clear-verify ()
181 (if (condition-case err
182 (funcall mml2015-verify-function)
184 (mm-set-handle-multipart-parameter
185 mm-security-handle 'gnus-details (cadr err))
187 (mm-set-handle-multipart-parameter
188 mm-security-handle 'gnus-info "OK")
189 (mm-set-handle-multipart-parameter
190 mm-security-handle 'gnus-info "Failed")))
192 (defun mml2015-mailcrypt-sign (cont)
193 (mc-sign-generic (message-options-get 'message-sender)
196 (funcall mml-boundary-function (incf mml-multipart-number)))
198 (goto-char (point-min))
199 (unless (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\r?$" nil t)
200 (error "Cannot find signed begin line." ))
201 (goto-char (match-beginning 0))
203 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
204 (error "Cannot not find PGP hash." ))
205 (setq hash (match-string 1))
206 (unless (re-search-forward "^$" nil t)
207 (error "Cannot not find PGP message." ))
209 (delete-region (point-min) (point))
210 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
212 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
214 (insert (format "\n--%s\n" boundary))
215 (goto-char (point-max))
216 (unless (re-search-backward "^-----END PGP \\(SIGNATURE\\)-----\r?$" nil t)
217 (error "Cannot find signature part." ))
218 (replace-match "MESSAGE" t t nil 1)
219 (goto-char (match-beginning 0))
220 (unless (re-search-backward "^-----BEGIN PGP \\(SIGNATURE\\)-----\r?$"
222 (error "Cannot find signature part." ))
223 (replace-match "MESSAGE" t t nil 1)
224 (goto-char (match-beginning 0))
225 (insert (format "--%s\n" boundary))
226 (insert "Content-Type: application/pgp-signature\n\n")
227 (goto-char (point-max))
228 (insert (format "--%s--\n" boundary))
229 (goto-char (point-max))))
231 (defun mml2015-mailcrypt-encrypt (cont)
232 (let ((mc-pgp-always-sign
233 (or mc-pgp-always-sign
234 (eq t (or (message-options-get 'message-sign-encrypt)
236 'message-sign-encrypt
237 (or (y-or-n-p "Sign the message? ")
241 (or (message-options-get 'message-recipients)
242 (message-options-set 'message-recipients
243 (mc-cleanup-recipient-headers
244 (read-string "Recipients: "))))
246 (message-options-get 'message-sender)))
248 (funcall mml-boundary-function (incf mml-multipart-number))))
249 (goto-char (point-min))
250 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
252 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
253 (insert (format "--%s\n" boundary))
254 (insert "Content-Type: application/pgp-encrypted\n\n")
255 (insert "Version: 1\n\n")
256 (insert (format "--%s\n" boundary))
257 (insert "Content-Type: application/octet-stream\n\n")
258 (goto-char (point-max))
259 (insert (format "--%s--\n" boundary))
260 (goto-char (point-max))))
265 (autoload 'gpg-decrypt "gpg")
266 (autoload 'gpg-verify "gpg")
267 (autoload 'gpg-sign-detached "gpg")
268 (autoload 'gpg-sign-encrypt "gpg")
269 (autoload 'gpg-passphrase-read "gpg"))
271 (defun mml2015-gpg-passphrase ()
272 (or (message-options-get 'gpg-passphrase)
273 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
275 (defun mml2015-gpg-decrypt-1 ()
276 (let ((cipher (current-buffer)) plain result)
277 (if (with-temp-buffer
279 (gpg-decrypt cipher (setq plain (current-buffer))
280 mml2015-result-buffer nil)
283 (insert-buffer plain)))
285 ;; Some wrong with the return value, check plain text buffer.
286 (if (> (point-max) (point-min))
288 (mm-set-handle-multipart-parameter
289 mm-security-handle 'gnus-details
290 (with-current-buffer mml2015-result-buffer
294 (defun mml2015-gpg-decrypt (handle ctl)
295 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
296 (mml2015-mailcrypt-decrypt handle ctl)))
298 (defun mml2015-gpg-clear-decrypt ()
300 (setq result (mml2015-gpg-decrypt-1))
302 (mm-set-handle-multipart-parameter
303 mm-security-handle 'gnus-info "OK")
304 (mm-set-handle-multipart-parameter
305 mm-security-handle 'gnus-info "Failed"))))
307 (defun mml2015-gpg-verify (handle ctl)
309 (let (part message signature)
310 (unless (setq part (mm-find-raw-part-by-type
311 ctl (or (mail-content-type-get ctl 'protocol)
312 "application/pgp-signature")
314 (mm-set-handle-multipart-parameter
315 mm-security-handle 'gnus-info "Corrupted")
316 (throw 'error handle))
318 (setq message (current-buffer))
321 (setq signature (current-buffer))
322 (unless (setq part (mm-find-part-by-type
323 (cdr handle) "application/pgp-signature" nil t))
324 (mm-set-handle-multipart-parameter
325 mm-security-handle 'gnus-info "Corrupted")
326 (throw 'error handle))
327 (mm-insert-part part)
328 (unless (gpg-verify message signature mml2015-result-buffer)
329 (mm-set-handle-multipart-parameter
330 mm-security-handle 'gnus-details
331 (with-current-buffer mml2015-result-buffer
333 (mm-set-handle-multipart-parameter
334 mm-security-handle 'gnus-info "Failed")
335 (throw 'error handle)))
336 (mm-set-handle-multipart-parameter
337 mm-security-handle 'gnus-info "OK"))
340 (defun mml2015-gpg-sign (cont)
342 (funcall mml-boundary-function (incf mml-multipart-number)))
343 (text (current-buffer)) signature)
344 (goto-char (point-max))
348 (unless (gpg-sign-detached text (setq signature (current-buffer))
349 mml2015-result-buffer
351 (message-options-get 'message-sender)
352 t t) ; armor & textmode
353 (unless (> (point-max) (point-min))
354 (pop-to-buffer mml2015-result-buffer)
355 (error "Sign error.")))
357 (goto-char (point-min))
358 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
360 ;;; FIXME: what is the micalg?
361 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
362 (insert (format "\n--%s\n" boundary))
363 (goto-char (point-max))
364 (insert (format "\n--%s\n" boundary))
365 (insert "Content-Type: application/pgp-signature\n\n")
366 (insert-buffer signature)
367 (goto-char (point-max))
368 (insert (format "--%s--\n" boundary))
369 (goto-char (point-max)))))
371 (defun mml2015-gpg-encrypt (cont)
373 (funcall mml-boundary-function (incf mml-multipart-number)))
374 (text (current-buffer))
377 (unless (gpg-sign-encrypt
378 text (setq cipher (current-buffer))
379 mml2015-result-buffer
382 (message-options-get 'message-recipients)
383 (message-options-set 'message-recipients
384 (read-string "Recipients: ")))
387 (message-options-get 'message-sender)
388 t t) ; armor & textmode
389 (unless (> (point-max) (point-min))
390 (pop-to-buffer mml2015-result-buffer)
391 (error "Encrypt error.")))
393 (delete-region (point-min) (point-max))
394 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
396 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
397 (insert (format "--%s\n" boundary))
398 (insert "Content-Type: application/pgp-encrypted\n\n")
399 (insert "Version: 1\n\n")
400 (insert (format "--%s\n" boundary))
401 (insert "Content-Type: application/octet-stream\n\n")
402 (insert-buffer cipher)
403 (goto-char (point-max))
404 (insert (format "--%s--\n" boundary))
405 (goto-char (point-max)))))
409 (defun mml2015-clean-buffer ()
410 (if (gnus-buffer-live-p mml2015-result-buffer)
411 (with-current-buffer mml2015-result-buffer
414 (setq mml2015-result-buffer
415 (gnus-get-buffer-create "*MML2015 Result*"))
418 (defsubst mml2015-clear-decrypt-function ()
419 (nth 6 (assq mml2015-use mml2015-function-alist)))
421 (defsubst mml2015-clear-verify-function ()
422 (nth 5 (assq mml2015-use mml2015-function-alist)))
425 (defun mml2015-decrypt (handle ctl)
426 (mml2015-clean-buffer)
427 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
429 (funcall func handle ctl)
433 (defun mml2015-decrypt-test (handle ctl)
437 (defun mml2015-verify (handle ctl)
438 (mml2015-clean-buffer)
439 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
441 (funcall func handle ctl)
445 (defun mml2015-verify-test (handle ctl)
449 (defun mml2015-encrypt (cont)
450 (mml2015-clean-buffer)
451 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
454 (error "Cannot find encrypt function."))))
457 (defun mml2015-sign (cont)
458 (mml2015-clean-buffer)
459 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
462 (error "Cannot find sign function."))))
465 (defun mml2015-self-encrypt ()
466 (mml2015-encrypt nil))
470 ;;; mml2015.el ends here