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
52 mml2015-mailcrypt-clear-snarf)
58 mml2015-gpg-clear-decrypt
60 "Alist of PGP/MIME functions.")
62 (defvar mml2015-result-buffer nil)
67 (autoload 'mailcrypt-decrypt "mailcrypt")
68 (autoload 'mailcrypt-verify "mailcrypt")
69 (autoload 'mc-pgp-always-sign "mailcrypt")
70 (autoload 'mc-encrypt-generic "mc-toplev")
71 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
72 (autoload 'mc-sign-generic "mc-toplev")
73 (autoload 'mc-snarf-keys "mc-toplev"))
76 (defvar mc-default-scheme)
79 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
80 (defvar mml2015-verify-function 'mailcrypt-verify)
81 (defvar mml2015-snarf-function 'mc-snarf-keys)
83 (defun mml2015-mailcrypt-decrypt (handle ctl)
84 (let (child handles result)
85 (unless (setq child (mm-find-part-by-type
87 "application/octet-stream" nil t))
88 (error "Corrupted pgp-encrypted part."))
90 (mm-insert-part child)
91 (setq result (funcall mml2015-decrypt-function))
93 (error "Decrypting error."))
94 (setq handles (mm-dissect-buffer t)))
95 (mm-destroy-parts handle)
96 (if (listp (car handles))
100 (defun mml2015-mailcrypt-clear-decrypt ()
102 (setq result (funcall mml2015-decrypt-function))
104 (error "Decrypting error."))))
106 (defun mml2015-fix-micalg (alg)
108 (if (and alg (string-match "^pgp-" alg))
109 (substring alg (match-end 0))
112 (defun mml2015-mailcrypt-verify (handle ctl)
114 (unless (setq part (mm-find-raw-part-by-type
115 ctl (or (mail-content-type-get ctl 'protocol)
116 "application/pgp-signature")
118 (error "Corrupted pgp-signature part."))
120 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
121 (insert (format "Hash: %s\n\n"
122 (or (mml2015-fix-micalg
123 (mail-content-type-get ctl 'micalg))
126 (goto-char (point-max))
127 (unless (setq part (mm-find-part-by-type
128 (cdr handle) "application/pgp-signature" nil t))
129 (error "Corrupted pgp-signature part."))
130 (mm-insert-part part)
131 (unless (funcall mml2015-verify-function)
132 (error "Verify error.")))
135 (defun mml2015-mailcrypt-clear-verify ()
136 (unless (funcall mml2015-verify-function)
137 (error "Verify error.")))
139 (defun mml2015-mailcrypt-clear-snarf ()
140 (funcall mml2015-snarf-function))
142 (defun mml2015-mailcrypt-sign (cont)
143 (mc-sign-generic (message-options-get 'message-sender)
146 (funcall mml-boundary-function (incf mml-multipart-number)))
147 (scheme-alist (funcall (or mc-default-scheme
148 (cdr (car mc-schemes)))))
150 (goto-char (point-min))
151 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
152 (error "Cannot find signed begin line." ))
153 (goto-char (match-beginning 0))
155 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
156 (error "Cannot not find PGP hash." ))
157 (setq hash (match-string 1))
158 (unless (re-search-forward "^$" nil t)
159 (error "Cannot not find PGP message." ))
161 (delete-region (point-min) (point))
162 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
164 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
166 (insert (format "\n--%s\n" boundary))
167 (goto-char (point-max))
168 (unless (re-search-backward (cdr (assq 'signed-end-line scheme-alist)))
169 (error "Cannot find signature part." ))
170 (goto-char (match-beginning 0))
171 (unless (re-search-backward "^-+BEGIN" nil t)
172 (error "Cannot find signature part." ))
173 (goto-char (match-beginning 0))
174 (insert (format "--%s\n" boundary))
175 (insert "Content-Type: application/pgp-signature\n\n")
176 (goto-char (point-max))
177 (insert (format "--%s--\n" boundary))
178 (goto-char (point-max))))
180 (defun mml2015-mailcrypt-encrypt (cont)
182 (or (message-options-get 'message-recipients)
183 (message-options-set 'message-recipients
184 (mc-cleanup-recipient-headers
185 (read-string "Recipients: "))))
187 (message-options-get 'message-sender)
188 (or mc-pgp-always-sign
190 (or (message-options-get 'message-sign-encrypt)
191 (message-options-set 'message-sign-encrypt
192 (or (y-or-n-p "Sign the message? ")
195 (funcall mml-boundary-function (incf mml-multipart-number))))
196 (goto-char (point-min))
197 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
199 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
200 (insert (format "--%s\n" boundary))
201 (insert "Content-Type: application/pgp-encrypted\n\n")
202 (insert "Version: 1\n\n")
203 (insert (format "--%s\n" boundary))
204 (insert "Content-Type: application/octet-stream\n\n")
205 (goto-char (point-max))
206 (insert (format "--%s--\n" boundary))
207 (goto-char (point-max))))
212 (autoload 'gpg-decrypt "gpg")
213 (autoload 'gpg-verify "gpg")
214 (autoload 'gpg-sign-detached "gpg")
215 (autoload 'gpg-sign-encrypt "gpg")
216 (autoload 'gpg-passphrase-read "gpg"))
218 (defun mml2015-gpg-passphrase ()
219 (or (message-options-get 'gpg-passphrase)
220 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
222 (defun mml2015-gpg-decrypt-1 ()
223 (let ((cipher (current-buffer)) plain result)
224 (if (with-temp-buffer
226 (gpg-decrypt cipher (setq plain (current-buffer))
227 mml2015-result-buffer nil)
230 (insert-buffer plain)))
232 ;; Some wrong with the return value, check plain text buffer.
233 (if (> (point-max) (point-min))
235 (pop-to-buffer mml2015-result-buffer)
238 (defun mml2015-gpg-decrypt (handle ctl)
239 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
240 (mml2015-mailcrypt-decrypt handle ctl)))
242 (defun mml2015-gpg-clear-decrypt ()
244 (setq result (mml2015-gpg-decrypt-1))
246 (error "Decrypting error."))))
248 (defun mml2015-gpg-verify (handle ctl)
249 (let (part message signature)
250 (unless (setq part (mm-find-raw-part-by-type
251 ctl (or (mail-content-type-get ctl 'protocol)
252 "application/pgp-signature")
254 (error "Corrupted pgp-signature part."))
256 (setq message (current-buffer))
259 (setq signature (current-buffer))
260 (unless (setq part (mm-find-part-by-type
261 (cdr handle) "application/pgp-signature" nil t))
262 (error "Corrupted pgp-signature part."))
263 (mm-insert-part part)
264 (unless (gpg-verify message signature mml2015-result-buffer)
265 (pop-to-buffer mml2015-result-buffer)
266 (error "Verify error.")))))
269 (defun mml2015-gpg-sign (cont)
271 (funcall mml-boundary-function (incf mml-multipart-number)))
272 (text (current-buffer)) signature)
273 (goto-char (point-max))
277 (unless (gpg-sign-detached text (setq signature (current-buffer))
278 mml2015-result-buffer
280 (message-options-get 'message-sender)
281 t t) ; armor & textmode
282 (unless (> (point-max) (point-min))
283 (pop-to-buffer mml2015-result-buffer)
284 (error "Sign error.")))
286 (goto-char (point-min))
287 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
289 ;;; FIXME: what is the micalg?
290 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
291 (insert (format "\n--%s\n" boundary))
292 (goto-char (point-max))
293 (insert (format "\n--%s\n" boundary))
294 (insert "Content-Type: application/pgp-signature\n\n")
295 (insert-buffer signature)
296 (goto-char (point-max))
297 (insert (format "--%s--\n" boundary))
298 (goto-char (point-max)))))
300 (defun mml2015-gpg-encrypt (cont)
302 (funcall mml-boundary-function (incf mml-multipart-number)))
303 (text (current-buffer))
306 (unless (gpg-sign-encrypt
307 text (setq cipher (current-buffer))
308 mml2015-result-buffer
311 (message-options-get 'message-recipients)
312 (message-options-set 'message-recipients
313 (read-string "Recipients: ")))
316 (message-options-get 'message-sender)
317 t t) ; armor & textmode
318 (unless (> (point-max) (point-min))
319 (pop-to-buffer mml2015-result-buffer)
320 (error "Encrypt error.")))
322 (delete-region (point-min) (point-max))
323 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
325 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
326 (insert (format "--%s\n" boundary))
327 (insert "Content-Type: application/pgp-encrypted\n\n")
328 (insert "Version: 1\n\n")
329 (insert (format "--%s\n" boundary))
330 (insert "Content-Type: application/octet-stream\n\n")
331 (insert-buffer cipher)
332 (goto-char (point-max))
333 (insert (format "--%s--\n" boundary))
334 (goto-char (point-max)))))
338 (defun mml2015-clean-buffer ()
339 (if (gnus-buffer-live-p mml2015-result-buffer)
340 (with-current-buffer mml2015-result-buffer
343 (setq mml2015-result-buffer
344 (gnus-get-buffer-create "*MML2015 Result*"))
347 (defsubst mml2015-clear-snarf-function ()
348 (nth 7 (assq mml2015-use mml2015-function-alist)))
350 (defsubst mml2015-clear-decrypt-function ()
351 (nth 6 (assq mml2015-use mml2015-function-alist)))
353 (defsubst mml2015-clear-verify-function ()
354 (nth 5 (assq mml2015-use mml2015-function-alist)))
357 (defun mml2015-decrypt (handle ctl)
358 (mml2015-clean-buffer)
359 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
361 (funcall func handle ctl)
365 (defun mml2015-decrypt-test (handle ctl)
369 (defun mml2015-verify (handle ctl)
370 (mml2015-clean-buffer)
371 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
373 (funcall func handle ctl)
377 (defun mml2015-verify-test (handle ctl)
381 (defun mml2015-encrypt (cont)
382 (mml2015-clean-buffer)
383 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
386 (error "Cannot find encrypt function."))))
389 (defun mml2015-sign (cont)
390 (mml2015-clean-buffer)
391 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
394 (error "Cannot find sign function."))))
398 ;;; mml2015.el ends here