2003-10-04 13:08:23 -04:00
|
|
|
require 'socket'
|
|
|
|
require 'openssl'
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
module DRb
|
|
|
|
|
2013-01-24 22:25:39 -05:00
|
|
|
# The protocol for DRb over an SSL socket
|
|
|
|
#
|
|
|
|
# The URI for a DRb socket over SSL is:
|
|
|
|
# <code>drbssl://<host>:<port>?<option></code>. The option is optional
|
2003-10-04 13:08:23 -04:00
|
|
|
class DRbSSLSocket < DRbTCPSocket
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# SSLConfig handles the needed SSL information for establishing a
|
|
|
|
# DRbSSLSocket connection, including generating the X509 / RSA pair.
|
|
|
|
#
|
|
|
|
# An instance of this config can be passed to DRbSSLSocket.new,
|
|
|
|
# DRbSSLSocket.open and DRbSSLSocket.open_server
|
|
|
|
#
|
|
|
|
# See DRb::DRbSSLSocket::SSLConfig.new for more details
|
2003-10-04 13:08:23 -04:00
|
|
|
class SSLConfig
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Default values for a SSLConfig instance.
|
|
|
|
#
|
|
|
|
# See DRb::DRbSSLSocket::SSLConfig.new for more details
|
2003-10-04 13:08:23 -04:00
|
|
|
DEFAULT = {
|
2011-05-18 17:19:18 -04:00
|
|
|
:SSLCertificate => nil,
|
|
|
|
:SSLPrivateKey => nil,
|
|
|
|
:SSLClientCA => nil,
|
|
|
|
:SSLCACertificatePath => nil,
|
|
|
|
:SSLCACertificateFile => nil,
|
2012-09-20 04:05:08 -04:00
|
|
|
:SSLTmpDhCallback => nil,
|
2011-05-18 17:19:18 -04:00
|
|
|
:SSLVerifyMode => ::OpenSSL::SSL::VERIFY_NONE,
|
|
|
|
:SSLVerifyDepth => nil,
|
|
|
|
:SSLVerifyCallback => nil, # custom verification
|
2003-10-04 13:08:23 -04:00
|
|
|
:SSLCertificateStore => nil,
|
2011-05-18 17:19:18 -04:00
|
|
|
# Must specify if you use auto generated certificate.
|
|
|
|
:SSLCertName => nil, # e.g. [["CN","fqdn.example.com"]]
|
|
|
|
:SSLCertComment => "Generated by Ruby/OpenSSL"
|
2003-10-04 13:08:23 -04:00
|
|
|
}
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Create a new DRb::DRbSSLSocket::SSLConfig instance
|
|
|
|
#
|
|
|
|
# The DRb::DRbSSLSocket will take either a +config+ Hash or an instance
|
2013-11-28 00:44:29 -05:00
|
|
|
# of SSLConfig, and will setup the certificate for its session for the
|
2013-01-24 23:02:46 -05:00
|
|
|
# configuration. If want it to generate a generic certificate, the bare
|
|
|
|
# minimum is to provide the :SSLCertName
|
|
|
|
#
|
|
|
|
# === Config options
|
|
|
|
#
|
|
|
|
# From +config+ Hash:
|
|
|
|
#
|
|
|
|
# :SSLCertificate ::
|
|
|
|
# An instance of OpenSSL::X509::Certificate. If this is not provided,
|
|
|
|
# then a generic X509 is generated, with a correspond :SSLPrivateKey
|
|
|
|
#
|
|
|
|
# :SSLPrivateKey ::
|
|
|
|
# A private key instance, like OpenSSL::PKey::RSA. This key must be
|
|
|
|
# the key that signed the :SSLCertificate
|
|
|
|
#
|
|
|
|
# :SSLClientCA ::
|
|
|
|
# An OpenSSL::X509::Certificate, or Array of certificates that will
|
|
|
|
# used as ClientCAs in the SSL Context
|
|
|
|
#
|
|
|
|
# :SSLCACertificatePath ::
|
|
|
|
# A path to the directory of CA certificates. The certificates must
|
|
|
|
# be in PEM format.
|
|
|
|
#
|
|
|
|
# :SSLCACertificateFile ::
|
|
|
|
# A path to a CA certificate file, in PEM format.
|
|
|
|
#
|
|
|
|
# :SSLTmpDhCallback ::
|
|
|
|
# A DH callback. See OpenSSL::SSL::SSLContext.tmp_dh_callback
|
|
|
|
#
|
|
|
|
# :SSLVerifyMode ::
|
|
|
|
# This is the SSL verification mode. See OpenSSL::SSL::VERIFY_* for
|
|
|
|
# available modes. The default is OpenSSL::SSL::VERIFY_NONE
|
|
|
|
#
|
|
|
|
# :SSLVerifyDepth ::
|
|
|
|
# Number of CA certificates to walk, when verifying a certificate
|
|
|
|
# chain.
|
|
|
|
#
|
|
|
|
# :SSLVerifyCallback ::
|
|
|
|
# A callback to be used for additional verification. See
|
|
|
|
# OpenSSL::SSL::SSLContext.verify_callback
|
|
|
|
#
|
|
|
|
# :SSLCertificateStore ::
|
|
|
|
# A OpenSSL::X509::Store used for verification of certificates
|
|
|
|
#
|
|
|
|
# :SSLCertName ::
|
|
|
|
# Issuer name for the certificate. This is required when generating
|
|
|
|
# the certificate (if :SSLCertificate and :SSLPrivateKey were not
|
|
|
|
# given). The value of this is to be an Array of pairs:
|
|
|
|
#
|
|
|
|
# [["C", "Raleigh"], ["ST","North Carolina"],
|
|
|
|
# ["CN","fqdn.example.com"]]
|
|
|
|
#
|
|
|
|
# See also OpenSSL::X509::Name
|
|
|
|
#
|
|
|
|
# :SSLCertComment ::
|
|
|
|
# A comment to be used for generating the certificate. The default is
|
|
|
|
# "Generated by Ruby/OpenSSL"
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# === Example
|
|
|
|
#
|
|
|
|
# These values can be added after the fact, like a Hash.
|
|
|
|
#
|
|
|
|
# require 'drb/ssl'
|
|
|
|
# c = DRb::DRbSSLSocket::SSLConfig.new {}
|
|
|
|
# c[:SSLCertificate] =
|
|
|
|
# OpenSSL::X509::Certificate.new(File.read('mycert.crt'))
|
|
|
|
# c[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(File.read('mycert.key'))
|
|
|
|
# c[:SSLVerifyMode] = OpenSSL::SSL::VERIFY_PEER
|
|
|
|
# c[:SSLCACertificatePath] = "/etc/ssl/certs/"
|
|
|
|
# c.setup_certificate
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
|
|
|
# require 'drb/ssl'
|
|
|
|
# c = DRb::DRbSSLSocket::SSLConfig.new({
|
|
|
|
# :SSLCertName => [["CN" => DRb::DRbSSLSocket.getservername]]
|
|
|
|
# })
|
|
|
|
# c.setup_certificate
|
|
|
|
#
|
2003-10-04 13:08:23 -04:00
|
|
|
def initialize(config)
|
2011-05-18 17:19:18 -04:00
|
|
|
@config = config
|
2003-10-04 13:08:23 -04:00
|
|
|
@cert = config[:SSLCertificate]
|
|
|
|
@pkey = config[:SSLPrivateKey]
|
|
|
|
@ssl_ctx = nil
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# A convenience method to access the values like a Hash
|
2009-03-05 22:56:38 -05:00
|
|
|
def [](key);
|
2011-05-18 17:19:18 -04:00
|
|
|
@config[key] || DEFAULT[key]
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Connect to IO +tcp+, with context of the current certificate
|
|
|
|
# configuration
|
2003-10-04 13:08:23 -04:00
|
|
|
def connect(tcp)
|
2011-05-18 17:19:18 -04:00
|
|
|
ssl = ::OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
|
|
|
|
ssl.sync = true
|
|
|
|
ssl.connect
|
|
|
|
ssl
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Accept connection to IO +tcp+, with context of the current certificate
|
|
|
|
# configuration
|
2003-10-04 13:08:23 -04:00
|
|
|
def accept(tcp)
|
2011-05-18 17:19:18 -04:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
|
|
|
|
ssl.sync = true
|
|
|
|
ssl.accept
|
|
|
|
ssl
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Ensures that :SSLCertificate and :SSLPrivateKey have been provided
|
|
|
|
# or that a new certificate is generated with the other parameters
|
|
|
|
# provided.
|
2003-10-04 13:08:23 -04:00
|
|
|
def setup_certificate
|
|
|
|
if @cert && @pkey
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-04-23 07:15:35 -04:00
|
|
|
rsa = OpenSSL::PKey::RSA.new(1024){|p, n|
|
2011-05-18 17:19:18 -04:00
|
|
|
next unless self[:verbose]
|
|
|
|
case p
|
|
|
|
when 0; $stderr.putc "." # BN_generate_prime
|
|
|
|
when 1; $stderr.putc "+" # BN_generate_prime
|
|
|
|
when 2; $stderr.putc "*" # searching good prime,
|
2011-05-18 20:07:25 -04:00
|
|
|
# n = #of try,
|
|
|
|
# but also data from BN_generate_prime
|
2011-05-18 17:19:18 -04:00
|
|
|
when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
|
2011-05-18 20:07:25 -04:00
|
|
|
# but also data from BN_generate_prime
|
2011-05-18 17:19:18 -04:00
|
|
|
else; $stderr.putc "*" # BN_generate_prime
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = OpenSSL::X509::Certificate.new
|
|
|
|
cert.version = 3
|
|
|
|
cert.serial = 0
|
|
|
|
name = OpenSSL::X509::Name.new(self[:SSLCertName])
|
|
|
|
cert.subject = name
|
|
|
|
cert.issuer = name
|
|
|
|
cert.not_before = Time.now
|
|
|
|
cert.not_after = Time.now + (365*24*60*60)
|
|
|
|
cert.public_key = rsa.public_key
|
|
|
|
|
|
|
|
ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
|
|
|
|
cert.extensions = [
|
|
|
|
ef.create_extension("basicConstraints","CA:FALSE"),
|
|
|
|
ef.create_extension("subjectKeyIdentifier", "hash") ]
|
2011-05-18 20:07:25 -04:00
|
|
|
ef.issuer_certificate = cert
|
|
|
|
cert.add_extension(ef.create_extension("authorityKeyIdentifier",
|
|
|
|
"keyid:always,issuer:always"))
|
|
|
|
if comment = self[:SSLCertComment]
|
|
|
|
cert.add_extension(ef.create_extension("nsComment", comment))
|
|
|
|
end
|
|
|
|
cert.sign(rsa, OpenSSL::Digest::SHA1.new)
|
2011-05-18 17:19:18 -04:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
@cert = cert
|
|
|
|
@pkey = rsa
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Establish the OpenSSL::SSL::SSLContext with the configuration
|
|
|
|
# parameters provided.
|
2003-10-04 13:08:23 -04:00
|
|
|
def setup_ssl_context
|
|
|
|
ctx = ::OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.cert = @cert
|
|
|
|
ctx.key = @pkey
|
2011-05-18 17:19:18 -04:00
|
|
|
ctx.client_ca = self[:SSLClientCA]
|
|
|
|
ctx.ca_path = self[:SSLCACertificatePath]
|
|
|
|
ctx.ca_file = self[:SSLCACertificateFile]
|
2012-09-20 04:05:08 -04:00
|
|
|
ctx.tmp_dh_callback = self[:SSLTmpDhCallback]
|
2011-05-18 17:19:18 -04:00
|
|
|
ctx.verify_mode = self[:SSLVerifyMode]
|
|
|
|
ctx.verify_depth = self[:SSLVerifyDepth]
|
|
|
|
ctx.verify_callback = self[:SSLVerifyCallback]
|
2003-10-04 13:08:23 -04:00
|
|
|
ctx.cert_store = self[:SSLCertificateStore]
|
|
|
|
@ssl_ctx = ctx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Parse the dRuby +uri+ for an SSL connection.
|
|
|
|
#
|
|
|
|
# Expects drbssl://...
|
|
|
|
#
|
|
|
|
# Raises DRbBadScheme or DRbBadURI if +uri+ is not matching or malformed
|
|
|
|
def self.parse_uri(uri) # :nodoc:
|
2003-10-04 13:08:23 -04:00
|
|
|
if uri =~ /^drbssl:\/\/(.*?):(\d+)(\?(.*))?$/
|
2011-05-18 17:19:18 -04:00
|
|
|
host = $1
|
|
|
|
port = $2.to_i
|
|
|
|
option = $4
|
|
|
|
[host, port, option]
|
2003-10-04 13:08:23 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
raise(DRbBadScheme, uri) unless uri =~ /^drbssl:/
|
2011-05-18 20:07:25 -04:00
|
|
|
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Return an DRb::DRbSSLSocket instance as a client-side connection,
|
|
|
|
# with the SSL connected. This is called from DRb::start_service or while
|
|
|
|
# connecting to a remote object:
|
|
|
|
#
|
|
|
|
# DRb.start_service 'drbssl://localhost:0', front, config
|
|
|
|
#
|
|
|
|
# +uri+ is the URI we are connected to,
|
|
|
|
# <code>'drbssl://localhost:0'</code> above, +config+ is our
|
|
|
|
# configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig
|
2003-10-04 13:08:23 -04:00
|
|
|
def self.open(uri, config)
|
2010-11-08 15:59:01 -05:00
|
|
|
host, port, = parse_uri(uri)
|
2003-10-04 13:08:23 -04:00
|
|
|
host.untaint
|
|
|
|
port.untaint
|
|
|
|
soc = TCPSocket.open(host, port)
|
|
|
|
ssl_conf = SSLConfig::new(config)
|
|
|
|
ssl_conf.setup_ssl_context
|
|
|
|
ssl = ssl_conf.connect(soc)
|
|
|
|
self.new(uri, ssl, ssl_conf, true)
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Returns a DRb::DRbSSLSocket instance as a server-side connection, with
|
|
|
|
# the SSL connected. This is called from DRb::start_service or while
|
|
|
|
# connecting to a remote object:
|
|
|
|
#
|
|
|
|
# DRb.start_service 'drbssl://localhost:0', front, config
|
|
|
|
#
|
|
|
|
# +uri+ is the URI we are connected to,
|
|
|
|
# <code>'drbssl://localhost:0'</code> above, +config+ is our
|
|
|
|
# configuration. Either a Hash or DRb::DRbSSLSocket::SSLConfig
|
2003-10-04 13:08:23 -04:00
|
|
|
def self.open_server(uri, config)
|
|
|
|
uri = 'drbssl://:0' unless uri
|
2010-11-08 15:59:01 -05:00
|
|
|
host, port, = parse_uri(uri)
|
2003-10-04 13:08:23 -04:00
|
|
|
if host.size == 0
|
2004-12-15 10:46:53 -05:00
|
|
|
host = getservername
|
|
|
|
soc = open_server_inaddr_any(host, port)
|
2003-10-04 13:08:23 -04:00
|
|
|
else
|
2011-05-18 17:19:18 -04:00
|
|
|
soc = TCPServer.open(host, port)
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
|
|
|
port = soc.addr[1] if port == 0
|
|
|
|
@uri = "drbssl://#{host}:#{port}"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-10-04 13:08:23 -04:00
|
|
|
ssl_conf = SSLConfig.new(config)
|
|
|
|
ssl_conf.setup_certificate
|
|
|
|
ssl_conf.setup_ssl_context
|
|
|
|
self.new(@uri, soc, ssl_conf, false)
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# This is a convenience method to parse +uri+ and separate out any
|
|
|
|
# additional options appended in the +uri+.
|
|
|
|
#
|
|
|
|
# Returns an option-less uri and the option => [uri,option]
|
|
|
|
#
|
|
|
|
# The +config+ is completely unused, so passing nil is sufficient.
|
|
|
|
def self.uri_option(uri, config) # :nodoc:
|
2003-10-04 13:08:23 -04:00
|
|
|
host, port, option = parse_uri(uri)
|
|
|
|
return "drbssl://#{host}:#{port}", option
|
|
|
|
end
|
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Create a DRb::DRbSSLSocket instance.
|
|
|
|
#
|
|
|
|
# +uri+ is the URI we are connected to.
|
|
|
|
# +soc+ is the tcp socket we are bound to.
|
|
|
|
# +config+ is our configuration. Either a Hash or SSLConfig
|
2013-11-29 13:13:34 -05:00
|
|
|
# +is_established+ is a boolean of whether +soc+ is currently established
|
2013-01-24 23:02:46 -05:00
|
|
|
#
|
|
|
|
# This is called automatically based on the DRb protocol.
|
2003-10-04 13:08:23 -04:00
|
|
|
def initialize(uri, soc, config, is_established)
|
|
|
|
@ssl = is_established ? soc : nil
|
|
|
|
super(uri, soc.to_io, config)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Returns the SSL stream
|
|
|
|
def stream; @ssl; end # :nodoc:
|
2003-10-04 13:08:23 -04:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
# Closes the SSL stream before closing the dRuby connection.
|
|
|
|
def close # :nodoc:
|
2003-10-04 13:08:23 -04:00
|
|
|
if @ssl
|
2011-05-18 17:19:18 -04:00
|
|
|
@ssl.close
|
|
|
|
@ssl = nil
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2013-01-24 23:02:46 -05:00
|
|
|
def accept # :nodoc:
|
2005-01-22 09:10:36 -05:00
|
|
|
begin
|
2011-05-18 20:07:25 -04:00
|
|
|
while true
|
|
|
|
soc = @socket.accept
|
|
|
|
break if (@acl ? @acl.allow_socket?(soc) : true)
|
|
|
|
soc.close
|
|
|
|
end
|
2012-04-21 18:52:23 -04:00
|
|
|
begin
|
|
|
|
ssl = @config.accept(soc)
|
2013-12-13 11:11:12 -05:00
|
|
|
rescue Exception
|
|
|
|
soc.close
|
|
|
|
raise
|
2012-04-21 18:52:23 -04:00
|
|
|
end
|
2011-05-18 20:07:25 -04:00
|
|
|
self.class.new(uri, ssl, @config, true)
|
2005-01-22 09:10:36 -05:00
|
|
|
rescue OpenSSL::SSL::SSLError
|
2011-05-18 17:19:18 -04:00
|
|
|
warn("#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})") if @config[:verbose]
|
|
|
|
retry
|
2005-01-22 09:10:36 -05:00
|
|
|
end
|
2003-10-04 13:08:23 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-10-04 13:08:23 -04:00
|
|
|
DRbProtocol.add_protocol(DRbSSLSocket)
|
|
|
|
end
|