Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / smime.el
blobab2a5b0f81334f40f5c5db4a7962e837fe1c1c41
1 ;;; smime.el --- S/MIME support library -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000-2018 Free Software Foundation, Inc.
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: SMIME X.509 PEM OpenSSL
8 ;; This file is part of GNU Emacs.
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.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library perform S/MIME operations from within Emacs.
27 ;; Functions for fetching certificates from public repositories are
28 ;; provided, currently from DNS and LDAP.
30 ;; It uses OpenSSL (tested with version 0.9.5a and 0.9.6) for signing,
31 ;; encryption and decryption.
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.
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.
44 ;; The home of this file is in Gnus, but also available from
45 ;; http://josefsson.org/smime.html.
47 ;;; Quick introduction:
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.
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:
57 ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
58 ;; ...
60 ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
61 ;; a key.
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.
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 its certificates from my PKCS#12
71 ;; certificate with:
73 ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
74 ;; ...
76 ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
77 ;; CA certificate.
79 ;; You should now be able to sign messages, and even verify messages
80 ;; sent by others that use the same CA as you.
82 ;; Bugs:
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. :-)
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.
94 ;; Cache password for e.g. 1h
96 ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
98 ;; begin rant
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.
104 ;; Why can't someone write a simple introduction to all this stuff?
105 ;; Until then, much of this resemble security by obscurity.
107 ;; Also, I'm not going to mention anything about the wonders of
108 ;; cryptopolitics. Oops, I just did.
110 ;; end rant
112 ;;; Revision history:
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
119 ;;; Code:
121 (require 'dig)
123 (require 'password-cache)
125 (eval-when-compile (require 'cl-lib))
127 (defgroup smime nil
128 "S/MIME configuration."
129 :group 'mime)
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)
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:
146 $ ln -s ca.pem \\=`openssl x509 -noout -hash -in ca.pem\\=`.0
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)
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)
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)
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)
181 ;; OpenSSL option to select the encryption cipher
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)
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).
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.
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.
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)
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)
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)
230 (defvar smime-details-buffer "*OpenSSL output*")
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
238 "Passphrase for secret key (RET for no passphrase): " cache-key)))
239 (if (string= passphrase "")
241 ;; FIXME test passphrase works before caching it.
242 (and passphrase cache-key (password-cache-add cache-key passphrase))
243 passphrase)))
245 ;; OpenSSL wrappers.
247 (defun smime-call-openssl-region (b e buf &rest args)
248 (pcase (apply #'call-process-region b e smime-openssl-program nil buf nil args)
249 (0 t)
250 (1 (message "OpenSSL: An error occurred parsing the command options.") nil)
251 (2 (message "OpenSSL: One of the input files could not be read.") nil)
252 (3 (message "OpenSSL: An error occurred creating the PKCS#7 file or when reading the MIME message.") nil)
253 (4 (message "OpenSSL: An error occurred decrypting or verifying the message.") nil)
254 (_ (error "Unknown OpenSSL exitcode"))))
256 (defun smime-make-certfiles (certfiles)
257 (if certfiles
258 (append (list "-certfile" (expand-file-name (car certfiles)))
259 (smime-make-certfiles (cdr certfiles)))))
261 ;; Sign+encrypt region
263 (defun smime-sign-region (b e keyfile)
264 "Sign region with certified key in KEYFILE.
265 If signing fails, the buffer is not modified. Region is assumed to
266 have proper MIME tags. KEYFILE is expected to contain a PEM encoded
267 private key and certificate as its car, and a list of additional
268 certificates to include in its caar. If no additional certificates is
269 included, KEYFILE may be the file containing the PEM encoded private
270 key and certificate itself."
271 (smime-new-details-buffer)
272 (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
273 (keyfile (or (car-safe keyfile) keyfile))
274 (buffer (generate-new-buffer " *smime*"))
275 (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
276 (tmpfile (make-temp-file "smime")))
277 (if passphrase
278 (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
279 (prog1
280 (when (prog1
281 (apply 'smime-call-openssl-region b e (list buffer tmpfile)
282 "smime" "-sign" "-signer" (expand-file-name keyfile)
283 (append
284 (smime-make-certfiles certfiles)
285 (if passphrase
286 (list "-passin" "env:GNUS_SMIME_PASSPHRASE"))))
287 (if passphrase
288 (setenv "GNUS_SMIME_PASSPHRASE" "" t))
289 (with-current-buffer smime-details-buffer
290 (insert-file-contents tmpfile)
291 (delete-file tmpfile)))
292 (delete-region b e)
293 (insert-buffer-substring buffer)
294 (goto-char b)
295 (when (looking-at "^MIME-Version: 1.0$")
296 (delete-region (point) (progn (forward-line 1) (point))))
298 (with-current-buffer smime-details-buffer
299 (goto-char (point-max))
300 (insert-buffer-substring buffer))
301 (kill-buffer buffer))))
303 (defun smime-encrypt-region (b e certfiles)
304 "Encrypt region for recipients specified in CERTFILES.
305 If encryption fails, the buffer is not modified. Region is assumed to
306 have proper MIME tags. CERTFILES is a list of filenames, each file
307 is expected to contain of a PEM encoded certificate."
308 (smime-new-details-buffer)
309 (let ((buffer (generate-new-buffer " *smime*"))
310 (tmpfile (make-temp-file "smime")))
311 (prog1
312 (when (prog1
313 (apply 'smime-call-openssl-region b e (list buffer tmpfile)
314 "smime" "-encrypt" smime-encrypt-cipher
315 (mapcar 'expand-file-name certfiles))
316 (with-current-buffer smime-details-buffer
317 (insert-file-contents tmpfile)
318 (delete-file tmpfile)))
319 (delete-region b e)
320 (insert-buffer-substring buffer)
321 (goto-char b)
322 (when (looking-at "^MIME-Version: 1.0$")
323 (delete-region (point) (progn (forward-line 1) (point))))
325 (with-current-buffer smime-details-buffer
326 (goto-char (point-max))
327 (insert-buffer-substring buffer))
328 (kill-buffer buffer))))
330 ;; Sign+encrypt buffer
332 (defun smime-sign-buffer (&optional keyfile buffer)
333 "S/MIME sign BUFFER with key in KEYFILE.
334 KEYFILE should contain a PEM encoded key and certificate."
335 (interactive)
336 (with-current-buffer (or buffer (current-buffer))
337 (unless (smime-sign-region
338 (point-min) (point-max)
339 (if keyfile
340 keyfile
341 (smime-get-key-with-certs-by-email
342 (gnus-completing-read
343 "Sign using key"
344 smime-keys nil (car-safe (car-safe smime-keys))))))
345 (error "Signing failed"))))
347 (defun smime-encrypt-buffer (&optional certfiles buffer)
348 "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
349 CERTFILES is a list of filenames, each file is expected to consist of
350 a PEM encoded key and certificate. Uses current buffer if BUFFER is
351 nil."
352 (interactive)
353 (with-current-buffer (or buffer (current-buffer))
354 (unless (smime-encrypt-region
355 (point-min) (point-max)
356 (or certfiles
357 (list (read-file-name "Recipient's S/MIME certificate: "
358 smime-certificate-directory nil))))
359 (error "Encryption failed"))))
361 ;; Verify+decrypt region
363 (defun smime-verify-region (b e)
364 "Verify S/MIME message in region between B and E.
365 Returns non-nil on success.
366 Any details (stdout and stderr) are left in the buffer specified by
367 `smime-details-buffer'."
368 (smime-new-details-buffer)
369 (let ((CAs (append (if smime-CA-file
370 (list "-CAfile"
371 (expand-file-name smime-CA-file)))
372 (if smime-CA-directory
373 (list "-CApath"
374 (expand-file-name smime-CA-directory))))))
375 (unless CAs
376 (error "No CA configured"))
377 (if smime-crl-check
378 (cl-pushnew smime-crl-check CAs :test #'equal))
379 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
380 "smime" "-verify" "-out" "/dev/null" CAs)
382 (insert-buffer-substring smime-details-buffer)
383 nil)))
385 (defun smime-noverify-region (b e)
386 "Verify integrity of S/MIME message in region between B and E.
387 Returns non-nil on success.
388 Any details (stdout and stderr) are left in the buffer specified by
389 `smime-details-buffer'."
390 (smime-new-details-buffer)
391 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
392 "smime" "-verify" "-noverify" "-out" '("/dev/null"))
394 (insert-buffer-substring smime-details-buffer)
395 nil))
397 (defun smime-decrypt-region (b e keyfile &optional from)
398 "Decrypt S/MIME message in region between B and E with key in KEYFILE.
399 Optional FROM specifies sender's mail address.
400 On success, replaces region with decrypted data and return non-nil.
401 Any details (stderr on success, stdout and stderr on error) are left
402 in the buffer specified by `smime-details-buffer'."
403 (smime-new-details-buffer)
404 (let ((buffer (generate-new-buffer " *smime*"))
405 (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
406 (tmpfile (make-temp-file "smime")))
407 (if passphrase
408 (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
409 (if (prog1
410 (apply 'smime-call-openssl-region b e
411 (list buffer tmpfile)
412 "smime" "-decrypt" "-recip" (expand-file-name keyfile)
413 (if passphrase
414 (list "-passin" "env:GNUS_SMIME_PASSPHRASE")))
415 (if passphrase
416 (setenv "GNUS_SMIME_PASSPHRASE" "" t))
417 (with-current-buffer smime-details-buffer
418 (insert-file-contents tmpfile)
419 (delete-file tmpfile)))
420 (progn
421 (delete-region b e)
422 (when from
423 (insert "From: " from "\n"))
424 (insert-buffer-substring buffer)
425 (kill-buffer buffer)
427 (with-current-buffer smime-details-buffer
428 (insert-buffer-substring buffer))
429 (kill-buffer buffer)
430 (delete-region b e)
431 (insert-buffer-substring smime-details-buffer)
432 nil)))
434 ;; Verify+Decrypt buffer
436 (defun smime-verify-buffer (&optional buffer)
437 "Verify integrity of S/MIME message in BUFFER.
438 Uses current buffer if BUFFER is nil. Returns non-nil on success.
439 Any details (stdout and stderr) are left in the buffer specified by
440 `smime-details-buffer'."
441 (interactive)
442 (with-current-buffer (or buffer (current-buffer))
443 (smime-verify-region (point-min) (point-max))))
445 (defun smime-noverify-buffer (&optional buffer)
446 "Verify integrity of S/MIME message in BUFFER.
447 Does NOT verify validity of certificate (only message integrity).
448 Uses current buffer if BUFFER is nil. Returns non-nil on success.
449 Any details (stdout and stderr) are left in the buffer specified by
450 `smime-details-buffer'."
451 (interactive)
452 (with-current-buffer (or buffer (current-buffer))
453 (smime-noverify-region (point-min) (point-max))))
455 (defun smime-decrypt-buffer (&optional buffer keyfile)
456 "Decrypt S/MIME message in BUFFER using KEYFILE.
457 Uses current buffer if BUFFER is nil, and query user of KEYFILE if it's nil.
458 On success, replaces data in buffer and return non-nil.
459 Any details (stderr on success, stdout and stderr on error) are left
460 in the buffer specified by `smime-details-buffer'."
461 (interactive)
462 (with-current-buffer (or buffer (current-buffer))
463 (smime-decrypt-region
464 (point-min) (point-max)
465 (expand-file-name
466 (or keyfile
467 (smime-get-key-by-email
468 (gnus-completing-read
469 "Decipher using key"
470 smime-keys nil (car-safe (car-safe smime-keys)))))))))
472 ;; Various operations
474 (defun smime-new-details-buffer ()
475 (with-current-buffer (get-buffer-create smime-details-buffer)
476 (erase-buffer)))
478 (defun smime-pkcs7-region (b e)
479 "Convert S/MIME message between points B and E into a PKCS7 message."
480 (smime-new-details-buffer)
481 (when (smime-call-openssl-region b e smime-details-buffer "smime" "-pk7out")
482 (delete-region b e)
483 (insert-buffer-substring smime-details-buffer)
486 (defun smime-pkcs7-certificates-region (b e)
487 "Extract any certificates enclosed in PKCS7 message between points B and E."
488 (smime-new-details-buffer)
489 (when (smime-call-openssl-region
490 b e smime-details-buffer "pkcs7" "-print_certs" "-text")
491 (delete-region b e)
492 (insert-buffer-substring smime-details-buffer)
495 (defun smime-pkcs7-email-region (b e)
496 "Get email addresses contained in certificate between points B and E.
497 A string or a list of strings is returned."
498 (smime-new-details-buffer)
499 (when (smime-call-openssl-region
500 b e smime-details-buffer "x509" "-email" "-noout")
501 (delete-region b e)
502 (insert-buffer-substring smime-details-buffer)
505 ;; Utility functions
507 (defun smime-get-certfiles (keyfile keys)
508 (if keys
509 (let ((curkey (car keys))
510 (otherkeys (cdr keys)))
511 (if (string= keyfile (cadr curkey))
512 (nth 2 curkey)
513 (smime-get-certfiles keyfile otherkeys)))))
515 (defun smime-buffer-as-string-region (b e)
516 "Return each line in region between B and E as a list of strings."
517 (save-excursion
518 (goto-char b)
519 (let (res)
520 (while (< (point) e)
521 (let ((str (buffer-substring (point) (point-at-eol))))
522 (unless (string= "" str)
523 (push str res)))
524 (forward-line))
525 res)))
527 ;; Find certificates
529 (defun smime-mail-to-domain (mailaddr)
530 (if (string-match "@" mailaddr)
531 (replace-match "." 'fixedcase 'literal mailaddr)
532 mailaddr))
534 (defun smime-cert-by-dns (mail)
535 "Find certificate via DNS for address MAIL."
536 (let* ((dig-dns-server smime-dns-server)
537 (digbuf (dig-invoke (smime-mail-to-domain mail) "cert" nil nil "+vc"))
538 (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
539 (certrr (with-current-buffer digbuf
540 (dig-extract-rr (smime-mail-to-domain mail) "cert")))
541 (cert (and certrr (dig-rr-get-pkix-cert certrr))))
542 (if cert
543 (with-current-buffer retbuf
544 (insert "-----BEGIN CERTIFICATE-----\n")
545 (let ((i 0) (len (length cert)))
546 (while (> (- len 64) i)
547 (insert (substring cert i (+ i 64)) "\n")
548 (setq i (+ i 64)))
549 (insert (substring cert i len) "\n"))
550 (insert "-----END CERTIFICATE-----\n"))
551 (kill-buffer retbuf)
552 (setq retbuf nil))
553 (kill-buffer digbuf)
554 retbuf))
556 (declare-function ldap-search "ldap"
557 (filter &optional host attributes attrsonly withdn))
559 (defun smime-cert-by-ldap-1 (mail host)
560 "Get certificate for MAIL from the ldap server at HOST."
561 (let ((ldapresult
562 (funcall
563 (progn
564 (require 'ldap)
565 'ldap-search)
566 (concat "mail=" mail)
567 host '("userCertificate") nil))
568 (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
569 ldapstr
570 cert)
571 (if (and (consp ldapresult)
572 ;; FIXME: This seems to expect a format rather different from
573 ;; the list of alists described in ldap.el.
574 (setq ldapstr (cadr (caar ldapresult)))
575 (> (length ldapstr) 0))
576 (with-current-buffer retbuf
577 ;; Certificates on LDAP servers _should_ be in DER format,
578 ;; but there are some servers out there that distributes the
579 ;; certificates in PEM format (with or without
580 ;; header/footer) so we try to handle them anyway.
581 (if (or (string= (substring ldapstr 0 27)
582 "-----BEGIN CERTIFICATE-----")
583 (string= (substring ldapstr 0 3)
584 "MII"))
585 (setq cert
586 (replace-regexp-in-string
587 (concat "\\(\n\\|\r\\|-----BEGIN CERTIFICATE-----\\|"
588 "-----END CERTIFICATE-----\\)")
590 ldapstr nil t))
591 (setq cert (base64-encode-string ldapstr t)))
592 (insert "-----BEGIN CERTIFICATE-----\n")
593 (let ((i 0) (len (length cert)))
594 (while (> (- len 64) i)
595 (insert (substring cert i (+ i 64)) "\n")
596 (setq i (+ i 64)))
597 (insert (substring cert i len) "\n"))
598 (insert "-----END CERTIFICATE-----\n"))
599 (kill-buffer retbuf)
600 (setq retbuf nil))
601 retbuf))
603 (defun smime-cert-by-ldap (mail)
604 "Find certificate via LDAP for address MAIL."
605 (if smime-ldap-host-list
606 (catch 'certbuf
607 (dolist (host smime-ldap-host-list)
608 (let ((retbuf (smime-cert-by-ldap-1 mail host)))
609 (when retbuf
610 (throw 'certbuf retbuf)))))))
612 ;; User interface.
614 (defvar smime-buffer "*SMIME*")
616 (defvar smime-mode-map
617 (let ((map (make-sparse-keymap)))
618 (suppress-keymap map)
619 (define-key map "q" 'smime-exit)
620 (define-key map "f" 'smime-certificate-info)
621 map))
623 (autoload 'gnus-completing-read "gnus-util")
625 (put 'smime-mode 'mode-class 'special)
626 (define-derived-mode smime-mode fundamental-mode ;special-mode
627 "SMIME"
628 "Major mode for browsing, viewing and fetching certificates.
630 All normal editing commands are switched off.
631 \\<smime-mode-map>
633 The following commands are available:
635 \\{smime-mode-map}"
636 (setq mode-line-process nil)
637 (buffer-disable-undo)
638 (setq truncate-lines t)
639 (setq buffer-read-only t))
641 (defun smime-certificate-info (certfile)
642 (interactive "fCertificate file: ")
643 (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
644 (switch-to-buffer buffer)
645 (erase-buffer)
646 (call-process smime-openssl-program nil buffer 'display
647 "x509" "-in" (expand-file-name certfile) "-text")
648 (fundamental-mode)
649 (set-buffer-modified-p nil)
650 (setq buffer-read-only t)
651 (goto-char (point-min))))
653 (defun smime-draw-buffer ()
654 (with-current-buffer smime-buffer
655 (let (buffer-read-only)
656 (erase-buffer)
657 (insert "\nYour keys:\n")
658 (dolist (key smime-keys)
659 (insert
660 (format "\t\t%s: %s\n" (car key) (cadr key))))
661 (insert "\nTrusted Certificate Authorities:\n")
662 (insert "\nKnown Certificates:\n"))))
664 (defun smime ()
665 "Go to the SMIME buffer."
666 (interactive)
667 (unless (get-buffer smime-buffer)
668 (with-current-buffer (get-buffer-create smime-buffer)
669 (smime-mode)))
670 (smime-draw-buffer)
671 (switch-to-buffer smime-buffer))
673 (defun smime-exit ()
674 "Quit the S/MIME buffer."
675 (interactive)
676 (kill-buffer (current-buffer)))
678 ;; Other functions
680 (defun smime-get-key-by-email (email)
681 (cadr (assoc email smime-keys)))
683 (defun smime-get-key-with-certs-by-email (email)
684 (cdr (assoc email smime-keys)))
686 (provide 'smime)
688 ;;; smime.el ends here