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