858737ce26c64763c6bbb2e0029a842449f8e94d
[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 (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)
99   (or (mml-smime-encrypt cont)
100       (error "Encryption failed... inspect message logs for errors")))
101
102 (defun mml-pgp-sign-buffer (cont)
103   (or (mml1991-sign cont)
104       (error "Signing failed... inspect message logs for errors")))
105
106 (defun mml-pgp-encrypt-buffer (cont)
107   (or (mml1991-encrypt cont)
108       (error "Encryption failed... inspect message logs for errors")))
109
110 (defun mml-pgpmime-sign-buffer (cont)
111   (or (mml2015-sign cont)
112       (error "Signing failed... inspect message logs for errors")))
113
114 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
115   (or (mml2015-encrypt cont sign)
116       (error "Encryption failed... inspect message logs for errors")))
117
118 (defun mml-secure-part (method &optional sign)
119   (save-excursion
120     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
121                                                 mml-encrypt-alist))))))
122       (cond ((re-search-backward
123               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
124              (goto-char (match-end 0))
125              (insert (if sign " sign=" " encrypt=") method)
126              (while tags
127                (let ((key (pop tags))
128                      (value (pop tags)))
129                  (when value
130                    ;; Quote VALUE if it contains suspicious characters.
131                    (when (string-match "[\"'\\~/*;() \t\n]" value)
132                      (setq value (prin1-to-string value)))
133                    (insert (format " %s=%s" key value))))))
134             ((or (re-search-backward
135                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
136                  (re-search-forward
137                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
138              (goto-char (match-end 0))
139              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
140                                                 (cons method tags))))
141             (t (error "The message is corrupted. No mail header separator"))))))
142
143 (defun mml-secure-sign-pgp ()
144   "Add MML tags to PGP sign this MML part."
145   (interactive)
146   (mml-secure-part "pgp" 'sign))
147
148 (defun mml-secure-sign-pgpmime ()
149   "Add MML tags to PGP/MIME sign this MML part."
150   (interactive)
151   (mml-secure-part "pgpmime" 'sign))
152
153 (defun mml-secure-sign-smime ()
154   "Add MML tags to S/MIME sign this MML part."
155   (interactive)
156   (mml-secure-part "smime" 'sign))
157
158 (defun mml-secure-encrypt-pgp ()
159   "Add MML tags to PGP encrypt this MML part."
160   (interactive)
161   (mml-secure-part "pgp"))
162
163 (defun mml-secure-encrypt-pgpmime ()
164   "Add MML tags to PGP/MIME encrypt this MML part."
165   (interactive)
166   (mml-secure-part "pgpmime"))
167
168 (defun mml-secure-encrypt-smime ()
169   "Add MML tags to S/MIME encrypt this MML part."
170   (interactive)
171   (mml-secure-part "smime"))
172
173 ;; defuns that add the proper <#secure ...> tag to the top of the message body
174 (defun mml-secure-message (method &optional modesym)
175   (let ((mode (prin1-to-string modesym))
176         insert-loc)
177     (mml-unsecure-message)
178     (save-excursion
179       (goto-char (point-min))
180       (cond ((re-search-forward
181               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
182              (goto-char (setq insert-loc (match-end 0)))
183              (unless (looking-at "<#secure")
184                (mml-insert-tag
185                 'secure 'method method 'mode mode)))
186             (t (error
187                 "The message is corrupted. No mail header separator"))))
188     (when (eql insert-loc (point))
189       (forward-line 1))))
190
191 (defun mml-unsecure-message ()
192   "Remove security related MML tags from message."
193   (interactive)
194   (save-excursion
195     (goto-char (point-max))
196     (when (re-search-backward "^<#secure.*>\n" nil t)
197       (kill-region (match-beginning 0) (match-end 0)))))
198
199 (defun mml-secure-message-sign-smime ()
200   "Add MML tag to encrypt/sign the entire message."
201   (interactive)
202   (mml-secure-message "smime" 'sign))
203
204 (defun mml-secure-message-sign-pgp ()
205   "Add MML tag to encrypt/sign the entire message."
206   (interactive)
207   (mml-secure-message "pgp" 'sign))
208
209 (defun mml-secure-message-sign-pgpmime ()
210   "Add MML tag to encrypt/sign the entire message."
211   (interactive)
212   (mml-secure-message "pgpmime" 'sign))
213
214 (defun mml-secure-message-encrypt-smime (&optional dontsign)
215   "Add MML tag to encrypt and sign the entire message.
216 If called with a prefix argument, only encrypt (do NOT sign)."
217   (interactive "P")
218   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
219
220 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
221   "Add MML tag to encrypt and sign the entire message.
222 If called with a prefix argument, only encrypt (do NOT sign)."
223   (interactive "P")
224   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
225
226 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
227   "Add MML tag to encrypt and sign the entire message.
228 If called with a prefix argument, only encrypt (do NOT sign)."
229   (interactive "P")
230   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
231
232 (provide 'mml-sec)
233
234 ;;; mml-sec.el ends here