* canlock.el (canlock): Change the parent group to news.
[gnus] / lisp / smime.el
1 ;;; smime.el --- S/MIME support library
2 ;; Copyright (c) 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
3
4 ;; Author: Simon Josefsson <simon@josefsson.org>
5 ;; Keywords: SMIME X.509 PEM OpenSSL
6
7 ;; This file is 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 ;; This library perform S/MIME operations from within Emacs.
27 ;;
28 ;; Functions for fetching certificates from public repositories are
29 ;; provided, currently from DNS and LDAP.
30 ;;
31 ;; It uses OpenSSL (tested with version 0.9.5a and 0.9.6) 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 ;; This library is not intended to provide a "raw" API for S/MIME,
41 ;; PKCSx or similar, it's intended to perform common operations
42 ;; done on messages encoded in these formats.  The terminology chosen
43 ;; reflect this.
44 ;;
45 ;; The home of this file is in Gnus CVS, but also available from
46 ;; http://josefsson.org/smime.html.
47
48 ;;; Quick introduction:
49
50 ;; Get your S/MIME certificate from VeriSign or someplace.  I used
51 ;; Netscape to generate the key and certificate request and stuff, and
52 ;; Netscape can export the key into PKCS#12 format.
53 ;;
54 ;; Enter OpenSSL.  To be able to use this library, it need to have the
55 ;; SMIME key readable in PEM format.  OpenSSL is used to convert the
56 ;; key:
57 ;;
58 ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
59 ;; ...
60 ;;
61 ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
62 ;; a key.
63 ;;
64 ;; Now you should be able to sign messages!  Create a buffer and write
65 ;; something and run M-x smime-sign-buffer RET RET and you should see
66 ;; your message MIME armoured and a signature.  Encryption, M-x
67 ;; smime-encrypt-buffer, should also work.
68 ;;
69 ;; To be able to verify messages you need to build up trust with
70 ;; someone.  Perhaps you trust the CA that issued your certificate, at
71 ;; least I did, so I export it's certificates from my PKCS#12
72 ;; certificate with:
73 ;;
74 ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
75 ;; ...
76 ;;
77 ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
78 ;; CA certificate.
79 ;;
80 ;; You should now be able to sign messages, and even verify messages
81 ;; sent by others that use the same CA as you.
82
83 ;; Bugs:
84 ;;
85 ;; Don't complain that this package doesn't do encrypted PEM files,
86 ;; submit a patch instead.  I store my keys in a safe place, so I
87 ;; didn't need the encryption.  Also, programming was made easier by
88 ;; that decision.  One might think that this even influenced were I
89 ;; store my keys, and one would probably be right. :-)
90 ;;
91 ;; Update: Mathias Herberts sent the patch.  However, it uses
92 ;; environment variables to pass the password to OpenSSL, which is
93 ;; slightly insecure. Hence a new todo: use a better -passin method.
94 ;;
95 ;; Cache password for e.g. 1h
96 ;;
97 ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
98
99 ;; begin rant
100 ;;
101 ;; I would include pointers to introductory text on concepts used in
102 ;; this library here, but the material I've read are so horrible I
103 ;; don't want to recomend them.
104 ;;
105 ;; Why can't someone write a simple introduction to all this stuff?
106 ;; Until then, much of this resemble security by obscurity.
107 ;;
108 ;; Also, I'm not going to mention anything about the wonders of
109 ;; cryptopolitics.  Oops, I just did.
110 ;;
111 ;; end rant
112
113 ;;; Revision history:
114
115 ;; 2000-06-05  initial version, committed to Gnus CVS contrib/
116 ;; 2000-10-28  retrieve certificates via DNS CERT RRs
117 ;; 2001-10-14  posted to gnu.emacs.sources
118 ;; 2005-02-13  retrieve certificates via LDAP
119
120 ;;; Code:
121
122 (require 'dig)
123 (require 'smime-ldap)
124 (require 'password)
125 (eval-when-compile (require 'cl))
126
127 (defgroup smime nil
128   "S/MIME configuration."
129   :group 'mime)
130
131 (defcustom smime-keys nil
132   "*Map mail addresses to a file containing Certificate (and private key).
133 The file is assumed to be in PEM format. You can also associate additional
134 certificates to be sent with every message to each address."
135   :type '(repeat (list (string :tag "Mail address")
136                        (file :tag "File name")
137                        (repeat :tag "Additional certificate files"
138                                (file :tag "File name"))))
139   :group 'smime)
140
141 (defcustom smime-CA-directory nil
142   "*Directory containing certificates for CAs you trust.
143 Directory should contain files (in PEM format) named to the X.509
144 hash of the certificate.  This can be done using OpenSSL such as:
145
146 $ ln -s ca.pem `openssl x509 -noout -hash -in ca.pem`.0
147
148 where `ca.pem' is the file containing a PEM encoded X.509 CA
149 certificate."
150   :type '(choice (const :tag "none" nil)
151                  directory)
152   :group 'smime)
153
154 (defcustom smime-CA-file nil
155   "*Files containing certificates for CAs you trust.
156 File should contain certificates in PEM format."
157   :version "22.1"
158   :type '(choice (const :tag "none" nil)
159                  file)
160   :group 'smime)
161
162 (defcustom smime-certificate-directory "~/Mail/certs/"
163   "*Directory containing other people's certificates.
164 It should contain files named to the X.509 hash of the certificate,
165 and the files themself should be in PEM format."
166 ;The S/MIME library provide simple functionality for fetching
167 ;certificates into this directory, so there is no need to populate it
168 ;manually.
169   :type 'directory
170   :group 'smime)
171
172 (defcustom smime-openssl-program
173   (and (condition-case ()
174            (eq 0 (call-process "openssl" nil nil nil "version"))
175          (error nil))
176        "openssl")
177   "*Name of OpenSSL binary."
178   :type 'string
179   :group 'smime)
180
181 ;; OpenSSL option to select the encryption cipher
182
183 (defcustom smime-encrypt-cipher "-des3"
184   "*Cipher algorithm used for encryption."
185   :version "22.1"
186   :type '(choice (const :tag "Triple DES" "-des3")
187                  (const :tag "DES"  "-des")
188                  (const :tag "RC2 40 bits" "-rc2-40")
189                  (const :tag "RC2 64 bits" "-rc2-64")
190                  (const :tag "RC2 128 bits" "-rc2-128"))
191   :group 'smime)
192
193 (defcustom smime-crl-check nil
194   "*Check revocation status of signers certificate using CRLs.
195 Enabling this will have OpenSSL check the signers certificate
196 against a certificate revocation list (CRL).
197
198 For this to work the CRL must be up-to-date and since they are
199 normally updated quite often (ie. several times a day) you
200 probably need some tool to keep them up-to-date. Unfortunately
201 Gnus cannot do this for you.
202
203 The CRL should either be appended (in PEM format) to your
204 `smime-CA-file' or be located in a file (also in PEM format) in
205 your `smime-certificate-directory' named to the X.509 hash of the
206 certificate with .r0 as file name extension.
207
208 At least OpenSSL version 0.9.7 is required for this to work."
209   :type '(choice (const :tag "No check" nil)
210                  (const :tag "Check certificate" "-crl_check")
211                  (const :tag "Check certificate chain" "-crl_check_all"))
212   :group 'smime)
213
214 (defcustom smime-dns-server nil
215   "*DNS server to query certificates from.
216 If nil, use system defaults."
217   :version "22.1"
218   :type '(choice (const :tag "System defaults")
219                  string)
220   :group 'smime)
221
222 (defcustom smime-ldap-host-list nil
223   "A list of LDAP hosts with S/MIME user certificates.
224 If needed search base, binddn, passwd, etc. for the LDAP host
225 must be set in `ldap-host-parameters-alist'."
226   :type '(repeat (string :tag "Host name"))
227   :version "23.0" ;; No Gnus
228   :group 'smime)
229
230 (defvar smime-details-buffer "*OpenSSL output*")
231
232 ;; Use mm-util?
233 (eval-and-compile
234   (defalias 'smime-make-temp-file
235     (if (fboundp 'make-temp-file)
236         'make-temp-file
237       (lambda (prefix &optional dir-flag) ;; Simple implementation
238         (expand-file-name
239          (make-temp-name prefix)
240          (if (fboundp 'temp-directory)
241              (temp-directory)
242            temporary-file-directory))))))
243
244 ;; Password dialog function
245
246 (defun smime-ask-passphrase (&optional cache-key)
247   "Asks the passphrase to unlock the secret key.
248 If `cache-key' and `password-cache' is non-nil then cache the
249 password under `cache-key'."
250   (let ((passphrase
251          (password-read-and-add
252           "Passphrase for secret key (RET for no passphrase): " cache-key)))
253     (if (string= passphrase "")
254         nil
255       passphrase)))
256
257 ;; OpenSSL wrappers.
258
259 (defun smime-call-openssl-region (b e buf &rest args)
260   (case (apply 'call-process-region b e smime-openssl-program nil buf nil args)
261     (0 t)
262     (1 (message "OpenSSL: An error occurred parsing the command options.") nil)
263     (2 (message "OpenSSL: One of the input files could not be read.") nil)
264     (3 (message "OpenSSL: An error occurred creating the PKCS#7 file or when reading the MIME message.") nil)
265     (4 (message "OpenSSL: An error occurred decrypting or verifying the message.") nil)
266     (t (error "Unknown OpenSSL exitcode") nil)))
267
268 (defun smime-make-certfiles (certfiles)
269   (if certfiles
270       (append (list "-certfile" (expand-file-name (car certfiles)))
271               (smime-make-certfiles (cdr certfiles)))))
272
273 ;; Sign+encrypt region
274
275 (defun smime-sign-region (b e keyfile)
276   "Sign region with certified key in KEYFILE.
277 If signing fails, the buffer is not modified.  Region is assumed to
278 have proper MIME tags.  KEYFILE is expected to contain a PEM encoded
279 private key and certificate as its car, and a list of additional
280 certificates to include in its caar.  If no additional certificates is
281 included, KEYFILE may be the file containing the PEM encoded private
282 key and certificate itself."
283   (smime-new-details-buffer)
284   (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
285          (keyfile (or (car-safe keyfile) keyfile))
286          (buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
287          (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
288          (tmpfile (smime-make-temp-file "smime")))
289     (if passphrase
290         (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
291     (prog1
292         (when (prog1
293                   (apply 'smime-call-openssl-region b e (list buffer tmpfile)
294                          "smime" "-sign" "-signer" (expand-file-name keyfile)
295                          (append
296                           (smime-make-certfiles certfiles)
297                           (if passphrase
298                               (list "-passin" "env:GNUS_SMIME_PASSPHRASE"))))
299                 (if passphrase
300                     (setenv "GNUS_SMIME_PASSPHRASE" "" t))
301                 (with-current-buffer smime-details-buffer
302                   (insert-file-contents tmpfile)
303                   (delete-file tmpfile)))
304           (delete-region b e)
305           (insert-buffer-substring buffer)
306           (goto-char b)
307           (when (looking-at "^MIME-Version: 1.0$")
308             (delete-region (point) (progn (forward-line 1) (point))))
309           t)
310       (with-current-buffer smime-details-buffer
311         (goto-char (point-max))
312         (insert-buffer-substring buffer))
313       (kill-buffer buffer))))
314
315 (defun smime-encrypt-region (b e certfiles)
316   "Encrypt region for recipients specified in CERTFILES.
317 If encryption fails, the buffer is not modified.  Region is assumed to
318 have proper MIME tags.  CERTFILES is a list of filenames, each file
319 is expected to contain of a PEM encoded certificate."
320   (smime-new-details-buffer)
321   (let ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
322         (tmpfile (smime-make-temp-file "smime")))
323     (prog1
324         (when (prog1
325                   (apply 'smime-call-openssl-region b e (list buffer tmpfile)
326                          "smime" "-encrypt" smime-encrypt-cipher
327                          (mapcar 'expand-file-name certfiles))
328                 (with-current-buffer smime-details-buffer
329                   (insert-file-contents tmpfile)
330                   (delete-file tmpfile)))
331           (delete-region b e)
332           (insert-buffer-substring buffer)
333           (goto-char b)
334           (when (looking-at "^MIME-Version: 1.0$")
335             (delete-region (point) (progn (forward-line 1) (point))))
336           t)
337       (with-current-buffer smime-details-buffer
338         (goto-char (point-max))
339         (insert-buffer-substring buffer))
340       (kill-buffer buffer))))
341
342 ;; Sign+encrypt buffer
343
344 (defun smime-sign-buffer (&optional keyfile buffer)
345   "S/MIME sign BUFFER with key in KEYFILE.
346 KEYFILE should contain a PEM encoded key and certificate."
347   (interactive)
348   (with-current-buffer (or buffer (current-buffer))
349     (unless (smime-sign-region
350              (point-min) (point-max)
351              (if keyfile
352                  keyfile
353                (smime-get-key-with-certs-by-email
354                 (completing-read
355                  (concat "Sign using which key? "
356                          (if smime-keys (concat "(default " (caar smime-keys) ") ")
357                            ""))
358                  smime-keys nil nil (car-safe (car-safe smime-keys))))))
359       (error "Signing failed"))))
360
361 (defun smime-encrypt-buffer (&optional certfiles buffer)
362   "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
363 CERTFILES is a list of filenames, each file is expected to consist of
364 a PEM encoded key and certificate.  Uses current buffer if BUFFER is
365 nil."
366   (interactive)
367   (with-current-buffer (or buffer (current-buffer))
368     (unless (smime-encrypt-region
369              (point-min) (point-max)
370              (or certfiles
371                  (list (read-file-name "Recipient's S/MIME certificate: "
372                                        smime-certificate-directory nil))))
373       (error "Encryption failed"))))
374
375 ;; Verify+decrypt region
376
377 (defun smime-verify-region (b e)
378   "Verify S/MIME message in region between B and E.
379 Returns non-nil on success.
380 Any details (stdout and stderr) are left in the buffer specified by
381 `smime-details-buffer'."
382   (smime-new-details-buffer)
383   (let ((CAs (append (if smime-CA-file
384                          (list "-CAfile"
385                                (expand-file-name smime-CA-file)))
386                      (if smime-CA-directory
387                          (list "-CApath"
388                                (expand-file-name smime-CA-directory))))))
389     (unless CAs
390       (error "No CA configured"))
391     (if smime-crl-check
392         (add-to-list 'CAs smime-crl-check))
393     (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
394                "smime" "-verify" "-out" "/dev/null" CAs)
395         t
396       (insert-buffer-substring smime-details-buffer)
397       nil)))
398
399 (defun smime-noverify-region (b e)
400   "Verify integrity of S/MIME message in region between B and E.
401 Returns non-nil on success.
402 Any details (stdout and stderr) are left in the buffer specified by
403 `smime-details-buffer'."
404   (smime-new-details-buffer)
405   (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
406              "smime" "-verify" "-noverify" "-out" '("/dev/null"))
407       t
408     (insert-buffer-substring smime-details-buffer)
409     nil))
410
411 (eval-when-compile
412   (defvar from))
413
414 (defun smime-decrypt-region (b e keyfile)
415   "Decrypt S/MIME message in region between B and E with key in KEYFILE.
416 On success, replaces region with decrypted data and return non-nil.
417 Any details (stderr on success, stdout and stderr on error) are left
418 in the buffer specified by `smime-details-buffer'."
419   (smime-new-details-buffer)
420   (let ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
421         CAs (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
422         (tmpfile (smime-make-temp-file "smime")))
423     (if passphrase
424         (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
425     (if (prog1
426             (apply 'smime-call-openssl-region b e
427                    (list buffer tmpfile)
428                    "smime" "-decrypt" "-recip" (expand-file-name keyfile)
429                    (if passphrase
430                        (list "-passin" "env:GNUS_SMIME_PASSPHRASE")))
431           (if passphrase
432               (setenv "GNUS_SMIME_PASSPHRASE" "" t))
433           (with-current-buffer smime-details-buffer
434             (insert-file-contents tmpfile)
435             (delete-file tmpfile)))
436         (progn
437           (delete-region b e)
438           (when (boundp 'from)
439             ;; `from' is dynamically bound in mm-dissect.
440             (insert "From: " from "\n"))
441           (insert-buffer-substring buffer)
442           (kill-buffer buffer)
443           t)
444       (with-current-buffer smime-details-buffer
445         (insert-buffer-substring buffer))
446       (kill-buffer buffer)
447       (delete-region b e)
448       (insert-buffer-substring smime-details-buffer)
449       nil)))
450
451 ;; Verify+Decrypt buffer
452
453 (defun smime-verify-buffer (&optional buffer)
454   "Verify integrity of S/MIME message in BUFFER.
455 Uses current buffer if BUFFER is nil. Returns non-nil on success.
456 Any details (stdout and stderr) are left in the buffer specified by
457 `smime-details-buffer'."
458   (interactive)
459   (with-current-buffer (or buffer (current-buffer))
460     (smime-verify-region (point-min) (point-max))))
461
462 (defun smime-noverify-buffer (&optional buffer)
463   "Verify integrity of S/MIME message in BUFFER.
464 Does NOT verify validity of certificate (only message integrity).
465 Uses current buffer if BUFFER is nil. Returns non-nil on success.
466 Any details (stdout and stderr) are left in the buffer specified by
467 `smime-details-buffer'."
468   (interactive)
469   (with-current-buffer (or buffer (current-buffer))
470     (smime-noverify-region (point-min) (point-max))))
471
472 (defun smime-decrypt-buffer (&optional buffer keyfile)
473   "Decrypt S/MIME message in BUFFER using KEYFILE.
474 Uses current buffer if BUFFER is nil, and query user of KEYFILE if it's nil.
475 On success, replaces data in buffer and return non-nil.
476 Any details (stderr on success, stdout and stderr on error) are left
477 in the buffer specified by `smime-details-buffer'."
478   (interactive)
479   (with-current-buffer (or buffer (current-buffer))
480     (smime-decrypt-region
481      (point-min) (point-max)
482      (expand-file-name
483       (or keyfile
484           (smime-get-key-by-email
485            (completing-read
486             (concat "Decipher using which key? "
487                     (if smime-keys (concat "(default " (caar smime-keys) ") ")
488                       ""))
489             smime-keys nil nil (car-safe (car-safe smime-keys)))))))))
490
491 ;; Various operations
492
493 (defun smime-new-details-buffer ()
494   (with-current-buffer (get-buffer-create smime-details-buffer)
495     (erase-buffer)))
496
497 (defun smime-pkcs7-region (b e)
498   "Convert S/MIME message between points B and E into a PKCS7 message."
499   (smime-new-details-buffer)
500   (when (smime-call-openssl-region b e smime-details-buffer "smime" "-pk7out")
501     (delete-region b e)
502     (insert-buffer-substring smime-details-buffer)
503     t))
504
505 (defun smime-pkcs7-certificates-region (b e)
506   "Extract any certificates enclosed in PKCS7 message between points B and E."
507   (smime-new-details-buffer)
508   (when (smime-call-openssl-region
509          b e smime-details-buffer "pkcs7" "-print_certs" "-text")
510     (delete-region b e)
511     (insert-buffer-substring smime-details-buffer)
512     t))
513
514 (defun smime-pkcs7-email-region (b e)
515   "Get email addresses contained in certificate between points B and E.
516 A string or a list of strings is returned."
517   (smime-new-details-buffer)
518   (when (smime-call-openssl-region
519          b e smime-details-buffer "x509" "-email" "-noout")
520     (delete-region b e)
521     (insert-buffer-substring smime-details-buffer)
522     t))
523
524 ;; Utility functions
525
526 (defun smime-get-certfiles (keyfile keys)
527   (if keys
528       (let ((curkey (car keys))
529             (otherkeys (cdr keys)))
530         (if (string= keyfile (cadr curkey))
531             (caddr curkey)
532           (smime-get-certfiles keyfile otherkeys)))))
533
534 (defun smime-buffer-as-string-region (b e)
535   "Return each line in region between B and E as a list of strings."
536   (save-excursion
537     (goto-char b)
538     (let (res)
539       (while (< (point) e)
540         (let ((str (buffer-substring (point) (point-at-eol))))
541           (unless (string= "" str)
542             (push str res)))
543         (forward-line))
544       res)))
545
546 ;; Find certificates
547
548 (defun smime-mail-to-domain (mailaddr)
549   (if (string-match "@" mailaddr)
550       (replace-match "." 'fixedcase 'literal mailaddr)
551     mailaddr))
552
553 (defun smime-cert-by-dns (mail)
554   "Find certificate via DNS for address MAIL."
555   (let* ((dig-dns-server smime-dns-server)
556          (digbuf (dig-invoke (smime-mail-to-domain mail) "cert" nil nil "+vc"))
557          (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
558          (certrr (with-current-buffer digbuf
559                    (dig-extract-rr (smime-mail-to-domain mail) "cert")))
560          (cert (and certrr (dig-rr-get-pkix-cert certrr))))
561       (if cert
562           (with-current-buffer retbuf
563             (insert "-----BEGIN CERTIFICATE-----\n")
564             (let ((i 0) (len (length cert)))
565               (while (> (- len 64) i)
566                 (insert (substring cert i (+ i 64)) "\n")
567                 (setq i (+ i 64)))
568               (insert (substring cert i len) "\n"))
569             (insert "-----END CERTIFICATE-----\n"))
570         (kill-buffer retbuf)
571         (setq retbuf nil))
572       (kill-buffer digbuf)
573       retbuf))
574
575 (defun smime-cert-by-ldap-1 (mail host)
576   "Get cetificate for MAIL from the ldap server at HOST."
577   (let ((ldapresult (smime-ldap-search (concat "mail=" mail)
578                                        host '("userCertificate") nil))
579         (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
580         cert)
581     (if (> (length ldapresult) 1)
582         (with-current-buffer retbuf
583           (setq cert (base64-encode-string (nth 1 (car (nth 1 ldapresult))) t))
584           (insert "-----BEGIN CERTIFICATE-----\n")
585           (let ((i 0) (len (length cert)))
586             (while (> (- len 64) i)
587               (insert (substring cert i (+ i 64)) "\n")
588               (setq i (+ i 64)))
589             (insert (substring cert i len) "\n"))
590           (insert "-----END CERTIFICATE-----\n"))
591       (kill-buffer retbuf)
592       (setq retbuf nil))
593     retbuf))
594
595 (defun smime-cert-by-ldap (mail)
596   "Find certificate via LDAP for address MAIL."
597   (if smime-ldap-host-list
598       (catch 'certbuf
599         (dolist (host smime-ldap-host-list)
600           (let ((retbuf (smime-cert-by-ldap-1 mail host)))
601             (when retbuf
602               (throw 'certbuf retbuf)))))))
603
604 ;; User interface.
605
606 (defvar smime-buffer "*SMIME*")
607
608 (defvar smime-mode-map nil)
609 (put 'smime-mode 'mode-class 'special)
610
611 (unless smime-mode-map
612   (setq smime-mode-map (make-sparse-keymap))
613   (suppress-keymap smime-mode-map)
614
615   (define-key smime-mode-map "q" 'smime-exit)
616   (define-key smime-mode-map "f" 'smime-certificate-info))
617
618 (defun smime-mode ()
619   "Major mode for browsing, viewing and fetching certificates.
620
621 All normal editing commands are switched off.
622 \\<smime-mode-map>
623
624 The following commands are available:
625
626 \\{smime-mode-map}"
627   (interactive)
628   (kill-all-local-variables)
629   (setq major-mode 'smime-mode)
630   (setq mode-name "SMIME")
631   (setq mode-line-process nil)
632   (use-local-map smime-mode-map)
633   (buffer-disable-undo)
634   (setq truncate-lines t)
635   (setq buffer-read-only t))
636
637 (defun smime-certificate-info (certfile)
638   (interactive "fCertificate file: ")
639   (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
640     (switch-to-buffer buffer)
641     (erase-buffer)
642     (call-process smime-openssl-program nil buffer 'display
643                   "x509" "-in" (expand-file-name certfile) "-text")
644     (fundamental-mode)
645     (set-buffer-modified-p nil)
646     (toggle-read-only t)
647     (goto-char (point-min))))
648
649 (defun smime-draw-buffer ()
650   (with-current-buffer smime-buffer
651     (let (buffer-read-only)
652       (erase-buffer)
653       (insert "\nYour keys:\n")
654       (dolist (key smime-keys)
655         (insert
656          (format "\t\t%s: %s\n" (car key) (cadr key))))
657       (insert "\nTrusted Certificate Authoritys:\n")
658       (insert "\nKnown Certificates:\n"))))
659
660 (defun smime ()
661   "Go to the SMIME buffer."
662   (interactive)
663   (unless (get-buffer smime-buffer)
664     (save-excursion
665       (set-buffer (get-buffer-create smime-buffer))
666       (smime-mode)))
667   (smime-draw-buffer)
668   (switch-to-buffer smime-buffer))
669
670 (defun smime-exit ()
671   "Quit the S/MIME buffer."
672   (interactive)
673   (kill-buffer (current-buffer)))
674
675 ;; Other functions
676
677 (defun smime-get-key-by-email (email)
678   (cadr (assoc email smime-keys)))
679
680 (defun smime-get-key-with-certs-by-email (email)
681   (cdr (assoc email smime-keys)))
682
683 (provide 'smime)
684
685 ;;; arch-tag: e3f9b938-5085-4510-8a11-6625269c9a9e
686 ;;; smime.el ends here