Skip to content

Commit ca515e8

Browse files
committed
tls server: allow requesting a client-cert
1 parent 0df4c66 commit ca515e8

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/ruby-tls/ssl.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ def self.SSL_CTX_sess_set_cache_size(ssl_ctx, op)
146146
attach_function :SSL_CTX_use_certificate, [:ssl_ctx, :x509], :int
147147
attach_function :SSL_CTX_set_cipher_list, [:ssl_ctx, :string], :int
148148
attach_function :SSL_CTX_set_session_id_context, [:ssl_ctx, :string, :buffer_length], :int
149+
attach_function :SSL_load_client_CA_file, [:string], :pointer
150+
attach_function :SSL_CTX_set_client_CA_list, [:ssl_ctx, :pointer], :void
149151

150152
# OpenSSL before 1.0.2 do not have these methods
151153
begin
@@ -322,6 +324,7 @@ def initialize(server, options = {})
322324
if @is_server
323325
set_private_key(options[:private_key] || SSL::DEFAULT_PRIVATE)
324326
set_certificate(options[:cert_chain] || SSL::DEFAULT_CERT)
327+
set_client_ca(options[:client_ca])
325328
end
326329

327330
SSL.SSL_CTX_set_cipher_list(@ssl_ctx, options[:ciphers] || CIPHERS)
@@ -406,6 +409,18 @@ def set_certificate(cert)
406409
raise 'invalid certificate or file not found'
407410
end
408411
end
412+
413+
def set_client_ca(ca)
414+
return unless ca
415+
416+
if File.file?(ca) && (ca_ptr = SSL.SSL_load_client_CA_file(ca))
417+
# there is no error checking provided by SSL_CTX_set_client_CA_list
418+
SSL.SSL_CTX_set_client_CA_list(@ssl_ctx, ca_ptr)
419+
else
420+
cleanup
421+
raise 'invalid ca certificate or file not found'
422+
end
423+
end
409424
end
410425

411426

0 commit comments

Comments
 (0)