From b830786f2d025dac69db636f10ed6107d50ee068 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Wed, 5 Aug 2015 22:55:38 +0000 Subject: * ext/openssl/lib/openssl/ssl.rb (module OpenSSL): move OpenSSL::SSL::SSLSocket#initialize to Ruby. * ext/openssl/ossl_ssl.c: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51495 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/lib/openssl/ssl.rb | 45 +++++++++++++++++++++++ ext/openssl/ossl_ssl.c | 82 ++++++++++-------------------------------- 2 files changed, 63 insertions(+), 64 deletions(-) (limited to 'ext') diff --git a/ext/openssl/lib/openssl/ssl.rb b/ext/openssl/lib/openssl/ssl.rb index cfa2a0c117..8fba3ee369 100644 --- a/ext/openssl/lib/openssl/ssl.rb +++ b/ext/openssl/lib/openssl/ssl.rb @@ -94,6 +94,15 @@ module OpenSSL attr_accessor :tmp_dh_callback + if ExtConfig::HAVE_TLSEXT_HOST_NAME + # A callback invoked at connect time to distinguish between multiple + # server names. + # + # The callback is invoked with an SSLSocket and a server name. The + # callback must return an SSLContext for the server name or nil. + attr_accessor :servername_cb + end + # call-seq: # SSLContext.new => ctx # SSLContext.new(:TLSv1) => ctx @@ -253,6 +262,42 @@ module OpenSSL include SocketForwarder include Nonblock + if ExtConfig::OPENSSL_NO_SOCK + def initialize(io, ctx = nil); raise NotImplmentedError; end + else + if ExtConfig::HAVE_TLSEXT_HOST_NAME + attr_accessor :hostname + end + + attr_reader :io, :context + attr_accessor :sync_close + alias :to_io :io + + # call-seq: + # SSLSocket.new(io) => aSSLSocket + # SSLSocket.new(io, ctx) => aSSLSocket + # + # Creates a new SSL socket from +io+ which must be a real ruby object (not an + # IO-like object that responds to read/write). + # + # If +ctx+ is provided the SSL Sockets initial params will be taken from + # the context. + # + # The OpenSSL::Buffering module provides additional IO methods. + # + # This method will freeze the SSLContext if one is provided; + # however, session management is still allowed in the frozen SSLContext. + + def initialize(io, context = OpenSSL::SSL::SSLContext.new) + @io = io + @context = context + @sync_close = false + @hostname = nil + context.setup + super() + end + end + ## # Perform hostname verification after an SSL connection is established # diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index 58f53032a2..286c447c82 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -29,6 +29,7 @@ } while (0) VALUE mSSL; +VALUE mSSLExtConfig; VALUE eSSLError; VALUE cSSLContext; VALUE cSSLSocket; @@ -71,22 +72,11 @@ static VALUE eSSLErrorWaitWritable; #define ossl_ssl_get_x509(o) rb_iv_get((o),"@x509") #define ossl_ssl_get_key(o) rb_iv_get((o),"@key") -#define ossl_ssl_set_io(o,v) rb_iv_set((o),"@io",(v)) -#define ossl_ssl_set_ctx(o,v) rb_iv_set((o),"@context",(v)) -#define ossl_ssl_set_sync_close(o,v) rb_iv_set((o),"@sync_close",(v)) #define ossl_ssl_set_x509(o,v) rb_iv_set((o),"@x509",(v)) #define ossl_ssl_set_key(o,v) rb_iv_set((o),"@key",(v)) #define ossl_ssl_set_tmp_dh(o,v) rb_iv_set((o),"@tmp_dh",(v)) #define ossl_ssl_set_tmp_ecdh(o,v) rb_iv_set((o),"@tmp_ecdh",(v)) -static const char *ossl_ssl_attr_readers[] = { "io", "context", }; -static const char *ossl_ssl_attrs[] = { -#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME - "hostname", -#endif - "sync_close", -}; - ID ID_callback_state; static VALUE sym_exception, sym_wait_readable, sym_wait_writable; @@ -1189,44 +1179,6 @@ ossl_ssl_s_alloc(VALUE klass) return TypedData_Wrap_Struct(klass, &ossl_ssl_type, NULL); } -/* - * call-seq: - * SSLSocket.new(io) => aSSLSocket - * SSLSocket.new(io, ctx) => aSSLSocket - * - * Creates a new SSL socket from +io+ which must be a real ruby object (not an - * IO-like object that responds to read/write). - * - * If +ctx+ is provided the SSL Sockets initial params will be taken from - * the context. - * - * The OpenSSL::Buffering module provides additional IO methods. - * - * This method will freeze the SSLContext if one is provided; - * however, session management is still allowed in the frozen SSLContext. - */ -static VALUE -ossl_ssl_initialize(int argc, VALUE *argv, VALUE self) -{ - VALUE io, ctx; - - if (rb_scan_args(argc, argv, "11", &io, &ctx) == 1) { - ctx = rb_funcall(cSSLContext, rb_intern("new"), 0); - } - OSSL_Check_Kind(ctx, cSSLContext); - Check_Type(io, T_FILE); - ossl_ssl_set_io(self, io); - ossl_ssl_set_ctx(self, ctx); - ossl_ssl_set_sync_close(self, Qfalse); - ossl_sslctx_setup(ctx); - - rb_iv_set(self, "@hostname", Qnil); - - rb_call_super(0, 0); - - return self; -} - static VALUE ossl_ssl_setup(VALUE self) { @@ -1986,6 +1938,17 @@ Init_ossl_ssl(void) * of SSLContext to set up connections. */ mSSL = rb_define_module_under(mOSSL, "SSL"); + + /* Document-module: OpenSSL::ExtConfig + * + * This module contains configuration information about the SSL extension, + * for example if socket support is enabled, or the host name TLS extension + * is enabled. Constants in this module will always be defined, but contain + * `true` or `false` values depending on the configuration of your OpenSSL + * installation. + */ + mSSLExtConfig = rb_define_module_under(mOSSL, "ExtConfig"); + /* Document-class: OpenSSL::SSL::SSLError * * Generic error class raised by SSLSocket and SSLContext. @@ -2138,15 +2101,11 @@ Init_ossl_ssl(void) rb_attr(cSSLContext, rb_intern("session_remove_cb"), 1, 1, Qfalse); #ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME - /* - * A callback invoked at connect time to distinguish between multiple - * server names. - * - * The callback is invoked with an SSLSocket and a server name. The - * callback must return an SSLContext for the server name or nil. - */ - rb_attr(cSSLContext, rb_intern("servername_cb"), 1, 1, Qfalse); + rb_define_const(mSSLExtConfig, "HAVE_TLSEXT_HOST_NAME", Qtrue); +#else + rb_define_const(mSSLExtConfig, "HAVE_TLSEXT_HOST_NAME", Qfalse); #endif + /* * A callback invoked whenever a new handshake is initiated. May be used * to disable renegotiation entirely. @@ -2316,15 +2275,10 @@ Init_ossl_ssl(void) */ cSSLSocket = rb_define_class_under(mSSL, "SSLSocket", rb_cObject); #ifdef OPENSSL_NO_SOCK - rb_define_method(cSSLSocket, "initialize", rb_notimplement, -1); + rb_define_const(mSSLExtConfig, "OPENSSL_NO_SOCK", Qtrue); #else + rb_define_const(mSSLExtConfig, "OPENSSL_NO_SOCK", Qfalse); rb_define_alloc_func(cSSLSocket, ossl_ssl_s_alloc); - for(i = 0; i < numberof(ossl_ssl_attr_readers); i++) - rb_attr(cSSLSocket, rb_intern(ossl_ssl_attr_readers[i]), 1, 0, Qfalse); - for(i = 0; i < numberof(ossl_ssl_attrs); i++) - rb_attr(cSSLSocket, rb_intern(ossl_ssl_attrs[i]), 1, 1, Qfalse); - rb_define_alias(cSSLSocket, "to_io", "io"); - rb_define_method(cSSLSocket, "initialize", ossl_ssl_initialize, -1); rb_define_method(cSSLSocket, "connect", ossl_ssl_connect, 0); rb_define_method(cSSLSocket, "connect_nonblock", ossl_ssl_connect_nonblock, -1); rb_define_method(cSSLSocket, "accept", ossl_ssl_accept, 0); -- cgit v1.2.3