Add 2011 to FSF/AIST copyright years.
[emacs.git] / lisp / gnus / smime.el
blob0ae7bbb5c84a1ae1067a85d0767974dfc3b172f6
1 ;;; smime.el --- S/MIME support library
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: SMIME X.509 PEM OpenSSL
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library perform S/MIME operations from within Emacs.
28 ;; Functions for fetching certificates from public repositories are
29 ;; provided, currently from DNS and LDAP.
31 ;; It uses OpenSSL (tested with version 0.9.5a and 0.9.6) for signing,
32 ;; encryption and decryption.
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.
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.
45 ;; The home of this file is in Gnus, but also available from
46 ;; http://josefsson.org/smime.html.
48 ;;; Quick introduction:
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.
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:
58 ;; $ openssl pkcs12 -in mykey.p12 -clcerts -nodes > mykey.pem
59 ;; ...
61 ;; Now, use M-x customize-variable smime-keys and add mykey.pem as
62 ;; a key.
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 armored and a signature. Encryption, M-x
67 ;; smime-encrypt-buffer, should also work.
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:
74 ;; $ openssl pkcs12 -in mykey.p12 -cacerts -nodes > cacert.pem
75 ;; ...
77 ;; Now, use M-x customize-variable smime-CAs and add cacert.pem as a
78 ;; CA certificate.
80 ;; You should now be able to sign messages, and even verify messages
81 ;; sent by others that use the same CA as you.
83 ;; Bugs:
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. :-)
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.
95 ;; Cache password for e.g. 1h
97 ;; Suggestions and comments are appreciated, mail me at simon@josefsson.org.
99 ;; begin rant
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.
105 ;; Why can't someone write a simple introduction to all this stuff?
106 ;; Until then, much of this resemble security by obscurity.
108 ;; Also, I'm not going to mention anything about the wonders of
109 ;; cryptopolitics. Oops, I just did.
111 ;; end rant
113 ;;; Revision history:
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
120 ;;; Code:
122 ;; For Emacs < 22.2.
123 (eval-and-compile
124 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
125 (require 'dig)
127 (if (locate-library "password-cache")
128 (require 'password-cache)
129 (require 'password))
131 (eval-when-compile (require 'cl))
133 (eval-and-compile
134 (cond
135 ((fboundp 'replace-in-string)
136 (defalias 'smime-replace-in-string 'replace-in-string))
137 ((fboundp 'replace-regexp-in-string)
138 (defun smime-replace-in-string (string regexp newtext &optional literal)
139 "Replace all matches for REGEXP with NEWTEXT in STRING.
140 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
141 string containing the replacements.
143 This is a compatibility function for different Emacsen."
144 (replace-regexp-in-string regexp newtext string nil literal)))))
146 (defgroup smime nil
147 "S/MIME configuration."
148 :group 'mime)
150 (defcustom smime-keys nil
151 "*Map mail addresses to a file containing Certificate (and private key).
152 The file is assumed to be in PEM format. You can also associate additional
153 certificates to be sent with every message to each address."
154 :type '(repeat (list (string :tag "Mail address")
155 (file :tag "File name")
156 (repeat :tag "Additional certificate files"
157 (file :tag "File name"))))
158 :group 'smime)
160 (defcustom smime-CA-directory nil
161 "*Directory containing certificates for CAs you trust.
162 Directory should contain files (in PEM format) named to the X.509
163 hash of the certificate. This can be done using OpenSSL such as:
165 $ ln -s ca.pem `openssl x509 -noout -hash -in ca.pem`.0
167 where `ca.pem' is the file containing a PEM encoded X.509 CA
168 certificate."
169 :type '(choice (const :tag "none" nil)
170 directory)
171 :group 'smime)
173 (defcustom smime-CA-file nil
174 "*Files containing certificates for CAs you trust.
175 File should contain certificates in PEM format."
176 :version "22.1"
177 :type '(choice (const :tag "none" nil)
178 file)
179 :group 'smime)
181 (defcustom smime-certificate-directory "~/Mail/certs/"
182 "*Directory containing other people's certificates.
183 It should contain files named to the X.509 hash of the certificate,
184 and the files themself should be in PEM format."
185 ;The S/MIME library provide simple functionality for fetching
186 ;certificates into this directory, so there is no need to populate it
187 ;manually.
188 :type 'directory
189 :group 'smime)
191 (defcustom smime-openssl-program
192 (and (condition-case ()
193 (eq 0 (call-process "openssl" nil nil nil "version"))
194 (error nil))
195 "openssl")
196 "*Name of OpenSSL binary."
197 :type 'string
198 :group 'smime)
200 ;; OpenSSL option to select the encryption cipher
202 (defcustom smime-encrypt-cipher "-des3"
203 "*Cipher algorithm used for encryption."
204 :version "22.1"
205 :type '(choice (const :tag "Triple DES" "-des3")
206 (const :tag "DES" "-des")
207 (const :tag "RC2 40 bits" "-rc2-40")
208 (const :tag "RC2 64 bits" "-rc2-64")
209 (const :tag "RC2 128 bits" "-rc2-128"))
210 :group 'smime)
212 (defcustom smime-crl-check nil
213 "*Check revocation status of signers certificate using CRLs.
214 Enabling this will have OpenSSL check the signers certificate
215 against a certificate revocation list (CRL).
217 For this to work the CRL must be up-to-date and since they are
218 normally updated quite often (ie. several times a day) you
219 probably need some tool to keep them up-to-date. Unfortunately
220 Gnus cannot do this for you.
222 The CRL should either be appended (in PEM format) to your
223 `smime-CA-file' or be located in a file (also in PEM format) in
224 your `smime-certificate-directory' named to the X.509 hash of the
225 certificate with .r0 as file name extension.
227 At least OpenSSL version 0.9.7 is required for this to work."
228 :type '(choice (const :tag "No check" nil)
229 (const :tag "Check certificate" "-crl_check")
230 (const :tag "Check certificate chain" "-crl_check_all"))
231 :group 'smime)
233 (defcustom smime-dns-server nil
234 "*DNS server to query certificates from.
235 If nil, use system defaults."
236 :version "22.1"
237 :type '(choice (const :tag "System defaults")
238 string)
239 :group 'smime)
241 (defcustom smime-ldap-host-list nil
242 "A list of LDAP hosts with S/MIME user certificates.
243 If needed search base, binddn, passwd, etc. for the LDAP host
244 must be set in `ldap-host-parameters-alist'."
245 :type '(repeat (string :tag "Host name"))
246 :version "23.1" ;; No Gnus
247 :group 'smime)
249 (defvar smime-details-buffer "*OpenSSL output*")
251 ;; Use mm-util?
252 (eval-and-compile
253 (defalias 'smime-make-temp-file
254 (if (fboundp 'make-temp-file)
255 'make-temp-file
256 (lambda (prefix &optional dir-flag) ;; Simple implementation
257 (expand-file-name
258 (make-temp-name prefix)
259 (if (fboundp 'temp-directory)
260 (temp-directory)
261 temporary-file-directory))))))
263 ;; Password dialog function
264 (declare-function password-read-and-add "password-cache" (prompt &optional key))
266 (defun smime-ask-passphrase (&optional cache-key)
267 "Asks the passphrase to unlock the secret key.
268 If `cache-key' and `password-cache' is non-nil then cache the
269 password under `cache-key'."
270 (let ((passphrase
271 (password-read-and-add
272 "Passphrase for secret key (RET for no passphrase): " cache-key)))
273 (if (string= passphrase "")
275 passphrase)))
277 ;; OpenSSL wrappers.
279 (defun smime-call-openssl-region (b e buf &rest args)
280 (case (apply 'call-process-region b e smime-openssl-program nil buf nil args)
281 (0 t)
282 (1 (message "OpenSSL: An error occurred parsing the command options.") nil)
283 (2 (message "OpenSSL: One of the input files could not be read.") nil)
284 (3 (message "OpenSSL: An error occurred creating the PKCS#7 file or when reading the MIME message.") nil)
285 (4 (message "OpenSSL: An error occurred decrypting or verifying the message.") nil)
286 (t (error "Unknown OpenSSL exitcode") nil)))
288 (defun smime-make-certfiles (certfiles)
289 (if certfiles
290 (append (list "-certfile" (expand-file-name (car certfiles)))
291 (smime-make-certfiles (cdr certfiles)))))
293 ;; Sign+encrypt region
295 (defun smime-sign-region (b e keyfile)
296 "Sign region with certified key in KEYFILE.
297 If signing fails, the buffer is not modified. Region is assumed to
298 have proper MIME tags. KEYFILE is expected to contain a PEM encoded
299 private key and certificate as its car, and a list of additional
300 certificates to include in its caar. If no additional certificates is
301 included, KEYFILE may be the file containing the PEM encoded private
302 key and certificate itself."
303 (smime-new-details-buffer)
304 (let* ((certfiles (and (cdr-safe keyfile) (cadr keyfile)))
305 (keyfile (or (car-safe keyfile) keyfile))
306 (buffer (generate-new-buffer " *smime*"))
307 (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
308 (tmpfile (smime-make-temp-file "smime")))
309 (if passphrase
310 (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
311 (prog1
312 (when (prog1
313 (apply 'smime-call-openssl-region b e (list buffer tmpfile)
314 "smime" "-sign" "-signer" (expand-file-name keyfile)
315 (append
316 (smime-make-certfiles certfiles)
317 (if passphrase
318 (list "-passin" "env:GNUS_SMIME_PASSPHRASE"))))
319 (if passphrase
320 (setenv "GNUS_SMIME_PASSPHRASE" "" t))
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))))
330 (with-current-buffer smime-details-buffer
331 (goto-char (point-max))
332 (insert-buffer-substring buffer))
333 (kill-buffer buffer))))
335 (defun smime-encrypt-region (b e certfiles)
336 "Encrypt region for recipients specified in CERTFILES.
337 If encryption fails, the buffer is not modified. Region is assumed to
338 have proper MIME tags. CERTFILES is a list of filenames, each file
339 is expected to contain of a PEM encoded certificate."
340 (smime-new-details-buffer)
341 (let ((buffer (generate-new-buffer " *smime*"))
342 (tmpfile (smime-make-temp-file "smime")))
343 (prog1
344 (when (prog1
345 (apply 'smime-call-openssl-region b e (list buffer tmpfile)
346 "smime" "-encrypt" smime-encrypt-cipher
347 (mapcar 'expand-file-name certfiles))
348 (with-current-buffer smime-details-buffer
349 (insert-file-contents tmpfile)
350 (delete-file tmpfile)))
351 (delete-region b e)
352 (insert-buffer-substring buffer)
353 (goto-char b)
354 (when (looking-at "^MIME-Version: 1.0$")
355 (delete-region (point) (progn (forward-line 1) (point))))
357 (with-current-buffer smime-details-buffer
358 (goto-char (point-max))
359 (insert-buffer-substring buffer))
360 (kill-buffer buffer))))
362 ;; Sign+encrypt buffer
364 (defun smime-sign-buffer (&optional keyfile buffer)
365 "S/MIME sign BUFFER with key in KEYFILE.
366 KEYFILE should contain a PEM encoded key and certificate."
367 (interactive)
368 (with-current-buffer (or buffer (current-buffer))
369 (unless (smime-sign-region
370 (point-min) (point-max)
371 (if keyfile
372 keyfile
373 (smime-get-key-with-certs-by-email
374 (completing-read
375 (concat "Sign using key"
376 (if smime-keys
377 (concat " (default " (caar smime-keys) "): ")
378 ": "))
379 smime-keys nil nil (car-safe (car-safe smime-keys))))))
380 (error "Signing failed"))))
382 (defun smime-encrypt-buffer (&optional certfiles buffer)
383 "S/MIME encrypt BUFFER for recipients specified in CERTFILES.
384 CERTFILES is a list of filenames, each file is expected to consist of
385 a PEM encoded key and certificate. Uses current buffer if BUFFER is
386 nil."
387 (interactive)
388 (with-current-buffer (or buffer (current-buffer))
389 (unless (smime-encrypt-region
390 (point-min) (point-max)
391 (or certfiles
392 (list (read-file-name "Recipient's S/MIME certificate: "
393 smime-certificate-directory nil))))
394 (error "Encryption failed"))))
396 ;; Verify+decrypt region
398 (defun smime-verify-region (b e)
399 "Verify S/MIME message in region between B and E.
400 Returns non-nil on success.
401 Any details (stdout and stderr) are left in the buffer specified by
402 `smime-details-buffer'."
403 (smime-new-details-buffer)
404 (let ((CAs (append (if smime-CA-file
405 (list "-CAfile"
406 (expand-file-name smime-CA-file)))
407 (if smime-CA-directory
408 (list "-CApath"
409 (expand-file-name smime-CA-directory))))))
410 (unless CAs
411 (error "No CA configured"))
412 (if smime-crl-check
413 (add-to-list 'CAs smime-crl-check))
414 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
415 "smime" "-verify" "-out" "/dev/null" CAs)
417 (insert-buffer-substring smime-details-buffer)
418 nil)))
420 (defun smime-noverify-region (b e)
421 "Verify integrity of S/MIME message in region between B and E.
422 Returns non-nil on success.
423 Any details (stdout and stderr) are left in the buffer specified by
424 `smime-details-buffer'."
425 (smime-new-details-buffer)
426 (if (apply 'smime-call-openssl-region b e (list smime-details-buffer t)
427 "smime" "-verify" "-noverify" "-out" '("/dev/null"))
429 (insert-buffer-substring smime-details-buffer)
430 nil))
432 (defvar from)
434 (defun smime-decrypt-region (b e keyfile)
435 "Decrypt S/MIME message in region between B and E with key in KEYFILE.
436 On success, replaces region with decrypted data and return non-nil.
437 Any details (stderr on success, stdout and stderr on error) are left
438 in the buffer specified by `smime-details-buffer'."
439 (smime-new-details-buffer)
440 (let ((buffer (generate-new-buffer " *smime*"))
441 CAs (passphrase (smime-ask-passphrase (expand-file-name keyfile)))
442 (tmpfile (smime-make-temp-file "smime")))
443 (if passphrase
444 (setenv "GNUS_SMIME_PASSPHRASE" passphrase))
445 (if (prog1
446 (apply 'smime-call-openssl-region b e
447 (list buffer tmpfile)
448 "smime" "-decrypt" "-recip" (expand-file-name keyfile)
449 (if passphrase
450 (list "-passin" "env:GNUS_SMIME_PASSPHRASE")))
451 (if passphrase
452 (setenv "GNUS_SMIME_PASSPHRASE" "" t))
453 (with-current-buffer smime-details-buffer
454 (insert-file-contents tmpfile)
455 (delete-file tmpfile)))
456 (progn
457 (delete-region b e)
458 (when (boundp 'from)
459 ;; `from' is dynamically bound in mm-dissect.
460 (insert "From: " from "\n"))
461 (insert-buffer-substring buffer)
462 (kill-buffer buffer)
464 (with-current-buffer smime-details-buffer
465 (insert-buffer-substring buffer))
466 (kill-buffer buffer)
467 (delete-region b e)
468 (insert-buffer-substring smime-details-buffer)
469 nil)))
471 ;; Verify+Decrypt buffer
473 (defun smime-verify-buffer (&optional buffer)
474 "Verify integrity of S/MIME message in BUFFER.
475 Uses current buffer if BUFFER is nil. Returns non-nil on success.
476 Any details (stdout and stderr) are left in the buffer specified by
477 `smime-details-buffer'."
478 (interactive)
479 (with-current-buffer (or buffer (current-buffer))
480 (smime-verify-region (point-min) (point-max))))
482 (defun smime-noverify-buffer (&optional buffer)
483 "Verify integrity of S/MIME message in BUFFER.
484 Does NOT verify validity of certificate (only message integrity).
485 Uses current buffer if BUFFER is nil. Returns non-nil on success.
486 Any details (stdout and stderr) are left in the buffer specified by
487 `smime-details-buffer'."
488 (interactive)
489 (with-current-buffer (or buffer (current-buffer))
490 (smime-noverify-region (point-min) (point-max))))
492 (defun smime-decrypt-buffer (&optional buffer keyfile)
493 "Decrypt S/MIME message in BUFFER using KEYFILE.
494 Uses current buffer if BUFFER is nil, and query user of KEYFILE if it's nil.
495 On success, replaces data in buffer and return non-nil.
496 Any details (stderr on success, stdout and stderr on error) are left
497 in the buffer specified by `smime-details-buffer'."
498 (interactive)
499 (with-current-buffer (or buffer (current-buffer))
500 (smime-decrypt-region
501 (point-min) (point-max)
502 (expand-file-name
503 (or keyfile
504 (smime-get-key-by-email
505 (completing-read
506 (concat "Decipher using key"
507 (if smime-keys (concat " (default " (caar smime-keys) "): ")
508 ": "))
509 smime-keys nil nil (car-safe (car-safe smime-keys)))))))))
511 ;; Various operations
513 (defun smime-new-details-buffer ()
514 (with-current-buffer (get-buffer-create smime-details-buffer)
515 (erase-buffer)))
517 (defun smime-pkcs7-region (b e)
518 "Convert S/MIME message between points B and E into a PKCS7 message."
519 (smime-new-details-buffer)
520 (when (smime-call-openssl-region b e smime-details-buffer "smime" "-pk7out")
521 (delete-region b e)
522 (insert-buffer-substring smime-details-buffer)
525 (defun smime-pkcs7-certificates-region (b e)
526 "Extract any certificates enclosed in PKCS7 message between points B and E."
527 (smime-new-details-buffer)
528 (when (smime-call-openssl-region
529 b e smime-details-buffer "pkcs7" "-print_certs" "-text")
530 (delete-region b e)
531 (insert-buffer-substring smime-details-buffer)
534 (defun smime-pkcs7-email-region (b e)
535 "Get email addresses contained in certificate between points B and E.
536 A string or a list of strings is returned."
537 (smime-new-details-buffer)
538 (when (smime-call-openssl-region
539 b e smime-details-buffer "x509" "-email" "-noout")
540 (delete-region b e)
541 (insert-buffer-substring smime-details-buffer)
544 ;; Utility functions
546 (defun smime-get-certfiles (keyfile keys)
547 (if keys
548 (let ((curkey (car keys))
549 (otherkeys (cdr keys)))
550 (if (string= keyfile (cadr curkey))
551 (caddr curkey)
552 (smime-get-certfiles keyfile otherkeys)))))
554 (defun smime-buffer-as-string-region (b e)
555 "Return each line in region between B and E as a list of strings."
556 (save-excursion
557 (goto-char b)
558 (let (res)
559 (while (< (point) e)
560 (let ((str (buffer-substring (point) (point-at-eol))))
561 (unless (string= "" str)
562 (push str res)))
563 (forward-line))
564 res)))
566 ;; Find certificates
568 (defun smime-mail-to-domain (mailaddr)
569 (if (string-match "@" mailaddr)
570 (replace-match "." 'fixedcase 'literal mailaddr)
571 mailaddr))
573 (defun smime-cert-by-dns (mail)
574 "Find certificate via DNS for address MAIL."
575 (let* ((dig-dns-server smime-dns-server)
576 (digbuf (dig-invoke (smime-mail-to-domain mail) "cert" nil nil "+vc"))
577 (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
578 (certrr (with-current-buffer digbuf
579 (dig-extract-rr (smime-mail-to-domain mail) "cert")))
580 (cert (and certrr (dig-rr-get-pkix-cert certrr))))
581 (if cert
582 (with-current-buffer retbuf
583 (insert "-----BEGIN CERTIFICATE-----\n")
584 (let ((i 0) (len (length cert)))
585 (while (> (- len 64) i)
586 (insert (substring cert i (+ i 64)) "\n")
587 (setq i (+ i 64)))
588 (insert (substring cert i len) "\n"))
589 (insert "-----END CERTIFICATE-----\n"))
590 (kill-buffer retbuf)
591 (setq retbuf nil))
592 (kill-buffer digbuf)
593 retbuf))
595 (defun smime-cert-by-ldap-1 (mail host)
596 "Get cetificate for MAIL from the ldap server at HOST."
597 (let ((ldapresult
598 (funcall
599 (if (or (featurep 'xemacs)
600 ;; For Emacs >= 22 we don't need smime-ldap.el
601 (< emacs-major-version 22))
602 (progn
603 (require 'smime-ldap)
604 'smime-ldap-search)
605 'ldap-search)
606 (concat "mail=" mail)
607 host '("userCertificate") nil))
608 (retbuf (generate-new-buffer (format "*certificate for %s*" mail)))
609 cert)
610 (if (and (>= (length ldapresult) 1)
611 (> (length (cadaar ldapresult)) 0))
612 (with-current-buffer retbuf
613 ;; Certificates on LDAP servers _should_ be in DER format,
614 ;; but there are some servers out there that distributes the
615 ;; certificates in PEM format (with or without
616 ;; header/footer) so we try to handle them anyway.
617 (if (or (string= (substring (cadaar ldapresult) 0 27)
618 "-----BEGIN CERTIFICATE-----")
619 (string= (substring (cadaar ldapresult) 0 3)
620 "MII"))
621 (setq cert
622 (smime-replace-in-string
623 (cadaar ldapresult)
624 (concat "\\(\n\\|\r\\|-----BEGIN CERTIFICATE-----\\|"
625 "-----END CERTIFICATE-----\\)")
626 "" t))
627 (setq cert (base64-encode-string (cadaar ldapresult) t)))
628 (insert "-----BEGIN CERTIFICATE-----\n")
629 (let ((i 0) (len (length cert)))
630 (while (> (- len 64) i)
631 (insert (substring cert i (+ i 64)) "\n")
632 (setq i (+ i 64)))
633 (insert (substring cert i len) "\n"))
634 (insert "-----END CERTIFICATE-----\n"))
635 (kill-buffer retbuf)
636 (setq retbuf nil))
637 retbuf))
639 (defun smime-cert-by-ldap (mail)
640 "Find certificate via LDAP for address MAIL."
641 (if smime-ldap-host-list
642 (catch 'certbuf
643 (dolist (host smime-ldap-host-list)
644 (let ((retbuf (smime-cert-by-ldap-1 mail host)))
645 (when retbuf
646 (throw 'certbuf retbuf)))))))
648 ;; User interface.
650 (defvar smime-buffer "*SMIME*")
652 (defvar smime-mode-map nil)
653 (put 'smime-mode 'mode-class 'special)
655 (unless smime-mode-map
656 (setq smime-mode-map (make-sparse-keymap))
657 (suppress-keymap smime-mode-map)
659 (define-key smime-mode-map "q" 'smime-exit)
660 (define-key smime-mode-map "f" 'smime-certificate-info))
662 (autoload 'gnus-run-mode-hooks "gnus-util")
664 (defun smime-mode ()
665 "Major mode for browsing, viewing and fetching certificates.
667 All normal editing commands are switched off.
668 \\<smime-mode-map>
670 The following commands are available:
672 \\{smime-mode-map}"
673 (interactive)
674 (kill-all-local-variables)
675 (setq major-mode 'smime-mode)
676 (setq mode-name "SMIME")
677 (setq mode-line-process nil)
678 (use-local-map smime-mode-map)
679 (buffer-disable-undo)
680 (setq truncate-lines t)
681 (setq buffer-read-only t)
682 (gnus-run-mode-hooks 'smime-mode-hook))
684 (defun smime-certificate-info (certfile)
685 (interactive "fCertificate file: ")
686 (let ((buffer (get-buffer-create (format "*certificate %s*" certfile))))
687 (switch-to-buffer buffer)
688 (erase-buffer)
689 (call-process smime-openssl-program nil buffer 'display
690 "x509" "-in" (expand-file-name certfile) "-text")
691 (fundamental-mode)
692 (set-buffer-modified-p nil)
693 (toggle-read-only t)
694 (goto-char (point-min))))
696 (defun smime-draw-buffer ()
697 (with-current-buffer smime-buffer
698 (let (buffer-read-only)
699 (erase-buffer)
700 (insert "\nYour keys:\n")
701 (dolist (key smime-keys)
702 (insert
703 (format "\t\t%s: %s\n" (car key) (cadr key))))
704 (insert "\nTrusted Certificate Authoritys:\n")
705 (insert "\nKnown Certificates:\n"))))
707 (defun smime ()
708 "Go to the SMIME buffer."
709 (interactive)
710 (unless (get-buffer smime-buffer)
711 (save-excursion
712 (set-buffer (get-buffer-create smime-buffer))
713 (smime-mode)))
714 (smime-draw-buffer)
715 (switch-to-buffer smime-buffer))
717 (defun smime-exit ()
718 "Quit the S/MIME buffer."
719 (interactive)
720 (kill-buffer (current-buffer)))
722 ;; Other functions
724 (defun smime-get-key-by-email (email)
725 (cadr (assoc email smime-keys)))
727 (defun smime-get-key-with-certs-by-email (email)
728 (cdr (assoc email smime-keys)))
730 (provide 'smime)
732 ;; arch-tag: e3f9b938-5085-4510-8a11-6625269c9a9e
733 ;;; smime.el ends here