On Fri, May 23, 2008 at 3:32 PM, Jean-Paul Calderone &lt;<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">On Fri, 23 May 2008 12:29:44 -0500, Kevin Horn &lt;<a href="mailto:kevin.horn@gmail.com" target="_blank">kevin.horn@gmail.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Howdy list,<br>
<br>
I&#39;m trying to implement a protocol using Twisted which has a &quot;STARTTLS&quot;<br>
command to switch the protocol from plain TCP to TCP over TLS.<br>
<br>
I&#39;ve mostly been going by the way that the imap4.py module seems to do it,<br>
but I can&#39;t seem to get a handshake to complete.<br>
<br>
I found this page ( <a href="http://wiki.vislab.usyd.edu.au/moin.cgi/SSLCertNotes" target="_blank">http://wiki.vislab.usyd.edu.au/moin.cgi/SSLCertNotes</a> )<br>
which was helpful, but I don&#39;t want to force client cert authentication.<br>
<br>
In order to separate this problem from other issues, I&#39;ve adapted the echo<br>
protocol code from above the above page to try and get a simple test case<br>
(my code below)<br>
<br>
I am recieving the following output and traceback when running the client<br>
code ( on both Windows and Linux ):<br>
<br>
using TLSv1:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
tls_echoclient.py<br>
</blockquote>
<br>
Sending: Hello, world!<br>
receive: ERROR: Must authenticate<br>
Sending: STARTTLS<br>
receive: READY<br>
Sending: Continuing<br>
connection lost (protocol)<br>
connection lost: [(&#39;SSL routines&#39;, &#39;SSL3_READ_BYTES&#39;, &#39;sslv3 alert handshake<br>
failure&#39;), (&#39;SSL routines&#39;, &#39;SSL3_READ_BYTES&#39;, &#39;ssl handshake failure&#39;)]<br>
Traceback (most recent call last):<br>
&nbsp;File &quot;C:\Documents and<br>
Settings\kevinh\Desktop\mine_id\sandbox\funsize\sslecho\tls_echoclient.py&quot;,<br>
line 58, in &lt;module&gt;<br>
 &nbsp; reactor.run()<br>
&nbsp;File &quot;C:\Python25\lib\site-packages\twisted\internet\posixbase.py&quot;, line<br>
223, in run<br>
 &nbsp; self.mainLoop()<br>
&nbsp;File &quot;C:\Python25\lib\site-packages\twisted\internet\posixbase.py&quot;, line<br>
234, in mainLoop<br>
 &nbsp; self.doIteration(t)<br>
&nbsp;File &quot;C:\Python25\lib\site-packages\twisted\internet\selectreactor.py&quot;,<br>
line 140, in doSelect<br>
 &nbsp; _logrun(selectable, _drdw, selectable, method, dict)<br>
--- &lt;exception caught here&gt; ---<br>
&nbsp;File &quot;C:\Python25\lib\site-packages\twisted\python\log.py&quot;, line 51, in<br>
callWithLogger<br>
 &nbsp; return callWithContext({&quot;system&quot;: lp}, func, *args, **kw)<br>
&lt;&lt; SNIP &gt;&gt;<br>
&nbsp;File &quot;C:\Python25\lib\site-packages\twisted\internet\base.py&quot;, line 490,<br>
in stop<br>
 &nbsp; &quot;Can&#39;t stop reactor that isn&#39;t running.&quot;)<br>
twisted.internet.error.ReactorNotRunning: Can&#39;t stop reactor that isn&#39;t<br>
running.<br>
<br>
What am I doing wrong? &nbsp;Is there a SSL config option I&#39;m setting<br>
incorrectly? &nbsp;Do I need to use a different SSL Context? Am I totally off<br>
base?<br>
<br>
Thanks,<br>
<br>
</blockquote>
<br></div></div>
The traceback here is just because you&#39;re calling reactor.stop() twice,<br>
once in Protocol.connectionLost, then again in Factory.clientConnectionLost.<br>
Get rid of one of these and at least you&#39;ll get rid of some spurious noise.<br>
</blockquote><div>&nbsp;<br>Thanks for responding, Jean-Paul, and thanks for the tip.&nbsp; I&#39;ve been so consumed <br>with reading through the noise that for some reason it never occurred to me to try <br>and get rid of it.<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
As far as the TLS part of your code goes, it basically looks okay. &nbsp;By doing<br>
a sendLine immediately before you call startTLS, you risk running into #686,<br>
but if you actually hit that, you should see a warning and the connection<br>
should be closed without an OpenSSL error.<br>
<br>
So I&#39;m not exactly sure what problem you&#39;re encountering. &nbsp;To further<br>
complicate matters, when I run your code, TLS is successfully negotiated.<br>
</blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Jean-Paul</blockquote><div><br><br>Well that&#39;s ... frustrating.&nbsp; I was hoping I had just overlooked something <br>obvious (and easy to fix!)<br><br>Can you tell me more about the environment you are running under?<br>
<br>So far I&#39;ve tried:<br>WinXP,&nbsp; Python 2.5, Twisted 8.0.1, pyOpenSSL 0.7, OpenSSL 0.9.8g<br>Linux(CentOS), Python 2.4, Twisted 8.1.0, pyOpenSSL 0.7, OpenSSL 0.9.7a<br><br>Perhaps there is something wrong with my certificates?&nbsp; I would expect that <br>
this would cause errors on the server end, though...<br><br>Is there any way to get more information about the handshake failure?&nbsp;</div></div><br>Thanks, <br><br>Kevin Horn<br>