1 ;;; mml-sec.el --- A package with security functions for MML documents
2 ;; Copyright (C) 2000, 2001, 2002, 2003 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., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
30 (eval-when-compile (require 'cl))
32 (defvar mml-sign-alist
33 '(("smime" mml-smime-sign-buffer mml-smime-sign-query)
34 ("pgp" mml-pgp-sign-buffer list)
35 ("pgpauto" mml-pgpauto-sign-buffer list)
36 ("pgpmime" mml-pgpmime-sign-buffer list))
37 "Alist of MIME signer functions.")
39 (defvar mml-default-sign-method (caar mml-sign-alist)
40 "Default sign method.")
42 (defvar mml-encrypt-alist
43 '(("smime" mml-smime-encrypt-buffer mml-smime-encrypt-query)
44 ("pgp" mml-pgp-encrypt-buffer list)
45 ("pgpauto" mml-pgpauto-sign-buffer list)
46 ("pgpmime" mml-pgpmime-encrypt-buffer list))
47 "Alist of MIME encryption functions.")
49 (defvar mml-default-encrypt-method (caar mml-encrypt-alist)
50 "Default encryption method.")
52 (defcustom mml-signencrypt-style-alist
57 "Alist specifying if `signencrypt' results in two separate operations or not.
58 The first entry indicates the MML security type, valid entries include
59 the strings \"smime\", \"pgp\", and \"pgpmime\". The second entry is
60 a symbol `separate' or `combined' where `separate' means that MML signs
61 and encrypt messages in a two step process, and `combined' means that MML
62 signs and encrypt the message in one step.
63 Note that the `combined' mode is NOT supported by all OpenPGP implementations,
64 in particular PGP version 2 does not support it!"
65 :type '(repeat (list (choice (const :tag "S/MIME" "smime")
66 (const :tag "PGP" "pgp")
67 (const :tag "PGP/MIME" "pgpmime")
68 (string :tag "User defined"))
69 (choice (const :tag "Separate" separate)
70 (const :tag "Combined" combined)))))
72 ;;; Configuration/helper functions
74 (defun mml-signencrypt-style (method &optional style)
75 "Function for setting/getting the signencrypt-style used. Takes two
76 arguments, the method (e.g. \"pgp\") and optionally the mode
77 \(e.g. combined). If the mode is omitted, the current value is returned.
79 For example, if you prefer to use combined sign & encrypt with
80 smime, putting the following in your Gnus startup file will
83 \(mml-set-signencrypt-style \"smime\" combined)
85 You can also customize or set `mml-signencrypt-style-alist' instead."
86 (let ((style-item (assoc method mml-signencrypt-style-alist)))
88 (if (or (eq style 'separate)
90 ;; valid style setting?
91 (setf (second style-item) style)
92 ;; otherwise, just return the current value
94 (gnus-message 3 "Warning, attempt to set invalid signencrypt-style"))))
96 ;;; Security functions
98 (defun mml-smime-sign-buffer (cont)
99 (or (mml-smime-sign cont)
100 (error "Signing failed... inspect message logs for errors")))
102 (defun mml-smime-encrypt-buffer (cont &optional sign)
104 (message "Combined sign and encrypt S/MIME not support yet")
106 (or (mml-smime-encrypt cont)
107 (error "Encryption failed... inspect message logs for errors")))
109 (defun mml-pgp-sign-buffer (cont)
110 (or (mml1991-sign cont)
111 (error "Signing failed... inspect message logs for errors")))
113 (defun mml-pgp-encrypt-buffer (cont &optional sign)
114 (or (mml1991-encrypt cont sign)
115 (error "Encryption failed... inspect message logs for errors")))
117 (defun mml-pgpmime-sign-buffer (cont)
118 (or (mml2015-sign cont)
119 (error "Signing failed... inspect message logs for errors")))
121 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
122 (or (mml2015-encrypt cont sign)
123 (error "Encryption failed... inspect message logs for errors")))
125 (defun mml-pgpauto-sign-buffer (cont)
127 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
130 (error "Encryption failed... inspect message logs for errors")))
132 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
134 (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
135 (mml2015-encrypt cont sign)
136 (mml1991-encrypt cont sign))
137 (error "Encryption failed... inspect message logs for errors")))
139 (defun mml-secure-part (method &optional sign)
141 (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
142 mml-encrypt-alist))))))
143 (cond ((re-search-backward
144 "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
145 (goto-char (match-end 0))
146 (insert (if sign " sign=" " encrypt=") method)
148 (let ((key (pop tags))
151 ;; Quote VALUE if it contains suspicious characters.
152 (when (string-match "[\"'\\~/*;() \t\n]" value)
153 (setq value (prin1-to-string value)))
154 (insert (format " %s=%s" key value))))))
155 ((or (re-search-backward
156 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
158 (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
159 (goto-char (match-end 0))
160 (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
161 (cons method tags))))
162 (t (error "The message is corrupted. No mail header separator"))))))
164 (defun mml-secure-sign-pgp ()
165 "Add MML tags to PGP sign this MML part."
167 (mml-secure-part "pgp" 'sign))
169 (defun mml-secure-sign-pgpauto ()
170 "Add MML tags to PGP-auto sign this MML part."
172 (mml-secure-part "pgpauto" 'sign))
174 (defun mml-secure-sign-pgpmime ()
175 "Add MML tags to PGP/MIME sign this MML part."
177 (mml-secure-part "pgpmime" 'sign))
179 (defun mml-secure-sign-smime ()
180 "Add MML tags to S/MIME sign this MML part."
182 (mml-secure-part "smime" 'sign))
184 (defun mml-secure-encrypt-pgp ()
185 "Add MML tags to PGP encrypt this MML part."
187 (mml-secure-part "pgp"))
189 (defun mml-secure-encrypt-pgpmime ()
190 "Add MML tags to PGP/MIME encrypt this MML part."
192 (mml-secure-part "pgpmime"))
194 (defun mml-secure-encrypt-smime ()
195 "Add MML tags to S/MIME encrypt this MML part."
197 (mml-secure-part "smime"))
199 ;; defuns that add the proper <#secure ...> tag to the top of the message body
200 (defun mml-secure-message (method &optional modesym)
201 (let ((mode (prin1-to-string modesym))
203 (mml-unsecure-message)
205 (goto-char (point-min))
206 (cond ((re-search-forward
207 (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
208 (goto-char (setq insert-loc (match-end 0)))
209 (unless (looking-at "<#secure")
211 'secure 'method method 'mode mode)))
213 "The message is corrupted. No mail header separator"))))
214 (when (eql insert-loc (point))
217 (defun mml-unsecure-message ()
218 "Remove security related MML tags from message."
221 (goto-char (point-max))
222 (when (re-search-backward "^<#secure.*>\n" nil t)
223 (delete-region (match-beginning 0) (match-end 0)))))
225 (defun mml-secure-message-sign-smime ()
226 "Add MML tag to encrypt/sign the entire message."
228 (mml-secure-message "smime" 'sign))
230 (defun mml-secure-message-sign-pgp ()
231 "Add MML tag to encrypt/sign the entire message."
233 (mml-secure-message "pgp" 'sign))
235 (defun mml-secure-message-sign-pgpmime ()
236 "Add MML tag to encrypt/sign the entire message."
238 (mml-secure-message "pgpmime" 'sign))
240 (defun mml-secure-message-sign-pgpauto ()
241 "Add MML tag to encrypt/sign the entire message."
243 (mml-secure-message "pgpauto" 'sign))
245 (defun mml-secure-message-encrypt-smime (&optional dontsign)
246 "Add MML tag to encrypt and sign the entire message.
247 If called with a prefix argument, only encrypt (do NOT sign)."
249 (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
251 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
252 "Add MML tag to encrypt and sign the entire message.
253 If called with a prefix argument, only encrypt (do NOT sign)."
255 (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
257 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
258 "Add MML tag to encrypt and sign the entire message.
259 If called with a prefix argument, only encrypt (do NOT sign)."
261 (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
263 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
264 "Add MML tag to encrypt and sign the entire message.
265 If called with a prefix argument, only encrypt (do NOT sign)."
267 (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
271 ;;; mml-sec.el ends here