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)
174 (let ((mc-pgp-always-sign
175 (or mc-pgp-always-sign
176 (eq t (or (message-options-get 'message-sign-encrypt)
178 'message-sign-encrypt
179 (or (y-or-n-p "Sign the message? ")
183 (or (message-options-get 'message-recipients)
184 (message-options-set 'message-recipients
185 (mc-cleanup-recipient-headers
186 (read-string "Recipients: "))))
188 (message-options-get 'message-sender)))
190 (funcall mml-boundary-function (incf mml-multipart-number))))
191 (goto-char (point-min))
192 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
194 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
195 (insert (format "--%s\n" boundary))
196 (insert "Content-Type: application/pgp-encrypted\n\n")
197 (insert "Version: 1\n\n")
198 (insert (format "--%s\n" boundary))
199 (insert "Content-Type: application/octet-stream\n\n")
200 (goto-char (point-max))
201 (insert (format "--%s--\n" boundary))
202 (goto-char (point-max))))
207 (autoload 'gpg-decrypt "gpg")
208 (autoload 'gpg-verify "gpg")
209 (autoload 'gpg-sign-detached "gpg")
210 (autoload 'gpg-sign-encrypt "gpg")
211 (autoload 'gpg-passphrase-read "gpg"))
213 (defun mml2015-gpg-passphrase ()
214 (or (message-options-get 'gpg-passphrase)
215 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
217 (defun mml2015-gpg-decrypt-1 ()
218 (let ((cipher (current-buffer)) plain result)
219 (if (with-temp-buffer
221 (gpg-decrypt cipher (setq plain (current-buffer))
222 mml2015-result-buffer nil)
225 (insert-buffer plain)))
227 ;; Some wrong with the return value, check plain text buffer.
228 (if (> (point-max) (point-min))
230 (pop-to-buffer mml2015-result-buffer)
233 (defun mml2015-gpg-decrypt (handle ctl)
234 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
235 (mml2015-mailcrypt-decrypt handle ctl)))
237 (defun mml2015-gpg-clear-decrypt ()
239 (setq result (mml2015-gpg-decrypt-1))
241 (error "Decrypting error."))))
243 (defun mml2015-gpg-verify (handle ctl)
244 (let (part message signature)
245 (unless (setq part (mm-find-raw-part-by-type
246 ctl (or (mail-content-type-get ctl 'protocol)
247 "application/pgp-signature")
249 (error "Corrupted pgp-signature part."))
251 (setq message (current-buffer))
254 (setq signature (current-buffer))
255 (unless (setq part (mm-find-part-by-type
256 (cdr handle) "application/pgp-signature" nil t))
257 (error "Corrupted pgp-signature part."))
258 (mm-insert-part part)
259 (unless (gpg-verify message signature mml2015-result-buffer)
260 (pop-to-buffer mml2015-result-buffer)
261 (error "Verify error.")))))
264 (defun mml2015-gpg-sign (cont)
266 (funcall mml-boundary-function (incf mml-multipart-number)))
267 (text (current-buffer)) signature)
268 (goto-char (point-max))
272 (unless (gpg-sign-detached text (setq signature (current-buffer))
273 mml2015-result-buffer
275 (message-options-get 'message-sender)
276 t t) ; armor & textmode
277 (unless (> (point-max) (point-min))
278 (pop-to-buffer mml2015-result-buffer)
279 (error "Sign error.")))
281 (goto-char (point-min))
282 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
284 ;;; FIXME: what is the micalg?
285 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
286 (insert (format "\n--%s\n" boundary))
287 (goto-char (point-max))
288 (insert (format "\n--%s\n" boundary))
289 (insert "Content-Type: application/pgp-signature\n\n")
290 (insert-buffer signature)
291 (goto-char (point-max))
292 (insert (format "--%s--\n" boundary))
293 (goto-char (point-max)))))
295 (defun mml2015-gpg-encrypt (cont)
297 (funcall mml-boundary-function (incf mml-multipart-number)))
298 (text (current-buffer))
301 (unless (gpg-sign-encrypt
302 text (setq cipher (current-buffer))
303 mml2015-result-buffer
306 (message-options-get 'message-recipients)
307 (message-options-set 'message-recipients
308 (read-string "Recipients: ")))
311 (message-options-get 'message-sender)
312 t t) ; armor & textmode
313 (unless (> (point-max) (point-min))
314 (pop-to-buffer mml2015-result-buffer)
315 (error "Encrypt error.")))
317 (delete-region (point-min) (point-max))
318 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
320 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
321 (insert (format "--%s\n" boundary))
322 (insert "Content-Type: application/pgp-encrypted\n\n")
323 (insert "Version: 1\n\n")
324 (insert (format "--%s\n" boundary))
325 (insert "Content-Type: application/octet-stream\n\n")
326 (insert-buffer cipher)
327 (goto-char (point-max))
328 (insert (format "--%s--\n" boundary))
329 (goto-char (point-max)))))
333 (defun mml2015-clean-buffer ()
334 (if (gnus-buffer-live-p mml2015-result-buffer)
335 (with-current-buffer mml2015-result-buffer
338 (setq mml2015-result-buffer
339 (gnus-get-buffer-create "*MML2015 Result*"))
342 (defsubst mml2015-clear-decrypt-function ()
343 (nth 6 (assq mml2015-use mml2015-function-alist)))
345 (defsubst mml2015-clear-verify-function ()
346 (nth 5 (assq mml2015-use mml2015-function-alist)))
349 (defun mml2015-decrypt (handle ctl)
350 (mml2015-clean-buffer)
351 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
353 (funcall func handle ctl)
357 (defun mml2015-decrypt-test (handle ctl)
361 (defun mml2015-verify (handle ctl)
362 (mml2015-clean-buffer)
363 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
365 (funcall func handle ctl)
369 (defun mml2015-verify-test (handle ctl)
373 (defun mml2015-encrypt (cont)
374 (mml2015-clean-buffer)
375 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
378 (error "Cannot find encrypt function."))))
381 (defun mml2015-sign (cont)
382 (mml2015-clean-buffer)
383 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
386 (error "Cannot find sign function."))))
389 (defun mml2015-self-encrypt ()
390 (mml2015-encrypt nil))
394 ;;; mml2015.el ends here