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)
81 (let (child handles result)
82 (unless (setq child (mm-find-part-by-type
84 "application/octet-stream" nil t))
85 (mm-set-handle-multipart-parameter
86 mm-security-handle 'gnus-info "Corrupted")
87 (throw 'error handle))
89 (mm-insert-part child)
92 (funcall mml2015-decrypt-function)
94 (mm-set-handle-multipart-parameter
95 mm-security-handle 'gnus-details (cadr err))
98 (mm-set-handle-multipart-parameter
99 mm-security-handle 'gnus-info "Failed")
100 (throw 'error handle))
101 (setq handles (mm-dissect-buffer t)))
102 (mm-destroy-parts handle)
103 (mm-set-handle-multipart-parameter
104 mm-security-handle 'gnus-info "OK")
105 (if (listp (car handles))
109 (defun mml2015-mailcrypt-clear-decrypt ()
113 (funcall mml2015-decrypt-function)
115 (mm-set-handle-multipart-parameter
116 mm-security-handle 'gnus-details (cadr err))
119 (mm-set-handle-multipart-parameter
120 mm-security-handle 'gnus-info "OK")
121 (mm-set-handle-multipart-parameter
122 mm-security-handle 'gnus-info "Failed"))))
124 (defun mml2015-fix-micalg (alg)
126 (if (and alg (string-match "^pgp-" alg))
127 (substring alg (match-end 0))
130 (defun mml2015-mailcrypt-verify (handle ctl)
133 (unless (setq part (mm-find-raw-part-by-type
134 ctl (or (mail-content-type-get ctl 'protocol)
135 "application/pgp-signature")
137 (mm-set-handle-multipart-parameter
138 mm-security-handle 'gnus-info "Corrupted")
139 (throw 'error handle))
141 (insert "-----BEGIN PGP SIGNED MESSAGE-----\n")
142 (insert (format "Hash: %s\n\n"
143 (or (mml2015-fix-micalg
144 (mail-content-type-get ctl 'micalg))
147 (goto-char (point-max))
148 (unless (setq part (mm-find-part-by-type
149 (cdr handle) "application/pgp-signature" nil t))
150 (mm-set-handle-multipart-parameter
151 mm-security-handle 'gnus-info "Corrupted")
152 (throw 'error handle))
153 (mm-insert-part part)
154 (unless (condition-case err
155 (funcall mml2015-verify-function)
157 (mm-set-handle-multipart-parameter
158 mm-security-handle 'gnus-details (cadr err))
160 (mm-set-handle-multipart-parameter
161 mm-security-handle 'gnus-info "Failed")
162 (throw 'error handle)))
163 (mm-set-handle-multipart-parameter
164 mm-security-handle 'gnus-info "OK")
167 (defun mml2015-mailcrypt-clear-verify ()
168 (if (condition-case err
169 (funcall mml2015-verify-function)
171 (mm-set-handle-multipart-parameter
172 mm-security-handle 'gnus-details (cadr err))
174 (mm-set-handle-multipart-parameter
175 mm-security-handle 'gnus-info "OK")
176 (mm-set-handle-multipart-parameter
177 mm-security-handle 'gnus-info "Failed")))
179 (defun mml2015-mailcrypt-sign (cont)
180 (mc-sign-generic (message-options-get 'message-sender)
183 (funcall mml-boundary-function (incf mml-multipart-number)))
184 (scheme-alist (funcall (or mc-default-scheme
185 (cdr (car mc-schemes)))))
187 (goto-char (point-min))
188 (unless (re-search-forward (cdr (assq 'signed-begin-line scheme-alist)))
189 (error "Cannot find signed begin line." ))
190 (goto-char (match-beginning 0))
192 (unless (looking-at "Hash:[ \t]*\\([a-zA-Z0-9]+\\)")
193 (error "Cannot not find PGP hash." ))
194 (setq hash (match-string 1))
195 (unless (re-search-forward "^$" nil t)
196 (error "Cannot not find PGP message." ))
198 (delete-region (point-min) (point))
199 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
201 (insert (format "\tmicalg=pgp-%s; protocol=\"application/pgp-signature\"\n"
203 (insert (format "\n--%s\n" boundary))
204 (goto-char (point-max))
205 (unless (re-search-backward (cdr (assq 'signed-end-line scheme-alist)))
206 (error "Cannot find signature part." ))
207 (goto-char (match-beginning 0))
208 (unless (re-search-backward "^-+BEGIN" nil t)
209 (error "Cannot find signature part." ))
210 (goto-char (match-beginning 0))
211 (insert (format "--%s\n" boundary))
212 (insert "Content-Type: application/pgp-signature\n\n")
213 (goto-char (point-max))
214 (insert (format "--%s--\n" boundary))
215 (goto-char (point-max))))
217 (defun mml2015-mailcrypt-encrypt (cont)
218 (let ((mc-pgp-always-sign
219 (or mc-pgp-always-sign
220 (eq t (or (message-options-get 'message-sign-encrypt)
222 'message-sign-encrypt
223 (or (y-or-n-p "Sign the message? ")
227 (or (message-options-get 'message-recipients)
228 (message-options-set 'message-recipients
229 (mc-cleanup-recipient-headers
230 (read-string "Recipients: "))))
232 (message-options-get 'message-sender)))
234 (funcall mml-boundary-function (incf mml-multipart-number))))
235 (goto-char (point-min))
236 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
238 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
239 (insert (format "--%s\n" boundary))
240 (insert "Content-Type: application/pgp-encrypted\n\n")
241 (insert "Version: 1\n\n")
242 (insert (format "--%s\n" boundary))
243 (insert "Content-Type: application/octet-stream\n\n")
244 (goto-char (point-max))
245 (insert (format "--%s--\n" boundary))
246 (goto-char (point-max))))
251 (autoload 'gpg-decrypt "gpg")
252 (autoload 'gpg-verify "gpg")
253 (autoload 'gpg-sign-detached "gpg")
254 (autoload 'gpg-sign-encrypt "gpg")
255 (autoload 'gpg-passphrase-read "gpg"))
257 (defun mml2015-gpg-passphrase ()
258 (or (message-options-get 'gpg-passphrase)
259 (message-options-set 'gpg-passphrase (gpg-passphrase-read))))
261 (defun mml2015-gpg-decrypt-1 ()
262 (let ((cipher (current-buffer)) plain result)
263 (if (with-temp-buffer
265 (gpg-decrypt cipher (setq plain (current-buffer))
266 mml2015-result-buffer nil)
269 (insert-buffer plain)))
271 ;; Some wrong with the return value, check plain text buffer.
272 (if (> (point-max) (point-min))
274 (mm-set-handle-multipart-parameter
275 mm-security-handle 'gnus-details
276 (buffer-string mml2015-result-buffer))
279 (defun mml2015-gpg-decrypt (handle ctl)
280 (let ((mml2015-decrypt-function 'mml2015-gpg-decrypt-1))
281 (mml2015-mailcrypt-decrypt handle ctl)))
283 (defun mml2015-gpg-clear-decrypt ()
285 (setq result (mml2015-gpg-decrypt-1))
287 (mm-set-handle-multipart-parameter
288 mm-security-handle 'gnus-info "OK")
289 (mm-set-handle-multipart-parameter
290 mm-security-handle 'gnus-info "Failed"))))
292 (defun mml2015-gpg-verify (handle ctl)
294 (let (part message signature)
295 (unless (setq part (mm-find-raw-part-by-type
296 ctl (or (mail-content-type-get ctl 'protocol)
297 "application/pgp-signature")
299 (mm-set-handle-multipart-parameter
300 mm-security-handle 'gnus-info "Corrupted")
301 (throw 'error handle))
303 (setq message (current-buffer))
306 (setq signature (current-buffer))
307 (unless (setq part (mm-find-part-by-type
308 (cdr handle) "application/pgp-signature" nil t))
309 (mm-set-handle-multipart-parameter
310 mm-security-handle 'gnus-info "Corrupted")
311 (throw 'error handle))
312 (mm-insert-part part)
313 (unless (gpg-verify message signature mml2015-result-buffer)
314 (mm-set-handle-multipart-parameter
315 mm-security-handle 'gnus-details
316 (buffer-string mml2015-result-buffer))
317 (mm-set-handle-multipart-parameter
318 mm-security-handle 'gnus-info "Failed")
319 (throw 'error handle))))
322 (defun mml2015-gpg-sign (cont)
324 (funcall mml-boundary-function (incf mml-multipart-number)))
325 (text (current-buffer)) signature)
326 (goto-char (point-max))
330 (unless (gpg-sign-detached text (setq signature (current-buffer))
331 mml2015-result-buffer
333 (message-options-get 'message-sender)
334 t t) ; armor & textmode
335 (unless (> (point-max) (point-min))
336 (pop-to-buffer mml2015-result-buffer)
337 (error "Sign error.")))
339 (goto-char (point-min))
340 (insert (format "Content-Type: multipart/signed; boundary=\"%s\";\n"
342 ;;; FIXME: what is the micalg?
343 (insert "\tmicalg=pgp-sha1; protocol=\"application/pgp-signature\"\n")
344 (insert (format "\n--%s\n" boundary))
345 (goto-char (point-max))
346 (insert (format "\n--%s\n" boundary))
347 (insert "Content-Type: application/pgp-signature\n\n")
348 (insert-buffer signature)
349 (goto-char (point-max))
350 (insert (format "--%s--\n" boundary))
351 (goto-char (point-max)))))
353 (defun mml2015-gpg-encrypt (cont)
355 (funcall mml-boundary-function (incf mml-multipart-number)))
356 (text (current-buffer))
359 (unless (gpg-sign-encrypt
360 text (setq cipher (current-buffer))
361 mml2015-result-buffer
364 (message-options-get 'message-recipients)
365 (message-options-set 'message-recipients
366 (read-string "Recipients: ")))
369 (message-options-get 'message-sender)
370 t t) ; armor & textmode
371 (unless (> (point-max) (point-min))
372 (pop-to-buffer mml2015-result-buffer)
373 (error "Encrypt error.")))
375 (delete-region (point-min) (point-max))
376 (insert (format "Content-Type: multipart/encrypted; boundary=\"%s\";\n"
378 (insert "\tprotocol=\"application/pgp-encrypted\"\n\n")
379 (insert (format "--%s\n" boundary))
380 (insert "Content-Type: application/pgp-encrypted\n\n")
381 (insert "Version: 1\n\n")
382 (insert (format "--%s\n" boundary))
383 (insert "Content-Type: application/octet-stream\n\n")
384 (insert-buffer cipher)
385 (goto-char (point-max))
386 (insert (format "--%s--\n" boundary))
387 (goto-char (point-max)))))
391 (defun mml2015-clean-buffer ()
392 (if (gnus-buffer-live-p mml2015-result-buffer)
393 (with-current-buffer mml2015-result-buffer
396 (setq mml2015-result-buffer
397 (gnus-get-buffer-create "*MML2015 Result*"))
400 (defsubst mml2015-clear-decrypt-function ()
401 (nth 6 (assq mml2015-use mml2015-function-alist)))
403 (defsubst mml2015-clear-verify-function ()
404 (nth 5 (assq mml2015-use mml2015-function-alist)))
407 (defun mml2015-decrypt (handle ctl)
408 (mml2015-clean-buffer)
409 (let ((func (nth 4 (assq mml2015-use mml2015-function-alist))))
411 (funcall func handle ctl)
415 (defun mml2015-decrypt-test (handle ctl)
419 (defun mml2015-verify (handle ctl)
420 (mml2015-clean-buffer)
421 (let ((func (nth 3 (assq mml2015-use mml2015-function-alist))))
423 (funcall func handle ctl)
427 (defun mml2015-verify-test (handle ctl)
431 (defun mml2015-encrypt (cont)
432 (mml2015-clean-buffer)
433 (let ((func (nth 2 (assq mml2015-use mml2015-function-alist))))
436 (error "Cannot find encrypt function."))))
439 (defun mml2015-sign (cont)
440 (mml2015-clean-buffer)
441 (let ((func (nth 1 (assq mml2015-use mml2015-function-alist))))
444 (error "Cannot find sign function."))))
447 (defun mml2015-self-encrypt ()
448 (mml2015-encrypt nil))
452 ;;; mml2015.el ends here