# Upgrading a socket to an SSLSocket

**URL:** <https://www.ruby-forum.com/t/upgrading-a-socket-to-an-sslsocket/187901>\
**Category:** Ruby\
**Created:** [May 1, 2010, 11:44am UTC](https://www.ruby-forum.com/t/upgrading-a-socket-to-an-sslsocket/187901 "2010-05-01T11:44:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![baduk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@baduk](https://www.ruby-forum.com/u/baduk)\
**Post date:** [May 1, 2010, 11:44am UTC](https://www.ruby-forum.com/t/upgrading-a-socket-to-an-sslsocket/187901/1 "2010-05-01T11:44:11Z")

</div>

Is it possible to ‘upgrade’ a normal socket to an SSL socket. I would  
like to transmit and receive some data in plain text initially, before  
performing the SSL handshake and start communicating securely, without  
having to close and create a new socket.

Does anyone have an example of this, preferably the more complete the  
better =)

Thanks

---

<div class="post-metadata">

**Author:** ![baduk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@baduk](https://www.ruby-forum.com/u/baduk)\
**Post date:** [May 2, 2010, 1:50am UTC](https://www.ruby-forum.com/t/upgrading-a-socket-to-an-sslsocket/187901/2 "2010-05-02T01:50:30Z")

</div>

> Is it possible to ‘upgrade’ a normal socket to an SSL socket. I would  
> like to transmit and receive some data in plain text initially, before  
> performing the SSL handshake and start communicating securely, without  
> having to close and create a new socket.

I think you can do it with eventmachine, since you call start\_tls  
whenever you want. not sure about normal sockets, maybe there’s a  
constructor to SSLSocket that takes a descriptor so you can pass  
descriptors about?  
-rp

---

<div class="post-metadata">

**Author:** ![baduk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@baduk](https://www.ruby-forum.com/u/baduk)\
**Post date:** [May 2, 2010, 10:43am UTC](https://www.ruby-forum.com/t/upgrading-a-socket-to-an-sslsocket/187901/3 "2010-05-02T10:43:56Z")

</div>

Baduk Baduk wrote:

> Is it possible to ‘upgrade’ a normal socket to an SSL socket. I would  
> like to transmit and receive some data in plain text initially, before  
> performing the SSL handshake and start communicating securely, without  
> having to close and create a new socket.
> 
> Does anyone have an example of this, preferably the more complete the  
> better =)

There’s a complete working example of this in ruby-ldapserver, see  
lib/ldap/server/connection.rb. The core is:

```
def startssl # :yields:
  @mutex.synchronize do
    raise LDAP::ResultError::OperationsError if @ssl or 

```

@active\_reqs.size \> 0  
yield if block\_given?  
@io = OpenSSL::SSL::SSLSocket.new(@io, @opt[:ssl\_ctx])  
@io.sync\_close = true  
@io.accept  
@ssl = true  
end  
end
