2000-11-30 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-current-buffer (mm-handle-multipart-original-buffer ctl)
121     ;; xxx modifies buffer -- noone else uses the buffer, so what the heck
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     (if (smime-verify-buffer)
133         ;; verify mail addresses in mail against those in certificate
134         (when (and (smime-pkcs7-region (point-min) (point-max))
135                    (smime-pkcs7-certificates-region (point-min) (point-max)))
136           (with-temp-buffer
137             (insert-buffer-substring (mm-handle-multipart-original-buffer ctl))
138             (if (not (member from (and (smime-pkcs7-email-region
139                                         (point-min) (point-max))
140                                        (smime-buffer-as-string-region
141                                         (point-min) (point-max)))))
142                 (progn
143                   (mm-set-handle-multipart-parameter 
144                    mm-security-handle 'gnus-info "Sender forged")
145                   (mm-set-handle-multipart-parameter
146                    mm-security-handle 'gnus-details 
147                    (with-current-buffer
148                        (mm-handle-multipart-original-buffer ctl)
149                      (buffer-string))))
150               (mm-set-handle-multipart-parameter 
151                mm-security-handle 'gnus-info "OK")
152               (kill-buffer smime-details-buffer))))
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        (with-current-buffer smime-details-buffer 
158          (buffer-string))))
159     handle))
160
161 (defun mml-smime-verify-test (handle ctl)
162   smime-openssl-program)
163
164 (provide 'mml-smime)
165
166 ;;; mml-smime.el ends here