Skip to content

Commit

Permalink
Get SNI for OpenSSL (crystal-lang#7291)
Browse files Browse the repository at this point in the history
  • Loading branch information
bararchy authored and Urde Graven committed Jan 12, 2019
1 parent fc07025 commit d01745a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/std/openssl/ssl/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,21 @@ describe OpenSSL::SSL::Server do
end
end
end

it "detects SNI hostname" do
tcp_server = TCPServer.new(0)
server_context, client_context = ssl_context_pair

OpenSSL::SSL::Server.open tcp_server, server_context do |server|
spawn do
sleep 1
OpenSSL::SSL::Socket::Client.open(TCPSocket.new(tcp_server.local_address.address, tcp_server.local_address.port), client_context, hostname: "example.com") do |socket|
end
end

client = server.accept
client.hostname.should eq("example.com")
client.close
end
end
end
1 change: 1 addition & 0 deletions src/openssl/lib_ssl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ lib LibSSL
fun tlsv1_2_method = TLSv1_2_method : SSLMethod

fun ssl_get_error = SSL_get_error(handle : SSL, ret : Int) : SSLError
fun ssl_get_servername = SSL_get_servername(ssl : SSL, host_type : TLSExt) : UInt8*
fun ssl_set_bio = SSL_set_bio(handle : SSL, rbio : LibCrypto::Bio*, wbio : LibCrypto::Bio*)
fun ssl_select_next_proto = SSL_select_next_proto(output : Char**, output_len : Char*, input : Char*, input_len : Int, client : Char*, client_len : Int) : Int
fun ssl_ctrl = SSL_ctrl(handle : SSL, cmd : Int, larg : Long, parg : Void*) : Long
Expand Down
7 changes: 7 additions & 0 deletions src/openssl/ssl/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,11 @@ abstract class OpenSSL::SSL::Socket < IO
def unbuffered_rewind
raise IO::Error.new("Can't rewind OpenSSL::SSL::Socket::Client")
end

# Returns the hostname provided through Server Name Indication (SNI)
def hostname : String?
if host_name = LibSSL.ssl_get_servername(@ssl, LibSSL::TLSExt::NAMETYPE_host_name)
String.new(host_name)
end
end
end

0 comments on commit d01745a

Please sign in to comment.