Import Oort Gnus v0.09.
[elisp/gnus.git-] / 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 (defcustom mml-signencrypt-style-alist
50   '(("smime"   separate)
51     ("pgp"     separate)
52     ("pgpmime" separate))
53   "Alist specifying if `signencrypt' results in two separate operations or not.
54 The first entry indicates the MML security type, valid entries include
55 the strings \"smime\", \"pgp\", and \"pgpmime\".  The second entry is
56 a symbol `separate' or `combined' where `separate' means that MML signs
57 and encrypt messages in a two step process, and `combined' means that MML
58 signs and encrypt the message in one step.
59 Note that the `combined' mode is NOT supported by all OpenPGP implementations,
60 in particular PGP version 2 does not support it!"
61   :type '(repeat (list (choice (const :tag "S/MIME" "smime")
62                                (const :tag "PGP" "pgp")
63                                (const :tag "PGP/MIME" "pgpmime")
64                                (string :tag "User defined"))
65                        (choice (const :tag "Separate" separate)
66                                (const :tag "Combined" combined)))))
67                               
68 ;;; Configuration/helper functions
69
70 (defun mml-signencrypt-style (method &optional style)
71   "Function for setting/getting the signencrypt-style used.  Takes two
72 arguments, the method (e.g. \"pgp\") and optionally the mode
73 \(e.g. combined).  If the mode is omitted, the current value is returned.
74
75 For example, if you prefer to use combined sign & encrypt with
76 smime, putting the following in your Gnus startup file will
77 enable that behavior:
78
79 \(mml-set-signencrypt-style \"smime\" combined)
80
81 You can also customize or set `mml-signencrypt-style-alist' instead."
82   (let ((style-item (assoc method mml-signencrypt-style-alist)))
83     (if style-item
84         (if (or (eq style 'separate)
85                 (eq style 'combined))
86             ;; valid style setting?
87             (setf (second style-item) style)
88           ;; otherwise, just return the current value
89           (second style-item))
90       (gnus-message 3 "Warning, attempt to set invalid signencrypt-style"))))
91
92 ;;; Security functions
93
94 (defun mml-smime-sign-buffer (cont)
95   (or (mml-smime-sign cont)
96       (error "Signing failed... inspect message logs for errors")))
97
98 (defun mml-smime-encrypt-buffer (cont &optional sign)
99   (when sign
100     (message "Combined sign and encrypt S/MIME not support yet")
101     (sit-for 1))
102   (or (mml-smime-encrypt cont)
103       (error "Encryption failed... inspect message logs for errors")))
104
105 (defun mml-pgp-sign-buffer (cont)
106   (or (mml1991-sign cont)
107       (error "Signing failed... inspect message logs for errors")))
108
109 (defun mml-pgp-encrypt-buffer (cont &optional sign)
110   (or (mml1991-encrypt cont sign)
111       (error "Encryption failed... inspect message logs for errors")))
112
113 (defun mml-pgpmime-sign-buffer (cont)
114   (or (mml2015-sign cont)
115       (error "Signing failed... inspect message logs for errors")))
116
117 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
118   (or (mml2015-encrypt cont sign)
119       (error "Encryption failed... inspect message logs for errors")))
120
121 (defun mml-secure-part (method &optional sign)
122   (save-excursion
123     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
124                                                 mml-encrypt-alist))))))
125       (cond ((re-search-backward
126               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
127              (goto-char (match-end 0))
128              (insert (if sign " sign=" " encrypt=") method)
129              (while tags
130                (let ((key (pop tags))
131                      (value (pop tags)))
132                  (when value
133                    ;; Quote VALUE if it contains suspicious characters.
134                    (when (string-match "[\"'\\~/*;() \t\n]" value)
135                      (setq value (prin1-to-string value)))
136                    (insert (format " %s=%s" key value))))))
137             ((or (re-search-backward
138                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
139                  (re-search-forward
140                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
141              (goto-char (match-end 0))
142              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
143                                                 (cons method tags))))
144             (t (error "The message is corrupted. No mail header separator"))))))
145
146 (defun mml-secure-sign-pgp ()
147   "Add MML tags to PGP sign this MML part."
148   (interactive)
149   (mml-secure-part "pgp" 'sign))
150
151 (defun mml-secure-sign-pgpmime ()
152   "Add MML tags to PGP/MIME sign this MML part."
153   (interactive)
154   (mml-secure-part "pgpmime" 'sign))
155
156 (defun mml-secure-sign-smime ()
157   "Add MML tags to S/MIME sign this MML part."
158   (interactive)
159   (mml-secure-part "smime" 'sign))
160
161 (defun mml-secure-encrypt-pgp ()
162   "Add MML tags to PGP encrypt this MML part."
163   (interactive)
164   (mml-secure-part "pgp"))
165
166 (defun mml-secure-encrypt-pgpmime ()
167   "Add MML tags to PGP/MIME encrypt this MML part."
168   (interactive)
169   (mml-secure-part "pgpmime"))
170
171 (defun mml-secure-encrypt-smime ()
172   "Add MML tags to S/MIME encrypt this MML part."
173   (interactive)
174   (mml-secure-part "smime"))
175
176 ;; defuns that add the proper <#secure ...> tag to the top of the message body
177 (defun mml-secure-message (method &optional modesym)
178   (let ((mode (prin1-to-string modesym))
179         insert-loc)
180     (mml-unsecure-message)
181     (save-excursion
182       (goto-char (point-min))
183       (cond ((re-search-forward
184               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
185              (goto-char (setq insert-loc (match-end 0)))
186              (unless (looking-at "<#secure")
187                (mml-insert-tag
188                 'secure 'method method 'mode mode)))
189             (t (error
190                 "The message is corrupted. No mail header separator"))))
191     (when (eql insert-loc (point))
192       (forward-line 1))))
193
194 (defun mml-unsecure-message ()
195   "Remove security related MML tags from message."
196   (interactive)
197   (save-excursion
198     (goto-char (point-max))
199     (when (re-search-backward "^<#secure.*>\n" nil t)
200       (kill-region (match-beginning 0) (match-end 0)))))
201
202 (defun mml-secure-message-sign-smime ()
203   "Add MML tag to encrypt/sign the entire message."
204   (interactive)
205   (mml-secure-message "smime" 'sign))
206
207 (defun mml-secure-message-sign-pgp ()
208   "Add MML tag to encrypt/sign the entire message."
209   (interactive)
210   (mml-secure-message "pgp" 'sign))
211
212 (defun mml-secure-message-sign-pgpmime ()
213   "Add MML tag to encrypt/sign the entire message."
214   (interactive)
215   (mml-secure-message "pgpmime" 'sign))
216
217 (defun mml-secure-message-encrypt-smime (&optional dontsign)
218   "Add MML tag to encrypt and sign the entire message.
219 If called with a prefix argument, only encrypt (do NOT sign)."
220   (interactive "P")
221   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
222
223 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
224   "Add MML tag to encrypt and sign the entire message.
225 If called with a prefix argument, only encrypt (do NOT sign)."
226   (interactive "P")
227   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
228
229 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
230   "Add MML tag to encrypt and sign the entire message.
231 If called with a prefix argument, only encrypt (do NOT sign)."
232   (interactive "P")
233   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
234
235 (provide 'mml-sec)
236
237 ;;; mml-sec.el ends here