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)
80 (let (child handles result)
81 (unless (setq child (mm-find-part-by-type
83 "application/octet-stream" nil t))
84 (error "Corrupted pgp-encrypted part."))
86 (mm-insert-part child)
87 (setq result (funcall mml2015-decrypt-function))
89 (error "Decrypting error."))
90 (setq handles (mm-dissect-buffer t)))
91 (mm-destroy-parts handle)
92 (if (listp (car handles))
96 (defun mml2015-mailcrypt-clear-decrypt ()
98 (setq result (funcall mml2015-decrypt-function))
100 (error "Decrypting error."))))
102 (defun mml2015-fix-micalg (alg)
104 (if (and alg (string-match "^pgp-" alg))
105 (substring alg (match-end 0))
108 (defun mml2015-mailcrypt-verify (handle ctl)
110 (unless (setq part (mm-find-raw-part-by-type
111 ctl (or (mail-content-type-get ctl 'protocol)
112 "application/pgp-signature")
114 (error "Corrupted pgp-signature part."))
116 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
117 (insert (format "Hash: %s\n\n"
118 (or (mml2015-fix-micalg
119 (mail-content-type-get ctl 'micalg))
122 (goto-char (point-max))
123 (unless (setq part (mm-find-part-by-type
124 (cdr handle) "application/pgp-signature" nil t))
125 (error "Corrupted pgp-signature part."))
126 (mm-insert-part part)
127 (unless (funcall mml2015-verify-function)
128 (error "Verify error.")))
131 (defun mml2015-mailcrypt-clear-verify ()
132 (unless (funcall mml2015-verify-function)
133 (error "Verify error.")))
135 (defun mml2015-mailcrypt-sign (cont)
136 (mc-sign-generic (message-options-get 'message-sender)
139 (funcall mml-boundary-function (incf mml-multipart-number)))
140 (scheme-alist (funcall (or mc-default-scheme
141 (cdr (car mc-schemes)))))
143 (goto-char (point-min))
144 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
145 (error "Cannot find signed begin line." ))
146 (goto-char (match-beginning 0))
148 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
149 (error "Cannot not find PGP hash." ))
150 (setq hash (match-string 1))
151 (unless (re-search-forward "^$" nil t)
152 (error "Cannot not find PGP message." ))
154 (delete-region (point-min) (point))
155 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
157 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
159 (insert (format "\n--%s\n" boundary))
160 (goto-char (point-max))
161 (unless (re-search-backward (cdr (assq 'signed-end-line scheme-alist)))
162 (error "Cannot find signature part." ))
163 (goto-char (match-beginning 0))
164 (unless (re-search-backward "^-+BEGIN" nil t)
165 (error "Cannot find signature part." ))
166 (goto-char (match-beginning 0))
167 (insert (format "--%s\n" boundary))
168 (insert "Content-Type: application/pgp-signature\n\n")
169 (goto-char (point-max))
170 (insert (format "--%s--\n" boundary))
171 (goto-char (point-max))))
173 (defun mml2015-mailcrypt-encrypt (cont)
175 (or (message-options-get 'message-recipients)
176 (message-options-set 'message-recipients
177 (mc-cleanup-recipient-headers
178 (read-string "Recipients: "))))
180 (message-options-get 'message-sender)
181 (or mc-pgp-always-sign
183 (or (message-options-get 'message-sign-encrypt)
184 (message-options-set 'message-sign-encrypt
185 (or (y-or-n-p "Sign the message? ")
188 (funcall mml-boundary-function (incf mml-multipart-number))))
189 (goto-char (point-min))
190 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
192 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
193 (insert (format "--%s\n" boundary))
194 (insert "Content-Type: application/pgp-encrypted\n\n")
195 (insert "Version: 1\n\n")
196 (insert (format "--%s\n" boundary))
197 (insert "Content-Type: application/octet-stream\n\n")
198 (goto-char (point-max))
199 (insert (format "--%s--\n" boundary))
200 (goto-char (point-max))))
205 (autoload 'gpg-decrypt "gpg")
206 (autoload 'gpg-verify "gpg")
207 (autoload 'gpg-sign-detached "gpg")
208 (autoload 'gpg-sign-encrypt "gpg")
209 (autoload 'gpg-passphrase-read "gpg"))
211 (defun mml2015-gpg-passphrase ()
212 (or (message-options-get 'gpg-passphrase)
213 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
215 (defun mml2015-gpg-decrypt-1 ()
216 (let ((cipher (current-buffer)) plain result)
217 (if (with-temp-buffer
219 (gpg-decrypt cipher (setq plain (current-buffer))
220 mml2015-result-buffer nil)
223 (insert-buffer plain)))
225 ;; Some wrong with the return value, check plain text buffer.
226 (if (> (point-max) (point-min))
228 (pop-to-buffer mml2015-result-buffer)
231 (defun mml2015-gpg-decrypt (handle ctl)
232 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
233 (mml2015-mailcrypt-decrypt handle ctl)))
235 (defun mml2015-gpg-clear-decrypt ()
237 (setq result (mml2015-gpg-decrypt-1))
239 (error "Decrypting error."))))
241 (defun mml2015-gpg-verify (handle ctl)
242 (let (part message signature)
243 (unless (setq part (mm-find-raw-part-by-type
244 ctl (or (mail-content-type-get ctl 'protocol)
245 "application/pgp-signature")
247 (error "Corrupted pgp-signature part."))
249 (setq message (current-buffer))
252 (setq signature (current-buffer))
253 (unless (setq part (mm-find-part-by-type
254 (cdr handle) "application/pgp-signature" nil t))
255 (error "Corrupted pgp-signature part."))
256 (mm-insert-part part)
257 (unless (gpg-verify message signature mml2015-result-buffer)
258 (pop-to-buffer mml2015-result-buffer)
259 (error "Verify error.")))))
262 (defun mml2015-gpg-sign (cont)
264 (funcall mml-boundary-function (incf mml-multipart-number)))
265 (text (current-buffer)) signature)
266 (goto-char (point-max))
270 (unless (gpg-sign-detached text (setq signature (current-buffer))
271 mml2015-result-buffer
273 (message-options-get 'message-sender)
274 t t) ; armor & textmode
275 (unless (> (point-max) (point-min))
276 (pop-to-buffer mml2015-result-buffer)
277 (error "Sign error.")))
279 (goto-char (point-min))
280 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
282 ;;; FIXME: what is the micalg?
283 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
284 (insert (format "\n--%s\n" boundary))
285 (goto-char (point-max))
286 (insert (format "\n--%s\n" boundary))
287 (insert "Content-Type: application/pgp-signature\n\n")
288 (insert-buffer signature)
289 (goto-char (point-max))
290 (insert (format "--%s--\n" boundary))
291 (goto-char (point-max)))))
293 (defun mml2015-gpg-encrypt (cont)
295 (funcall mml-boundary-function (incf mml-multipart-number)))
296 (text (current-buffer))
299 (unless (gpg-sign-encrypt
300 text (setq cipher (current-buffer))
301 mml2015-result-buffer
304 (message-options-get 'message-recipients)
305 (message-options-set 'message-recipients
306 (read-string "Recipients: ")))
309 (message-options-get 'message-sender)
310 t t) ; armor & textmode
311 (unless (> (point-max) (point-min))
312 (pop-to-buffer mml2015-result-buffer)
313 (error "Encrypt error.")))
315 (delete-region (point-min) (point-max))
316 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
318 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
319 (insert (format "--%s\n" boundary))
320 (insert "Content-Type: application/pgp-encrypted\n\n")
321 (insert "Version: 1\n\n")
322 (insert (format "--%s\n" boundary))
323 (insert "Content-Type: application/octet-stream\n\n")
324 (insert-buffer cipher)
325 (goto-char (point-max))
326 (insert (format "--%s--\n" boundary))
327 (goto-char (point-max)))))
331 (defun mml2015-clean-buffer ()
332 (if (gnus-buffer-live-p mml2015-result-buffer)
333 (with-current-buffer mml2015-result-buffer
336 (setq mml2015-result-buffer
337 (gnus-get-buffer-create "*MML2015 Result*"))
340 (defsubst mml2015-clear-decrypt-function ()
341 (nth 6 (assq mml2015-use mml2015-function-alist)))
343 (defsubst mml2015-clear-verify-function ()
344 (nth 5 (assq mml2015-use mml2015-function-alist)))
347 (defun mml2015-decrypt (handle ctl)
348 (mml2015-clean-buffer)
349 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
351 (funcall func handle ctl)
355 (defun mml2015-decrypt-test (handle ctl)
359 (defun mml2015-verify (handle ctl)
360 (mml2015-clean-buffer)
361 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
363 (funcall func handle ctl)
367 (defun mml2015-verify-test (handle ctl)
371 (defun mml2015-encrypt (cont)
372 (mml2015-clean-buffer)
373 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
376 (error "Cannot find encrypt function."))))
379 (defun mml2015-sign (cont)
380 (mml2015-clean-buffer)
381 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
384 (error "Cannot find sign function."))))
388 ;;; mml2015.el ends here