<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi!<div><br></div><div>In general it is not possible to detect and upgrade a socket to ssl, BUT if the client initiate with some </div><div>known message data that can be distinguished from the SSL hello message, like HTTP requests then </div><div>you can do something like (I have, and it works :-):</div><div><br></div><div>The socket should already be in {packet,0} packet mode, set {active,false}, {packet,0} in the options to listen.</div><div>Then either read in passive mode:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{ok,Header} = gen_tcp:recv(Socket, MinHeaderBlockSize),<span class="Apple-tab-span" style="white-space:pre">             </span>%% say 8 bytes ?</div><div><br></div><div>Or something like this in active once mode</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>inet:setopts(Socket, [{active, once}]),<span class="Apple-tab-span" style="white-space:pre">     </span>%% not easy to control the number of bytes received here yet (there is an EEP!)</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>Header = </div><div><span class="Apple-tab-span" style="white-space:pre">       </span>receive</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>{tcp, Socket, Header} -> ok</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>end</div><div><span class="Apple-tab-span" style="white-space:pre">          </span></div><div>Then do a match on the data that the client normally send with some known SSL intro messages:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Type = detect(Header),</div><div><br></div><div><div>detect(<<"GET", _/binary>>) ->    plain;</div><div>detect(<<"POST", _/binary>>) ->    plain;</div><div>detect(<<"OPTIONS", _/binary>>) ->  plain;</div><div>detect(<<"TRACE", _/binary>>) ->    plain;</div><div>...</div><div>detect(<<1:1,_Len:15,1:8,_Version:16, _/binary>>) ->  ssl;</div><div>detect(<<ContentType:8, _Version:16, _Length:16, _/binary>>) -></div><div>    if ContentType == 22 ->  %% HANDSHAKE</div><div>            ssl;</div><div>       true -></div><div>            undefined</div><div>    end;</div><div>detect(_) -></div><div>    undefined.</div></div><div><br></div><div>Then switch to ssl if that was detected:</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">       </span>if Type =:= ssl -></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>%% Socket MUST be in passive mode at this point</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>gen_tcp:unrecv(Socket, Header),<span class="Apple-tab-span" style="white-space:pre">             </span>%% push back SSL data </div><div><span class="Apple-tab-span" style="white-space:pre">          </span>{ok, SSLSocket} = ssl:ssl_accept(Socket, SSLOptions, SSLAcceptTimeout),</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>do_ssl_request(SSLSocket);</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>    true -></div><div><span class="Apple-tab-span" style="white-space:pre">             </span>do_plain_request(Socket)</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>end</div><div><br></div><div>/Tony</div><div><br></div><div><div><div>On 17 apr 2013, at 09:41, Ingela Andin <<a href="mailto:ingela.andin@gmail.com">ingela.andin@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Hi!<br><div><div class="gmail_extra"><br><div class="gmail_quote">2013/4/17 pablo platt <span dir="ltr"><<a href="mailto:pablo.platt@gmail.com" target="_blank">pablo.platt@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr"><div><div>Hi,<br><br></div>Is it possible to accept SSL connections and normal TCP connections on the same port?<br></div>Maybe accept normal TCP connections. If the connection is SSL connection pass it to the ssl module  and if it is a normal TCP connection just handle the socket.<br>
</div></blockquote><div><br></div><div>It is possible to upgrade a tcp socket to an ssl socket.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr"><div></div><div>If it is possible, how can I distinct SSL from non SSL connections?<br></div></div></blockquote><div><br></div><div>That is the hard part. You must have some scheme to negotiate the upgrade with the client over plain tcp. (Like STARTTLS, HTTP Connect etc)<br>
 </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div>How can I pass the socket to the ssl module?<br></div></div></blockquote>
<br><div>ssl:ssl_accept(TcpSocket, SslOptions)  or ssl:connect(TcpSocket, SslOptions) <br></div><div>make sure the socket is passive ({active, false}) before you make the call.<br></div><div><br></div>Regards Ingela Erlang/OTP team Ericsson AB<br>
</div></div></div></div>
_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>http://erlang.org/mailman/listinfo/erlang-questions<br></blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; "><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">"Installing applications can lead to corruption over time. </span><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"</span></div><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; "><br></span></div></span><br class="Apple-interchange-newline">
</div>
<br></div></body></html>