Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I figured it out, thanks to that link to some decent documentation.</p> <p>For one thing, SSLSocket.connect() is only meant to be called on the client.</p> <p>But the main problem is that I'm trying to take a GServer socket and upgrade it to SSL. Instead, I should use OpenSSL::SSL::SSLServer.</p> <p>Also, I separated my certificate and private key into two files.</p> <p>Here is the working server:</p> <pre><code>#!/usr/bin/ruby require "socket" require "openssl" require "thread" listeningPort = Integer(ARGV[0]) server = TCPServer.new(listeningPort) sslContext = OpenSSL::SSL::SSLContext.new sslContext.cert = OpenSSL::X509::Certificate.new(File.open("cert.pem")) sslContext.key = OpenSSL::PKey::RSA.new(File.open("priv.pem")) sslServer = OpenSSL::SSL::SSLServer.new(server, sslContext) puts "Listening on port #{listeningPort}" loop do connection = sslServer.accept Thread.new { begin while (lineIn = connection.gets) lineIn = lineIn.chomp $stdout.puts "=&gt; " + lineIn lineOut = "You said: " + lineIn $stdout.puts "&lt;= " + lineOut connection.puts lineOut end rescue $stderr.puts $! end } end </code></pre> <p>And client:</p> <pre><code>#!/usr/bin/ruby require "socket" require "thread" require "openssl" host = ARGV[0] port = Integer(ARGV[1]) socket = TCPSocket.new(host, port) expectedCert = OpenSSL::X509::Certificate.new(File.open("cert.pem")) ssl = OpenSSL::SSL::SSLSocket.new(socket) ssl.sync_close = true ssl.connect if ssl.peer_cert.to_s != expectedCert.to_s stderrr.puts "Unexpected certificate" exit(1) end Thread.new { begin while lineIn = ssl.gets lineIn = lineIn.chomp $stdout.puts lineIn end rescue $stderr.puts "Error in input loop: " + $! end } while (lineOut = $stdin.gets) lineOut = lineOut.chomp ssl.puts lineOut end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload