9d64d57ba1633c3edcef400b3cf4fa5ff39586a0
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; This file is not part of GNU Emacs, but the same permissions apply.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'smime)
27 (require 'mml2015)
28 (require 'mml-smime)
29 (eval-when-compile (require 'cl))
30
31 (defvar mml-sign-alist
32   '(("smime"     mml-smime-sign-buffer     mml-smime-sign-query)
33     ("pgpmime"   mml-pgpmime-sign-buffer   list))
34   "Alist of MIME signer functions.")
35
36 (defvar mml-default-sign-method (caar mml-sign-alist)
37   "Default sign method.")
38
39 (defvar mml-encrypt-alist
40   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
41     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
42   "Alist of MIME encryption functions.")
43
44 (defvar mml-default-encrypt-method (caar mml-encrypt-alist)
45   "Default encryption method.")
46
47 ;;; Security functions
48
49 (defun mml-smime-sign-buffer (cont)
50   (or (mml-smime-sign cont)
51       (error "Signing failed... inspect message logs for errors")))
52
53 (defun mml-smime-encrypt-buffer (cont)
54   (or (mml-smime-encrypt cont)
55       (error "Encryption failed... inspect message logs for errors")))
56
57 (defun mml-pgpmime-sign-buffer (cont)
58   (or (mml2015-sign cont)
59       (error "Signing failed... inspect message logs for errors")))
60
61 (defun mml-pgpmime-encrypt-buffer (cont)
62   (or (mml2015-encrypt cont)
63       (error "Encryption failed... inspect message logs for errors")))
64
65 (defun mml-secure-part (method &optional sign)
66   (save-excursion
67     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
68                                                 mml-encrypt-alist))))))
69       (cond ((re-search-backward
70               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
71              (goto-char (match-end 0))
72              (insert (if sign " sign=" " encrypt=") method)
73              (while tags
74                (let ((key (pop tags))
75                      (value (pop tags)))
76                  (when value
77                    ;; Quote VALUE if it contains suspicious characters.
78                    (when (string-match "[\"'\\~/*;() \t\n]" value)
79                      (setq value (prin1-to-string value)))
80                    (insert (format " %s=%s" key value))))))
81             ((or (re-search-backward 
82                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
83                  (re-search-forward
84                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
85              (goto-char (match-end 0))
86              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
87                                                 (cons method tags))))
88             (t (error "The message is corrupted. No mail header separator"))))))
89
90 (defun mml-secure-sign-pgpmime ()
91   "Add MML tags to PGP/MIME sign this MML part."
92   (interactive)
93   (mml-secure-part "pgpmime" 'sign))
94
95 (defun mml-secure-sign-smime ()
96   "Add MML tags to S/MIME sign this MML part."
97   (interactive)
98   (mml-secure-part "smime" 'sign))
99
100 (defun mml-secure-encrypt-pgpmime ()
101   "Add MML tags to PGP/MIME encrypt this MML part."
102   (interactive)
103   (mml-secure-part "pgpmime"))
104
105 (defun mml-secure-encrypt-smime ()
106   "Add MML tags to S/MIME encrypt this MML part."
107   (interactive)
108   (mml-secure-part "smime"))
109
110 (provide 'mml-sec)
111
112 ;;; mml-sec.el ends here