diff options
author | Eric Wong <e@80x24.org> | 2014-11-29 04:08:54 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2014-12-02 02:10:12 +0000 |
commit | 65a903181cd5cdd78b4df7eacc1c574f0ef8e95c (patch) | |
tree | 2887d483c14b975bdab99e6150e8a4bdf6a4e619 /lib/yahns/openssl_client.rb | |
parent | a1dba8aa91a533870c44ec0b695391f16be9a71f (diff) | |
download | yahns-65a903181cd5cdd78b4df7eacc1c574f0ef8e95c.tar.gz |
The current CA model and code quality of OpenSSL have long put me off from supporting TLS; however but efforts such as "Let's Encrypt" and the fallout from Heartbleed give me hope for the future. This implements, as much as possible, a "hands-off" approach to TLS support via OpenSSL. This implementation allows us to shift responsibility away from us to users and upstreams (the Ruby 'openssl' extension maintainers, software packagers, and OpenSSL project itself). This is also perhaps the easiest way for now for us, while being most powerful for users. It requires users to configure their own OpenSSL context object which we'll use as-is. This context object is used as the :ssl_ctx parameter to the "listen" directive in the yahns configuration file: require 'openssl' # we will not do this for the user, even ctx = OpenSSL::SSL::SSLContext.new # user must configure ctx here... listen 443, ssl_ctx: ctx This way, in case we support GnuTLS or other TLS libraries, there'll be less confusion as to what a user is actually using. Note: this feature requires Ruby 2.1 and later for non-kgio {read,write}_nonblock(.. exception: false) support.
Diffstat (limited to 'lib/yahns/openssl_client.rb')
-rw-r--r-- | lib/yahns/openssl_client.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/yahns/openssl_client.rb b/lib/yahns/openssl_client.rb new file mode 100644 index 0000000..e4e76c9 --- /dev/null +++ b/lib/yahns/openssl_client.rb @@ -0,0 +1,52 @@ +# Copyright (C) 2014, all contributors <yahns-public@yhbt.net> +# License: GPLv3 or later (see COPYING for details) + +require_relative 'sendfile_compat' + +# this is to be included into a Kgio::Socket-derived class +# this requires Ruby 2.1 and later for "exception: false" +module Yahns::OpenSSLClient # :nodoc: + include Yahns::SendfileCompat + + def yahns_init_ssl(ssl_ctx) + @need_accept = true + @ssl = OpenSSL::SSL::SSLSocket.new(self, ssl_ctx) + end + + def kgio_trywrite(buf) + rv = @ssl.write_nonblock(buf, exception: false) + Integer === rv and + rv = buf.bytesize == rv ? nil : buf.byteslice(rv, buf.bytesize) + rv + end + + def kgio_syssend(buf, flags) + kgio_trywrite(buf) + end + + def kgio_tryread(len, buf) + if @need_accept + # most protocols require read before write, so we start the negotiation + # process here: + begin + @ssl.accept_nonblock + rescue IO::WaitReadable + return :wait_readable + rescue IO::WaitWritable + return :wait_writable + end + @need_accept = false + end + @ssl.read_nonblock(len, buf, exception: false) + end + + def shutdown(*args) + @ssl.shutdown(*args) + super # BasicSocket#shutdown + end + + def close + @ssl.close + super # IO#close + end +end |