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 (defvar mml2015-function-alist
45 '((mailcrypt mml2015-mailcrypt-sign
46 mml2015-mailcrypt-encrypt
47 mml2015-mailcrypt-verify
48 mml2015-mailcrypt-decrypt)
53 "Alist of PGP/MIME functions.")
55 (defvar mml2015-result-buffer nil)
60 (autoload 'mailcrypt-decrypt "mailcrypt")
61 (autoload 'mailcrypt-verify "mailcrypt")
62 (autoload 'mc-encrypt-generic "mc-toplev")
63 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
64 (autoload 'mc-sign-generic "mc-toplev"))
67 (defvar mc-default-scheme)
70 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
71 (defvar mml2015-verify-function 'mailcrypt-verify)
73 (defun mml2015-mailcrypt-decrypt (handle ctl)
74 (let (child handles result)
75 (unless (setq child (mm-find-part-by-type (cdr handle)
76 "application/octet-stream"))
77 (error "Corrupted pgp-encrypted part."))
79 (mm-insert-part child)
80 (setq result (funcall mml2015-decrypt-function))
82 (error "Decrypting error."))
83 (setq handles (mm-dissect-buffer t)))
84 (mm-destroy-parts handle)
85 (if (listp (car handles))
89 (defun mml2015-fix-micalg (alg)
91 (if (and alg (string-match "^pgp-" alg))
92 (substring alg (match-end 0))
95 (defun mml2015-mailcrypt-verify (handle ctl)
97 (unless (setq part (mm-find-raw-part-by-type
98 ctl "application/pgp-signature" t))
99 (error "Corrupted pgp-signature part."))
101 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
102 (insert (format "Hash: %s\n\n"
103 (or (mml2015-fix-micalg
104 (mail-content-type-get ctl 'micalg))
107 (goto-char (point-max))
108 (unless (setq part (mm-find-part-by-type
109 (cdr handle) "application/pgp-signature"))
110 (error "Corrupted pgp-signature part."))
111 (mm-insert-part part)
112 (unless (funcall mml2015-verify-function)
113 (error "Verify error.")))
116 (defun mml2015-mailcrypt-sign (cont)
117 (mc-sign-generic (message-options-get 'message-sender)
120 (funcall mml-boundary-function (incf mml-multipart-number)))
121 (scheme-alist (funcall (or mc-default-scheme
122 (cdr (car mc-schemes)))))
124 (goto-char (point-min))
125 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
126 (error "Cannot find signed begin line." ))
127 (goto-char (match-beginning 0))
129 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
130 (error "Cannot not find PGP hash." ))
131 (setq hash (match-string 1))
132 (unless (re-search-forward "^$" nil t)
133 (error "Cannot not find PGP message." ))
135 (delete-region (point-min) (point))
136 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
138 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
140 (insert (format "\n--%s\n" boundary))
141 (goto-char (point-max))
142 (unless (re-search-backward (cdr (assq 'signed-end-line scheme-alist)))
143 (error "Cannot find signature part." ))
144 (goto-char (match-beginning 0))
145 (unless (re-search-backward "^-+BEGIN" nil t)
146 (error "Cannot find signature part." ))
147 (goto-char (match-beginning 0))
148 (insert (format "--%s\n" boundary))
149 (insert "Content-Type: application/pgp-signature\n\n")
150 (goto-char (point-max))
151 (insert (format "--%s--\n" boundary))
152 (goto-char (point-max))))
154 (defun mml2015-mailcrypt-encrypt (cont)
156 (or (message-options-get 'message-recipients)
157 (message-options-set 'message-recipients
158 (mc-cleanup-recipient-headers
159 (read-string "Recipients: ")))))
161 (funcall mml-boundary-function (incf mml-multipart-number))))
162 (goto-char (point-min))
163 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
165 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
166 (insert (format "--%s\n" boundary))
167 (insert "Content-Type: application/pgp-encrypted\n\n")
168 (insert "Version: 1\n\n")
169 (insert (format "--%s\n" boundary))
170 (insert "Content-Type: application/octet-stream\n\n")
171 (goto-char (point-max))
172 (insert (format "--%s--\n" boundary))
173 (goto-char (point-max))))
178 (autoload 'gpg-decrypt "gpg")
179 (autoload 'gpg-verify "gpg")
180 (autoload 'gpg-sign-detached "gpg")
181 (autoload 'gpg-sign-encrypt "gpg")
182 (autoload 'gpg-passphrase-read "gpg"))
184 (defun mml2015-gpg-passphrase ()
185 (or (message-options-get 'gpg-passphrase)
186 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
188 (defun mml2015-gpg-decrypt-1 ()
189 (let ((cipher (current-buffer)) plain result)
190 (if (with-temp-buffer
192 (gpg-decrypt cipher (setq plain (current-buffer))
193 mml2015-result-buffer nil)
196 (insert-buffer plain)))
198 ;; Some wrong with the return value, check plain text buffer.
199 (if (> (point-max) (point-min))
201 (pop-to-buffer mml2015-result-buffer)
204 (defun mml2015-gpg-decrypt (handle ctl)
205 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
206 (mml2015-mailcrypt-decrypt handle ctl)))
208 (defun mml2015-gpg-verify (handle ctl)
209 (let (part message signature)
210 (unless (setq part (mm-find-raw-part-by-type
211 ctl "application/pgp-signature" t))
212 (error "Corrupted pgp-signature part."))
214 (setq message (current-buffer))
217 (setq signature (current-buffer))
218 (unless (setq part (mm-find-part-by-type
219 (cdr handle) "application/pgp-signature"))
220 (error "Corrupted pgp-signature part."))
221 (mm-insert-part part)
222 (unless (gpg-verify message signature mml2015-result-buffer)
223 (pop-to-buffer mml2015-result-buffer)
224 (error "Verify error.")))))
227 (defun mml2015-gpg-sign (cont)
229 (funcall mml-boundary-function (incf mml-multipart-number)))
230 (text (current-buffer)) signature)
231 (goto-char (point-max))
235 (unless (gpg-sign-detached text (setq signature (current-buffer))
236 mml2015-result-buffer
238 (message-options-get 'message-sender)
239 t t) ; armor & textmode
240 (unless (> (point-max) (point-min))
241 (pop-to-buffer mml2015-result-buffer)
242 (error "Sign error.")))
244 (goto-char (point-min))
245 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
247 ;;; FIXME: what is the micalg?
248 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
249 (insert (format "\n--%s\n" boundary))
250 (goto-char (point-max))
251 (insert (format "\n--%s\n" boundary))
252 (insert "Content-Type: application/pgp-signature\n\n")
253 (insert-buffer signature)
254 (goto-char (point-max))
255 (insert (format "--%s--\n" boundary))
256 (goto-char (point-max)))))
258 (defun mml2015-gpg-encrypt (cont)
260 (funcall mml-boundary-function (incf mml-multipart-number)))
261 (text (current-buffer))
264 (unless (gpg-sign-encrypt
265 text (setq cipher (current-buffer))
266 mml2015-result-buffer
269 (message-options-get 'message-recipients)
270 (message-options-set 'message-recipients
271 (read-string "Recipients: ")))
274 (message-options-get 'message-sender)
275 t t) ; armor & textmode
276 (unless (> (point-max) (point-min))
277 (pop-to-buffer mml2015-result-buffer)
278 (error "Encrypt error.")))
280 (delete-region (point-min) (point-max))
281 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
283 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
284 (insert (format "--%s\n" boundary))
285 (insert "Content-Type: application/pgp-encrypted\n\n")
286 (insert "Version: 1\n\n")
287 (insert (format "--%s\n" boundary))
288 (insert "Content-Type: application/octet-stream\n\n")
289 (insert-buffer cipher)
290 (goto-char (point-max))
291 (insert (format "--%s--\n" boundary))
292 (goto-char (point-max)))))
296 (defun mml2015-clean-buffer ()
297 (if (gnus-buffer-live-p mml2015-result-buffer)
298 (with-current-buffer mml2015-result-buffer
301 (setq mml2015-result-buffer
302 (gnus-get-buffer-create "*MML2015 Result*"))
306 (defun mml2015-decrypt (handle ctl)
307 (mml2015-clean-buffer)
308 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
310 (funcall func handle ctl)
314 (defun mml2015-verify (handle ctl)
315 (mml2015-clean-buffer)
316 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
318 (funcall func handle ctl)
322 (defun mml2015-encrypt (cont)
323 (mml2015-clean-buffer)
324 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
327 (error "Cannot find encrypt function."))))
330 (defun mml2015-sign (cont)
331 (mml2015-clean-buffer)
332 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
335 (error "Cannot find sign function."))))
339 ;;; mml2015.el ends here