2003-10-04 21:08:23 +04:00
|
|
|
require 'socket'
|
|
|
|
require 'openssl'
|
|
|
|
require 'drb/drb'
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
module DRb
|
|
|
|
|
2013-01-25 07:25:39 +04: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 21:08:23 +04:00
|
|
|
class DRbSSLSocket < DRbTCPSocket
|
|
|
|
|
2013-01-25 07:25:39 +04:00
|
|
|
# :stopdoc:
|
2003-10-04 21:08:23 +04:00
|
|
|
class SSLConfig
|
|
|
|
|
|
|
|
DEFAULT = {
|
2011-05-19 01:19:18 +04:00
|
|
|
:SSLCertificate => nil,
|
|
|
|
:SSLPrivateKey => nil,
|
|
|
|
:SSLClientCA => nil,
|
|
|
|
:SSLCACertificatePath => nil,
|
|
|
|
:SSLCACertificateFile => nil,
|
2012-09-20 12:05:08 +04:00
|
|
|
:SSLTmpDhCallback => nil,
|
2011-05-19 01:19:18 +04:00
|
|
|
:SSLVerifyMode => ::OpenSSL::SSL::VERIFY_NONE,
|
|
|
|
:SSLVerifyDepth => nil,
|
|
|
|
:SSLVerifyCallback => nil, # custom verification
|
2003-10-04 21:08:23 +04:00
|
|
|
:SSLCertificateStore => nil,
|
2011-05-19 01: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 21:08:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
def initialize(config)
|
2011-05-19 01:19:18 +04:00
|
|
|
@config = config
|
2003-10-04 21:08:23 +04:00
|
|
|
@cert = config[:SSLCertificate]
|
|
|
|
@pkey = config[:SSLPrivateKey]
|
|
|
|
@ssl_ctx = nil
|
|
|
|
end
|
|
|
|
|
2009-03-06 06:56:38 +03:00
|
|
|
def [](key);
|
2011-05-19 01:19:18 +04:00
|
|
|
@config[key] || DEFAULT[key]
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def connect(tcp)
|
2011-05-19 01:19:18 +04:00
|
|
|
ssl = ::OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
|
|
|
|
ssl.sync = true
|
|
|
|
ssl.connect
|
|
|
|
ssl
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-10-04 21:08:23 +04:00
|
|
|
def accept(tcp)
|
2011-05-19 01:19:18 +04:00
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
|
|
|
|
ssl.sync = true
|
|
|
|
ssl.accept
|
|
|
|
ssl
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-10-04 21:08:23 +04:00
|
|
|
def setup_certificate
|
|
|
|
if @cert && @pkey
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-04-23 15:15:35 +04:00
|
|
|
rsa = OpenSSL::PKey::RSA.new(1024){|p, n|
|
2011-05-19 01: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-19 04:07:25 +04:00
|
|
|
# n = #of try,
|
|
|
|
# but also data from BN_generate_prime
|
2011-05-19 01:19:18 +04:00
|
|
|
when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
|
2011-05-19 04:07:25 +04:00
|
|
|
# but also data from BN_generate_prime
|
2011-05-19 01: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-19 04: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-19 01:19:18 +04:00
|
|
|
|
2011-05-19 04:07:25 +04:00
|
|
|
@cert = cert
|
|
|
|
@pkey = rsa
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup_ssl_context
|
|
|
|
ctx = ::OpenSSL::SSL::SSLContext.new
|
|
|
|
ctx.cert = @cert
|
|
|
|
ctx.key = @pkey
|
2011-05-19 01:19:18 +04:00
|
|
|
ctx.client_ca = self[:SSLClientCA]
|
|
|
|
ctx.ca_path = self[:SSLCACertificatePath]
|
|
|
|
ctx.ca_file = self[:SSLCACertificateFile]
|
2012-09-20 12:05:08 +04:00
|
|
|
ctx.tmp_dh_callback = self[:SSLTmpDhCallback]
|
2011-05-19 01:19:18 +04:00
|
|
|
ctx.verify_mode = self[:SSLVerifyMode]
|
|
|
|
ctx.verify_depth = self[:SSLVerifyDepth]
|
|
|
|
ctx.verify_callback = self[:SSLVerifyCallback]
|
2003-10-04 21:08:23 +04:00
|
|
|
ctx.cert_store = self[:SSLCertificateStore]
|
|
|
|
@ssl_ctx = ctx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse_uri(uri)
|
|
|
|
if uri =~ /^drbssl:\/\/(.*?):(\d+)(\?(.*))?$/
|
2011-05-19 01:19:18 +04:00
|
|
|
host = $1
|
|
|
|
port = $2.to_i
|
|
|
|
option = $4
|
|
|
|
[host, port, option]
|
2003-10-04 21:08:23 +04:00
|
|
|
else
|
2011-05-19 01:19:18 +04:00
|
|
|
raise(DRbBadScheme, uri) unless uri =~ /^drbssl:/
|
2011-05-19 04:07:25 +04:00
|
|
|
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.open(uri, config)
|
2010-11-08 23:59:01 +03:00
|
|
|
host, port, = parse_uri(uri)
|
2003-10-04 21: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
|
|
|
|
|
|
|
|
def self.open_server(uri, config)
|
|
|
|
uri = 'drbssl://:0' unless uri
|
2010-11-08 23:59:01 +03:00
|
|
|
host, port, = parse_uri(uri)
|
2003-10-04 21:08:23 +04:00
|
|
|
if host.size == 0
|
2004-12-15 18:46:53 +03:00
|
|
|
host = getservername
|
|
|
|
soc = open_server_inaddr_any(host, port)
|
2003-10-04 21:08:23 +04:00
|
|
|
else
|
2011-05-19 01:19:18 +04:00
|
|
|
soc = TCPServer.open(host, port)
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
|
|
|
port = soc.addr[1] if port == 0
|
|
|
|
@uri = "drbssl://#{host}:#{port}"
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-10-04 21: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
|
|
|
|
|
|
|
|
def self.uri_option(uri, config)
|
|
|
|
host, port, option = parse_uri(uri)
|
|
|
|
return "drbssl://#{host}:#{port}", option
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(uri, soc, config, is_established)
|
|
|
|
@ssl = is_established ? soc : nil
|
|
|
|
super(uri, soc.to_io, config)
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-10-04 21:08:23 +04:00
|
|
|
def stream; @ssl; end
|
|
|
|
|
|
|
|
def close
|
|
|
|
if @ssl
|
2011-05-19 01:19:18 +04:00
|
|
|
@ssl.close
|
|
|
|
@ssl = nil
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-10-04 21:08:23 +04:00
|
|
|
def accept
|
2005-01-22 17:10:36 +03:00
|
|
|
begin
|
2011-05-19 04:07:25 +04:00
|
|
|
while true
|
|
|
|
soc = @socket.accept
|
|
|
|
break if (@acl ? @acl.allow_socket?(soc) : true)
|
|
|
|
soc.close
|
|
|
|
end
|
2012-04-22 02:52:23 +04:00
|
|
|
begin
|
|
|
|
ssl = @config.accept(soc)
|
|
|
|
ensure
|
|
|
|
soc.close if $!
|
|
|
|
end
|
2011-05-19 04:07:25 +04:00
|
|
|
self.class.new(uri, ssl, @config, true)
|
2005-01-22 17:10:36 +03:00
|
|
|
rescue OpenSSL::SSL::SSLError
|
2011-05-19 01:19:18 +04:00
|
|
|
warn("#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})") if @config[:verbose]
|
|
|
|
retry
|
2005-01-22 17:10:36 +03:00
|
|
|
end
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
2013-01-25 07:25:39 +04:00
|
|
|
|
|
|
|
# :stopdoc:
|
2003-10-04 21:08:23 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2003-10-04 21:08:23 +04:00
|
|
|
DRbProtocol.add_protocol(DRbSSLSocket)
|
|
|
|
end
|