1 ;;; mml-sec.el --- A package with security functions for MML documents
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
29 (eval-when-compile (require 'cl))
31 (if (featurep 'xemacs)
32 ;; Not all XEmacs versions support `noerror' arg of `require'.
33 (or (featurep 'password-cache)
34 (load "password-cache" t)
36 (or (require 'password-cache nil t)
39 (autoload 'mml2015-sign "mml2015")
40 (autoload 'mml2015-encrypt "mml2015")
41 (autoload 'mml1991-sign "mml1991")
42 (autoload 'mml1991-encrypt "mml1991")
43 (autoload 'message-goto-body "message")
44 (autoload 'mml-insert-tag "mml")
45 (autoload 'mml-smime-sign "mml-smime")
46 (autoload 'mml-smime-encrypt "mml-smime")
47 (autoload 'mml-smime-sign-query "mml-smime")
48 (autoload 'mml-smime-encrypt-query "mml-smime")
49 (autoload 'mml-smime-verify "mml-smime")
50 (autoload 'mml-smime-verify-test "mml-smime")
52 (defvar mml-sign-alist
53 '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
54 ("pgp" mml-pgp-sign-buffer list)
55 ("pgpauto" mml-pgpauto-sign-buffer list)
56 ("pgpmime" mml-pgpmime-sign-buffer list))
57 "Alist of MIME signer functions.")
59 (defcustom mml-default-sign-method "pgpmime"
61 The string must have an entry in `mml-sign-alist'."
63 :type '(choice (const "smime")
70 (defvar mml-encrypt-alist
71 '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
72 ("pgp" mml-pgp-encrypt-buffer list)
73 ("pgpauto" mml-pgpauto-sign-buffer list)
74 ("pgpmime" mml-pgpmime-encrypt-buffer list))
75 "Alist of MIME encryption functions.")
77 (defcustom mml-default-encrypt-method "pgpmime"
78 "Default encryption method.
79 The string must have an entry in `mml-encrypt-alist'."
81 :type '(choice (const "smime")
88 (defcustom mml-signencrypt-style-alist
93 "Alist specifying if `signencrypt' results in two separate operations or not.
94 The first entry indicates the MML security type, valid entries include
95 the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
96 a symbol `separate' or `combined' where `separate' means that MML signs
97 and encrypt messages in a two step process, and `combined' means that MML
98 signs and encrypt the message in one step.
100 Note that the output generated by using a `combined' mode is NOT
101 understood by all PGP implementations, in particular PGP version
102 2 does not support it! See Info node `(message)Security' for
106 :type '(repeat (list (choice (const :tag "S/MIME" "smime")
107 (const :tag "PGP" "pgp")
108 (const :tag "PGP/MIME" "pgpmime")
109 (string :tag "User defined"))
110 (choice (const :tag "Separate" separate)
111 (const :tag "Combined" combined)))))
113 (defcustom mml-secure-verbose nil
114 "If non-nil, ask the user about the current operation more verbosely."
118 (defcustom mml-secure-cache-passphrase password-cache
119 "If t, cache passphrase."
123 (defcustom mml-secure-passphrase-cache-expiry password-cache-expiry
124 "How many seconds the passphrase is cached.
125 Whether the passphrase is cached at all is controlled by
126 `mml-secure-cache-passphrase'."
130 ;;; Configuration/helper functions
132 (defun mml-signencrypt-style (method &optional style)
133 "Function for setting/getting the signencrypt-style used. Takes two
134 arguments, the method (e.g. \"pgp\") and optionally the mode
135 \(e.g. combined). If the mode is omitted, the current value is returned.
137 For example, if you prefer to use combined sign & encrypt with
138 smime, putting the following in your Gnus startup file will
139 enable that behavior:
141 \(mml-set-signencrypt-style \"smime\" combined)
143 You can also customize or set `mml-signencrypt-style-alist' instead."
144 (let ((style-item (assoc method mml-signencrypt-style-alist)))
146 (if (or (eq style 'separate)
147 (eq style 'combined))
148 ;; valid style setting?
149 (setf (second style-item) style)
150 ;; otherwise, just return the current value
152 (message "Warning, attempt to set invalid signencrypt style"))))
154 ;;; Security functions
156 (defun mml-smime-sign-buffer (cont)
157 (or (mml-smime-sign cont)
158 (error "Signing failed... inspect message logs for errors")))
160 (defun mml-smime-encrypt-buffer (cont &optional sign)
162 (message "Combined sign and encrypt S/MIME not support yet")
164 (or (mml-smime-encrypt cont)
165 (error "Encryption failed... inspect message logs for errors")))
167 (defun mml-pgp-sign-buffer (cont)
168 (or (mml1991-sign cont)
169 (error "Signing failed... inspect message logs for errors")))
171 (defun mml-pgp-encrypt-buffer (cont &optional sign)
172 (or (mml1991-encrypt cont sign)
173 (error "Encryption failed... inspect message logs for errors")))
175 (defun mml-pgpmime-sign-buffer (cont)
176 (or (mml2015-sign cont)
177 (error "Signing failed... inspect message logs for errors")))
179 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
180 (or (mml2015-encrypt cont sign)
181 (error "Encryption failed... inspect message logs for errors")))
183 (defun mml-pgpauto-sign-buffer (cont)
185 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
188 (error "Encryption failed... inspect message logs for errors")))
190 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
192 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
193 (mml2015-encrypt cont sign)
194 (mml1991-encrypt cont sign))
195 (error "Encryption failed... inspect message logs for errors")))
197 (defun mml-secure-part (method &optional sign)
199 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
200 mml-encrypt-alist))))))
201 (cond ((re-search-backward
202 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
203 (goto-char (match-end 0))
204 (insert (if sign " sign=" " encrypt=") method)
206 (let ((key (pop tags))
209 ;; Quote VALUE if it contains suspicious characters.
210 (when (string-match "[\"'\\~/*;() \t\n]" value)
211 (setq value (prin1-to-string value)))
212 (insert (format " %s=%s" key value))))))
213 ((or (re-search-backward
214 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
216 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
217 (goto-char (match-end 0))
218 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
219 (cons method tags))))
220 (t (error "The message is corrupted. No mail header separator"))))))
222 (defvar mml-secure-method
223 (if (equal mml-default-encrypt-method mml-default-sign-method)
224 mml-default-sign-method
226 "Current security method. Internal variable.")
228 (defun mml-secure-sign (&optional method)
229 "Add MML tags to sign this MML part.
230 Use METHOD if given. Else use `mml-secure-method' or
231 `mml-default-sign-method'."
234 (or method mml-secure-method mml-default-sign-method)
237 (defun mml-secure-encrypt (&optional method)
238 "Add MML tags to encrypt this MML part.
239 Use METHOD if given. Else use `mml-secure-method' or
240 `mml-default-sign-method'."
243 (or method mml-secure-method mml-default-sign-method)))
245 (defun mml-secure-sign-pgp ()
246 "Add MML tags to PGP sign this MML part."
248 (mml-secure-part "pgp" 'sign))
250 (defun mml-secure-sign-pgpauto ()
251 "Add MML tags to PGP-auto sign this MML part."
253 (mml-secure-part "pgpauto" 'sign))
255 (defun mml-secure-sign-pgpmime ()
256 "Add MML tags to PGP/MIME sign this MML part."
258 (mml-secure-part "pgpmime" 'sign))
260 (defun mml-secure-sign-smime ()
261 "Add MML tags to S/MIME sign this MML part."
263 (mml-secure-part "smime" 'sign))
265 (defun mml-secure-encrypt-pgp ()
266 "Add MML tags to PGP encrypt this MML part."
268 (mml-secure-part "pgp"))
270 (defun mml-secure-encrypt-pgpmime ()
271 "Add MML tags to PGP/MIME encrypt this MML part."
273 (mml-secure-part "pgpmime"))
275 (defun mml-secure-encrypt-smime ()
276 "Add MML tags to S/MIME encrypt this MML part."
278 (mml-secure-part "smime"))
280 ;; defuns that add the proper <#secure ...> tag to the top of the message body
281 (defun mml-secure-message (method &optional modesym)
282 (let ((mode (prin1-to-string modesym))
284 (if (or (eq modesym 'sign)
285 (eq modesym 'signencrypt))
286 (funcall (nth 2 (assoc method mml-sign-alist))))
287 (if (or (eq modesym 'encrypt)
288 (eq modesym 'signencrypt))
289 (funcall (nth 2 (assoc method mml-encrypt-alist))))))
291 (mml-unsecure-message)
293 (goto-char (point-min))
294 (cond ((re-search-forward
295 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
296 (goto-char (setq insert-loc (match-end 0)))
297 (unless (looking-at "<#secure")
298 (apply 'mml-insert-tag
299 'secure 'method method 'mode mode tags)))
301 "The message is corrupted. No mail header separator"))))
302 (when (eql insert-loc (point))
305 (defun mml-unsecure-message ()
306 "Remove security related MML tags from message."
309 (goto-char (point-max))
310 (when (re-search-backward "^<#secure.*>\n" nil t)
311 (delete-region (match-beginning 0) (match-end 0)))))
314 (defun mml-secure-message-sign (&optional method)
315 "Add MML tags to sign this MML part.
316 Use METHOD if given. Else use `mml-secure-method' or
317 `mml-default-sign-method'."
320 (or method mml-secure-method mml-default-sign-method)
323 (defun mml-secure-message-sign-encrypt (&optional method)
324 "Add MML tag to sign and encrypt the entire message.
325 Use METHOD if given. Else use `mml-secure-method' or
326 `mml-default-sign-method'."
329 (or method mml-secure-method mml-default-sign-method)
332 (defun mml-secure-message-encrypt (&optional method)
333 "Add MML tag to encrypt the entire message.
334 Use METHOD if given. Else use `mml-secure-method' or
335 `mml-default-sign-method'."
338 (or method mml-secure-method mml-default-sign-method)
341 (defun mml-secure-message-sign-smime ()
342 "Add MML tag to encrypt/sign the entire message."
344 (mml-secure-message "smime" 'sign))
346 (defun mml-secure-message-sign-pgp ()
347 "Add MML tag to encrypt/sign the entire message."
349 (mml-secure-message "pgp" 'sign))
351 (defun mml-secure-message-sign-pgpmime ()
352 "Add MML tag to encrypt/sign the entire message."
354 (mml-secure-message "pgpmime" 'sign))
356 (defun mml-secure-message-sign-pgpauto ()
357 "Add MML tag to encrypt/sign the entire message."
359 (mml-secure-message "pgpauto" 'sign))
361 (defun mml-secure-message-encrypt-smime (&optional dontsign)
362 "Add MML tag to encrypt and sign the entire message.
363 If called with a prefix argument, only encrypt (do NOT sign)."
365 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
367 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
368 "Add MML tag to encrypt and sign the entire message.
369 If called with a prefix argument, only encrypt (do NOT sign)."
371 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
373 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
374 "Add MML tag to encrypt and sign the entire message.
375 If called with a prefix argument, only encrypt (do NOT sign)."
377 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
379 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
380 "Add MML tag to encrypt and sign the entire message.
381 If called with a prefix argument, only encrypt (do NOT sign)."
383 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
387 ;;; arch-tag: 111c56e7-df5e-4287-87d7-93ed2911ec6c
388 ;;; mml-sec.el ends here