(gnus-replace-in-string): Revert bogus sync from v5-10.
[gnus] / lisp / mml-smime.el
1 ;;; mml-smime.el --- S/MIME support for MML
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 ;; Keywords: Gnus, MIME, S/MIME, MML
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 2, or (at your
14 ;; option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'smime)
33 (require 'mm-decode)
34 (autoload 'message-narrow-to-headers "message")
35 (autoload 'message-fetch-field "message")
36
37 (defun mml-smime-sign (cont)
38   (when (null smime-keys)
39     (customize-variable 'smime-keys)
40     (error "No S/MIME keys configured, use customize to add your key"))
41   (smime-sign-buffer (cdr (assq 'keyfile cont)))
42   (goto-char (point-min))
43   (while (search-forward "\r\n" nil t)
44     (replace-match "\n" t t))
45   (goto-char (point-max)))
46
47 (defun mml-smime-encrypt (cont)
48   (let (certnames certfiles tmp file tmpfiles)
49     ;; xxx tmp files are always an security issue
50     (while (setq tmp (pop cont))
51       (if (and (consp tmp) (eq (car tmp) 'certfile))
52           (push (cdr tmp) certnames)))
53     (while (setq tmp (pop certnames))
54       (if (not (and (not (file-exists-p tmp))
55                     (get-buffer tmp)))
56           (push tmp certfiles)
57         (setq file (mm-make-temp-file (expand-file-name "mml."
58                                                         mm-tmp-directory)))
59         (with-current-buffer tmp
60           (write-region (point-min) (point-max) file))
61         (push file certfiles)
62         (push file tmpfiles)))
63     (if (smime-encrypt-buffer certfiles)
64         (progn
65           (while (setq tmp (pop tmpfiles))
66             (delete-file tmp))
67           t)
68       (while (setq tmp (pop tmpfiles))
69         (delete-file tmp))
70       nil))
71   (goto-char (point-max)))
72
73 (defun mml-smime-sign-query ()
74   ;; query information (what certificate) from user when MML tag is
75   ;; added, for use later by the signing process
76   (when (null smime-keys)
77     (customize-variable 'smime-keys)
78     (error "No S/MIME keys configured, use customize to add your key"))
79   (list 'keyfile
80         (if (= (length smime-keys) 1)
81             (cadar smime-keys)
82           (or (let ((from (cadr (funcall (if (boundp
83                                               'gnus-extract-address-components)
84                                              gnus-extract-address-components
85                                            'mail-extract-address-components)
86                                          (or (save-excursion
87                                                (save-restriction
88                                                  (message-narrow-to-headers)
89                                                  (message-fetch-field "from")))
90                                              "")))))
91                 (and from (smime-get-key-by-email from)))
92               (smime-get-key-by-email
93                (completing-read "Sign this part with what signature? "
94                                 smime-keys nil nil
95                                 (and (listp (car-safe smime-keys))
96                                      (caar smime-keys))))))))
97
98 (defun mml-smime-get-file-cert ()
99   (ignore-errors
100     (list 'certfile (read-file-name
101                      "File with recipient's S/MIME certificate: "
102                      smime-certificate-directory nil t ""))))
103
104 (defun mml-smime-get-dns-cert ()
105   ;; todo: deal with comma separated multiple recipients
106   (let (result who bad cert)
107     (condition-case ()
108         (while (not result)
109           (setq who (read-from-minibuffer
110                      (format "%sLookup certificate for: " (or bad ""))
111                      (cadr (funcall (if (boundp
112                                          'gnus-extract-address-components)
113                                         gnus-extract-address-components
114                                       'mail-extract-address-components)
115                                     (or (save-excursion
116                                           (save-restriction
117                                             (message-narrow-to-headers)
118                                             (message-fetch-field "to")))
119                                         "")))))
120           (if (setq cert (smime-cert-by-dns who))
121               (setq result (list 'certfile (buffer-name cert)))
122             (setq bad (format "`%s' not found. " who))))
123       (quit))
124     result))
125
126 (defun mml-smime-get-ldap-cert ()
127   ;; todo: deal with comma separated multiple recipients
128   (let (result who bad cert)
129     (condition-case ()
130         (while (not result)
131           (setq who (read-from-minibuffer
132                      (format "%sLookup certificate for: " (or bad ""))
133                      (cadr (funcall gnus-extract-address-components
134                                     (or (save-excursion
135                                           (save-restriction
136                                             (message-narrow-to-headers)
137                                             (message-fetch-field "to")))
138                                         "")))))
139           (if (setq cert (smime-cert-by-ldap who))
140               (setq result (list 'certfile (buffer-name cert)))
141             (setq bad (format "`%s' not found. " who))))
142       (quit))
143     result))
144
145 (defun mml-smime-encrypt-query ()
146   ;; todo: try dns/ldap automatically first, before prompting user
147   (let (certs done)
148     (while (not done)
149       (ecase (read (gnus-completing-read-with-default
150                     "ldap" "Fetch certificate from"
151                     '(("dns") ("ldap") ("file")) nil t))
152         (dns (setq certs (append certs
153                                  (mml-smime-get-dns-cert))))
154         (ldap (setq certs (append certs
155