file new.xpm was added on branch t-gnus-6_17 on 2006-04-11 22:59:16 +0000
[elisp/gnus.git-] / lisp / tls.el
1 ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
2
3 ;; Copyright (C) 1996-1999, 2003, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Keywords: comm, tls, gnutls, ssl
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 2, or (at your option)
13 ;; 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; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This package implements a simple wrapper around "gnutls-cli" to
28 ;; make Emacs support TLS/SSL.
29 ;;
30 ;; Usage is the same as `open-network-stream', i.e.:
31 ;;
32 ;; (setq tmp (open-tls-stream "test" (current-buffer) "news.mozilla.org" 563))
33 ;; ...
34 ;; #<process test>
35 ;; (process-send-string tmp "mode reader\n")
36 ;; 200 secnews.netscape.com Netscape-Collabra/3.52 03615 NNRP ready ...
37 ;; nil
38 ;; (process-send-string tmp "quit\n")
39 ;; 205
40 ;; nil
41
42 ;; To use this package as a replacement for ssl.el by William M. Perry
43 ;; <wmperry@cs.indiana.edu>, you need to evaluate the following:
44 ;;
45 ;; (defalias 'open-ssl-stream 'open-tls-stream)
46
47 ;;; Code:
48
49 (require 'pces)
50
51 (eval-and-compile
52   (autoload 'format-spec "format-spec")
53   (autoload 'format-spec-make "format-spec"))
54
55 (defgroup tls nil
56   "Transport Layer Security (TLS) parameters."
57   :group 'comm)
58
59 (defcustom tls-program '("gnutls-cli -p %p %h"
60                          "gnutls-cli -p %p %h --protocols ssl3")
61   "List of strings containing commands to start TLS stream to a host.
62 Each entry in the list is tried until a connection is successful.
63 %s is replaced with server hostname, %p with port to connect to.
64 The program should read input on stdin and write output to
65 stdout.  Also see `tls-success' for what the program should output
66 after successful negotiation."
67   :type '(repeat string)
68   :group 'tls)
69
70 (defcustom tls-process-connection-type nil
71   "*Value for `process-connection-type' to use when starting TLS process."
72   :version "22.1"
73   :type 'boolean
74   :group 'tls)
75
76 (defcustom tls-success "- Handshake was completed"
77   "*Regular expression indicating completed TLS handshakes.
78 The default is what GNUTLS's \"gnutls-cli\" outputs."
79   :version "22.1"
80   :type 'regexp
81   :group 'tls)
82
83 (defcustom tls-certtool-program (executable-find "certtool")
84   "Name of  GnuTLS certtool.
85 Used by `tls-certificate-information'."
86   :version "22.1"
87   :type '(repeat string)
88   :group 'tls)
89
90 (defun tls-certificate-information (der)
91   "Parse X.509 certificate in DER format into an assoc list."
92   (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
93                              (base64-encode-string der)
94                              "\n-----END CERTIFICATE-----\n"))
95         (exit-code 0))
96     (with-current-buffer (get-buffer-create " *certtool*")
97       (erase-buffer)
98       (insert certificate)
99       (setq exit-code (condition-case ()
100                           (call-process-region (point-min) (point-max)
101                                                tls-certtool-program
102                                                t (list (current-buffer) nil) t
103                                                "--certificate-info")
104                         (error -1)))
105       (if (/= exit-code 0)
106           nil
107         (let ((vals nil))
108           (goto-char (point-min))
109           (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
110             (push (cons (match-string 1) (match-string 2)) vals))
111           (nreverse vals))))))
112
113 (defun open-tls-stream (name buffer host service)
114   "Open a TLS connection for a service to a host.
115 Returns a subprocess-object to represent the connection.
116 Input and output work as for subprocesses; `delete-process' closes it.
117 Args are NAME BUFFER HOST SERVICE.
118 NAME is name for process.  It is modified if necessary to make it unique.
119 BUFFER is the buffer (or buffer-name) to associate with the process.
120  Process output goes at end of that buffer, unless you specify
121  an output stream or filter function to handle the output.
122  BUFFER may be also nil, meaning that this process is not associated
123  with any buffer
124 Third arg is name of the host to connect to, or its IP address.
125 Fourth arg SERVICE is name of the service desired, or an integer
126 specifying a port number to connect to."
127   (let ((cmds tls-program) cmd done)
128     (message "Opening TLS connection to `%s'..." host)
129     (while (and (not done) (setq cmd (pop cmds)))
130       (message "Opening TLS connection with `%s'..." cmd)
131       (let* ((process-connection-type tls-process-connection-type)
132              (process (as-binary-process
133                        (start-process
134                         name buffer shell-file-name shell-command-switch
135                         (format-spec
136                          cmd
137                          (format-spec-make
138                           ?h host
139                           ?p (if (integerp service)
140                                  (int-to-string service)
141                                service))))))
142              response)
143         (while (and process
144                     (memq (process-status process) '(open run))
145                     (save-excursion
146                       (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
147                       (goto-char (point-min))
148                       (not (setq done (re-search-forward tls-success nil t)))))
149           (accept-process-output process 1)
150           (sit-for 1))
151         (message "Opening TLS connection with `%s'...%s" cmd
152                  (if done "done" "failed"))
153         (if done
154             (setq done process)
155           (delete-process process))))
156     (message "Opening TLS connection to `%s'...%s"
157              host (if done "done" "failed"))
158     done))
159
160 (provide 'tls)
161
162 ;;; tls.el ends here