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