Skip to content

Commit 2e3e77b

Browse files
committed
Merge pull request #161 from sonOfRa/tlsoptions
Implement custom tls options
2 parents b8deeec + acffb16 commit 2e3e77b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib/net/ldap.rb

+21-5
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,6 @@ def authenticate(username, password)
537537
# additional capabilities are added, more configuration values will be
538538
# added here.
539539
#
540-
# Currently, the only supported argument is { :method => :simple_tls }.
541-
# (Equivalently, you may pass the symbol :simple_tls all by itself,
542-
# without enclosing it in a Hash.)
543-
#
544540
# The :simple_tls encryption method encrypts <i>all</i> communications
545541
# with the LDAP server. It completely establishes SSL/TLS encryption with
546542
# the LDAP server before any LDAP-protocol data is exchanged. There is no
@@ -563,10 +559,30 @@ def authenticate(username, password)
563559
# The :start_tls like the :simple_tls encryption method also encrypts all
564560
# communcations with the LDAP server. With the exception that it operates
565561
# over the standard TCP port.
562+
#
563+
# In order to verify certificates and enable other TLS options, the
564+
# :tls_options hash can be passed alongside :simple_tls or :start_tls.
565+
# This hash contains any options that can be passed to
566+
# OpenSSL::SSL::SSLContext#set_params(). The most common options passed
567+
# should be OpenSSL::SSL::SSLContext::DEFAULT_PARAMS, or the :ca_file option,
568+
# which contains a path to a Certificate Authority file (PEM-encoded).
569+
#
570+
# Example for a default setup without custom settings:
571+
# {
572+
# :method => :simple_tls,
573+
# :tls_options => OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
574+
# }
575+
#
576+
# Example for specifying a CA-File and only allowing TLSv1.1 connections:
577+
#
578+
# {
579+
# :method => :start_tls,
580+
# :tls_options => { :ca_file => "/etc/cafile.pem", :ssl_version => "TLSv1_1" }
581+
# }
566582
def encryption(args)
567583
case args
568584
when :simple_tls, :start_tls
569-
args = { :method => args }
585+
args = { :method => args, :tls_options => {} }
570586
end
571587
@encryption = args
572588
end

lib/net/ldap/connection.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ def close
4141
end
4242
end
4343

44-
def self.wrap_with_ssl(io)
44+
def self.wrap_with_ssl(io, tls_options = {})
4545
raise Net::LDAP::LdapError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL
46+
4647
ctx = OpenSSL::SSL::SSLContext.new
48+
49+
# By default, we do not verify certificates. For a 1.0 release, this should probably be changed at some point.
50+
# See discussion in https://github.com/ruby-ldap/ruby-net-ldap/pull/161
51+
ctx.set_params(tls_options) unless tls_options.empty?
52+
4753
conn = OpenSSL::SSL::SSLSocket.new(io, ctx)
4854
conn.connect
4955

@@ -85,7 +91,7 @@ def self.wrap_with_ssl(io)
8591
def setup_encryption(args)
8692
case args[:method]
8793
when :simple_tls
88-
@conn = self.class.wrap_with_ssl(@conn)
94+
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
8995
# additional branches requiring server validation and peer certs, etc.
9096
# go here.
9197
when :start_tls
@@ -102,7 +108,7 @@ def setup_encryption(args)
102108
end
103109

104110
if pdu.result_code.zero?
105-
@conn = self.class.wrap_with_ssl(@conn)
111+
@conn = self.class.wrap_with_ssl(@conn, args[:tls_options])
106112
else
107113
raise Net::LDAP::LdapError, "start_tls failed: #{pdu.result_code}"
108114
end

test/test_ldap_connection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_queued_read_setup_encryption_with_start_tls
202202
and_return(result2)
203203
mock.should_receive(:write)
204204
conn = Net::LDAP::Connection.new(:socket => mock)
205-
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock).
205+
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, nil).
206206
and_return(mock)
207207

208208
conn.next_msgid # simulates ongoing query

0 commit comments

Comments
 (0)