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