(mml2015-epg-sign): Save the signing keys in
[gnus] / lisp / mml-sec.el
1 ;;; mml-sec.el --- A package with security functions for MML documents
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7
8 ;; This file is part of GNU Emacs.
9
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 2, or (at your option)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'mml-smime)
30 (eval-when-compile (require 'cl))
31 (require 'password)
32 (autoload 'mml2015-sign "mml2015")
33 (autoload 'mml2015-encrypt "mml2015")
34 (autoload 'mml1991-sign "mml1991")
35 (autoload 'mml1991-encrypt "mml1991")
36 (autoload 'message-goto-body "message")
37 (autoload 'mml-insert-tag "mml")
38
39 (defvar mml-sign-alist
40   '(("smime"     mml-smime-sign-buffer     mml-smime-sign-query)
41     ("pgp"       mml-pgp-sign-buffer       list)
42     ("pgpauto"   mml-pgpauto-sign-buffer  list)
43     ("pgpmime"   mml-pgpmime-sign-buffer   list))
44   "Alist of MIME signer functions.")
45
46 (defcustom mml-default-sign-method "pgpmime"
47   "Default sign method.
48 The string must have an entry in `mml-sign-alist'."
49   :version "22.1"
50   :type '(choice (const "smime")
51                  (const "pgp")
52                  (const "pgpauto")
53                  (const "pgpmime")
54                  string)
55   :group 'message)
56
57 (defvar mml-encrypt-alist
58   '(("smime"     mml-smime-encrypt-buffer     mml-smime-encrypt-query)
59     ("pgp"       mml-pgp-encrypt-buffer       list)
60     ("pgpauto"   mml-pgpauto-sign-buffer  list)
61     ("pgpmime"   mml-pgpmime-encrypt-buffer   list))
62   "Alist of MIME encryption functions.")
63
64 (defcustom mml-default-encrypt-method "pgpmime"
65   "Default encryption method.
66 The string must have an entry in `mml-encrypt-alist'."
67   :version "22.1"
68   :type '(choice (const "smime")
69                  (const "pgp")
70                  (const "pgpauto")
71                  (const "pgpmime")
72                  string)
73   :group 'message)
74
75 (defcustom mml-signencrypt-style-alist
76   '(("smime"   separate)
77     ("pgp"     combined)
78     ("pgpauto" combined)
79     ("pgpmime" combined))
80   "Alist specifying if `signencrypt' results in two separate operations or not.
81 The first entry indicates the MML security type, valid entries include
82 the strings \"smime\", \"pgp\", and \"pgpmime\".  The second entry is
83 a symbol `separate' or `combined' where `separate' means that MML signs
84 and encrypt messages in a two step process, and `combined' means that MML
85 signs and encrypt the message in one step.
86
87 Note that the output generated by using a `combined' mode is NOT
88 understood by all PGP implementations, in particular PGP version
89 2 does not support it!  See Info node `(message)Security' for
90 details."
91   :version "22.1"
92   :group 'message
93   :type '(repeat (list (choice (const :tag "S/MIME" "smime")
94                                (const :tag "PGP" "pgp")
95                                (const :tag "PGP/MIME" "pgpmime")
96                                (string :tag "User defined"))
97                        (choice (const :tag "Separate" separate)
98                                (const :tag "Combined" combined)))))
99
100 (defcustom mml-secure-verbose nil
101   "If non-nil, ask the user about the current operation more verbosely."
102   :group 'message
103   :type 'boolean)
104
105 (defcustom mml-secure-cache-passphrase password-cache
106   "If t, cache passphrase."
107   :group 'message
108   :type 'boolean)
109
110 (defcustom mml-secure-passphrase-cache-expiry password-cache-expiry
111   "How many seconds the passphrase is cached.
112 Whether the passphrase is cached at all is controlled by
113 `mml-secure-cache-passphrase'."
114   :group 'message
115   :type 'integer)
116
117 ;;; Configuration/helper functions
118
119 (defun mml-signencrypt-style (method &optional style)
120   "Function for setting/getting the signencrypt-style used.  Takes two
121 arguments, the method (e.g. \"pgp\") and optionally the mode
122 \(e.g. combined).  If the mode is omitted, the current value is returned.
123
124 For example, if you prefer to use combined sign & encrypt with
125 smime, putting the following in your Gnus startup file will
126 enable that behavior:
127
128 \(mml-set-signencrypt-style \"smime\" combined)
129
130 You can also customize or set `mml-signencrypt-style-alist' instead."
131   (let ((style-item (assoc method mml-signencrypt-style-alist)))
132     (if style-item
133         (if (or (eq style 'separate)
134                 (eq style 'combined))
135             ;; valid style setting?
136             (setf (second style-item) style)
137           ;; otherwise, just return the current value
138           (second style-item))
139       (message "Warning, attempt to set invalid signencrypt style"))))
140
141 ;;; Security functions
142
143 (defun mml-smime-sign-buffer (cont)
144   (or (mml-smime-sign cont)
145       (error "Signing failed... inspect message logs for errors")))
146
147 (defun mml-smime-encrypt-buffer (cont &optional sign)
148   (when sign
149     (message "Combined sign and encrypt S/MIME not support yet")
150     (sit-for 1))
151   (or (mml-smime-encrypt cont)
152       (error "Encryption failed... inspect message logs for errors")))
153
154 (defun mml-pgp-sign-buffer (cont)
155   (or (mml1991-sign cont)
156       (error "Signing failed... inspect message logs for errors")))
157
158 (defun mml-pgp-encrypt-buffer (cont &optional sign)
159   (or (mml1991-encrypt cont sign)
160       (error "Encryption failed... inspect message logs for errors")))
161
162 (defun mml-pgpmime-sign-buffer (cont)
163   (or (mml2015-sign cont)
164       (error "Signing failed... inspect message logs for errors")))
165
166 (defun mml-pgpmime-encrypt-buffer (cont &optional sign)
167   (or (mml2015-encrypt cont sign)
168       (error "Encryption failed... inspect message logs for errors")))
169
170 (defun mml-pgpauto-sign-buffer (cont)
171   (message-goto-body)
172   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
173           (mml2015-sign cont)
174         (mml1991-sign cont))
175       (error "Encryption failed... inspect message logs for errors")))
176
177 (defun mml-pgpauto-encrypt-buffer (cont &optional sign)
178   (message-goto-body)
179   (or (if (re-search-backward "Content-Type: *multipart/.*" nil t) ; there must be a better way...
180           (mml2015-encrypt cont sign)
181         (mml1991-encrypt cont sign))
182       (error "Encryption failed... inspect message logs for errors")))
183
184 (defun mml-secure-part (method &optional sign)
185   (save-excursion
186     (let ((tags (funcall (nth 2 (assoc method (if sign mml-sign-alist
187                                                 mml-encrypt-alist))))))
188       (cond ((re-search-backward
189               "<#\\(multipart\\|part\\|external\\|mml\\)" nil t)
190              (goto-char (match-end 0))
191              (insert (if sign " sign=" " encrypt=") method)
192              (while tags
193                (let ((key (pop tags))
194                      (value (pop tags)))
195                  (when value
196                    ;; Quote VALUE if it contains suspicious characters.
197                    (when (string-match "[\"'\\~/*;() \t\n]" value)
198                      (setq value (prin1-to-string value)))
199                    (insert (format " %s=%s" key value))))))
200             ((or (re-search-backward
201                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
202                  (re-search-forward
203                   (concat "^" (regexp-quote mail-header-separator) "\n") nil t))
204              (goto-char (match-end 0))
205              (apply 'mml-insert-tag 'part (cons (if sign 'sign 'encrypt)
206                                                 (cons method tags))))
207             (t (error "The message is corrupted. No mail header separator"))))))
208
209 (defvar mml-secure-method
210   (if (equal mml-default-encrypt-method mml-default-sign-method)
211       mml-default-sign-method
212     "pgpmime")
213   "Current security method.  Internal variable.")
214
215 (defun mml-secure-sign (&optional method)
216   "Add MML tags to sign this MML part.
217 Use METHOD if given.  Else use `mml-secure-method' or
218 `mml-default-sign-method'."
219   (interactive)
220   (mml-secure-part
221    (or method mml-secure-method mml-default-sign-method)
222    'sign))
223
224 (defun mml-secure-encrypt (&optional method)
225   "Add MML tags to encrypt this MML part.
226 Use METHOD if given.  Else use `mml-secure-method' or
227 `mml-default-sign-method'."
228   (interactive)
229   (mml-secure-part
230    (or method mml-secure-method mml-default-sign-method)))
231
232 (defun mml-secure-sign-pgp ()
233   "Add MML tags to PGP sign this MML part."
234   (interactive)
235   (mml-secure-part "pgp" 'sign))
236
237 (defun mml-secure-sign-pgpauto ()
238   "Add MML tags to PGP-auto sign this MML part."
239   (interactive)
240   (mml-secure-part "pgpauto" 'sign))
241
242 (defun mml-secure-sign-pgpmime ()
243   "Add MML tags to PGP/MIME sign this MML part."
244   (interactive)
245   (mml-secure-part "pgpmime" 'sign))
246
247 (defun mml-secure-sign-smime ()
248   "Add MML tags to S/MIME sign this MML part."
249   (interactive)
250   (mml-secure-part "smime" 'sign))
251
252 (defun mml-secure-encrypt-pgp ()
253   "Add MML tags to PGP encrypt this MML part."
254   (interactive)
255   (mml-secure-part "pgp"))
256
257 (defun mml-secure-encrypt-pgpmime ()
258   "Add MML tags to PGP/MIME encrypt this MML part."
259   (interactive)
260   (mml-secure-part "pgpmime"))
261
262 (defun mml-secure-encrypt-smime ()
263   "Add MML tags to S/MIME encrypt this MML part."
264   (interactive)
265   (mml-secure-part "smime"))
266
267 ;; defuns that add the proper <#secure ...> tag to the top of the message body
268 (defun mml-secure-message (method &optional modesym)
269   (let ((mode (prin1-to-string modesym))
270         (tags (append
271                (if (or (eq modesym 'sign)
272                        (eq modesym 'signencrypt))
273                    (funcall (nth 2 (assoc method mml-sign-alist))))
274                (if (or (eq modesym 'encrypt)
275                        (eq modesym 'signencrypt))
276                    (funcall (nth 2 (assoc method mml-encrypt-alist))))))
277         insert-loc)
278     (mml-unsecure-message)
279     (save-excursion
280       (goto-char (point-min))
281       (cond ((re-search-forward
282               (concat "^" (regexp-quote mail-header-separator) "\n") nil t)
283              (goto-char (setq insert-loc (match-end 0)))
284              (unless (looking-at "<#secure")
285                (apply 'mml-insert-tag
286                 'secure 'method method 'mode mode tags)))
287             (t (error
288                 "The message is corrupted. No mail header separator"))))
289     (when (eql insert-loc (point))
290       (forward-line 1))))
291
292 (defun mml-unsecure-message ()
293   "Remove security related MML tags from message."
294   (interactive)
295   (save-excursion
296     (goto-char (point-max))
297     (when (re-search-backward "^<#secure.*>\n" nil t)
298       (delete-region (match-beginning 0) (match-end 0)))))
299
300
301 (defun mml-secure-message-sign (&optional method)
302   "Add MML tags to sign this MML part.
303 Use METHOD if given. Else use `mml-secure-method' or
304 `mml-default-sign-method'."
305   (interactive)
306   (mml-secure-part
307    (or method mml-secure-method mml-default-sign-method)
308    'sign))
309
310 (defun mml-secure-message-sign-encrypt (&optional method)
311   "Add MML tag to sign and encrypt the entire message.
312 Use METHOD if given. Else use `mml-secure-method' or
313 `mml-default-sign-method'."
314   (interactive)
315   (mml-secure-message
316    (or method mml-secure-method mml-default-sign-method)
317    'signencrypt))
318
319 (defun mml-secure-message-encrypt (&optional method)
320   "Add MML tag to encrypt the entire message.
321 Use METHOD if given. Else use `mml-secure-method' or
322 `mml-default-sign-method'."
323   (interactive)
324   (mml-secure-message
325    (or method mml-secure-method mml-default-sign-method)
326    'encrypt))
327
328 (defun mml-secure-message-sign-smime ()
329   "Add MML tag to encrypt/sign the entire message."
330   (interactive)
331   (mml-secure-message "smime" 'sign))
332
333 (defun mml-secure-message-sign-pgp ()
334   "Add MML tag to encrypt/sign the entire message."
335   (interactive)
336   (mml-secure-message "pgp" 'sign))
337
338 (defun mml-secure-message-sign-pgpmime ()
339   "Add MML tag to encrypt/sign the entire message."
340   (interactive)
341   (mml-secure-message "pgpmime" 'sign))
342
343 (defun mml-secure-message-sign-pgpauto ()
344   "Add MML tag to encrypt/sign the entire message."
345   (interactive)
346   (mml-secure-message "pgpauto" 'sign))
347
348 (defun mml-secure-message-encrypt-smime (&optional dontsign)
349   "Add MML tag to encrypt and sign the entire message.
350 If called with a prefix argument, only encrypt (do NOT sign)."
351   (interactive "P")
352   (mml-secure-message "smime" (if dontsign 'encrypt 'signencrypt)))
353
354 (defun mml-secure-message-encrypt-pgp (&optional dontsign)
355   "Add MML tag to encrypt and sign the entire message.
356 If called with a prefix argument, only encrypt (do NOT sign)."
357   (interactive "P")
358   (mml-secure-message "pgp" (if dontsign 'encrypt 'signencrypt)))
359
360 (defun mml-secure-message-encrypt-pgpmime (&optional dontsign)
361   "Add MML tag to encrypt and sign the entire message.
362 If called with a prefix argument, only encrypt (do NOT sign)."
363   (interactive "P")
364   (mml-secure-message "pgpmime" (if dontsign 'encrypt 'signencrypt)))
365
366 (defun mml-secure-message-encrypt-pgpauto (&optional dontsign)
367   "Add MML tag to encrypt and sign the entire message.
368 If called with a prefix argument, only encrypt (do NOT sign)."
369   (interactive "P")
370   (mml-secure-message "pgpauto" (if dontsign 'encrypt 'signencrypt)))
371
372 (provide 'mml-sec)
373
374 ;;; arch-tag: 111c56e7-df5e-4287-87d7-93ed2911ec6c
375 ;;; mml-sec.el ends here