Skip to content

Commit a192625

Browse files
committed
RUBY-886 verify server certificates by default when ssl is true
1 parent 5c60606 commit a192625

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
@@ -100,22 +100,34 @@ def readbyte
100100

101101
def create_context(options)
102102
context = OpenSSL::SSL::SSLContext.new
103-
if options[:ssl_cert]
104-
context.cert = OpenSSL::X509::Certificate.new(File.open(options[:ssl_cert]))
105-
end
106-
if options[:ssl_key]
107-
if options[:ssl_key_pass_phrase]
108-
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]),
109-
options[:ssl_key_pass_phrase])
110-
else
111-
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]))
112-
end
103+
set_cert(context, options) if options[:ssl_cert]
104+
set_key(context, options) if options[:ssl_key]
105+
set_cert_verification(context, options) unless options[:ssl_verify] == false
106+
context
107+
end
108+
109+
def set_cert(context, options)
110+
context.cert = OpenSSL::X509::Certificate.new(File.open(options[:ssl_cert]))
111+
end
112+
113+
def set_key(context, options)
114+
if options[:ssl_key_pass_phrase]
115+
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]),
116+
options[:ssl_key_pass_phrase])
117+
else
118+
context.key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]))
113119
end
114-
if options[:ssl_verify] || options[:ssl_ca_cert]
115-
context.ca_file = options[:ssl_ca_cert]
116-
context.verify_mode = OpenSSL::SSL::VERIFY_PEER
120+
end
121+
122+
def set_cert_verification(context, options)
123+
context.verify_mode = OpenSSL::SSL::VERIFY_PEER
124+
cert_store = OpenSSL::X509::Store.new
125+
if options[:ssl_ca_cert]
126+
cert_store.add_file(options[:ssl_ca_cert])
127+
else
128+
cert_store.set_default_paths
117129
end
118-
context
130+
context.cert_store = cert_store
119131
end
120132

121133
def verify_certificate!(socket)

0 commit comments

Comments
 (0)