1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
28 (eval-when-compile (require 'cl))
29 (autoload 'mml2015-sign "mml2015")
30 (autoload 'mml2015-encrypt "mml2015")
31 (autoload 'mml1991-sign "mml1991")
32 (autoload 'mml1991-encrypt "mml1991")
33 (autoload 'message-goto-body "message")
34 (autoload 'mml-insert-tag "mml")
36 (defvar mml-sign-alist
37 '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
38 ("pgp" mml-pgp-sign-buffer list)
39 ("pgpauto" mml-pgpauto-sign-buffer list)
40 ("pgpmime" mml-pgpmime-sign-buffer list))
41 "Alist of MIME signer functions.")
43 (defcustom mml-default-sign-method "pgpmime"
45 The string must have an entry in `mml-sign-alist'."
47 :type '(choice (const "smime")
54 (defvar mml-encrypt-alist
55 '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
56 ("pgp" mml-pgp-encrypt-buffer list)
57 ("pgpauto" mml-pgpauto-sign-buffer list)
58 ("pgpmime" mml-pgpmime-encrypt-buffer list))
59 "Alist of MIME encryption functions.")
61 (defcustom mml-default-encrypt-method "pgpmime"
62 "Default encryption method.
63 The string must have an entry in `mml-encrypt-alist'."
65 :type '(choice (const "smime")
72 (defcustom mml-signencrypt-style-alist
77 "Alist specifying if `signencrypt' results in two separate operations or not.
78 The first entry indicates the MML security type, valid entries include
79 the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
80 a symbol `separate' or `combined' where `separate' means that MML signs
81 and encrypt messages in a two step process, and `combined' means that MML
82 signs and encrypt the message in one step.
84 Note that the output generated by using a `combined' mode is NOT
85 understood by all PGP implementations, in particular PGP version
86 2 does not support it! See Info node `(message)Security' for
90 :type '(repeat (list (choice (const :tag "S/MIME" "smime")
91 (const :tag "PGP" "pgp")
92 (const :tag "PGP/MIME" "pgpmime")
93 (string :tag "User defined"))
94 (choice (const :tag "Separate" separate)
95 (const :tag "Combined" combined)))))
97 ;;; Configuration/helper functions
99 (defun mml-signencrypt-style (method &optional style)
100 "Function for setting/getting the signencrypt-style used. Takes two
101 arguments, the method (e.g. \"pgp\") and optionally the mode
102 \(e.g. combined). If the mode is omitted, the current value is returned.
104 For example, if you prefer to use combined sign & encrypt with
105 smime, putting the following in your Gnus startup file will
106 enable that behavior:
108 \(mml-set-signencrypt-style \"smime\" combined)
110 You can also customize or set `mml-signencrypt-style-alist' instead."
111 (let ((style-item (assoc method mml-signencrypt-style-alist)))
113 (if (or (eq style 'separate)
114 (eq style 'combined))
115 ;; valid style setting?
116 (setf (second style-item) style)
117 ;; otherwise, just return the current value
119 (message "Warning, attempt to set invalid signencrypt style"))))
121 ;;; Security functions
123 (defun mml-smime-sign-buffer (cont)
124 (or (mml-smime-sign cont)
125 (error "Signing failed... inspect message logs for errors")))
127 (defun mml-smime-encrypt-buffer (cont &optional sign)
129 (message "Combined sign and encrypt S/MIME not support yet")
131 (or (mml-smime-encrypt cont)
132 (error "Encryption failed... inspect message logs for errors")))
134 (defun mml-pgp-sign-buffer (cont)
135 (or (mml1991-sign cont)
136 (error "Signing failed... inspect message logs for errors")))
138 (defun mml-pgp-encrypt-buffer (cont &optional sign)
139 (or (mml1991-encrypt cont sign)
140 (error "Encryption failed... inspect message logs for errors")))
142 (defun mml-pgpmime-sign-buffer (cont)
143 (or (mml2015-sign cont)
144 (error "Signing failed... inspect message logs for errors")))
146 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
147 (or (mml2015-encrypt cont sign)
148 (error "Encryption failed... inspect message logs for errors")))
150 (defun mml-pgpauto-sign-buffer (cont)
152 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
155 (error "Encryption failed... inspect message logs for errors")))
157 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
159 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
160 (mml2015-encrypt cont sign)
161 (mml1991-encrypt cont sign))
162 (error "Encryption failed... inspect message logs for errors")))
164 (defun mml-secure-part (method &optional sign)
166 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
167 mml-encrypt-alist))))))
168 (cond ((re-search-backward
169 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
170 (goto-char (match-end 0))
171 (insert (if sign " sign=" " encrypt=") method)
173 (let ((key (pop tags))
176 ;; Quote VALUE if it contains suspicious characters.
177 (when (string-match "[\"'\\~/*;() \t\n]" value)
178 (setq value (prin1-to-string value)))
179 (insert (format " %s=%s" key value))))))
180 ((or (re-search-backward
181 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
183 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
184 (goto-char (match-end 0))
185 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
186 (cons method tags))))
187 (t (error "The message is corrupted. No mail header separator"))))))
189 (defun mml-secure-sign-pgp ()
190 "Add MML tags to PGP sign this MML part."
192 (mml-secure-part "pgp" 'sign))
194 (defun mml-secure-sign-pgpauto ()
195 "Add MML tags to PGP-auto sign this MML part."
197 (mml-secure-part "pgpauto" 'sign))
199 (defun mml-secure-sign-pgpmime ()
200 "Add MML tags to PGP/MIME sign this MML part."
202 (mml-secure-part "pgpmime" 'sign))
204 (defun mml-secure-sign-smime ()
205 "Add MML tags to S/MIME sign this MML part."
207 (mml-secure-part "smime" 'sign))
209 (defun mml-secure-encrypt-pgp ()
210 "Add MML tags to PGP encrypt this MML part."
212 (mml-secure-part "pgp"))
214 (defun mml-secure-encrypt-pgpmime ()
215 "Add MML tags to PGP/MIME encrypt this MML part."
217 (mml-secure-part "pgpmime"))
219 (defun mml-secure-encrypt-smime ()
220 "Add MML tags to S/MIME encrypt this MML part."
222 (mml-secure-part "smime"))
224 ;; defuns that add the proper <#secure ...> tag to the top of the message body
225 (defun mml-secure-message (method &optional modesym)
226 (let ((mode (prin1-to-string modesym))
228 (if (or (eq modesym 'sign)
229 (eq modesym 'signencrypt))
230 (funcall (nth 2 (assoc method mml-sign-alist))))
231 (if (or (eq modesym 'encrypt)
232 (eq modesym 'signencrypt))
233 (funcall (nth 2 (assoc method mml-encrypt-alist))))))
235 (mml-unsecure-message)
237 (goto-char (point-min))
238 (cond ((re-search-forward
239 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
240 (goto-char (setq insert-loc (match-end 0)))
241 (unless (looking-at "<#secure")
242 (apply 'mml-insert-tag
243 'secure 'method method 'mode mode tags)))
245 "The message is corrupted. No mail header separator"))))
246 (when (eql insert-loc (point))
249 (defun mml-unsecure-message ()
250 "Remove security related MML tags from message."
253 (goto-char (point-max))
254 (when (re-search-backward "^<#secure.*>\n" nil t)
255 (delete-region (match-beginning 0) (match-end 0)))))
257 (defun mml-secure-message-sign-smime ()
258 "Add MML tag to encrypt/sign the entire message."
260 (mml-secure-message "smime" 'sign))
262 (defun mml-secure-message-sign-pgp ()
263 "Add MML tag to encrypt/sign the entire message."
265 (mml-secure-message "pgp" 'sign))
267 (defun mml-secure-message-sign-pgpmime ()
268 "Add MML tag to encrypt/sign the entire message."
270 (mml-secure-message "pgpmime" 'sign))
272 (defun mml-secure-message-sign-pgpauto ()
273 "Add MML tag to encrypt/sign the entire message."
275 (mml-secure-message "pgpauto" 'sign))
277 (defun mml-secure-message-encrypt-smime (&optional dontsign)
278 "Add MML tag to encrypt and sign the entire message.
279 If called with a prefix argument, only encrypt (do NOT sign)."
281 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
283 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
284 "Add MML tag to encrypt and sign the entire message.
285 If called with a prefix argument, only encrypt (do NOT sign)."
287 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
289 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
290 "Add MML tag to encrypt and sign the entire message.
291 If called with a prefix argument, only encrypt (do NOT sign)."
293 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
295 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
296 "Add MML tag to encrypt and sign the entire message.
297 If called with a prefix argument, only encrypt (do NOT sign)."
299 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
303 ;;; mml-sec.el ends here