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