8 class DRbSSLSocket < DRbTCPSocket
13 :SSLCertificate => nil,
14 :SSLPrivateKey => nil,
16 :SSLCACertificatePath => nil,
17 :SSLCACertificateFile => nil,
18 :SSLVerifyMode => ::OpenSSL::SSL::VERIFY_NONE,
19 :SSLVerifyDepth => nil,
20 :SSLVerifyCallback => nil, # custom verification
21 :SSLCertificateStore => nil,
22 # Must specify if you use auto generated certificate.
23 :SSLCertName => nil, # e.g. [["CN","fqdn.example.com"]]
24 :SSLCertComment => "Generated by Ruby/OpenSSL"
27 def initialize(config)
29 @cert = config[:SSLCertificate]
30 @pkey = config[:SSLPrivateKey]
35 @config[key] || DEFAULT[key]
39 ssl = ::OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
46 ssl = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_ctx)
57 rsa = OpenSSL::PKey::RSA.new(512){|p, n|
58 next unless self[:verbose]
60 when 0; $stderr.putc "." # BN_generate_prime
61 when 1; $stderr.putc "+" # BN_generate_prime
62 when 2; $stderr.putc "*" # searching good prime,
64 # but also data from BN_generate_prime
65 when 3; $stderr.putc "\n" # found good prime, n==0 - p, n==1 - q,
66 # but also data from BN_generate_prime
67 else; $stderr.putc "*" # BN_generate_prime
71 cert = OpenSSL::X509::Certificate.new
74 name = OpenSSL::X509::Name.new(self[:SSLCertName])
77 cert.not_before = Time.now
78 cert.not_after = Time.now + (365*24*60*60)
79 cert.public_key = rsa.public_key
81 ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
83 ef.create_extension("basicConstraints","CA:FALSE"),
84 ef.create_extension("subjectKeyIdentifier", "hash") ]
85 ef.issuer_certificate = cert
86 cert.add_extension(ef.create_extension("authorityKeyIdentifier",
87 "keyid:always,issuer:always"))
88 if comment = self[:SSLCertComment]
89 cert.add_extension(ef.create_extension("nsComment", comment))
91 cert.sign(rsa, OpenSSL::Digest::SHA1.new)
98 ctx = ::OpenSSL::SSL::SSLContext.new
101 ctx.client_ca = self[:SSLClientCA]
102 ctx.ca_path = self[:SSLCACertificatePath]
103 ctx.ca_file = self[:SSLCACertificateFile]
104 ctx.verify_mode = self[:SSLVerifyMode]
105 ctx.verify_depth = self[:SSLVerifyDepth]
106 ctx.verify_callback = self[:SSLVerifyCallback]
107 ctx.cert_store = self[:SSLCertificateStore]
112 def self.parse_uri(uri)
113 if uri =~ /^drbssl:\/\/(.*?):(\d+)(\?(.*))?$/
119 raise(DRbBadScheme, uri) unless uri =~ /^drbssl:/
120 raise(DRbBadURI, 'can\'t parse uri:' + uri)
124 def self.open(uri, config)
125 host, port, option = parse_uri(uri)
128 soc = TCPSocket.open(host, port)
129 ssl_conf = SSLConfig::new(config)
130 ssl_conf.setup_ssl_context
131 ssl = ssl_conf.connect(soc)
132 self.new(uri, ssl, ssl_conf, true)
135 def self.open_server(uri, config)
136 uri = 'drbssl://:0' unless uri
137 host, port, opt = parse_uri(uri)
140 soc = open_server_inaddr_any(host, port)
142 soc = TCPServer.open(host, port)
144 port = soc.addr[1] if port == 0
145 @uri = "drbssl://#{host}:#{port}"
147 ssl_conf = SSLConfig.new(config)
148 ssl_conf.setup_certificate
149 ssl_conf.setup_ssl_context
150 self.new(@uri, soc, ssl_conf, false)
153 def self.uri_option(uri, config)
154 host, port, option = parse_uri(uri)
155 return "drbssl://#{host}:#{port}", option
158 def initialize(uri, soc, config, is_established)
159 @ssl = is_established ? soc : nil
160 super(uri, soc.to_io, config)
163 def stream; @ssl; end
177 break if (@acl ? @acl.allow_socket?(soc) : true)
180 ssl = @config.accept(soc)
181 self.class.new(uri, ssl, @config, true)
182 rescue OpenSSL::SSL::SSLError
183 warn("#{__FILE__}:#{__LINE__}: warning: #{$!.message} (#{$!.class})") if @config[:verbose]
189 DRbProtocol.add_protocol(DRbSSLSocket)