Revision: miles@gnu.org--gnu-2005/gnus--devo--0--patch-182
[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 gnus-extract-address-components
83                                          (or (save-excursion
84                                                (save-restriction
85                                                  (message-narrow-to-headers)
86                                                  (message-fetch-field "from")))
87                                              "")))))
88                 (and from (smime-get-key-by-email from)))
89               (smime-get-key-by-email
90                (completing-read "Sign this part with what signature? "
91                                 smime-keys nil nil
92                                 (and (listp (car-safe smime-keys))
93                                      (caar smime-keys))))))))
94
95 (defun mml-smime-get-file-cert ()
96   (ignore-errors
97     (list 'certfile (read-file-name
98                      "File with recipient's S/MIME certificate: "
99                      smime-certificate-directory nil t ""))))
100
101 (defun mml-smime-get-dns-cert ()
102   ;; todo: deal with comma separated multiple recipients
103   (let (result who bad cert)
104     (condition-case ()
105         (while (not result)
106           (setq who (read-from-minibuffer
107                      (format "%sLookup certificate for: " (or bad ""))
108                      (cadr (funcall gnus-extract-address-components
109                                     (or (save-excursion
110                                           (save-restriction
111                                             (message-narrow-to-headers)
112                                             (message-fetch-field "to")))
113                                         "")))))
114           (if (setq cert (smime-cert-by-dns who))
115               (setq result (list 'certfile (buffer-name cert)))
116             (setq bad (format "`%s' not found. " who))))
117       (quit))
118     result))
119
120 (defun mml-smime-get-ldap-cert ()
121   ;; todo: deal with comma separated multiple recipients
122   (let (result who bad cert)
123     (condition-case ()
124         (while (not result)
125           (setq who (read-from-minibuffer
126                      (format "%sLookup certificate for: " (or bad ""))
127                      (cadr (funcall gnus-extract-address-components
128                                     (or (save-excursion
129                                           (save-restriction
130                                             (message-narrow-to-headers)
131                                             (message-fetch-field "to")))
132                                         "")))))
133           (if (setq cert (smime-cert-by-ldap who))
134               (setq result (list 'certfile (buffer-name cert)))
135             (setq bad (format "`%s' not found. " who))))
136       (quit))
137     result))
138
139 (defun mml-smime-encrypt-query ()
140   ;; todo: try dns/ldap automatically first, before prompting user
141   (let (certs done)
142     (while (not done)
143       (ecase (read (gnus-completing-read-with-default
144                     "ldap" "Fetch certificate from"
145                     '(("dns") ("ldap") ("file")) nil t))
146         (dns (setq certs (append certs
147                                  (mml-smime-get-dns-cert))))
148         (ldap (setq certs (append certs
149                                   (mml-smime-get-ldap-cert))))
150         (file (setq certs (append certs
151                                   (mml-smime-get-file-cert)))))
152       (setq done (not (y-or-n-p "Add more recipients? "))))
153     certs))
154
155 (defun mml-smime-verify (handle ctl)
156   (with-temp-buffer
157     (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
158     (goto-char (point-min))
159     (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
160     (insert (format "protocol=\"%s\"; "
161                     (mm-handle-multipart-ctl-parameter ctl 'protocol)))
162     (insert (format "micalg=\"%s\"; "
163                     (mm-handle-multipart-ctl-parameter ctl 'micalg)))
164     (insert (format "boundary=\"%s\"\n\n"
165                     (mm-handle-multipart-ctl-parameter ctl 'boundary)))
166     (when (get-buffer smime-details-buffer)
167       (kill-buffer smime-details-buffer))
168     (let ((buf (current-buffer))
169           (good-signature (smime-noverify-buffer))
170           (good-certificate (and (or smime-CA-file smime-CA-directory)
171                                  (smime-verify-buffer)))
172           addresses openssl-output)
173       (setq openssl-output (with-current-buffer smime-details-buffer
174                              (buffer-string)))
175       (if (not good-signature)
176           (progn
177             ;; we couldn't verify message, fail with openssl output as message
178             (mm-set-handle-multipart-parameter
179              mm-security-handle 'gnus-info "Failed")
180             (mm-set-handle-multipart-parameter
181              mm-security-handle 'gnus-details
182              (concat "OpenSSL failed to verify message integrity:\n"
183                      "-------------------------------------------\n"
184                      openssl-output)))
185         ;; verify mail addresses in mail against those in certificate
186         (when (and (smime-pkcs7-region (point-min) (point-max))
187                    (smime-pkcs7-certificates-region (point-min) (point-max)))
188           (with-temp-buffer
189             (insert-buffer-substring buf)
190             (goto-char (point-min))
191             (while (re-search-forward "-----END CERTIFICATE-----" nil t)
192               (when (smime-pkcs7-email-region (point-min) (point))
193                 (setq addresses (append (smime-buffer-as-string-region
194                                          (point-min) (point)) addresses)))
195               (delete-region (point-min) (point)))
196             (setq addresses (mapcar 'downcase addresses))))
197         (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
198             (mm-set-handle-multipart-parameter
199              mm-security-handle 'gnus-info "Sender address forged")
200           (if good-certificate
201               (mm-set-handle-multipart-parameter
202                mm-security-handle 'gnus-info "Ok (sender authenticated)")
203             (mm-set-handle-multipart-parameter
204              mm-security-handle 'gnus-info "Ok (sender not trusted)")))
205         (mm-set-handle-multipart-parameter
206          mm-security-handle 'gnus-details
207          (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
208                  (if addresses
209                      (concat "Addresses in certificate: "
210                              (mapconcat 'identity addresses ", "))
211                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
212                  "\n" "\n"
213                  "OpenSSL output:\n"
214                  "---------------\n" openssl-output "\n"
215                  "Certificate(s) inside S/MIME signature:\n"
216                  "---------------------------------------\n"
217                  (buffer-string) "\n")))))
218   handle)
219
220 (defun mml-smime-verify-test (handle ctl)
221   smime-openssl-program)
222
223 (provide 'mml-smime)
224
225 ;;; arch-tag: f1bf94d4-f2cd-4c6f-b059-ad69492817e2
226 ;;; mml-smime.el ends here