]> code.delx.au - gnu-emacs/blob - lisp/gnus/smime.el
Remove obsolete leading * from defcustom, defface doc strings.
[gnu-emacs] / lisp / gnus / smime.el
1 ;;; smime.el --- S/MIME support library
2
3 ;; Copyright (C) 2000-2016 Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: SMIME X.509 PEM OpenSSL
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This library perform S/MIME operations from within Emacs.
26 ;;
27 ;; Functions for fetching certificates from public repositories are
28 ;; provided, currently from DNS and LDAP.
29 ;;
30 ;; It uses OpenSSL (tested with version 0.9.5a and 0.9.6) for signing,
31 ;; encryption and decryption.
32 ;;
33 ;; Some general knowledge of S/MIME, X.509, PKCS#12, PEM etc is
34 ;; probably required to use this library in any useful way.
35 ;; Especially, don't expect this library to buy security for you. If
36 ;; you don't understand what you are doing, you're as likely to lose
37 ;; security than gain any by using this library.
38 ;;
39 ;; This library is not intended to provide a "raw" API for S/MIME,
40 ;; PKCSx or similar, it's intended to perform common operations
41 ;; done on messages encoded in these formats. The terminology chosen
42 ;; reflect this.
43 ;;
44 ;; The home of this file is in Gnus, but also available from
45 ;; http://josefsson.org/smime.html.
46
47 ;;; Quick introduction:
48
49 ;; Get your S/MIME certificate from VeriSign or someplace. I used
50 ;; Netscape to generate the key and certificate request and stuff, and
51 ;; Netscape can export the key into PKCS#12 format.
52 ;;
53 ;; Enter OpenSSL. To be able to use this library, it need to have the
54 ;; SMIME key readable in PEM format. OpenSSL is used to convert the
55 ;; key:
56 ;;
57 ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
58 ;; ...
59 ;;
60 ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
61 ;; a key.
62 ;;
63 ;; Now you should be able to sign messages! Create a buffer and write
64 ;; something and run M-x smime-sign-buffer RET RET and you should see
65 ;; your message MIME armored and a signature. Encryption, M-x
66 ;; smime-encrypt-buffer, should also work.
67 ;;
68 ;; To be able to verify messages you need to build up trust with
69 ;; someone. Perhaps you trust the CA that issued your certificate, at
70 ;; least I did, so I export it's certificates from my PKCS#12
71 ;; certificate with:
72 ;;
73 ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
74 ;; ...
75 ;;
76 ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
77 ;; CA certificate.
78 ;;
79 ;; You should now be able to sign messages, and even verify messages
80 ;; sent by others that use the same CA as you.
81
82 ;; Bugs:
83 ;;
84 ;; Don't complain that this package doesn't do encrypted PEM files,
85 ;; submit a patch instead. I store my keys in a safe place, so I
86 ;; didn't need the encryption. Also, programming was made easier by
87 ;; that decision. One might think that this even influenced were I
88 ;; store my keys, and one would probably be right. :-)
89 ;;
90 ;; Update: Mathias Herberts sent the patch. However, it uses
91 ;; environment variables to pass the password to OpenSSL, which is
92 ;; slightly insecure. Hence a new todo: use a better -passin method.
93 ;;
94 ;; Cache password for e.g. 1h
95 ;;
96 ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
97
98 ;; begin rant
99 ;;
100 ;; I would include pointers to introductory text on concepts used in
101 ;; this library here, but the material I've read are so horrible I
102 ;; don't want to recommend them.
103 ;;
104 ;; Why can't someone write a simple introduction to all this stuff?
105 ;; Until then, much of this resemble security by obscurity.
106 ;;
107 ;; Also, I'm not going to mention anything about the wonders of
108 ;; cryptopolitics. Oops, I just did.
109 ;;
110 ;; end rant
111
112 ;;; Revision history:
113
114 ;; 2000-06-05 initial version, committed to Gnus CVS contrib/
115 ;; 2000-10-28 retrieve certificates via DNS CERT RRs
116 ;; 2001-10-14 posted to gnu.emacs.sources
117 ;; 2005-02-13 retrieve certificates via LDAP
118
119 ;;; Code:
120
121 (require 'dig)
122
123 (require 'password-cache)
124
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 themselves 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 (i.e., 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.1" ;; No Gnus
228 :group 'smime)
229
230 (defvar smime-details-buffer "*OpenSSL output*")
231
232 (defun smime-ask-passphrase (&optional cache-key)
233 "Asks the passphrase to unlock the secret key.
234 If `cache-key' and `password-cache' is non-nil then cache the
235 password under `cache-key'."
236 (let ((passphrase
237 (password-read-and-add
238 "Passphrase for secret key (RET for no passphrase): " cache-key)))
239 (if (string= passphrase "")
240 nil
241 passphrase)))
242
243 ;; OpenSSL wrappers.
244
245 (defun smime-call-openssl-region (b e buf &rest args)
246 (case (apply 'call-process-region b e smime-openssl-program nil buf nil args)
247 (0 t)
248 (1 (message "OpenSSL: An error occurred parsing the command options.") nil)
249 (2 (message "OpenSSL: One of the input files could not be read.") nil)
250 (3 (message "OpenSSL: An error occurred creating the PKCS#7 file or when reading the MIME message.") nil)
251 (4 (message "OpenSSL: An error occurred decrypting or verifying the message.") nil)
252 (t (error "Unknown OpenSSL exitcode") nil)))
253
254 (defun smime-make-certfiles (certfiles)
255 (if certfiles
256 (append (list "-certfile" (expand-file-name (car certfiles)))
257 (smime-make-certfiles (cdr certfiles)))))
258
259 ;; Sign+encrypt region
260
261 (defun smime-sign-region (b e keyfile)
262 "Sign region with certified key in KEYFILE.
263 If signing fails, the buffer is not modified. Region is assumed to
264 have proper MIME tags. KEYFILE is expected to contain a PEM encoded
265 private key and certificate as its car, and a list of additional
266 certificates to include in its caar. If no additional certificates is
267 included, KEYFILE may be the file containing the PEM encoded private
268 key and certificate itself."
269 (smime-new-details-buffer)
270 (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
271 (keyfile (or (car-safe keyfile) keyfile))
272 (buffer (generate-new-buffer " *smime*"))
273 (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
274 (tmpfile (make-temp-file "smime")))
275 (if passphrase
276 (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
277 (prog1
278 (when (prog1
279 (apply 'smime-call-openssl-region b e (list buffer tmpfile)
280 "smime" "-sign" "-signer" (expand-file-name keyfile)
281 (append
282 (smime-make-certfiles certfiles)
283 (if passphrase
284 (list "-passin" "env:GNUS_SMIME_PASSPHRASE"))))
285 (if passphrase
286 (setenv "GNUS_SMIME_PASSPHRASE" "" t))
287 (with-current-buffer smime-details-buffer
288 (insert-file-contents tmpfile)
289 (delete-file tmpfile)))
290 (delete-region b e)
291 (insert-buffer-substring buffer)
292 (goto-char b)
293 (when (looking-at "^MIME-Version: 1.0$")
294 (delete-region (point) (progn (forward-line 1) (point))))
295 t)
296 (with-current-buffer smime-details-buffer
297 (goto-char (point-max))
298 (insert-buffer-substring buffer))
299 (kill-buffer buffer))))
300
301 (defun smime-encrypt-region (b e certfiles)
302 "Encrypt region for recipients specified in CERTFILES.
303 If encryption fails, the buffer is not modified. Region is assumed to
304 have proper MIME tags. CERTFILES is a list of filenames, each file
305 is expected to contain of a PEM encoded certificate."
306 (smime-new-details-buffer)
307 (let ((buffer (generate-new-buffer " *smime*"))
308 (tmpfile (make-temp-file "smime")))
309 (prog1
310 (when (prog1
311 (apply 'smime-call-openssl-region b e (list buffer tmpfile)
312 "smime" "-encrypt" smime-encrypt-cipher
313 (mapcar 'expand-file-name certfiles))
314 (with-current-buffer smime-details-buffer
315 (insert-file-contents tmpfile)
316 (delete-file tmpfile)))
317 (delete-region b e)
318 (insert-buffer-substring buffer)
319 (goto-char b)
320 (when (looking-at "^MIME-Version: 1.0$")
321 (delete-region (point) (progn (forward-line 1) (point))))
322 t)
323 (with-current-buffer smime-details-buffer
324 (goto-char (point-max))
325 (insert-buffer-substring buffer))
326 (kill-buffer buffer))))
327
328 ;; Sign+encrypt buffer
329
330 (defun smime-sign-buffer (&optional keyfile buffer)
331 "S/MIME sign BUFFER with key in KEYFILE.
332 KEYFILE should contain a PEM encoded key and certificate."
333 (interactive)
334 (with-current-buffer (or buffer (current-buffer))
335 (unless (smime-sign-region
336 (point-min) (point-max)
337 (if keyfile
338 keyfile
339 (smime-get-key-with-certs-by-email
340 (gnus-completing-read
341 "Sign using key"
342 smime-keys nil (car-safe (car-safe smime-keys))))))
343 (error "Signing failed"))))
344
345 (defun smime-encrypt-buffer (&optional certfiles buffer)
346 "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
347 CERTFILES is a list of filenames, each file is expected to consist of
348 a PEM encoded key and certificate. Uses current buffer if BUFFER is
349 nil."
350 (interactive)
351 (with-current-buffer (or buffer (current-buffer))
352 (unless (smime-encrypt-region
353 (point-min) (point-max)
354 (or certfiles
355 (list (read-file-name "Recipient's S/MIME certificate: "
356 smime-certificate-directory nil))))
357 (error "Encryption failed"))))
358
359 ;; Verify+decrypt region
360
361 (defun smime-verify-region (b e)
362 "Verify S/MIME message in region between B and E.
363 Returns non-nil on success.
364 Any details (stdout and stderr) are left in the buffer specified by
365 `smime-details-buffer'."
366 (smime-new-details-buffer)
367 (let ((CAs (append (if smime-CA-file
368 (list "-CAfile"
369 (expand-file-name smime-CA-file)))
370 (if smime-CA-directory
371 (list "-CApath"
372 (expand-file-name smime-CA-directory))))))
373 (unless CAs
374 (error "No CA configured"))
375 (if smime-crl-check
376 (add-to-list 'CAs smime-crl-check))
377 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
378 "smime" "-verify" "-out" "/dev/null" CAs)
379 t
380 (insert-buffer-substring smime-details-buffer)
381 nil)))
382
383 (defun smime-noverify-region (b e)
384 "Verify integrity of S/MIME message in region between B and E.
385 Returns non-nil on success.
386 Any details (stdout and stderr) are left in the buffer specified by
387 `smime-details-buffer'."
388 (smime-new-details-buffer)
389 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
390 "smime" "-verify" "-noverify" "-out" '("/dev/null"))
391 t
392 (insert-buffer-substring smime-details-buffer)
393 nil))
394
395 (defun smime-decrypt-region (b e keyfile &optional from)
396 "Decrypt S/MIME message in region between B and E with key in KEYFILE.
397 Optional FROM specifies sender's mail address.
398 On success, replaces region with decrypted data and return non-nil.
399 Any details (stderr on success, stdout and stderr on error) are left
400 in the buffer specified by `smime-details-buffer'."
401 (smime-new-details-buffer)
402 (let ((buffer (generate-new-buffer " *smime*"))
403 CAs (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
404 (tmpfile (make-temp-file "smime")))
405 (if passphrase
406 (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
407 (if (prog1
408 (apply 'smime-call-openssl-region b e
409 (list buffer tmpfile)
410 "smime" "-decrypt" "-recip" (expand-file-name keyfile)
411 (if passphrase
412 (list "-passin" "env:GNUS_SMIME_PASSPHRASE")))
413 (if passphrase
414 (setenv "GNUS_SMIME_PASSPHRASE" "" t))
415 (with-current-buffer smime-details-buffer
416 (insert-file-contents tmpfile)
417 (delete-file tmpfile)))
418 (progn
419 (delete-region b e)
420 (when from
421 (insert "From: " from "\n"))
422 (insert-buffer-substring buffer)
423 (kill-buffer buffer)
424 t)
425 (with-current-buffer smime-details-buffer
426 (insert-buffer-substring buffer))
427 (kill-buffer buffer)
428 (delete-region b e)
429 (insert-buffer-substring smime-details-buffer)
430 nil)))
431
432 ;; Verify+Decrypt buffer
433
434 (defun smime-verify-buffer (&optional buffer)
435 "Verify integrity of S/MIME message in BUFFER.
436 Uses current buffer if BUFFER is nil. Returns non-nil on success.
437 Any details (stdout and stderr) are left in the buffer specified by
438 `smime-details-buffer'."
439 (interactive)
440 (with-current-buffer (or buffer (current-buffer))
441 (smime-verify-region (point-min) (point-max))))
442
443 (defun smime-noverify-buffer (&optional buffer)
444 "Verify integrity of S/MIME message in BUFFER.
445 Does NOT verify validity of certificate (only message integrity).
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-noverify-region (point-min) (point-max))))
452
453 (defun smime-decrypt-buffer (&optional buffer keyfile)
454 "Decrypt S/MIME message in BUFFER using KEYFILE.
455 Uses current buffer if BUFFER is nil, and query user of KEYFILE if it's nil.
456 On success, replaces data in buffer and return non-nil.
457 Any details (stderr on success, stdout and stderr on error) are left
458 in the buffer specified by `smime-details-buffer'."
459 (interactive)
460 (with-current-buffer (or buffer (current-buffer))
461 (smime-decrypt-region
462 (point-min) (point-max)
463 (expand-file-name
464 (or keyfile
465 (smime-get-key-by-email
466 (gnus-completing-read
467 "Decipher using key"
468 smime-keys nil (car-safe (car-safe smime-keys)))))))))
469
470 ;; Various operations
471
472 (defun smime-new-details-buffer ()
473 (with-current-buffer (get-buffer-create smime-details-buffer)
474 (erase-buffer)))
475
476 (defun smime-pkcs7-region (b e)
477 "Convert S/MIME message between points B and E into a PKCS7 message."
478 (smime-new-details-buffer)
479 (when (smime-call-openssl-region b e smime-details-buffer "smime" "-pk7out")
480 (delete-region b e)
481 (insert-buffer-substring smime-details-buffer)
482 t))
483
484 (defun smime-pkcs7-certificates-region (b e)
485 "Extract any certificates enclosed in PKCS7 message between points B and E."
486 (smime-new-details-buffer)
487 (when (smime-call-openssl-region
488 b e smime-details-buffer "pkcs7" "-print_certs" "-text")
489 (delete-region b e)
490 (insert-buffer-substring smime-details-buffer)
491 t))
492
493 (defun smime-pkcs7-email-region (b e)
494 "Get email addresses contained in certificate between points B and E.
495 A string or a list of strings is returned."
496 (smime-new-details-buffer)
497 (when (smime-call-openssl-region
498 b e smime-details-buffer "x509" "-email" "-noout")
499 (delete-region b e)
500 (insert-buffer-substring smime-details-buffer)
501 t))
502
503 ;; Utility functions
504
505 (defun smime-get-certfiles (keyfile keys)
506 (if keys
507 (let ((curkey (car keys))
508 (otherkeys (cdr keys)))
509 (if (string= keyfile (cadr curkey))
510 (caddr curkey)
511 (smime-get-certfiles keyfile otherkeys)))))
512
513 (defun smime-buffer-as-string-region (b e)
514 "Return each line in region between B and E as a list of strings."
515 (save-excursion
516 (goto-char b)
517 (let (res)
518 (while (< (point) e)
519 (let ((str (buffer-substring (point) (point-at-eol))))
520 (unless (string= "" str)
521 (push str res)))
522 (forward-line))
523 res)))
524
525 ;; Find certificates
526
527 (defun smime-mail-to-domain (mailaddr)
528 (if (string-match "@" mailaddr)
529 (replace-match "." 'fixedcase 'literal mailaddr)
530 mailaddr))
531
532 (defun smime-cert-by-dns (mail)
533 "Find certificate via DNS for address MAIL."
534 (let* ((dig-dns-server smime-dns-server)
535 (digbuf (dig-invoke (smime-mail-to-domain mail) "cert" nil nil "+vc"))
536 (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
537 (certrr (with-current-buffer digbuf
538 (dig-extract-rr (smime-mail-to-domain mail) "cert")))
539 (cert (and certrr (dig-rr-get-pkix-cert certrr))))
540 (if cert
541 (with-current-buffer retbuf
542 (insert "-----BEGIN CERTIFICATE-----\n")
543 (let ((i 0) (len (length cert)))
544 (while (> (- len 64) i)
545 (insert (substring cert i (+ i 64)) "\n")
546 (setq i (+ i 64)))
547 (insert (substring cert i len) "\n"))
548 (insert "-----END CERTIFICATE-----\n"))
549 (kill-buffer retbuf)
550 (setq retbuf nil))
551 (kill-buffer digbuf)
552 retbuf))
553
554 (declare-function ldap-search "ldap"
555 (filter &optional host attributes attrsonly withdn))
556
557 (defun smime-cert-by-ldap-1 (mail host)
558 "Get certificate for MAIL from the ldap server at HOST."
559 (let ((ldapresult
560 (funcall
561 (progn
562 (require 'ldap)
563 'ldap-search)
564 (concat "mail=" mail)
565 host '("userCertificate") nil))
566 (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
567 cert)
568 (if (and (>= (length ldapresult) 1)
569 (> (length (cadaar ldapresult)) 0))
570 (with-current-buffer retbuf
571 ;; Certificates on LDAP servers _should_ be in DER format,
572 ;; but there are some servers out there that distributes the
573 ;; certificates in PEM format (with or without
574 ;; header/footer) so we try to handle them anyway.
575 (if (or (string= (substring (cadaar ldapresult) 0 27)
576 "-----BEGIN CERTIFICATE-----")
577 (string= (substring (cadaar ldapresult) 0 3)
578 "MII"))
579 (setq cert
580 (replace-regexp-in-string
581 (concat "\\(\n\\|\r\\|-----BEGIN CERTIFICATE-----\\|"
582 "-----END CERTIFICATE-----\\)")
583 ""
584 (cadaar ldapresult) nil t))
585 (setq cert (base64-encode-string (cadaar ldapresult) t)))
586 (insert "-----BEGIN CERTIFICATE-----\n")
587 (let ((i 0) (len (length cert)))
588 (while (> (- len 64) i)
589 (insert (substring cert i (+ i 64)) "\n")
590 (setq i (+ i 64)))
591 (insert (substring cert i len) "\n"))
592 (insert "-----END CERTIFICATE-----\n"))
593 (kill-buffer retbuf)
594 (setq retbuf nil))
595 retbuf))
596
597 (defun smime-cert-by-ldap (mail)
598 "Find certificate via LDAP for address MAIL."
599 (if smime-ldap-host-list
600 (catch 'certbuf
601 (dolist (host smime-ldap-host-list)
602 (let ((retbuf (smime-cert-by-ldap-1 mail host)))
603 (when retbuf
604 (throw 'certbuf retbuf)))))))
605
606 ;; User interface.
607
608 (defvar smime-buffer "*SMIME*")
609
610 (defvar smime-mode-map
611 (let ((map (make-sparse-keymap)))
612 (suppress-keymap map)
613 (define-key map "q" 'smime-exit)
614 (define-key map "f" 'smime-certificate-info)
615 map))
616
617 (autoload 'gnus-completing-read "gnus-util")
618
619 (put 'smime-mode 'mode-class 'special)
620 (define-derived-mode smime-mode fundamental-mode ;special-mode
621 "SMIME"
622 "Major mode for browsing, viewing and fetching certificates.
623
624 All normal editing commands are switched off.
625 \\<smime-mode-map>
626
627 The following commands are available:
628
629 \\{smime-mode-map}"
630 (setq mode-line-process nil)
631 (buffer-disable-undo)
632 (setq truncate-lines t)
633 (setq buffer-read-only t))
634
635 (defun smime-certificate-info (certfile)
636 (interactive "fCertificate file: ")
637 (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
638 (switch-to-buffer buffer)
639 (erase-buffer)
640 (call-process smime-openssl-program nil buffer 'display
641 "x509" "-in" (expand-file-name certfile) "-text")
642 (fundamental-mode)
643 (set-buffer-modified-p nil)
644 (setq buffer-read-only t)
645 (goto-char (point-min))))
646
647 (defun smime-draw-buffer ()
648 (with-current-buffer smime-buffer
649 (let (buffer-read-only)
650 (erase-buffer)
651 (insert "\nYour keys:\n")
652 (dolist (key smime-keys)
653 (insert
654 (format "\t\t%s: %s\n" (car key) (cadr key))))
655 (insert "\nTrusted Certificate Authorities:\n")
656 (insert "\nKnown Certificates:\n"))))
657
658 (defun smime ()
659 "Go to the SMIME buffer."
660 (interactive)
661 (unless (get-buffer smime-buffer)
662 (with-current-buffer (get-buffer-create smime-buffer)
663 (smime-mode)))
664 (smime-draw-buffer)
665 (switch-to-buffer smime-buffer))
666
667 (defun smime-exit ()
668 "Quit the S/MIME buffer."
669 (interactive)
670 (kill-buffer (current-buffer)))
671
672 ;; Other functions
673
674 (defun smime-get-key-by-email (email)
675 (cadr (assoc email smime-keys)))
676
677 (defun smime-get-key-with-certs-by-email (email)
678 (cdr (assoc email smime-keys)))
679
680 (provide 'smime)
681
682 ;;; smime.el ends here