Revision: miles@gnu.org--gnu-2005/gnus--devo--0--patch-195
[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 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                                   (mml-smime-get-ldap-cert))))
156         (file (setq certs (append certs
157                                   (mml-smime-get-file-cert)))))
158       (setq done (not (y-or-n-p "Add more recipients? "))))
159     certs))
160
161 (defun mml-smime-verify (handle ctl)
162   (with-temp-buffer
163     (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
164     (goto-char (point-min))
165     (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
166     (insert (format "protocol=\"%s\"; "
167                     (mm-handle-multipart-ctl-parameter ctl 'protocol)))
168     (insert (format "micalg=\"%s\"; "
169                     (mm-handle-multipart-ctl-parameter ctl 'micalg)))
170     (insert (format "boundary=\"%s\"\n\n"
171                     (mm-handle-multipart-ctl-parameter ctl 'boundary)))
172     (when (get-buffer smime-details-buffer)
173       (kill-buffer smime-details-buffer))
174     (let ((buf (current-buffer))
175           (good-signature (smime-noverify-buffer))
176           (good-certificate (and (or smime-CA-file smime-CA-directory)
177                                  (smime-verify-buffer)))
178           addresses openssl-output)
179       (setq openssl-output (with-current-buffer smime-details-buffer
180                              (buffer-string)))
181       (if (not good-signature)
182           (progn
183             ;; we couldn't verify message, fail with openssl output as message
184             (mm-set-handle-multipart-parameter
185              mm-security-handle 'gnus-info "Failed")
186             (mm-set-handle-multipart-parameter
187              mm-security-handle 'gnus-details
188              (concat "OpenSSL failed to verify message integrity:\n"
189                      "-------------------------------------------\n"
190                      openssl-output)))
191         ;; verify mail addresses in mail against those in certificate
192         (when (and (smime-pkcs7-region (point-min) (point-max))
193                    (smime-pkcs7-certificates-region (point-min) (point-max)))
194           (with-temp-buffer
195             (insert-buffer-substring buf)
196             (goto-char (point-min))
197             (while (re-search-forward "-----END CERTIFICATE-----" nil t)
198               (when (smime-pkcs7-email-region (point-min) (point))
199                 (setq addresses (append (smime-buffer-as-string-region
200                                          (point-min) (point)) addresses)))
201               (delete-region (point-min) (point)))
202             (setq addresses (mapcar 'downcase addresses))))
203         (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
204             (mm-set-handle-multipart-parameter
205              mm-security-handle 'gnus-info "Sender address forged")
206           (if good-certificate
207               (mm-set-handle-multipart-parameter
208                mm-security-handle 'gnus-info "Ok (sender authenticated)")
209             (mm-set-handle-multipart-parameter
210              mm-security-handle 'gnus-info "Ok (sender not trusted)")))
211         (mm-set-handle-multipart-parameter
212          mm-security-handle 'gnus-details
213          (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
214                  (if addresses
215                      (concat "Addresses in certificate: "
216                              (mapconcat 'identity addresses ", "))
217                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
218                  "\n" "\n"
219                  "OpenSSL output:\n"
220                  "---------------\n" openssl-output "\n"
221                  "Certificate(s) inside S/MIME signature:\n"
222                  "---------------------------------------\n"
223                  (buffer-string) "\n")))))
224   handle)
225
226 (defun mml-smime-verify-test (handle ctl)
227   smime-openssl-program)
228
229 (provide 'mml-smime)
230
231 ;;; arch-tag: f1bf94d4-f2cd-4c6f-b059-ad69492817e2
232 ;;; mml-smime.el ends here