1 ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Simon Josefsson <simon@josefsson.org>
7 ;; Keywords: comm, tls, gnutls, ssl
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 2, or (at your option)
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; 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.
28 ;; This package implements a simple wrapper around "gnutls-cli" to
29 ;; make Emacs support TLS/SSL.
31 ;; Usage is the same as `open-network-stream', i.e.:
33 ;; (setq tmp (open-tls-stream "test" (current-buffer) "news.mozilla.org" 563))
36 ;; (process-send-string tmp "mode reader\n")
37 ;; 200 secnews.netscape.com Netscape-Collabra/3.52 03615 NNRP ready ...
39 ;; (process-send-string tmp "quit\n")
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:
46 ;; (defalias 'open-ssl-stream 'open-tls-stream)
53 (autoload 'format-spec "format-spec")
54 (autoload 'format-spec-make "format-spec"))
57 "Transport Layer Security (TLS) parameters."
60 (defcustom tls-program '("gnutls-cli -p %p %h"
61 "gnutls-cli -p %p %h --protocols ssl3"
62 "openssl s_client -connect %h:%p -no_ssl2")
63 "List of strings containing commands to start TLS stream to a host.
64 Each entry in the list is tried until a connection is successful.
65 %s is replaced with server hostname, %p with port to connect to.
66 The program should read input on stdin and write output to
67 stdout. Also see `tls-success' for what the program should output
68 after successful negotiation."
69 :type '(repeat string)
73 (defcustom tls-process-connection-type nil
74 "*Value for `process-connection-type' to use when starting TLS process."
79 (defcustom tls-success "- Handshake was completed\\|SSL handshake has read "
80 "*Regular expression indicating completed TLS handshakes.
81 The default is what GNUTLS's \"gnutls-cli\" or OpenSSL's
82 \"openssl s_client\" outputs."
87 (defcustom tls-certtool-program (executable-find "certtool")
88 "Name of GnuTLS certtool.
89 Used by `tls-certificate-information'."
91 :type '(repeat string)
94 (defun tls-certificate-information (der)
95 "Parse X.509 certificate in DER format into an assoc list."
96 (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n"
97 (base64-encode-string der)
98 "\n-----END CERTIFICATE-----\n"))
100 (with-current-buffer (get-buffer-create " *certtool*")
103 (setq exit-code (condition-case ()
104 (call-process-region (point-min) (point-max)
106 t (list (current-buffer) nil) t
107 "--certificate-info")
112 (goto-char (point-min))
113 (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t)
114 (push (cons (match-string 1) (match-string 2)) vals))
117 (defun open-tls-stream (name buffer host port)
118 "Open a TLS connection for a port to a host.
119 Returns a subprocess-object to represent the connection.
120 Input and output work as for subprocesses; `delete-process' closes it.
121 Args are NAME BUFFER HOST PORT.
122 NAME is name for process. It is modified if necessary to make it unique.
123 BUFFER is the buffer (or buffer-name) to associate with the process.
124 Process output goes at end of that buffer, unless you specify
125 an output stream or filter function to handle the output.
126 BUFFER may be also nil, meaning that this process is not associated
128 Third arg is name of the host to connect to, or its IP address.
129 Fourth arg PORT is an integer specifying a port to connect to."
130 (let ((cmds tls-program) cmd done)
131 (message "Opening TLS connection to `%s'..." host)
132 (while (and (not done) (setq cmd (pop cmds)))
133 (message "Opening TLS connection with `%s'..." cmd)
134 (let* ((process-connection-type tls-process-connection-type)
135 (process (as-binary-process
137 name buffer shell-file-name shell-command-switch
142 ?p (if (integerp port)
147 (memq (process-status process) '(open run))
149 (set-buffer buffer) ;; XXX "blue moon" nntp.el bug
150 (goto-char (point-min))
151 (not (setq done (re-search-forward tls-success nil t)))))
152 (accept-process-output process 1)
154 (message "Opening TLS connection with `%s'...%s" cmd
155 (if done "done" "failed"))
158 (delete-process process))))
159 (message "Opening TLS connection to `%s'...%s"
160 host (if done "done" "failed"))