Import Gnus v5.10.6.
[elisp/gnus.git-] / contrib / smime.el
1 ;;; smime.el --- S/MIME support library
2 ;; Copyright (c) 2000 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: SMIME X.509 PEM OpenSSL
6
7 ;; This file is not a part of GNU Emacs, but the same permissions apply.
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 ;; This library perform S/MIME operations from within Emacs.
27 ;;
28 ;; Functions for fetching certificates from public repositories are
29 ;; NOT provided (yet).
30 ;;
31 ;; It uses OpenSSL (tested with version 0.9.5a) for signing,
32 ;; encryption and decryption.
33 ;;
34 ;; Some general knowledge of S/MIME, X.509, PKCS#12, PEM etc is
35 ;; probably required to use this library in any useful way.
36 ;; Especially, don't expect this library to buy security for you.  If
37 ;; you don't understand what you are doing, you're as likely to lose
38 ;; security than gain any by using this library.
39
40 ;;; Quick introduction:
41
42 ;; Get your S/MIME certificate from VeriSign or someplace.  I used
43 ;; Netscape to generate the key and certificate request and stuff, and
44 ;; Netscape can export the key into PKCS#12 format.
45 ;;
46 ;; Enter OpenSSL.  To be able to use this library, it need to have the
47 ;; SMIME key readable in PEM format.  OpenSSL is used to convert the
48 ;; key:
49 ;;
50 ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
51 ;; ...
52 ;;
53 ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
54 ;; a key.
55 ;;
56 ;; Now you should be able to sign messages!  Create a buffer and write
57 ;; something and run M-x smime-sign-buffer RET RET and you should see
58 ;; your message MIME armoured and a signature.  Encryption, M-x
59 ;; smime-encrypt-buffer, should also work.
60 ;;
61 ;; To be able to verify messages you need to build up trust with
62 ;; someone.  Perhaps you trust the CA that issued your certificate, at
63 ;; least I did, so I export it's certificates from my PKCS#12
64 ;; certificate with:
65 ;;
66 ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
67 ;; ...
68 ;;
69 ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
70 ;; CA certificate.
71 ;;
72 ;; You should now be able to sign messages, and even verify messages
73 ;; sent by others that use the same CA as you.
74
75 ;; Bugs:
76 ;;
77 ;; Don't complain that this package doesn't do encrypted PEM files,
78 ;; submit a patch instead.  I store my keys in a safe place, so I
79 ;; didn't need the encryption.  Also, programming this was made a lot
80 ;; easier by that decision.  One might think that this even influenced
81 ;; were I store my keys, and one would probably be right. :-)
82 ;;
83 ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
84
85 ;; <rant>
86 ;;
87 ;; I would include pointers to introductory text on concepts used in
88 ;; this library here, but the material I've read are so horrible I
89 ;; don't want to recomend them.
90 ;;
91 ;; Why can't someone write a simple introduction to all this stuff?
92 ;; Until then, much of this resemble security by obscurity.
93 ;;
94 ;; Also, I'm not going to mention anything about the wonders of
95 ;; cryptopolitics.  Oops, I just did.
96 ;;
97 ;; </rant>
98
99 ;;; Revision history:
100
101 ;; version 0 not released
102
103 ;;; Code:
104
105 (defgroup smime nil
106   "S/MIME configuration.")
107
108 (defcustom smime-keys nil
109   "Map your mail addresses to a file with your certified key.
110 The file is assumed to be in PEM format and not encrypted."
111   :type '(repeat (list (string :tag "Mail address")
112                        (file :tag "File name")))
113   :group 'smime)
114
115 (defcustom smime-CAs nil
116   "List of directories/files containing certificates for CAs you trust.
117 Files should be in PEM format.
118 Directories should contain files (in PEM format) named to the X.509
119 hash of the certificate."
120   :type '(repeat (radio (directory :tag "Trusted CA directory")
121                         (file :tag "Trusted CA file")))
122   :group 'smime)
123
124 (defcustom smime-certificate-directory "~/Mail/certs/"
125   "Directory containing other people's certificates.
126 It should contain files named to the X.509 hash of the certificate,
127 and the files themself should be in PEM format.
128 The S/MIME library provide simple functionality for fetching
129 certificates into this directory, so there is no need to populate it
130 manually."
131   :type 'directory
132   :group 'smime)
133
134 (defcustom smime-openssl-program "openssl"
135   "Name of OpenSSL binary."
136   :type 'string
137   :group 'smime)
138
139 ;; OpenSSL wrappers.
140
141 (defun smime-call-openssl-region (b e buf &rest args)
142   (case (apply 'call-process-region b e smime-openssl-program nil buf nil args)
143     (0 t)
144     (1 (error "OpenSSL: An error occurred parsing the command options."))
145     (2 (error "OpenSSL: One of the input files could not be read."))
146     (3 (error "OpenSSL: an error occurred creating the PKCS#7 file or when reading the MIME message."))
147     (4 (error "OpenSSL: an error occurred decrypting or verifying the message."))
148     (t (error "Unknown OpenSSL exitcode %s" exitcode))))
149
150 (defun smime-sign-region (b e keyfile)
151   "Sign region with certified key in KEYFILE.
152 If signing fails, the buffer is not modified.  Region is assumed to
153 have proper MIME tags.  KEYFILE is expected to contain a PEM encoded
154 private key and certificate."
155   (let* ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*"))))
156     (when (smime-call-openssl-region b e buffer "smime" "-sign"
157                                      "-signer" (expand-file-name keyfile))
158       (delete-region b e)
159       (insert-buffer buffer)
160       (kill-buffer buffer)
161       t)))
162
163 (defun smime-encrypt-region (b e certfiles)
164   "Encrypt region for recipients specified in CERTFILES.
165 If encryption fails, the buffer is not modified.  Region is assumed to
166 have proper MIME tags.  CERTFILES is a list of filenames, each file
167 is expected to contain of a PEM encoded certificate."
168   (let ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*"))))
169     (when (apply 'smime-call-openssl-region b e buffer "smime" "-encrypt"
170                  (mapcar 'expand-file-name certfiles))
171       (delete-region b e)
172       (insert-buffer buffer)
173       (kill-buffer buffer)
174       t)))
175
176 (defun smime-sign-buffer (&optional keyfile buffer)
177   "S/MIME sign BUFFER with key in KEYFILE.
178 KEYFILE should contain a PEM encoded key and certificate."
179   (interactive)
180   (with-current-buffer (or buffer (current-buffer))
181     (smime-sign-region
182      (point-min) (point-max) 
183      (or keyfile
184          (smime-get-key-by-email
185           (completing-read "Sign using which signature? " smime-keys nil nil
186                            (and (listp (car-safe smime-keys)) (caar smime-keys))))))))
187
188 (defun smime-encrypt-buffer (&optional certfiles buffer)
189   "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
190 CERTFILES is a list of filenames, each file is expected to consist of
191 a PEM encoded key and certificate.  Uses current buffer if BUFFER is
192 nil."
193   (interactive)
194   (with-current-buffer (or buffer (current-buffer))
195     (smime-encrypt-region 
196      (point-min) (point-max)
197      (or certfiles
198          (list (read-file-name "Recipient's S/MIME certificate: "
199                                smime-certificate-directory nil))))))
200
201 ;; User interface.
202
203 (defvar smime-buffer "*SMIME*")
204
205 (defvar smime-mode-map nil)
206 (put 'smime-mode 'mode-class 'special)
207
208 (unless smime-mode-map
209   (setq smime-mode-map (make-sparse-keymap))
210   (suppress-keymap smime-mode-map)
211
212   (define-key smime-mode-map "q" 'smime-exit)
213   (define-key smime-mode-map "f" 'smime-certificate-info))
214
215 (defun smime-mode ()
216   "Major mode for browsing, viewing and fetching certificates.
217
218 All normal editing commands are switched off.
219 \\<smime-mode-map>
220
221 The following commands are available:
222
223 \\{smime-mode-map}"
224   (interactive)
225   (kill-all-local-variables)
226   (setq major-mode 'smime-mode)
227   (setq mode-name "SMIME")
228   (setq mode-line-process nil)
229   (use-local-map smime-mode-map)
230   (buffer-disable-undo)
231   (setq truncate-lines t)
232   (setq buffer-read-only t))
233
234 (defun smime-certificate-info (certfile)
235   (interactive "fCertificate file: ")
236   (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
237     (switch-to-buffer buffer)
238     (erase-buffer)
239     (call-process smime-openssl-program nil buffer 'display
240                   "x509" "-in" (expand-file-name certfile) "-text")
241     (fundamental-mode)
242     (set-buffer-modified-p nil)
243     (toggle-read-only t)
244     (goto-char (point-min))))
245
246 (defun smime-draw-buffer ()
247   (with-current-buffer smime-buffer
248     (let (buffer-read-only)
249       (erase-buffer)
250       (insert "\nYour keys:\n")
251       (dolist (key smime-keys)
252         (insert 
253          (format "\t\t%s: %s\n" (car key) (cadr key))))
254       (insert "\nTrusted Certificate Authoritys:\n")
255       (insert "\nKnown Certificates:\n"))))
256
257 (defun smime ()
258   "Go to the SMIME buffer."
259   (interactive)
260   (unless (get-buffer smime-buffer)
261     (save-excursion
262       (set-buffer (get-buffer-create smime-buffer))
263       (smime-mode)))
264   (smime-draw-buffer)
265   (switch-to-buffer smime-buffer))
266
267 (defun smime-exit ()
268   "Quit the S/MIME buffer."
269   (interactive)
270   (kill-buffer (current-buffer)))
271
272 ;; Other functions
273
274 (defun smime-get-key-by-email (email)
275   (cadr (assoc email smime-keys)))
276
277 (provide 'smime)
278
279 ;;; smime.el ends here