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 (cdr handle)
82 "application/octet-stream"))
83 (error "Corrupted pgp-encrypted part."))
85 (mm-insert-part child)
86 (setq result (funcall mml2015-decrypt-function))
88 (error "Decrypting error."))
89 (setq handles (mm-dissect-buffer t)))
90 (mm-destroy-parts handle)
91 (if (listp (car handles))
95 (defun mml2015-mailcrypt-clear-decrypt ()
97 (setq result (funcall mml2015-decrypt-function))
99 (error "Decrypting error."))))
101 (defun mml2015-fix-micalg (alg)
103 (if (and alg (string-match "^pgp-" alg))
104 (substring alg (match-end 0))
107 (defun mml2015-mailcrypt-verify (handle ctl)
109 (unless (setq part (mm-find-raw-part-by-type
110 ctl "application/pgp-signature" t))
111 (error "Corrupted pgp-signature part."))
113 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
114 (insert (format "Hash: %s\n\n"
115 (or (mml2015-fix-micalg
116 (mail-content-type-get ctl 'micalg))
119 (goto-char (point-max))
120 (unless (setq part (mm-find-part-by-type
121 (cdr handle) "application/pgp-signature"))
122 (error "Corrupted pgp-signature part."))
123 (mm-insert-part part)
124 (unless (funcall mml2015-verify-function)
125 (error "Verify error.")))
128 (defun mml2015-mailcrypt-clear-verify ()
129 (unless (funcall mml2015-verify-function)
130 (error "Verify error.")))
132 (defun mml2015-mailcrypt-sign (cont)
133 (mc-sign-generic (message-options-get 'message-sender)
136 (funcall mml-boundary-function (incf mml-multipart-number)))
137 (scheme-alist (funcall (or mc-default-scheme
138 (cdr (car mc-schemes)))))
140 (goto-char (point-min))
141 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
142 (error "Cannot find signed begin line." ))
143 (goto-char (match-beginning 0))
145 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
146 (error "Cannot not find PGP hash." ))
147 (setq hash (match-string 1))
148 (unless (re-search-forward "^$" nil t)
149 (error "Cannot not find PGP message." ))
151 (delete-region (point-min) (point))
152 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
154 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
156 (insert (format "\n--%s\n" boundary))
157 (goto-char (point-max))
158 (unless (re-search-backward (cdr (assq 'signed-end-line scheme-alist)))
159 (error "Cannot find signature part." ))
160 (goto-char (match-beginning 0))
161 (unless (re-search-backward "^-+BEGIN" nil t)
162 (error "Cannot find signature part." ))
163 (goto-char (match-beginning 0))
164 (insert (format "--%s\n" boundary))
165 (insert "Content-Type: application/pgp-signature\n\n")
166 (goto-char (point-max))
167 (insert (format "--%s--\n" boundary))
168 (goto-char (point-max))))
170 (defun mml2015-mailcrypt-encrypt (cont)
172 (or (message-options-get 'message-recipients)
173 (message-options-set 'message-recipients
174 (mc-cleanup-recipient-headers
175 (read-string "Recipients: "))))
177 (message-options-get 'message-sender)
178 (or mc-pgp-always-sign
180 (or (message-options-get 'message-sign-encrypt)
181 (message-options-set 'message-sign-encrypt
182 (or (y-or-n-p "Sign the message? ")
185 (funcall mml-boundary-function (incf mml-multipart-number))))
186 (goto-char (point-min))
187 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
189 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
190 (insert (format "--%s\n" boundary))
191 (insert "Content-Type: application/pgp-encrypted\n\n")
192 (insert "Version: 1\n\n")
193 (insert (format "--%s\n" boundary))
194 (insert "Content-Type: application/octet-stream\n\n")
195 (goto-char (point-max))
196 (insert (format "--%s--\n" boundary))
197 (goto-char (point-max))))
202 (autoload 'gpg-decrypt "gpg")
203 (autoload 'gpg-verify "gpg")
204 (autoload 'gpg-sign-detached "gpg")
205 (autoload 'gpg-sign-encrypt "gpg")
206 (autoload 'gpg-passphrase-read "gpg"))
208 (defun mml2015-gpg-passphrase ()
209 (or (message-options-get 'gpg-passphrase)
210 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
212 (defun mml2015-gpg-decrypt-1 ()
213 (let ((cipher (current-buffer)) plain result)
214 (if (with-temp-buffer
216 (gpg-decrypt cipher (setq plain (current-buffer))
217 mml2015-result-buffer nil)
220 (insert-buffer plain)))
222 ;; Some wrong with the return value, check plain text buffer.
223 (if (> (point-max) (point-min))
225 (pop-to-buffer mml2015-result-buffer)
228 (defun mml2015-gpg-decrypt (handle ctl)
229 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
230 (mml2015-mailcrypt-decrypt handle ctl)))
232 (defun mml2015-gpg-clear-decrypt ()
234 (setq result (mml2015-gpg-decrypt-1))
236 (error "Decrypting error."))))
238 (defun mml2015-gpg-verify (handle ctl)
239 (let (part message signature)
240 (unless (setq part (mm-find-raw-part-by-type
241 ctl "application/pgp-signature" t))
242 (error "Corrupted pgp-signature part."))
244 (setq message (current-buffer))
247 (setq signature (current-buffer))
248 (unless (setq part (mm-find-part-by-type
249 (cdr handle) "application/pgp-signature"))
250 (error "Corrupted pgp-signature part."))
251 (mm-insert-part part)
252 (unless (gpg-verify message signature mml2015-result-buffer)
253 (pop-to-buffer mml2015-result-buffer)
254 (error "Verify error.")))))
257 (defun mml2015-gpg-sign (cont)
259 (funcall mml-boundary-function (incf mml-multipart-number)))
260 (text (current-buffer)) signature)
261 (goto-char (point-max))
265 (unless (gpg-sign-detached text (setq signature (current-buffer))
266 mml2015-result-buffer
268 (message-options-get 'message-sender)
269 t t) ; armor & textmode
270 (unless (> (point-max) (point-min))
271 (pop-to-buffer mml2015-result-buffer)
272 (error "Sign error.")))
274 (goto-char (point-min))
275 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
277 ;;; FIXME: what is the micalg?
278 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
279 (insert (format "\n--%s\n" boundary))
280 (goto-char (point-max))
281 (insert (format "\n--%s\n" boundary))
282 (insert "Content-Type: application/pgp-signature\n\n")
283 (insert-buffer signature)
284 (goto-char (point-max))
285 (insert (format "--%s--\n" boundary))
286 (goto-char (point-max)))))
288 (defun mml2015-gpg-encrypt (cont)
290 (funcall mml-boundary-function (incf mml-multipart-number)))
291 (text (current-buffer))
294 (unless (gpg-sign-encrypt
295 text (setq cipher (current-buffer))
296 mml2015-result-buffer
299 (message-options-get 'message-recipients)
300 (message-options-set 'message-recipients
301 (read-string "Recipients: ")))
304 (message-options-get 'message-sender)
305 t t) ; armor & textmode
306 (unless (> (point-max) (point-min))
307 (pop-to-buffer mml2015-result-buffer)
308 (error "Encrypt error.")))
310 (delete-region (point-min) (point-max))
311 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
313 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
314 (insert (format "--%s\n" boundary))
315 (insert "Content-Type: application/pgp-encrypted\n\n")
316 (insert "Version: 1\n\n")
317 (insert (format "--%s\n" boundary))
318 (insert "Content-Type: application/octet-stream\n\n")
319 (insert-buffer cipher)
320 (goto-char (point-max))
321 (insert (format "--%s--\n" boundary))
322 (goto-char (point-max)))))
326 (defun mml2015-clean-buffer ()
327 (if (gnus-buffer-live-p mml2015-result-buffer)
328 (with-current-buffer mml2015-result-buffer
331 (setq mml2015-result-buffer
332 (gnus-get-buffer-create "*MML2015 Result*"))
335 (defsubst mml2015-clear-decrypt-function ()
336 (nth 6 (assq mml2015-use mml2015-function-alist)))
338 (defsubst mml2015-clear-verify-function ()
339 (nth 5 (assq mml2015-use mml2015-function-alist)))
342 (defun mml2015-decrypt (handle ctl)
343 (mml2015-clean-buffer)
344 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
346 (funcall func handle ctl)
350 (defun mml2015-verify (handle ctl)
351 (mml2015-clean-buffer)
352 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
354 (funcall func handle ctl)
358 (defun mml2015-encrypt (cont)
359 (mml2015-clean-buffer)
360 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
363 (error "Cannot find encrypt function."))))
366 (defun mml2015-sign (cont)
367 (mml2015-clean-buffer)
368 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
371 (error "Cannot find sign function."))))
375 ;;; mml2015.el ends here