ruby/ext/openssl/lib/openssl/ssl.rb
gotoyuzo a579d8bf68 * ext/openssl/ossl_ssl.c (ossl_ssl_read, ossl_ssl_write): should
call rb_sys_fail instead of rasing SSLError if SSL_ERROR_SYSCALL
  occured.

* ext/openssl/lib/openssl/buffering.rb (Buffering#fill_rbuff):
  should rescue Errno::EAGAIN.

* ext/openssl/lib/openssl/buffering.rb (Buffering#each): fix typo.
  suggested by Brian Ollenberger.

* ext/openssl/lib/openssl/ssl.rb: set non-blocking flag to the
  underlying IO.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7974 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-02-15 17:50:47 +00:00

129 lines
2.7 KiB
Ruby

=begin
= $RCSfile$ -- Ruby-space definitions that completes C-space funcs for SSL
= Info
'OpenSSL for Ruby 2' project
Copyright (C) 2001 GOTOU YUUZOU <gotoyuzo@notwork.org>
All rights reserved.
= Licence
This program is licenced under the same licence as Ruby.
(See the file 'LICENCE'.)
= Version
$Id$
=end
require "openssl"
require "openssl/buffering"
require "fcntl"
module OpenSSL
module SSL
module SocketForwarder
def addr
to_io.addr
end
def peeraddr
to_io.peeraddr
end
def getsockopt(level, optname, optval)
to_io.setsockopt(level, optname, optval)
end
def setsockopt(level, optname)
to_io.setsockopt(level, optname)
end
def fcntl(*args)
to_io.fcntl(*args)
end
def closed?
to_io.closed?
end
def do_not_reverse_lookup=(flag)
to_io.do_not_reverse_lookup = flag
end
end
module Nonblock
def initialize(*args)
flag = @io.fcntl(Fcntl::F_GETFL) | File::NONBLOCK
@io.fcntl(Fcntl::F_SETFL, flag)
super
end
end
class SSLSocket
include Buffering
include SocketForwarder
include Nonblock
def post_connection_check(hostname)
check_common_name = true
cert = peer_cert
cert.extensions.each{|ext|
next if ext.oid != "subjectAltName"
ext.value.split(/,\s+/).each{|general_name|
if /\ADNS:(.*)/ =~ general_name
check_common_name = false
reg = Regexp.escape($1).gsub(/\\\*/, "[^.]+")
return true if /\A#{reg}\z/i =~ hostname
elsif /\AIP Address:(.*)/ =~ general_name
check_common_name = false
return true if $1 == hostname
end
}
}
if check_common_name
cert.subject.to_a.each{|oid, value|
if oid == "CN" && value.casecmp(hostname) == 0
return true
end
}
end
raise SSLError, "hostname not match"
end
end
class SSLServer
include SocketForwarder
attr_accessor :start_immediately
def initialize(svr, ctx)
@svr = svr
@ctx = ctx
@start_immediately = true
end
def to_io
@svr
end
def listen(backlog=5)
@svr.listen(backlog)
end
def accept
sock = @svr.accept
begin
ssl = OpenSSL::SSL::SSLSocket.new(sock, @ctx)
ssl.sync_close = true
ssl.accept if @start_immediately
ssl
rescue SSLError => ex
sock.close
raise ex
end
end
def close
@svr.close
end
end
end
end