* gnus-range.el (gnus-list-range-intersection): Correct the logic.
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002 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 'mml2015)
27 (require 'mml1991)
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     ("pgp"       mml-pgp-sign-buffer       list)
34     ("pgpmime"   mml-pgpmime-sign-buffer   list))
35   "Alist of MIME signer functions.")
36
37 (defvar mml-default-sign-method (caar mml-sign-alist)
38   "Default sign method.")
39
40 (defvar mml-encrypt-alist
41   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
42     ("pgp"       mml-pgp-encrypt-buffer       list)
43     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
44   "Alist of MIME encryption functions.")
45
46 (defvar mml-default-encrypt-method (caar mml-encrypt-alist)
47   "Default encryption method.")
48
49 ;;; Security functions
50
51 (defun mml-smime-sign-buffer (cont)
52   (or (mml-smime-sign cont)
53       (error "Signing failed... inspect message logs for errors")))
54
55 (defun mml-smime-encrypt-buffer (cont)
56   (or (mml-smime-encrypt cont)
57       (error "Encryption failed... inspect message logs for errors")))
58
59 (defun mml-pgp-sign-buffer (cont)
60   (or (mml1991-sign cont)
61       (error "Signing failed... inspect message logs for errors")))
62
63 (defun mml-pgp-encrypt-buffer (cont)
64   (or (mml1991-encrypt cont)
65       (error "Encryption failed... inspect message logs for errors")))
66
67 (defun mml-pgpmime-sign-buffer (cont)
68   (or (mml2015-sign cont)
69       (error "Signing failed... inspect message logs for errors")))
70
71 (defun mml-pgpmime-encrypt-buffer (cont)
72   (or (mml2015-encrypt cont)
73       (error "Encryption failed... inspect message logs for errors")))
74
75 (defun mml-secure-part (method &optional sign)
76   (save-excursion
77     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
78                                                 mml-encrypt-alist))))))
79       (cond ((re-search-backward
80               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
81              (goto-char (match-end 0))
82              (insert (if sign " sign=" " encrypt=") method)
83              (while tags
84                (let ((key (pop tags))
85                      (value (pop tags)))
86                  (when value
87                    ;; Quote VALUE if it contains suspicious characters.
88                    (when (string-match "[\"'\\~/*;() \t\n]" value)
89                      (setq value (prin1-to-string value)))
90                    (insert (format " %s=%s" key value))))))
91             ((or (re-search-backward
92                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
93                  (re-search-forward
94                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
95              (goto-char (match-end 0))
96              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
97                                                 (cons method tags))))
98             (t (error "The message is corrupted. No mail header separator"))))))
99
100 (defun mml-secure-sign-pgp ()
101   "Add MML tags to PGP sign this MML part."
102   (interactive)
103   (mml-secure-part "pgp" 'sign))
104
105 (defun mml-secure-sign-pgpmime ()
106   "Add MML tags to PGP/MIME sign this MML part."
107   (interactive)
108   (mml-secure-part "pgpmime" 'sign))
109
110 (defun mml-secure-sign-smime ()
111   "Add MML tags to S/MIME sign this MML part."
112   (interactive)
113   (mml-secure-part "smime" 'sign))
114
115 (defun mml-secure-encrypt-pgp ()
116   "Add MML tags to PGP encrypt this MML part."
117   (interactive)
118   (mml-secure-part "pgp"))
119
120 (defun mml-secure-encrypt-pgpmime ()
121   "Add MML tags to PGP/MIME encrypt this MML part."
122   (interactive)
123   (mml-secure-part "pgpmime"))
124
125 (defun mml-secure-encrypt-smime ()
126   "Add MML tags to S/MIME encrypt this MML part."
127   (interactive)
128   (mml-secure-part "smime"))
129
130 ;; defuns that add the proper <#secure ...> tag to the top of the message body
131 (defun mml-secure-message (method &optional sign)
132   (let ((mode (if sign "sign" "encrypt"))
133         insert-loc)
134     (mml-unsecure-message)
135     (save-excursion
136       (goto-char (point-max))
137       (cond ((re-search-backward
138               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
139              (goto-char (setq insert-loc (match-end 0)))
140              (unless (looking-at "<#secure")
141                (mml-insert-tag
142                 'secure 'method method 'mode mode)))
143             (t (error
144                 "The message is corrupted. No mail header separator"))))
145     (when (eql insert-loc (point))
146       (forward-line 1))))
147
148 (defun mml-unsecure-message ()
149   (interactive)
150   (save-excursion
151     (goto-char (point-max))
152     (when (re-search-backward "^<#secure.*>\n" nil t)
153       (kill-region (match-beginning 0) (match-end 0)))))
154
155 (defun mml-secure-message-sign-smime ()
156   "Add MML tag to encrypt/sign the entire message."
157   (interactive)
158   (mml-secure-message "smime" 'sign))
159
160 (defun mml-secure-message-sign-pgp ()
161   "Add MML tag to encrypt/sign the entire message."
162   (interactive)
163   (mml-secure-message "pgp" 'sign))
164
165 (defun mml-secure-message-sign-pgpmime ()
166   "Add MML tag to encrypt/sign the entire message."
167   (interactive)
168   (mml-secure-message "pgpmime" 'sign))
169
170 (defun mml-secure-message-encrypt-smime ()
171   "Add MML tag to encrypt/sign the entire message."
172   (interactive)
173   (mml-secure-message "smime"))
174
175 (defun mml-secure-message-encrypt-pgp ()
176   "Add MML tag to encrypt/sign the entire message."
177   (interactive)
178   (mml-secure-message "pgp"))
179
180 (defun mml-secure-message-encrypt-pgpmime ()
181   "Add MML tag to encrypt/sign the entire message."
182   (interactive)
183   (mml-secure-message "pgpmime"))
184
185 (provide 'mml-sec)
186
187 ;;; mml-sec.el ends here