Skip to content

Commit

Permalink
Merge pull request #406 from jpdasma/sni-support
Browse files Browse the repository at this point in the history
Add support to use SNI
  • Loading branch information
HarlemSquirrel authored Sep 2, 2022
2 parents 5fa0213 + af098c5 commit 3f94287
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def socket_class=(socket_class)
def prepare_socket(server, timeout=nil)
socket = server[:socket]
encryption = server[:encryption]
hostname = server[:host]

@conn = socket
setup_encryption(encryption, timeout) if encryption
setup_encryption(encryption, timeout, hostname) if encryption
end

def open_connection(server)
Expand Down Expand Up @@ -86,7 +87,7 @@ def close
end
end

def self.wrap_with_ssl(io, tls_options = {}, timeout=nil)
def self.wrap_with_ssl(io, tls_options = {}, timeout=nil, hostname=nil)
raise Net::LDAP::NoOpenSSLError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL

ctx = OpenSSL::SSL::SSLContext.new
Expand All @@ -96,6 +97,7 @@ def self.wrap_with_ssl(io, tls_options = {}, timeout=nil)
ctx.set_params(tls_options) unless tls_options.empty?

conn = OpenSSL::SSL::SSLSocket.new(io, ctx)
conn.hostname = hostname

begin
if timeout
Expand Down Expand Up @@ -148,11 +150,11 @@ def self.wrap_with_ssl(io, tls_options = {}, timeout=nil)
# communications, as with simple_tls. Thanks for Kouhei Sutou for
# generously contributing the :start_tls path.
#++
def setup_encryption(args, timeout=nil)
def setup_encryption(args, timeout=nil, hostname=nil)
args[:tls_options] ||= {}
case args[:method]
when :simple_tls
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout, hostname)
# additional branches requiring server validation and peer certs, etc.
# go here.
when :start_tls
Expand All @@ -170,7 +172,7 @@ def setup_encryption(args, timeout=nil)

raise Net::LDAP::StartTLSError,
"start_tls failed: #{pdu.result_code}" unless pdu.result_code.zero?
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout)
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout, hostname)
else
raise Net::LDAP::EncMethodUnsupportedError, "unsupported encryption method #{args[:method]}"
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_queued_read_setup_encryption_with_start_tls
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil)
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil, nil)
.and_return(mock)

conn.next_msgid # simulates ongoing query
Expand Down

0 comments on commit 3f94287

Please sign in to comment.