I have a mail server to which I'm connecting from a ruby application using protocol IMAP. But I get an error in rails console:
Loading dev environment (Rails 7.0.4) irb(main):001:0> @connection = Net::IMAP.new('mail.test.com.test', 993, true) Traceback (most recent call last): 2: from (irb):1 1: from (irb):1:in `new' Errno::ECONNRESET (Connection reset by peer - SSL_connect) Vesrions used:
rails: 7.0.4 ruby: ruby 2.7.3p183 Note:
We have firewall in between, and rule for port 993 is allowed.
nc -vz mail.test.com.test 993 mail.test.com.test (192.168.1.186:993) open So I started testing with OpenSSL over implicit port 993 and recieved error:
openssl s_client -connect mail.test.com.test:993 CONNECTED(00000003) write:errno=104 --- no peer certificate available --- No client certificate CA names sent --- SSL handshake has read 0 bytes and written 319 bytes Verification: OK --- New, (NONE), Cipher is (NONE) Secure Renegotiation IS NOT supported No ALPN negotiated Early data was not sent Verify return code: 0 (ok) --- But it seems that the IMAP connection over an explicit port 143 with TLS works fine:
openssl s_client -connect mail.test.com.test:143 -starttls imap -brief CONNECTION ESTABLISHED Protocol version: TLSv1.2 Ciphersuite: <suite> Peer certificate: CN = mail.test.com.test Hash used: SHA256 Signature type: RSA Verification: OK Server Temp Key: <key>, P-384, 384 bits . OK CAPABILITY completed. In the rails console, it would be like this:
irb(main):061:0> @connection = Net::IMAP.new('mail.test.com.test', 143, false) => #<Net::IMAP:0x0000f0 @mon_data=#<Monitor:0x0000d78>, @mon_data_owner_object_id=5230, @host="mail.test.com.test", @port=143, @tag_prefix="RUBY", @tagno=0, @open_timeout=30, @idl... irb(main):064:0> @connection.starttls => #<struct Net::IMAP::TaggedResponse tag="RUBY0001", name="OK", data=#<struct Net::IMAP::ResponseText code=nil, text="Begin TLS negotiation now.">, raw_data="RUBY0001 OK Begin TLS negotiation now.\r\n"> Why does this happen? How can I fix this issue?