<div dir="ltr">Hi!<div><br></div><div>I think this might be a timing bug. Can you try the following patch?<br></div><div><br></div><div>Regards Ingela Erlang/OTP Team - Ericsson AB</div><div><br></div><div>







<p class="gmail-p1"><span class="gmail-s1"><b>diff --git a/lib/ssl/src/tls_connection.erl b/lib/ssl/src/tls_connection.erl</b></span></p>
<p class="gmail-p1"><span class="gmail-s1"><b>index 32991d3..e1f8e78 100644</b></span></p>
<p class="gmail-p1"><span class="gmail-s1"><b>--- a/lib/ssl/src/tls_connection.erl</b></span></p>
<p class="gmail-p1"><span class="gmail-s1"><b>+++ b/lib/ssl/src/tls_connection.erl</b></span></p>
<p class="gmail-p1"><span class="gmail-s2">@@ -420,6 +420,7 @@</span><span class="gmail-s1"> handle_common_event(internal, #alert{} = Alert, StateName,</span></p>
<p class="gmail-p1"><span class="gmail-s1"> handle_common_event(internal,  #ssl_tls{type = ?HANDSHAKE, fragment = Data}, </span></p>
<p class="gmail-p1"><span class="gmail-s1">                    StateName, #state{protocol_buffers =</span></p>
<p class="gmail-p1"><span class="gmail-s1">                                          #protocol_buffers{tls_handshake_buffer = Buf0} = Buffers,</span></p>
<p class="gmail-p2"><span class="gmail-s1">+                                      unprocessed_handshake_events = NumUnprocessed,</span></p>
<p class="gmail-p1"><span class="gmail-s1">                                      negotiated_version = Version,</span></p>
<p class="gmail-p1"><span class="gmail-s1">                                      ssl_options = Options} = State0) -></span></p>
<p class="gmail-p1"><span class="gmail-s1">     try</span></p>
<p class="gmail-p1"><span class="gmail-s2">@@ -432,7 +433,7 @@</span><span class="gmail-s1"> handle_common_event(internal,  #ssl_tls{type = ?HANDSHAKE, fragment = Data},</span></p>
<p class="gmail-p1"><span class="gmail-s1">            connection -></span></p>
<p class="gmail-p1"><span class="gmail-s1">                ssl_connection:hibernate_after(StateName, State, Events);</span></p>
<p class="gmail-p1"><span class="gmail-s1">            _ -></span></p>
<p class="gmail-p3"><span class="gmail-s1">-               {next_state, StateName, State#state{unprocessed_handshake_events = unprocessed_events(Events)}, Events}</span></p>
<p class="gmail-p2"><span class="gmail-s1">+</span><span class="gmail-s3">               </span><span class="gmail-s1">{next_state, StateName, State#state{unprocessed_handshake_events = unprocessed_events(Events, NumUnprocessed)}, Events}</span></p>
<p class="gmail-p1"><span class="gmail-s1">        end</span></p>
<p class="gmail-p1"><span class="gmail-s1">     catch throw:#alert{} = Alert -></span></p>
<p class="gmail-p1"><span class="gmail-s1">            ssl_connection:handle_own_alert(Alert, Version, StateName, State0)</span></p>
<p class="gmail-p1"><span class="gmail-s2">@@ -615,8 +616,6 @@</span><span class="gmail-s1"> next_event(StateName, Record, State, Actions) -></span></p>
<p class="gmail-p1"><span class="gmail-s1">            {next_state, StateName, State, [{next_event, internal, Alert} | Actions]}</span></p>
<p class="gmail-p1"><span class="gmail-s1">     end.</span></p>
<p class="gmail-p4"><span class="gmail-s1"> </span></p>
<p class="gmail-p3"><span class="gmail-s1">-tls_handshake_events([]) -></span></p>
<p class="gmail-p3"><span class="gmail-s1">-    throw(?ALERT_REC(?FATAL, ?HANDSHAKE_FAILURE, malformed_handshake));</span></p>
<p class="gmail-p1"><span class="gmail-s1"> tls_handshake_events(Packets) -></span></p>
<p class="gmail-p1"><span class="gmail-s1">     lists:map(fun(Packet) -></span></p>
<p class="gmail-p1"><span class="gmail-s1">                      {next_event, internal, {handshake, Packet}}</span></p>
<p class="gmail-p1"><span class="gmail-s2">@@ -727,8 +726,10 @@</span><span class="gmail-s1"> gen_info(Event, StateName, #state{negotiated_version = Version} = State) -></span></p>
<p class="gmail-p1"><span class="gmail-s1">                                                       malformed_handshake_data), </span></p>
<p class="gmail-p1"><span class="gmail-s1">                                            Version, StateName, State)  </span></p>
<p class="gmail-p1"><span class="gmail-s1">     end.</span></p>
<p class="gmail-p3"><span class="gmail-s1">-           </span></p>
<p class="gmail-p3"><span class="gmail-s1">-unprocessed_events(Events) -></span></p>
<p class="gmail-p2"><span class="gmail-s1">+</span></p>
<p class="gmail-p2"><span class="gmail-s1">+unprocessed_events([], Unprocessed) -></span></p>
<p class="gmail-p2"><span class="gmail-s1">+    Unprocessed;</span></p>
<p class="gmail-p2"><span class="gmail-s1">+unprocessed_events(Events, _) -></span></p>
<p class="gmail-p1"><span class="gmail-s1">     %% The first handshake event will be processed immediately</span></p>
<p class="gmail-p1"><span class="gmail-s1">     %% as it is entered first in the event queue and</span></p>
<p class="gmail-p1"><span class="gmail-s1">     %% when it is processed there will be length(Events)-1</span></p></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-12-21 14:47 GMT+01:00 Justin Wood <span dir="ltr"><<a href="mailto:justin.k.wood@gmail.com" target="_blank">justin.k.wood@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">After upgrading to Erlang OTP-19.2 and, in different requests, specified the version as one of tlsv1, tlsv1.1 or tlsv1.2. Every time I received the same error.<div><br></div><div>I'm not sure if it is helpful at all, but I can replicate this on Windows, OSX 10.11 (El Capitan) and Ubuntu 14.04. I have not tried on other platforms.<br><div><br></div><div>Is there any other information that I would be able to give that would be helpful?</div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 21, 2016 at 3:38 AM, Technion <span dir="ltr"><<a href="mailto:technion@lolware.net" target="_blank">technion@lolware.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div dir="ltr">
<div id="m_5777177461594144945m_-8708617999540836611divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif" dir="ltr">
<p>Hi Justin,</p>
<p><br>
</p>
<p>Is this earlier discussion relevant:</p>
<p><br>
</p>
<p><a class="m_5777177461594144945m_-8708617999540836611OWAAutoLink" id="m_5777177461594144945m_-8708617999540836611LPlnk203460" href="http://erlang.org/pipermail/erlang-questions/2016-November/090780.html" target="_blank">http://erlang.org/pipermail/er<wbr>lang-questions/2016-November/<wbr>090780.html</a></p>
<p><br>
</p>
<p><br>
</p>
<br>
<br>
<div style="color:rgb(0,0,0)">
<hr style="display:inline-block;width:98%">
<div id="m_5777177461594144945m_-8708617999540836611divRplyFwdMsg" dir="ltr"><font color="#000000" face="Calibri, sans-serif" style="font-size:11pt"><b>From:</b> <a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erlan<wbr>g.org</a> <<a href="mailto:erlang-questions-bounces@erlang.org" target="_blank">erlang-questions-bounces@erla<wbr>ng.org</a>> on behalf of Justin Wood <<a href="mailto:justin.k.wood@gmail.com" target="_blank">justin.k.wood@gmail.com</a>><br>
<b>Sent:</b> Wednesday, 21 December 2016 11:56 AM<br>
<b>To:</b> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<b>Subject:</b> [erlang-questions] SSL handshake failure</font>
<div> </div>
</div><div><div class="m_5777177461594144945h5">
<div>
<div dir="ltr">Hi there,
<div><br>
</div>
<div>I am attempting to use the ssl module in order to get a socket to a remote server (MongoDB). I am using the following line of code (OTP 19)</div>
<div><br>
</div>
<div>ssl:connect("<a id="m_5777177461594144945m_-8708617999540836611LPlnk803551" href="http://my-server.net" target="_blank">my-server.net</a>", 27017, [binary, {active, false}, {packet, raw}], 5000).</div>
<div><br>
</div>
<div>Whenever I use this, I get the following:</div>
<div><br>
</div>
<div>
<div>=ERROR REPORT==== 20-Dec-2016::19:13:13 ===</div>
<div>SSL: certify: tls_connection.erl:603:Fatal error: handshake failure - malformed_handshake</div>
<div>{error,{tls_alert,"handshake failure"}}</div>
</div>
<div><br>
</div>
<div>Which lead me to look into erlang:get_stacktrace/0</div>
<div><br>
</div>
<div>
<div>erlang:get_stacktrace().<br>
[{tls_connection,start_fsm,8,</div>
<div>                 [{file,"tls_connection.erl"},<wbr>{line,79}]},</div>
<div> {ssl_connection,connect,8,</div>
<div>                 [{file,"ssl_connection.erl"},<wbr>{line,84}]},</div>
<div> {erl_eval,do_apply,6,[{file,"<wbr>erl_eval.erl"},{line,674}]},</div>
<div> {shell,exprs,7,[{file,"shell.<wbr>erl"},{line,686}]},</div>
<div> {shell,eval_exprs,7,[{file,"s<wbr>hell.erl"},{line,641}]},</div>
<div> {shell,eval_loop,3,[{file,"sh<wbr>ell.erl"},{line,626}]}]</div>
</div>
<div><br>
</div>
<div>My problem is that the certificate that the server is using should be legitimate. I can connect, without error, using other clients and languages.</div>
<div><br>
</div>
<div>I took a look through Wireshark in order to try and figure out what is going on and this is what I see.</div>
<div><br>
</div>
<div>* The Client says hello and presents a list of cipher suites.</div>
<div>* The Server says hello and says that it wants to use TLS_RSA_WITH_AES_256_GCM_SHA38<wbr>4 (This cipher is in the list that the client sends)</div>
<div>* The Server sends the Client the certificates it is using (these both appear to be signed by DigiCert).</div>
<div>* The Server sends an encrypted handshake message.</div>
<div>* The Client responds with a Fatal Alert stating a Handshake Failure.</div>
<div>* The Server sends another encrypted handshake message.</div>
<div><br>
</div>
<div>I have verified that ssl:connect/4 is working fine as I can connect to a number of different miscellaneous services (including a number of other MongoDB instances).</div>
<div><br>
</div>
<div>I was wondering if there is anything else I can do to try and figure out why erlang does not allow this connection.</div>
<div><br>
</div>
<div>Justin </div>
</div>
</div>
</div></div></div>
</div>
</div>

</blockquote></div><br></div>
</div></div><br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/<wbr>listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>