1 ;;; smime.el --- S/MIME support library
2 ;; Copyright (c) 2000 Free Software Foundation, Inc.
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: SMIME X.509 PEM OpenSSL
7 ;; This file is not a part of GNU Emacs, but the same permissions apply.
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.
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.
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.
26 ;; This library perform S/MIME operations from within Emacs.
28 ;; Functions for fetching certificates from public repositories are
29 ;; NOT provided (yet).
31 ;; It uses OpenSSL (tested with version 0.9.5a) for signing,
32 ;; encryption and decryption.
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.
40 ;;; Quick introduction:
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.
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
50 ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
53 ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
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.
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
66 ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
69 ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
72 ;; You should now be able to sign messages, and even verify messages
73 ;; sent by others that use the same CA as you.
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. :-)
83 ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
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.
91 ;; Why can't someone write a simple introduction to all this stuff?
92 ;; Until then, much of this resemble security by obscurity.
94 ;; Also, I'm not going to mention anything about the wonders of
95 ;; cryptopolitics. Oops, I just did.
101 ;; version 0 not released
106 "S/MIME configuration.")
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")))
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")))
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
134 (defcustom smime-openssl-program "openssl"
135 "Name of OpenSSL binary."
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)
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))))
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))
159 (insert-buffer buffer)
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))
172 (insert-buffer buffer)
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."
180 (with-current-buffer (or buffer (current-buffer))
182 (point-min) (point-max)
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))))))))
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
194 (with-current-buffer (or buffer (current-buffer))
195 (smime-encrypt-region
196 (point-min) (point-max)
198 (list (read-file-name "Recipient's S/MIME certificate: "
199 smime-certificate-directory nil))))))
203 (defvar smime-buffer "*SMIME*")
205 (defvar smime-mode-map nil)
206 (put 'smime-mode 'mode-class 'special)
208 (unless smime-mode-map
209 (setq smime-mode-map (make-sparse-keymap))
210 (suppress-keymap smime-mode-map)
212 (define-key smime-mode-map "q" 'smime-exit)
213 (define-key smime-mode-map "f" 'smime-certificate-info))
216 "Major mode for browsing, viewing and fetching certificates.
218 All normal editing commands are switched off.
221 The following commands are available:
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))
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)
239 (call-process smime-openssl-program nil buffer 'display
240 "x509" "-in" (expand-file-name certfile) "-text")
242 (set-buffer-modified-p nil)
244 (goto-char (point-min))))
246 (defun smime-draw-buffer ()
247 (with-current-buffer smime-buffer
248 (let (buffer-read-only)
250 (insert "\nYour keys:\n")
251 (dolist (key smime-keys)
253 (format "\t\t%s: %s\n" (car key) (cadr key))))
254 (insert "\nTrusted Certificate Authoritys:\n")
255 (insert "\nKnown Certificates:\n"))))
258 "Go to the SMIME buffer."
260 (unless (get-buffer smime-buffer)
262 (set-buffer (get-buffer-create smime-buffer))
265 (switch-to-buffer smime-buffer))
268 "Quit the S/MIME buffer."
270 (kill-buffer (current-buffer)))
274 (defun smime-get-key-by-email (email)
275 (cadr (assoc email smime-keys)))
279 ;;; smime.el ends here