Merge branch 'master' into comment-cache
[emacs.git] / lisp / gnus / smime.el
blobe3c284f033c9042da83559f23caa2bc9f86d54dd
1 ;;; smime.el --- S/MIME support library -*- lexical-binding:t -*-
3 ;; Copyright (C) 2000-2017 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 <http://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 it's 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-and-add
238 "Passphrase for secret key (RET for no passphrase): " cache-key)))
239 (if (string= passphrase "")
241 passphrase)))
243 ;; OpenSSL wrappers.
245 (defun smime-call-openssl-region (b e buf &rest args)
246 (pcase (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 (_ (error "Unknown OpenSSL exitcode"))))
254 (defun smime-make-certfiles (certfiles)
255 (if certfiles
256 (append (list "-certfile" (expand-file-name (car certfiles)))
257 (smime-make-certfiles (cdr certfiles)))))
259 ;; Sign+encrypt region
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))))
296 (with-current-buffer smime-details-buffer
297 (goto-char (point-max))
298 (insert-buffer-substring buffer))
299 (kill-buffer buffer))))
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))))
323 (with-current-buffer smime-details-buffer
324 (goto-char (point-max))
325 (insert-buffer-substring buffer))
326 (kill-buffer buffer))))
328 ;; Sign+encrypt buffer
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"))))
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"))))
359 ;; Verify+decrypt region
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 (cl-pushnew smime-crl-check CAs :test #'equal))
377 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
378 "smime" "-verify" "-out" "/dev/null" CAs)
380 (insert-buffer-substring smime-details-buffer)
381 nil)))
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"))
392 (insert-buffer-substring smime-details-buffer)
393 nil))
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 (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)
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)))
432 ;; Verify+Decrypt buffer
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))))
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))))
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)))))))))
470 ;; Various operations
472 (defun smime-new-details-buffer ()
473 (with-current-buffer (get-buffer-create smime-details-buffer)
474 (erase-buffer)))
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)
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)
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)
503 ;; Utility functions
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 (nth 2 curkey)
511 (smime-get-certfiles keyfile otherkeys)))))
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)))
525 ;; Find certificates
527 (defun smime-mail-to-domain (mailaddr)
528 (if (string-match "@" mailaddr)
529 (replace-match "." 'fixedcase 'literal mailaddr)
530 mailaddr))
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))
554 (declare-function ldap-search "ldap"
555 (filter &optional host attributes attrsonly withdn))
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 ldapstr
568 cert)
569 (if (and (consp ldapresult)
570 ;; FIXME: This seems to expect a format rather different from
571 ;; the list of alists described in ldap.el.
572 (setq ldapstr (cadr (caar ldapresult)))
573 (> (length ldapstr) 0))
574 (with-current-buffer retbuf
575 ;; Certificates on LDAP servers _should_ be in DER format,
576 ;; but there are some servers out there that distributes the
577 ;; certificates in PEM format (with or without
578 ;; header/footer) so we try to handle them anyway.
579 (if (or (string= (substring ldapstr 0 27)
580 "-----BEGIN CERTIFICATE-----")
581 (string= (substring ldapstr 0 3)
582 "MII"))
583 (setq cert
584 (replace-regexp-in-string
585 (concat "\\(\n\\|\r\\|-----BEGIN CERTIFICATE-----\\|"
586 "-----END CERTIFICATE-----\\)")
588 ldapstr nil t))
589 (setq cert (base64-encode-string ldapstr t)))
590 (insert "-----BEGIN CERTIFICATE-----\n")
591 (let ((i 0) (len (length cert)))
592 (while (> (- len 64) i)
593 (insert (substring cert i (+ i 64)) "\n")
594 (setq i (+ i 64)))
595 (insert (substring cert i len) "\n"))
596 (insert "-----END CERTIFICATE-----\n"))
597 (kill-buffer retbuf)
598 (setq retbuf nil))
599 retbuf))
601 (defun smime-cert-by-ldap (mail)
602 "Find certificate via LDAP for address MAIL."
603 (if smime-ldap-host-list
604 (catch 'certbuf
605 (dolist (host smime-ldap-host-list)
606 (let ((retbuf (smime-cert-by-ldap-1 mail host)))
607 (when retbuf
608 (throw 'certbuf retbuf)))))))
610 ;; User interface.
612 (defvar smime-buffer "*SMIME*")
614 (defvar smime-mode-map
615 (let ((map (make-sparse-keymap)))
616 (suppress-keymap map)
617 (define-key map "q" 'smime-exit)
618 (define-key map "f" 'smime-certificate-info)
619 map))
621 (autoload 'gnus-completing-read "gnus-util")
623 (put 'smime-mode 'mode-class 'special)
624 (define-derived-mode smime-mode fundamental-mode ;special-mode
625 "SMIME"
626 "Major mode for browsing, viewing and fetching certificates.
628 All normal editing commands are switched off.
629 \\<smime-mode-map>
631 The following commands are available:
633 \\{smime-mode-map}"
634 (setq mode-line-process nil)
635 (buffer-disable-undo)
636 (setq truncate-lines t)
637 (setq buffer-read-only t))
639 (defun smime-certificate-info (certfile)
640 (interactive "fCertificate file: ")
641 (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
642 (switch-to-buffer buffer)
643 (erase-buffer)
644 (call-process smime-openssl-program nil buffer 'display
645 "x509" "-in" (expand-file-name certfile) "-text")
646 (fundamental-mode)
647 (set-buffer-modified-p nil)
648 (setq buffer-read-only t)
649 (goto-char (point-min))))
651 (defun smime-draw-buffer ()
652 (with-current-buffer smime-buffer
653 (let (buffer-read-only)
654 (erase-buffer)
655 (insert "\nYour keys:\n")
656 (dolist (key smime-keys)
657 (insert
658 (format "\t\t%s: %s\n" (car key) (cadr key))))
659 (insert "\nTrusted Certificate Authorities:\n")
660 (insert "\nKnown Certificates:\n"))))
662 (defun smime ()
663 "Go to the SMIME buffer."
664 (interactive)
665 (unless (get-buffer smime-buffer)
666 (with-current-buffer (get-buffer-create smime-buffer)
667 (smime-mode)))
668 (smime-draw-buffer)
669 (switch-to-buffer smime-buffer))
671 (defun smime-exit ()
672 "Quit the S/MIME buffer."
673 (interactive)
674 (kill-buffer (current-buffer)))
676 ;; Other functions
678 (defun smime-get-key-by-email (email)
679 (cadr (assoc email smime-keys)))
681 (defun smime-get-key-with-certs-by-email (email)
682 (cdr (assoc email smime-keys)))
684 (provide 'smime)
686 ;;; smime.el ends here