(message-narrow-to-headers): Autoload.
[gnus] / lisp / mml-smime.el
1 ;;; mml-smime.el --- S/MIME support for MML
2 ;; Copyright (c) 2000, 2001, 2003 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 (require 'smime)
29 (require 'mm-decode)
30 (autoload 'message-narrow-to-headers "message")
31
32 (defun mml-smime-sign (cont)
33   (when (null smime-keys)
34     (customize-variable 'smime-keys)
35     (error "No S/MIME keys configured, use customize to add your key"))
36   (smime-sign-buffer (cdr (assq 'keyfile cont)))
37   (goto-char (point-max)))
38
39 (defun mml-smime-encrypt (cont)
40   (let (certnames certfiles tmp file tmpfiles)
41     ;; xxx tmp files are always an security issue
42     (while (setq tmp (pop cont))
43       (if (and (consp tmp) (eq (car tmp) 'certfile))
44           (push (cdr tmp) certnames)))
45     (while (setq tmp (pop certnames))
46       (if (not (and (not (file-exists-p tmp))
47                     (get-buffer tmp)))
48           (push tmp certfiles)
49         (setq file (mm-make-temp-file (expand-file-name "mml." 
50                                                         mm-tmp-directory)))
51         (with-current-buffer tmp
52           (write-region (point-min) (point-max) file))
53         (push file certfiles)
54         (push file tmpfiles)))
55     (if (smime-encrypt-buffer certfiles)
56         (progn
57           (while (setq tmp (pop tmpfiles))
58             (delete-file tmp))
59           t)
60       (while (setq tmp (pop tmpfiles))
61         (delete-file tmp))
62       nil))
63   (goto-char (point-max)))
64
65 (defun mml-smime-sign-query ()
66   ;; query information (what certificate) from user when MML tag is
67   ;; added, for use later by the signing process
68   (when (null smime-keys)
69     (customize-variable 'smime-keys)
70     (error "No S/MIME keys configured, use customize to add your key"))
71   (list 'keyfile
72         (if (= (length smime-keys) 1)
73             (cadar smime-keys)
74           (or (let ((from (cadr (funcall gnus-extract-address-components
75                                          (or (save-excursion
76                                                (save-restriction
77                                                  (message-narrow-to-headers)
78                                                  (message-fetch-field "from")))
79                                              "")))))
80                 (and from (smime-get-key-by-email from)))
81               (smime-get-key-by-email
82                (completing-read "Sign this part with what signature? "
83                                 smime-keys nil nil
84                                 (and (listp (car-safe smime-keys))
85                                      (caar smime-keys))))))))
86
87 (defun mml-smime-get-file-cert ()
88   (ignore-errors
89     (list 'certfile (read-file-name
90                      "File with recipient's S/MIME certificate: "
91                      smime-certificate-directory nil t ""))))
92
93 (defun mml-smime-get-dns-cert ()
94   ;; todo: deal with comma separated multiple recipients
95   (let (result who bad cert)
96     (condition-case ()
97         (while (not result)
98           (setq who (read-from-minibuffer
99                      (format "%sLookup certificate for: " (or bad ""))
100                      (cadr (funcall gnus-extract-address-components
101                                     (or (save-excursion
102                                           (save-restriction
103                                             (message-narrow-to-headers)
104                                             (message-fetch-field "to")))
105                                         "")))))
106           (if (setq cert (smime-cert-by-dns who))
107               (setq result (list 'certfile (buffer-name cert)))
108             (setq bad (format "`%s' not found. " who))))
109       (quit))
110     result))
111
112 (defun mml-smime-encrypt-query ()
113   ;; todo: add ldap support (xemacs ldap api?)
114   ;; todo: try dns/ldap automatically first, before prompting user
115   (let (certs done)
116     (while (not done)
117       (ecase (read (gnus-completing-read-with-default
118                     "dns" "Fetch certificate from"
119                     '(("dns") ("file")) nil t))
120         (dns (setq certs (append certs
121                                  (mml-smime-get-dns-cert))))
122         (file (setq certs (append certs
123                                   (mml-smime-get-file-cert)))))
124       (setq done (not (y-or-n-p "Add more recipients? "))))
125     certs))
126
127 (defun mml-smime-verify (handle ctl)
128   (with-temp-buffer
129     (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
130     (goto-char (point-min))
131     (insert (format "Content-Type: %s; " (mm-handle-media-type ctl)))
132     (insert (format "protocol=\"%s\"; "
133                     (mm-handle-multipart-ctl-parameter ctl 'protocol)))
134     (insert (format "micalg=\"%s\"; "
135                     (mm-handle-multipart-ctl-parameter ctl 'micalg)))
136     (insert (format "boundary=\"%s\"\n\n"
137                     (mm-handle-multipart-ctl-parameter ctl 'boundary)))
138     (when (get-buffer smime-details-buffer)
139       (kill-buffer smime-details-buffer))
140     (let ((buf (current-buffer))
141           (good-signature (smime-noverify-buffer))
142           (good-certificate (and (or smime-CA-file smime-CA-directory)
143                                  (smime-verify-buffer)))
144           addresses openssl-output)
145       (setq openssl-output (with-current-buffer smime-details-buffer
146                              (buffer-string)))
147       (if (not good-signature)
148           (progn
149             ;; we couldn't verify message, fail with openssl output as message
150             (mm-set-handle-multipart-parameter
151              mm-security-handle 'gnus-info "Failed")
152             (mm-set-handle-multipart-parameter
153              mm-security-handle 'gnus-details
154              (concat "OpenSSL failed to verify message integrity:\n"
155                      "-------------------------------------------\n"
156                      openssl-output)))
157         ;; verify mail addresses in mail against those in certificate
158         (when (and (smime-pkcs7-region (point-min) (point-max))
159                    (smime-pkcs7-certificates-region (point-min) (point-max)))
160           (with-temp-buffer
161             (insert-buffer-substring buf)
162             (goto-char (point-min))
163             (while (re-search-forward "-----END CERTIFICATE-----" nil t)
164               (when (smime-pkcs7-email-region (point-min) (point))
165                 (setq addresses (append (smime-buffer-as-string-region
166                                          (point-min) (point)) addresses)))
167               (delete-region (point-min) (point)))
168             (setq addresses (mapcar 'downcase addresses))))
169         (if (not (member (downcase (or (mm-handle-multipart-from ctl) "")) addresses))
170             (mm-set-handle-multipart-parameter
171              mm-security-handle 'gnus-info "Sender address forged")
172           (if good-certificate
173               (mm-set-handle-multipart-parameter
174                mm-security-handle 'gnus-info "Ok (sender authenticated)")
175             (mm-set-handle-multipart-parameter
176              mm-security-handle 'gnus-info "Ok (sender not trusted)")))
177         (mm-set-handle-multipart-parameter
178          mm-security-handle 'gnus-details
179          (concat "Sender claimed to be: " (mm-handle-multipart-from ctl) "\n"
180                  (if addresses
181                      (concat "Addresses in certificate: "
182                              (mapconcat 'identity addresses ", "))
183                    "No addresses found in certificate. (Requires OpenSSL 0.9.6 or later.)")
184                  "\n" "\n"
185                  "OpenSSL output:\n"
186                  "---------------\n" openssl-output "\n"
187                  "Certificate(s) inside S/MIME signature:\n"
188                  "---------------------------------------\n"
189                  (buffer-string) "\n")))))
190   handle)
191
192 (defun mml-smime-verify-test (handle ctl)
193   smime-openssl-program)
194
195 (provide 'mml-smime)
196
197 ;;; mml-smime.el ends here