Skip to content

Commit 3a80ab5

Browse files
committed
RUBY-886 verify server certificates by default when ssl is true
1 parent 8c2fbe0 commit 3a80ab5

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

lib/mongo/socket/ssl.rb

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,34 @@ def readbyte
9797

9898
def create_context(options)
9999
context = OpenSSL::SSL::SSLContext.new
100-
if options[:ssl_cert]
101-
context.cert = OpenSSL::X509::Certificate.new(File.open(options[:ssl_cert]))
102-
end
103-
if options[:ssl_key]
104-
if options[:ssl_key_pass_phrase]
105-
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]),
106-
options[:ssl_key_pass_phrase])
107-
else
108-
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]))
109-
end
100+
set_cert(context, options) if options[:ssl_cert]
101+
set_key(context, options) if options[:ssl_key]
102+
set_cert_verification(context, options) unless options[:ssl_verify] == false
103+
context
104+
end
105+
106+
def set_cert(context, options)
107+
context.cert = OpenSSL::X509::Certificate.new(File.open(options[:ssl_cert]))
108+
end
109+
110+
def set_key(context, options)
111+
if options[:ssl_key_pass_phrase]
112+
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]),
113+
options[:ssl_key_pass_phrase])
114+
else
115+
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]))
110116
end
111-
if options[:ssl_verify] || options[:ssl_ca_cert]
112-
context.ca_file = options[:ssl_ca_cert]
113-
context.verify_mode = OpenSSL::SSL::VERIFY_PEER
117+
end
118+
119+
def set_cert_verification(context, options)
120+
context.verify_mode = OpenSSL::SSL::VERIFY_PEER
121+
cert_store = OpenSSL::X509::Store.new
122+
if options[:ssl_ca_cert]
123+
cert_store.add_file(options[:ssl_ca_cert])
124+
else
125+
cert_store.set_default_paths
114126
end
115-
context
127+
context.cert_store = cert_store
116128
end
117129

118130
def verify_certificate!(socket)

0 commit comments

Comments
 (0)