2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-07-23 12:51:36 -04:00
|
|
|
#
|
|
|
|
# https.rb -- SSL/TLS enhancement for HTTPServer
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2001 GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: https.rb,v 1.15 2003/07/22 19:20:42 gotoyuzo Exp $
|
|
|
|
|
2003-08-19 02:00:36 -04:00
|
|
|
require 'webrick/ssl'
|
2017-07-07 13:09:39 -04:00
|
|
|
require 'webrick/httpserver'
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
module Config
|
2003-08-19 02:00:36 -04:00
|
|
|
HTTP.update(SSL)
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
|
2013-01-25 20:12:54 -05:00
|
|
|
##
|
|
|
|
#--
|
|
|
|
# Adds SSL functionality to WEBrick::HTTPRequest
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class HTTPRequest
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# HTTP request SSL cipher
|
|
|
|
|
|
|
|
attr_reader :cipher
|
|
|
|
|
|
|
|
##
|
|
|
|
# HTTP request server certificate
|
|
|
|
|
|
|
|
attr_reader :server_cert
|
|
|
|
|
|
|
|
##
|
|
|
|
# HTTP request client certificate
|
|
|
|
|
|
|
|
attr_reader :client_cert
|
|
|
|
|
|
|
|
# :stopdoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
alias orig_parse parse
|
|
|
|
|
|
|
|
def parse(socket=nil)
|
2003-12-19 09:31:24 -05:00
|
|
|
if socket.respond_to?(:cert)
|
|
|
|
@server_cert = socket.cert || @config[:SSLCertificate]
|
2003-08-19 02:00:36 -04:00
|
|
|
@client_cert = socket.peer_cert
|
2003-11-04 18:48:13 -05:00
|
|
|
@client_cert_chain = socket.peer_cert_chain
|
2003-08-19 02:00:36 -04:00
|
|
|
@cipher = socket.cipher
|
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
orig_parse(socket)
|
|
|
|
end
|
|
|
|
|
|
|
|
alias orig_parse_uri parse_uri
|
|
|
|
|
|
|
|
def parse_uri(str, scheme="https")
|
2010-01-17 00:31:52 -05:00
|
|
|
if server_cert
|
2003-07-23 12:51:36 -04:00
|
|
|
return orig_parse_uri(str, scheme)
|
|
|
|
end
|
|
|
|
return orig_parse_uri(str)
|
|
|
|
end
|
2011-05-29 10:14:38 -04:00
|
|
|
private :parse_uri
|
2003-07-23 12:51:36 -04:00
|
|
|
|
|
|
|
alias orig_meta_vars meta_vars
|
|
|
|
|
|
|
|
def meta_vars
|
|
|
|
meta = orig_meta_vars
|
2010-01-17 00:31:52 -05:00
|
|
|
if server_cert
|
2003-07-23 12:51:36 -04:00
|
|
|
meta["HTTPS"] = "on"
|
2003-08-19 02:00:36 -04:00
|
|
|
meta["SSL_SERVER_CERT"] = @server_cert.to_pem
|
2003-07-23 12:51:36 -04:00
|
|
|
meta["SSL_CLIENT_CERT"] = @client_cert ? @client_cert.to_pem : ""
|
2003-11-04 18:48:13 -05:00
|
|
|
if @client_cert_chain
|
|
|
|
@client_cert_chain.each_with_index{|cert, i|
|
|
|
|
meta["SSL_CLIENT_CERT_CHAIN_#{i}"] = cert.to_pem
|
|
|
|
}
|
|
|
|
end
|
2003-08-19 02:00:36 -04:00
|
|
|
meta["SSL_CIPHER"] = @cipher[0]
|
2003-12-22 16:13:06 -05:00
|
|
|
meta["SSL_PROTOCOL"] = @cipher[1]
|
|
|
|
meta["SSL_CIPHER_USEKEYSIZE"] = @cipher[2].to_s
|
|
|
|
meta["SSL_CIPHER_ALGKEYSIZE"] = @cipher[3].to_s
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
meta
|
|
|
|
end
|
2013-01-25 20:12:54 -05:00
|
|
|
|
|
|
|
# :startdoc:
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
2017-07-07 13:09:39 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
#--
|
|
|
|
# Fake WEBrick::HTTPRequest for lookup_server
|
|
|
|
|
|
|
|
class SNIRequest
|
|
|
|
|
|
|
|
##
|
|
|
|
# The SNI hostname
|
|
|
|
|
|
|
|
attr_reader :host
|
|
|
|
|
|
|
|
##
|
|
|
|
# The socket address of the server
|
|
|
|
|
|
|
|
attr_reader :addr
|
|
|
|
|
|
|
|
##
|
|
|
|
# The port this request is for
|
|
|
|
|
|
|
|
attr_reader :port
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new SNIRequest.
|
|
|
|
|
|
|
|
def initialize(sslsocket, hostname)
|
|
|
|
@host = hostname
|
|
|
|
@addr = sslsocket.addr
|
|
|
|
@port = @addr[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
#--
|
|
|
|
# Adds SSL functionality to WEBrick::HTTPServer
|
|
|
|
|
|
|
|
class HTTPServer < ::WEBrick::GenericServer
|
|
|
|
##
|
|
|
|
# ServerNameIndication callback
|
|
|
|
|
|
|
|
def ssl_servername_callback(sslsocket, hostname = nil)
|
|
|
|
req = SNIRequest.new(sslsocket, hostname)
|
|
|
|
server = lookup_server(req)
|
|
|
|
server ? server.ssl_context : nil
|
|
|
|
end
|
2017-07-17 21:59:28 -04:00
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Check whether +server+ is also SSL server.
|
|
|
|
# Also +server+'s SSL context will be created.
|
|
|
|
|
|
|
|
alias orig_virtual_host virtual_host
|
|
|
|
|
|
|
|
def virtual_host(server)
|
|
|
|
if @config[:SSLEnable] && !server.ssl_context
|
|
|
|
raise ArgumentError, "virtual host must set SSLEnable to true"
|
|
|
|
end
|
|
|
|
orig_virtual_host(server)
|
|
|
|
end
|
|
|
|
|
|
|
|
# :startdoc:
|
2017-07-07 13:09:39 -04:00
|
|
|
end
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|