starttls.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. ;;; starttls.el --- STARTTLS functions
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Daiki Ueno <ueno@unixuser.org>
  4. ;; Author: Simon Josefsson <simon@josefsson.org>
  5. ;; Created: 1999/11/20
  6. ;; Keywords: TLS, SSL, OpenSSL, GnuTLS, mail, news
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This module defines some utility functions for STARTTLS profiles.
  20. ;; [RFC 2595] "Using TLS with IMAP, POP3 and ACAP"
  21. ;; by Chris Newman <chris.newman@innosoft.com> (1999/06)
  22. ;; This file now contains a combination of the two previous
  23. ;; implementations both called "starttls.el". The first one is Daiki
  24. ;; Ueno's starttls.el which uses his own "starttls" command line tool,
  25. ;; and the second one is Simon Josefsson's starttls.el which uses
  26. ;; "gnutls-cli" from GnuTLS.
  27. ;;
  28. ;; If "starttls" is available, it is preferred by the code over
  29. ;; "gnutls-cli", for backwards compatibility. Use
  30. ;; `starttls-use-gnutls' to toggle between implementations if you have
  31. ;; both tools installed. It is recommended to use GnuTLS, though, as
  32. ;; it performs more verification of the certificates.
  33. ;; The GnuTLS support requires GnuTLS 0.9.90 (released 2003-10-08) or
  34. ;; later, from <http://www.gnu.org/software/gnutls/>, or "starttls"
  35. ;; from <ftp://ftp.opaopa.org/pub/elisp/>.
  36. ;; Usage is similar to `open-network-stream'. For example:
  37. ;;
  38. ;; (when (setq tmp (starttls-open-stream
  39. ;; "test" (current-buffer) "yxa.extundo.com" 25))
  40. ;; (accept-process-output tmp 15)
  41. ;; (process-send-string tmp "STARTTLS\n")
  42. ;; (accept-process-output tmp 15)
  43. ;; (message "STARTTLS output:\n%s" (starttls-negotiate tmp))
  44. ;; (process-send-string tmp "EHLO foo\n"))
  45. ;; An example run yields the following output:
  46. ;;
  47. ;; 220 yxa.extundo.com ESMTP Sendmail 8.12.11/8.12.11/Debian-3; Wed, 26 May 2004 19:12:29 +0200; (No UCE/UBE) logging access from: c494102a.s-bi.bostream.se(OK)-c494102a.s-bi.bostream.se [217.215.27.65]
  48. ;; 220 2.0.0 Ready to start TLS
  49. ;; 250-yxa.extundo.com Hello c494102a.s-bi.bostream.se [217.215.27.65], pleased to meet you
  50. ;; 250-ENHANCEDSTATUSCODES
  51. ;; 250-PIPELINING
  52. ;; 250-EXPN
  53. ;; 250-VERB
  54. ;; 250-8BITMIME
  55. ;; 250-SIZE
  56. ;; 250-DSN
  57. ;; 250-ETRN
  58. ;; 250-AUTH DIGEST-MD5 CRAM-MD5 PLAIN LOGIN
  59. ;; 250-DELIVERBY
  60. ;; 250 HELP
  61. ;; nil
  62. ;;
  63. ;; With the message buffer containing:
  64. ;;
  65. ;; STARTTLS output:
  66. ;; *** Starting TLS handshake
  67. ;; - Server's trusted authorities:
  68. ;; [0]: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
  69. ;; - Certificate type: X.509
  70. ;; - Got a certificate list of 2 certificates.
  71. ;;
  72. ;; - Certificate[0] info:
  73. ;; # The hostname in the certificate matches 'yxa.extundo.com'.
  74. ;; # valid since: Wed May 26 12:16:00 CEST 2004
  75. ;; # expires at: Wed Jul 26 12:16:00 CEST 2023
  76. ;; # serial number: 04
  77. ;; # fingerprint: 7c 04 4b c1 fa 26 9b 5d 90 22 52 3c 65 3d 85 3a
  78. ;; # version: #1
  79. ;; # public key algorithm: RSA
  80. ;; # Modulus: 1024 bits
  81. ;; # Subject's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=Mail server,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
  82. ;; # Issuer's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
  83. ;;
  84. ;; - Certificate[1] info:
  85. ;; # valid since: Sun May 23 11:35:00 CEST 2004
  86. ;; # expires at: Sun Jul 23 11:35:00 CEST 2023
  87. ;; # serial number: 00
  88. ;; # fingerprint: fc 76 d8 63 1a c9 0b 3b fa 40 fe ed 47 7a 58 ae
  89. ;; # version: #3
  90. ;; # public key algorithm: RSA
  91. ;; # Modulus: 1024 bits
  92. ;; # Subject's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
  93. ;; # Issuer's DN: C=SE,ST=Stockholm,L=Stockholm,O=YXA,OU=CA,CN=yxa.extundo.com,EMAIL=staff@yxa.extundo.com
  94. ;;
  95. ;; - Peer's certificate issuer is unknown
  96. ;; - Peer's certificate is NOT trusted
  97. ;; - Version: TLS 1.0
  98. ;; - Key Exchange: RSA
  99. ;; - Cipher: ARCFOUR 128
  100. ;; - MAC: SHA
  101. ;; - Compression: NULL
  102. ;;; Code:
  103. (defgroup starttls nil
  104. "Support for `Transport Layer Security' protocol."
  105. :version "21.1"
  106. :group 'mail)
  107. (defcustom starttls-gnutls-program "gnutls-cli"
  108. "Name of GnuTLS command line tool.
  109. This program is used when GnuTLS is used, i.e. when
  110. `starttls-use-gnutls' is non-nil."
  111. :version "22.1"
  112. :type 'string
  113. :group 'starttls)
  114. (defcustom starttls-program "starttls"
  115. "The program to run in a subprocess to open an TLSv1 connection.
  116. This program is used when the `starttls' command is used,
  117. i.e. when `starttls-use-gnutls' is nil."
  118. :type 'string
  119. :group 'starttls)
  120. (defcustom starttls-use-gnutls (not (executable-find starttls-program))
  121. "*Whether to use GnuTLS instead of the `starttls' command."
  122. :version "22.1"
  123. :type 'boolean
  124. :group 'starttls)
  125. (defcustom starttls-extra-args nil
  126. "Extra arguments to `starttls-program'.
  127. These apply when the `starttls' command is used, i.e. when
  128. `starttls-use-gnutls' is nil."
  129. :type '(repeat string)
  130. :group 'starttls)
  131. (defcustom starttls-extra-arguments nil
  132. "Extra arguments to `starttls-program'.
  133. These apply when GnuTLS is used, i.e. when `starttls-use-gnutls' is non-nil.
  134. For example, non-TLS compliant servers may require
  135. '(\"--protocols\" \"ssl3\"). Invoke \"gnutls-cli --help\" to
  136. find out which parameters are available."
  137. :version "22.1"
  138. :type '(repeat string)
  139. :group 'starttls)
  140. (defcustom starttls-process-connection-type nil
  141. "*Value for `process-connection-type' to use when starting STARTTLS process."
  142. :version "22.1"
  143. :type 'boolean
  144. :group 'starttls)
  145. (defcustom starttls-connect "- Simple Client Mode:\n\n"
  146. "*Regular expression indicating successful connection.
  147. The default is what GnuTLS's \"gnutls-cli\" outputs."
  148. ;; GnuTLS cli.c:main() prints this string when it is starting to run
  149. ;; in the application read/write phase. If the logic, or the string
  150. ;; itself, is modified, this must be updated.
  151. :version "22.1"
  152. :type 'regexp
  153. :group 'starttls)
  154. (defcustom starttls-failure "\\*\\*\\* Handshake has failed"
  155. "*Regular expression indicating failed TLS handshake.
  156. The default is what GnuTLS's \"gnutls-cli\" outputs."
  157. ;; GnuTLS cli.c:do_handshake() prints this string on failure. If the
  158. ;; logic, or the string itself, is modified, this must be updated.
  159. :version "22.1"
  160. :type 'regexp
  161. :group 'starttls)
  162. (defcustom starttls-success "- Compression: "
  163. "*Regular expression indicating completed TLS handshakes.
  164. The default is what GnuTLS's \"gnutls-cli\" outputs."
  165. ;; GnuTLS cli.c:do_handshake() calls, on success,
  166. ;; common.c:print_info(), that unconditionally print this string
  167. ;; last. If that logic, or the string itself, is modified, this
  168. ;; must be updated.
  169. :version "22.1"
  170. :type 'regexp
  171. :group 'starttls)
  172. (defun starttls-negotiate-gnutls (process)
  173. "Negotiate TLS on PROCESS opened by `open-starttls-stream'.
  174. This should typically only be done once. It typically returns a
  175. multi-line informational message with information about the
  176. handshake, or nil on failure."
  177. (let (buffer info old-max done-ok done-bad)
  178. (if (null (setq buffer (process-buffer process)))
  179. ;; XXX How to remove/extract the TLS negotiation junk?
  180. (signal-process (process-id process) 'SIGALRM)
  181. (with-current-buffer buffer
  182. (save-excursion
  183. (setq old-max (goto-char (point-max)))
  184. (signal-process (process-id process) 'SIGALRM)
  185. (while (and (processp process)
  186. (eq (process-status process) 'run)
  187. (save-excursion
  188. (goto-char old-max)
  189. (not (or (setq done-ok (re-search-forward
  190. starttls-success nil t))
  191. (setq done-bad (re-search-forward
  192. starttls-failure nil t))))))
  193. (accept-process-output process 1 100)
  194. (sit-for 0.1))
  195. (setq info (buffer-substring-no-properties old-max (point-max)))
  196. (delete-region old-max (point-max))
  197. (if (or (and done-ok (not done-bad))
  198. ;; Prevent mitm that fake success msg after failure msg.
  199. (and done-ok done-bad (< done-ok done-bad)))
  200. info
  201. (message "STARTTLS negotiation failed: %s" info)
  202. nil))))))
  203. (defun starttls-negotiate (process)
  204. (if starttls-use-gnutls
  205. (starttls-negotiate-gnutls process)
  206. (signal-process (process-id process) 'SIGALRM)))
  207. (eval-and-compile
  208. (if (fboundp 'set-process-query-on-exit-flag)
  209. (defalias 'starttls-set-process-query-on-exit-flag
  210. 'set-process-query-on-exit-flag)
  211. (defalias 'starttls-set-process-query-on-exit-flag
  212. 'process-kill-without-query)))
  213. (defun starttls-open-stream-gnutls (name buffer host port)
  214. (message "Opening STARTTLS connection to `%s:%s'..." host port)
  215. (let* (done
  216. (old-max (with-current-buffer buffer (point-max)))
  217. (process-connection-type starttls-process-connection-type)
  218. (process (apply #'start-process name buffer
  219. starttls-gnutls-program "-s" host
  220. "-p" (if (integerp port)
  221. (int-to-string port)
  222. port)
  223. starttls-extra-arguments)))
  224. (starttls-set-process-query-on-exit-flag process nil)
  225. (while (and (processp process)
  226. (eq (process-status process) 'run)
  227. (with-current-buffer buffer
  228. (goto-char old-max)
  229. (not (setq done (re-search-forward
  230. starttls-connect nil t)))))
  231. (accept-process-output process 0 100)
  232. (sit-for 0.1))
  233. (if done
  234. (with-current-buffer buffer
  235. (delete-region old-max done))
  236. (delete-process process)
  237. (setq process nil))
  238. (message "Opening STARTTLS connection to `%s:%s'...%s"
  239. host port (if done "done" "failed"))
  240. process))
  241. ;;;###autoload
  242. (defun starttls-open-stream (name buffer host port)
  243. "Open a TLS connection for a port to a host.
  244. Returns a subprocess object to represent the connection.
  245. Input and output work as for subprocesses; `delete-process' closes it.
  246. Args are NAME BUFFER HOST PORT.
  247. NAME is name for process. It is modified if necessary to make it unique.
  248. BUFFER is the buffer (or `buffer-name') to associate with the process.
  249. Process output goes at end of that buffer, unless you specify
  250. an output stream or filter function to handle the output.
  251. BUFFER may be also nil, meaning that this process is not associated
  252. with any buffer
  253. Third arg is name of the host to connect to, or its IP address.
  254. Fourth arg PORT is an integer specifying a port to connect to.
  255. If `starttls-use-gnutls' is nil, this may also be a service name, but
  256. GnuTLS requires a port number."
  257. (if starttls-use-gnutls
  258. (starttls-open-stream-gnutls name buffer host port)
  259. (message "Opening STARTTLS connection to `%s:%s'" host (format "%s" port))
  260. (let* ((process-connection-type starttls-process-connection-type)
  261. (process (apply #'start-process
  262. name buffer starttls-program
  263. host (format "%s" port)
  264. starttls-extra-args)))
  265. (starttls-set-process-query-on-exit-flag process nil)
  266. process)))
  267. (defun starttls-available-p ()
  268. "Say whether the STARTTLS programs are available."
  269. (and (not (memq system-type '(windows-nt ms-dos)))
  270. (executable-find (if starttls-use-gnutls
  271. starttls-gnutls-program
  272. starttls-program))))
  273. (defalias 'starttls-any-program-available 'starttls-available-p)
  274. (make-obsolete 'starttls-any-program-available 'starttls-available-p
  275. "2011-08-02")
  276. (provide 'starttls)
  277. ;;; starttls.el ends here