* gnus-art.el (gnus-copy-article-ignored-headers): Update :version.
[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
130 (defcustom smime-keys nil
131   "*Map mail addresses to a file containing Certificate (and private key).
132 The file is assumed to be in PEM format. You can also associate additional
133 certificates to be sent with every message to each address."
134   :type '(repeat (list (string :tag "Mail address")
135                        (file :tag "File name")
136                        (repeat :tag "Additional certificate files"
137                                (file :tag "File name"))))
138   :group 'smime)
139
140 (defcustom smime-CA-directory nil
141   "*Directory containing certificates for CAs you trust.
142 Directory should contain files (in PEM format) named to the X.509
143 hash of the certificate.  This can be done using OpenSSL such as:
144
145 $ ln -s ca.pem `openssl x509 -noout -hash -in ca.pem`.0
146
147 where `ca.pem' is the file containing a PEM encoded X.509 CA
148 certificate."
149   :type '(choice (const :tag "none" nil)
150                  directory)
151   :group 'smime)
152
153 (defcustom smime-CA-file nil
154   "*Files containing certificates for CAs you trust.
155 File should contain certificates in PEM format."
156   :version "22.1"
157   :type '(choice (const :tag "none" nil)
158                  file)
159   :group 'smime)
160
161 (defcustom smime-certificate-directory "~/Mail/certs/"
162   "*Directory containing other people's certificates.
163 It should contain files named to the X.509 hash of the certificate,
164 and the files themself should be in PEM format."
165 ;The S/MIME library provide simple functionality for fetching
166 ;certificates into this directory, so there is no need to populate it
167 ;manually.
168   :type 'directory
169   :group 'smime)
170
171 (defcustom smime-openssl-program
172   (and (condition-case ()
173            (eq 0 (call-process "openssl" nil nil nil "version"))
174          (error nil))
175        "openssl")
176   "*Name of OpenSSL binary."
177   :type 'string
178   :group 'smime)
179
180 ;; OpenSSL option to select the encryption cipher
181
182 (defcustom smime-encrypt-cipher "-des3"
183   "*Cipher algorithm used for encryption."
184   :version "22.1"
185   :type '(choice (const :tag "Triple DES" "-des3")
186                  (const :tag "DES"  "-des")
187                  (const :tag "RC2 40 bits" "-rc2-40")
188                  (const :tag "RC2 64 bits" "-rc2-64")
189                  (const :tag "RC2 128 bits" "-rc2-128"))
190   :group 'smime)
191
192 (defcustom smime-crl-check nil
193   "*Check revocation status of signers certificate using CRLs.
194 Enabling this will have OpenSSL check the signers certificate
195 against a certificate revocation list (CRL).
196
197 For this to work the CRL must be up-to-date and since they are
198 normally updated quite often (ie. several times a day) you
199 probably need some tool to keep them up-to-date. Unfortunately
200 Gnus cannot do this for you.
201
202 The CRL should either be appended (in PEM format) to your
203 `smime-CA-file' or be located in a file (also in PEM format) in
204 your `smime-certificate-directory' named to the X.509 hash of the
205 certificate with .r0 as file name extension.
206
207 At least OpenSSL version 0.9.7 is required for this to work."
208   :type '(choice (const :tag "No check" nil)
209                  (const :tag "Check certificate" "-crl_check")
210                  (const :tag "Check certificate chain" "-crl_check_all"))
211   :group 'smime)
212
213 (defcustom smime-dns-server nil
214   "*DNS server to query certificates from.
215 If nil, use system defaults."
216   :version "22.1"
217   :type '(choice (const :tag "System defaults")
218                  string)
219   :group 'smime)
220
221 (defcustom smime-ldap-host-list nil
222   "A list of LDAP hosts with S/MIME user certificates.
223 If needed search base, binddn, passwd, etc. for the LDAP host
224 must be set in `ldap-host-parameters-alist'."
225   :type '(repeat (string :tag "Host name"))
226   :version "23.0" ;; No Gnus
227   :group 'smime)
228
229 (defvar smime-details-buffer "*OpenSSL output*")
230
231 ;; Use mm-util?
232 (eval-and-compile
233   (defalias 'smime-make-temp-file
234     (if (fboundp 'make-temp-file)
235         'make-temp-file
236       (lambda (prefix &optional dir-flag) ;; Simple implementation
237         (expand-file-name
238          (make-temp-name prefix)
239          (if (fboundp 'temp-directory)
240              (temp-directory)
241            temporary-file-directory))))))
242
243 ;; Password dialog function
244
245 (defun smime-ask-passphrase (&optional cache-key)
246   "Asks the passphrase to unlock the secret key.
247 If `cache-key' and `password-cache' is non-nil then cache the
248 password under `cache-key'."
249   (let ((passphrase
250          (password-read-and-add
251           "Passphrase for secret key (RET for no passphrase): " cache-key)))
252     (if (string= passphrase "")
253         nil
254       passphrase)))
255
256 ;; OpenSSL wrappers.
257
258 (defun smime-call-openssl-region (b e buf &rest args)
259   (case (apply 'call-process-region b e smime-openssl-program nil buf nil args)
260     (0 t)
261     (1 (message "OpenSSL: An error occurred parsing the command options.") nil)
262     (2 (message "OpenSSL: One of the input files could not be read.") nil)
263     (3 (message "OpenSSL: An error occurred creating the PKCS#7 file or when reading the MIME message.") nil)
264     (4 (message "OpenSSL: An error occurred decrypting or verifying the message.") nil)
265     (t (error "Unknown OpenSSL exitcode") nil)))
266
267 (defun smime-make-certfiles (certfiles)
268   (if certfiles
269       (append (list "-certfile" (expand-file-name (car certfiles)))
270               (smime-make-certfiles (cdr certfiles)))))
271
272 ;; Sign+encrypt region
273
274 (defun smime-sign-region (b e keyfile)
275   "Sign region with certified key in KEYFILE.
276 If signing fails, the buffer is not modified.  Region is assumed to
277 have proper MIME tags.  KEYFILE is expected to contain a PEM encoded
278 private key and certificate as its car, and a list of additional
279 certificates to include in its caar.  If no additional certificates is
280 included, KEYFILE may be the file containing the PEM encoded private
281 key and certificate itself."
282   (smime-new-details-buffer)
283   (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
284          (keyfile (or (car-safe keyfile) keyfile))
285          (buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
286          (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
287          (tmpfile (smime-make-temp-file "smime")))
288     (if passphrase
289         (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
290     (prog1
291         (when (prog1
292                   (apply 'smime-call-openssl-region b e (list buffer tmpfile)
293                          "smime" "-sign" "-signer" (expand-file-name keyfile)
294                          (append
295                           (smime-make-certfiles certfiles)
296                           (if passphrase
297                               (list "-passin" "env:GNUS_SMIME_PASSPHRASE"))))
298                 (if passphrase
299                     (setenv "GNUS_SMIME_PASSPHRASE" "" t))
300                 (with-current-buffer smime-details-buffer
301                   (insert-file-contents tmpfile)
302                   (delete-file tmpfile)))
303           (delete-region b e)
304           (insert-buffer-substring buffer)
305           (goto-char b)
306           (when (looking-at "^MIME-Version: 1.0$")
307             (delete-region (point) (progn (forward-line 1) (point))))
308           t)
309       (with-current-buffer smime-details-buffer
310         (goto-char (point-max))
311         (insert-buffer-substring buffer))
312       (kill-buffer buffer))))
313
314 (defun smime-encrypt-region (b e certfiles)
315   "Encrypt region for recipients specified in CERTFILES.
316 If encryption fails, the buffer is not modified.  Region is assumed to
317 have proper MIME tags.  CERTFILES is a list of filenames, each file
318 is expected to contain of a PEM encoded certificate."
319   (smime-new-details-buffer)
320   (let ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
321         (tmpfile (smime-make-temp-file "smime")))
322     (prog1
323         (when (prog1
324                   (apply 'smime-call-openssl-region b e (list buffer tmpfile)
325                          "smime" "-encrypt" smime-encrypt-cipher
326                          (mapcar 'expand-file-name certfiles))
327                 (with-current-buffer smime-details-buffer
328                   (insert-file-contents tmpfile)
329                   (delete-file tmpfile)))
330           (delete-region b e)
331           (insert-buffer-substring buffer)
332           (goto-char b)
333           (when (looking-at "^MIME-Version: 1.0$")
334             (delete-region (point) (progn (forward-line 1) (point))))
335           t)
336       (with-current-buffer smime-details-buffer
337         (goto-char (point-max))
338         (insert-buffer-substring buffer))
339       (kill-buffer buffer))))
340
341 ;; Sign+encrypt buffer
342
343 (defun smime-sign-buffer (&optional keyfile buffer)
344   "S/MIME sign BUFFER with key in KEYFILE.
345 KEYFILE should contain a PEM encoded key and certificate."
346   (interactive)
347   (with-current-buffer (or buffer (current-buffer))
348     (unless (smime-sign-region
349              (point-min) (point-max)
350              (if keyfile
351                  keyfile
352                (smime-get-key-with-certs-by-email
353                 (completing-read
354                  (concat "Sign using which key? "
355                          (if smime-keys (concat "(default " (caar smime-keys) ") ")
356                            ""))
357                  smime-keys nil nil (car-safe (car-safe smime-keys))))))
358       (error "Signing failed"))))
359
360 (defun smime-encrypt-buffer (&optional certfiles buffer)
361   "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
362 CERTFILES is a list of filenames, each file is expected to consist of
363 a PEM encoded key and certificate.  Uses current buffer if BUFFER is
364 nil."
365   (interactive)
366   (with-current-buffer (or buffer (current-buffer))
367     (unless (smime-encrypt-region
368              (point-min) (point-max)
369              (or certfiles
370                  (list (read-file-name "Recipient's S/MIME certificate: "
371                                        smime-certificate-directory nil))))
372       (error "Encryption failed"))))
373
374 ;; Verify+decrypt region
375
376 (defun smime-verify-region (b e)
377   "Verify S/MIME message in region between B and E.
378 Returns non-nil on success.
379 Any details (stdout and stderr) are left in the buffer specified by
380 `smime-details-buffer'."
381   (smime-new-details-buffer)
382   (let ((CAs (append (if smime-CA-file
383                          (list "-CAfile"
384                                (expand-file-name smime-CA-file)))
385                      (if smime-CA-directory
386                          (list "-CApath"
387                                (expand-file-name smime-CA-directory))))))
388     (unless CAs
389       (error "No CA configured"))
390     (if smime-crl-check
391         (add-to-list 'CAs smime-crl-check))
392     (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
393                "smime" "-verify" "-out" "/dev/null" CAs)
394         t
395       (insert-buffer-substring smime-details-buffer)
396       nil)))
397
398 (defun smime-noverify-region (b e)
399   "Verify integrity of S/MIME message in region between B and E.
400 Returns non-nil on success.
401 Any details (stdout and stderr) are left in the buffer specified by
402 `smime-details-buffer'."
403   (smime-new-details-buffer)
404   (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
405              "smime" "-verify" "-noverify" "-out" '("/dev/null"))
406       t
407     (insert-buffer-substring smime-details-buffer)
408     nil))
409
410 (eval-when-compile
411   (defvar from))
412
413 (defun smime-decrypt-region (b e keyfile)
414   "Decrypt S/MIME message in region between B and E with key in KEYFILE.
415 On success, replaces region with decrypted data and return non-nil.
416 Any details (stderr on success, stdout and stderr on error) are left
417 in the buffer specified by `smime-details-buffer'."
418   (smime-new-details-buffer)
419   (let ((buffer (generate-new-buffer (generate-new-buffer-name " *smime*")))
420         CAs (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
421         (tmpfile (smime-make-temp-file "smime")))
422     (if passphrase
423         (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
424     (if (prog1
425             (apply 'smime-call-openssl-region b e
426                    (list buffer tmpfile)
427                    "smime" "-decrypt" "-recip" (expand-file-name keyfile)
428                    (if passphrase
429                        (list "-passin" "env:GNUS_SMIME_PASSPHRASE")))
430           (if passphrase
431               (setenv "GNUS_SMIME_PASSPHRASE" "" t))
432           (with-current-buffer smime-details-buffer
433             (insert-file-contents tmpfile)
434             (delete-file tmpfile)))
435         (progn
436           (delete-region b e)
437           (when (boundp 'from)
438             ;; `from' is dynamically bound in mm-dissect.
439             (insert "From: " from "\n"))
440           (insert-buffer-substring buffer)
441           (kill-buffer buffer)
442           t)
443       (with-current-buffer smime-details-buffer
444         (insert-buffer-substring buffer))
445       (kill-buffer buffer)
446       (delete-region b e)
447       (insert-buffer-substring smime-details-buffer)
448       nil)))
449
450 ;; Verify+Decrypt buffer
451
452 (defun smime-verify-buffer (&optional buffer)
453   "Verify integrity of S/MIME message in BUFFER.
454 Uses current buffer if BUFFER is nil. Returns non-nil on success.
455 Any details (stdout and stderr) are left in the buffer specified by
456 `smime-details-buffer'."
457   (interactive)
458   (with-current-buffer (or buffer (current-buffer))
459     (smime-verify-region (point-min) (point-max))))
460
461 (defun smime-noverify-buffer (&optional buffer)
462   "Verify integrity of S/MIME message in BUFFER.
463 Does NOT verify validity of certificate (only message integrity).
464 Uses current buffer if BUFFER is nil. Returns non-nil on success.
465 Any details (stdout and stderr) are left in the buffer specified by
466 `smime-details-buffer'."
467   (interactive)
468   (with-current-buffer (or buffer (current-buffer))
469     (smime-noverify-region (point-min) (point-max))))
470
471 (defun smime-decrypt-buffer (&optional buffer keyfile)
472   "Decrypt S/MIME message in BUFFER using KEYFILE.
473 Uses current buffer if BUFFER is nil, and query user of KEYFILE if it's nil.
474 On success, replaces data in buffer and return non-nil.
475 Any details (stderr on success, stdout and stderr on error) are left
476 in the buffer specified by `smime-details-buffer'."
477   (interactive)
478   (with-current-buffer (or buffer (current-buffer))
479     (smime-decrypt-region
480      (point-min) (point-max)
481      (expand-file-name
482       (or keyfile
483           (smime-get-key-by-email
484            (completing-read
485             (concat "Decipher using which key? "
486                     (if smime-keys (concat "(default " (caar smime-keys) ") ")
487                       ""))
488             smime-keys nil nil (car-safe (car-safe smime-keys)))))))))
489
490 ;; Various operations
491
492 (defun smime-new-details-buffer ()
493   (with-current-buffer (get-buffer-create smime-details-buffer)
494     (erase-buffer)))
495
496 (defun smime-pkcs7-region (b e)
497   "Convert S/MIME message between points B and E into a PKCS7 message."
498   (smime-new-details-buffer)
499   (when (smime-call-openssl-region b e smime-details-buffer "smime" "-pk7out")
500     (delete-region b e)
501     (insert-buffer-substring smime-details-buffer)
502     t))
503
504 (defun smime-pkcs7-certificates-region (b e)
505   "Extract any certificates enclosed in PKCS7 message between points B and E."
506   (smime-new-details-buffer)
507   (when (smime-call-openssl-region
508          b e smime-details-buffer "pkcs7" "-print_certs" "-text")
509     (delete-region b e)
510     (insert-buffer-substring smime-details-buffer)
511     t))
512
513 (defun smime-pkcs7-email-region (b e)
514   "Get email addresses contained in certificate between points B and E.
515 A string or a list of strings is returned."
516   (smime-new-details-buffer)
517   (when (smime-call-openssl-region
518          b e smime-details-buffer "x509" "-email" "-noout")
519     (delete-region b e)
520     (insert-buffer-substring smime-details-buffer)
521     t))
522
523 ;; Utility functions
524
525 (defun smime-get-certfiles (keyfile keys)
526   (if keys
527       (let ((curkey (car keys))
528             (otherkeys (cdr keys)))
529         (if (string= keyfile (cadr curkey))
530             (caddr curkey)
531           (smime-get-certfiles keyfile otherkeys)))))
532
533 (defun smime-buffer-as-string-region (b e)
534   "Return each line in region between B and E as a list of strings."
535   (save-excursion
536     (goto-char b)
537     (let (res)
538       (while (< (point) e)
539         (let ((str (buffer-substring (point) (point-at-eol))))
540           (unless (string= "" str)
541             (push str res)))
542         (forward-line))
543       res)))
544
545 ;; Find certificates
546
547 (defun smime-mail-to-domain (mailaddr)
548   (if (string-match "@" mailaddr)
549       (replace-match "." 'fixedcase 'literal mailaddr)
550     mailaddr))
551
552 (defun smime-cert-by-dns (mail)
553   "Find certificate via DNS for address MAIL."
554   (let* ((dig-dns-server smime-dns-server)
555          (digbuf (dig-invoke (smime-mail-to-domain mail) "cert" nil nil "+vc"))
556          (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
557          (certrr (with-current-buffer digbuf
558                    (dig-extract-rr (smime-mail-to-domain mail) "cert")))
559          (cert (and certrr (dig-rr-get-pkix-cert certrr))))
560       (if cert
561           (with-current-buffer retbuf
562             (insert "-----BEGIN CERTIFICATE-----\n")
563             (let ((i 0) (len (length cert)))
564               (while (> (- len 64) i)
565                 (insert (substring cert i (+ i 64)) "\n")
566                 (setq i (+ i 64)))
567               (insert (substring cert i len) "\n"))
568             (insert "-----END CERTIFICATE-----\n"))
569         (kill-buffer retbuf)
570         (setq retbuf nil))
571       (kill-buffer digbuf)
572       retbuf))
573
574 (defun smime-cert-by-ldap-1 (mail host)
575   "Get cetificate for MAIL from the ldap server at HOST."
576   (let ((ldapresult (smime-ldap-search (concat "mail=" mail)
577                                        host '("userCertificate") nil))
578         (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
579         cert)
580     (if (> (length ldapresult) 1)
581         (with-current-buffer retbuf
582           (setq cert (base64-encode-string (nth 1 (car (nth 1 ldapresult))) t))
583           (insert "-----BEGIN CERTIFICATE-----\n")
584           (let ((i 0) (len (length cert)))
585             (while (> (- len 64) i)
586               (insert (substring cert i (+ i 64)) "\n")
587               (setq i (+ i 64)))
588             (insert (substring cert i len) "\n"))
589           (insert "-----END CERTIFICATE-----\n"))
590       (kill-buffer retbuf)
591       (setq retbuf nil))
592     retbuf))
593
594 (defun smime-cert-by-ldap (mail)
595   "Find certificate via LDAP for address MAIL."
596   (if smime-ldap-host-list
597       (catch 'certbuf
598         (dolist (host smime-ldap-host-list)
599           (let ((retbuf (smime-cert-by-ldap-1 mail host)))
600             (when retbuf
601               (throw 'certbuf retbuf)))))))
602
603 ;; User interface.
604
605 (defvar smime-buffer "*SMIME*")
606
607 (defvar smime-mode-map nil)
608 (put 'smime-mode 'mode-class 'special)
609
610 (unless smime-mode-map
611   (setq smime-mode-map (make-sparse-keymap))
612   (suppress-keymap smime-mode-map)
613
614   (define-key smime-mode-map "q" 'smime-exit)
615   (define-key smime-mode-map "f" 'smime-certificate-info))
616
617 (defun smime-mode ()
618   "Major mode for browsing, viewing and fetching certificates.
619
620 All normal editing commands are switched off.
621 \\<smime-mode-map>
622
623 The following commands are available:
624
625 \\{smime-mode-map}"
626   (interactive)
627   (kill-all-local-variables)
628   (setq major-mode 'smime-mode)
629   (setq mode-name "SMIME")
630   (setq mode-line-process nil)
631   (use-local-map smime-mode-map)
632   (buffer-disable-undo)
633   (setq truncate-lines t)
634   (setq buffer-read-only t))
635
636 (defun smime-certificate-info (certfile)
637   (interactive "fCertificate file: ")
638   (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
639     (switch-to-buffer buffer)
640     (erase-buffer)
641     (call-process smime-openssl-program nil buffer 'display
642                   "x509" "-in" (expand-file-name certfile) "-text")
643     (fundamental-mode)
644     (set-buffer-modified-p nil)
645     (toggle-read-only t)
646     (goto-char (point-min))))
647
648 (defun smime-draw-buffer ()
649   (with-current-buffer smime-buffer
650     (let (buffer-read-only)
651       (erase-buffer)
652       (insert "\nYour keys:\n")
653       (dolist (key smime-keys)
654         (insert
655          (format "\t\t%s: %s\n" (car key) (cadr key))))
656       (insert "\nTrusted Certificate Authoritys:\n")
657       (insert "\nKnown Certificates:\n"))))
658
659 (defun smime ()
660   "Go to the SMIME buffer."
661   (interactive)
662   (unless (get-buffer smime-buffer)
663     (save-excursion
664       (set-buffer (get-buffer-create smime-buffer))
665       (smime-mode)))
666   (smime-draw-buffer)
667   (switch-to-buffer smime-buffer))
668
669 (defun smime-exit ()
670   "Quit the S/MIME buffer."
671   (interactive)
672   (kill-buffer (current-buffer)))
673
674 ;; Other functions
675
676 (defun smime-get-key-by-email (email)
677   (cadr (assoc email smime-keys)))
678
679 (defun smime-get-key-with-certs-by-email (email)
680   (cdr (assoc email smime-keys)))
681
682 (provide 'smime)
683
684 ;;; arch-tag: e3f9b938-5085-4510-8a11-6625269c9a9e
685 ;;; smime.el ends here