Synch to No Gnus 200506160601, and
[elisp/gnus.git-] / lisp / mml-smime.el
1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: Gnus, MIME, S/MIME, MML
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 ;; EMIKO doesn't provide the smime.el module.
31 (condition-case nil
32     (require 'smime)
33   (error))
34 (require 'mm-decode)
35 (autoload 'message-narrow-to-headers "message")
36 (autoload 'message-fetch-field "message")
37
38 (defun mml-smime-sign (cont)
39   (when (null smime-keys)
40     (customize-variable 'smime-keys)
41     (error "No S/MIME keys configured, use customize to add your key"))
42   (smime-sign-buffer (cdr (assq 'keyfile cont)))
43   (goto-char (point-min))
44   (while (search-forward "\r\n" nil t)
45     (replace-match "\n" t t))
46   (goto-char (point-max)))
47
48 (defun mml-smime-encrypt (cont)
49   (let (certnames certfiles tmp file tmpfiles)
50     ;; xxx tmp files are always an security issue
51     (while (setq tmp (pop cont))
52       (if (and (consp tmp) (eq (car tmp) 'certfile))
53           (push (cdr tmp) certnames)))
54     (while (setq tmp (pop certnames))
55       (if (not (and (not (file-exists-p tmp))
56                     (get-buffer tmp)))
57           (push tmp certfiles)
58         (setq file (mm-make-temp-file (expand-file-name "mml." 
59                                                         mm-tmp-directory)))
60         (with-current-buffer tmp
61           (write-region (point-min) (point-max) file))
62         (push file certfiles)
63         (push file tmpfiles)))
64     (if (smime-encrypt-buffer certfiles)
65         (progn
66           (while (setq tmp (pop tmpfiles))
67             (delete-file tmp))
68           t)
69       (while (setq tmp (pop tmpfiles))
70         (delete-file tmp))
71       nil))
72   (goto-char (point-max)))
73
74 (defun mml-smime-sign-query ()
75   ;; query information (what certificate) from user when MML tag is
76   ;; added, for use later by the signing process
77   (when (null smime-keys)
78     (customize-variable 'smime-keys)
79     (error "No S/MIME keys configured, use customize to add your key"))
80   (list 'keyfile
81         (if (= (length smime-keys) 1)
82             (cadar smime-keys)
83           (or (let ((from (cadr (funcall gnus-extract-address-components
84                                          (or (save-excursion
85                                                (save-restriction
86                                                  (message-narrow-to-headers)
87                                                  (message-fetch-field "from")))
88                                              "")))))
89                 (and from (smime-get-key-by-email from)))
90               (smime-get-key-by-email
91                (completing-read "Sign this part with what signature? "
92                                 smime-keys nil nil
93                                 (and (listp (car-safe smime-keys))
94                                      (caar smime-keys))))))))
95
96 (defun mml-smime-get-file-cert ()
97   (ignore-errors
98     (list 'certfile (read-file-name
99                      "File with recipient's S/MIME certificate: "
100                      smime-certificate-directory nil t ""))))
101
102 (defun mml-smime-get-dns-cert ()
103   ;; todo: deal with comma separated multiple recipients
104   (let (result who bad cert)
105     (condition-case ()
106         (while (not result)
107           (setq who (read-from-minibuffer
108                      (format "%sLookup certificate for: " (or bad ""))
109                      (cadr (funcall gnus-extract-address-components
110                                     (or (save-excursion
111                                           (save-restriction
112                                             (message-narrow-to-headers)
113                                             (message-fetch-field "to")))
114                                         "")))))
115           (if (setq cert (smime-cert-by-dns who))
116               (setq result (list 'certfile (buffer-name cert)))
117             (setq bad (format "`%s' not found. " who))))
118       (quit))
119     result))
120
121 (defun mml-smime-get-ldap-cert ()
122   ;; todo: deal with comma separated multiple recipients
123   (let (result who bad cert)
124     (condition-case ()
125         (while (not result)
126           (setq who (read-from-minibuffer
127                      (format "%sLookup certificate for: " (or bad ""))
128                      (cadr (funcall gnus-extract-address-components
129                                     (or (save-excursion
130                                           (save-restriction
131                                             (message-narrow-to-headers)
132                                             (message-fetch-field "to")))
133                                         "")))))
134           (if (setq cert (smime-cert-by-ldap who))
135               (setq result (list 'certfile (buffer-name cert)))
136             (setq bad (format "`%s' not found. " who))))
137       (quit))
138     result))
139
140 (defun mml-smime-encrypt-query ()
141   ;; todo: add ldap support (xemacs ldap api?)
142   ;; todo: try dns/ldap automatically first, before prompting user
143   (let (certs done)
144     (while (not done)
145       (ecase (read (gnus-completing-read-with-default
146                     "ldap" "Fetch certificate from"
147                     '(("dns") ("ldap") ("file")) nil t))
148         (dns (setq certs (append certs
149                                  (mml-smime-get-dns-cert))))
150         (ldap (setq certs (append certs
151                                   (mml-smime-get-ldap-cert))))
152         (file (setq certs (append certs
153                                   (mml-smime-get-file-cert)))))
154       (setq done (not (y-or-n-p "Add more recipients? "))))
155     certs))
156
157 (defun mml-smime-verify (handle ctl)
158   (with-temp-buffer
159     (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
160     (goto-char (point-min))
161     (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
162     (insert (format "protocol=\"%s\"; "
163                     (mm-handle-multipart-ctl-parameter ctl 'protocol)))
164     (insert (format "micalg=\"%s\"; "
165                     (mm-handle-multipart-ctl-parameter ctl 'micalg)))
166     (insert (format "boundary=\"%s\"\n\n"
167                     (mm-handle-multipart-ctl-parameter ctl 'boundary)))
168     (when (get-buffer smime-details-buffer)
169       (kill-buffer smime-details-buffer))
170     (let ((buf (current-buffer))
171           (good-signature (smime-noverify-buffer))
172           (good-certificate (and (or smime-CA-file smime-CA-directory)
173                                  (smime-verify-buffer)))
174           addresses openssl-output)
175       (setq openssl-output (with-current-buffer smime-details-buffer
176                              (buffer-string)))
177       (if (not good-signature)
178           (progn
179             ;; we couldn't verify message, fail with openssl output as message
180             (mm-set-handle-multipart-parameter
181              mm-security-handle 'gnus-info "Failed")
182             (mm-set-handle-multipart-parameter
183              mm-security-handle 'gnus-details
184              (concat "OpenSSL failed to verify message integrity:\n"
185                      "-------------------------------------------\n"
186                      openssl-output)))
187         ;; verify mail addresses in mail against those in certificate
188         (when (and (smime-pkcs7-region (point-min) (point-max))
189                    (smime-pkcs7-certificates-region (point-min) (point-max)))
190           (with-temp-buffer
191             (insert-buffer-substring buf)
192             (goto-char (point-min))
193             (while (re-search-forward "-----END CERTIFICATE-----" nil t)
194               (when (smime-pkcs7-email-region (point-min) (point))
195                 (setq addresses (append (smime-buffer-as-string-region
196                                          (point-min) (point)) addresses)))
197               (delete-region (point-min) (point)))
198             (setq addresses (mapcar 'downcase addresses))))
199         (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
200             (mm-set-handle-multipart-parameter
201              mm-security-handle 'gnus-info "Sender address forged")
202           (if good-certificate
203               (mm-set-handle-multipart-parameter
204                mm-security-handle 'gnus-info "Ok (sender authenticated)")
205             (mm-set-handle-multipart-parameter
206              mm-security-handle 'gnus-info "Ok (sender not trusted)")))
207         (mm-set-handle-multipart-parameter
208          mm-security-handle 'gnus-details
209          (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
210                  (if addresses
211                      (concat "Addresses in certificate: "
212                              (mapconcat 'identity addresses ", "))
213                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
214                  "\n" "\n"
215                  "OpenSSL output:\n"
216                  "---------------\n" openssl-output "\n"
217                  "Certificate(s) inside S/MIME signature:\n"
218                  "---------------------------------------\n"
219                  (buffer-string) "\n")))))
220   handle)
221
222 (defun mml-smime-verify-test (handle ctl)
223   smime-openssl-program)
224
225 (provide 'mml-smime)
226
227 ;;; mml-smime.el ends here