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