PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/net/tls.el

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