Commit | Line | Data |
---|---|---|
01b2d1dd SJ |
1 | ;;; tls.el --- TLS/SSL support via wrapper around GnuTLS |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1996-1999, 2002-2012 Free Software Foundation, Inc. |
01b2d1dd SJ |
4 | |
5 | ;; Author: Simon Josefsson <simon@josefsson.org> | |
6 | ;; Keywords: comm, tls, gnutls, ssl | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
874a927a | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
01b2d1dd | 11 | ;; it under the terms of the GNU General Public License as published by |
874a927a GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
01b2d1dd SJ |
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 | |
874a927a | 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
01b2d1dd SJ |
18 | ;; GNU General Public License for more details. |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
874a927a | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
01b2d1dd SJ |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; This package implements a simple wrapper around "gnutls-cli" to | |
26 | ;; make Emacs support TLS/SSL. | |
27 | ;; | |
28 | ;; Usage is the same as `open-network-stream', i.e.: | |
29 | ;; | |
30 | ;; (setq tmp (open-tls-stream "test" (current-buffer) "news.mozilla.org" 563)) | |
31 | ;; ... | |
32 | ;; #<process test> | |
33 | ;; (process-send-string tmp "mode reader\n") | |
34 | ;; 200 secnews.netscape.com Netscape-Collabra/3.52 03615 NNRP ready ... | |
35 | ;; nil | |
36 | ;; (process-send-string tmp "quit\n") | |
37 | ;; 205 | |
38 | ;; nil | |
39 | ||
40 | ;; To use this package as a replacement for ssl.el by William M. Perry | |
41 | ;; <wmperry@cs.indiana.edu>, you need to evaluate the following: | |
42 | ;; | |
43 | ;; (defalias 'open-ssl-stream 'open-tls-stream) | |
44 | ||
45 | ;;; Code: | |
46 | ||
3c5fa30a GM |
47 | (autoload 'format-spec "format-spec") |
48 | (autoload 'format-spec-make "format-spec") | |
01b2d1dd SJ |
49 | |
50 | (defgroup tls nil | |
51 | "Transport Layer Security (TLS) parameters." | |
52 | :group 'comm) | |
53 | ||
59c95dc2 | 54 | (defcustom tls-end-of-info |
cd6db47c GM |
55 | (concat |
56 | "\\(" | |
57 | ;; `openssl s_client' regexp. See ssl/ssl_txt.c lines 219-220. | |
58 | ;; According to apps/s_client.c line 1515 `---' is always the last | |
59 | ;; line that is printed by s_client before the real data. | |
60 | "^ Verify return code: .+\n---\n\\|" | |
61 | ;; `gnutls' regexp. See src/cli.c lines 721-. | |
62 | "^- Simple Client Mode:\n" | |
63 | "\\(\n\\|" ; ignore blank lines | |
2cddada0 | 64 | ;; According to GnuTLS v2.1.5 src/cli.c lines 640-650 and 705-715 |
7b63c073 | 65 | ;; in `main' the handshake will start after this message. If the |
2cddada0 | 66 | ;; handshake fails, the programs will abort. |
cd6db47c GM |
67 | "^\\*\\*\\* Starting TLS handshake\n\\)*" |
68 | "\\)") | |
59c95dc2 GM |
69 | "Regexp matching end of TLS client informational messages. |
70 | Client data stream begins after the last character matched by | |
71 | this. The default matches `openssl s_client' (version 0.9.8c) | |
72 | and `gnutls-cli' (version 2.0.1) output." | |
73 | :version "22.2" | |
74 | :type 'regexp | |
75 | :group 'tls) | |
76 | ||
aecb42aa LMI |
77 | (defcustom tls-program '("gnutls-cli --insecure -p %p %h" |
78 | "gnutls-cli --insecure -p %p %h --protocols ssl3" | |
a1d16a7b | 79 | "openssl s_client -connect %h:%p -no_ssl2 -ign_eof") |
01b2d1dd SJ |
80 | "List of strings containing commands to start TLS stream to a host. |
81 | Each entry in the list is tried until a connection is successful. | |
4a3c7686 | 82 | %h is replaced with server hostname, %p with port to connect to. |
01b2d1dd | 83 | The program should read input on stdin and write output to |
b890d447 MB |
84 | stdout. |
85 | ||
86 | See `tls-checktrust' on how to check trusted root certs. | |
87 | ||
88 | Also see `tls-success' for what the program should output after | |
89 | successful negotiation." | |
90 | :type | |
91 | '(choice | |
92 | (list :tag "Choose commands" | |
93 | :value | |
94 | ("gnutls-cli -p %p %h" | |
95 | "gnutls-cli -p %p %h --protocols ssl3" | |
d55fe5bb | 96 | "openssl s_client -connect %h:%p -no_ssl2 -ign_eof") |
b890d447 MB |
97 | (set :inline t |
98 | ;; FIXME: add brief `:tag "..."' descriptions. | |
99 | ;; (repeat :inline t :tag "Other" (string)) | |
100 | ;; See `tls-checktrust': | |
101 | (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h") | |
102 | (const "gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3") | |
d55fe5bb | 103 | (const "openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof") |
b890d447 MB |
104 | ;; No trust check: |
105 | (const "gnutls-cli -p %p %h") | |
106 | (const "gnutls-cli -p %p %h --protocols ssl3") | |
d55fe5bb | 107 | (const "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")) |
b890d447 MB |
108 | (repeat :inline t :tag "Other" (string))) |
109 | (const :tag "Default list of commands" | |
110 | ("gnutls-cli -p %p %h" | |
111 | "gnutls-cli -p %p %h --protocols ssl3" | |
d55fe5bb | 112 | "openssl s_client -connect %h:%p -no_ssl2 -ign_eof")) |
b890d447 MB |
113 | (list :tag "List of commands" |
114 | (repeat :tag "Command" (string)))) | |
3031d8b0 | 115 | :version "22.1" |
01b2d1dd SJ |
116 | :group 'tls) |
117 | ||
118 | (defcustom tls-process-connection-type nil | |
b890d447 | 119 | "Value for `process-connection-type' to use when starting TLS process." |
bf247b6e | 120 | :version "22.1" |
01b2d1dd SJ |
121 | :type 'boolean |
122 | :group 'tls) | |
123 | ||
3031d8b0 | 124 | (defcustom tls-success "- Handshake was completed\\|SSL handshake has read " |
b890d447 | 125 | "Regular expression indicating completed TLS handshakes. |
321decc8 | 126 | The default is what GnuTLS's \"gnutls-cli\" or OpenSSL's |
3031d8b0 | 127 | \"openssl s_client\" outputs." |
bf247b6e | 128 | :version "22.1" |
01b2d1dd SJ |
129 | :type 'regexp |
130 | :group 'tls) | |
131 | ||
b890d447 MB |
132 | (defcustom tls-checktrust nil |
133 | "Indicate if certificates should be checked against trusted root certs. | |
134 | If this is `ask', the user can decide whether to accept an | |
135 | untrusted certificate. You may have to adapt `tls-program' in | |
136 | order to make this feature work properly, i.e., to ensure that | |
137 | the external program knows about the root certificates you | |
138 | consider trustworthy, e.g.: | |
139 | ||
140 | \(setq tls-program | |
141 | '(\"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h\" | |
142 | \"gnutls-cli --x509cafile /etc/ssl/certs/ca-certificates.crt -p %p %h --protocols ssl3\" | |
d55fe5bb | 143 | \"openssl s_client -connect %h:%p -CAfile /etc/ssl/certs/ca-certificates.crt -no_ssl2 -ign_eof\"))" |
b890d447 MB |
144 | :type '(choice (const :tag "Always" t) |
145 | (const :tag "Never" nil) | |
146 | (const :tag "Ask" ask)) | |
dd22fbf6 | 147 | :version "23.1" ;; No Gnus |
b890d447 MB |
148 | :group 'tls) |
149 | ||
150 | (defcustom tls-untrusted | |
151 | "- Peer's certificate is NOT trusted\\|Verify return code: \\([^0] \\|.[^ ]\\)" | |
152 | "Regular expression indicating failure of TLS certificate verification. | |
321decc8 | 153 | The default is what GnuTLS's \"gnutls-cli\" or OpenSSL's |
b890d447 MB |
154 | \"openssl s_client\" return in the event of unsuccessful |
155 | verification." | |
156 | :type 'regexp | |
dd22fbf6 | 157 | :version "23.1" ;; No Gnus |
b890d447 MB |
158 | :group 'tls) |
159 | ||
160 | (defcustom tls-hostmismatch | |
161 | "# The hostname in the certificate does NOT match" | |
162 | "Regular expression indicating a host name mismatch in certificate. | |
163 | When the host name specified in the certificate doesn't match the | |
164 | name of the host you are connecting to, gnutls-cli issues a | |
165 | warning to this effect. There is no such feature in openssl. Set | |
166 | this to nil if you want to ignore host name mismatches." | |
167 | :type 'regexp | |
dd22fbf6 | 168 | :version "23.1" ;; No Gnus |
b890d447 MB |
169 | :group 'tls) |
170 | ||
18965008 SJ |
171 | (defcustom tls-certtool-program (executable-find "certtool") |
172 | "Name of GnuTLS certtool. | |
173 | Used by `tls-certificate-information'." | |
bf247b6e | 174 | :version "22.1" |
9bdd0e16 | 175 | :type 'string |
18965008 SJ |
176 | :group 'tls) |
177 | ||
178 | (defun tls-certificate-information (der) | |
179 | "Parse X.509 certificate in DER format into an assoc list." | |
180 | (let ((certificate (concat "-----BEGIN CERTIFICATE-----\n" | |
181 | (base64-encode-string der) | |
182 | "\n-----END CERTIFICATE-----\n")) | |
183 | (exit-code 0)) | |
184 | (with-current-buffer (get-buffer-create " *certtool*") | |
185 | (erase-buffer) | |
186 | (insert certificate) | |
187 | (setq exit-code (condition-case () | |
188 | (call-process-region (point-min) (point-max) | |
189 | tls-certtool-program | |
190 | t (list (current-buffer) nil) t | |
191 | "--certificate-info") | |
192 | (error -1))) | |
193 | (if (/= exit-code 0) | |
194 | nil | |
195 | (let ((vals nil)) | |
196 | (goto-char (point-min)) | |
197 | (while (re-search-forward "^\\([^:]+\\): \\(.*\\)" nil t) | |
198 | (push (cons (match-string 1) (match-string 2)) vals)) | |
199 | (nreverse vals)))))) | |
200 | ||
a1d16a7b | 201 | (defun open-tls-stream (name buffer host port) |
3031d8b0 | 202 | "Open a TLS connection for a port to a host. |
01b2d1dd SJ |
203 | Returns a subprocess-object to represent the connection. |
204 | Input and output work as for subprocesses; `delete-process' closes it. | |
3031d8b0 | 205 | Args are NAME BUFFER HOST PORT. |
01b2d1dd | 206 | NAME is name for process. It is modified if necessary to make it unique. |
b890d447 | 207 | BUFFER is the buffer (or buffer name) to associate with the process. |
01b2d1dd SJ |
208 | Process output goes at end of that buffer, unless you specify |
209 | an output stream or filter function to handle the output. | |
210 | BUFFER may be also nil, meaning that this process is not associated | |
211 | with any buffer | |
212 | Third arg is name of the host to connect to, or its IP address. | |
3031d8b0 | 213 | Fourth arg PORT is an integer specifying a port to connect to." |
0ad32c54 CY |
214 | (let ((cmds tls-program) |
215 | (use-temp-buffer (null buffer)) | |
216 | process cmd done) | |
217 | (if use-temp-buffer | |
3c2dbd94 KY |
218 | (setq buffer (generate-new-buffer " TLS")) |
219 | ;; BUFFER is a string but does not exist as a buffer object. | |
220 | (unless (and (get-buffer buffer) | |
221 | (buffer-name (get-buffer buffer))) | |
222 | (generate-new-buffer buffer))) | |
cd6db47c | 223 | (with-current-buffer buffer |
59c95dc2 GM |
224 | (message "Opening TLS connection to `%s'..." host) |
225 | (while (and (not done) (setq cmd (pop cmds))) | |
59c95dc2 | 226 | (let ((process-connection-type tls-process-connection-type) |
8ed4a859 MH |
227 | (formatted-cmd |
228 | (format-spec | |
229 | cmd | |
230 | (format-spec-make | |
231 | ?h host | |
232 | ?p (if (integerp port) | |
233 | (int-to-string port) | |
234 | port)))) | |
59c95dc2 | 235 | response) |
8ed4a859 | 236 | (message "Opening TLS connection with `%s'..." formatted-cmd) |
59c95dc2 GM |
237 | (setq process (start-process |
238 | name buffer shell-file-name shell-command-switch | |
8ed4a859 | 239 | formatted-cmd)) |
2696d88f G |
240 | (funcall (if (fboundp 'set-process-query-on-exit-flag) |
241 | 'set-process-query-on-exit-flag | |
242 | 'process-kill-without-query) | |
243 | process nil) | |
59c95dc2 GM |
244 | (while (and process |
245 | (memq (process-status process) '(open run)) | |
246 | (progn | |
247 | (goto-char (point-min)) | |
0a4d4654 GM |
248 | (not (setq done (re-search-forward |
249 | tls-success nil t))))) | |
59c95dc2 GM |
250 | (unless (accept-process-output process 1) |
251 | (sit-for 1))) | |
8ed4a859 | 252 | (message "Opening TLS connection with `%s'...%s" formatted-cmd |
59c95dc2 | 253 | (if done "done" "failed")) |
0a4d4654 GM |
254 | (if (not done) |
255 | (delete-process process) | |
256 | ;; advance point to after all informational messages that | |
257 | ;; `openssl s_client' and `gnutls' print | |
258 | (let ((start-of-data nil)) | |
259 | (while | |
ea666a77 RS |
260 | (not (setq start-of-data |
261 | ;; the string matching `tls-end-of-info' | |
262 | ;; might come in separate chunks from | |
263 | ;; `accept-process-output', so start the | |
264 | ;; search where `tls-success' ended | |
265 | (save-excursion | |
266 | (if (re-search-forward tls-end-of-info nil t) | |
267 | (match-end 0))))) | |
0a4d4654 GM |
268 | (accept-process-output process 1)) |
269 | (if start-of-data | |
270 | ;; move point to start of client data | |
271 | (goto-char start-of-data))) | |
ea666a77 | 272 | (setq done process)))) |
0a4d4654 GM |
273 | (when (and done |
274 | (or | |
275 | (and tls-checktrust | |
276 | (save-excursion | |
277 | (goto-char (point-min)) | |
278 | (re-search-forward tls-untrusted nil t)) | |
279 | (or | |
280 | (and (not (eq tls-checktrust 'ask)) | |
281 | (message "The certificate presented by `%s' is \ | |
282 | NOT trusted." host)) | |
283 | (not (yes-or-no-p | |
284 | (format "The certificate presented by `%s' is \ | |
285 | NOT trusted. Accept anyway? " host))))) | |
286 | (and tls-hostmismatch | |
287 | (save-excursion | |
288 | (goto-char (point-min)) | |
289 | (re-search-forward tls-hostmismatch nil t)) | |
290 | (not (yes-or-no-p | |
291 | (format "Host name in certificate doesn't \ | |
292 | match `%s'. Connect anyway? " host)))))) | |
293 | (setq done nil) | |
294 | (delete-process process))) | |
295 | (message "Opening TLS connection to `%s'...%s" | |
296 | host (if done "done" "failed")) | |
0ad32c54 | 297 | (when use-temp-buffer |
b7131d2f | 298 | (if done (set-process-buffer process nil)) |
0ad32c54 | 299 | (kill-buffer buffer)) |
01b2d1dd SJ |
300 | done)) |
301 | ||
302 | (provide 'tls) | |
303 | ||
304 | ;;; tls.el ends here |