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