Skip to content

Commit 72fb594

Browse files
author
Stephen von Takach
committed
Add client support for SNI
1 parent 1248185 commit 72fb594

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/ruby-tls/ssl.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'ffi'
44
require 'ffi-compiler/loader'
55
require 'thread'
6-
require 'thread_safe'
6+
require 'concurrent'
77

88

99
module RubyTls
@@ -141,6 +141,14 @@ def self.SSL_CTX_sess_set_cache_size(ssl_ctx, op)
141141
SSL_CTX_ctrl(ssl_ctx, SSL_CTRL_SET_SESS_CACHE_SIZE, op, nil)
142142
end
143143

144+
attach_function :SSL_ctrl, [:ssl, :int, :long, :pointer], :long
145+
SSL_CTRL_SET_TLSEXT_HOSTNAME = 55
146+
TLSEXT_NAMETYPE_host_name = 0
147+
def self.SSL_set_tlsext_host_name(ssl, host_name)
148+
name = FFI::MemoryPointer.from_string(host_name)
149+
SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, name)
150+
end
151+
144152
attach_function :SSL_CTX_use_PrivateKey_file, [:ssl_ctx, :string, :int], :int, :blocking => true
145153
attach_function :SSL_CTX_use_PrivateKey, [:ssl_ctx, :pointer], :int
146154
attach_function :ERR_print_errors_fp, [:pointer], :void # Pointer == File Handle
@@ -295,7 +303,7 @@ class Context
295303
SESSION = 'ruby-tls'
296304

297305

298-
ALPN_LOOKUP = ThreadSafe::Cache.new
306+
ALPN_LOOKUP = ::Concurrent::Map.new
299307
ALPN_Select_CB = FFI::Function.new(:int, [
300308
# array of str, unit8 out,uint8 in, *arg
301309
:pointer, :pointer, :pointer, :string, :uint, :pointer
@@ -429,7 +437,7 @@ def set_client_ca(ca)
429437

430438

431439
class Box
432-
InstanceLookup = ThreadSafe::Cache.new
440+
InstanceLookup = ::Concurrent::Map.new
433441

434442
READ_BUFFER = 2048
435443

@@ -461,6 +469,12 @@ def initialize(server, transport, options = {})
461469
SSL.SSL_set_verify(@ssl, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, VerifyCB)
462470
end
463471

472+
# Add Server Name Indication (SNI) for client connections
473+
# TODO:: Server support for SNI
474+
if !server && options[:host_name]
475+
SSL.SSL_set_tlsext_host_name(@ssl, options[:host_name])
476+
end
477+
464478
SSL.SSL_connect(@ssl) unless server
465479
end
466480

spec/verify_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Client2
77
def initialize(client_data, dir)
88
@client_data = client_data
9-
@ssl = RubyTls::SSL::Box.new(false, self, private_key: dir + 'client.key', cert_chain: dir + 'client.crt')
9+
@ssl = RubyTls::SSL::Box.new(false, self, private_key: dir + 'client.key', cert_chain: dir + 'client.crt', host_name: 'just.testing.com')
1010
end
1111

1212
attr_reader :ssl

0 commit comments

Comments
 (0)