Skip to content

IOError with OpenSSL #connect_nonblock: Writing not possible during handshake #1715

Closed
@Zapotek

Description

@Zapotek

Hey guys,

I've got another socket/SSL inconsistency between MRI and JRuby.
I'm not 100% sure but its root cause may be the same as #1694.

Code:

require 'socket'
require 'openssl'

def start_server( host, port )
    s = TCPServer.new( host, port )

    context                 = OpenSSL::SSL::SSLContext.new
    context.key             = OpenSSL::PKey::RSA.new( 2048 )
    context.cert            = OpenSSL::X509::Certificate.new
    context.cert.subject    = OpenSSL::X509::Name.new( [['CN', 'localhost']] )
    context.cert.issuer     = context.cert.subject
    context.cert.public_key = context.key
    context.cert.not_before = Time.now
    context.cert.not_after  = Time.now + 60 * 60 * 24
    context.verify_mode     = OpenSSL::SSL::VERIFY_NONE
    context.cert.version    = 2
    context.cert.serial     = 1
    context.cert.sign(context.key, OpenSSL::Digest::SHA1.new)

    OpenSSL::SSL::SSLServer.new( s, context )
end

def connect( host, port )
    context = OpenSSL::SSL::SSLContext.new
    context.verify_mode = OpenSSL::SSL::VERIFY_NONE

    s = OpenSSL::SSL::SSLSocket.new( TCPSocket.new( host, port ), context )
    s.sync_close = true

    begin
        s.connect_nonblock
        # Works with #connect
    rescue IO::WaitReadable, IO::WaitWritable
    end

    s
end

address = ['127.0.0.1', 7331]

server = start_server( *address )

t = Thread.new do
    puts "Got: #{server.accept.readpartial(1024).inspect}"
end

c = connect( *address )

p IO.select( nil, [c] )
# => [[], [#<OpenSSL::SSL::SSLSocket:0x7a21bdb8 @context=#<OpenSSL::SSL::SSLContext:0x469fea95 @verify_mode=0>, @eof=false, @sync_close=true, @rbuffer="", @hostname="", @io=#<TCPSocket:fd 17>, @sync=true>], []]

begin
    c.write 'stuff'
rescue => e
    p e
    # #<IOError: Writing not possible during handshake>
    puts e.backtrace.join( "\n" )
    # org/jruby/ext/openssl/SSLSocket.java:673:in `syswrite'
    # /home/zapotek/.rvm/rubies/jruby-1.7.12/lib/ruby/shared/jopenssl19/openssl/buffering.rb:318:in `do_write'
    # /home/zapotek/.rvm/rubies/jruby-1.7.12/lib/ruby/shared/jopenssl19/openssl/buffering.rb:336:in `write'
    # tmp/jruby_openssl.rb:51:in `(root)'

    t.kill
end

t.join

MRI 2.1.2:

Got: "stuff"

JRuby (jruby 1.7.12 (2.0.0p195) 2014-04-15 643e292 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_55-b13 [linux-amd64]):

#<IOError: Writing not possible during handshake>
org/jruby/ext/openssl/SSLSocket.java:673:in `syswrite'
/home/zapotek/.rvm/rubies/jruby-1.7.12/lib/ruby/shared/jopenssl19/openssl/buffering.rb:318:in `do_write'
/home/zapotek/.rvm/rubies/jruby-1.7.12/lib/ruby/shared/jopenssl19/openssl/buffering.rb:336:in `write'
tmp/jruby_openssl.rb:52:in `(root)'

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions