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