From: "Iñaki Baz Castillo" Date: 2011-12-03T00:47:18+09:00 Subject: Re: Using OpenSSL for making a network TLS server 2011/12/2 John Downey : > Ruby's OpenSSL library has an SSLServer class that will do exactly what > you're asking. Here is just some quick example code (most of the code is > just building a self-signed cert, you could just as easily load this from a > file): > > require 'openssl' > require 'socket' > > tcp_server = TCPServer.new 8080 > > ctx = OpenSSL::SSL::SSLContext.new > ctx.key = OpenSSL::PKey::RSA.new 2048 > ctx.cert = OpenSSL::X509::Certificate.new > ctx.cert.subject = OpenSSL::X509::Name.new [['CN', 'localhost']] > ctx.cert.issuer = ctx.cert.subject > ctx.cert.public_key = ctx.key > ctx.cert.not_before = Time.now > ctx.cert.not_after = Time.now + 60 * 60 * 24 > ctx.cert.sign ctx.key, OpenSSL::Digest::SHA1.new > > server = OpenSSL::SSL::SSLServer.new tcp_server, ctx > socket = server.accept > puts socket.gets > > With anything having to do with crypto I would highly recommend not trying > to create the solution yourself (it is far to easy to screw up). Instead > you should take time to review existing solutions that have been "battle > hardened." Really thanks a lot. -- Iñaki Baz Castillo