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-pgp-always-sign "mailcrypt")
63 (autoload 'mc-encrypt-generic "mc-toplev")
64 (autoload 'mc-cleanup-recipient-headers "mc-toplev")
65 (autoload 'mc-sign-generic "mc-toplev"))
68 (defvar mc-default-scheme)
71 (defvar mml2015-decrypt-function 'mailcrypt-decrypt)
72 (defvar mml2015-verify-function 'mailcrypt-verify)
74 (defun mml2015-mailcrypt-decrypt (handle ctl)
75 (let (child handles result)
76 (unless (setq child (mm-find-part-by-type (cdr handle)
77 "application/octet-stream"))
78 (error "Corrupted pgp-encrypted part."))
80 (mm-insert-part child)
81 (setq result (funcall mml2015-decrypt-function))
83 (error "Decrypting error."))
84 (setq handles (mm-dissect-buffer t)))
85 (mm-destroy-parts handle)
86 (if (listp (car handles))
90 (defun mml2015-fix-micalg (alg)
92 (if (and alg (string-match "^pgp-" alg))
93 (substring alg (match-end 0))
96 (defun mml2015-mailcrypt-verify (handle ctl)
98 (unless (setq part (mm-find-raw-part-by-type
99 ctl "application/pgp-signature" t))
100 (error "Corrupted pgp-signature part."))
102 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
103 (insert (format "Hash: %s\n\n"
104 (or (mml2015-fix-micalg
105 (mail-content-type-get ctl 'micalg))
108 (goto-char (point-max))
109 (unless (setq part (mm-find-part-by-type
110 (cdr handle) "application/pgp-signature"))
111 (error "Corrupted pgp-signature part."))
112 (mm-insert-part part)
113 (unless (funcall mml2015-verify-function)
114 (error "Verify error.")))
117 (defun mml2015-mailcrypt-sign (cont)
118 (mc-sign-generic (message-options-get 'message-sender)
121 (funcall mml-boundary-function (incf mml-multipart-number)))
122 (scheme-alist (funcall (or mc-default-scheme
123 (cdr (car mc-schemes)))))
125 (goto-char (point-min))
126 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
127 (error "Cannot find signed begin line." ))
128 (goto-char (match-beginning 0))
130 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
131 (error "Cannot not find PGP hash." ))
132 (setq hash (match-string 1))
133 (unless (re-search-forward "^$" nil t)
134 (error "Cannot not find PGP message." ))
136 (delete-region (point-min) (point))
137 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
139 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
141 (insert (format "\n--%s\n" boundary))
142 (goto-char (point-max))
143 (unless (re-search-backward (cdr (assq 'signed-end-line scheme-alist)))
144 (error "Cannot find signature part." ))
145 (goto-char (match-beginning 0))
146 (unless (re-search-backward "^-+BEGIN" nil t)
147 (error "Cannot find signature part." ))
148 (goto-char (match-beginning 0))
149 (insert (format "--%s\n" boundary))
150 (insert "Content-Type: application/pgp-signature\n\n")
151 (goto-char (point-max))
152 (insert (format "--%s--\n" boundary))
153 (goto-char (point-max))))
155 (defun mml2015-mailcrypt-encrypt (cont)
157 (or (message-options-get 'message-recipients)
158 (message-options-set 'message-recipients
159 (mc-cleanup-recipient-headers
160 (read-string "Recipients: "))))
162 (message-options-get 'message-sender)
163 (or mc-pgp-always-sign
165 (or (message-options-get 'message-sign-encrypt)
166 (message-options-set 'message-sign-encrypt
167 (or (y-or-n-p "Sign the message? ")
170 (funcall mml-boundary-function (incf mml-multipart-number))))
171 (goto-char (point-min))
172 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
174 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
175 (insert (format "--%s\n" boundary))
176 (insert "Content-Type: application/pgp-encrypted\n\n")
177 (insert "Version: 1\n\n")
178 (insert (format "--%s\n" boundary))
179 (insert "Content-Type: application/octet-stream\n\n")
180 (goto-char (point-max))
181 (insert (format "--%s--\n" boundary))
182 (goto-char (point-max))))
187 (autoload 'gpg-decrypt "gpg")
188 (autoload 'gpg-verify "gpg")
189 (autoload 'gpg-sign-detached "gpg")
190 (autoload 'gpg-sign-encrypt "gpg")
191 (autoload 'gpg-passphrase-read "gpg"))
193 (defun mml2015-gpg-passphrase ()
194 (or (message-options-get 'gpg-passphrase)
195 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
197 (defun mml2015-gpg-decrypt-1 ()
198 (let ((cipher (current-buffer)) plain result)
199 (if (with-temp-buffer
201 (gpg-decrypt cipher (setq plain (current-buffer))
202 mml2015-result-buffer nil)
205 (insert-buffer plain)))
207 ;; Some wrong with the return value, check plain text buffer.
208 (if (> (point-max) (point-min))
210 (pop-to-buffer mml2015-result-buffer)
213 (defun mml2015-gpg-decrypt (handle ctl)
214 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
215 (mml2015-mailcrypt-decrypt handle ctl)))
217 (defun mml2015-gpg-verify (handle ctl)
218 (let (part message signature)
219 (unless (setq part (mm-find-raw-part-by-type
220 ctl "application/pgp-signature" t))
221 (error "Corrupted pgp-signature part."))
223 (setq message (current-buffer))
226 (setq signature (current-buffer))
227 (unless (setq part (mm-find-part-by-type
228 (cdr handle) "application/pgp-signature"))
229 (error "Corrupted pgp-signature part."))
230 (mm-insert-part part)
231 (unless (gpg-verify message signature mml2015-result-buffer)
232 (pop-to-buffer mml2015-result-buffer)
233 (error "Verify error.")))))
236 (defun mml2015-gpg-sign (cont)
238 (funcall mml-boundary-function (incf mml-multipart-number)))
239 (text (current-buffer)) signature)
240 (goto-char (point-max))
244 (unless (gpg-sign-detached text (setq signature (current-buffer))
245 mml2015-result-buffer
247 (message-options-get 'message-sender)
248 t t) ; armor & textmode
249 (unless (> (point-max) (point-min))
250 (pop-to-buffer mml2015-result-buffer)
251 (error "Sign error.")))
253 (goto-char (point-min))
254 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
256 ;;; FIXME: what is the micalg?
257 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
258 (insert (format "\n--%s\n" boundary))
259 (goto-char (point-max))
260 (insert (format "\n--%s\n" boundary))
261 (insert "Content-Type: application/pgp-signature\n\n")
262 (insert-buffer signature)
263 (goto-char (point-max))
264 (insert (format "--%s--\n" boundary))
265 (goto-char (point-max)))))
267 (defun mml2015-gpg-encrypt (cont)
269 (funcall mml-boundary-function (incf mml-multipart-number)))
270 (text (current-buffer))
273 (unless (gpg-sign-encrypt
274 text (setq cipher (current-buffer))
275 mml2015-result-buffer
278 (message-options-get 'message-recipients)
279 (message-options-set 'message-recipients
280 (read-string "Recipients: ")))
283 (message-options-get 'message-sender)
284 t t) ; armor & textmode
285 (unless (> (point-max) (point-min))
286 (pop-to-buffer mml2015-result-buffer)
287 (error "Encrypt error.")))
289 (delete-region (point-min) (point-max))
290 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
292 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
293 (insert (format "--%s\n" boundary))
294 (insert "Content-Type: application/pgp-encrypted\n\n")
295 (insert "Version: 1\n\n")
296 (insert (format "--%s\n" boundary))
297 (insert "Content-Type: application/octet-stream\n\n")
298 (insert-buffer cipher)
299 (goto-char (point-max))
300 (insert (format "--%s--\n" boundary))
301 (goto-char (point-max)))))
305 (defun mml2015-clean-buffer ()
306 (if (gnus-buffer-live-p mml2015-result-buffer)
307 (with-current-buffer mml2015-result-buffer
310 (setq mml2015-result-buffer
311 (gnus-get-buffer-create "*MML2015 Result*"))
315 (defun mml2015-decrypt (handle ctl)
316 (mml2015-clean-buffer)
317 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
319 (funcall func handle ctl)
323 (defun mml2015-verify (handle ctl)
324 (mml2015-clean-buffer)
325 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
327 (funcall func handle ctl)
331 (defun mml2015-encrypt (cont)
332 (mml2015-clean-buffer)
333 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
336 (error "Cannot find encrypt function."))))
339 (defun mml2015-sign (cont)
340 (mml2015-clean-buffer)
341 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
344 (error "Cannot find sign function."))))
348 ;;; mml2015.el ends here